gd32vf103_adc.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /*!
  2. \file gd32vf103_adc.c
  3. \brief ADC driver
  4. \version 2019-6-5, V1.0.0, firmware for GD32VF103
  5. */
  6. /*
  7. Copyright (c) 2019, 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 "gd32vf103_adc.h"
  30. /* discontinuous mode macro*/
  31. #define ADC_CHANNEL_LENGTH_SUBTRACT_ONE ((uint8_t)1U)
  32. /* ADC regular channel macro */
  33. #define ADC_REGULAR_CHANNEL_RANK_SIX ((uint8_t)6U)
  34. #define ADC_REGULAR_CHANNEL_RANK_TWELVE ((uint8_t)12U)
  35. #define ADC_REGULAR_CHANNEL_RANK_SIXTEEN ((uint8_t)16U)
  36. #define ADC_REGULAR_CHANNEL_RANK_LENGTH ((uint8_t)5U)
  37. /* ADC sampling time macro */
  38. #define ADC_CHANNEL_SAMPLE_TEN ((uint8_t)10U)
  39. #define ADC_CHANNEL_SAMPLE_EIGHTEEN ((uint8_t)18U)
  40. #define ADC_CHANNEL_SAMPLE_LENGTH ((uint8_t)3U)
  41. /* ADC inserted channel macro */
  42. #define ADC_INSERTED_CHANNEL_RANK_LENGTH ((uint8_t)5U)
  43. #define ADC_INSERTED_CHANNEL_SHIFT_LENGTH ((uint8_t)15U)
  44. /* ADC inserted channel offset macro */
  45. #define ADC_OFFSET_LENGTH ((uint8_t)3U)
  46. #define ADC_OFFSET_SHIFT_LENGTH ((uint8_t)4U)
  47. /*!
  48. \brief reset ADC
  49. \param[in] adc_periph: ADCx, x=0,1
  50. \param[out] none
  51. \retval none
  52. */
  53. void adc_deinit(uint32_t adc_periph)
  54. {
  55. switch(adc_periph){
  56. case ADC0:
  57. /* reset ADC0 */
  58. rcu_periph_reset_enable(RCU_ADC0RST);
  59. rcu_periph_reset_disable(RCU_ADC0RST);
  60. break;
  61. case ADC1:
  62. /* reset ADC1 */
  63. rcu_periph_reset_enable(RCU_ADC1RST);
  64. rcu_periph_reset_disable(RCU_ADC1RST);
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. /*!
  71. \brief configure the ADC sync mode
  72. \param[in] mode: ADC mode
  73. only one parameter can be selected which is shown as below:
  74. \arg ADC_MODE_FREE: all the ADCs work independently
  75. \arg ADC_DAUL_REGULAL_PARALLEL_INSERTED_PARALLEL: ADC0 and ADC1 work in combined regular parallel + inserted parallel mode
  76. \arg ADC_DAUL_REGULAL_PARALLEL_INSERTED_ROTATION: ADC0 and ADC1 work in combined regular parallel + trigger rotation mode
  77. \arg ADC_DAUL_INSERTED_PARALLEL_REGULAL_FOLLOWUP_FAST: ADC0 and ADC1 work in combined inserted parallel + follow-up fast mode
  78. \arg ADC_DAUL_INSERTED_PARALLEL_REGULAL_FOLLOWUP_SLOW: ADC0 and ADC1 work in combined inserted parallel + follow-up slow mode
  79. \arg ADC_DAUL_INSERTED_PARALLEL: ADC0 and ADC1 work in inserted parallel mode only
  80. \arg ADC_DAUL_REGULAL_PARALLEL: ADC0 and ADC1 work in regular parallel mode only
  81. \arg ADC_DAUL_REGULAL_FOLLOWUP_FAST: ADC0 and ADC1 work in follow-up fast mode only
  82. \arg ADC_DAUL_REGULAL_FOLLOWUP_SLOW: ADC0 and ADC1 work in follow-up slow mode only
  83. \arg ADC_DAUL_INSERTED_TRIGGER_ROTATION: ADC0 and ADC1 work in trigger rotation mode only
  84. \param[out] none
  85. \retval none
  86. */
  87. void adc_mode_config(uint32_t mode)
  88. {
  89. ADC_CTL0(ADC0) &= ~(ADC_CTL0_SYNCM);
  90. ADC_CTL0(ADC0) |= mode;
  91. }
  92. /*!
  93. \brief enable or disable ADC special function
  94. \param[in] adc_periph: ADCx, x=0,1
  95. \param[in] function: the function to config
  96. only one parameter can be selected which is shown as below:
  97. \arg ADC_SCAN_MODE: scan mode select
  98. \arg ADC_INSERTED_CHANNEL_AUTO: inserted channel group convert automatically
  99. \arg ADC_CONTINUOUS_MODE: continuous mode select
  100. \param[in] newvalue: ENABLE or DISABLE
  101. \param[out] none
  102. \retval none
  103. */
  104. void adc_special_function_config(uint32_t adc_periph, uint32_t function, ControlStatus newvalue)
  105. {
  106. if(newvalue){
  107. if(0U != (function & ADC_SCAN_MODE)){
  108. /* enable scan mode */
  109. ADC_CTL0(adc_periph) |= ADC_SCAN_MODE;
  110. }
  111. if(0U != (function & ADC_INSERTED_CHANNEL_AUTO)){
  112. /* enable inserted channel group convert automatically */
  113. ADC_CTL0(adc_periph) |= ADC_INSERTED_CHANNEL_AUTO;
  114. }
  115. if(0U != (function & ADC_CONTINUOUS_MODE)){
  116. /* enable continuous mode */
  117. ADC_CTL1(adc_periph) |= ADC_CONTINUOUS_MODE;
  118. }
  119. }else{
  120. if(0U != (function & ADC_SCAN_MODE)){
  121. /* disable scan mode */
  122. ADC_CTL0(adc_periph) &= ~ADC_SCAN_MODE;
  123. }
  124. if(0U != (function & ADC_INSERTED_CHANNEL_AUTO)){
  125. /* disable inserted channel group convert automatically */
  126. ADC_CTL0(adc_periph) &= ~ADC_INSERTED_CHANNEL_AUTO;
  127. }
  128. if(0U != (function & ADC_CONTINUOUS_MODE)){
  129. /* disable continuous mode */
  130. ADC_CTL1(adc_periph) &= ~ADC_CONTINUOUS_MODE;
  131. }
  132. }
  133. }
  134. /*!
  135. \brief configure ADC data alignment
  136. \param[in] adc_periph: ADCx, x=0,1
  137. \param[in] data_alignment: data alignment select
  138. only one parameter can be selected which is shown as below:
  139. \arg ADC_DATAALIGN_RIGHT: LSB alignment
  140. \arg ADC_DATAALIGN_LEFT: MSB alignment
  141. \param[out] none
  142. \retval none
  143. */
  144. void adc_data_alignment_config(uint32_t adc_periph, uint32_t data_alignment)
  145. {
  146. if(ADC_DATAALIGN_RIGHT != data_alignment){
  147. /* MSB alignment */
  148. ADC_CTL1(adc_periph) |= ADC_CTL1_DAL;
  149. }else{
  150. /* LSB alignment */
  151. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DAL);
  152. }
  153. }
  154. /*!
  155. \brief enable ADC interface
  156. \param[in] adc_periph: ADCx, x=0,1
  157. \param[out] none
  158. \retval none
  159. */
  160. void adc_enable(uint32_t adc_periph)
  161. {
  162. if(RESET == (ADC_CTL1(adc_periph) & ADC_CTL1_ADCON)){
  163. /* enable ADC */
  164. ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_ADCON;
  165. }
  166. }
  167. /*!
  168. \brief disable ADC interface
  169. \param[in] adc_periph: ADCx, x=0,1
  170. \param[out] none
  171. \retval none
  172. */
  173. void adc_disable(uint32_t adc_periph)
  174. {
  175. /* disable ADC */
  176. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ADCON);
  177. }
  178. /*!
  179. \brief ADC calibration and reset calibration
  180. \param[in] adc_periph: ADCx, x=0,1
  181. \param[out] none
  182. \retval none
  183. */
  184. void adc_calibration_enable(uint32_t adc_periph)
  185. {
  186. /* reset the selected ADC1 calibration registers */
  187. ADC_CTL1(adc_periph) |= (uint32_t) ADC_CTL1_RSTCLB;
  188. /* check the RSTCLB bit state */
  189. while(RESET != (ADC_CTL1(adc_periph) & ADC_CTL1_RSTCLB)){
  190. }
  191. /* enable ADC calibration process */
  192. ADC_CTL1(adc_periph) |= ADC_CTL1_CLB;
  193. /* check the CLB bit state */
  194. while(RESET != (ADC_CTL1(adc_periph) & ADC_CTL1_CLB)){
  195. }
  196. }
  197. /*!
  198. \brief enable the temperature sensor and Vrefint channel
  199. \param[in] none
  200. \param[out] none
  201. \retval none
  202. */
  203. void adc_tempsensor_vrefint_enable(void)
  204. {
  205. /* enable the temperature sensor and Vrefint channel */
  206. ADC_CTL1(ADC0) |= ADC_CTL1_TSVREN;
  207. }
  208. /*!
  209. \brief disable the temperature sensor and Vrefint channel
  210. \param[in] none
  211. \param[out] none
  212. \retval none
  213. */
  214. void adc_tempsensor_vrefint_disable(void)
  215. {
  216. /* disable the temperature sensor and Vrefint channel */
  217. ADC_CTL1(ADC0) &= ~ADC_CTL1_TSVREN;
  218. }
  219. /*!
  220. \brief enable DMA request
  221. \param[in] adc_periph: ADCx, x=0,1
  222. \param[out] none
  223. \retval none
  224. */
  225. void adc_dma_mode_enable(uint32_t adc_periph)
  226. {
  227. /* enable DMA request */
  228. ADC_CTL1(adc_periph) |= (uint32_t)(ADC_CTL1_DMA);
  229. }
  230. /*!
  231. \brief disable DMA request
  232. \param[in] adc_periph: ADCx, x=0,1
  233. \param[out] none
  234. \retval none
  235. */
  236. void adc_dma_mode_disable(uint32_t adc_periph)
  237. {
  238. /* disable DMA request */
  239. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DMA);
  240. }
  241. /*!
  242. \brief configure ADC discontinuous mode
  243. \param[in] adc_periph: ADCx, x=0,1
  244. \param[in] adc_channel_group: select the channel group
  245. only one parameter can be selected which is shown as below:
  246. \arg ADC_REGULAR_CHANNEL: regular channel group
  247. \arg ADC_INSERTED_CHANNEL: inserted channel group
  248. \arg ADC_CHANNEL_DISCON_DISABLE: disable discontinuous mode of regular & inserted channel
  249. \param[in] length: number of conversions in discontinuous mode,the number can be 1..8
  250. for regular channel, the number has no effect for inserted channel
  251. \param[out] none
  252. \retval none
  253. */
  254. void adc_discontinuous_mode_config(uint32_t adc_periph, uint8_t adc_channel_group, uint8_t length)
  255. {
  256. /* disable discontinuous mode of regular & inserted channel */
  257. ADC_CTL0(adc_periph) &= ~((uint32_t)(ADC_CTL0_DISRC | ADC_CTL0_DISIC));
  258. switch(adc_channel_group){
  259. case ADC_REGULAR_CHANNEL:
  260. /* config the number of conversions in discontinuous mode */
  261. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_DISNUM);
  262. ADC_CTL0(adc_periph) |= CTL0_DISNUM(((uint32_t)length - ADC_CHANNEL_LENGTH_SUBTRACT_ONE));
  263. /* enable regular channel group discontinuous mode */
  264. ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISRC;
  265. break;
  266. case ADC_INSERTED_CHANNEL:
  267. /* enable inserted channel group discontinuous mode */
  268. ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISIC;
  269. break;
  270. case ADC_CHANNEL_DISCON_DISABLE:
  271. /* disable discontinuous mode of regular & inserted channel */
  272. default:
  273. break;
  274. }
  275. }
  276. /*!
  277. \brief configure the length of regular channel group or inserted channel group
  278. \param[in] adc_periph: ADCx, x=0,1
  279. \param[in] adc_channel_group: select the channel group
  280. only one parameter can be selected which is shown as below:
  281. \arg ADC_REGULAR_CHANNEL: regular channel group
  282. \arg ADC_INSERTED_CHANNEL: inserted channel group
  283. \param[in] length: the length of the channel
  284. regular channel 1-16
  285. inserted channel 1-4
  286. \param[out] none
  287. \retval none
  288. */
  289. void adc_channel_length_config(uint32_t adc_periph, uint8_t adc_channel_group, uint32_t length)
  290. {
  291. switch(adc_channel_group){
  292. case ADC_REGULAR_CHANNEL:
  293. /* configure the length of regular channel group */
  294. ADC_RSQ0(adc_periph) &= ~((uint32_t)ADC_RSQ0_RL);
  295. ADC_RSQ0(adc_periph) |= RSQ0_RL((uint32_t)(length-ADC_CHANNEL_LENGTH_SUBTRACT_ONE));
  296. break;
  297. case ADC_INSERTED_CHANNEL:
  298. /* configure the length of inserted channel group */
  299. ADC_ISQ(adc_periph) &= ~((uint32_t)ADC_ISQ_IL);
  300. ADC_ISQ(adc_periph) |= ISQ_IL((uint32_t)(length-ADC_CHANNEL_LENGTH_SUBTRACT_ONE));
  301. break;
  302. default:
  303. break;
  304. }
  305. }
  306. /*!
  307. \brief configure ADC regular channel
  308. \param[in] adc_periph: ADCx, x=0,1
  309. \param[in] rank: the regular group sequence rank,this parameter must be between 0 to 15
  310. \param[in] adc_channel: the selected ADC channel
  311. only one parameter can be selected which is shown as below:
  312. \arg ADC_CHANNEL_x(x=0..17)(x=16 and x=17 are only for ADC0): ADC Channelx
  313. \param[in] sample_time: the sample time value
  314. only one parameter can be selected which is shown as below:
  315. \arg ADC_SAMPLETIME_1POINT5: 1.5 cycles
  316. \arg ADC_SAMPLETIME_7POINT5: 7.5 cycles
  317. \arg ADC_SAMPLETIME_13POINT5: 13.5 cycles
  318. \arg ADC_SAMPLETIME_28POINT5: 28.5 cycles
  319. \arg ADC_SAMPLETIME_41POINT5: 41.5 cycles
  320. \arg ADC_SAMPLETIME_55POINT5: 55.5 cycles
  321. \arg ADC_SAMPLETIME_71POINT5: 71.5 cycles
  322. \arg ADC_SAMPLETIME_239POINT5: 239.5 cycles
  323. \param[out] none
  324. \retval none
  325. */
  326. void adc_regular_channel_config(uint32_t adc_periph, uint8_t rank, uint8_t adc_channel, uint32_t sample_time)
  327. {
  328. uint32_t rsq,sampt;
  329. /* ADC regular sequence config */
  330. if(rank < ADC_REGULAR_CHANNEL_RANK_SIX){
  331. /* the regular group sequence rank is smaller than six */
  332. rsq = ADC_RSQ2(adc_periph);
  333. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (ADC_REGULAR_CHANNEL_RANK_LENGTH*rank)));
  334. /* the channel number is written to these bits to select a channel as the nth conversion in the regular channel group */
  335. rsq |= ((uint32_t)adc_channel << (ADC_REGULAR_CHANNEL_RANK_LENGTH*rank));
  336. ADC_RSQ2(adc_periph) = rsq;
  337. }else if(rank < ADC_REGULAR_CHANNEL_RANK_TWELVE){
  338. /* the regular group sequence rank is smaller than twelve */
  339. rsq = ADC_RSQ1(adc_periph);
  340. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_SIX))));
  341. /* the channel number is written to these bits to select a channel as the nth conversion in the regular channel group */
  342. rsq |= ((uint32_t)adc_channel << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_SIX)));
  343. ADC_RSQ1(adc_periph) = rsq;
  344. }else if(rank < ADC_REGULAR_CHANNEL_RANK_SIXTEEN){
  345. /* the regular group sequence rank is smaller than sixteen */
  346. rsq = ADC_RSQ0(adc_periph);
  347. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_TWELVE))));
  348. /* the channel number is written to these bits to select a channel as the nth conversion in the regular channel group */
  349. rsq |= ((uint32_t)adc_channel << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_TWELVE)));
  350. ADC_RSQ0(adc_periph) = rsq;
  351. }else{
  352. }
  353. /* ADC sampling time config */
  354. if(adc_channel < ADC_CHANNEL_SAMPLE_TEN){
  355. /* the regular group sequence rank is smaller than ten */
  356. sampt = ADC_SAMPT1(adc_periph);
  357. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel)));
  358. /* channel sample time set*/
  359. sampt |= (uint32_t)(sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel));
  360. ADC_SAMPT1(adc_periph) = sampt;
  361. }else if(adc_channel < ADC_CHANNEL_SAMPLE_EIGHTEEN){
  362. /* the regular group sequence rank is smaller than eighteen */
  363. sampt = ADC_SAMPT0(adc_periph);
  364. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel-ADC_CHANNEL_SAMPLE_TEN))));
  365. /* channel sample time set*/
  366. sampt |= (uint32_t)(sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel-ADC_CHANNEL_SAMPLE_TEN)));
  367. ADC_SAMPT0(adc_periph) = sampt;
  368. }else{
  369. }
  370. }
  371. /*!
  372. \brief configure ADC inserted channel
  373. \param[in] adc_periph: ADCx, x=0,1
  374. \param[in] rank: the inserted group sequencer rank,this parameter must be between 0 to 3
  375. \param[in] adc_channel: the selected ADC channel
  376. only one parameter can be selected which is shown as below:
  377. \arg ADC_CHANNEL_x(x=0..17)(x=16 and x=17 are only for ADC0): ADC Channelx
  378. \param[in] sample_time: The sample time value
  379. only one parameter can be selected which is shown as below:
  380. \arg ADC_SAMPLETIME_1POINT5: 1.5 cycles
  381. \arg ADC_SAMPLETIME_7POINT5: 7.5 cycles
  382. \arg ADC_SAMPLETIME_13POINT5: 13.5 cycles
  383. \arg ADC_SAMPLETIME_28POINT5: 28.5 cycles
  384. \arg ADC_SAMPLETIME_41POINT5: 41.5 cycles
  385. \arg ADC_SAMPLETIME_55POINT5: 55.5 cycles
  386. \arg ADC_SAMPLETIME_71POINT5: 71.5 cycles
  387. \arg ADC_SAMPLETIME_239POINT5: 239.5 cycles
  388. \param[out] none
  389. \retval none
  390. */
  391. void adc_inserted_channel_config(uint32_t adc_periph, uint8_t rank, uint8_t adc_channel, uint32_t sample_time)
  392. {
  393. uint8_t inserted_length;
  394. uint32_t isq,sampt;
  395. /* get inserted channel group length */
  396. inserted_length = (uint8_t)GET_BITS(ADC_ISQ(adc_periph) , 20U , 21U);
  397. /* the channel number is written to these bits to select a channel as the nth conversion in the inserted channel group */
  398. isq = ADC_ISQ(adc_periph);
  399. isq &= ~((uint32_t)(ADC_ISQ_ISQN << (ADC_INSERTED_CHANNEL_SHIFT_LENGTH-(inserted_length-rank)*ADC_INSERTED_CHANNEL_RANK_LENGTH)));
  400. isq |= ((uint32_t)adc_channel << (ADC_INSERTED_CHANNEL_SHIFT_LENGTH-(inserted_length-rank)*ADC_INSERTED_CHANNEL_RANK_LENGTH));
  401. ADC_ISQ(adc_periph) = isq;
  402. /* ADC sampling time config */
  403. if(adc_channel < ADC_CHANNEL_SAMPLE_TEN){
  404. /* the inserted group sequence rank is smaller than ten */
  405. sampt = ADC_SAMPT1(adc_periph);
  406. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel)));
  407. /* channel sample time set*/
  408. sampt |= (uint32_t) sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel);
  409. ADC_SAMPT1(adc_periph) = sampt;
  410. }else if(adc_channel < ADC_CHANNEL_SAMPLE_EIGHTEEN){
  411. /* the inserted group sequence rank is smaller than eighteen */
  412. sampt = ADC_SAMPT0(adc_periph);
  413. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel-ADC_CHANNEL_SAMPLE_TEN))));
  414. /* channel sample time set*/
  415. sampt |= ((uint32_t)sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel-ADC_CHANNEL_SAMPLE_TEN)));
  416. ADC_SAMPT0(adc_periph) = sampt;
  417. }else{
  418. }
  419. }
  420. /*!
  421. \brief configure ADC inserted channel offset
  422. \param[in] adc_periph: ADCx, x=0,1
  423. \param[in] inserted_channel: insert channel select
  424. only one parameter can be selected
  425. \arg ADC_INSERTED_CHANNEL_0: inserted channel0
  426. \arg ADC_INSERTED_CHANNEL_1: inserted channel1
  427. \arg ADC_INSERTED_CHANNEL_2: inserted channel2
  428. \arg ADC_INSERTED_CHANNEL_3: inserted channel3
  429. \param[in] offset: the offset data
  430. \param[out] none
  431. \retval none
  432. */
  433. void adc_inserted_channel_offset_config(uint32_t adc_periph, uint8_t inserted_channel, uint16_t offset)
  434. {
  435. uint8_t inserted_length;
  436. uint32_t num = 0U;
  437. inserted_length = (uint8_t)GET_BITS(ADC_ISQ(adc_periph) , 20U , 21U);
  438. num = ((uint32_t)ADC_OFFSET_LENGTH - ((uint32_t)inserted_length - (uint32_t)inserted_channel));
  439. if(num <= ADC_OFFSET_LENGTH){
  440. /* calculate the offset of the register */
  441. num = num * ADC_OFFSET_SHIFT_LENGTH;
  442. /* config the offset of the selected channels */
  443. REG32((adc_periph) + 0x14U + num) = IOFFX_IOFF((uint32_t)offset);
  444. }
  445. }
  446. /*!
  447. \brief configure ADC external trigger source
  448. \param[in] adc_periph: ADCx, x=0,1
  449. \param[in] adc_channel_group: select the channel group
  450. only one parameter can be selected which is shown as below:
  451. \arg ADC_REGULAR_CHANNEL: regular channel group
  452. \arg ADC_INSERTED_CHANNEL: inserted channel group
  453. \param[in] external_trigger_source: regular or inserted group trigger source
  454. only one parameter can be selected
  455. for regular channel:
  456. \arg ADC0_1_EXTTRIG_REGULAR_T0_CH0: TIMER0 CH0 event select
  457. \arg ADC0_1_EXTTRIG_REGULAR_T0_CH1: TIMER0 CH1 event select
  458. \arg ADC0_1_EXTTRIG_REGULAR_T0_CH2: TIMER0 CH2 event select
  459. \arg ADC0_1_EXTTRIG_REGULAR_T1_CH1: TIMER1 CH1 event select
  460. \arg ADC0_1_EXTTRIG_REGULAR_T2_TRGO: TIMER2 TRGO event select
  461. \arg ADC0_1_EXTTRIG_REGULAR_T3_CH3: TIMER3 CH3 event select
  462. \arg ADC0_1_EXTTRIG_REGULAR_EXTI_11: external interrupt line 11
  463. \arg ADC0_1_EXTTRIG_REGULAR_NONE: software trigger
  464. for inserted channel:
  465. \arg ADC0_1_EXTTRIG_INSERTED_T0_TRGO: TIMER0 TRGO event select
  466. \arg ADC0_1_EXTTRIG_INSERTED_T0_CH3: TIMER0 CH3 event select
  467. \arg ADC0_1_EXTTRIG_INSERTED_T1_TRGO: TIMER1 TRGO event select
  468. \arg ADC0_1_EXTTRIG_INSERTED_T1_CH0: TIMER1 CH0 event select
  469. \arg ADC0_1_EXTTRIG_INSERTED_T2_CH3: TIMER2 CH3 event select
  470. \arg ADC0_1_EXTTRIG_INSERTED_T3_TRGO: TIMER3 TRGO event select
  471. \arg ADC0_1_EXTTRIG_INSERTED_EXTI_15: external interrupt line 15
  472. \arg ADC0_1_EXTTRIG_INSERTED_NONE: software trigger
  473. \param[out] none
  474. \retval none
  475. */
  476. void adc_external_trigger_source_config(uint32_t adc_periph, uint8_t adc_channel_group, uint32_t external_trigger_source)
  477. {
  478. switch(adc_channel_group){
  479. case ADC_REGULAR_CHANNEL:
  480. /* configure ADC regular group external trigger source */
  481. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSRC);
  482. ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
  483. break;
  484. case ADC_INSERTED_CHANNEL:
  485. /* configure ADC inserted group external trigger source */
  486. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSIC);
  487. ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. /*!
  494. \brief configure ADC external trigger
  495. \param[in] adc_periph: ADCx, x=0,1
  496. \param[in] adc_channel_group: select the channel group
  497. one or more parameters can be selected which are shown as below:
  498. \arg ADC_REGULAR_CHANNEL: regular channel group
  499. \arg ADC_INSERTED_CHANNEL: inserted channel group
  500. \param[in] newvalue: ENABLE or DISABLE
  501. \param[out] none
  502. \retval none
  503. */
  504. void adc_external_trigger_config(uint32_t adc_periph, uint8_t adc_channel_group, ControlStatus newvalue)
  505. {
  506. if(newvalue){
  507. if(0U != (adc_channel_group & ADC_REGULAR_CHANNEL)){
  508. /* enable ADC regular channel group external trigger */
  509. ADC_CTL1(adc_periph) |= ADC_CTL1_ETERC;
  510. }
  511. if(0U != (adc_channel_group & ADC_INSERTED_CHANNEL)){
  512. /* enable ADC inserted channel group external trigger */
  513. ADC_CTL1(adc_periph) |= ADC_CTL1_ETEIC;
  514. }
  515. }else{
  516. if(0U != (adc_channel_group & ADC_REGULAR_CHANNEL)){
  517. /* disable ADC regular channel group external trigger */
  518. ADC_CTL1(adc_periph) &= ~ADC_CTL1_ETERC;
  519. }
  520. if(0U != (adc_channel_group & ADC_INSERTED_CHANNEL)){
  521. /* disable ADC regular channel group external trigger */
  522. ADC_CTL1(adc_periph) &= ~ADC_CTL1_ETEIC;
  523. }
  524. }
  525. }
  526. /*!
  527. \brief enable ADC software trigger
  528. \param[in] adc_periph: ADCx, x=0,1
  529. \param[in] adc_channel_group: select the channel group
  530. one or more parameters can be selected which are shown as below:
  531. \arg ADC_REGULAR_CHANNEL: regular channel group
  532. \arg ADC_INSERTED_CHANNEL: inserted channel group
  533. \param[out] none
  534. \retval none
  535. */
  536. void adc_software_trigger_enable(uint32_t adc_periph, uint8_t adc_channel_group)
  537. {
  538. if(0U != (adc_channel_group & ADC_REGULAR_CHANNEL)){
  539. /* enable ADC regular channel group software trigger */
  540. ADC_CTL1(adc_periph) |= ADC_CTL1_SWRCST;
  541. }
  542. if(0U != (adc_channel_group & ADC_INSERTED_CHANNEL)){
  543. /* enable ADC inserted channel group software trigger */
  544. ADC_CTL1(adc_periph) |= ADC_CTL1_SWICST;
  545. }
  546. }
  547. /*!
  548. \brief read ADC regular group data register
  549. \param[in] adc_periph: ADCx, x=0,1
  550. \param[in] none
  551. \param[out] none
  552. \retval the conversion value
  553. */
  554. uint16_t adc_regular_data_read(uint32_t adc_periph)
  555. {
  556. return (uint16_t)(ADC_RDATA(adc_periph));
  557. }
  558. /*!
  559. \brief read ADC inserted group data register
  560. \param[in] adc_periph: ADCx, x=0,1
  561. \param[in] inserted_channel: insert channel select
  562. only one parameter can be selected
  563. \arg ADC_INSERTED_CHANNEL_0: inserted Channel0
  564. \arg ADC_INSERTED_CHANNEL_1: inserted channel1
  565. \arg ADC_INSERTED_CHANNEL_2: inserted Channel2
  566. \arg ADC_INSERTED_CHANNEL_3: inserted Channel3
  567. \param[out] none
  568. \retval the conversion value
  569. */
  570. uint16_t adc_inserted_data_read(uint32_t adc_periph, uint8_t inserted_channel)
  571. {
  572. uint32_t idata;
  573. /* read the data of the selected channel */
  574. switch(inserted_channel){
  575. case ADC_INSERTED_CHANNEL_0:
  576. /* read the data of channel 0 */
  577. idata = ADC_IDATA0(adc_periph);
  578. break;
  579. case ADC_INSERTED_CHANNEL_1:
  580. /* read the data of channel 1 */
  581. idata = ADC_IDATA1(adc_periph);
  582. break;
  583. case ADC_INSERTED_CHANNEL_2:
  584. /* read the data of channel 2 */
  585. idata = ADC_IDATA2(adc_periph);
  586. break;
  587. case ADC_INSERTED_CHANNEL_3:
  588. /* read the data of channel 3 */
  589. idata = ADC_IDATA3(adc_periph);
  590. break;
  591. default:
  592. idata = 0U;
  593. break;
  594. }
  595. return (uint16_t)idata;
  596. }
  597. /*!
  598. \brief read the last ADC0 and ADC1 conversion result data in sync mode
  599. \param[in] none
  600. \param[out] none
  601. \retval the conversion value
  602. */
  603. uint32_t adc_sync_mode_convert_value_read(void)
  604. {
  605. /* return conversion value */
  606. return ADC_RDATA(ADC0);
  607. }
  608. /*!
  609. \brief configure ADC analog watchdog single channel
  610. \param[in] adc_periph: ADCx, x=0,1
  611. \param[in] adc_channel: the selected ADC channel
  612. only one parameter can be selected which is shown as below:
  613. \arg ADC_CHANNEL_x: ADC Channelx(x=0..17)(x=16 and x=17 are only for ADC0)
  614. \param[out] none
  615. \retval none
  616. */
  617. void adc_watchdog_single_channel_enable(uint32_t adc_periph, uint8_t adc_channel)
  618. {
  619. ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC | ADC_CTL0_WDCHSEL);
  620. /* analog watchdog channel select */
  621. ADC_CTL0(adc_periph) |= (uint32_t)adc_channel;
  622. ADC_CTL0(adc_periph) |= (uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC);
  623. }
  624. /*!
  625. \brief configure ADC analog watchdog group channel
  626. \param[in] adc_periph: ADCx, x=0,1
  627. \param[in] adc_channel_group: the channel group use analog watchdog
  628. only one parameter can be selected which is shown as below:
  629. \arg ADC_REGULAR_CHANNEL: regular channel group
  630. \arg ADC_INSERTED_CHANNEL: inserted channel group
  631. \arg ADC_REGULAR_INSERTED_CHANNEL: both regular and inserted group
  632. \param[out] none
  633. \retval none
  634. */
  635. void adc_watchdog_group_channel_enable(uint32_t adc_periph, uint8_t adc_channel_group)
  636. {
  637. ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC);
  638. /* select the group */
  639. switch(adc_channel_group){
  640. case ADC_REGULAR_CHANNEL:
  641. /* regular channel analog watchdog enable */
  642. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_RWDEN;
  643. break;
  644. case ADC_INSERTED_CHANNEL:
  645. /* inserted channel analog watchdog enable */
  646. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_IWDEN;
  647. break;
  648. case ADC_REGULAR_INSERTED_CHANNEL:
  649. /* regular and inserted channel analog watchdog enable */
  650. ADC_CTL0(adc_periph) |= (uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN);
  651. break;
  652. default:
  653. break;
  654. }
  655. }
  656. /*!
  657. \brief disable ADC analog watchdog
  658. \param[in] adc_periph: ADCx, x=0,1
  659. \param[out] none
  660. \retval none
  661. */
  662. void adc_watchdog_disable(uint32_t adc_periph)
  663. {
  664. ADC_CTL0(adc_periph) &= (uint32_t)~(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC | ADC_CTL0_WDCHSEL);
  665. }
  666. /*!
  667. \brief configure ADC analog watchdog threshold
  668. \param[in] adc_periph: ADCx, x=0,1
  669. \param[in] low_threshold: analog watchdog low threshold, 0..4095
  670. \param[in] high_threshold: analog watchdog high threshold, 0..4095
  671. \param[out] none
  672. \retval none
  673. */
  674. void adc_watchdog_threshold_config(uint32_t adc_periph, uint16_t low_threshold, uint16_t high_threshold)
  675. {
  676. ADC_WDLT(adc_periph) = (uint32_t)WDLT_WDLT(low_threshold);
  677. ADC_WDHT(adc_periph) = (uint32_t)WDHT_WDHT(high_threshold);
  678. }
  679. /*!
  680. \brief get the ADC flag bits
  681. \param[in] adc_periph: ADCx, x=0,1
  682. \param[in] adc_flag: the adc flag bits
  683. only one parameter can be selected which is shown as below:
  684. \arg ADC_FLAG_WDE: analog watchdog event flag
  685. \arg ADC_FLAG_EOC: end of group conversion flag
  686. \arg ADC_FLAG_EOIC: end of inserted group conversion flag
  687. \arg ADC_FLAG_STIC: start flag of inserted channel group
  688. \arg ADC_FLAG_STRC: start flag of regular channel group
  689. \param[out] none
  690. \retval FlagStatus: SET or RESET
  691. */
  692. FlagStatus adc_flag_get(uint32_t adc_periph, uint32_t adc_flag)
  693. {
  694. FlagStatus reval = RESET;
  695. if(ADC_STAT(adc_periph) & adc_flag){
  696. reval = SET;
  697. }
  698. return reval;
  699. }
  700. /*!
  701. \brief clear the ADC flag bits
  702. \param[in] adc_periph: ADCx, x=0,1
  703. \param[in] adc_flag: the adc flag bits
  704. one or more parameters can be selected which are shown as below:
  705. \arg ADC_FLAG_WDE: analog watchdog event flag
  706. \arg ADC_FLAG_EOC: end of group conversion flag
  707. \arg ADC_FLAG_EOIC: end of inserted group conversion flag
  708. \arg ADC_FLAG_STIC: start flag of inserted channel group
  709. \arg ADC_FLAG_STRC: start flag of regular channel group
  710. \param[out] none
  711. \retval none
  712. */
  713. void adc_flag_clear(uint32_t adc_periph, uint32_t adc_flag)
  714. {
  715. ADC_STAT(adc_periph) &= ~((uint32_t)adc_flag);
  716. }
  717. /*!
  718. \brief get the bit state of ADCx software start conversion
  719. \param[in] adc_periph: ADCx, x=0,1
  720. \param[in] none
  721. \param[out] none
  722. \retval FlagStatus: SET or RESET
  723. */
  724. FlagStatus adc_regular_software_startconv_flag_get(uint32_t adc_periph)
  725. {
  726. FlagStatus reval = RESET;
  727. if((uint32_t)RESET != (ADC_CTL1(adc_periph) & ADC_CTL1_SWRCST)){
  728. reval = SET;
  729. }
  730. return reval;
  731. }
  732. /*!
  733. \brief get the bit state of ADCx software inserted channel start conversion
  734. \param[in] adc_periph: ADCx, x=0,1
  735. \param[in] none
  736. \param[out] none
  737. \retval FlagStatus: SET or RESET
  738. */
  739. FlagStatus adc_inserted_software_startconv_flag_get(uint32_t adc_periph)
  740. {
  741. FlagStatus reval = RESET;
  742. if((uint32_t)RESET != (ADC_CTL1(adc_periph) & ADC_CTL1_SWICST)){
  743. reval = SET;
  744. }
  745. return reval;
  746. }
  747. /*!
  748. \brief get the ADC interrupt bits
  749. \param[in] adc_periph: ADCx, x=0,1
  750. \param[in] adc_interrupt: the adc interrupt bits
  751. only one parameter can be selected which is shown as below:
  752. \arg ADC_INT_FLAG_WDE: analog watchdog interrupt
  753. \arg ADC_INT_FLAG_EOC: end of group conversion interrupt
  754. \arg ADC_INT_FLAG_EOIC: end of inserted group conversion interrupt
  755. \param[out] none
  756. \retval FlagStatus: SET or RESET
  757. */
  758. FlagStatus adc_interrupt_flag_get(uint32_t adc_periph, uint32_t adc_interrupt)
  759. {
  760. FlagStatus interrupt_flag = RESET;
  761. uint32_t state;
  762. /* check the interrupt bits */
  763. switch(adc_interrupt){
  764. case ADC_INT_FLAG_WDE:
  765. /* get the ADC analog watchdog interrupt bits */
  766. state = ADC_STAT(adc_periph) & ADC_STAT_WDE;
  767. if((ADC_CTL0(adc_periph) & ADC_CTL0_WDEIE) && state){
  768. interrupt_flag = SET;
  769. }
  770. break;
  771. case ADC_INT_FLAG_EOC:
  772. /* get the ADC end of group conversion interrupt bits */
  773. state = ADC_STAT(adc_periph) & ADC_STAT_EOC;
  774. if((ADC_CTL0(adc_periph) & ADC_CTL0_EOCIE) && state){
  775. interrupt_flag = SET;
  776. }
  777. break;
  778. case ADC_INT_FLAG_EOIC:
  779. /* get the ADC end of inserted group conversion interrupt bits */
  780. state = ADC_STAT(adc_periph) & ADC_STAT_EOIC;
  781. if((ADC_CTL0(adc_periph) & ADC_CTL0_EOICIE) && state){
  782. interrupt_flag = SET;
  783. }
  784. break;
  785. default:
  786. break;
  787. }
  788. return interrupt_flag;
  789. }
  790. /*!
  791. \brief clear the ADC flag
  792. \param[in] adc_periph: ADCx, x=0,1
  793. \param[in] adc_interrupt: the adc status flag
  794. one or more parameters can be selected which are shown as below:
  795. \arg ADC_INT_FLAG_WDE: analog watchdog interrupt
  796. \arg ADC_INT_FLAG_EOC: end of group conversion interrupt
  797. \arg ADC_INT_FLAG_EOIC: end of inserted group conversion interrupt
  798. \param[out] none
  799. \retval none
  800. */
  801. void adc_interrupt_flag_clear(uint32_t adc_periph, uint32_t adc_interrupt)
  802. {
  803. ADC_STAT(adc_periph) &= ~((uint32_t)adc_interrupt);
  804. }
  805. /*!
  806. \brief enable ADC interrupt
  807. \param[in] adc_periph: ADCx, x=0,1
  808. \param[in] adc_interrupt: the adc interrupt
  809. one or more parameters can be selected which are shown as below:
  810. \arg ADC_INT_WDE: analog watchdog interrupt flag
  811. \arg ADC_INT_EOC: end of group conversion interrupt flag
  812. \arg ADC_INT_EOIC: end of inserted group conversion interrupt flag
  813. \param[out] none
  814. \retval none
  815. */
  816. void adc_interrupt_enable(uint32_t adc_periph, uint32_t adc_interrupt)
  817. {
  818. /* enable ADC analog watchdog interrupt */
  819. if(0U != (adc_interrupt & ADC_INT_WDE)){
  820. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_WDEIE;
  821. }
  822. /* enable ADC end of group conversion interrupt */
  823. if(0U != (adc_interrupt & ADC_INT_EOC)){
  824. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_EOCIE;
  825. }
  826. /* enable ADC end of inserted group conversion interrupt */
  827. if(0U != (adc_interrupt & ADC_INT_EOIC)){
  828. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_EOICIE;
  829. }
  830. }
  831. /*!
  832. \brief disable ADC interrupt
  833. \param[in] adc_periph: ADCx, x=0,1
  834. \param[in] adc_interrupt: the adc interrupt flag
  835. one or more parameters can be selected which are shown as below:
  836. \arg ADC_INT_WDE: analog watchdog interrupt flag
  837. \arg ADC_INT_EOC: end of group conversion interrupt flag
  838. \arg ADC_INT_EOIC: end of inserted group conversion interrupt flag
  839. \param[out] none
  840. \retval none
  841. */
  842. void adc_interrupt_disable(uint32_t adc_periph, uint32_t adc_interrupt)
  843. {
  844. /* disable ADC analog watchdog interrupt */
  845. if(0U != (adc_interrupt & ADC_INT_WDE)){
  846. ADC_CTL0(adc_periph) &= ~(uint32_t) ADC_CTL0_WDEIE;
  847. }
  848. /* disable ADC end of group conversion interrupt */
  849. if(0U != (adc_interrupt & ADC_INT_EOC)){
  850. ADC_CTL0(adc_periph) &= ~(uint32_t) ADC_CTL0_EOCIE;
  851. }
  852. /* disable ADC end of inserted group conversion interrupt */
  853. if(0U != (adc_interrupt & ADC_INT_EOIC)){
  854. ADC_CTL0(adc_periph) &= ~(uint32_t) ADC_CTL0_EOICIE;
  855. }
  856. }
  857. /*!
  858. \brief adc resolution config
  859. \param[in] adc_periph: ADCx, x=0,1
  860. \param[in] resolution: ADC resolution
  861. only one parameter can be selected which is shown as below:
  862. \arg ADC_RESOLUTION_12B: 12-bit ADC resolution
  863. \arg ADC_RESOLUTION_10B: 10-bit ADC resolution
  864. \arg ADC_RESOLUTION_8B: 8-bit ADC resolution
  865. \arg ADC_RESOLUTION_6B: 6-bit ADC resolution
  866. \param[out] none
  867. \retval none
  868. */
  869. void adc_resolution_config(uint32_t adc_periph, uint32_t resolution)
  870. {
  871. ADC_OVSCR(adc_periph) &= ~((uint32_t)ADC_OVSCR_DRES);
  872. ADC_OVSCR(adc_periph) |= (uint32_t)resolution;
  873. }
  874. /*!
  875. \brief adc oversample mode config
  876. \param[in] adc_periph: ADCx, x=0,1
  877. \param[in] mode: ADC oversampling mode
  878. only one parameter can be selected which is shown as below:
  879. \arg ADC_OVERSAMPLING_ALL_CONVERT: all oversampled conversions for a channel
  880. are done consecutively after a trigger
  881. \arg ADC_OVERSAMPLING_ONE_CONVERT: each oversampled conversion for a channel
  882. needs a trigger
  883. \param[in] shift: ADC oversampling shift
  884. only one parameter can be selected which is shown as below:
  885. \arg ADC_OVERSAMPLING_SHIFT_NONE: no oversampling shift
  886. \arg ADC_OVERSAMPLING_SHIFT_1B: 1-bit oversampling shift
  887. \arg ADC_OVERSAMPLING_SHIFT_2B: 2-bit oversampling shift
  888. \arg ADC_OVERSAMPLING_SHIFT_3B: 3-bit oversampling shift
  889. \arg ADC_OVERSAMPLING_SHIFT_4B: 3-bit oversampling shift
  890. \arg ADC_OVERSAMPLING_SHIFT_5B: 5-bit oversampling shift
  891. \arg ADC_OVERSAMPLING_SHIFT_6B: 6-bit oversampling shift
  892. \arg ADC_OVERSAMPLING_SHIFT_7B: 7-bit oversampling shift
  893. \arg ADC_OVERSAMPLING_SHIFT_8B: 8-bit oversampling shift
  894. \param[in] ratio: ADC oversampling ratio
  895. only one parameter can be selected which is shown as below:
  896. \arg ADC_OVERSAMPLING_RATIO_MUL2: oversampling ratio X2
  897. \arg ADC_OVERSAMPLING_RATIO_MUL4: oversampling ratio X4
  898. \arg ADC_OVERSAMPLING_RATIO_MUL8: oversampling ratio X8
  899. \arg ADC_OVERSAMPLING_RATIO_MUL16: oversampling ratio X16
  900. \arg ADC_OVERSAMPLING_RATIO_MUL32: oversampling ratio X32
  901. \arg ADC_OVERSAMPLING_RATIO_MUL64: oversampling ratio X64
  902. \arg ADC_OVERSAMPLING_RATIO_MUL128: oversampling ratio X128
  903. \arg ADC_OVERSAMPLING_RATIO_MUL256: oversampling ratio X256
  904. \param[out] none
  905. \retval none
  906. */
  907. void adc_oversample_mode_config(uint32_t adc_periph, uint8_t mode, uint16_t shift,uint8_t ratio)
  908. {
  909. if(mode){
  910. ADC_OVSCR(adc_periph) |= (uint32_t)ADC_OVSCR_TOVS;
  911. }else{
  912. ADC_OVSCR(adc_periph) &= ~((uint32_t)ADC_OVSCR_TOVS);
  913. }
  914. /* config the shift and ratio */
  915. ADC_OVSCR(adc_periph) &= ~((uint32_t)(ADC_OVSCR_OVSR | ADC_OVSCR_OVSS));
  916. ADC_OVSCR(adc_periph) |= ((uint32_t)shift | (uint32_t)ratio);
  917. }
  918. /*!
  919. \brief enable ADC oversample mode
  920. \param[in] adc_periph: ADCx, x=0,1
  921. \param[out] none
  922. \retval none
  923. */
  924. void adc_oversample_mode_enable(uint32_t adc_periph)
  925. {
  926. ADC_OVSCR(adc_periph) |= ADC_OVSCR_OVSEN;
  927. }
  928. /*!
  929. \brief disable ADC oversample mode
  930. \param[in] adc_periph: ADCx, x=0,1
  931. \param[out] none
  932. \retval none
  933. */
  934. void adc_oversample_mode_disable(uint32_t adc_periph)
  935. {
  936. ADC_OVSCR(adc_periph) &= ~((uint32_t)ADC_OVSCR_OVSEN);
  937. }