gd32e23x_adc.c 28 KB

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