at32f403a_407_adc.c 30 KB

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