at32f413_adc.c 31 KB

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