gd32f4xx_adc.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. /*!
  2. \file gd32f4xx_adc.c
  3. \brief ADC driver
  4. \version 2016-08-15, V1.0.0, firmware for GD32F4xx
  5. \version 2018-12-12, V2.0.0, firmware for GD32F4xx
  6. \version 2020-09-30, V2.1.0, firmware for GD32F4xx
  7. */
  8. /*
  9. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. 1. Redistributions of source code must retain the above copyright notice, this
  13. list of conditions and the following disclaimer.
  14. 2. Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. 3. Neither the name of the copyright holder nor the names of its contributors
  18. may be used to endorse or promote products derived from this software without
  19. specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  24. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. OF SUCH DAMAGE.
  30. */
  31. #include "gd32f4xx_adc.h"
  32. #define REGULAR_TRIGGER_MODE ((uint32_t)28U)
  33. #define INSERTED_TRIGGER_MODE ((uint32_t)20U)
  34. /* discontinuous mode macro*/
  35. #define ADC_CHANNEL_LENGTH_SUBTRACT_ONE ((uint8_t)1U)
  36. /* ADC regular channel macro */
  37. #define ADC_REGULAR_CHANNEL_RANK_SIX ((uint8_t)6U)
  38. #define ADC_REGULAR_CHANNEL_RANK_TWELVE ((uint8_t)12U)
  39. #define ADC_REGULAR_CHANNEL_RANK_SIXTEEN ((uint8_t)16U)
  40. #define ADC_REGULAR_CHANNEL_RANK_LENGTH ((uint8_t)5U)
  41. /* ADC sampling time macro */
  42. #define ADC_CHANNEL_SAMPLE_TEN ((uint8_t)10U)
  43. #define ADC_CHANNEL_SAMPLE_EIGHTEEN ((uint8_t)18U)
  44. #define ADC_CHANNEL_SAMPLE_LENGTH ((uint8_t)3U)
  45. /* ADC inserted channel macro */
  46. #define ADC_INSERTED_CHANNEL_RANK_LENGTH ((uint8_t)5U)
  47. #define ADC_INSERTED_CHANNEL_SHIFT_LENGTH ((uint8_t)15U)
  48. /* ADC inserted channel offset macro */
  49. #define ADC_OFFSET_LENGTH ((uint8_t)3U)
  50. #define ADC_OFFSET_SHIFT_LENGTH ((uint8_t)4U)
  51. /*!
  52. \brief reset ADC
  53. \param[in] none
  54. \param[out] none
  55. \retval none
  56. */
  57. void adc_deinit(void)
  58. {
  59. rcu_periph_reset_enable(RCU_ADCRST);
  60. rcu_periph_reset_disable(RCU_ADCRST);
  61. }
  62. /*!
  63. \brief configure the ADC clock for all the ADCs
  64. \param[in] prescaler: configure ADCs prescaler ratio
  65. only one parameter can be selected which is shown as below:
  66. \arg ADC_ADCCK_PCLK2_DIV2: PCLK2 div2
  67. \arg ADC_ADCCK_PCLK2_DIV4: PCLK2 div4
  68. \arg ADC_ADCCK_PCLK2_DIV6: PCLK2 div6
  69. \arg ADC_ADCCK_PCLK2_DIV8: PCLK2 div8
  70. \arg ADC_ADCCK_HCLK_DIV5: HCLK div5
  71. \arg ADC_ADCCK_HCLK_DIV6: HCLK div6
  72. \arg ADC_ADCCK_HCLK_DIV10: HCLK div10
  73. \arg ADC_ADCCK_HCLK_DIV20: HCLK div20
  74. \param[out] none
  75. \retval none
  76. */
  77. void adc_clock_config(uint32_t prescaler)
  78. {
  79. ADC_SYNCCTL &= ~((uint32_t)ADC_SYNCCTL_ADCCK);
  80. ADC_SYNCCTL |= (uint32_t) prescaler;
  81. }
  82. /*!
  83. \brief enable or disable ADC special function
  84. \param[in] adc_periph: ADCx,x=0,1,2
  85. \param[in] function: the function to config
  86. only one parameter can be selected which is shown as below:
  87. \arg ADC_SCAN_MODE: scan mode select
  88. \arg ADC_INSERTED_CHANNEL_AUTO: inserted channel group convert automatically
  89. \arg ADC_CONTINUOUS_MODE: continuous mode select
  90. \param[in] newvalue: ENABLE or DISABLE
  91. \param[out] none
  92. \retval none
  93. */
  94. void adc_special_function_config(uint32_t adc_periph , uint32_t function , ControlStatus newvalue)
  95. {
  96. if(newvalue){
  97. if(0U != (function & ADC_SCAN_MODE)){
  98. /* enable scan mode */
  99. ADC_CTL0(adc_periph) |= ADC_SCAN_MODE;
  100. }
  101. if(0U != (function & ADC_INSERTED_CHANNEL_AUTO)){
  102. /* enable inserted channel group convert automatically */
  103. ADC_CTL0(adc_periph) |= ADC_INSERTED_CHANNEL_AUTO;
  104. }
  105. if(0U != (function & ADC_CONTINUOUS_MODE)){
  106. /* enable continuous mode */
  107. ADC_CTL1(adc_periph) |= ADC_CONTINUOUS_MODE;
  108. }
  109. }else{
  110. if(0U != (function & ADC_SCAN_MODE)){
  111. /* disable scan mode */
  112. ADC_CTL0(adc_periph) &= ~ADC_SCAN_MODE;
  113. }
  114. if(0U != (function & ADC_INSERTED_CHANNEL_AUTO)){
  115. /* disable inserted channel group convert automatically */
  116. ADC_CTL0(adc_periph) &= ~ADC_INSERTED_CHANNEL_AUTO;
  117. }
  118. if(0U != (function & ADC_CONTINUOUS_MODE)){
  119. /* disable continuous mode */
  120. ADC_CTL1(adc_periph) &= ~ADC_CONTINUOUS_MODE;
  121. }
  122. }
  123. }
  124. /*!
  125. \brief configure ADC data alignment
  126. \param[in] adc_periph: ADCx,x=0,1,2
  127. \param[in] data_alignment: data alignment select
  128. only one parameter can be selected which is shown as below:
  129. \arg ADC_DATAALIGN_RIGHT: LSB alignment
  130. \arg ADC_DATAALIGN_LEFT: MSB alignment
  131. \param[out] none
  132. \retval none
  133. */
  134. void adc_data_alignment_config(uint32_t adc_periph , uint32_t data_alignment)
  135. {
  136. if(ADC_DATAALIGN_RIGHT != data_alignment){
  137. /* MSB alignment */
  138. ADC_CTL1(adc_periph) |= ADC_CTL1_DAL;
  139. }else{
  140. /* LSB alignment */
  141. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DAL);
  142. }
  143. }
  144. /*!
  145. \brief enable ADC interface
  146. \param[in] adc_periph: ADCx,x=0,1,2
  147. \param[out] none
  148. \retval none
  149. */
  150. void adc_enable(uint32_t adc_periph)
  151. {
  152. if(RESET == (ADC_CTL1(adc_periph) & ADC_CTL1_ADCON)){
  153. /* enable ADC */
  154. ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_ADCON;
  155. }
  156. }
  157. /*!
  158. \brief disable ADC interface
  159. \param[in] adc_periph: ADCx,x=0,1,2
  160. \param[out] none
  161. \retval none
  162. */
  163. void adc_disable(uint32_t adc_periph)
  164. {
  165. /* disable ADC */
  166. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ADCON);
  167. }
  168. /*!
  169. \brief ADC calibration and reset calibration
  170. \param[in] adc_periph: ADCx,x=0,1,2
  171. \param[out] none
  172. \retval none
  173. */
  174. void adc_calibration_enable(uint32_t adc_periph)
  175. {
  176. /* reset the selected ADC calibration registers */
  177. ADC_CTL1(adc_periph) |= (uint32_t) ADC_CTL1_RSTCLB;
  178. /* check the RSTCLB bit state */
  179. while(RESET != (ADC_CTL1(adc_periph) & ADC_CTL1_RSTCLB)){
  180. }
  181. /* enable ADC calibration process */
  182. ADC_CTL1(adc_periph) |= ADC_CTL1_CLB;
  183. /* check the CLB bit state */
  184. while(RESET != (ADC_CTL1(adc_periph) & ADC_CTL1_CLB)){
  185. }
  186. }
  187. /*!
  188. \brief configure temperature sensor and internal reference voltage channel or VBAT channel function
  189. \param[in] function: temperature sensor and internal reference voltage channel or VBAT channel
  190. only one parameter can be selected which is shown as below:
  191. \arg ADC_VBAT_CHANNEL_SWITCH: channel 18 (1/4 voltate of external battery) switch of ADC0
  192. \arg ADC_TEMP_VREF_CHANNEL_SWITCH: channel 16 (temperature sensor) and 17 (internal reference voltage) switch of ADC0
  193. \param[in] newvalue: ENABLE or DISABLE
  194. \param[out] none
  195. \retval none
  196. */
  197. void adc_channel_16_to_18(uint32_t function, ControlStatus newvalue)
  198. {
  199. if(newvalue){
  200. if(RESET != (function & ADC_VBAT_CHANNEL_SWITCH)){
  201. /* enable ADC0 Vbat channel */
  202. ADC_SYNCCTL |= ADC_VBAT_CHANNEL_SWITCH;
  203. }
  204. if(RESET != (function & ADC_TEMP_VREF_CHANNEL_SWITCH)){
  205. /* enable ADC0 Vref and Temperature channel */
  206. ADC_SYNCCTL |= ADC_TEMP_VREF_CHANNEL_SWITCH;
  207. }
  208. }else{
  209. if(RESET != (function & ADC_VBAT_CHANNEL_SWITCH)){
  210. /* disable ADC0 Vbat channel */
  211. ADC_SYNCCTL &= ~ADC_VBAT_CHANNEL_SWITCH;
  212. }
  213. if(RESET != (function & ADC_TEMP_VREF_CHANNEL_SWITCH)){
  214. /* disable ADC0 Vref and Temperature channel */
  215. ADC_SYNCCTL &= ~ADC_TEMP_VREF_CHANNEL_SWITCH;
  216. }
  217. }
  218. }
  219. /*!
  220. \brief configure ADC resolution
  221. \param[in] adc_periph: ADCx,x=0,1,2
  222. \param[in] resolution: ADC resolution
  223. only one parameter can be selected which is shown as below:
  224. \arg ADC_RESOLUTION_12B: 12-bit ADC resolution
  225. \arg ADC_RESOLUTION_10B: 10-bit ADC resolution
  226. \arg ADC_RESOLUTION_8B: 8-bit ADC resolution
  227. \arg ADC_RESOLUTION_6B: 6-bit ADC resolution
  228. \param[out] none
  229. \retval none
  230. */
  231. void adc_resolution_config(uint32_t adc_periph , uint32_t resolution)
  232. {
  233. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_DRES);
  234. ADC_CTL0(adc_periph) |= (uint32_t)resolution;
  235. }
  236. /*!
  237. \brief configure ADC oversample mode
  238. \param[in] adc_periph: ADCx,x=0,1,2
  239. \param[in] mode: ADC oversampling mode
  240. only one parameter can be selected which is shown as below:
  241. \arg ADC_OVERSAMPLING_ALL_CONVERT: all oversampled conversions for a channel are done consecutively after a trigger
  242. \arg ADC_OVERSAMPLING_ONE_CONVERT: each oversampled conversion for a channel needs a trigger
  243. \param[in] shift: ADC oversampling shift
  244. only one parameter can be selected which is shown as below:
  245. \arg ADC_OVERSAMPLING_SHIFT_NONE: no oversampling shift
  246. \arg ADC_OVERSAMPLING_SHIFT_1B: 1-bit oversampling shift
  247. \arg ADC_OVERSAMPLING_SHIFT_2B: 2-bit oversampling shift
  248. \arg ADC_OVERSAMPLING_SHIFT_3B: 3-bit oversampling shift
  249. \arg ADC_OVERSAMPLING_SHIFT_4B: 3-bit oversampling shift
  250. \arg ADC_OVERSAMPLING_SHIFT_5B: 5-bit oversampling shift
  251. \arg ADC_OVERSAMPLING_SHIFT_6B: 6-bit oversampling shift
  252. \arg ADC_OVERSAMPLING_SHIFT_7B: 7-bit oversampling shift
  253. \arg ADC_OVERSAMPLING_SHIFT_8B: 8-bit oversampling shift
  254. \param[in] ratio: ADC oversampling ratio
  255. only one parameter can be selected which is shown as below:
  256. \arg ADC_OVERSAMPLING_RATIO_MUL2: oversampling ratio multiple 2
  257. \arg ADC_OVERSAMPLING_RATIO_MUL4: oversampling ratio multiple 4
  258. \arg ADC_OVERSAMPLING_RATIO_MUL8: oversampling ratio multiple 8
  259. \arg ADC_OVERSAMPLING_RATIO_MUL16: oversampling ratio multiple 16
  260. \arg ADC_OVERSAMPLING_RATIO_MUL32: oversampling ratio multiple 32
  261. \arg ADC_OVERSAMPLING_RATIO_MUL64: oversampling ratio multiple 64
  262. \arg ADC_OVERSAMPLING_RATIO_MUL128: oversampling ratio multiple 128
  263. \arg ADC_OVERSAMPLING_RATIO_MUL256: oversampling ratio multiple 256
  264. \param[out] none
  265. \retval none
  266. */
  267. void adc_oversample_mode_config(uint32_t adc_periph , uint32_t mode , uint16_t shift , uint8_t ratio)
  268. {
  269. if(ADC_OVERSAMPLING_ONE_CONVERT == mode){
  270. ADC_OVSAMPCTL(adc_periph) |= (uint32_t)ADC_OVSAMPCTL_TOVS;
  271. }else{
  272. ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)ADC_OVSAMPCTL_TOVS);
  273. }
  274. /* config the shift and ratio */
  275. ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)(ADC_OVSAMPCTL_OVSR | ADC_OVSAMPCTL_OVSS));
  276. ADC_OVSAMPCTL(adc_periph) |= ((uint32_t)shift | (uint32_t)ratio);
  277. }
  278. /*!
  279. \brief enable ADC oversample mode
  280. \param[in] adc_periph: ADCx,x=0,1,2
  281. \param[out] none
  282. \retval none
  283. */
  284. void adc_oversample_mode_enable(uint32_t adc_periph)
  285. {
  286. ADC_OVSAMPCTL(adc_periph) |= ADC_OVSAMPCTL_OVSEN;
  287. }
  288. /*!
  289. \brief disable ADC oversample mode
  290. \param[in] adc_periph: ADCx,x=0,1,2
  291. \param[out] none
  292. \retval none
  293. */
  294. void adc_oversample_mode_disable(uint32_t adc_periph)
  295. {
  296. ADC_OVSAMPCTL(adc_periph) &= ~((uint32_t)ADC_OVSAMPCTL_OVSEN);
  297. }
  298. /*!
  299. \brief enable DMA request
  300. \param[in] adc_periph: ADCx,x=0,1,2
  301. \param[out] none
  302. \retval none
  303. */
  304. void adc_dma_mode_enable(uint32_t adc_periph)
  305. {
  306. /* enable DMA request */
  307. ADC_CTL1(adc_periph) |= (uint32_t)(ADC_CTL1_DMA);
  308. }
  309. /*!
  310. \brief disable DMA request
  311. \param[in] adc_periph: ADCx,x=0,1,2
  312. \param[out] none
  313. \retval none
  314. */
  315. void adc_dma_mode_disable(uint32_t adc_periph)
  316. {
  317. /* disable DMA request */
  318. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DMA);
  319. }
  320. /*!
  321. \brief when DMA=1, the DMA engine issues a request at end of each regular conversion
  322. \param[in] adc_periph: ADCx,x=0,1,2
  323. \param[out] none
  324. \retval none
  325. */
  326. void adc_dma_request_after_last_enable(uint32_t adc_periph)
  327. {
  328. ADC_CTL1(adc_periph) |= (uint32_t)(ADC_CTL1_DDM);
  329. }
  330. /*!
  331. \brief the DMA engine is disabled after the end of transfer signal from DMA controller is detected
  332. \param[in] adc_periph: ADCx,x=0,1,2
  333. \param[out] none
  334. \retval none
  335. */
  336. void adc_dma_request_after_last_disable(uint32_t adc_periph)
  337. {
  338. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_DDM);
  339. }
  340. /*!
  341. \brief configure ADC discontinuous mode
  342. \param[in] adc_periph: ADCx,x=0,1,2
  343. \param[in] adc_channel_group: select the channel group
  344. only one parameter can be selected which is shown as below:
  345. \arg ADC_REGULAR_CHANNEL: regular channel group
  346. \arg ADC_INSERTED_CHANNEL: inserted channel group
  347. \arg ADC_CHANNEL_DISCON_DISABLE: disable discontinuous mode of regular & inserted channel
  348. \param[in] length: number of conversions in discontinuous mode,the number can be 1..8
  349. for regular channel ,the number has no effect for inserted channel
  350. \param[out] none
  351. \retval none
  352. */
  353. void adc_discontinuous_mode_config(uint32_t adc_periph , uint8_t adc_channel_group , uint8_t length)
  354. {
  355. /* disable discontinuous mode of regular & inserted channel */
  356. ADC_CTL0(adc_periph) &= ~((uint32_t)( ADC_CTL0_DISRC | ADC_CTL0_DISIC ));
  357. switch(adc_channel_group){
  358. case ADC_REGULAR_CHANNEL:
  359. /* config the number of conversions in discontinuous mode */
  360. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_DISNUM);
  361. if((length <= 8U) && (length >= 1U)){
  362. ADC_CTL0(adc_periph) |= CTL0_DISNUM(((uint32_t)length - ADC_CHANNEL_LENGTH_SUBTRACT_ONE));
  363. }
  364. /* enable regular channel group discontinuous mode */
  365. ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISRC;
  366. break;
  367. case ADC_INSERTED_CHANNEL:
  368. /* enable inserted channel group discontinuous mode */
  369. ADC_CTL0(adc_periph) |= (uint32_t)ADC_CTL0_DISIC;
  370. break;
  371. case ADC_CHANNEL_DISCON_DISABLE:
  372. /* disable discontinuous mode of regular & inserted channel */
  373. default:
  374. break;
  375. }
  376. }
  377. /*!
  378. \brief configure the length of regular channel group or inserted channel group
  379. \param[in] adc_periph: ADCx,x=0,1,2
  380. \param[in] adc_channel_group: select the channel group
  381. only one parameter can be selected which is shown as below:
  382. \arg ADC_REGULAR_CHANNEL: regular channel group
  383. \arg ADC_INSERTED_CHANNEL: inserted channel group
  384. \param[in] length: the length of the channel
  385. regular channel 1-16
  386. inserted channel 1-4
  387. \param[out] none
  388. \retval none
  389. */
  390. void adc_channel_length_config(uint32_t adc_periph , uint8_t adc_channel_group , uint32_t length)
  391. {
  392. switch(adc_channel_group){
  393. case ADC_REGULAR_CHANNEL:
  394. if((length >= 1U) && (length <= 16U)){
  395. ADC_RSQ0(adc_periph) &= ~((uint32_t)ADC_RSQ0_RL);
  396. ADC_RSQ0(adc_periph) |= RSQ0_RL((uint32_t)(length-ADC_CHANNEL_LENGTH_SUBTRACT_ONE));
  397. }
  398. break;
  399. case ADC_INSERTED_CHANNEL:
  400. if((length >= 1U) && (length <= 4U)){
  401. ADC_ISQ(adc_periph) &= ~((uint32_t)ADC_ISQ_IL);
  402. ADC_ISQ(adc_periph) |= ISQ_IL((uint32_t)(length-ADC_CHANNEL_LENGTH_SUBTRACT_ONE));
  403. }
  404. break;
  405. default:
  406. break;
  407. }
  408. }
  409. /*!
  410. \brief configure ADC regular channel
  411. \param[in] adc_periph: ADCx,x=0,1,2
  412. \param[in] rank: the regular group sequencer rank,this parameter must be between 0 to 15
  413. \param[in] adc_channel: the selected ADC channel
  414. only one parameter can be selected which is shown as below:
  415. \arg ADC_CHANNEL_x(x=0..18): ADC Channelx
  416. \param[in] sample_time: the sample time value
  417. only one parameter can be selected which is shown as below:
  418. \arg ADC_SAMPLETIME_3: 3 cycles
  419. \arg ADC_SAMPLETIME_15: 15 cycles
  420. \arg ADC_SAMPLETIME_28: 28 cycles
  421. \arg ADC_SAMPLETIME_56: 56 cycles
  422. \arg ADC_SAMPLETIME_84: 84 cycles
  423. \arg ADC_SAMPLETIME_112: 112 cycles
  424. \arg ADC_SAMPLETIME_144: 144 cycles
  425. \arg ADC_SAMPLETIME_480: 480 cycles
  426. \param[out] none
  427. \retval none
  428. */
  429. void adc_regular_channel_config(uint32_t adc_periph , uint8_t rank , uint8_t adc_channel , uint32_t sample_time)
  430. {
  431. uint32_t rsq,sampt;
  432. /* ADC regular sequence config */
  433. if(rank < ADC_REGULAR_CHANNEL_RANK_SIX){
  434. /* the regular group sequence rank is smaller than six */
  435. rsq = ADC_RSQ2(adc_periph);
  436. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (ADC_REGULAR_CHANNEL_RANK_LENGTH*rank)));
  437. /* the channel number is written to these bits to select a channel as the nth conversion in the regular channel group */
  438. rsq |= ((uint32_t)adc_channel << (ADC_REGULAR_CHANNEL_RANK_LENGTH*rank));
  439. ADC_RSQ2(adc_periph) = rsq;
  440. }else if(rank < ADC_REGULAR_CHANNEL_RANK_TWELVE){
  441. /* the regular group sequence rank is smaller than twelve */
  442. rsq = ADC_RSQ1(adc_periph);
  443. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_SIX))));
  444. /* the channel number is written to these bits to select a channel as the nth conversion in the regular channel group */
  445. rsq |= ((uint32_t)adc_channel << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_SIX)));
  446. ADC_RSQ1(adc_periph) = rsq;
  447. }else if(rank < ADC_REGULAR_CHANNEL_RANK_SIXTEEN){
  448. /* the regular group sequence rank is smaller than sixteen */
  449. rsq = ADC_RSQ0(adc_periph);
  450. rsq &= ~((uint32_t)(ADC_RSQX_RSQN << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_TWELVE))));
  451. /* the channel number is written to these bits to select a channel as the nth conversion in the regular channel group */
  452. rsq |= ((uint32_t)adc_channel << (ADC_REGULAR_CHANNEL_RANK_LENGTH*(rank-ADC_REGULAR_CHANNEL_RANK_TWELVE)));
  453. ADC_RSQ0(adc_periph) = rsq;
  454. }else{
  455. }
  456. /* ADC sampling time config */
  457. if(adc_channel < ADC_CHANNEL_SAMPLE_TEN){
  458. /* the regular group sequence rank is smaller than ten */
  459. sampt = ADC_SAMPT1(adc_periph);
  460. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel)));
  461. /* channel sample time set*/
  462. sampt |= (uint32_t)(sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel));
  463. ADC_SAMPT1(adc_periph) = sampt;
  464. }else if(adc_channel <= ADC_CHANNEL_SAMPLE_EIGHTEEN){
  465. /* the regular group sequence rank is smaller than eighteen */
  466. sampt = ADC_SAMPT0(adc_periph);
  467. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel-ADC_CHANNEL_SAMPLE_TEN))));
  468. /* channel sample time set*/
  469. sampt |= (uint32_t)(sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel-ADC_CHANNEL_SAMPLE_TEN)));
  470. ADC_SAMPT0(adc_periph) = sampt;
  471. }else{
  472. }
  473. }
  474. /*!
  475. \brief configure ADC inserted channel
  476. \param[in] adc_periph: ADCx,x=0,1,2
  477. \param[in] rank: the inserted group sequencer rank,this parameter must be between 0 to 3
  478. \param[in] adc_channel: the selected ADC channel
  479. only one parameter can be selected which is shown as below:
  480. \arg ADC_CHANNEL_x(x=0..18): ADC Channelx
  481. \param[in] sample_time: The sample time value
  482. only one parameter can be selected which is shown as below:
  483. \arg ADC_SAMPLETIME_3: 3 cycles
  484. \arg ADC_SAMPLETIME_15: 15 cycles
  485. \arg ADC_SAMPLETIME_28: 28 cycles
  486. \arg ADC_SAMPLETIME_56: 56 cycles
  487. \arg ADC_SAMPLETIME_84: 84 cycles
  488. \arg ADC_SAMPLETIME_112: 112 cycles
  489. \arg ADC_SAMPLETIME_144: 144 cycles
  490. \arg ADC_SAMPLETIME_480: 480 cycles
  491. \param[out] none
  492. \retval none
  493. */
  494. void adc_inserted_channel_config(uint32_t adc_periph , uint8_t rank , uint8_t adc_channel , uint32_t sample_time)
  495. {
  496. uint8_t inserted_length;
  497. uint32_t isq,sampt;
  498. /* get inserted channel group length */
  499. inserted_length = (uint8_t)GET_BITS(ADC_ISQ(adc_periph) , 20U , 21U);
  500. /* the channel number is written to these bits to select a channel as the nth conversion in the inserted channel group */
  501. if(rank < 4U){
  502. isq = ADC_ISQ(adc_periph);
  503. isq &= ~((uint32_t)(ADC_ISQ_ISQN << (ADC_INSERTED_CHANNEL_SHIFT_LENGTH-(inserted_length-rank)*ADC_INSERTED_CHANNEL_RANK_LENGTH)));
  504. isq |= ((uint32_t)adc_channel << (ADC_INSERTED_CHANNEL_SHIFT_LENGTH-(inserted_length-rank)*ADC_INSERTED_CHANNEL_RANK_LENGTH));
  505. ADC_ISQ(adc_periph) = isq;
  506. }
  507. /* ADC sampling time config */
  508. if(adc_channel < ADC_CHANNEL_SAMPLE_TEN){
  509. /* the inserted group sequence rank is smaller than ten */
  510. sampt = ADC_SAMPT1(adc_periph);
  511. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel)));
  512. /* channel sample time set*/
  513. sampt |= (uint32_t) sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*adc_channel);
  514. ADC_SAMPT1(adc_periph) = sampt;
  515. }else if(adc_channel <= ADC_CHANNEL_SAMPLE_EIGHTEEN){
  516. /* the inserted group sequence rank is smaller than eighteen */
  517. sampt = ADC_SAMPT0(adc_periph);
  518. sampt &= ~((uint32_t)(ADC_SAMPTX_SPTN << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel - ADC_CHANNEL_SAMPLE_TEN))));
  519. /* channel sample time set*/
  520. sampt |= ((uint32_t)sample_time << (ADC_CHANNEL_SAMPLE_LENGTH*(adc_channel - ADC_CHANNEL_SAMPLE_TEN)));
  521. ADC_SAMPT0(adc_periph) = sampt;
  522. }else{
  523. }
  524. }
  525. /*!
  526. \brief configure ADC inserted channel offset
  527. \param[in] adc_periph: ADCx,x=0,1,2
  528. \param[in] inserted_channel : insert channel select
  529. only one parameter can be selected which is shown as below:
  530. \arg ADC_INSERTED_CHANNEL_0: inserted channel0
  531. \arg ADC_INSERTED_CHANNEL_1: inserted channel1
  532. \arg ADC_INSERTED_CHANNEL_2: inserted channel2
  533. \arg ADC_INSERTED_CHANNEL_3: inserted channel3
  534. \param[in] offset : the offset data
  535. \param[out] none
  536. \retval none
  537. */
  538. void adc_inserted_channel_offset_config(uint32_t adc_periph , uint8_t inserted_channel , uint16_t offset)
  539. {
  540. uint8_t inserted_length;
  541. uint32_t num = 0U;
  542. inserted_length = (uint8_t)GET_BITS(ADC_ISQ(adc_periph) , 20U , 21U);
  543. num = ((uint32_t)ADC_OFFSET_LENGTH - ((uint32_t)inserted_length - (uint32_t)inserted_channel));
  544. if(num <= ADC_OFFSET_LENGTH){
  545. /* calculate the offset of the register */
  546. num = num * ADC_OFFSET_SHIFT_LENGTH;
  547. /* config the offset of the selected channels */
  548. REG32((adc_periph) + 0x14U + num) = IOFFX_IOFF((uint32_t)offset);
  549. }
  550. }
  551. /*!
  552. \brief configure ADC external trigger source
  553. \param[in] adc_periph: ADCx,x=0,1,2
  554. \param[in] adc_channel_group: select the channel group
  555. only one parameter can be selected which is shown as below:
  556. \arg ADC_REGULAR_CHANNEL: regular channel group
  557. \arg ADC_INSERTED_CHANNEL: inserted channel group
  558. \param[in] external_trigger_source: regular or inserted group trigger source
  559. for regular channel:
  560. only one parameter can be selected which is shown as below:
  561. \arg ADC_EXTTRIG_REGULAR_T0_CH0: external trigger timer 0 CC0 event select for regular channel
  562. \arg ADC_EXTTRIG_REGULAR_T0_CH1: external trigger timer 0 CC1 event select for regular channel
  563. \arg ADC_EXTTRIG_REGULAR_T0_CH2: external trigger timer 0 CC2 event select for regular channel
  564. \arg ADC_EXTTRIG_REGULAR_T1_CH1: external trigger timer 1 CC1 event select for regular channel
  565. \arg ADC_EXTTRIG_REGULAR_T1_CH2: external trigger timer 1 CC2 event select for regular channel
  566. \arg ADC_EXTTRIG_REGULAR_T1_CH3: external trigger timer 1 CC3 event select for regular channel
  567. \arg ADC_EXTTRIG_REGULAR_T1_TRGO: external trigger timer 1 TRGO event select for regular channel
  568. \arg ADC_EXTTRIG_REGULAR_T2_CH0 : external trigger timer 2 CC0 event select for regular channel
  569. \arg ADC_EXTTRIG_REGULAR_T2_TRGO : external trigger timer 2 TRGO event select for regular channel
  570. \arg ADC_EXTTRIG_REGULAR_T3_CH3: external trigger timer 3 CC3 event select for regular channel
  571. \arg ADC_EXTTRIG_REGULAR_T4_CH0: external trigger timer 4 CC0 event select for regular channel
  572. \arg ADC_EXTTRIG_REGULAR_T4_CH1: external trigger timer 4 CC1 event select for regular channel
  573. \arg ADC_EXTTRIG_REGULAR_T4_CH2: external trigger timer 4 CC2 event select for regular channel
  574. \arg ADC_EXTTRIG_REGULAR_T7_CH0: external trigger timer 7 CC0 event select for regular channel
  575. \arg ADC_EXTTRIG_REGULAR_T7_TRGO: external trigger timer 7 TRGO event select for regular channel
  576. \arg ADC_EXTTRIG_REGULAR_EXTI_11: external trigger extiline 11 select for regular channel
  577. for inserted channel:
  578. only one parameter can be selected which is shown as below:
  579. \arg ADC_EXTTRIG_INSERTED_T0_CH3: timer0 capture compare 3
  580. \arg ADC_EXTTRIG_INSERTED_T0_TRGO: timer0 TRGO event
  581. \arg ADC_EXTTRIG_INSERTED_T1_CH0: timer1 capture compare 0
  582. \arg ADC_EXTTRIG_INSERTED_T1_TRGO: timer1 TRGO event
  583. \arg ADC_EXTTRIG_INSERTED_T2_CH1: timer2 capture compare 1
  584. \arg ADC_EXTTRIG_INSERTED_T2_CH3: timer2 capture compare 3
  585. \arg ADC_EXTTRIG_INSERTED_T3_CH0: timer3 capture compare 0
  586. \arg ADC_EXTTRIG_INSERTED_T3_CH1: timer3 capture compare 1
  587. \arg ADC_EXTTRIG_INSERTED_T3_CH2: timer3 capture compare 2
  588. \arg ADC_EXTTRIG_INSERTED_T3_TRGO: timer3 capture compare TRGO
  589. \arg ADC_EXTTRIG_INSERTED_T4_CH3: timer4 capture compare 3
  590. \arg ADC_EXTTRIG_INSERTED_T4_TRGO: timer4 capture compare TRGO
  591. \arg ADC_EXTTRIG_INSERTED_T7_CH1: timer7 capture compare 1
  592. \arg ADC_EXTTRIG_INSERTED_T7_CH2: timer7 capture compare 2
  593. \arg ADC_EXTTRIG_INSERTED_T7_CH3: timer7 capture compare 3
  594. \arg ADC_EXTTRIG_INSERTED_EXTI_15: external interrupt line 15
  595. \param[out] none
  596. \retval none
  597. */
  598. void adc_external_trigger_source_config(uint32_t adc_periph , uint8_t adc_channel_group , uint32_t external_trigger_source)
  599. {
  600. switch(adc_channel_group){
  601. case ADC_REGULAR_CHANNEL:
  602. /* configure ADC regular group external trigger source */
  603. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSRC);
  604. ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
  605. break;
  606. case ADC_INSERTED_CHANNEL:
  607. /* configure ADC inserted group external trigger source */
  608. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETSIC);
  609. ADC_CTL1(adc_periph) |= (uint32_t)external_trigger_source;
  610. break;
  611. default:
  612. break;
  613. }
  614. }
  615. /*!
  616. \brief enable ADC external trigger
  617. \param[in] adc_periph: ADCx,x=0,1,2
  618. \param[in] adc_channel_group: select the channel group
  619. only one parameter can be selected which is shown as below:
  620. \arg ADC_REGULAR_CHANNEL: regular channel group
  621. \arg ADC_INSERTED_CHANNEL: inserted channel group
  622. \param[in] trigger_mode: external trigger mode
  623. only one parameter can be selected which is shown as below:
  624. \arg EXTERNAL_TRIGGER_DISABLE: external trigger disable
  625. \arg EXTERNAL_TRIGGER_RISING: rising edge of external trigger
  626. \arg EXTERNAL_TRIGGER_FALLING: falling edge of external trigger
  627. \arg EXTERNAL_TRIGGER_RISING_FALLING: rising and falling edge of external trigger
  628. \param[out] none
  629. \retval none
  630. */
  631. void adc_external_trigger_config(uint32_t adc_periph , uint8_t adc_channel_group , uint32_t trigger_mode)
  632. {
  633. switch(adc_channel_group){
  634. case ADC_REGULAR_CHANNEL:
  635. /* configure ADC regular channel group external trigger mode */
  636. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETMRC);
  637. ADC_CTL1(adc_periph) |= (uint32_t) (trigger_mode << REGULAR_TRIGGER_MODE);
  638. break;
  639. case ADC_INSERTED_CHANNEL:
  640. /* configure ADC inserted channel group external trigger mode */
  641. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_ETMIC);
  642. ADC_CTL1(adc_periph) |= (uint32_t) (trigger_mode << INSERTED_TRIGGER_MODE);
  643. break;
  644. default:
  645. break;
  646. }
  647. }
  648. /*!
  649. \brief enable ADC software trigger
  650. \param[in] adc_periph: ADCx,x=0,1,2
  651. \param[in] adc_channel_group: select the channel group
  652. only one parameter can be selected which is shown as below:
  653. \arg ADC_REGULAR_CHANNEL: regular channel group
  654. \arg ADC_INSERTED_CHANNEL: inserted channel group
  655. \param[out] none
  656. \retval none
  657. */
  658. void adc_software_trigger_enable(uint32_t adc_periph , uint8_t adc_channel_group)
  659. {
  660. switch(adc_channel_group){
  661. case ADC_REGULAR_CHANNEL:
  662. /* enable ADC regular channel group software trigger */
  663. ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_SWRCST;
  664. break;
  665. case ADC_INSERTED_CHANNEL:
  666. /* enable ADC inserted channel group software trigger */
  667. ADC_CTL1(adc_periph) |= (uint32_t)ADC_CTL1_SWICST;
  668. break;
  669. default:
  670. break;
  671. }
  672. }
  673. /*!
  674. \brief configure end of conversion mode
  675. \param[in] adc_periph: ADCx,x=0,1,2
  676. \param[in] end_selection: end of conversion mode
  677. only one parameter can be selected which is shown as below:
  678. \arg ADC_EOC_SET_SEQUENCE: only at the end of a sequence of regular conversions, the EOC bit is set.Overflow detection is disabled unless DMA=1.
  679. \arg ADC_EOC_SET_CONVERSION: at the end of each regular conversion, the EOC bit is set.Overflow is detected automatically.
  680. \param[out] none
  681. \retval none
  682. */
  683. void adc_end_of_conversion_config(uint32_t adc_periph , uint8_t end_selection)
  684. {
  685. switch(end_selection){
  686. case ADC_EOC_SET_SEQUENCE:
  687. /* only at the end of a sequence of regular conversions, the EOC bit is set */
  688. ADC_CTL1(adc_periph) &= ~((uint32_t)ADC_CTL1_EOCM);
  689. break;
  690. case ADC_EOC_SET_CONVERSION:
  691. /* at the end of each regular conversion, the EOC bit is set.Overflow is detected automatically */
  692. ADC_CTL1(adc_periph) |= (uint32_t)(ADC_CTL1_EOCM);
  693. break;
  694. default:
  695. break;
  696. }
  697. }
  698. /*!
  699. \brief read ADC regular group data register
  700. \param[in] adc_periph: ADCx,x=0,1,2
  701. \param[in] none
  702. \param[out] none
  703. \retval the conversion value
  704. */
  705. uint16_t adc_regular_data_read(uint32_t adc_periph)
  706. {
  707. return (uint16_t)(ADC_RDATA(adc_periph));
  708. }
  709. /*!
  710. \brief read ADC inserted group data register
  711. \param[in] adc_periph: ADCx,x=0,1,2
  712. \param[in] inserted_channel : insert channel select
  713. only one parameter can be selected which is shown as below:
  714. \arg ADC_INSERTED_CHANNEL_0: inserted Channel0
  715. \arg ADC_INSERTED_CHANNEL_1: inserted channel1
  716. \arg ADC_INSERTED_CHANNEL_2: inserted Channel2
  717. \arg ADC_INSERTED_CHANNEL_3: inserted Channel3
  718. \param[out] none
  719. \retval the conversion value
  720. */
  721. uint16_t adc_inserted_data_read(uint32_t adc_periph , uint8_t inserted_channel)
  722. {
  723. uint32_t idata;
  724. /* read the data of the selected channel */
  725. switch(inserted_channel){
  726. case ADC_INSERTED_CHANNEL_0:
  727. /* read the data of channel 0 */
  728. idata = ADC_IDATA0(adc_periph);
  729. break;
  730. case ADC_INSERTED_CHANNEL_1:
  731. /* read the data of channel 1 */
  732. idata = ADC_IDATA1(adc_periph);
  733. break;
  734. case ADC_INSERTED_CHANNEL_2:
  735. /* read the data of channel 2 */
  736. idata = ADC_IDATA2(adc_periph);
  737. break;
  738. case ADC_INSERTED_CHANNEL_3:
  739. /* read the data of channel 3 */
  740. idata = ADC_IDATA3(adc_periph);
  741. break;
  742. default:
  743. idata = 0U;
  744. break;
  745. }
  746. return (uint16_t)idata;
  747. }
  748. /*!
  749. \brief disable ADC analog watchdog single channel
  750. \param[in] adc_periph: ADCx,x=0,1,2
  751. \param[out] none
  752. \retval none
  753. */
  754. void adc_watchdog_single_channel_disable(uint32_t adc_periph )
  755. {
  756. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_WDSC);
  757. }
  758. /*!
  759. \brief enable ADC analog watchdog single channel
  760. \param[in] adc_periph: ADCx,x=0,1,2
  761. \param[in] adc_channel: the selected ADC channel
  762. only one parameter can be selected which is shown as below:
  763. \arg ADC_CHANNEL_x: ADC Channelx(x=0..18)
  764. \param[out] none
  765. \retval none
  766. */
  767. void adc_watchdog_single_channel_enable(uint32_t adc_periph , uint8_t adc_channel)
  768. {
  769. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_WDCHSEL);
  770. /* analog watchdog channel select */
  771. ADC_CTL0(adc_periph) |= (uint32_t)adc_channel;
  772. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_WDSC;
  773. }
  774. /*!
  775. \brief configure ADC analog watchdog group channel
  776. \param[in] adc_periph: ADCx,x=0,1,2
  777. \param[in] adc_channel_group: the channel group use analog watchdog
  778. only one parameter can be selected which is shown as below:
  779. \arg ADC_REGULAR_CHANNEL: regular channel group
  780. \arg ADC_INSERTED_CHANNEL: inserted channel group
  781. \arg ADC_REGULAR_INSERTED_CHANNEL: both regular and inserted group
  782. \param[out] none
  783. \retval none
  784. */
  785. void adc_watchdog_group_channel_enable(uint32_t adc_periph , uint8_t adc_channel_group)
  786. {
  787. ADC_CTL0(adc_periph) &= ~((uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN | ADC_CTL0_WDSC));
  788. /* select the group */
  789. switch(adc_channel_group){
  790. case ADC_REGULAR_CHANNEL:
  791. /* regular channel analog watchdog enable */
  792. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_RWDEN;
  793. break;
  794. case ADC_INSERTED_CHANNEL:
  795. /* inserted channel analog watchdog enable */
  796. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_IWDEN;
  797. break;
  798. case ADC_REGULAR_INSERTED_CHANNEL:
  799. /* regular and inserted channel analog watchdog enable */
  800. ADC_CTL0(adc_periph) |= (uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN);
  801. break;
  802. default:
  803. break;
  804. }
  805. }
  806. /*!
  807. \brief disable ADC analog watchdog
  808. \param[in] adc_periph: ADCx,x=0,1,2
  809. \param[in] adc_channel_group: the channel group use analog watchdog
  810. only one parameter can be selected which is shown as below:
  811. \arg ADC_REGULAR_CHANNEL: regular channel group
  812. \arg ADC_INSERTED_CHANNEL: inserted channel group
  813. \arg ADC_REGULAR_INSERTED_CHANNEL: both regular and inserted group
  814. \param[out] none
  815. \retval none
  816. */
  817. void adc_watchdog_disable(uint32_t adc_periph , uint8_t adc_channel_group)
  818. {
  819. /* select the group */
  820. switch(adc_channel_group){
  821. case ADC_REGULAR_CHANNEL:
  822. /* disable ADC analog watchdog regular channel group */
  823. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_RWDEN);
  824. break;
  825. case ADC_INSERTED_CHANNEL:
  826. /* disable ADC analog watchdog inserted channel group */
  827. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_IWDEN);
  828. break;
  829. case ADC_REGULAR_INSERTED_CHANNEL:
  830. /* disable ADC analog watchdog regular and inserted channel group */
  831. ADC_CTL0(adc_periph) &= ~((uint32_t)(ADC_CTL0_RWDEN | ADC_CTL0_IWDEN));
  832. break;
  833. default:
  834. break;
  835. }
  836. }
  837. /*!
  838. \brief configure ADC analog watchdog threshold
  839. \param[in] adc_periph: ADCx,x=0,1,2
  840. \param[in] low_threshold: analog watchdog low threshold,0..4095
  841. \param[in] high_threshold: analog watchdog high threshold,0..4095
  842. \param[out] none
  843. \retval none
  844. */
  845. void adc_watchdog_threshold_config(uint32_t adc_periph , uint16_t low_threshold , uint16_t high_threshold)
  846. {
  847. /* configure ADC analog watchdog low threshold */
  848. ADC_WDLT(adc_periph) = (uint32_t)WDLT_WDLT(low_threshold);
  849. /* configure ADC analog watchdog high threshold */
  850. ADC_WDHT(adc_periph) = (uint32_t)WDHT_WDHT(high_threshold);
  851. }
  852. /*!
  853. \brief get the ADC flag bits
  854. \param[in] adc_periph: ADCx,x=0,1,2
  855. \param[in] adc_flag: the adc flag bits
  856. only one parameter can be selected which is shown as below:
  857. \arg ADC_FLAG_WDE: analog watchdog event flag
  858. \arg ADC_FLAG_EOC: end of group conversion flag
  859. \arg ADC_FLAG_EOIC: end of inserted group conversion flag
  860. \arg ADC_FLAG_STIC: start flag of inserted channel group
  861. \arg ADC_FLAG_STRC: start flag of regular channel group
  862. \arg ADC_FLAG_ROVF: regular data register overflow flag
  863. \param[out] none
  864. \retval FlagStatus: SET or RESET
  865. */
  866. FlagStatus adc_flag_get(uint32_t adc_periph , uint32_t adc_flag)
  867. {
  868. FlagStatus reval = RESET;
  869. if(ADC_STAT(adc_periph) & adc_flag){
  870. reval = SET;
  871. }
  872. return reval;
  873. }
  874. /*!
  875. \brief clear the ADC flag bits
  876. \param[in] adc_periph: ADCx,x=0,1,2
  877. \param[in] adc_flag: the adc flag bits
  878. only one parameter can be selected which is shown as below:
  879. \arg ADC_FLAG_WDE: analog watchdog event flag
  880. \arg ADC_FLAG_EOC: end of group conversion flag
  881. \arg ADC_FLAG_EOIC: end of inserted group conversion flag
  882. \arg ADC_FLAG_STIC: start flag of inserted channel group
  883. \arg ADC_FLAG_STRC: start flag of regular channel group
  884. \arg ADC_FLAG_ROVF: regular data register overflow flag
  885. \param[out] none
  886. \retval none
  887. */
  888. void adc_flag_clear(uint32_t adc_periph , uint32_t adc_flag)
  889. {
  890. ADC_STAT(adc_periph) &= ~((uint32_t)adc_flag);
  891. }
  892. /*!
  893. \brief get the bit state of ADCx software start conversion
  894. \param[in] adc_periph: ADCx, x=0,1,2 only one among these parameters can be selected
  895. \param[in] none
  896. \param[out] none
  897. \retval FlagStatus: SET or RESET
  898. */
  899. FlagStatus adc_regular_software_startconv_flag_get(uint32_t adc_periph)
  900. {
  901. FlagStatus reval = RESET;
  902. if((uint32_t)RESET != (ADC_STAT(adc_periph) & ADC_STAT_STRC)){
  903. reval = SET;
  904. }
  905. return reval;
  906. }
  907. /*!
  908. \brief get the bit state of ADCx software inserted channel start conversion
  909. \param[in] adc_periph: ADCx, x=0,1,2 only one among these parameters can be selected
  910. \param[in] none
  911. \param[out] none
  912. \retval FlagStatus: SET or RESET
  913. */
  914. FlagStatus adc_inserted_software_startconv_flag_get(uint32_t adc_periph)
  915. {
  916. FlagStatus reval = RESET;
  917. if((uint32_t)RESET != (ADC_STAT(adc_periph) & ADC_STAT_STIC)){
  918. reval = SET;
  919. }
  920. return reval;
  921. }
  922. /*!
  923. \brief get the ADC interrupt bits
  924. \param[in] adc_periph: ADCx,x=0,1,2
  925. \param[in] adc_interrupt: the adc interrupt bits
  926. only one parameter can be selected which is shown as below:
  927. \arg ADC_INT_FLAG_WDE: analog watchdog interrupt
  928. \arg ADC_INT_FLAG_EOC: end of group conversion interrupt
  929. \arg ADC_INT_FLAG_EOIC: end of inserted group conversion interrupt
  930. \arg ADC_INT_FLAG_ROVF: regular data register overflow interrupt
  931. \param[out] none
  932. \retval FlagStatus: SET or RESET
  933. */
  934. FlagStatus adc_interrupt_flag_get(uint32_t adc_periph , uint32_t adc_interrupt)
  935. {
  936. FlagStatus interrupt_flag = RESET;
  937. uint32_t state;
  938. /* check the interrupt bits */
  939. switch(adc_interrupt){
  940. case ADC_INT_FLAG_WDE:
  941. /* get the ADC analog watchdog interrupt bits */
  942. state = ADC_STAT(adc_periph) & ADC_STAT_WDE;
  943. if((ADC_CTL0(adc_periph) & ADC_CTL0_WDEIE) && state){
  944. interrupt_flag = SET;
  945. }
  946. break;
  947. case ADC_INT_FLAG_EOC:
  948. /* get the ADC end of group conversion interrupt bits */
  949. state = ADC_STAT(adc_periph) & ADC_STAT_EOC;
  950. if((ADC_CTL0(adc_periph) & ADC_CTL0_EOCIE) && state){
  951. interrupt_flag = SET;
  952. }
  953. break;
  954. case ADC_INT_FLAG_EOIC:
  955. /* get the ADC end of inserted group conversion interrupt bits */
  956. state = ADC_STAT(adc_periph) & ADC_STAT_EOIC;
  957. if((ADC_CTL0(adc_periph) & ADC_CTL0_EOICIE) && state){
  958. interrupt_flag = SET;
  959. }
  960. break;
  961. case ADC_INT_FLAG_ROVF:
  962. /* get the ADC regular data register overflow interrupt bits */
  963. state = ADC_STAT(adc_periph) & ADC_STAT_ROVF;
  964. if((ADC_CTL0(adc_periph) & ADC_CTL0_ROVFIE) && state){
  965. interrupt_flag = SET;
  966. }
  967. break;
  968. default:
  969. break;
  970. }
  971. return interrupt_flag;
  972. }
  973. /*!
  974. \brief clear the ADC flag
  975. \param[in] adc_periph: ADCx,x=0,1,2
  976. \param[in] adc_interrupt: the adc status flag
  977. only one parameter can be selected which is shown as below:
  978. \arg ADC_INT_FLAG_WDE: analog watchdog interrupt
  979. \arg ADC_INT_FLAG_EOC: end of group conversion interrupt
  980. \arg ADC_INT_FLAG_EOIC: end of inserted group conversion interrupt
  981. \arg ADC_INT_FLAG_ROVF: regular data register overflow interrupt
  982. \param[out] none
  983. \retval none
  984. */
  985. void adc_interrupt_flag_clear(uint32_t adc_periph , uint32_t adc_interrupt)
  986. {
  987. ADC_STAT(adc_periph) &= ~((uint32_t)adc_interrupt);
  988. }
  989. /*!
  990. \brief enable ADC interrupt
  991. \param[in] adc_periph: ADCx,x=0,1,2
  992. \param[in] adc_interrupt: the adc interrupt flag
  993. only one parameter can be selected which is shown as below:
  994. \arg ADC_INT_WDE: analog watchdog interrupt flag
  995. \arg ADC_INT_EOC: end of group conversion interrupt flag
  996. \arg ADC_INT_EOIC: end of inserted group conversion interrupt flag
  997. \arg ADC_INT_ROVF: regular data register overflow interrupt flag
  998. \param[out] none
  999. \retval none
  1000. */
  1001. void adc_interrupt_enable(uint32_t adc_periph , uint32_t adc_interrupt)
  1002. {
  1003. switch(adc_interrupt){
  1004. case ADC_INT_WDE:
  1005. /* enable analog watchdog interrupt */
  1006. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_WDEIE;
  1007. break;
  1008. case ADC_INT_EOC:
  1009. /* enable end of group conversion interrupt */
  1010. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_EOCIE;
  1011. break;
  1012. case ADC_INT_EOIC:
  1013. /* enable end of inserted group conversion interrupt */
  1014. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_EOICIE;
  1015. break;
  1016. case ADC_INT_ROVF:
  1017. ADC_CTL0(adc_periph) |= (uint32_t) ADC_CTL0_ROVFIE;
  1018. break;
  1019. default:
  1020. break;
  1021. }
  1022. }
  1023. /*!
  1024. \brief disable ADC interrupt
  1025. \param[in] adc_periph: ADCx,x=0,1,2
  1026. \param[in] adc_flag: the adc interrupt flag
  1027. only one parameter can be selected which is shown as below:
  1028. \arg ADC_INT_WDE: analog watchdog interrupt flag
  1029. \arg ADC_INT_EOC: end of group conversion interrupt flag
  1030. \arg ADC_INT_EOIC: end of inserted group conversion interrupt flag
  1031. \arg ADC_INT_ROVF: regular data register overflow interrupt flag
  1032. \param[out] none
  1033. \retval none
  1034. */
  1035. void adc_interrupt_disable(uint32_t adc_periph , uint32_t adc_interrupt)
  1036. {
  1037. switch(adc_interrupt){
  1038. /* select the interrupt source */
  1039. case ADC_INT_WDE:
  1040. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_WDEIE);
  1041. break;
  1042. case ADC_INT_EOC:
  1043. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_EOCIE);
  1044. break;
  1045. case ADC_INT_EOIC:
  1046. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_EOICIE);
  1047. break;
  1048. case ADC_INT_ROVF:
  1049. ADC_CTL0(adc_periph) &= ~((uint32_t)ADC_CTL0_ROVFIE);
  1050. break;
  1051. default:
  1052. break;
  1053. }
  1054. }
  1055. /*!
  1056. \brief configure the ADC sync mode
  1057. \param[in] sync_mode: ADC sync mode
  1058. only one parameter can be selected which is shown as below:
  1059. \arg ADC_SYNC_MODE_INDEPENDENT: all the ADCs work independently
  1060. \arg ADC_DAUL_REGULAL_PARALLEL_INSERTED_PARALLEL: ADC0 and ADC1 work in combined regular parallel & inserted parallel mode
  1061. \arg ADC_DAUL_REGULAL_PARALLEL_INSERTED_ROTATION: ADC0 and ADC1 work in combined regular parallel & trigger rotation mode
  1062. \arg ADC_DAUL_INSERTED_PARALLEL: ADC0 and ADC1 work in inserted parallel mode
  1063. \arg ADC_DAUL_REGULAL_PARALLEL: ADC0 and ADC1 work in regular parallel mode
  1064. \arg ADC_DAUL_REGULAL_FOLLOW_UP: ADC0 and ADC1 work in follow-up mode
  1065. \arg ADC_DAUL_INSERTED_TRRIGGER_ROTATION: ADC0 and ADC1 work in trigger rotation mode
  1066. \arg ADC_ALL_REGULAL_PARALLEL_INSERTED_PARALLEL: all ADCs work in combined regular parallel & inserted parallel mode
  1067. \arg ADC_ALL_REGULAL_PARALLEL_INSERTED_ROTATION: all ADCs work in combined regular parallel & trigger rotation mode
  1068. \arg ADC_ALL_INSERTED_PARALLEL: all ADCs work in inserted parallel mode
  1069. \arg ADC_ALL_REGULAL_PARALLEL: all ADCs work in regular parallel mode
  1070. \arg ADC_ALL_REGULAL_FOLLOW_UP: all ADCs work in follow-up mode
  1071. \arg ADC_ALL_INSERTED_TRRIGGER_ROTATION: all ADCs work in trigger rotation mode
  1072. \param[out] none
  1073. \retval none
  1074. */
  1075. void adc_sync_mode_config(uint32_t sync_mode)
  1076. {
  1077. ADC_SYNCCTL &= ~(ADC_SYNCCTL_SYNCM);
  1078. ADC_SYNCCTL |= sync_mode;
  1079. }
  1080. /*!
  1081. \brief configure the delay between 2 sampling phases in ADC sync modes
  1082. \param[in] sample_delay: the delay between 2 sampling phases in ADC sync modes
  1083. only one parameter can be selected which is shown as below:
  1084. \arg ADC_SYNC_DELAY_xCYCLE: x=5..20,the delay between 2 sampling phases in ADC sync modes is x ADC clock cycles
  1085. \param[out] none
  1086. \retval none
  1087. */
  1088. void adc_sync_delay_config(uint32_t sample_delay)
  1089. {
  1090. ADC_SYNCCTL &= ~(ADC_SYNCCTL_SYNCDLY);
  1091. ADC_SYNCCTL |= sample_delay;
  1092. }
  1093. /*!
  1094. \brief configure ADC sync DMA mode selection
  1095. \param[in] dma_mode: ADC sync DMA mode
  1096. only one parameter can be selected which is shown as below:
  1097. \arg ADC_SYNC_DMA_DISABLE: ADC sync DMA disabled
  1098. \arg ADC_SYNC_DMA_MODE0: ADC sync DMA mode 0
  1099. \arg ADC_SYNC_DMA_MODE1: ADC sync DMA mode 1
  1100. \param[out] none
  1101. \retval none
  1102. */
  1103. void adc_sync_dma_config(uint32_t dma_mode )
  1104. {
  1105. ADC_SYNCCTL &= ~(ADC_SYNCCTL_SYNCDMA);
  1106. ADC_SYNCCTL |= dma_mode;
  1107. }
  1108. /*!
  1109. \brief configure ADC sync DMA engine is disabled after the end of transfer signal from DMA controller is detected
  1110. \param[in] none
  1111. \param[out] none
  1112. \retval none
  1113. */
  1114. void adc_sync_dma_request_after_last_enable(void)
  1115. {
  1116. ADC_SYNCCTL |= ADC_SYNCCTL_SYNCDDM;
  1117. }
  1118. /*!
  1119. \brief configure ADC sync DMA engine issues requests according to the SYNCDMA bits
  1120. \param[in] none
  1121. \param[out] none
  1122. \retval none
  1123. */
  1124. void adc_sync_dma_request_after_last_disable(void)
  1125. {
  1126. ADC_SYNCCTL &= ~(ADC_SYNCCTL_SYNCDDM);
  1127. }
  1128. /*!
  1129. \brief read ADC sync regular data register
  1130. \param[in] none
  1131. \param[out] none
  1132. \retval sync regular data
  1133. */
  1134. uint32_t adc_sync_regular_data_read(void)
  1135. {
  1136. return (uint32_t)ADC_SYNCDATA;
  1137. }