gd32f30x_adc.c 34 KB

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