gd32e23x_adc.c 29 KB

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