gd32f4xx_adc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*!
  2. \file gd32f4xx_adc.c
  3. \brief ADC driver
  4. */
  5. /*
  6. Copyright (C) 2016 GigaDevice
  7. 2016-08-15, V1.0.0, firmware for GD32F4xx
  8. */
  9. #include "gd32f4xx_adc.h"
  10. #define REGULAR_CHANNEL_LENGTH_OFFSET ((uint32_t)20U)
  11. #define INSERTED_CHANNEL_LENGTH_OFFSET ((uint32_t)20U)
  12. #define REGULAR_DISCONTINUOUS_NUMBER ((uint32_t)13U)
  13. #define REGULAR_TRIGGER_MODE ((uint32_t)28U)
  14. #define INSERTED_TRIGGER_MODE ((uint32_t)20U)
  15. /*!
  16. \brief ADC reset
  17. \param[in] none
  18. \param[out] none
  19. \retval none
  20. */
  21. void adc_deinit(void)
  22. {
  23. rcu_periph_reset_enable(RCU_ADCRST);
  24. rcu_periph_reset_disable(RCU_ADCRST);
  25. }
  26. /*!
  27. \brief enable ADC interface
  28. \param[in] adc_periph: ADCx,x=0,1,2
  29. \param[in] none
  30. \param[out] none
  31. \retval none
  32. */
  33. void adc_enable(uint32_t adc_periph)
  34. {
  35. if(RESET == (ADC_CTL1(adc_periph) & ADC_CTL1_ADCON)){
  36. ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_ADCON;
  37. }
  38. }
  39. /*!
  40. \brief disable ADC interface
  41. \param[in] adc_periph: ADCx,x=0,1,2
  42. \param[in] none
  43. \param[out] none
  44. \retval none
  45. */
  46. void adc_disable(uint32_t adc_periph)
  47. {
  48. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ADCON);
  49. }
  50. /*!
  51. \brief ADC data alignment config
  52. \param[in] adc_periph: ADCx,x=0,1,2
  53. \param[in] inserted_channel : insert channel select
  54. \arg ADC_DATAALIGN_RIGHT: LSB alignment
  55. \arg ADC_DATAALIGN_LEFT: MSB alignment
  56. \param[out] none
  57. \retval none
  58. */
  59. void adc_data_alignment_config(uint32_t adc_periph , uint8_t data_alignment)
  60. {
  61. if(data_alignment){
  62. ADC_CTL1(adc_periph) |= ADC_CTL1_DAL;
  63. }else{
  64. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DAL);
  65. }
  66. }
  67. /*!
  68. \brief ADC resolution config
  69. \param[in] adc_periph: ADCx,x=0,1,2
  70. \param[in] resolution: ADC resolution
  71. \arg ADC_RESOLUTION_12B: 12-bit ADC resolution
  72. \arg ADC_RESOLUTION_10B: 10-bit ADC resolution
  73. \arg ADC_RESOLUTION_8B: 8-bit ADC resolution
  74. \arg ADC_RESOLUTION_6B: 6-bit ADC resolution
  75. \param[out] none
  76. \retval none
  77. */
  78. void adc_resolution_config(uint32_t adc_periph , uint32_t resolution)
  79. {
  80. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_DRES);
  81. ADC_CTL0(adc_periph) |= (uint32_t)resolution;
  82. }
  83. /*!
  84. \brief ADC calibration and reset calibration
  85. \param[in] adc_periph: ADCx,x=0,1,2
  86. \param[in] none
  87. \param[out] none
  88. \retval none
  89. */
  90. void adc_calibration_enable(uint32_t adc_periph)
  91. {
  92. /* reset the selected ADC1 calibration registers */
  93. ADC_CTL1(adc_periph) |= (uint32_t) ADC_CTL1_RSTCLB;
  94. /* check the RSTCLB bit state */
  95. while((ADC_CTL1(adc_periph) & ADC_CTL1_RSTCLB)){
  96. }
  97. /* enable ADC calibration process */
  98. ADC_CTL1(adc_periph) |= ADC_CTL1_CLB;
  99. /* check the CLB bit state */
  100. while((ADC_CTL1(adc_periph) & ADC_CTL1_CLB)){
  101. }
  102. }
  103. /*!
  104. \brief ADC discontinuous mode config
  105. \param[in] adc_periph: ADCx,x=0,1,2
  106. \param[in] adc_channel_group: select the channel group
  107. \arg ADC_REGULAR_CHANNEL: regular channel group
  108. \arg ADC_INSERTED_CHANNEL: inserted channel group
  109. \param[in] length: number of conversions in discontinuous mode,the number can be 1..8
  110. for regular channel ,the number is no effect for inserted channel
  111. \param[out] none
  112. \retval none
  113. */
  114. void adc_discontinuous_mode_config(uint32_t adc_periph , uint8_t adc_channel_group , uint8_t length)
  115. {
  116. ADC_CTL0(adc_periph) &= ~((uint32_t)( ADC_CTL0_DISRC | ADC_CTL0_DISIC ));
  117. switch(adc_channel_group){
  118. case ADC_REGULAR_CHANNEL:
  119. /* config the number of conversions in discontinuous mode */
  120. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_DISNUM);
  121. if((length <= 8U) && (length >= 1U)){
  122. ADC_CTL0(adc_periph) |= ((uint32_t)length - 1U) << REGULAR_DISCONTINUOUS_NUMBER;
  123. }
  124. ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISRC;
  125. break;
  126. case ADC_INSERTED_CHANNEL:
  127. ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISIC;
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. /*!
  134. \brief config end of conversion mode
  135. \param[in] adc_periph: ADCx,x=0,1,2
  136. \param[in] end_selection: end of conversion mode
  137. \arg ADC_EOC_SET_SEQUENCE: only at the end of a sequence of regular conversions, the EOC bit is set
  138. \arg ADC_EOC_SET_CONVERSION: at the end of each regular conversion, the EOC bit is set.
  139. \param[out] none
  140. \retval none
  141. */
  142. void adc_end_of_conversion_config(uint32_t adc_periph , uint8_t end_selection)
  143. {
  144. switch(end_selection){
  145. case ADC_EOC_SET_SEQUENCE:
  146. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_EOCM);
  147. break;
  148. case ADC_EOC_SET_CONVERSION:
  149. ADC_CTL1(adc_periph) |= (uint32_t)(ADC_CTL1_EOCM);
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. /*!
  156. \brief ADC special function enable or disable
  157. \param[in] adc_periph: ADCx,x=0,1,2
  158. \param[in] function: select the function to config
  159. \arg ADC_SCAN_MODE: scan mode select
  160. \arg ADC_INSERTED_CHANNEL_AUTO: inserted channel group convert automatically
  161. \arg ADC_CONTINUOUS_MODE: continuous mode select
  162. \param[in] newvalue: ENABLE or DISABLE
  163. \param[out] none
  164. \retval none
  165. */
  166. void adc_special_function_config(uint32_t adc_periph , uint8_t function , ControlStatus newvalue)
  167. {
  168. if(newvalue){
  169. switch(function){
  170. case ADC_SCAN_MODE:
  171. ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_SM;
  172. break;
  173. case ADC_INSERTED_CHANNEL_AUTO:
  174. ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_ICA;
  175. break;
  176. case ADC_CONTINUOUS_MODE:
  177. ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_CTN;
  178. break;
  179. default:
  180. break;
  181. }
  182. }else{
  183. switch(function){
  184. case ADC_SCAN_MODE:
  185. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_SM);
  186. break;
  187. case ADC_INSERTED_CHANNEL_AUTO:
  188. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_ICA);
  189. break;
  190. case ADC_CONTINUOUS_MODE:
  191. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_CTN);
  192. break;
  193. default:
  194. break;
  195. }
  196. }
  197. }
  198. /*!
  199. \brief configure the ADC clock for all the ADCs
  200. \param[in] prescaler: configure ADCs prescaler ratio
  201. \arg ADC_ADCCK_PCLK2_DIV2: PCLK2 div2
  202. \arg ADC_ADCCK_PCLK2_DIV4: PCLK2 div4
  203. \arg ADC_ADCCK_PCLK2_DIV6: PCLK2 div6
  204. \arg ADC_ADCCK_PCLK2_DIV8: PCLK2 div8
  205. \arg ADC_ADCCK_HCLK_DIV5: HCLK div5
  206. \arg ADC_ADCCK_HCLK_DIV6: HCLK div6
  207. \arg ADC_ADCCK_HCLK_DIV10: HCLK div10
  208. \arg ADC_ADCCK_HCLK_DIV20: HCLK div20
  209. \param[out] none
  210. \retval none
  211. */
  212. void adc_clock_config(uint32_t prescaler)
  213. {
  214. ADC_SYNCCTL &= ~((uint32_t)ADC_SYNCCTL_ADCCK);
  215. ADC_SYNCCTL |= (uint32_t) prescaler;
  216. }
  217. /*!
  218. \brief configure the ADC clock for all the ADCs
  219. \param[in] function: temperature sensor and internal reference voltage channel or VBAT channel
  220. \arg ADC_VBAT_CHANNEL_SWITCH: channel 18 (1/4 voltate of external battery) switch of ADC0
  221. \arg ADC_TEMP_VREF_CHANNEL_SWITCH: channel 16 (temperature sensor) and 17 (internal reference voltage) switch of ADC0
  222. \param[in] newvalue: ENABLE or DISABLE
  223. \param[out] none
  224. \retval none
  225. */
  226. void adc_channel_16_to_18(uint8_t function,ControlStatus newvalue)
  227. {
  228. if(newvalue){
  229. switch(function){
  230. case ADC_VBAT_CHANNEL_SWITCH:
  231. ADC_SYNCCTL |= (uint32_t)ADC_SYNCCTL_VBATEN;
  232. break;
  233. case ADC_TEMP_VREF_CHANNEL_SWITCH:
  234. ADC_SYNCCTL |= (uint32_t)ADC_SYNCCTL_TSVREN;
  235. break;
  236. default:
  237. break;
  238. }
  239. }else{
  240. switch(function){
  241. case ADC_VBAT_CHANNEL_SWITCH:
  242. ADC_SYNCCTL &= ~((uint32_t)ADC_SYNCCTL_VBATEN);
  243. break;
  244. case ADC_TEMP_VREF_CHANNEL_SWITCH:
  245. ADC_SYNCCTL &= ~((uint32_t)ADC_SYNCCTL_TSVREN);
  246. break;
  247. default:
  248. break;
  249. }
  250. }
  251. }
  252. /*!
  253. \brief config the length of regular channel group or inserted channel group
  254. \param[in] adc_periph: ADCx,x=0,1,2
  255. \param[in] adc_channel_group: select the channel group
  256. \arg ADC_REGULAR_CHANNEL: regular channel group
  257. \arg ADC_INSERTED_CHANNEL: inserted channel group
  258. \param[in] length: the length of the channel
  259. regular channel 1-16
  260. inserted channel 1-4
  261. \param[out] none
  262. \retval none
  263. */
  264. void adc_channel_length_config(uint32_t adc_periph , uint8_t adc_channel_group , uint32_t length)
  265. {
  266. switch(adc_channel_group){
  267. case ADC_REGULAR_CHANNEL:
  268. if((length >= 1U) && (length <= 16U)){
  269. ADC_RSQ0(adc_periph) &= ~((uint32_t)ADC_RSQ0_RL);
  270. ADC_RSQ0(adc_periph) |= (uint32_t)((length-1U) << REGULAR_CHANNEL_LENGTH_OFFSET);
  271. }
  272. break;
  273. case ADC_INSERTED_CHANNEL:
  274. if((length >= 1U) && (length <= 4U)){
  275. ADC_ISQ(adc_periph) &= ~((uint32_t)ADC_ISQ_IL);
  276. ADC_ISQ(adc_periph) |= (uint32_t)((length-1U) << INSERTED_CHANNEL_LENGTH_OFFSET);
  277. }
  278. break;
  279. default:
  280. break;
  281. }
  282. }
  283. /*!
  284. \brief ADC external trigger enable
  285. \param[in] adc_periph: ADCx,x=0,1,2
  286. \param[in] adc_channel_group: select the channel group
  287. \arg ADC_REGULAR_CHANNEL: regular channel group
  288. \arg ADC_INSERTED_CHANNEL: inserted channel group
  289. \param[in] trigger_mode: external trigger mode
  290. \arg EXTERNAL_TRIGGER_DISABLE: external trigger disable
  291. \arg EXTERNAL_TRIGGER_RISING: rising edge of external trigger
  292. \arg EXTERNAL_TRIGGER_FALLING: falling edge of external trigger
  293. \arg EXTERNAL_TRIGGER_RISING_FALLING: rising and falling edge of external trigger
  294. \param[out] none
  295. \retval none
  296. */
  297. void adc_external_trigger_config(uint32_t adc_periph , uint8_t adc_channel_group , uint32_t trigger_mode)
  298. {
  299. switch(adc_channel_group){
  300. case ADC_REGULAR_CHANNEL:
  301. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETMRC);
  302. ADC_CTL1(adc_periph) |= (uint32_t) (trigger_mode << REGULAR_TRIGGER_MODE);
  303. break;
  304. case ADC_INSERTED_CHANNEL:
  305. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETMIC);
  306. ADC_CTL1(adc_periph) |= (uint32_t) (trigger_mode << INSERTED_TRIGGER_MODE);
  307. break;
  308. default:
  309. break;
  310. }
  311. }
  312. /*!
  313. \brief ADC external trigger source config
  314. \param[in] adc_periph: ADCx,x=0,1,2
  315. \param[in] adc_channel_group: select the channel group
  316. \arg ADC_REGULAR_CHANNEL: regular channel group
  317. \arg ADC_INSERTED_CHANNEL: inserted channel group
  318. \param[in] external_trigger_source: regular or inserted group trigger source
  319. for regular channel:
  320. \arg ADC_EXTTRIG_REGULAR_T0_CH0: external trigger timer 0 CC0 event select for regular channel
  321. \arg ADC_EXTTRIG_REGULAR_T0_CH1: external trigger timer 0 CC1 event select for regular channel
  322. \arg ADC_EXTTRIG_REGULAR_T0_CH2: external trigger timer 0 CC2 event select for regular channel
  323. \arg ADC_EXTTRIG_REGULAR_T1_CH1: external trigger timer 1 CC1 event select for regular channel
  324. \arg ADC_EXTTRIG_REGULAR_T1_CH2: external trigger timer 1 CC2 event select for regular channel
  325. \arg ADC_EXTTRIG_REGULAR_T1_CH3: external trigger timer 1 CC3 event select for regular channel
  326. \arg ADC_EXTTRIG_REGULAR_T1_TRGO: external trigger timer 1 TRGO event select for regular channel
  327. \arg ADC_EXTTRIG_REGULAR_T2_CH0 : external trigger timer 2 CC0 event select for regular channel
  328. \arg ADC_EXTTRIG_REGULAR_T2_TRGO : external trigger timer 2 TRGO event select for regular channel
  329. \arg ADC_EXTTRIG_REGULAR_T3_CH3: external trigger timer 3 CC3 event select for regular channel
  330. \arg ADC_EXTTRIG_REGULAR_T4_CH0: external trigger timer 4 CC0 event select for regular channel
  331. \arg ADC_EXTTRIG_REGULAR_T4_CH1: external trigger timer 4 CC1 event select for regular channel
  332. \arg ADC_EXTTRIG_REGULAR_T4_CH2: external trigger timer 4 CC2 event select for regular channel
  333. \arg ADC_EXTTRIG_REGULAR_T7_CH0: external trigger timer 7 CC0 event select for regular channel
  334. \arg ADC_EXTTRIG_REGULAR_T7_TRGO: external trigger timer 7 TRGO event select for regular channel
  335. \arg ADC_EXTTRIG_REGULAR_EXTI_11: external trigger extiline 11 select for regular channel
  336. for inserted channel:
  337. \arg ADC_EXTTRIG_INSERTED_T0_CH3: timer0 capture compare 3
  338. \arg ADC_EXTTRIG_INSERTED_T0_TRGO: timer0 TRGO event
  339. \arg ADC_EXTTRIG_INSERTED_T1_CH0: timer1 capture compare 0
  340. \arg ADC_EXTTRIG_INSERTED_T1_TRGO: timer1 TRGO event
  341. \arg ADC_EXTTRIG_INSERTED_T2_CH1: timer2 capture compare 1
  342. \arg ADC_EXTTRIG_INSERTED_T2_CH3: timer2 capture compare 3
  343. \arg ADC_EXTTRIG_INSERTED_T3_CH0: timer3 capture compare 0
  344. \arg ADC_EXTTRIG_INSERTED_T3_CH1: timer3 capture compare 1
  345. \arg ADC_EXTTRIG_INSERTED_T3_CH2: timer3 capture compare 2
  346. \arg ADC_EXTTRIG_INSERTED_T3_TRGO: timer3 capture compare TRGO
  347. \arg ADC_EXTTRIG_INSERTED_T4_CH3: timer4 capture compare 3
  348. \arg ADC_EXTTRIG_INSERTED_T4_TRGO: timer4 capture compare TRGO
  349. \arg ADC_EXTTRIG_INSERTED_T7_CH1: timer7 capture compare 1
  350. \arg ADC_EXTTRIG_INSERTED_T7_CH2: timer7 capture compare 2
  351. \arg ADC_EXTTRIG_INSERTED_T7_CH3: timer7 capture compare 3
  352. \arg ADC_EXTTRIG_INSERTED_EXTI_15: external interrupt line 15
  353. \param[out] none
  354. \retval none
  355. */
  356. void adc_external_trigger_source_config(uint32_t adc_periph , uint8_t adc_channel_group , uint32_t external_trigger_source)
  357. {
  358. switch(adc_channel_group){
  359. case ADC_REGULAR_CHANNEL:
  360. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSRC);
  361. ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
  362. break;
  363. case ADC_INSERTED_CHANNEL:
  364. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSIC);
  365. ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
  366. break;
  367. default:
  368. break;
  369. }
  370. }
  371. /*!
  372. \brief ADC software trigger enable
  373. \param[in] adc_periph: ADCx,x=0,1,2
  374. \param[in] adc_channel_group: select the channel group
  375. \arg ADC_REGULAR_CHANNEL: regular channel group
  376. \arg ADC_INSERTED_CHANNEL: inserted channel group
  377. \param[out] none
  378. \retval none
  379. */
  380. void adc_software_trigger_enable(uint32_t adc_periph , uint8_t adc_channel_group)
  381. {
  382. switch(adc_channel_group){
  383. case ADC_REGULAR_CHANNEL:
  384. ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_SWRCST;
  385. break;
  386. case ADC_INSERTED_CHANNEL:
  387. ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_SWICST;
  388. break;
  389. default:
  390. break;
  391. }
  392. }
  393. /*!
  394. \brief get the ADC flag bits
  395. \param[in] adc_periph: ADCx,x=0,1,2
  396. \param[in] adc_flag: the adc flag bits
  397. \arg ADC_FLAG_WDE: analog watchdog event flag
  398. \arg ADC_FLAG_EOC: end of group conversion flag
  399. \arg ADC_FLAG_EOIC: end of inserted group conversion flag
  400. \arg ADC_FLAG_STIC: start flag of inserted channel group
  401. \arg ADC_FLAG_STRC: start flag of regular channel group
  402. \arg ADC_FLAG_ROVF: regular data register overflow flag
  403. \param[out] none
  404. \retval FlagStatus: SET or RESET
  405. */
  406. FlagStatus adc_flag_get(uint32_t adc_periph , uint32_t adc_flag)
  407. {
  408. if(ADC_STAT(adc_periph) & adc_flag){
  409. return SET;
  410. }
  411. return RESET;
  412. }
  413. /*!
  414. \brief clear the ADC flag bits
  415. \param[in] adc_periph: ADCx,x=0,1,2
  416. \param[in] adc_flag: the adc flag bits
  417. \arg ADC_FLAG_WDE: analog watchdog event flag
  418. \arg ADC_FLAG_EOC: end of group conversion flag
  419. \arg ADC_FLAG_EOIC: end of inserted group conversion flag
  420. \arg ADC_FLAG_STIC: start flag of inserted channel group
  421. \arg ADC_FLAG_STRC: start flag of regular channel group
  422. \arg ADC_FLAG_ROVF: regular data register overflow flag
  423. \param[out] none
  424. \retval none
  425. */
  426. void adc_flag_clear(uint32_t adc_periph , uint32_t adc_flag)
  427. {
  428. ADC_STAT(adc_periph) &= ~((uint32_t)adc_flag);
  429. }
  430. /*!
  431. \brief get the ADC interrupt bits
  432. \param[in] adc_periph: ADCx,x=0,1,2
  433. \param[in] adc_interrupt: the adc interrupt bits
  434. \arg ADC_INT_WDE: analog watchdog interrupt
  435. \arg ADC_INT_EOC: end of group conversion interrupt
  436. \arg ADC_INT_EOIC: end of inserted group conversion interrupt
  437. \arg ADC_INT_ROVF: regular data register overflow interrupt
  438. \param[out] none
  439. \retval FlagStatus: SET or RESET
  440. */
  441. FlagStatus adc_interrupt_flag_get(uint32_t adc_periph , uint32_t adc_interrupt)
  442. {
  443. FlagStatus interrupt_flag = RESET;
  444. uint32_t state;
  445. /* check the interrupt bits */
  446. switch(adc_interrupt){
  447. case ADC_INT_WDE:
  448. state = ADC_STAT(adc_periph) & ADC_STAT_WDE;
  449. if((ADC_CTL0(adc_periph) & ADC_CTL0_WDEIE) && state){
  450. interrupt_flag = SET;
  451. }
  452. break;
  453. case ADC_INT_EOC:
  454. state = ADC_STAT(adc_periph) & ADC_STAT_EOC;
  455. if((ADC_CTL0(adc_periph) & ADC_CTL0_EOCIE) && state){
  456. interrupt_flag = SET;
  457. }
  458. break;
  459. case ADC_INT_EOIC:
  460. state = ADC_STAT(adc_periph) & ADC_STAT_EOIC;
  461. if((ADC_CTL0(adc_periph) & ADC_CTL0_EOICIE) && state){
  462. interrupt_flag = SET;
  463. }
  464. break;
  465. case ADC_INT_ROVF:
  466. state = ADC_STAT(adc_periph) & ADC_STAT_ROVF;
  467. if((ADC_CTL0(adc_periph) & ADC_CTL0_ROVFIE) && state){
  468. interrupt_flag = SET;
  469. }
  470. break;
  471. default:
  472. break;
  473. }
  474. return interrupt_flag;
  475. }
  476. /*!
  477. \brief clear the ADC flag
  478. \param[in] adc_periph: ADCx,x=0,1,2
  479. \param[in] adc_flag: the adc status flag
  480. \arg ADC_INT_WDE: analog watchdog interrupt
  481. \arg ADC_INT_EOC: end of group conversion interrupt
  482. \arg ADC_INT_EOIC: end of inserted group conversion interrupt
  483. \arg ADC_INT_ROVF: regular data register overflow interrupt
  484. \param[out] none
  485. \retval none
  486. */
  487. void adc_interrupt_flag_clear(uint32_t adc_periph , uint32_t adc_interrupt)
  488. {
  489. ADC_STAT(adc_periph) &= ~((uint32_t)adc_interrupt);
  490. }
  491. /*!
  492. \brief ADC interrupt enable
  493. \param[in] adc_periph: ADCx,x=0,1,2
  494. \param[in] adc_flag: the adc interrupt flag
  495. \arg ADC_INT_WDE: analog watchdog interrupt flag
  496. \arg ADC_INT_EOC: end of group conversion interrupt flag
  497. \arg ADC_INT_EOIC: end of inserted group conversion interrupt flag
  498. \arg ADC_INT_ROVF: regular data register overflow interrupt flag
  499. \param[out] none
  500. \retval none
  501. */
  502. void adc_interrupt_enable(uint32_t adc_periph , uint32_t adc_interrupt)
  503. {
  504. switch(adc_interrupt){
  505. case ADC_INT_WDE:
  506. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_WDEIE;
  507. break;
  508. case ADC_INT_EOC:
  509. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_EOCIE;
  510. break;
  511. case ADC_INT_EOIC:
  512. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_EOICIE;
  513. break;
  514. case ADC_INT_ROVF:
  515. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_ROVFIE;
  516. break;
  517. default:
  518. break;
  519. }
  520. }
  521. /*!
  522. \brief ADC interrupt disable
  523. \param[in] adc_periph: ADCx,x=0,1,2
  524. \param[in] adc_flag: the adc interrupt flag
  525. \arg ADC_INT_WDE: analog watchdog interrupt flag
  526. \arg ADC_INT_EOC: end of group conversion interrupt flag
  527. \arg ADC_INT_EOIC: end of inserted group conversion interrupt flag
  528. \arg ADC_INT_ROVF: regular data register overflow interrupt flag
  529. \param[out] none
  530. \retval none
  531. */
  532. void adc_interrupt_disable(uint32_t adc_periph , uint32_t adc_interrupt)
  533. {
  534. switch(adc_interrupt){
  535. /* select the interrupt source */
  536. case ADC_INT_WDE:
  537. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_WDEIE);
  538. break;
  539. case ADC_INT_EOC:
  540. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_EOCIE);
  541. break;
  542. case ADC_INT_EOIC:
  543. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_EOICIE);
  544. break;
  545. case ADC_INT_ROVF:
  546. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_ROVFIE);
  547. break;
  548. default:
  549. break;
  550. }
  551. }
  552. /*!
  553. \brief ADC analog watchdog single channel disable
  554. \param[in] adc_periph: ADCx,x=0,1,2
  555. \param[out] none
  556. \retval none
  557. */
  558. void adc_watchdog_single_channel_disable(uint32_t adc_periph )
  559. {
  560. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_WDSC);
  561. }
  562. /*!
  563. \brief ADC analog watchdog single channel enable
  564. \param[in] adc_periph: ADCx,x=0,1,2
  565. \param[in] adc_channel: the selected ADC channel
  566. \arg ADC_CHANNEL_x: ADC Channelx(x=0..18)
  567. \param[out] none
  568. \retval none
  569. */
  570. void adc_watchdog_single_channel_enable(uint32_t adc_periph , uint8_t adc_channel)
  571. {
  572. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_WDCHSEL);
  573. ADC_CTL0(adc_periph) |= (uint32_t)adc_channel;
  574. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_WDSC;
  575. }
  576. /*!
  577. \brief adc analog watchdog group channel config
  578. \param[in] adc_periph: ADCx,x=0,1,2
  579. \param[in] adc_channel_group: the channel group use analog watchdog
  580. \arg ADC_REGULAR_CHANNEL: regular channel group
  581. \arg ADC_INSERTED_CHANNEL: inserted channel group
  582. \arg ADC_REGULAR_INSERTED_CHANNEL: both regular and inserted group
  583. \param[out] none
  584. \retval none
  585. */
  586. void adc_watchdog_enable(uint32_t adc_periph , uint8_t adc_channel_group)
  587. {
  588. ADC_CTL0(adc_periph) &= ~((uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC));
  589. /* select the group */
  590. switch(adc_channel_group){
  591. case ADC_REGULAR_CHANNEL:
  592. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_RWDEN;
  593. break;
  594. case ADC_INSERTED_CHANNEL:
  595. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_IWDEN;
  596. break;
  597. case ADC_REGULAR_INSERTED_CHANNEL:
  598. ADC_CTL0(adc_periph) |= (uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN);
  599. break;
  600. default:
  601. break;
  602. }
  603. }
  604. /*!
  605. \brief ADC analog watchdog disable
  606. \param[in] adc_periph: ADCx,x=0,1,2
  607. \param[in] adc_channel_group: the channel group use analog watchdog
  608. \arg ADC_REGULAR_CHANNEL: regular channel group
  609. \arg ADC_INSERTED_CHANNEL: inserted channel group
  610. \arg ADC_REGULAR_INSERTED_CHANNEL: both regular and inserted group
  611. \param[out] none
  612. \retval none
  613. */
  614. void adc_watchdog_disable(uint32_t adc_periph , uint8_t adc_channel_group)
  615. {
  616. /* select the group */
  617. switch(adc_channel_group){
  618. case ADC_REGULAR_CHANNEL:
  619. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_RWDEN);
  620. break;
  621. case ADC_INSERTED_CHANNEL:
  622. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_IWDEN);
  623. break;
  624. case ADC_REGULAR_INSERTED_CHANNEL:
  625. ADC_CTL0(adc_periph) &= ~((uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN));
  626. break;
  627. default:
  628. break;
  629. }
  630. }
  631. /*!
  632. \brief ADC analog watchdog threshold config
  633. \param[in] adc_periph: ADCx,x=0,1,2
  634. \param[in] low_threshold: analog watchdog low threshold,0..4095
  635. \param[in] high_threshold: analog watchdog high threshold,0..4095
  636. \param[out] none
  637. \retval none
  638. */
  639. void adc_watchdog_threshold_config(uint32_t adc_periph , uint16_t low_threshold , uint16_t high_threshold)
  640. {
  641. ADC_WDLT(adc_periph) = (uint32_t)low_threshold;
  642. ADC_WDHT(adc_periph) = (uint32_t)high_threshold;
  643. }
  644. /*!
  645. \brief ADC regular channel config
  646. \param[in] adc_periph: ADCx,x=0,1,2
  647. \param[in] rank: the regular group sequencer rank,this parameter must be between 0 to 15
  648. \param[in] adc_channel: the selected ADC channel
  649. \arg ADC_CHANNEL_x(x=0..18): ADC Channelx
  650. \param[in] sample_time: the sample time value
  651. \arg ADC_SAMPLETIME_3: 3 cycles
  652. \arg ADC_SAMPLETIME_15: 15 cycles
  653. \arg ADC_SAMPLETIME_28: 28 cycles
  654. \arg ADC_SAMPLETIME_56: 56 cycles
  655. \arg ADC_SAMPLETIME_84: 84 cycles
  656. \arg ADC_SAMPLETIME_112: 112 cycles
  657. \arg ADC_SAMPLETIME_144: 144 cycles
  658. \arg ADC_SAMPLETIME_480: 480 cycles
  659. \param[out] none
  660. \retval none
  661. */
  662. void adc_regular_channel_config(uint32_t adc_periph , uint8_t rank , uint8_t adc_channel , uint32_t sample_time)
  663. {
  664. uint32_t rsq,sampt;
  665. /* ADC regular sequence config */
  666. if(rank < 6U){
  667. rsq = ADC_RSQ2(adc_periph);
  668. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (5U*rank)));
  669. rsq |= ((uint32_t)adc_channel << (5U*rank));
  670. ADC_RSQ2(adc_periph) = rsq;
  671. }else if(rank < 12U){
  672. rsq = ADC_RSQ1(adc_periph);
  673. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (5U*(rank-6U))));
  674. rsq |= ((uint32_t)adc_channel << (5U*(rank-6U)));
  675. ADC_RSQ1(adc_periph) = rsq;
  676. }else if(rank < 16U){
  677. rsq = ADC_RSQ0(adc_periph);
  678. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (5U*(rank-12U))));
  679. rsq |= ((uint32_t)adc_channel << (5U*(rank-12U)));
  680. ADC_RSQ0(adc_periph) = rsq;
  681. }else{
  682. }
  683. /* ADC sampling time config */
  684. if(adc_channel < 10U){
  685. sampt = ADC_SAMPT1(adc_periph);
  686. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*adc_channel)));
  687. sampt |= (uint32_t)(sample_time << (3U*adc_channel));
  688. ADC_SAMPT1(adc_periph) = sampt;
  689. }else if(adc_channel < 19U){
  690. sampt = ADC_SAMPT0(adc_periph);
  691. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*(adc_channel-10U))));
  692. sampt |= (uint32_t)(sample_time << (3U*(adc_channel-10U)));
  693. ADC_SAMPT0(adc_periph) = sampt;
  694. }else{
  695. }
  696. }
  697. /*!
  698. \brief ADC regular group data register read
  699. \param[in] adc_periph: ADCx,x=0,1,2
  700. \param[in] none
  701. \param[out] none
  702. \retval the conversion value
  703. */
  704. uint16_t adc_regular_data_read(uint32_t adc_periph)
  705. {
  706. return (uint16_t)(ADC_RDATA(adc_periph));
  707. }
  708. /*!
  709. \brief ADC inserted channel config
  710. \param[in] adc_periph: ADCx,x=0,1,2
  711. \param[in] rank: the inserted group sequencer rank,this parameter must be between 0 to 3
  712. \param[in] adc_channel: the selected ADC channel
  713. \arg ADC_CHANNEL_x(x=0..18): ADC Channelx
  714. \param[in] sample_time: The sample time value
  715. \arg ADC_SAMPLETIME_3: 3 cycles
  716. \arg ADC_SAMPLETIME_15: 15 cycles
  717. \arg ADC_SAMPLETIME_28: 28 cycles
  718. \arg ADC_SAMPLETIME_56: 56 cycles
  719. \arg ADC_SAMPLETIME_84: 84 cycles
  720. \arg ADC_SAMPLETIME_112: 112 cycles
  721. \arg ADC_SAMPLETIME_144: 144 cycles
  722. \arg ADC_SAMPLETIME_480: 480 cycles
  723. \param[out] none
  724. \retval none
  725. */
  726. void adc_inserted_channel_config(uint32_t adc_periph , uint8_t rank , uint8_t adc_channel , uint8_t sample_time)
  727. {
  728. uint8_t inserted_length;
  729. uint32_t isq,sampt;
  730. inserted_length = (uint8_t)GET_BITS(ADC_ISQ(adc_periph) , 20U , 21U);
  731. if(rank < 4U){
  732. isq = ADC_ISQ(adc_periph);
  733. isq &= ~((uint32_t)(ADC_ISQ_ISQN << (15U-(inserted_length-rank)*5U)));
  734. isq |= ((uint32_t)adc_channel << (15U-(inserted_length-rank)*5U));
  735. ADC_ISQ(adc_periph) = isq;
  736. }
  737. if(adc_channel < 10U){
  738. sampt = ADC_SAMPT1(adc_periph);
  739. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*adc_channel)));
  740. sampt |= (uint32_t) sample_time << (3U*adc_channel);
  741. ADC_SAMPT1(adc_periph) = sampt;
  742. }else if(adc_channel < 19U){
  743. sampt = ADC_SAMPT0(adc_periph);
  744. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*(adc_channel-10U))));
  745. sampt |= ((uint32_t)sample_time << (3U*(adc_channel-10U)));
  746. ADC_SAMPT0(adc_periph) = sampt;
  747. }else{
  748. }
  749. }
  750. /*!
  751. \brief ADC inserted channel offset config
  752. \param[in] adc_periph: ADCx,x=0,1,2
  753. \param[in] inserted_channel : insert channel select
  754. \arg ADC_INSERTED_CHANNEL_0: inserted channel0
  755. \arg ADC_INSERTED_CHANNEL_1: inserted channel1
  756. \arg ADC_INSERTED_CHANNEL_2: inserted channel2
  757. \arg ADC_INSERTED_CHANNEL_3: inserted channel3
  758. \param[in] offset : the offset data
  759. \param[out] none
  760. \retval the conversion value
  761. */
  762. void adc_inserted_channel_offset_config(uint32_t adc_periph , uint8_t inserted_channel , uint16_t offset)
  763. {
  764. /* config the offset of the selected channels */
  765. if(ADC_INSERTED_CHANNEL_0 == inserted_channel){
  766. ADC_IOFF0(adc_periph) = (uint32_t)offset;
  767. }else if(ADC_INSERTED_CHANNEL_1 == inserted_channel){
  768. ADC_IOFF1(adc_periph) = (uint32_t)offset;
  769. }else if(ADC_INSERTED_CHANNEL_2 == inserted_channel){
  770. ADC_IOFF2(adc_periph) = (uint32_t)offset;
  771. }else if(ADC_INSERTED_CHANNEL_3 == inserted_channel){
  772. ADC_IOFF3(adc_periph) = (uint32_t)offset;
  773. }else{
  774. }
  775. }
  776. /*!
  777. \brief ADC inserted group data register read
  778. \param[in] adc_periph: ADCx,x=0,1,2
  779. \param[in] inserted_channel : insert channel select
  780. \arg ADC_INSERTED_CHANNEL_0: inserted Channel0
  781. \arg ADC_INSERTED_CHANNEL_1: inserted channel1
  782. \arg ADC_INSERTED_CHANNEL_2: inserted Channel2
  783. \arg ADC_INSERTED_CHANNEL_3: inserted Channel3
  784. \param[out] none
  785. \retval the conversion value
  786. */
  787. uint16_t adc_inserted_data_read(uint32_t adc_periph , uint8_t inserted_channel)
  788. {
  789. uint32_t idata;
  790. /* read the data of the selected channel */
  791. switch(inserted_channel){
  792. case ADC_INSERTED_CHANNEL_0:
  793. idata = ADC_IDATA0(adc_periph);
  794. break;
  795. case ADC_INSERTED_CHANNEL_1:
  796. idata = ADC_IDATA1(adc_periph);
  797. break;
  798. case ADC_INSERTED_CHANNEL_2:
  799. idata = ADC_IDATA2(adc_periph);
  800. break;
  801. case ADC_INSERTED_CHANNEL_3:
  802. idata = ADC_IDATA3(adc_periph);
  803. break;
  804. default:
  805. idata = 0U;
  806. break;
  807. }
  808. return (uint16_t)idata;
  809. }
  810. /*!
  811. \brief DMA request enable
  812. \param[in] adc_periph: ADCx,x=0,1,2
  813. \param[out] none
  814. \retval none
  815. */
  816. void adc_dma_mode_enable(uint32_t adc_periph)
  817. {
  818. ADC_CTL1(adc_periph) |= (uint32_t)(ADC_CTL1_DMA);
  819. }
  820. /*!
  821. \brief DMA request disable
  822. \param[in] adc_periph: ADCx,x=0,1,2
  823. \param[out] none
  824. \retval none
  825. */
  826. void adc_dma_mode_disable(uint32_t adc_periph)
  827. {
  828. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DMA);
  829. }
  830. /*!
  831. \brief when DMA=1, the DMA engine issues a request at end of each regular conversion
  832. \param[in] adc_periph: ADCx,x=0,1,2
  833. \param[out] none
  834. \retval none
  835. */
  836. void adc_dma_request_after_last_enable(uint32_t adc_periph)
  837. {
  838. ADC_CTL1(adc_periph) |= (uint32_t)(ADC_CTL1_DDM);
  839. }
  840. /*!
  841. \brief the DMA engine is disabled after the end of transfer signal from DMA controller is detected
  842. \param[in] adc_periph: ADCx,x=0,1,2
  843. \param[out] none
  844. \retval none
  845. */
  846. void adc_dma_request_after_last_disable(uint32_t adc_periph)
  847. {
  848. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DDM);
  849. }
  850. /*!
  851. \brief ADC oversample mode config
  852. \param[in] adc_periph: ADCx,x=0,1,2
  853. \param[in] mode: ADC oversampling mode
  854. \arg ADC_OVERSAMPLING_ALL_CONVERT: all oversampled conversions for a channel are done consecutively after a trigger
  855. \arg ADC_OVERSAMPLING_ONE_CONVERT: each oversampled conversion for a channel needs a trigger
  856. \param[in] shift: ADC oversampling shift
  857. \arg ADC_OVERSAMPLING_SHIFT_NONE: no oversampling shift
  858. \arg ADC_OVERSAMPLING_SHIFT_1B: 1-bit oversampling shift
  859. \arg ADC_OVERSAMPLING_SHIFT_2B: 2-bit oversampling shift
  860. \arg ADC_OVERSAMPLING_SHIFT_3B: 3-bit oversampling shift
  861. \arg ADC_OVERSAMPLING_SHIFT_4B: 3-bit oversampling shift
  862. \arg ADC_OVERSAMPLING_SHIFT_5B: 5-bit oversampling shift
  863. \arg ADC_OVERSAMPLING_SHIFT_6B: 6-bit oversampling shift
  864. \arg ADC_OVERSAMPLING_SHIFT_7B: 7-bit oversampling shift
  865. \arg ADC_OVERSAMPLING_SHIFT_8B: 8-bit oversampling shift
  866. \param[in] ratio: ADC oversampling ratio
  867. \arg ADC_OVERSAMPLING_RATIO_MUL2: oversampling ratio multiple 2
  868. \arg ADC_OVERSAMPLING_RATIO_MUL4: oversampling ratio multiple 4
  869. \arg ADC_OVERSAMPLING_RATIO_MUL8: oversampling ratio multiple 8
  870. \arg ADC_OVERSAMPLING_RATIO_MUL16: oversampling ratio multiple 16
  871. \arg ADC_OVERSAMPLING_RATIO_MUL32: oversampling ratio multiple 32
  872. \arg ADC_OVERSAMPLING_RATIO_MUL64: oversampling ratio multiple 64
  873. \arg ADC_OVERSAMPLING_RATIO_MUL128: oversampling ratio multiple 128
  874. \arg ADC_OVERSAMPLING_RATIO_MUL256: oversampling ratio multiple 256
  875. \param[out] none
  876. \retval none
  877. */
  878. void adc_oversample_mode_config(uint32_t adc_periph , uint8_t mode , uint16_t shift , uint8_t ratio)
  879. {
  880. if(ADC_OVERSAMPLING_ONE_CONVERT == mode){
  881. ADC_OVSAMPCTL(adc_periph) |= (uint32_t)ADC_OVSAMPCTL_TOVS;
  882. }else{
  883. ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)ADC_OVSAMPCTL_TOVS);
  884. }
  885. /* config the shift and ratio */
  886. ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)(ADC_OVSAMPCTL_OVSR | ADC_OVSAMPCTL_OVSS));
  887. ADC_OVSAMPCTL(adc_periph) |= ((uint32_t)shift | (uint32_t)ratio);
  888. }
  889. /*!
  890. \brief ADC oversample mode enable
  891. \param[in] adc_periph: ADCx,x=0,1,2
  892. \param[out] none
  893. \retval none
  894. */
  895. void adc_oversample_mode_enable(uint32_t adc_periph)
  896. {
  897. ADC_OVSAMPCTL(adc_periph) |= ADC_OVSAMPCTL_OVSEN;
  898. }
  899. /*!
  900. \brief ADC oversample mode disable
  901. \param[in] adc_periph: ADCx,x=0,1,2
  902. \param[out] none
  903. \retval none
  904. */
  905. void adc_oversample_mode_disable(uint32_t adc_periph)
  906. {
  907. ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)ADC_OVSAMPCTL_OVSEN);
  908. }
  909. /*!
  910. \brief configure the ADC sync mode
  911. \param[in] sync_mode: ADC sync mode
  912. \arg ADC_SYNC_MODE_INDEPENDENT: all the ADCs work independently
  913. \arg ADC_DAUL_REGULAL_PARALLEL_INSERTED_PARALLEL: ADC0 and ADC1 work in combined regular parallel & inserted parallel mode
  914. \arg ADC_DAUL_REGULAL_PARALLEL_INSERTED_ROTATION: ADC0 and ADC1 work in combined regular parallel & trigger rotation mode
  915. \arg ADC_DAUL_INSERTED_PARALLEL: ADC0 and ADC1 work in inserted parallel mode
  916. \arg ADC_DAUL_REGULAL_PARALLEL: ADC0 and ADC1 work in regular parallel mode
  917. \arg ADC_DAUL_REGULAL_FOLLOW_UP: ADC0 and ADC1 work in follow-up mode
  918. \arg ADC_DAUL_INSERTED_TRRIGGER_ROTATION: ADC0 and ADC1 work in trigger rotation mode
  919. \arg ADC_ALL_REGULAL_PARALLEL_INSERTED_PARALLEL: all ADCs work in combined regular parallel & inserted parallel mode
  920. \arg ADC_ALL_REGULAL_PARALLEL_INSERTED_ROTATION: all ADCs work in combined regular parallel & trigger rotation mode
  921. \arg ADC_ALL_INSERTED_PARALLEL: all ADCs work in inserted parallel mode
  922. \arg ADC_ALL_REGULAL_PARALLEL: all ADCs work in regular parallel mode
  923. \arg ADC_ALL_REGULAL_FOLLOW_UP: all ADCs work in follow-up mode
  924. \arg ADC_ALL_INSERTED_TRRIGGER_ROTATION: all ADCs work in trigger rotation mode
  925. \param[out] none
  926. \retval none
  927. */
  928. void adc_sync_mode_config(uint32_t sync_mode)
  929. {
  930. ADC_SYNCCTL &= ~(ADC_SYNCCTL_SYNCM);
  931. ADC_SYNCCTL |= sync_mode;
  932. }
  933. /*!
  934. \brief configure the delay between 2 sampling phases in ADC sync modes
  935. \param[in] sample_delay: the delay between 2 sampling phases in ADC sync modes
  936. \arg ADC_SYNC_DELAY_xCYCLE: x=5..20,the delay between 2 sampling phases in ADC sync modes is x ADC clock cycles
  937. \param[out] none
  938. \retval none
  939. */
  940. void adc_sync_delay_config(uint32_t sample_delay)
  941. {
  942. ADC_SYNCCTL &= ~(ADC_SYNCCTL_SYNCDLY);
  943. ADC_SYNCCTL |= sample_delay;
  944. }
  945. /*!
  946. \brief configure ADC sync DMA mode selection
  947. \param[in] dma_mode: ADC sync DMA mode
  948. \arg ADC_SYNC_DMA_DISABLE: ADC sync DMA disabled
  949. \arg ADC_SYNC_DMA_MODE0: ADC sync DMA disabled
  950. \arg ADC_SYNC_DMA_MODE1: ADC sync DMA disabled
  951. \param[out] none
  952. \retval none
  953. */
  954. void adc_sync_dma_config(uint32_t dma_mode )
  955. {
  956. ADC_SYNCCTL &= ~(ADC_SYNCCTL_SYNCDMA);
  957. ADC_SYNCCTL |= dma_mode;
  958. }
  959. /*!
  960. \brief configure ADC sync DMA engine is disabled after the end of transfer signal from DMA controller is detected
  961. \param[in] none
  962. \param[out] none
  963. \retval none
  964. */
  965. void adc_sync_dma_request_after_last_enable(void)
  966. {
  967. ADC_SYNCCTL |= ADC_SYNCCTL_SYNCDDM;
  968. }
  969. /*!
  970. \brief configure ADC sync DMA engine issues requests according to the SYNCDMA bits
  971. \param[in] none
  972. \param[out] none
  973. \retval none
  974. */
  975. void adc_sync_dma_request_after_last_disable(void)
  976. {
  977. ADC_SYNCCTL &= ~(ADC_SYNCCTL_SYNCDDM);
  978. }
  979. /*!
  980. \brief ADC sync regular data register read
  981. \param[in] none
  982. \param[out] none
  983. \retval sync regular data
  984. */
  985. uint32_t adc_sync_regular_data_read(void)
  986. {
  987. return (uint32_t)ADC_SYNCDATA;
  988. }