gd32f3x0_adc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*!
  2. \file gd32f3x0_adc.c
  3. \brief ADC driver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0_adc.h"
  10. /*!
  11. \brief reset ADC
  12. \param[in] none
  13. \param[out] none
  14. \retval none
  15. */
  16. void adc_deinit(void)
  17. {
  18. rcu_periph_reset_enable(RCU_ADCRST);
  19. rcu_periph_reset_disable(RCU_ADCRST);
  20. }
  21. /*!
  22. \brief enable ADC interface
  23. \param[in] none
  24. \param[out] none
  25. \retval none
  26. */
  27. void adc_enable(void)
  28. {
  29. if(RESET == (ADC_CTL1 & ADC_CTL1_ADCON)){
  30. ADC_CTL1 |= (uint32_t)ADC_CTL1_ADCON;
  31. }
  32. }
  33. /*!
  34. \brief disable ADC interface
  35. \param[in] none
  36. \param[out] none
  37. \retval none
  38. */
  39. void adc_disable(void)
  40. {
  41. ADC_CTL1 &= ~((uint32_t)ADC_CTL1_ADCON);
  42. }
  43. /*!
  44. \brief ADC calibration and reset calibration
  45. \param[in] none
  46. \param[out] none
  47. \retval none
  48. */
  49. void adc_calibration_enable(void)
  50. {
  51. /* reset the selected ADC calibration register */
  52. ADC_CTL1 |= (uint32_t) ADC_CTL1_RSTCLB;
  53. /* check the RSTCLB bit state */
  54. while((ADC_CTL1 & ADC_CTL1_RSTCLB)){
  55. }
  56. /* enable ADC calibration process */
  57. ADC_CTL1 |= ADC_CTL1_CLB;
  58. /* check the CLB bit state */
  59. while((ADC_CTL1 & ADC_CTL1_CLB)){
  60. }
  61. }
  62. /*!
  63. \brief enable DMA request
  64. \param[in] none
  65. \param[out] none
  66. \retval none
  67. */
  68. void adc_dma_mode_enable(void)
  69. {
  70. ADC_CTL1 |= (uint32_t)(ADC_CTL1_DMA);
  71. }
  72. /*!
  73. \brief disable DMA request
  74. \param[in] none
  75. \param[out] none
  76. \retval none
  77. */
  78. void adc_dma_mode_disable(void)
  79. {
  80. ADC_CTL1 &= ~((uint32_t)ADC_CTL1_DMA);
  81. }
  82. /*!
  83. \brief enable the temperature sensor and Vrefint channel
  84. \param[in] none
  85. \param[out] none
  86. \retval none
  87. */
  88. void adc_tempsensor_vrefint_enable(void)
  89. {
  90. /* enable the temperature sensor and Vrefint channel */
  91. ADC_CTL1 |= ADC_CTL1_TSVREN;
  92. }
  93. /*!
  94. \brief disable the temperature sensor and Vrefint channel
  95. \param[in] none
  96. \param[out] none
  97. \retval none
  98. */
  99. void adc_tempsensor_vrefint_disable(void)
  100. {
  101. /* disable the temperature sensor and Vrefint channel */
  102. ADC_CTL1 &= ~ADC_CTL1_TSVREN;
  103. }
  104. /*!
  105. \brief enable the vbat channel
  106. \param[in] none
  107. \param[out] none
  108. \retval none
  109. */
  110. void adc_vbat_enable(void)
  111. {
  112. /* enable the vbat channel */
  113. ADC_CTL1 |= ADC_CTL1_VBETEN;
  114. }
  115. /*!
  116. \brief disable the vbat channel
  117. \param[in] none
  118. \param[out] none
  119. \retval none
  120. */
  121. void adc_vbat_disable(void)
  122. {
  123. /* disable the vbat channel */
  124. ADC_CTL1 &= ~ADC_CTL1_VBETEN;
  125. }
  126. /*!
  127. \brief configure ADC resolution
  128. \param[in] resolution: ADC resolution
  129. only one among these parameters can be selected
  130. \arg ADC_RESOLUTION_12B: 12-bit ADC resolution
  131. \arg ADC_RESOLUTION_10B: 10-bit ADC resolution
  132. \arg ADC_RESOLUTION_8B: 8-bit ADC resolution
  133. \arg ADC_RESOLUTION_6B: 6-bit ADC resolution
  134. \param[out] none
  135. \retval none
  136. */
  137. void adc_resolution_config(uint32_t resolution)
  138. {
  139. ADC_CTL0 &= ~((uint32_t)ADC_CTL0_DRES);
  140. ADC_CTL0 |= (uint32_t)resolution;
  141. }
  142. /*!
  143. \brief configure ADC discontinuous mode
  144. \param[in] channel_group: select the channel group
  145. only one among these parameters can be selected
  146. \arg ADC_REGULAR_CHANNEL: regular channel group
  147. \arg ADC_INSERTED_CHANNEL: inserted channel group
  148. \arg ADC_CHANNEL_DISCON_DISABLE: disable discontinuous mode of regular and inserted channel
  149. \param[in] length: number of conversions in discontinuous mode,the number can be 1..8
  150. for regular channel, the number has no effect for inserted channel
  151. \param[out] none
  152. \retval none
  153. */
  154. void adc_discontinuous_mode_config(uint8_t channel_group, uint8_t length)
  155. {
  156. ADC_CTL0 &= ~((uint32_t)(ADC_CTL0_DISRC | ADC_CTL0_DISIC));
  157. switch(channel_group){
  158. case ADC_REGULAR_CHANNEL:
  159. /* configure the number of conversions in discontinuous mode */
  160. ADC_CTL0 &= ~((uint32_t)ADC_CTL0_DISNUM);
  161. ADC_CTL0 |= CTL0_DISNUM(((uint32_t)length - 1U));
  162. ADC_CTL0 |= (uint32_t)ADC_CTL0_DISRC;
  163. break;
  164. case ADC_INSERTED_CHANNEL:
  165. ADC_CTL0 |= (uint32_t)ADC_CTL0_DISIC;
  166. break;
  167. case ADC_CHANNEL_DISCON_DISABLE:
  168. default:
  169. break;
  170. }
  171. }
  172. /*!
  173. \brief configure ADC special function
  174. \param[in] function: the function to configure
  175. one or more parameters can be selected below:
  176. \arg ADC_SCAN_MODE: scan mode select
  177. \arg ADC_INSERTED_CHANNEL_AUTO: inserted channel group convert automatically
  178. \arg ADC_CONTINUOUS_MODE: continuous mode select
  179. \param[in] newvalue: ENABLE or DISABLE
  180. \param[out] none
  181. \retval none
  182. */
  183. void adc_special_function_config(uint32_t function, ControlStatus newvalue)
  184. {
  185. if(newvalue){
  186. /* enable ADC scan mode */
  187. if(RESET != (function & ADC_SCAN_MODE)){
  188. ADC_CTL0 |= ADC_SCAN_MODE;
  189. }
  190. /* enable ADC inserted channel group convert automatically */
  191. if(RESET != (function & ADC_INSERTED_CHANNEL_AUTO)){
  192. ADC_CTL0 |= ADC_INSERTED_CHANNEL_AUTO;
  193. }
  194. /* enable ADC continuous mode */
  195. if(RESET != (function & ADC_CONTINUOUS_MODE)){
  196. ADC_CTL1 |= ADC_CONTINUOUS_MODE;
  197. }
  198. }else{
  199. /* disable ADC scan mode */
  200. if(RESET != (function & ADC_SCAN_MODE)){
  201. ADC_CTL0 &= ~ADC_SCAN_MODE;
  202. }
  203. /* disable ADC inserted channel group convert automatically */
  204. if(RESET != (function & ADC_INSERTED_CHANNEL_AUTO)){
  205. ADC_CTL0 &= ~ADC_INSERTED_CHANNEL_AUTO;
  206. }
  207. /* disable ADC continuous mode */
  208. if(RESET != (function & ADC_CONTINUOUS_MODE)){
  209. ADC_CTL1 &= ~ADC_CONTINUOUS_MODE;
  210. }
  211. }
  212. }
  213. /*!
  214. \brief configure ADC data alignment
  215. \param[in] data_alignment: data alignment select
  216. only one parameter can be selected
  217. \arg ADC_DATAALIGN_RIGHT: LSB alignment
  218. \arg ADC_DATAALIGN_LEFT: MSB alignment
  219. \param[out] none
  220. \retval none
  221. */
  222. void adc_data_alignment_config(uint32_t data_alignment)
  223. {
  224. if(ADC_DATAALIGN_RIGHT != data_alignment){
  225. ADC_CTL1 |= ADC_CTL1_DAL;
  226. }else{
  227. ADC_CTL1 &= ~((uint32_t)ADC_CTL1_DAL);
  228. }
  229. }
  230. /*!
  231. \brief configure the length of regular channel group or inserted channel group
  232. \param[in] channel_group: select the channel group
  233. only one parameter can be selected
  234. \arg ADC_REGULAR_CHANNEL: regular channel group
  235. \arg ADC_INSERTED_CHANNEL: inserted channel group
  236. \param[in] length: the length of the channel
  237. regular channel 1-16
  238. inserted channel 1-4
  239. \param[out] none
  240. \retval none
  241. */
  242. void adc_channel_length_config(uint8_t channel_group, uint32_t length)
  243. {
  244. switch(channel_group){
  245. case ADC_REGULAR_CHANNEL:
  246. /* configure the length of regular channel group */
  247. ADC_RSQ0 &= ~((uint32_t)ADC_RSQ0_RL);
  248. ADC_RSQ0 |= RSQ0_RL((uint32_t)(length-1U));
  249. break;
  250. case ADC_INSERTED_CHANNEL:
  251. /* configure the length of inserted channel group */
  252. ADC_ISQ &= ~((uint32_t)ADC_ISQ_IL);
  253. ADC_ISQ |= ISQ_IL((uint32_t)(length-1U));
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. /*!
  260. \brief configure ADC regular channel
  261. \param[in] rank: the regular group sequence rank, this parameter must be between 0 to 15
  262. \param[in] channel: the selected ADC channel
  263. only one among these parameters can be selected
  264. \arg ADC_CHANNEL_x(x=0..18): ADC Channelx
  265. \param[in] sample_time: the sample time value
  266. only one parameter can be selected
  267. \arg ADC_SAMPLETIME_1POINT5: 1.5 cycles
  268. \arg ADC_SAMPLETIME_7POINT5: 7.5 cycles
  269. \arg ADC_SAMPLETIME_13POINT5: 13.5 cycles
  270. \arg ADC_SAMPLETIME_28POINT5: 28.5 cycles
  271. \arg ADC_SAMPLETIME_41POINT5: 41.5 cycles
  272. \arg ADC_SAMPLETIME_55POINT5: 55.5 cycles
  273. \arg ADC_SAMPLETIME_71POINT5: 71.5 cycles
  274. \arg ADC_SAMPLETIME_239POINT5: 239.5 cycles
  275. \param[out] none
  276. \retval none
  277. */
  278. void adc_regular_channel_config(uint8_t rank, uint8_t channel, uint32_t sample_time)
  279. {
  280. uint32_t rsq,sampt;
  281. /* configure ADC regular sequence */
  282. if(rank < 6U){
  283. rsq = ADC_RSQ2;
  284. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (5U*rank)));
  285. rsq |= ((uint32_t)channel << (5U*rank));
  286. ADC_RSQ2 = rsq;
  287. }else if(rank < 12U){
  288. rsq = ADC_RSQ1;
  289. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (5U*(rank-6U))));
  290. rsq |= ((uint32_t)channel << (5U*(rank-6U)));
  291. ADC_RSQ1 = rsq;
  292. }else if(rank < 16U){
  293. rsq = ADC_RSQ0;
  294. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (5U*(rank-12U))));
  295. rsq |= ((uint32_t)channel << (5U*(rank-12U)));
  296. ADC_RSQ0 = rsq;
  297. }else{
  298. }
  299. /* configure ADC sampling time */
  300. if(channel < 10U){
  301. sampt = ADC_SAMPT1;
  302. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*channel)));
  303. sampt |= (uint32_t)(sample_time << (3U*channel));
  304. ADC_SAMPT1 = sampt;
  305. }else if(channel < 19U){
  306. sampt = ADC_SAMPT0;
  307. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*(channel-10U))));
  308. sampt |= (uint32_t)(sample_time << (3U*(channel-10U)));
  309. ADC_SAMPT0 = sampt;
  310. }else{
  311. /* illegal parameters */
  312. }
  313. }
  314. /*!
  315. \brief configure ADC inserted channel
  316. \param[in] rank: the inserted group sequencer rank,this parameter must be between 0 to 3
  317. \param[in] channel: the selected ADC channel
  318. only one among these parameters can be selected
  319. \arg ADC_CHANNEL_x(x=0..18): ADC Channelx
  320. \param[in] sample_time: The sample time value
  321. only one parameter can be selected
  322. \arg ADC_SAMPLETIME_1POINT5: 1.5 cycles
  323. \arg ADC_SAMPLETIME_7POINT5: 7.5 cycles
  324. \arg ADC_SAMPLETIME_13POINT5: 13.5 cycles
  325. \arg ADC_SAMPLETIME_28POINT5: 28.5 cycles
  326. \arg ADC_SAMPLETIME_41POINT5: 41.5 cycles
  327. \arg ADC_SAMPLETIME_55POINT5: 55.5 cycles
  328. \arg ADC_SAMPLETIME_71POINT5: 71.5 cycles
  329. \arg ADC_SAMPLETIME_239POINT5: 239.5 cycles
  330. \param[out] none
  331. \retval none
  332. */
  333. void adc_inserted_channel_config(uint8_t rank, uint8_t channel, uint32_t sample_time)
  334. {
  335. uint8_t inserted_length;
  336. uint32_t isq,sampt;
  337. inserted_length = (uint8_t)GET_BITS(ADC_ISQ , 20U , 21U);
  338. isq = ADC_ISQ;
  339. isq &= ~((uint32_t)(ADC_ISQ_ISQN << (15U - (inserted_length - rank)*5U)));
  340. isq |= ((uint32_t)channel << (15U - (inserted_length - rank)*5U));
  341. ADC_ISQ = isq;
  342. /* configure ADC sampling time */
  343. if(channel < 10U){
  344. sampt = ADC_SAMPT1;
  345. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*channel)));
  346. sampt |= (uint32_t) sample_time << (3U*channel);
  347. ADC_SAMPT1 = sampt;
  348. }else if(channel < 19U){
  349. sampt = ADC_SAMPT0;
  350. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (3U*(channel - 10U))));
  351. sampt |= ((uint32_t)sample_time << (3U*(channel - 10U)));
  352. ADC_SAMPT0 = sampt;
  353. }else{
  354. /* illegal parameters */
  355. }
  356. }
  357. /*!
  358. \brief configure ADC inserted channel offset
  359. \param[in] inserted_channel: insert channel select
  360. only one parameter can be selected
  361. \arg ADC_INSERTED_CHANNEL_0: ADC inserted channel 0
  362. \arg ADC_INSERTED_CHANNEL_1: ADC inserted channel 1
  363. \arg ADC_INSERTED_CHANNEL_2: ADC inserted channel 2
  364. \arg ADC_INSERTED_CHANNEL_3: ADC inserted channel 3
  365. \param[in] offset: the offset data
  366. \param[out] none
  367. \retval none
  368. */
  369. void adc_inserted_channel_offset_config(uint8_t inserted_channel, uint16_t offset)
  370. {
  371. uint8_t inserted_length;
  372. uint32_t num = 0U;
  373. inserted_length = (uint8_t)GET_BITS(ADC_ISQ, 20U, 21U);
  374. num = 3U - (inserted_length - inserted_channel);
  375. if(num <= 3U){
  376. /* calculate the offset of the register */
  377. num = num * 4U;
  378. /* configure the offset of the selected channels */
  379. REG32((ADC) + 0x14U + num) = IOFFX_IOFF((uint32_t)offset);
  380. }
  381. }
  382. /*!
  383. \brief enable or disable ADC external trigger
  384. \param[in] channel_group: select the channel group
  385. one or more parameters can be selected
  386. \arg ADC_REGULAR_CHANNEL: regular channel group
  387. \arg ADC_INSERTED_CHANNEL: inserted channel group
  388. \param[in] newvalue: ENABLE or DISABLE
  389. \param[out] none
  390. \retval none
  391. */
  392. void adc_external_trigger_config(uint8_t channel_group, ControlStatus newvalue)
  393. {
  394. if(newvalue){
  395. /* external trigger enable for regular channel */
  396. if(RESET != (channel_group & ADC_REGULAR_CHANNEL)){
  397. ADC_CTL1 |= ADC_CTL1_ETERC;
  398. }
  399. /* external trigger enable for inserted channel */
  400. if(RESET != (channel_group & ADC_INSERTED_CHANNEL)){
  401. ADC_CTL1 |= ADC_CTL1_ETEIC;
  402. }
  403. }else{
  404. /* external trigger disable for regular channel */
  405. if(RESET != (channel_group & ADC_REGULAR_CHANNEL)){
  406. ADC_CTL1 &= ~ADC_CTL1_ETERC;
  407. }
  408. /* external trigger disable for inserted channel */
  409. if(RESET != (channel_group & ADC_INSERTED_CHANNEL)){
  410. ADC_CTL1 &= ~ADC_CTL1_ETEIC;
  411. }
  412. }
  413. }
  414. /*!
  415. \brief configure ADC external trigger source
  416. \param[in] channel_group: select the channel group
  417. only one parameter can be selected
  418. \arg ADC_REGULAR_CHANNEL: regular channel group
  419. \arg ADC_INSERTED_CHANNEL: inserted channel group
  420. \param[in] external_trigger_source: regular or inserted group trigger source
  421. only one parameter can be selected
  422. for regular channel:
  423. \arg ADC_EXTTRIG_REGULAR_T0_CH0: TIMER0 CH0 event select
  424. \arg ADC_EXTTRIG_REGULAR_T0_CH1: TIMER0 CH1 event select
  425. \arg ADC_EXTTRIG_REGULAR_T0_CH2: TIMER0 CH2 event select
  426. \arg ADC_EXTTRIG_REGULAR_T1_CH1: TIMER1 CH1 event select
  427. \arg ADC_EXTTRIG_REGULAR_T2_TRGO: TIMER2 TRGO event select
  428. \arg ADC_EXTTRIG_REGULAR_T14_CH1: TIMER14 CH1 event select
  429. \arg ADC_EXTTRIG_REGULAR_EXTI_11: external interrupt line 11
  430. \arg ADC_EXTTRIG_REGULAR_NONE: software trigger
  431. for inserted channel:
  432. \arg ADC_EXTTRIG_INSERTED_T0_TRGO: TIMER0 TRGO event select
  433. \arg ADC_EXTTRIG_INSERTED_T0_CH3: TIMER0 CH3 event select
  434. \arg ADC_EXTTRIG_INSERTED_T1_TRGO: TIMER1 TRGO event select
  435. \arg ADC_EXTTRIG_INSERTED_T1_CH0: TIMER1 CH0 event select
  436. \arg ADC_EXTTRIG_INSERTED_T2_CH3: TIMER2 CH3 event select
  437. \arg ADC_EXTTRIG_INSERTED_T14_TRGO: TIMER14 TRGO event select
  438. \arg ADC_EXTTRIG_INSERTED_EXTI_15: external interrupt line 15
  439. \arg ADC_EXTTRIG_INSERTED_NONE: software trigger
  440. \param[out] none
  441. \retval none
  442. */
  443. void adc_external_trigger_source_config(uint8_t channel_group, uint32_t external_trigger_source)
  444. {
  445. switch(channel_group){
  446. case ADC_REGULAR_CHANNEL:
  447. /* external trigger select for regular channel */
  448. ADC_CTL1 &= ~((uint32_t)ADC_CTL1_ETSRC);
  449. ADC_CTL1 |= (uint32_t)external_trigger_source;
  450. break;
  451. case ADC_INSERTED_CHANNEL:
  452. /* external trigger select for inserted channel */
  453. ADC_CTL1 &= ~((uint32_t)ADC_CTL1_ETSIC);
  454. ADC_CTL1 |= (uint32_t)external_trigger_source;
  455. break;
  456. default:
  457. break;
  458. }
  459. }
  460. /*!
  461. \brief enable ADC software trigger
  462. \param[in] channel_group: select the channel group
  463. one or more parameters can be selected
  464. \arg ADC_REGULAR_CHANNEL: regular channel group
  465. \arg ADC_INSERTED_CHANNEL: inserted channel group
  466. \param[out] none
  467. \retval none
  468. */
  469. void adc_software_trigger_enable(uint8_t channel_group)
  470. {
  471. /* enable regular group channel software trigger */
  472. if(RESET != (channel_group & ADC_REGULAR_CHANNEL)){
  473. ADC_CTL1 |= ADC_CTL1_SWRCST;
  474. }
  475. /* enable inserted channel group software trigger */
  476. if(RESET != (channel_group & ADC_INSERTED_CHANNEL)){
  477. ADC_CTL1 |= ADC_CTL1_SWICST;
  478. }
  479. }
  480. /*!
  481. \brief read ADC regular group data register
  482. \param[in] none
  483. \param[out] none
  484. \retval the conversion value
  485. */
  486. uint16_t adc_regular_data_read(void)
  487. {
  488. return ((uint16_t)ADC_RDATA);
  489. }
  490. /*!
  491. \brief read ADC inserted group data register
  492. \param[in] inserted_channel: inserted channel select
  493. only one parameter can be selected
  494. \arg ADC_INSERTED_CHANNEL_0: ADC inserted channel 0
  495. \arg ADC_INSERTED_CHANNEL_1: ADC inserted channel 1
  496. \arg ADC_INSERTED_CHANNEL_2: ADC inserted channel 2
  497. \arg ADC_INSERTED_CHANNEL_3: ADC inserted channel 3
  498. \param[out] none
  499. \retval the conversion value
  500. */
  501. uint16_t adc_inserted_data_read(uint8_t inserted_channel)
  502. {
  503. uint32_t idata;
  504. /* read the data of the selected channel */
  505. switch(inserted_channel){
  506. case ADC_INSERTED_CHANNEL_0:
  507. idata = ADC_IDATA0;
  508. break;
  509. case ADC_INSERTED_CHANNEL_1:
  510. idata = ADC_IDATA1;
  511. break;
  512. case ADC_INSERTED_CHANNEL_2:
  513. idata = ADC_IDATA2;
  514. break;
  515. case ADC_INSERTED_CHANNEL_3:
  516. idata = ADC_IDATA3;
  517. break;
  518. default:
  519. idata = 0U;
  520. break;
  521. }
  522. return (uint16_t)idata;
  523. }
  524. /*!
  525. \brief get the ADC flag bits
  526. \param[in] flag: the adc flag bits
  527. only one parameter can be selected
  528. \arg ADC_FLAG_WDE: analog watchdog event flag
  529. \arg ADC_FLAG_EOC: end of group conversion flag
  530. \arg ADC_FLAG_EOIC: end of inserted group conversion flag
  531. \arg ADC_FLAG_STIC: start flag of inserted channel group
  532. \arg ADC_FLAG_STRC: start flag of regular channel group
  533. \param[out] none
  534. \retval FlagStatus: SET or RESET
  535. */
  536. FlagStatus adc_flag_get(uint32_t flag)
  537. {
  538. FlagStatus reval = RESET;
  539. if(ADC_STAT & flag){
  540. reval = SET;
  541. }
  542. return reval;
  543. }
  544. /*!
  545. \brief clear the ADC flag
  546. \param[in] flag: the adc flag
  547. one or more parameters can be selected
  548. \arg ADC_FLAG_WDE: analog watchdog event flag
  549. \arg ADC_FLAG_EOC: end of group conversion flag
  550. \arg ADC_FLAG_EOIC: end of inserted group conversion flag
  551. \arg ADC_FLAG_STIC: start flag of inserted channel group
  552. \arg ADC_FLAG_STRC: start flag of regular channel group
  553. \param[out] none
  554. \retval none
  555. */
  556. void adc_flag_clear(uint32_t flag)
  557. {
  558. ADC_STAT &= ~((uint32_t)flag);
  559. }
  560. /*!
  561. \brief get the ADC interrupt flag
  562. \param[in] flag: the adc interrupt flag
  563. only oneparameter can be selected
  564. \arg ADC_INT_FLAG_WDE: analog watchdog interrupt flag
  565. \arg ADC_INT_FLAG_EOC: end of group conversion interrupt flag
  566. \arg ADC_INT_FLAG_EOIC: end of inserted group conversion interrupt flag
  567. \param[out] none
  568. \retval FlagStatus: SET or RESET
  569. */
  570. FlagStatus adc_interrupt_flag_get(uint32_t flag)
  571. {
  572. FlagStatus interrupt_flag = RESET;
  573. uint32_t state;
  574. /* check the interrupt bits */
  575. switch(flag){
  576. case ADC_INT_FLAG_WDE:
  577. state = ADC_STAT & ADC_STAT_WDE;
  578. if((ADC_CTL0 & ADC_CTL0_WDEIE) && state){
  579. interrupt_flag = SET;
  580. }
  581. break;
  582. case ADC_INT_FLAG_EOC:
  583. state = ADC_STAT & ADC_STAT_EOC;
  584. if((ADC_CTL0 & ADC_CTL0_EOCIE) && state){
  585. interrupt_flag = SET;
  586. }
  587. break;
  588. case ADC_INT_FLAG_EOIC:
  589. state = ADC_STAT & ADC_STAT_EOIC;
  590. if((ADC_CTL0 & ADC_CTL0_EOICIE) && state){
  591. interrupt_flag = SET;
  592. }
  593. break;
  594. default:
  595. break;
  596. }
  597. return interrupt_flag;
  598. }
  599. /*!
  600. \brief clear ADC interrupt flag
  601. \param[in] flag: the adc interrupt flag
  602. only oneparameter can be selected
  603. \arg ADC_INT_FLAG_WDE: analog watchdog interrupt flag
  604. \arg ADC_INT_FLAG_EOC: end of group conversion interrupt flag
  605. \arg ADC_INT_FLAG_EOIC: end of inserted group conversion interrupt flag
  606. \param[out] none
  607. \retval none
  608. */
  609. void adc_interrupt_flag_clear(uint32_t flag)
  610. {
  611. ADC_STAT &= ~((uint32_t)flag);
  612. }
  613. /*!
  614. \brief enable ADC interrupt
  615. \param[in] interrupt: the adc interrupt
  616. one or more parameters can be selected
  617. \arg ADC_INT_WDE: analog watchdog interrupt
  618. \arg ADC_INT_EOC: end of group conversion interrupt
  619. \arg ADC_INT_EOIC: end of inserted group conversion interrupt
  620. \param[out] none
  621. \retval none
  622. */
  623. void adc_interrupt_enable(uint32_t interrupt)
  624. {
  625. /* enable analog watchdog interrupt */
  626. if(RESET != (interrupt & ADC_INT_WDE)){
  627. ADC_CTL0 |= (uint32_t)ADC_CTL0_WDEIE;
  628. }
  629. /* enable end of group conversion interrupt */
  630. if(RESET != (interrupt & ADC_INT_EOC)){
  631. ADC_CTL0 |= (uint32_t)ADC_CTL0_EOCIE;
  632. }
  633. /* enable end of inserted group conversion interrupt */
  634. if(RESET != (interrupt & ADC_INT_EOIC)){
  635. ADC_CTL0 |= (uint32_t)ADC_CTL0_EOICIE;
  636. }
  637. }
  638. /*!
  639. \brief disable ADC interrupt
  640. \param[in] interrupt: the adc interrupt flag
  641. one or more parameters can be selected
  642. \arg ADC_INT_WDE: analog watchdog interrupt
  643. \arg ADC_INT_EOC: end of group conversion interrupt
  644. \arg ADC_INT_EOIC: end of inserted group conversion interrupt
  645. \param[out] none
  646. \retval none
  647. */
  648. void adc_interrupt_disable(uint32_t interrupt)
  649. {
  650. /* disable analog watchdog interrupt */
  651. if(RESET != (interrupt & ADC_INT_WDE)){
  652. ADC_CTL0 &= ~(uint32_t)ADC_CTL0_WDEIE;
  653. }
  654. /* disable end of group conversion interrupt */
  655. if(RESET != (interrupt & ADC_INT_EOC)){
  656. ADC_CTL0 &= ~(uint32_t)ADC_CTL0_EOCIE;
  657. }
  658. /* disable end of inserted group conversion interrupt */
  659. if(RESET != (interrupt & ADC_INT_EOIC)){
  660. ADC_CTL0 &= ~(uint32_t)ADC_CTL0_EOICIE;
  661. }
  662. }
  663. /*!
  664. \brief configure ADC analog watchdog single channel
  665. \param[in] channel: the selected ADC channel
  666. only one among these parameters can be selected
  667. \arg ADC_CHANNEL_x(x=0..18): ADC Channelx
  668. \param[out] none
  669. \retval none
  670. */
  671. void adc_watchdog_single_channel_enable(uint8_t channel)
  672. {
  673. ADC_CTL0 &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC | ADC_CTL0_WDCHSEL);
  674. ADC_CTL0 |= (uint32_t)channel;
  675. ADC_CTL0 |= (uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC);
  676. }
  677. /*!
  678. \brief configure ADC analog watchdog group channel
  679. \param[in] channel_group: the channel group use analog watchdog
  680. only one parameter can be selected
  681. \arg ADC_REGULAR_CHANNEL: regular channel group
  682. \arg ADC_INSERTED_CHANNEL: inserted channel group
  683. \arg ADC_REGULAR_INSERTED_CHANNEL: both regular and inserted group
  684. \param[out] none
  685. \retval none
  686. */
  687. void adc_watchdog_group_channel_enable(uint8_t channel_group)
  688. {
  689. ADC_CTL0 &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC);
  690. /* select the group */
  691. switch(channel_group){
  692. case ADC_REGULAR_CHANNEL:
  693. ADC_CTL0 |= (uint32_t)ADC_CTL0_RWDEN;
  694. break;
  695. case ADC_INSERTED_CHANNEL:
  696. ADC_CTL0 |= (uint32_t)ADC_CTL0_IWDEN;
  697. break;
  698. case ADC_REGULAR_INSERTED_CHANNEL:
  699. ADC_CTL0 |= (uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN);
  700. break;
  701. default:
  702. break;
  703. }
  704. }
  705. /*!
  706. \brief disable ADC analog watchdog
  707. \param[in] none
  708. \param[out] none
  709. \retval none
  710. */
  711. void adc_watchdog_disable(void)
  712. {
  713. ADC_CTL0 &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC | ADC_CTL0_WDCHSEL);
  714. }
  715. /*!
  716. \brief configure ADC analog watchdog threshold
  717. \param[in] low_threshold: analog watchdog low threshold,0..4095
  718. \param[in] high_threshold: analog watchdog high threshold,0..4095
  719. \param[out] none
  720. \retval none
  721. */
  722. void adc_watchdog_threshold_config(uint16_t low_threshold, uint16_t high_threshold)
  723. {
  724. ADC_WDLT = (uint32_t)WDLT_WDLT(low_threshold);
  725. ADC_WDHT = (uint32_t)WDHT_WDHT(high_threshold);
  726. }
  727. /*!
  728. \brief configure ADC oversample mode
  729. \param[in] mode: ADC oversampling mode
  730. only oneparameter can be selected
  731. \arg ADC_OVERSAMPLING_ALL_CONVERT: all oversampled conversions for a channel are done consecutively after a trigger
  732. \arg ADC_OVERSAMPLING_ONE_CONVERT: each oversampled conversion for a channel needs a trigger
  733. \param[in] shift: ADC oversampling shift
  734. only oneparameter can be selected
  735. \arg ADC_OVERSAMPLING_SHIFT_NONE: no oversampling shift
  736. \arg ADC_OVERSAMPLING_SHIFT_1B: 1-bit oversampling shift
  737. \arg ADC_OVERSAMPLING_SHIFT_2B: 2-bit oversampling shift
  738. \arg ADC_OVERSAMPLING_SHIFT_3B: 3-bit oversampling shift
  739. \arg ADC_OVERSAMPLING_SHIFT_4B: 3-bit oversampling shift
  740. \arg ADC_OVERSAMPLING_SHIFT_5B: 5-bit oversampling shift
  741. \arg ADC_OVERSAMPLING_SHIFT_6B: 6-bit oversampling shift
  742. \arg ADC_OVERSAMPLING_SHIFT_7B: 7-bit oversampling shift
  743. \arg ADC_OVERSAMPLING_SHIFT_8B: 8-bit oversampling shift
  744. \param[in] ratio: ADC oversampling ratio
  745. only oneparameter can be selected
  746. \arg ADC_OVERSAMPLING_RATIO_MUL2: oversampling ratio multiple 2
  747. \arg ADC_OVERSAMPLING_RATIO_MUL4: oversampling ratio multiple 4
  748. \arg ADC_OVERSAMPLING_RATIO_MUL8: oversampling ratio multiple 8
  749. \arg ADC_OVERSAMPLING_RATIO_MUL16: oversampling ratio multiple 16
  750. \arg ADC_OVERSAMPLING_RATIO_MUL32: oversampling ratio multiple 32
  751. \arg ADC_OVERSAMPLING_RATIO_MUL64: oversampling ratio multiple 64
  752. \arg ADC_OVERSAMPLING_RATIO_MUL128: oversampling ratio multiple 128
  753. \arg ADC_OVERSAMPLING_RATIO_MUL256: oversampling ratio multiple 256
  754. \param[out] none
  755. \retval none
  756. */
  757. void adc_oversample_mode_config(uint8_t mode, uint16_t shift, uint8_t ratio)
  758. {
  759. /* configure ADC oversampling mode */
  760. if(ADC_OVERSAMPLING_ONE_CONVERT == mode){
  761. ADC_OVSAMPCTL |= (uint32_t)ADC_OVSAMPCTL_TOVS;
  762. }else{
  763. ADC_OVSAMPCTL &= ~((uint32_t)ADC_OVSAMPCTL_TOVS);
  764. }
  765. /* configure the shift and ratio */
  766. ADC_OVSAMPCTL &= ~((uint32_t)(ADC_OVSAMPCTL_OVSR | ADC_OVSAMPCTL_OVSS));
  767. ADC_OVSAMPCTL |= ((uint32_t)shift | (uint32_t)ratio);
  768. }
  769. /*!
  770. \brief enable ADC oversample mode
  771. \param[in] none
  772. \param[out] none
  773. \retval none
  774. */
  775. void adc_oversample_mode_enable(void)
  776. {
  777. ADC_OVSAMPCTL |= ADC_OVSAMPCTL_OVSEN;
  778. }
  779. /*!
  780. \brief disable ADC oversample mode
  781. \param[in] none
  782. \param[out] none
  783. \retval none
  784. */
  785. void adc_oversample_mode_disable(void)
  786. {
  787. ADC_OVSAMPCTL &= ~((uint32_t)ADC_OVSAMPCTL_OVSEN);
  788. }