gd32f3x0_adc.c 29 KB

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