apm32f10x_adc.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /*!
  2. * @file apm32f10x_adc.c
  3. *
  4. * @brief This file provides all the ADC firmware functions
  5. *
  6. * @version V1.0.2
  7. *
  8. * @date 2022-01-05
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2020-2022 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be usefull and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. #include "apm32f10x_adc.h"
  26. #include "apm32f10x_rcm.h"
  27. /** @addtogroup Peripherals_Library Standard Peripheral Library
  28. @{
  29. */
  30. /** @addtogroup ADC_Driver ADC Driver
  31. @{
  32. */
  33. /** @addtogroup ADC_Fuctions Fuctions
  34. @{
  35. */
  36. /*!
  37. * @brief Reset ADC peripheral registers to their default reset values.
  38. *
  39. * @param adc: Select ADC peripheral.
  40. *
  41. * @retval None
  42. *
  43. * @note adc can be ADC1, ADC2 or ADC3.
  44. */
  45. void ADC_Reset(ADC_T *adc)
  46. {
  47. if (adc == ADC1)
  48. {
  49. RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_ADC1);
  50. RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_ADC1);
  51. }
  52. else if (adc == ADC2)
  53. {
  54. RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_ADC2);
  55. RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_ADC2);
  56. }
  57. else if (adc == ADC3)
  58. {
  59. RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_ADC3);
  60. RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_ADC3);
  61. }
  62. }
  63. /*!
  64. * @brief Config the ADC peripheral according to the specified parameters in the adcConfig.
  65. *
  66. * @param adc: Select ADC peripheral.
  67. *
  68. * @param adcConfig: pointer to a ADC_Config_T structure.
  69. *
  70. * @retval None
  71. *
  72. * @note adc can be ADC1, ADC2 or ADC3.
  73. */
  74. void ADC_Config(ADC_T *adc, ADC_Config_T *adcConfig)
  75. {
  76. uint32_t reg;
  77. reg = adc->CTRL1;
  78. reg &= 0xFFF0FEFF;
  79. reg |= (uint32_t)((adcConfig->mode) | ((uint32_t)adcConfig->scanConvMode << 8));
  80. adc->CTRL1 = reg;
  81. reg = adc->CTRL2;
  82. reg &= 0xFFF1F7FD;
  83. reg |= (uint32_t)adcConfig->dataAlign | \
  84. (uint32_t)adcConfig->externalTrigConv | \
  85. ((uint32_t)adcConfig->continuosConvMode << 1);
  86. adc->CTRL2 = reg;
  87. reg = adc->REGSEQ1;
  88. reg &= 0xFF0FFFFF;
  89. reg |= (uint32_t)((adcConfig->nbrOfChannel - (uint8_t)1) << 20);
  90. adc->REGSEQ1 = reg;
  91. }
  92. /*!
  93. * @brief Fills each ADC_Config_T member with its default value.
  94. *
  95. * @param adcConfig: pointer to a ADC_Config_T structure which will be initialized.
  96. *
  97. * @retval None
  98. */
  99. void ADC_ConfigStructInit(ADC_Config_T *adcConfig)
  100. {
  101. adcConfig->mode = ADC_MODE_INDEPENDENT;
  102. adcConfig->scanConvMode = DISABLE;
  103. adcConfig->continuosConvMode = DISABLE;
  104. adcConfig->externalTrigConv = ADC_EXT_TRIG_CONV_TMR1_CC1;
  105. adcConfig->dataAlign = ADC_DATA_ALIGN_RIGHT;
  106. adcConfig->nbrOfChannel = 1;
  107. }
  108. /*!
  109. * @brief Enables the specified ADC peripheral.
  110. *
  111. * @param adc: Select the ADC peripheral.
  112. *
  113. * @retval None
  114. *
  115. * @note adc can be ADC1, ADC2 or ADC3.
  116. */
  117. void ADC_Enable(ADC_T *adc)
  118. {
  119. adc->CTRL2_B.ADCEN = BIT_SET;
  120. }
  121. /*!
  122. * @brief Disable the specified ADC peripheral.
  123. *
  124. * @param adc: Select the ADC peripheral.
  125. *
  126. * @retval None
  127. *
  128. * @note adc can be ADC1, ADC2 or ADC3.
  129. */
  130. void ADC_Disable(ADC_T *adc)
  131. {
  132. adc->CTRL2_B.ADCEN = BIT_RESET;
  133. }
  134. /*!
  135. * @brief Disable the specified ADC DMA request.
  136. *
  137. * @param adc: Select the ADC peripheral.
  138. *
  139. * @retval None
  140. *
  141. * @note adc can be ADC1, ADC2 or ADC3.
  142. */
  143. void ADC_EnableDMA(ADC_T *adc)
  144. {
  145. adc->CTRL2_B.DMAEN = BIT_SET;
  146. }
  147. /*!
  148. * @brief Disable the specified ADC DMA request.
  149. *
  150. * @param adc: Select the ADC peripheral.
  151. *
  152. * @retval None
  153. *
  154. * @note adc can be ADC1, ADC2 or ADC3.
  155. */
  156. void ADC_DisableDMA(ADC_T *adc)
  157. {
  158. adc->CTRL2_B.DMAEN = BIT_RESET;
  159. }
  160. /*!
  161. * @brief Reset the specified ADC calibration registers.
  162. *
  163. * @param adc: Select the ADC peripheral.
  164. *
  165. * @retval None
  166. *
  167. * @note adc can be ADC1, ADC2 or ADC3.
  168. */
  169. void ADC_ResetCalibration(ADC_T *adc)
  170. {
  171. adc->CTRL2_B.CALRST = BIT_SET;
  172. }
  173. /*!
  174. * @brief Reads the specified ADC calibration reset status.
  175. *
  176. * @param adc: Select the ADC peripheral.
  177. *
  178. * @retval The status of ADC calibration reset.
  179. *
  180. * @note adc can be ADC1, ADC2 or ADC3.
  181. */
  182. uint8_t ADC_ReadResetCalibrationStatus(ADC_T *adc)
  183. {
  184. uint8_t ret;
  185. ret = (adc->CTRL2_B.CALRST) ? BIT_SET : BIT_RESET;
  186. return ret;
  187. }
  188. /*!
  189. * @brief Starts the specified ADC calibration.
  190. *
  191. * @param adc: Select the ADC peripheral.
  192. *
  193. * @retval None
  194. *
  195. * @note adc can be ADC1, ADC2 or ADC3.
  196. */
  197. void ADC_StartCalibration(ADC_T *adc)
  198. {
  199. adc->CTRL2_B.CAL = BIT_SET;
  200. }
  201. /*!
  202. * @brief Reads the specified ADC calibration start flag.
  203. *
  204. * @param adc: Select the ADC peripheral.
  205. *
  206. * @retval The status of ADC calibration start.
  207. *
  208. * @note adc can be ADC1, ADC2 or ADC3.
  209. */
  210. uint8_t ADC_ReadCalibrationStartFlag(ADC_T *adc)
  211. {
  212. uint8_t ret;
  213. ret = (adc->CTRL2_B.CAL) ? BIT_SET : BIT_RESET;
  214. return ret;
  215. }
  216. /*!
  217. * @brief Enables the specified ADC software start conversion.
  218. *
  219. * @param adc: Select the ADC peripheral.
  220. *
  221. * @retval None
  222. *
  223. * @note adc can be ADC1, ADC2 or ADC3.
  224. */
  225. void ADC_EnableSoftwareStartConv(ADC_T *adc)
  226. {
  227. adc->CTRL2 |= 0x00500000;
  228. }
  229. /*!
  230. * @brief Disable the specified ADC software start conversion.
  231. *
  232. * @param adc: Select the ADC peripheral.
  233. *
  234. * @retval None
  235. *
  236. * @note adc can be ADC1, ADC2 or ADC3.
  237. */
  238. void ADC_DisableSoftwareStartConv(ADC_T *adc)
  239. {
  240. adc->CTRL2 &= 0xFFAFFFFF;
  241. }
  242. /*!
  243. * @brief Reads the specified ADC Software start conversion Status.
  244. *
  245. * @param adc: Select the ADC peripheral.
  246. *
  247. * @retval The status of ADC Software start conversion registers.
  248. *
  249. * @note adc can be ADC1, ADC2 or ADC3.
  250. */
  251. uint8_t ADC_ReadSoftwareStartConvStatus(ADC_T *adc)
  252. {
  253. uint8_t ret;
  254. ret = (adc->CTRL2_B.REGSWSC) ? BIT_SET : BIT_RESET;
  255. return ret;
  256. }
  257. /*!
  258. * @brief Configures the specified ADC regular discontinuous mode.
  259. *
  260. * @param adc: Select the ADC peripheral.
  261. *
  262. * @param number: The number of the discontinuous mode regular channels.
  263. * This parameter can be between 1 and 8.
  264. *
  265. * @retval None
  266. *
  267. * @note adc can be ADC1, ADC2 or ADC3.
  268. */
  269. void ADC_ConfigDiscMode(ADC_T *adc, uint8_t number)
  270. {
  271. adc->CTRL1_B.DISCNUMCFG |= number - 1;
  272. }
  273. /*!
  274. * @brief Enable the specified ADC regular discontinuous mode.
  275. *
  276. * @param adc: Select the ADC peripheral.
  277. *
  278. * @retval None
  279. *
  280. * @note adc can be ADC1, ADC2 or ADC3.
  281. */
  282. void ADC_EnableDiscMode(ADC_T *adc)
  283. {
  284. adc->CTRL1_B.REGDISCEN = BIT_SET;
  285. }
  286. /*!
  287. * @brief Disable the specified ADC regular discontinuous mode.
  288. *
  289. * @param adc: Select the ADC peripheral.
  290. *
  291. * @retval None
  292. *
  293. * @note adc can be ADC1, ADC2 or ADC3.
  294. */
  295. void ADC_DisableDiscMode(ADC_T *adc)
  296. {
  297. adc->CTRL1_B.REGDISCEN = BIT_RESET;
  298. }
  299. /*!
  300. * @brief Configures the specified ADC regular channel.
  301. *
  302. * @param adc: Select the ADC peripheral.
  303. *
  304. * @param channel: Select the ADC channel.
  305. * This parameter can be one of the following values:
  306. * @arg ADC_CHANNEL_0: ADC channel 0
  307. * @arg ADC_CHANNEL_1: ADC channel 1
  308. * @arg ADC_CHANNEL_2: ADC channel 2
  309. * @arg ADC_CHANNEL_3: ADC channel 3
  310. * @arg ADC_CHANNEL_4: ADC channel 4
  311. * @arg ADC_CHANNEL_5: ADC channel 5
  312. * @arg ADC_CHANNEL_6: ADC channel 6
  313. * @arg ADC_CHANNEL_7: ADC channel 7
  314. * @arg ADC_CHANNEL_8: ADC channel 8
  315. * @arg ADC_CHANNEL_9: ADC channel 9
  316. * @arg ADC_CHANNEL_10: ADC channel 10
  317. * @arg ADC_CHANNEL_11: ADC channel 11
  318. * @arg ADC_CHANNEL_12: ADC channel 12
  319. * @arg ADC_CHANNEL_13: ADC channel 13
  320. * @arg ADC_CHANNEL_14: ADC channel 14
  321. * @arg ADC_CHANNEL_15: ADC channel 15
  322. * @arg ADC_CHANNEL_16: ADC channel 16 which is connected to TempSensor
  323. * @arg ADC_CHANNEL_17: ADC channel 17 which is connected to Vrefint
  324. *
  325. * @param rank: The rank in the regular group sequencer
  326. * This parameter must be between 1 to 16.
  327. *
  328. * @param sampleTime: the specified ADC channel SampleTime
  329. * The parameter can be one of following values:
  330. * @arg ADC_SAMPLETIME_1CYCLES5: ADC 1.5 clock cycles
  331. * @arg ADC_SAMPLETIME_7CYCLES5: ADC 7.5 clock cycles
  332. * @arg ADC_SAMPLETIME_13CYCLES5: ADC 13.5 clock cycles
  333. * @arg ADC_SAMPLETIME_28CYCLES5: ADC 28.5 clock cycles
  334. * @arg ADC_SAMPLETIME_41CYCLES5: ADC 41.5 clock cycles
  335. * @arg ADC_SAMPLETIME_55CYCLES5: ADC 55.5 clock cycles
  336. * @arg ADC_SAMPLETIME_71CYCLES5: ADC 71.5 clock cycles
  337. * @arg ADC_SAMPLETIME_239CYCLES5: ADC 239.5 clock cycles
  338. *
  339. * @retval None
  340. *
  341. * @note adc can be ADC1, ADC2 or ADC3.
  342. */
  343. void ADC_ConfigRegularChannel(ADC_T *adc, uint8_t channel, uint8_t rank, uint8_t sampleTime)
  344. {
  345. uint32_t temp1 = 0;
  346. uint32_t temp2 = 0;
  347. if (channel > ADC_CHANNEL_9)
  348. {
  349. temp1 = adc->SMPTIM1;
  350. temp2 = SMPCYCCFG_SET_SMPTIM1 << (3 * (channel - 10));
  351. temp1 &= ~temp2;
  352. temp2 = (uint32_t)sampleTime << (3 * (channel - 10));
  353. temp1 |= temp2;
  354. adc->SMPTIM1 = temp1;
  355. }
  356. else
  357. {
  358. temp1 = adc->SMPTIM2;
  359. temp2 = SMPCYCCFG_SET_SMPTIM2 << (3 * channel);
  360. temp1 &= ~temp2;
  361. temp2 = (uint32_t)sampleTime << (3 * channel);
  362. temp1 |= temp2;
  363. adc->SMPTIM2 = temp1;
  364. }
  365. if (rank < 7)
  366. {
  367. temp1 = adc->REGSEQ3;
  368. temp2 = REGSEQC_SET_REGSEQ3 << (5 * (rank - 1));
  369. temp1 &= ~temp2;
  370. temp2 = (uint32_t)channel << (5 * (rank - 1));
  371. temp1 |= temp2;
  372. adc->REGSEQ3 = temp1;
  373. }
  374. else if (rank < 13)
  375. {
  376. temp1 = adc->REGSEQ2;
  377. temp2 = REGSEQC_SET_REGSEQ2 << (5 * (rank - 7));
  378. temp1 &= ~temp2;
  379. temp2 = (uint32_t)channel << (5 * (rank - 7));
  380. temp1 |= temp2;
  381. adc->REGSEQ2 = temp1;
  382. }
  383. else
  384. {
  385. temp1 = adc->REGSEQ1;
  386. temp2 = REGSEQC_SET_REGSEQ1 << (5 * (rank - 13));
  387. temp1 &= ~temp2;
  388. temp2 = (uint32_t)channel << (5 * (rank - 13));
  389. temp1 |= temp2;
  390. adc->REGSEQ1 = temp1;
  391. }
  392. }
  393. /*!
  394. * @brief Enable the specified ADC regular channel external trigger.
  395. *
  396. * @param adc: Select the ADC peripheral.
  397. *
  398. * @retval None
  399. *
  400. * @note adc can be ADC1, ADC2 or ADC3.
  401. */
  402. void ADC_EnableExternalTrigConv(ADC_T *adc)
  403. {
  404. adc->CTRL2_B.REGEXTTRGEN = BIT_SET;
  405. }
  406. /*!
  407. * @brief Disable the specified ADC regular channel external trigger.
  408. *
  409. * @param adc: Select the ADC peripheral.
  410. *
  411. * @retval None
  412. *
  413. * @note adc can be ADC1, ADC2 or ADC3.
  414. */
  415. void ADC_DisableExternalTrigConv(ADC_T *adc)
  416. {
  417. adc->CTRL2_B.REGEXTTRGEN = BIT_RESET;
  418. }
  419. /*!
  420. * @brief Reads the specified ADC conversion result data.
  421. *
  422. * @param adc: Select the ADC peripheral.
  423. *
  424. * @retval The Data conversion value.
  425. *
  426. * @note adc can be ADC1, ADC2 or ADC3.
  427. */
  428. uint16_t ADC_ReadConversionValue(ADC_T *adc)
  429. {
  430. return (uint16_t) adc->REGDATA;
  431. }
  432. /*!
  433. * @brief Reads the specified ADC conversion result data in dual mode.
  434. *
  435. * @param adc: Select the ADC peripheral.
  436. *
  437. * @retval The Data conversion value.
  438. *
  439. * @note adc can be ADC1, ADC2 or ADC3.
  440. */
  441. uint32_t ADC_ReadDualModeConversionValue(ADC_T *adc)
  442. {
  443. return (*(__IOM uint32_t *) RDG_ADDRESS);
  444. }
  445. /*!
  446. * @brief Enable the specified ADC automatic injected group.
  447. *
  448. * @param adc: Select the ADC peripheral.
  449. *
  450. * @retval None
  451. *
  452. * @note adc can be ADC1, ADC2 or ADC3.
  453. */
  454. void ADC_EnableAutoInjectedConv(ADC_T *adc)
  455. {
  456. adc->CTRL1_B.INJGACEN = BIT_SET;
  457. }
  458. /*!
  459. * @brief Disable the specified ADC automatic injected group.
  460. *
  461. * @param adc: Select the ADC peripheral.
  462. *
  463. * @retval None
  464. *
  465. * @note adc can be ADC1, ADC2 or ADC3.
  466. */
  467. void ADC_DisableAutoInjectedConv(ADC_T *adc)
  468. {
  469. adc->CTRL1_B.INJGACEN = BIT_RESET;
  470. }
  471. /*!
  472. * @brief Enable the specified ADC discontinuous mode for injected group.
  473. *
  474. * @param adc: Select the ADC peripheral.
  475. *
  476. * @retval None
  477. *
  478. * @note adc can be ADC1, ADC2 or ADC3.
  479. */
  480. void ADC_EnableInjectedDiscMode(ADC_T *adc)
  481. {
  482. adc->CTRL1_B.INJDISCEN = BIT_SET;
  483. }
  484. /*!
  485. * @brief Disable the specified ADC discontinuous mode for injected group.
  486. *
  487. * @param adc: Select the ADC peripheral.
  488. *
  489. * @retval None
  490. *
  491. * @note adc can be ADC1, ADC2 or ADC3.
  492. */
  493. void ADC_DisableInjectedDiscMode(ADC_T *adc)
  494. {
  495. adc->CTRL1_B.INJDISCEN = BIT_RESET;
  496. }
  497. /*!
  498. * @brief Configures the specified ADC external trigger for injected channels conversion
  499. *
  500. * @param adc: Select the ADC peripheral
  501. *
  502. * @param extTrigInjecConv: Select the ADC trigger to start injected conversion
  503. * This parameter can be one of the following values:
  504. * @arg ADC_EXT_TRIG_INJEC_CONV_TMR1_TRGO : Select Timer1 TRGO event (for ADC1, ADC2 and ADC3)
  505. * @arg ADC_EXT_TRIG_INJEC_CONV_TMR1_CC4 : Select Timer1 capture compare4 (for ADC1, ADC2 and ADC3)
  506. * @arg ADC_EXT_TRIG_INJEC_CONV_TMR2_TRGO : Select Timer2 TRGO event (for ADC1 and ADC2)
  507. * @arg ADC_EXT_TRIG_INJEC_CONV_TMR2_CC1 : Select Timer2 capture compare1 (for ADC1 and ADC2)
  508. * @arg ADC_EXT_TRIG_INJEC_CONV_TMR3_CC4 : Select Timer3 capture compare4 (for ADC1 and ADC2)
  509. * @arg ADC_EXT_TRIG_INJEC_CONV_TMR4_TRGO : Select Timer4 TRGO event selected (for ADC1 and ADC2)
  510. * @arg ADC_EXT_TRIG_INJEC_CONV_EINT15_T8_CC4: External interrupt line 15 or Timer8 capture compare4 event
  511. * (for ADC1 and ADC2)
  512. *
  513. * @arg ADC_EXT_TRIG_INJEC_CONV_TMR4_CC3 : Timer4 capture compare3 selected (for ADC3 only)
  514. * @arg ADC_EXT_TRIG_INJEC_CONV_TMR8_CC2 : Timer8 capture compare2 selected (for ADC3 only)
  515. * @arg ADC_EXT_TRIG_INJEC_CONV_TMR8_CC4 : Timer8 capture compare4 selected (for ADC3 only)
  516. * @arg ADC_EXT_TRIG_INJEC_CONV_TMR5_TRGO: Timer5 TRGO event selected (for ADC3 only)
  517. * @arg ADC_EXT_TRIG_INJEC_CONV_TMR5_CC4 : Timer5 capture compare4 selected (for ADC3 only)
  518. * @arg ADC_EXT_TRIG_INJEC_CONV_NONE : Injected conversion started by software instead of external trigger
  519. * (for ADC1, ADC2 and ADC3)
  520. *
  521. * @retval None
  522. *
  523. * @note adc can be ADC1, ADC2 or ADC3.
  524. */
  525. void ADC_ConfigExternalTrigInjectedConv(ADC_T *adc, ADC_EXT_TRIG_INJEC_CONV_T extTrigInjecConv)
  526. {
  527. adc->CTRL2_B.INJGEXTTRGSEL = RESET;
  528. adc->CTRL2_B.INJGEXTTRGSEL |= extTrigInjecConv;
  529. }
  530. /*!
  531. * @brief Ensable the specified ADC injected channels conversion through
  532. *
  533. * @param adc: Select the ADC peripheral
  534. *
  535. * @retval None
  536. *
  537. * @note adc can be ADC1, ADC2 or ADC3.
  538. */
  539. void ADC_EnableExternalTrigInjectedConv(ADC_T *adc)
  540. {
  541. adc->CTRL2_B.INJEXTTRGEN = BIT_SET;
  542. }
  543. /*!
  544. * @brief Disable the specified ADC injected channels conversion through
  545. *
  546. * @param adc: Select the ADC peripheral
  547. *
  548. * @retval None
  549. *
  550. * @note adc can be ADC1, ADC2 or ADC3.
  551. */
  552. void ADC_DisableExternalTrigInjectedConv(ADC_T *adc)
  553. {
  554. adc->CTRL2_B.INJEXTTRGEN = BIT_RESET;
  555. }
  556. /*!
  557. * @brief Enable the specified ADC start of the injected
  558. *
  559. * @param adc: Select the ADC peripheral
  560. *
  561. * @retval None
  562. *
  563. * @note adc can be ADC1, ADC2 or ADC3.
  564. */
  565. void ADC_EnableSoftwareStartInjectedConv(ADC_T *adc)
  566. {
  567. adc->CTRL2_B.INJEXTTRGEN = BIT_SET;
  568. adc->CTRL2_B.INJSWSC = BIT_SET;
  569. }
  570. /*!
  571. * @brief Disable the specified ADC start of the injected
  572. *
  573. * @param adc: Select the ADC peripheral
  574. *
  575. * @retval None
  576. *
  577. * @note adc can be ADC1, ADC2 or ADC3.
  578. */
  579. void ADC_DisableSoftwareStartInjectedConv(ADC_T *adc)
  580. {
  581. adc->CTRL2_B.INJEXTTRGEN = BIT_RESET;
  582. adc->CTRL2_B.INJSWSC = BIT_RESET;
  583. }
  584. /*!
  585. * @brief Reads the specified ADC Software start injected conversion Status
  586. *
  587. * @param adc: Select the ADC peripheral
  588. *
  589. * @retval The status of ADC Software start injected conversion
  590. *
  591. * @note adc can be ADC1, ADC2 or ADC3.
  592. */
  593. uint8_t ADC_ReadSoftwareStartInjectedConvStatus(ADC_T *adc)
  594. {
  595. uint8_t ret;
  596. ret = (adc->CTRL2_B.INJSWSC) ? BIT_SET : BIT_RESET;
  597. return ret;
  598. }
  599. /*!
  600. * @brief Configures the specified ADC injected channel.
  601. *
  602. * @param adc: Select the ADC peripheral.
  603. *
  604. * @param channel: Select the ADC injected channel.
  605. * This parameter can be one of the following values:
  606. * @arg ADC_CHANNEL_0: ADC channel 0
  607. * @arg ADC_CHANNEL_1: ADC channel 1
  608. * @arg ADC_CHANNEL_2: ADC channel 2
  609. * @arg ADC_CHANNEL_3: ADC channel 3
  610. * @arg ADC_CHANNEL_4: ADC channel 4
  611. * @arg ADC_CHANNEL_5: ADC channel 5
  612. * @arg ADC_CHANNEL_6: ADC channel 6
  613. * @arg ADC_CHANNEL_7: ADC channel 7
  614. * @arg ADC_CHANNEL_8: ADC channel 8
  615. * @arg ADC_CHANNEL_9: ADC channel 9
  616. * @arg ADC_CHANNEL_10: ADC channel 10
  617. * @arg ADC_CHANNEL_11: ADC channel 11
  618. * @arg ADC_CHANNEL_12: ADC channel 12
  619. * @arg ADC_CHANNEL_13: ADC channel 13
  620. * @arg ADC_CHANNEL_14: ADC channel 14
  621. * @arg ADC_CHANNEL_15: ADC channel 15
  622. * @arg ADC_CHANNEL_16: ADC channel 16 which is connected to TempSensor
  623. * @arg ADC_CHANNEL_17: ADC channel 17 which is connected to Vrefint
  624. *
  625. * @param rank: The rank in the injected group sequencer.
  626. * This parameter must be between 1 to 4.
  627. *
  628. * @param sampleTime: the specified ADC channel SampleTime
  629. * The parameter can be one of following values:
  630. * @arg ADC_SAMPLETIME_1CYCLES5: ADC 1.5 clock cycles
  631. * @arg ADC_SAMPLETIME_7CYCLES5: ADC 7.5 clock cycles
  632. * @arg ADC_SAMPLETIME_13CYCLES5: ADC 13.5 clock cycles
  633. * @arg ADC_SAMPLETIME_28CYCLES5: ADC 28.5 clock cycles
  634. * @arg ADC_SAMPLETIME_41CYCLES5: ADC 41.5 clock cycles
  635. * @arg ADC_SAMPLETIME_55CYCLES5: ADC 55.5 clock cycles
  636. * @arg ADC_SAMPLETIME_71CYCLES5: ADC 71.5 clock cycles
  637. * @arg ADC_SAMPLETIME_239CYCLES5: ADC 239.5 clock cycles
  638. *
  639. * @retval None
  640. *
  641. * @note adc can be ADC1, ADC2 or ADC3.
  642. */
  643. void ADC_ConfigInjectedChannel(ADC_T *adc, uint8_t channel, uint8_t rank, uint8_t sampleTime)
  644. {
  645. uint32_t temp1 = 0;
  646. uint32_t temp2 = 0;
  647. uint32_t temp3 = 0;
  648. if (channel > ADC_CHANNEL_9)
  649. {
  650. temp1 = adc->SMPTIM1;
  651. temp2 = SMPCYCCFG_SET_SMPTIM1 << (3 * (channel - 10));
  652. temp1 &= ~temp2;
  653. temp2 = (uint32_t)sampleTime << (3 * (channel - 10));
  654. temp1 |= temp2;
  655. adc->SMPTIM1 = temp1;
  656. }
  657. else
  658. {
  659. temp1 = adc->SMPTIM2;
  660. temp2 = SMPCYCCFG_SET_SMPTIM2 << (3 * channel);
  661. temp1 &= ~temp2;
  662. temp2 = (uint32_t)sampleTime << (3 * channel);
  663. temp1 |= temp2;
  664. adc->SMPTIM2 = temp1;
  665. }
  666. temp1 = adc->INJSEQ;
  667. temp3 = (temp1 & INJSEQ_SET_INJSEQLEN) >> 20;
  668. temp2 = INJSEQ_SET_INJSEQC << (5 * (uint8_t)((rank + 3) - (temp3 + 1)));
  669. temp1 &= ~temp2;
  670. temp2 = (uint32_t)channel << (5 * (uint8_t)((rank + 3) - (temp3 + 1)));
  671. temp1 |= temp2;
  672. adc->INJSEQ = temp1;
  673. }
  674. /*!
  675. * @brief Configures the specified ADC injected channel.
  676. *
  677. * @param adc: Select the ADC peripheral.
  678. *
  679. * @param length: The sequencer length.
  680. * This parameter must be a number between 1 to 4.
  681. *
  682. * @retval None
  683. *
  684. * @note adc can be ADC1, ADC2 or ADC3.
  685. */
  686. void ADC_ConfigInjectedSequencerLength(ADC_T *adc, uint8_t length)
  687. {
  688. adc->INJSEQ_B.INJSEQLEN = RESET;
  689. adc->INJSEQ_B.INJSEQLEN |= length - 1;
  690. }
  691. /*!
  692. * @brief Configures the specified ADC injected channel conversion value offset.
  693. *
  694. * @param adc: Select the ADC peripheral.
  695. *
  696. * @param channel: Select the ADC injected channel.
  697. * This parameter can be one of the following values:
  698. * @arg ADC_INJEC_CHANNEL_1: select Injected Channel 1
  699. * @arg ADC_INJEC_CHANNEL_2: select Injected Channel 2
  700. * @arg ADC_INJEC_CHANNEL_3: select Injected Channel 3
  701. * @arg ADC_INJEC_CHANNEL_4: select Injected Channel 4
  702. *
  703. * @param offSet: The specified ADC injected channel offset.
  704. * This parameter must be a 12bit value.
  705. *
  706. * @retval None
  707. *
  708. * @note adc can be ADC1, ADC2 or ADC3.
  709. */
  710. void ADC_ConfigInjectedOffset(ADC_T *adc, ADC_INJEC_CHANNEL_T channel, uint16_t offSet)
  711. {
  712. __IOM uint32_t tmp = 0;
  713. tmp = (uint32_t)adc;
  714. tmp += channel;
  715. *(__IOM uint32_t *) tmp = (uint32_t)offSet;
  716. }
  717. /*!
  718. * @brief Reads the ADC injected channel conversion value.
  719. *
  720. * @param adc: Select the ADC peripheral.
  721. *
  722. * @param channel: Select the ADC injected channel.
  723. * This parameter can be one of the following values:
  724. * @arg ADC_INJEC_CHANNEL_1: select Injected Channel 1
  725. * @arg ADC_INJEC_CHANNEL_2: select Injected Channel 2
  726. * @arg ADC_INJEC_CHANNEL_3: select Injected Channel 3
  727. * @arg ADC_INJEC_CHANNEL_4: select Injected Channel 4
  728. *
  729. * @retval The Data of conversion value.
  730. *
  731. * @note adc can be ADC1, ADC2 or ADC3.
  732. */
  733. uint16_t ADC_ReadInjectedConversionValue(ADC_T *adc, ADC_INJEC_CHANNEL_T channel)
  734. {
  735. __IOM uint32_t temp = 0;
  736. temp = (uint32_t)adc;
  737. temp += channel + INJDATA_OFFSET;
  738. return (uint16_t)(*(__IOM uint32_t *) temp);
  739. }
  740. /*!
  741. * @brief Enable the specified ADC analog watchdog.
  742. *
  743. * @param adc: Select the ADC peripheral.
  744. *
  745. * @param analogWatchdog: The ADC analog watchdog configuration
  746. * This parameter can be one of the following values:
  747. * @arg ADC_ANALOG_WATCHDOG_SINGLE_REG : Analog watchdog on a single regular channel
  748. * @arg ADC_ANALOG_WATCHDOG_SINGLE_INJEC : Analog watchdog on a single injected channel
  749. * @arg ADC_ANALOG_WATCHDOG_SINGLE_REG_INJEC : Analog watchdog on a single regular or injected channel
  750. * @arg ADC_ANALOG_WATCHDOG_ALL_REG : Analog watchdog on all regular channel
  751. * @arg ADC_ANALOG_WATCHDOG_ALL_INJEC : Analog watchdog on all injected channel
  752. * @arg ADC_ANALOG_WATCHDOG_ALL_REG_ALL_INJEC : Analog watchdog on all regular and injected channels
  753. * @arg ADC_ANALOG_WATCHDOG_NONE : No channel guarded by the analog watchdog
  754. *
  755. * @retval None
  756. *
  757. * @note adc can be ADC1, ADC2 or ADC3.
  758. */
  759. void ADC_EnableAnalogWatchdog(ADC_T *adc, uint32_t analogWatchdog)
  760. {
  761. adc->CTRL1 &= 0xFF3FFDFF;
  762. adc->CTRL1 |= analogWatchdog;
  763. }
  764. /*!
  765. * @brief Disable the specified ADC analog watchdog.
  766. *
  767. * @param adc: Select the ADC peripheral.
  768. *
  769. * @retval None
  770. *
  771. * @note adc can be ADC1, ADC2 or ADC3.
  772. */
  773. void ADC_DisableAnalogWatchdog(ADC_T *adc)
  774. {
  775. adc->CTRL1 &= 0xFF3FFDFF;
  776. }
  777. /*!
  778. * @brief Configures the specified ADC high and low thresholds of the analog watchdog.
  779. *
  780. * @param adc: Select the ADC peripheral.
  781. *
  782. * @param highThreshold: The ADC analog watchdog High threshold value.
  783. * This parameter must be a 12bit value.
  784. *
  785. * @param lowThreshold: The ADC analog watchdog Low threshold value.
  786. * This parameter must be a 12bit value.
  787. *
  788. * @retval None
  789. *
  790. * @note adc can be ADC1, ADC2 or ADC3.
  791. */
  792. void ADC_ConfigAnalogWatchdogThresholds(ADC_T *adc, uint16_t highThreshold, uint16_t lowThreshold)
  793. {
  794. adc->AWDHT = highThreshold;
  795. adc->AWDLT = lowThreshold;
  796. }
  797. /*!
  798. * @brief Configures the specified ADC analog watchdog guarded single channel
  799. *
  800. * @param adc: Select the ADC peripheral
  801. *
  802. * @param channel: Select the ADC channel
  803. * This parameter can be one of the following values:
  804. * @arg ADC_Channel_0: Select ADC Channel 0
  805. * @arg ADC_Channel_1: Select ADC Channel 1
  806. * @arg ADC_Channel_2: Select ADC Channel 2
  807. * @arg ADC_Channel_3: Select ADC Channel 3
  808. * @arg ADC_Channel_4: Select ADC Channel 4
  809. * @arg ADC_Channel_5: Select ADC Channel 5
  810. * @arg ADC_Channel_6: Select ADC Channel 6
  811. * @arg ADC_Channel_7: Select ADC Channel 7
  812. * @arg ADC_Channel_8: Select ADC Channel 8
  813. * @arg ADC_Channel_9: Select ADC Channel 9
  814. * @arg ADC_Channel_10: Select ADC Channel 10
  815. * @arg ADC_Channel_11: Select ADC Channel 11
  816. * @arg ADC_Channel_12: Select ADC Channel 12
  817. * @arg ADC_Channel_13: Select ADC Channel 13
  818. * @arg ADC_Channel_14: Select ADC Channel 14
  819. * @arg ADC_Channel_15: Select ADC Channel 15
  820. * @arg ADC_Channel_16: Select ADC Channel 16 which is connected to TempSensor
  821. * @arg ADC_Channel_17: Select ADC Channel 17 which is connected to Vrefint
  822. *
  823. * @retval None
  824. *
  825. * @note adc can be ADC1, ADC2 or ADC3.
  826. */
  827. void ADC_ConfigAnalogWatchdogSingleChannel(ADC_T *adc, uint8_t channel)
  828. {
  829. adc->CTRL1_B.AWDCHSEL = BIT_RESET;
  830. adc->CTRL1 |= channel;
  831. }
  832. /*!
  833. * @brief Enable the specified ADC temperature sensor and Vrefint channel.
  834. *
  835. * @param adc: Select the ADC peripheral.
  836. *
  837. * @retval None
  838. *
  839. * @note adc can be ADC1, ADC2 or ADC3.
  840. */
  841. void ADC_EnableTempSensorVrefint(ADC_T *adc)
  842. {
  843. adc->CTRL2_B.TSVREFEN = BIT_SET;
  844. }
  845. /*!
  846. * @brief Disable the specified ADC temperature sensor and Vrefint channel.
  847. *
  848. * @param adc: Select the ADC peripheral
  849. *
  850. * @retval None
  851. *
  852. * @note adc can be ADC1, ADC2 or ADC3.
  853. */
  854. void ADC_DisableTempSensorVrefint(ADC_T *adc)
  855. {
  856. adc->CTRL2_B.TSVREFEN = BIT_RESET;
  857. }
  858. /*!
  859. * @brief Enable the specified ADC interrupt.
  860. *
  861. * @param adc: Select the ADC peripheral.
  862. *
  863. * @param interrupt: Select the ADC interrupt sources
  864. * This parameter can be any combination of the following values:
  865. * @arg ADC_INT_AWD : Enable Analog watchdog interrupt
  866. * @arg ADC_INT_EOC : Enable End of conversion interrupt
  867. * @arg ADC_INT_INJEOC : Enable End of injected conversion interrupt
  868. *
  869. * @retval None
  870. *
  871. * @note adc can be ADC1, ADC2 or ADC3.
  872. */
  873. void ADC_EnableInterrupt(ADC_T *adc, uint16_t interrupt)
  874. {
  875. uint8_t mask;
  876. mask = (uint8_t)interrupt;
  877. adc->CTRL1 |= (uint8_t)mask;
  878. }
  879. /*!
  880. * @brief Disable the specified ADC interrupt.
  881. *
  882. * @param adc: Select the ADC peripheral.
  883. *
  884. * @param interrupt: Select the ADC interrupt sources
  885. * This parameter can be any combination of the following values:
  886. * @arg ADC_INT_AWD : Disable Analog watchdog interrupt
  887. * @arg ADC_INT_EOC : Disable End of conversion interrupt
  888. * @arg ADC_INT_INJEOC : Disable End of injected conversion interrupt
  889. *
  890. * @retval None
  891. *
  892. * @note adc can be ADC1, ADC2 or ADC3.
  893. */
  894. void ADC_DisableInterrupt(ADC_T *adc, uint16_t interrupt)
  895. {
  896. uint8_t mask;
  897. mask = (uint8_t)interrupt;
  898. adc->CTRL1 &= (~(uint32_t)mask);
  899. }
  900. /*!
  901. * @brief Reads the specified ADC flag
  902. *
  903. * @param adc: Select the ADC peripheral
  904. *
  905. * @param flag: Select the flag to check
  906. * This parameter can be one of the following values:
  907. * @arg ADC_FLAG_AWD : Analog watchdog flag
  908. * @arg ADC_FLAG_EOC : End of conversion flag
  909. * @arg ADC_FLAG_INJEOC: End of injected group conversion flag
  910. * @arg ADC_FLAG_INJCS : Injected group conversion Start flag
  911. * @arg ADC_FLAG_REGCS : Regular group conversion Start flag
  912. *
  913. * @retval The status of ADC flag
  914. *
  915. * @note adc can be ADC1, ADC2 or ADC3.
  916. */
  917. uint8_t ADC_ReadStatusFlag(ADC_T *adc, ADC_FLAG_T flag)
  918. {
  919. return (adc->STS & flag) ? SET : RESET;
  920. }
  921. /*!
  922. * @brief Clears the specified ADC flag
  923. *
  924. * @param adc: Select the ADC peripheral
  925. *
  926. * @param flag: Select the flag to clear
  927. * This parameter can be any combination of the following values:
  928. * @arg ADC_FLAG_AWD : Analog watchdog flag
  929. * @arg ADC_FLAG_EOC : End of conversion flag
  930. * @arg ADC_FLAG_INJEOC: End of injected group conversion flag
  931. * @arg ADC_FLAG_INJCS : Injected group conversion Start flag
  932. * @arg ADC_FLAG_REGCS : Regular group conversion Start flag
  933. *
  934. * @retval None
  935. *
  936. * @note adc can be ADC1, ADC2 or ADC3.
  937. */
  938. void ADC_ClearStatusFlag(ADC_T *adc, uint8_t flag)
  939. {
  940. adc->STS = ~(uint32_t)flag;
  941. }
  942. /*!
  943. * @brief Reads the specified ADC Interrupt flag.
  944. *
  945. * @param adc: Select the ADC peripheral.
  946. *
  947. * @param interrupt: Select the ADC interrupt source.
  948. * This parameter can be one of the following values:
  949. * @arg ADC_INT_AWD : Enable Analog watchdog interrupt
  950. * @arg ADC_INT_EOC : Enable End of conversion interrupt
  951. * @arg ADC_INT_INJEOC : Enable End of injected conversion interrupt
  952. *
  953. * @retval The status of ADC interrupt
  954. *
  955. * @note adc can be ADC1, ADC2 or ADC3.
  956. */
  957. uint8_t ADC_ReadIntFlag(ADC_T *adc, ADC_INT_T flag)
  958. {
  959. uint8_t bitStatus = RESET;
  960. uint32_t itmask = 0;
  961. uint32_t enableStatus = 0;
  962. itmask = flag >> 8;
  963. enableStatus = (adc->CTRL1 & (uint8_t)flag);
  964. if (((adc->STS & itmask) != (uint32_t)RESET) && enableStatus)
  965. {
  966. bitStatus = SET;
  967. }
  968. else
  969. {
  970. bitStatus = RESET;
  971. }
  972. return bitStatus;
  973. }
  974. /*!
  975. * @brief Clears the specified ADC Interrupt pending bits.
  976. *
  977. * @param adc: Select the ADC peripheral.
  978. *
  979. * @param interrupt: Select the ADC interrupt source.
  980. * This parameter can be any combination of the following values:
  981. * @arg ADC_INT_AWD : Enable Analog watchdog interrupt
  982. * @arg ADC_INT_EOC : Enable End of conversion interrupt
  983. * @arg ADC_INT_INJEOC : Enable End of injected conversion interrupt
  984. *
  985. * @retval None
  986. *
  987. * @note adc can be ADC1, ADC2 or ADC3.
  988. */
  989. void ADC_ClearIntFlag(ADC_T *adc, uint16_t flag)
  990. {
  991. uint8_t mask = 0;
  992. mask = (uint8_t)(flag >> 8);
  993. adc->STS = ~(uint32_t)mask;
  994. }
  995. /**@} end of group ADC_Fuctions*/
  996. /**@} end of group ADC_Driver*/
  997. /**@} end of group Peripherals_Library*/