at32f413_adc.c 30 KB

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