at32f415_adc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /**
  2. **************************************************************************
  3. * @file at32f415_adc.c
  4. * @brief contains all the functions for the adc firmware library
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. #include "at32f415_conf.h"
  25. /** @addtogroup AT32F415_periph_driver
  26. * @{
  27. */
  28. /** @defgroup ADC
  29. * @brief ADC driver modules
  30. * @{
  31. */
  32. #ifdef ADC_MODULE_ENABLED
  33. /** @defgroup ADC_private_functions
  34. * @{
  35. */
  36. /**
  37. * @brief deinitialize the adc peripheral registers to their default reset values.
  38. * @param adc_x: select the adc peripheral.
  39. * this parameter can be one of the following values:
  40. * ADC1.
  41. * @retval none
  42. */
  43. void adc_reset(adc_type *adc_x)
  44. {
  45. if(adc_x == ADC1)
  46. {
  47. crm_periph_reset(CRM_ADC1_PERIPH_RESET, TRUE);
  48. crm_periph_reset(CRM_ADC1_PERIPH_RESET, FALSE);
  49. }
  50. }
  51. /**
  52. * @brief enable or disable the specified adc peripheral.
  53. * @param adc_x: select the adc peripheral.
  54. * this parameter can be one of the following values:
  55. * ADC1.
  56. * @param new_state: new state of a/d converter.
  57. * this parameter can be: TRUE or FALSE.
  58. * note:after adc ready,user set adcen bit will cause ordinary conversion
  59. * @retval none
  60. */
  61. void adc_enable(adc_type *adc_x, confirm_state new_state)
  62. {
  63. adc_x->ctrl2_bit.adcen = new_state;
  64. }
  65. /**
  66. * @brief adc base default para init.
  67. * @param sequence_mode: set the state of adc sequence mode.
  68. * this parameter can be:TRUE or FALSE
  69. * @param repeat_mode: set the state of adc repeat conversion mode.
  70. * this parameter can be:TRUE or FALSE
  71. * @param data_align: set the state of adc data alignment.
  72. * this parameter can be one of the following values:
  73. * - ADC_RIGHT_ALIGNMENT
  74. * - ADC_LEFT_ALIGNMENT
  75. * @param ordinary_channel_length: configure the adc ordinary channel sequence length.
  76. * this parameter can be:
  77. * - (0x1~0x10)
  78. * @retval none
  79. */
  80. void adc_base_default_para_init(adc_base_config_type *adc_base_struct)
  81. {
  82. adc_base_struct->sequence_mode = FALSE;
  83. adc_base_struct->repeat_mode = FALSE;
  84. adc_base_struct->data_align = ADC_RIGHT_ALIGNMENT;
  85. adc_base_struct->ordinary_channel_length = 1;
  86. }
  87. /**
  88. * @brief initialize the adc peripheral according to the specified parameters.
  89. * @param adc_x: select the adc peripheral.
  90. * this parameter can be one of the following values:
  91. * ADC1.
  92. * @param sequence_mode: set the state of adc sequence mode.
  93. * this parameter can be:TRUE or FALSE
  94. * @param repeat_mode: set the state of adc repeat conversion mode.
  95. * this parameter can be:TRUE or FALSE
  96. * @param data_align: set the state of adc data alignment.
  97. * this parameter can be one of the following values:
  98. * - ADC_RIGHT_ALIGNMENT
  99. * - ADC_LEFT_ALIGNMENT
  100. * @param ordinary_channel_length: configure the adc ordinary channel sequence length.
  101. * this parameter can be:
  102. * - (0x1~0xf)
  103. * @retval none
  104. */
  105. void adc_base_config(adc_type *adc_x, adc_base_config_type *adc_base_struct)
  106. {
  107. adc_x->ctrl1_bit.sqen = adc_base_struct->sequence_mode;
  108. adc_x->ctrl2_bit.rpen = adc_base_struct->repeat_mode;
  109. adc_x->ctrl2_bit.dtalign = adc_base_struct->data_align;
  110. adc_x->osq1_bit.oclen = adc_base_struct->ordinary_channel_length - 1;
  111. }
  112. /**
  113. * @brief enable or disable the adc dma transfer.
  114. * @param adc_x: select the adc peripheral.
  115. * this parameter can be one of the following values:
  116. * ADC1.
  117. * @param new_state: new state of the adc dma transfer.
  118. * this parameter can be: TRUE or FALSE.
  119. * @retval none
  120. */
  121. void adc_dma_mode_enable(adc_type *adc_x, confirm_state new_state)
  122. {
  123. adc_x->ctrl2_bit.ocdmaen = new_state;
  124. }
  125. /**
  126. * @brief enable or disable the specified adc interrupts.
  127. * @param adc_x: select the adc peripheral.
  128. * this parameter can be one of the following values:
  129. * ADC1.
  130. * @param adc_int: specifies the adc interrupt sources to be enabled or disabled.
  131. * this parameter can be one of the following values:
  132. * - ADC_VMOR_INT
  133. * - ADC_CCE_INT
  134. * - ADC_PCCE_INT
  135. * @param new_state: new state of the specified adc interrupts.
  136. * this parameter can be: TRUE or FALSE.
  137. * @retval none
  138. */
  139. void adc_interrupt_enable(adc_type *adc_x, uint32_t adc_int, confirm_state new_state)
  140. {
  141. if(new_state == TRUE)
  142. {
  143. adc_x->ctrl1 |= adc_int;
  144. }
  145. else if(new_state == FALSE)
  146. {
  147. adc_x->ctrl1 &= ~adc_int;
  148. }
  149. }
  150. /**
  151. * @brief initialize calibration register of the specified adc peripheral.
  152. * @param adc_x: select the adc peripheral.
  153. * this parameter can be one of the following values:
  154. * ADC1.
  155. * @retval none
  156. */
  157. void adc_calibration_init(adc_type *adc_x)
  158. {
  159. adc_x->ctrl2_bit.adcalinit = TRUE;
  160. }
  161. /**
  162. * @brief get calibration register's initialize status of the specified adc peripheral.
  163. * @param adc_x: select the adc peripheral.
  164. * this parameter can be one of the following values:
  165. * ADC1.
  166. * @retval the new state of reset calibration register status(SET or RESET).
  167. */
  168. flag_status adc_calibration_init_status_get(adc_type *adc_x)
  169. {
  170. if(adc_x->ctrl2_bit.adcalinit)
  171. {
  172. return SET;
  173. }
  174. else
  175. {
  176. return RESET;
  177. }
  178. }
  179. /**
  180. * @brief start calibration process of the specified adc peripheral.
  181. * @param adc_x: select the adc peripheral.
  182. * this parameter can be one of the following values:
  183. * ADC1.
  184. * @retval none
  185. */
  186. void adc_calibration_start(adc_type *adc_x)
  187. {
  188. adc_x->ctrl2_bit.adcal = TRUE;
  189. }
  190. /**
  191. * @brief get calibration status of the specified adc peripheral.
  192. * @param adc_x: select the adc peripheral.
  193. * this parameter can be one of the following values:
  194. * ADC1.
  195. * @retval the new state of calibration status(SET or RESET).
  196. */
  197. flag_status adc_calibration_status_get(adc_type *adc_x)
  198. {
  199. if(adc_x->ctrl2_bit.adcal)
  200. {
  201. return SET;
  202. }
  203. else
  204. {
  205. return RESET;
  206. }
  207. }
  208. /**
  209. * @brief enable or disable the voltage monitoring on single/all ordinary or preempt channels of the specified adc peripheral.
  210. * @param adc_x: select the adc peripheral.
  211. * this parameter can be one of the following values:
  212. * ADC1.
  213. * @param adc_voltage_monitoring: choose the adc_voltage_monitoring config.
  214. * this parameter can be one of the following values:
  215. * - ADC_VMONITOR_SINGLE_ORDINARY
  216. * - ADC_VMONITOR_SINGLE_PREEMPT
  217. * - ADC_VMONITOR_SINGLE_ORDINARY_PREEMPT
  218. * - ADC_VMONITOR_ALL_ORDINARY
  219. * - ADC_VMONITOR_ALL_PREEMPT
  220. * - ADC_VMONITOR_ALL_ORDINARY_PREEMPT
  221. * - ADC_VMONITOR_NONE
  222. * @retval none
  223. */
  224. void adc_voltage_monitor_enable(adc_type *adc_x, adc_voltage_monitoring_type adc_voltage_monitoring)
  225. {
  226. adc_x->ctrl1_bit.ocvmen = FALSE;
  227. adc_x->ctrl1_bit.pcvmen = FALSE;
  228. adc_x->ctrl1_bit.vmsgen = FALSE;
  229. adc_x->ctrl1 |= adc_voltage_monitoring;
  230. }
  231. /**
  232. * @brief set voltage monitoring's high and low thresholds value of the specified adc peripheral.
  233. * @param adc_x: select the adc peripheral.
  234. * this parameter can be one of the following values:
  235. * ADC1.
  236. * @param adc_high_threshold: voltage monitoring's high thresholds value.
  237. * this parameter can be:
  238. * - (0x000~0xFFF)
  239. * @param adc_low_threshold: voltage monitoring's low thresholds value.
  240. * this parameter can be:
  241. * - (0x000~0xFFF)
  242. * @retval none
  243. */
  244. void adc_voltage_monitor_threshold_value_set(adc_type *adc_x, uint16_t adc_high_threshold, uint16_t adc_low_threshold)
  245. {
  246. adc_x->vmhb_bit.vmhb = adc_high_threshold;
  247. adc_x->vmlb_bit.vmlb = adc_low_threshold;
  248. }
  249. /**
  250. * @brief select the voltage monitoring's channel of the specified adc peripheral.
  251. * @param adc_x: select the adc peripheral.
  252. * this parameter can be one of the following values:
  253. * ADC1.
  254. * @param adc_channel: select the channel.
  255. * this parameter can be one of the following values:
  256. * - ADC_CHANNEL_0 - ADC_CHANNEL_1 - ADC_CHANNEL_2 - ADC_CHANNEL_3
  257. * - ADC_CHANNEL_4 - ADC_CHANNEL_5 - ADC_CHANNEL_6 - ADC_CHANNEL_7
  258. * - ADC_CHANNEL_8 - ADC_CHANNEL_9 - ADC_CHANNEL_10 - ADC_CHANNEL_11
  259. * - ADC_CHANNEL_12 - ADC_CHANNEL_13 - ADC_CHANNEL_14 - ADC_CHANNEL_15
  260. * - ADC_CHANNEL_16 - ADC_CHANNEL_17
  261. * @retval none
  262. */
  263. void adc_voltage_monitor_single_channel_select(adc_type *adc_x, adc_channel_select_type adc_channel)
  264. {
  265. adc_x->ctrl1_bit.vmcsel = adc_channel;
  266. }
  267. /**
  268. * @brief set ordinary channel's corresponding rank in the sequencer and sample time of the specified adc peripheral.
  269. * @param adc_x: select the adc peripheral.
  270. * this parameter can be one of the following values:
  271. * ADC1.
  272. * @param adc_channel: select the channel.
  273. * this parameter can be one of the following values:
  274. * - ADC_CHANNEL_0 - ADC_CHANNEL_1 - ADC_CHANNEL_2 - ADC_CHANNEL_3
  275. * - ADC_CHANNEL_4 - ADC_CHANNEL_5 - ADC_CHANNEL_6 - ADC_CHANNEL_7
  276. * - ADC_CHANNEL_8 - ADC_CHANNEL_9 - ADC_CHANNEL_10 - ADC_CHANNEL_11
  277. * - ADC_CHANNEL_12 - ADC_CHANNEL_13 - ADC_CHANNEL_14 - ADC_CHANNEL_15
  278. * - ADC_CHANNEL_16 - ADC_CHANNEL_17
  279. * @param adc_sequence: set rank in the ordinary group sequencer.
  280. * this parameter must be:
  281. * - between 1 to 16
  282. * @param adc_sampletime: set the sampletime of adc channel.
  283. * this parameter can be one of the following values:
  284. * - ADC_SAMPLETIME_1_5
  285. * - ADC_SAMPLETIME_7_5
  286. * - ADC_SAMPLETIME_13_5
  287. * - ADC_SAMPLETIME_28_5
  288. * - ADC_SAMPLETIME_41_5
  289. * - ADC_SAMPLETIME_55_5
  290. * - ADC_SAMPLETIME_71_5
  291. * - ADC_SAMPLETIME_239_5
  292. * @retval none
  293. */
  294. void adc_ordinary_channel_set(adc_type *adc_x, adc_channel_select_type adc_channel, uint8_t adc_sequence, adc_sampletime_select_type adc_sampletime)
  295. {
  296. uint32_t tmp_reg;
  297. if(adc_channel < ADC_CHANNEL_10)
  298. {
  299. tmp_reg = adc_x->spt2;
  300. tmp_reg &= ~(0x07 << 3 * adc_channel);
  301. tmp_reg |= adc_sampletime << 3 * adc_channel;
  302. adc_x->spt2 = tmp_reg;
  303. }
  304. else
  305. {
  306. tmp_reg = adc_x->spt1;
  307. tmp_reg &= ~(0x07 << 3 * (adc_channel - ADC_CHANNEL_10));
  308. tmp_reg |= adc_sampletime << 3 * (adc_channel - ADC_CHANNEL_10);
  309. adc_x->spt1 = tmp_reg;
  310. }
  311. if(adc_sequence >= 13)
  312. {
  313. tmp_reg = adc_x->osq1;
  314. tmp_reg &= ~(0x01F << 5 * (adc_sequence - 13));
  315. tmp_reg |= adc_channel << 5 * (adc_sequence - 13);
  316. adc_x->osq1 = tmp_reg;
  317. }
  318. else if(adc_sequence >= 7)
  319. {
  320. tmp_reg = adc_x->osq2;
  321. tmp_reg &= ~(0x01F << 5 * (adc_sequence - 7));
  322. tmp_reg |= adc_channel << 5 * (adc_sequence - 7);
  323. adc_x->osq2 = tmp_reg;
  324. }
  325. else
  326. {
  327. tmp_reg = adc_x->osq3;
  328. tmp_reg &= ~(0x01F << 5 * (adc_sequence - 1));
  329. tmp_reg |= adc_channel << 5 * (adc_sequence - 1);
  330. adc_x->osq3 = tmp_reg;
  331. }
  332. }
  333. /**
  334. * @brief set preempt channel lenghth of the specified adc peripheral.
  335. * @param adc_x: select the adc peripheral.
  336. * this parameter can be one of the following values:
  337. * ADC1.
  338. * @param adc_channel_lenght: set the adc preempt channel lenghth.
  339. * this parameter can be:
  340. * - (0x1~0x4)
  341. * @retval none
  342. */
  343. void adc_preempt_channel_length_set(adc_type *adc_x, uint8_t adc_channel_lenght)
  344. {
  345. adc_x->psq_bit.pclen = adc_channel_lenght - 1;
  346. }
  347. /**
  348. * @brief configure preempt channel's corresponding rank in the sequencer and sample time of the specified adc peripheral.
  349. * @param adc_x: select the adc peripheral.
  350. * this parameter can be one of the following values:
  351. * ADC1.
  352. * @param adc_channel: select the channel.
  353. * this parameter can be one of the following values:
  354. * - ADC_CHANNEL_0 - ADC_CHANNEL_1 - ADC_CHANNEL_2 - ADC_CHANNEL_3
  355. * - ADC_CHANNEL_4 - ADC_CHANNEL_5 - ADC_CHANNEL_6 - ADC_CHANNEL_7
  356. * - ADC_CHANNEL_8 - ADC_CHANNEL_9 - ADC_CHANNEL_10 - ADC_CHANNEL_11
  357. * - ADC_CHANNEL_12 - ADC_CHANNEL_13 - ADC_CHANNEL_14 - ADC_CHANNEL_15
  358. * - ADC_CHANNEL_16 - ADC_CHANNEL_17
  359. * @param adc_sequence: set rank in the preempt group sequencer.
  360. * this parameter must be:
  361. * - between 1 to 4
  362. * @param adc_sampletime: config the sampletime of adc channel.
  363. * this parameter can be one of the following values:
  364. * - ADC_SAMPLETIME_1_5
  365. * - ADC_SAMPLETIME_7_5
  366. * - ADC_SAMPLETIME_13_5
  367. * - ADC_SAMPLETIME_28_5
  368. * - ADC_SAMPLETIME_41_5
  369. * - ADC_SAMPLETIME_55_5
  370. * - ADC_SAMPLETIME_71_5
  371. * - ADC_SAMPLETIME_239_5
  372. * @retval none
  373. */
  374. void adc_preempt_channel_set(adc_type *adc_x, adc_channel_select_type adc_channel, uint8_t adc_sequence, adc_sampletime_select_type adc_sampletime)
  375. {
  376. uint32_t tmp_reg;
  377. uint8_t sequence_index;
  378. if(adc_channel < ADC_CHANNEL_10)
  379. {
  380. tmp_reg = adc_x->spt2;
  381. tmp_reg &= ~(0x07 << 3 * adc_channel);
  382. tmp_reg |= adc_sampletime << 3 * adc_channel;
  383. adc_x->spt2 = tmp_reg;
  384. }
  385. else
  386. {
  387. tmp_reg = adc_x->spt1;
  388. tmp_reg &= ~(0x07 << 3 * (adc_channel - ADC_CHANNEL_10));
  389. tmp_reg |= adc_sampletime << 3 * (adc_channel - ADC_CHANNEL_10);
  390. adc_x->spt1 = tmp_reg;
  391. }
  392. sequence_index = adc_sequence + 3 - adc_x->psq_bit.pclen;
  393. switch(sequence_index)
  394. {
  395. case 1:
  396. adc_x->psq_bit.psn1 = adc_channel;
  397. break;
  398. case 2:
  399. adc_x->psq_bit.psn2 = adc_channel;
  400. break;
  401. case 3:
  402. adc_x->psq_bit.psn3 = adc_channel;
  403. break;
  404. case 4:
  405. adc_x->psq_bit.psn4 = adc_channel;
  406. break;
  407. default:
  408. break;
  409. }
  410. }
  411. /**
  412. * @brief enable or disable the ordinary channel's external trigger and
  413. * set external trigger event of the specified adc peripheral.
  414. * @param adc_x: select the adc peripheral.
  415. * this parameter can be one of the following values:
  416. * ADC1.
  417. * @param adc_ordinary_trig: select the external trigger event.
  418. * this parameter can be one of the following values:
  419. * adc1
  420. * - ADC12_ORDINARY_TRIG_TMR1CH1 - ADC12_ORDINARY_TRIG_TMR1CH2 - ADC12_ORDINARY_TRIG_TMR1CH3 - ADC12_ORDINARY_TRIG_TMR2CH2
  421. * - ADC12_ORDINARY_TRIG_TMR3TRGOUT - ADC12_ORDINARY_TRIG_TMR4CH4 - ADC12_ORDINARY_TRIG_EXINT11_TMR1TRGOUT - ADC12_ORDINARY_TRIG_SOFTWARE
  422. * - ADC12_ORDINARY_TRIG_TMR1TRGOUT
  423. * @param new_state: new state of ordinary channel's external trigger.
  424. * this parameter can be: TRUE or FALSE.
  425. * @retval none
  426. */
  427. void adc_ordinary_conversion_trigger_set(adc_type *adc_x, adc_ordinary_trig_select_type adc_ordinary_trig, confirm_state new_state)
  428. {
  429. if(adc_ordinary_trig > 7)
  430. {
  431. adc_x->ctrl2_bit.octesel_h = 1;
  432. adc_x->ctrl2_bit.octesel_l = adc_ordinary_trig & 0x7;
  433. }
  434. else
  435. {
  436. adc_x->ctrl2_bit.octesel_h = 0;
  437. adc_x->ctrl2_bit.octesel_l = adc_ordinary_trig & 0x7;
  438. }
  439. adc_x->ctrl2_bit.octen = new_state;
  440. }
  441. /**
  442. * @brief enable or disable the preempt channel's external trigger and
  443. * set external trigger event of the specified adc peripheral.
  444. * @param adc_x: select the adc peripheral.
  445. * this parameter can be one of the following values:
  446. * ADC1.
  447. * @param adc_preempt_trig: select the external trigger event.
  448. * this parameter can be one of the following values:
  449. * adc1
  450. * - ADC12_PREEMPT_TRIG_TMR1TRGOUT - ADC12_PREEMPT_TRIG_TMR1CH4 - ADC12_PREEMPT_TRIG_TMR2TRGOUT - ADC12_PREEMPT_TRIG_TMR2CH1
  451. * - ADC12_PREEMPT_TRIG_TMR3CH4 - ADC12_PREEMPT_TRIG_TMR4TRGOUT - ADC12_PREEMPT_TRIG_EXINT15_TMR1CH4 - ADC12_PREEMPT_TRIG_SOFTWARE
  452. * - ADC12_PREEMPT_TRIG_TMR1CH1
  453. * @param new_state: new state of preempt channel's external trigger.
  454. * this parameter can be: TRUE or FALSE.
  455. * @retval none
  456. */
  457. void adc_preempt_conversion_trigger_set(adc_type *adc_x, adc_preempt_trig_select_type adc_preempt_trig, confirm_state new_state)
  458. {
  459. if(adc_preempt_trig > 7)
  460. {
  461. adc_x->ctrl2_bit.pctesel_h = 1;
  462. adc_x->ctrl2_bit.pctesel_l = adc_preempt_trig & 0x7;
  463. }
  464. else
  465. {
  466. adc_x->ctrl2_bit.pctesel_h = 0;
  467. adc_x->ctrl2_bit.pctesel_l = adc_preempt_trig & 0x7;
  468. }
  469. adc_x->ctrl2_bit.pcten = new_state;
  470. }
  471. /**
  472. * @brief set preempt channel's conversion value offset of the specified adc peripheral.
  473. * @param adc_x: select the adc peripheral.
  474. * this parameter can be one of the following values:
  475. * ADC1.
  476. * @param adc_preempt_channel: select the preempt channel.
  477. * this parameter can be one of the following values:
  478. * - ADC_PREEMPT_CHANNEL_1
  479. * - ADC_PREEMPT_CHANNEL_2
  480. * - ADC_PREEMPT_CHANNEL_3
  481. * - ADC_PREEMPT_CHANNEL_4
  482. * @param adc_offset_value: set the adc preempt channel's conversion value offset.
  483. * this parameter can be:
  484. * - (0x000~0xFFF)
  485. * @retval none
  486. */
  487. void adc_preempt_offset_value_set(adc_type *adc_x, adc_preempt_channel_type adc_preempt_channel, uint16_t adc_offset_value)
  488. {
  489. switch(adc_preempt_channel)
  490. {
  491. case ADC_PREEMPT_CHANNEL_1:
  492. adc_x->pcdto1_bit.pcdto1 = adc_offset_value;
  493. break;
  494. case ADC_PREEMPT_CHANNEL_2:
  495. adc_x->pcdto2_bit.pcdto2 = adc_offset_value;
  496. break;
  497. case ADC_PREEMPT_CHANNEL_3:
  498. adc_x->pcdto3_bit.pcdto3 = adc_offset_value;
  499. break;
  500. case ADC_PREEMPT_CHANNEL_4:
  501. adc_x->pcdto4_bit.pcdto4 = adc_offset_value;
  502. break;
  503. default:
  504. break;
  505. }
  506. }
  507. /**
  508. * @brief set partitioned mode channel count of the specified adc peripheral.
  509. * @param adc_x: select the adc peripheral.
  510. * this parameter can be one of the following values:
  511. * ADC1.
  512. * @param adc_channel_count: configure the adc partitioned mode channel count.
  513. * this parameter can be:
  514. * - (0x1~0x8)
  515. * @retval none
  516. */
  517. void adc_ordinary_part_count_set(adc_type *adc_x, uint8_t adc_channel_count)
  518. {
  519. adc_x->ctrl1_bit.ocpcnt = adc_channel_count - 1;
  520. }
  521. /**
  522. * @brief enable or disable the partitioned mode on ordinary channel of the specified adc peripheral.
  523. * @param adc_x: select the adc peripheral.
  524. * this parameter can be one of the following values:
  525. * ADC1.
  526. * @param new_state: new state of ordinary channel's partitioned mode.
  527. * this parameter can be: TRUE or FALSE.
  528. * @retval none
  529. */
  530. void adc_ordinary_part_mode_enable(adc_type *adc_x, confirm_state new_state)
  531. {
  532. adc_x->ctrl1_bit.ocpen = new_state;
  533. }
  534. /**
  535. * @brief enable or disable the partitioned mode on preempt channel of the specified adc peripheral.
  536. * @param adc_x: select the adc peripheral.
  537. * this parameter can be one of the following values:
  538. * ADC1.
  539. * @param new_state: new state of preempt channel's partitioned mode.
  540. * this parameter can be: TRUE or FALSE.
  541. * @retval none
  542. */
  543. void adc_preempt_part_mode_enable(adc_type *adc_x, confirm_state new_state)
  544. {
  545. adc_x->ctrl1_bit.pcpen = new_state;
  546. }
  547. /**
  548. * @brief enable or disable automatic preempt group conversion of the specified adc peripheral.
  549. * @param adc_x: select the adc peripheral.
  550. * this parameter can be one of the following values:
  551. * ADC1.
  552. * @param new_state: new state of automatic preempt group conversion.
  553. * this parameter can be: TRUE or FALSE.
  554. * @retval none
  555. */
  556. void adc_preempt_auto_mode_enable(adc_type *adc_x, confirm_state new_state)
  557. {
  558. adc_x->ctrl1_bit.pcautoen = new_state;
  559. }
  560. /**
  561. * @brief enable or disable the temperature sensor and vintrv channel.
  562. * @param new_state: new state of Internal temperature sensor and vintrv.
  563. * this parameter can be: TRUE or FALSE.
  564. * note:this bit is present only in adc1
  565. * @retval none
  566. */
  567. void adc_tempersensor_vintrv_enable(confirm_state new_state)
  568. {
  569. ADC1->ctrl2_bit.itsrven = new_state;
  570. }
  571. /**
  572. * @brief enable or disable ordinary software start conversion of the specified adc peripheral.
  573. * @param adc_x: select the adc peripheral.
  574. * this parameter can be one of the following values:
  575. * ADC1.
  576. * @param new_state: new state of ordinary software start conversion.
  577. * this parameter can be: TRUE or FALSE.
  578. * @retval none
  579. */
  580. void adc_ordinary_software_trigger_enable(adc_type *adc_x, confirm_state new_state)
  581. {
  582. adc_x->ctrl2_bit.ocswtrg = new_state;
  583. }
  584. /**
  585. * @brief get ordinary software start conversion status of the specified adc peripheral.
  586. * @param adc_x: select the adc peripheral.
  587. * this parameter can be one of the following values:
  588. * ADC1.
  589. * @retval the new state of ordinary software start conversion status(SET or RESET).
  590. */
  591. flag_status adc_ordinary_software_trigger_status_get(adc_type *adc_x)
  592. {
  593. if(adc_x->ctrl2_bit.ocswtrg)
  594. {
  595. return SET;
  596. }
  597. else
  598. {
  599. return RESET;
  600. }
  601. }
  602. /**
  603. * @brief enable or disable preempt software start conversion of the specified adc peripheral.
  604. * @param adc_x: select the adc peripheral.
  605. * this parameter can be one of the following values:
  606. * ADC1.
  607. * @param new_state: new state of preempt software start conversion.
  608. * this parameter can be: TRUE or FALSE.
  609. * @retval none
  610. */
  611. void adc_preempt_software_trigger_enable(adc_type *adc_x, confirm_state new_state)
  612. {
  613. adc_x->ctrl2_bit.pcswtrg = new_state;
  614. }
  615. /**
  616. * @brief get preempt software start conversion status of the specified adc peripheral.
  617. * @param adc_x: select the adc peripheral.
  618. * this parameter can be one of the following values:
  619. * ADC1.
  620. * @retval the new state of preempt software start conversion status(SET or RESET).
  621. */
  622. flag_status adc_preempt_software_trigger_status_get(adc_type *adc_x)
  623. {
  624. if(adc_x->ctrl2_bit.pcswtrg)
  625. {
  626. return SET;
  627. }
  628. else
  629. {
  630. return RESET;
  631. }
  632. }
  633. /**
  634. * @brief return the last conversion data for ordinary channel of the specified adc peripheral.
  635. * @param adc_x: select the adc peripheral.
  636. * this parameter can be one of the following values:
  637. * ADC1.
  638. * @retval the last conversion data for ordinary channel.
  639. */
  640. uint16_t adc_ordinary_conversion_data_get(adc_type *adc_x)
  641. {
  642. return (uint16_t)(adc_x->odt_bit.odt);
  643. }
  644. /**
  645. * @brief return the conversion data for selection preempt channel of the specified adc peripheral.
  646. * @param adc_x: select the adc peripheral.
  647. * this parameter can be one of the following values:
  648. * ADC1.
  649. * @param adc_preempt_channel: select the preempt channel.
  650. * this parameter can be one of the following values:
  651. * - ADC_PREEMPT_CHANNEL_1
  652. * - ADC_PREEMPT_CHANNEL_2
  653. * - ADC_PREEMPT_CHANNEL_3
  654. * - ADC_PREEMPT_CHANNEL_4
  655. * @retval the conversion data for selection preempt channel.
  656. */
  657. uint16_t adc_preempt_conversion_data_get(adc_type *adc_x, adc_preempt_channel_type adc_preempt_channel)
  658. {
  659. uint16_t preempt_conv_data_index = 0;
  660. switch(adc_preempt_channel)
  661. {
  662. case ADC_PREEMPT_CHANNEL_1:
  663. preempt_conv_data_index = (uint16_t)(adc_x->pdt1_bit.pdt1);
  664. break;
  665. case ADC_PREEMPT_CHANNEL_2:
  666. preempt_conv_data_index = (uint16_t)(adc_x->pdt2_bit.pdt2);
  667. break;
  668. case ADC_PREEMPT_CHANNEL_3:
  669. preempt_conv_data_index = (uint16_t)(adc_x->pdt3_bit.pdt3);
  670. break;
  671. case ADC_PREEMPT_CHANNEL_4:
  672. preempt_conv_data_index = (uint16_t)(adc_x->pdt4_bit.pdt4);
  673. break;
  674. default:
  675. break;
  676. }
  677. return preempt_conv_data_index;
  678. }
  679. /**
  680. * @brief get flag of the specified adc peripheral.
  681. * @param adc_x: select the adc peripheral.
  682. * this parameter can be one of the following values:
  683. * ADC1.
  684. * @param adc_flag: select the adc flag.
  685. * this parameter can be one of the following values:
  686. * - ADC_VMOR_FLAG
  687. * - ADC_CCE_FLAG
  688. * - ADC_PCCE_FLAG
  689. * - ADC_PCCS_FLAG(no interrupt associated)
  690. * - ADC_OCCS_FLAG(no interrupt associated)
  691. * @retval the new state of adc flag status(SET or RESET).
  692. */
  693. flag_status adc_flag_get(adc_type *adc_x, uint8_t adc_flag)
  694. {
  695. flag_status status = RESET;
  696. if((adc_x->sts & adc_flag) == RESET)
  697. {
  698. status = RESET;
  699. }
  700. else
  701. {
  702. status = SET;
  703. }
  704. return status;
  705. }
  706. /**
  707. * @brief get interrupt flag of the specified adc peripheral.
  708. * @param adc_x: select the adc peripheral.
  709. * this parameter can be one of the following values:
  710. * ADC1.
  711. * @param adc_flag: select the adc flag.
  712. * this parameter can be one of the following values:
  713. * - ADC_VMOR_FLAG
  714. * - ADC_CCE_FLAG
  715. * - ADC_PCCE_FLAG
  716. * @retval the new state of adc flag status(SET or RESET).
  717. */
  718. flag_status adc_interrupt_flag_get(adc_type *adc_x, uint8_t adc_flag)
  719. {
  720. flag_status status = RESET;
  721. switch(adc_flag)
  722. {
  723. case ADC_VMOR_FLAG:
  724. if(adc_x->sts_bit.vmor && adc_x->ctrl1_bit.vmorien)
  725. {
  726. status = SET;
  727. }
  728. break;
  729. case ADC_CCE_FLAG:
  730. if(adc_x->sts_bit.cce && adc_x->ctrl1_bit.cceien)
  731. {
  732. status = SET;
  733. }
  734. break;
  735. case ADC_PCCE_FLAG:
  736. if(adc_x->sts_bit.pcce && adc_x->ctrl1_bit.pcceien)
  737. {
  738. status = SET;
  739. }
  740. break;
  741. default:
  742. break;
  743. }
  744. return status;
  745. }
  746. /**
  747. * @brief clear flag of the specified adc peripheral.
  748. * @param adc_x: select the adc peripheral.
  749. * this parameter can be one of the following values:
  750. * ADC1.
  751. * @param adc_flag: select the adc flag.
  752. * this parameter can be any combination of the following values:
  753. * - ADC_VMOR_FLAG
  754. * - ADC_CCE_FLAG(also can clear by reading the adc_x->odt)
  755. * - ADC_PCCE_FLAG
  756. * - ADC_PCCS_FLAG
  757. * - ADC_OCCS_FLAG
  758. * @retval none
  759. */
  760. void adc_flag_clear(adc_type *adc_x, uint32_t adc_flag)
  761. {
  762. adc_x->sts = ~adc_flag;
  763. }
  764. /**
  765. * @}
  766. */
  767. #endif
  768. /**
  769. * @}
  770. */
  771. /**
  772. * @}
  773. */