n32g45x_adc.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_adc.c
  29. * @author Nations
  30. * @version v1.0.4
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_adc.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup ADC
  40. * @brief ADC driver modules
  41. * @{
  42. */
  43. /** @addtogroup ADC_Private_TypesDefinitions
  44. * @{
  45. */
  46. /**
  47. * @}
  48. */
  49. /** @addtogroup ADC_Private_Defines
  50. * @{
  51. */
  52. /* ADC DISC_NUM mask */
  53. #define CR1_DISCNUM_Reset ((uint32_t)0xFFFF1FFF)
  54. /* ADC DISC_EN mask */
  55. #define CTRL1_DISC_EN_SET ((uint32_t)0x00000800)
  56. #define CTRL1_DISC_EN_RESET ((uint32_t)0xFFFFF7FF)
  57. /* ADC INJ_AUTO mask */
  58. #define CR1_JAUTO_Set ((uint32_t)0x00000400)
  59. #define CR1_JAUTO_Reset ((uint32_t)0xFFFFFBFF)
  60. /* ADC INJ_DISC_EN mask */
  61. #define CTRL1_INJ_DISC_EN_SET ((uint32_t)0x00001000)
  62. #define CTRL1_INJ_DISC_EN_RESET ((uint32_t)0xFFFFEFFF)
  63. /* ADC AWDG_CH mask */
  64. #define CTRL1_AWDG_CH_RESET ((uint32_t)0xFFFFFFE0)
  65. /* ADC Analog watchdog enable mode mask */
  66. #define CTRL1_AWDG_MODE_RESET ((uint32_t)0xFF3FFDFF)
  67. /* CTRL1 register Mask */
  68. #define CTRL1_CLR_MASK ((uint32_t)0xFFF0FEFF)
  69. /* ADC AD_ON mask */
  70. #define CTRL2_AD_ON_SET ((uint32_t)0x00000001)
  71. #define CTRL2_AD_ON_RESET ((uint32_t)0xFFFFFFFE)
  72. /* ADC DMA mask */
  73. #define CTRL2_DMA_SET ((uint32_t)0x00000100)
  74. #define CTRL2_DMA_RESET ((uint32_t)0xFFFFFEFF)
  75. /* ADC CAL mask */
  76. #define CTRL2_CAL_SET ((uint32_t)0x00000004)
  77. /* ADC SOFT_START mask */
  78. #define CTRL2_SOFT_START_SET ((uint32_t)0x00400000)
  79. /* ADC EXT_TRIG mask */
  80. #define CTRL2_EXT_TRIG_SET ((uint32_t)0x00100000)
  81. #define CTRL2_EXT_TRIG_RESET ((uint32_t)0xFFEFFFFF)
  82. /* ADC Software start mask */
  83. #define CTRL2_EXT_TRIG_SWSTART_SET ((uint32_t)0x00500000)
  84. #define CTRL2_EXT_TRIG_SWSTART_RESET ((uint32_t)0xFFAFFFFF)
  85. /* ADC INJ_EXT_SEL mask */
  86. #define CTRL2_INJ_EXT_SEL_RESET ((uint32_t)0xFFFF8FFF)
  87. /* ADC INJ_EXT_TRIG mask */
  88. #define CTRL2_INJ_EXT_TRIG_SET ((uint32_t)0x00008000)
  89. #define CTRL2_INJ_EXT_TRIG_RESET ((uint32_t)0xFFFF7FFF)
  90. /* ADC INJ_SWSTART mask */
  91. #define CTRL2_INJ_SWSTART_SET ((uint32_t)0x00200000)
  92. /* ADC injected software start mask */
  93. #define CTRL2_INJ_EXT_TRIG_JSWSTART_SET ((uint32_t)0x00208000)
  94. #define CTRL2_INJ_EXT_TRIG_JSWSTART_RESET ((uint32_t)0xFFDF7FFF)
  95. /* ADC TSPD mask */
  96. #define CTRL2_TSVREFE_SET ((uint32_t)0x00800000)
  97. #define CTRL2_TSVREFE_RESET ((uint32_t)0xFF7FFFFF)
  98. /* CTRL2 register Mask */
  99. #define CTRL2_CLR_MASK ((uint32_t)0xFFF1F7FD)
  100. /* ADC SQx mask */
  101. #define SQR4_SEQ_SET ((uint32_t)0x0000001F)
  102. #define SQR3_SEQ_SET ((uint32_t)0x0000001F)
  103. #define SQR2_SEQ_SET ((uint32_t)0x0000001F)
  104. #define SQR1_SEQ_SET ((uint32_t)0x0000001F)
  105. /* RSEQ1 register Mask */
  106. #define RSEQ1_CLR_MASK ((uint32_t)0xFF0FFFFF)
  107. /* ADC JSQx mask */
  108. #define JSEQ_JSQ_SET ((uint32_t)0x0000001F)
  109. /* ADC INJ_LEN mask */
  110. #define JSEQ_INJ_LEN_SET ((uint32_t)0x00300000)
  111. #define JSEQ_INJ_LEN_RESET ((uint32_t)0xFFCFFFFF)
  112. /* ADC SAMPTx mask */
  113. #define SAMPT1_SMP_SET ((uint32_t)0x00000007)
  114. #define SAMPT2_SMP_SET ((uint32_t)0x00000007)
  115. /* ADC JDATx registers offset */
  116. #define JDAT_OFFSET ((uint8_t)0x28)
  117. /* ADC1 DAT register base address */
  118. #define DAT_ADDR ((uint32_t)0x4001244C)
  119. /* ADC STS register mask */
  120. #define ADC_STS_RESERVE_MASK ((uint32_t)0x0000007F)
  121. /**
  122. * @}
  123. */
  124. /** @addtogroup ADC_Private_Macros
  125. * @{
  126. */
  127. /**
  128. * @}
  129. */
  130. /** @addtogroup ADC_Private_Variables
  131. * @{
  132. */
  133. /**
  134. * @}
  135. */
  136. /** @addtogroup ADC_Private_FunctionPrototypes
  137. * @{
  138. */
  139. /**
  140. * @}
  141. */
  142. /** @addtogroup ADC_Private_Functions
  143. * @{
  144. */
  145. /**
  146. * @brief Deinitializes the ADCx peripheral registers to their default reset values.
  147. * @param ADCx where x can be 1, 2 ,3 or 4 to select the ADC peripheral.
  148. */
  149. void ADC_DeInit(ADC_Module* ADCx)
  150. {
  151. /* Check the parameters */
  152. assert_param(IsAdcModule(ADCx));
  153. if (ADCx == ADC1)
  154. {
  155. /* Enable ADC1 reset state */
  156. RCC_EnableAHBPeriphReset(RCC_AHB_PERIPH_ADC1, ENABLE);
  157. /* Release ADC1 from reset state */
  158. RCC_EnableAHBPeriphReset(RCC_AHB_PERIPH_ADC1, DISABLE);
  159. }
  160. else if (ADCx == ADC2)
  161. {
  162. /* Enable ADC2 reset state */
  163. RCC_EnableAHBPeriphReset(RCC_AHB_PERIPH_ADC2, ENABLE);
  164. /* Release ADC2 from reset state */
  165. RCC_EnableAHBPeriphReset(RCC_AHB_PERIPH_ADC2, DISABLE);
  166. }
  167. else if (ADCx == ADC3)
  168. {
  169. /* Enable ADC2 reset state */
  170. RCC_EnableAHBPeriphReset(RCC_AHB_PERIPH_ADC3, ENABLE);
  171. /* Release ADC2 from reset state */
  172. RCC_EnableAHBPeriphReset(RCC_AHB_PERIPH_ADC3, DISABLE);
  173. }
  174. else
  175. {
  176. if (ADCx == ADC4)
  177. {
  178. /* Enable ADC3 reset state */
  179. RCC_EnableAHBPeriphReset(RCC_AHB_PERIPH_ADC4, ENABLE);
  180. /* Release ADC3 from reset state */
  181. RCC_EnableAHBPeriphReset(RCC_AHB_PERIPH_ADC4, DISABLE);
  182. }
  183. }
  184. }
  185. /**
  186. * @brief Initializes the ADCx peripheral according to the specified parameters
  187. * in the ADC_InitStruct.
  188. * @param ADCx where x can be 1, 2 ,3 or 4 to select the ADC peripheral.
  189. * @param ADC_InitStruct pointer to an ADC_InitType structure that contains
  190. * the configuration information for the specified ADC peripheral.
  191. */
  192. void ADC_Init(ADC_Module* ADCx, ADC_InitType* ADC_InitStruct)
  193. {
  194. uint32_t tmpreg1 = 0;
  195. uint8_t tmpreg2 = 0;
  196. /* Check the parameters */
  197. assert_param(IsAdcModule(ADCx));
  198. assert_param(IsAdcWorkMode(ADC_InitStruct->WorkMode));
  199. assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->MultiChEn));
  200. assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ContinueConvEn));
  201. assert_param(IsAdcExtTrig(ADC_InitStruct->ExtTrigSelect));
  202. assert_param(IsAdcDatAlign(ADC_InitStruct->DatAlign));
  203. assert_param(IsAdcSeqLenValid(ADC_InitStruct->ChsNumber));
  204. /*---------------------------- ADCx CTRL1 Configuration -----------------*/
  205. /* Get the ADCx CTRL1 value */
  206. tmpreg1 = ADCx->CTRL1;
  207. /* Clear DUALMOD and SCAN bits */
  208. tmpreg1 &= CTRL1_CLR_MASK;
  209. /* Configure ADCx: Dual mode and scan conversion mode */
  210. /* Set DUALMOD bits according to WorkMode value */
  211. /* Set SCAN bit according to MultiChEn value */
  212. tmpreg1 |= (uint32_t)(ADC_InitStruct->WorkMode | ((uint32_t)ADC_InitStruct->MultiChEn << 8));
  213. /* Write to ADCx CTRL1 */
  214. ADCx->CTRL1 = tmpreg1;
  215. /*---------------------------- ADCx CTRL2 Configuration -----------------*/
  216. /* Get the ADCx CTRL2 value */
  217. tmpreg1 = ADCx->CTRL2;
  218. /* Clear CONT, ALIGN and EXTSEL bits */
  219. tmpreg1 &= CTRL2_CLR_MASK;
  220. /* Configure ADCx: external trigger event and continuous conversion mode */
  221. /* Set ALIGN bit according to DatAlign value */
  222. /* Set EXTSEL bits according to ExtTrigSelect value */
  223. /* Set CONT bit according to ContinueConvEn value */
  224. tmpreg1 |= (uint32_t)(ADC_InitStruct->DatAlign | ADC_InitStruct->ExtTrigSelect
  225. | ((uint32_t)ADC_InitStruct->ContinueConvEn << 1));
  226. /* Write to ADCx CTRL2 */
  227. ADCx->CTRL2 = tmpreg1;
  228. /*---------------------------- ADCx RSEQ1 Configuration -----------------*/
  229. /* Get the ADCx RSEQ1 value */
  230. tmpreg1 = ADCx->RSEQ1;
  231. /* Clear L bits */
  232. tmpreg1 &= RSEQ1_CLR_MASK;
  233. /* Configure ADCx: regular channel sequence length */
  234. /* Set L bits according to ChsNumber value */
  235. tmpreg2 |= (uint8_t)(ADC_InitStruct->ChsNumber - (uint8_t)1);
  236. tmpreg1 |= (uint32_t)tmpreg2 << 20;
  237. /* Write to ADCx RSEQ1 */
  238. ADCx->RSEQ1 = tmpreg1;
  239. }
  240. /**
  241. * @brief Fills each ADC_InitStruct member with its default value.
  242. * @param ADC_InitStruct pointer to an ADC_InitType structure which will be initialized.
  243. */
  244. void ADC_InitStruct(ADC_InitType* ADC_InitStruct)
  245. {
  246. /* Reset ADC init structure parameters values */
  247. /* Initialize the WorkMode member */
  248. ADC_InitStruct->WorkMode = ADC_WORKMODE_INDEPENDENT;
  249. /* initialize the MultiChEn member */
  250. ADC_InitStruct->MultiChEn = DISABLE;
  251. /* Initialize the ContinueConvEn member */
  252. ADC_InitStruct->ContinueConvEn = DISABLE;
  253. /* Initialize the ExtTrigSelect member */
  254. ADC_InitStruct->ExtTrigSelect = ADC_EXT_TRIGCONV_T1_CC1;
  255. /* Initialize the DatAlign member */
  256. ADC_InitStruct->DatAlign = ADC_DAT_ALIGN_R;
  257. /* Initialize the ChsNumber member */
  258. ADC_InitStruct->ChsNumber = 1;
  259. }
  260. /**
  261. * @brief Enables or disables the specified ADC peripheral.
  262. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  263. * @param Cmd new state of the ADCx peripheral.
  264. * This parameter can be: ENABLE or DISABLE.
  265. */
  266. void ADC_Enable(ADC_Module* ADCx, FunctionalState Cmd)
  267. {
  268. /* Check the parameters */
  269. assert_param(IsAdcModule(ADCx));
  270. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  271. if (Cmd != DISABLE)
  272. {
  273. /* Set the AD_ON bit to wake up the ADC from power down mode */
  274. ADCx->CTRL2 |= CTRL2_AD_ON_SET;
  275. }
  276. else
  277. {
  278. /* Disable the selected ADC peripheral */
  279. ADCx->CTRL2 &= CTRL2_AD_ON_RESET;
  280. }
  281. }
  282. /**
  283. * @brief Enables or disables the specified ADC DMA request.
  284. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  285. * @param Cmd new state of the selected ADC DMA transfer.
  286. * This parameter can be: ENABLE or DISABLE.
  287. */
  288. void ADC_EnableDMA(ADC_Module* ADCx, FunctionalState Cmd)
  289. {
  290. /* Check the parameters */
  291. assert_param(IsAdcDmaModule(ADCx));
  292. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  293. if (Cmd != DISABLE)
  294. {
  295. /* Enable the selected ADC DMA request */
  296. ADCx->CTRL2 |= CTRL2_DMA_SET;
  297. }
  298. else
  299. {
  300. /* Disable the selected ADC DMA request */
  301. ADCx->CTRL2 &= CTRL2_DMA_RESET;
  302. }
  303. }
  304. /**
  305. * @brief Enables or disables the specified ADC interrupts.
  306. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  307. * @param ADC_IT specifies the ADC interrupt sources to be enabled or disabled.
  308. * This parameter can be any combination of the following values:
  309. * @arg ADC_INT_ENDC End of conversion interrupt mask
  310. * @arg ADC_INT_AWD Analog watchdog interrupt mask
  311. * @arg ADC_INT_JENDC End of injected conversion interrupt mask
  312. * @param Cmd new state of the specified ADC interrupts.
  313. * This parameter can be: ENABLE or DISABLE.
  314. */
  315. void ADC_ConfigInt(ADC_Module* ADCx, uint16_t ADC_IT, FunctionalState Cmd)
  316. {
  317. uint8_t itmask = 0;
  318. /* Check the parameters */
  319. assert_param(IsAdcModule(ADCx));
  320. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  321. assert_param(IsAdcInt(ADC_IT));
  322. /* Get the ADC IT index */
  323. itmask = (uint8_t)ADC_IT;
  324. if (Cmd != DISABLE)
  325. {
  326. /* Enable the selected ADC interrupts */
  327. ADCx->CTRL1 |= itmask;
  328. }
  329. else
  330. {
  331. /* Disable the selected ADC interrupts */
  332. ADCx->CTRL1 &= (~(uint32_t)itmask);
  333. }
  334. }
  335. /**
  336. * @brief Starts the selected ADC calibration process.
  337. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  338. */
  339. void ADC_StartCalibration(ADC_Module* ADCx)
  340. {
  341. /* Check the parameters */
  342. assert_param(IsAdcModule(ADCx));
  343. /* Enable the selected ADC calibration process */
  344. if(ADCx->CALFACT==0)
  345. ADCx->CTRL2 |= CTRL2_CAL_SET;
  346. }
  347. /**
  348. * @brief Gets the selected ADC calibration status.
  349. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  350. * @return The new state of ADC calibration (SET or RESET).
  351. */
  352. FlagStatus ADC_GetCalibrationStatus(ADC_Module* ADCx)
  353. {
  354. FlagStatus bitstatus = RESET;
  355. /* Check the parameters */
  356. assert_param(IsAdcModule(ADCx));
  357. /* Check the status of CAL bit */
  358. if ((ADCx->CTRL2 & CTRL2_CAL_SET) != (uint32_t)RESET)
  359. {
  360. /* CAL bit is set: calibration on going */
  361. bitstatus = SET;
  362. }
  363. else
  364. {
  365. /* CAL bit is reset: end of calibration */
  366. bitstatus = RESET;
  367. }
  368. if(ADCx->CALFACT!=0)
  369. bitstatus = RESET;
  370. /* Return the CAL bit status */
  371. return bitstatus;
  372. }
  373. /**
  374. * @brief Enables or disables the selected ADC software start conversion .
  375. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  376. * @param Cmd new state of the selected ADC software start conversion.
  377. * This parameter can be: ENABLE or DISABLE.
  378. */
  379. void ADC_EnableSoftwareStartConv(ADC_Module* ADCx, FunctionalState Cmd)
  380. {
  381. /* Check the parameters */
  382. assert_param(IsAdcModule(ADCx));
  383. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  384. if (Cmd != DISABLE)
  385. {
  386. /* Enable the selected ADC conversion on external event and start the selected
  387. ADC conversion */
  388. ADCx->CTRL2 |= CTRL2_EXT_TRIG_SWSTART_SET;
  389. }
  390. else
  391. {
  392. /* Disable the selected ADC conversion on external event and stop the selected
  393. ADC conversion */
  394. ADCx->CTRL2 &= CTRL2_EXT_TRIG_SWSTART_RESET;
  395. }
  396. }
  397. /**
  398. * @brief Gets the selected ADC Software start conversion Status.
  399. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  400. * @return The new state of ADC software start conversion (SET or RESET).
  401. */
  402. FlagStatus ADC_GetSoftwareStartConvStatus(ADC_Module* ADCx)
  403. {
  404. FlagStatus bitstatus = RESET;
  405. /* Check the parameters */
  406. assert_param(IsAdcModule(ADCx));
  407. /* Check the status of SOFT_START bit */
  408. if ((ADCx->CTRL2 & CTRL2_SOFT_START_SET) != (uint32_t)RESET)
  409. {
  410. /* SOFT_START bit is set */
  411. bitstatus = SET;
  412. }
  413. else
  414. {
  415. /* SOFT_START bit is reset */
  416. bitstatus = RESET;
  417. }
  418. /* Return the SOFT_START bit status */
  419. return bitstatus;
  420. }
  421. /**
  422. * @brief Configures the discontinuous mode for the selected ADC regular
  423. * group channel.
  424. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  425. * @param Number specifies the discontinuous mode regular channel
  426. * count value. This number must be between 1 and 8.
  427. */
  428. void ADC_ConfigDiscModeChannelCount(ADC_Module* ADCx, uint8_t Number)
  429. {
  430. uint32_t tmpreg1 = 0;
  431. uint32_t tmpreg2 = 0;
  432. /* Check the parameters */
  433. assert_param(IsAdcModule(ADCx));
  434. assert_param(IsAdcSeqDiscNumberValid(Number));
  435. /* Get the old register value */
  436. tmpreg1 = ADCx->CTRL1;
  437. /* Clear the old discontinuous mode channel count */
  438. tmpreg1 &= CR1_DISCNUM_Reset;
  439. /* Set the discontinuous mode channel count */
  440. tmpreg2 = Number - 1;
  441. tmpreg1 |= tmpreg2 << 13;
  442. /* Store the new register value */
  443. ADCx->CTRL1 = tmpreg1;
  444. }
  445. /**
  446. * @brief Enables or disables the discontinuous mode on regular group
  447. * channel for the specified ADC
  448. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  449. * @param Cmd new state of the selected ADC discontinuous mode
  450. * on regular group channel.
  451. * This parameter can be: ENABLE or DISABLE.
  452. */
  453. void ADC_EnableDiscMode(ADC_Module* ADCx, FunctionalState Cmd)
  454. {
  455. /* Check the parameters */
  456. assert_param(IsAdcModule(ADCx));
  457. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  458. if (Cmd != DISABLE)
  459. {
  460. /* Enable the selected ADC regular discontinuous mode */
  461. ADCx->CTRL1 |= CTRL1_DISC_EN_SET;
  462. }
  463. else
  464. {
  465. /* Disable the selected ADC regular discontinuous mode */
  466. ADCx->CTRL1 &= CTRL1_DISC_EN_RESET;
  467. }
  468. }
  469. /**
  470. * @brief Configures for the selected ADC regular channel its corresponding
  471. * rank in the sequencer and its sample time.
  472. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  473. * @param ADC_Channel the ADC channel to configure.
  474. * This parameter can be one of the following values:
  475. * @arg ADC_CH_0 ADC Channel0 selected
  476. * @arg ADC_CH_1 ADC Channel1 selected
  477. * @arg ADC_CH_2 ADC Channel2 selected
  478. * @arg ADC_CH_3 ADC Channel3 selected
  479. * @arg ADC_CH_4 ADC Channel4 selected
  480. * @arg ADC_CH_5 ADC Channel5 selected
  481. * @arg ADC_CH_6 ADC Channel6 selected
  482. * @arg ADC_CH_7 ADC Channel7 selected
  483. * @arg ADC_CH_8 ADC Channel8 selected
  484. * @arg ADC_CH_9 ADC Channel9 selected
  485. * @arg ADC_CH_10 ADC Channel10 selected
  486. * @arg ADC_CH_11 ADC Channel11 selected
  487. * @arg ADC_CH_12 ADC Channel12 selected
  488. * @arg ADC_CH_13 ADC Channel13 selected
  489. * @arg ADC_CH_14 ADC Channel14 selected
  490. * @arg ADC_CH_15 ADC Channel15 selected
  491. * @arg ADC_CH_16 ADC Channel16 selected
  492. * @arg ADC_CH_17 ADC Channel17 selected
  493. * @arg ADC_CH_18 ADC Channel18 selected
  494. * @param Rank The rank in the regular group sequencer. This parameter must be between 1 to 16.
  495. * @param ADC_SampleTime The sample time value to be set for the selected channel.
  496. * This parameter can be one of the following values:
  497. * @arg ADC_SAMP_TIME_1CYCLES5 Sample time equal to 1.5 cycles
  498. * @arg ADC_SAMP_TIME_7CYCLES5 Sample time equal to 7.5 cycles
  499. * @arg ADC_SAMP_TIME_13CYCLES5 Sample time equal to 13.5 cycles
  500. * @arg ADC_SAMP_TIME_28CYCLES5 Sample time equal to 28.5 cycles
  501. * @arg ADC_SAMP_TIME_41CYCLES5 Sample time equal to 41.5 cycles
  502. * @arg ADC_SAMP_TIME_55CYCLES5 Sample time equal to 55.5 cycles
  503. * @arg ADC_SAMP_TIME_71CYCLES5 Sample time equal to 71.5 cycles
  504. * @arg ADC_SAMP_TIME_239CYCLES5 Sample time equal to 239.5 cycles
  505. */
  506. void ADC_ConfigRegularChannel(ADC_Module* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
  507. {
  508. uint32_t tmpreg1 = 0, tmpreg2 = 0;
  509. /* Check the parameters */
  510. assert_param(IsAdcModule(ADCx));
  511. assert_param(IsAdcChannel(ADC_Channel));
  512. assert_param(IsAdcReqRankValid(Rank));
  513. assert_param(IsAdcSampleTime(ADC_SampleTime));
  514. if (ADC_Channel == ADC_CH_18)
  515. {
  516. tmpreg1 = ADCx->SAMPT3;
  517. tmpreg1 &= (~0x00000007);
  518. tmpreg1 |= ADC_SampleTime;
  519. ADCx->SAMPT3 = tmpreg1;
  520. }
  521. else if (ADC_Channel > ADC_CH_9) /* if ADC_CH_10 ... ADC_CH_17 is selected */
  522. {
  523. /* Get the old register value */
  524. tmpreg1 = ADCx->SAMPT1;
  525. /* Calculate the mask to clear */
  526. tmpreg2 = SAMPT1_SMP_SET << (3 * (ADC_Channel - 10));
  527. /* Clear the old channel sample time */
  528. tmpreg1 &= ~tmpreg2;
  529. /* Calculate the mask to set */
  530. tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));
  531. /* Set the new channel sample time */
  532. tmpreg1 |= tmpreg2;
  533. /* Store the new register value */
  534. ADCx->SAMPT1 = tmpreg1;
  535. }
  536. else /* ADC_Channel include in ADC_Channel_[0..9] */
  537. {
  538. /* Get the old register value */
  539. tmpreg1 = ADCx->SAMPT2;
  540. /* Calculate the mask to clear */
  541. tmpreg2 = SAMPT2_SMP_SET << (3 * ADC_Channel);
  542. /* Clear the old channel sample time */
  543. tmpreg1 &= ~tmpreg2;
  544. /* Calculate the mask to set */
  545. tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
  546. /* Set the new channel sample time */
  547. tmpreg1 |= tmpreg2;
  548. /* Store the new register value */
  549. ADCx->SAMPT2 = tmpreg1;
  550. }
  551. /* For Rank 1 to 6 */
  552. if (Rank < 7)
  553. {
  554. /* Get the old register value */
  555. tmpreg1 = ADCx->RSEQ3;
  556. /* Calculate the mask to clear */
  557. tmpreg2 = SQR3_SEQ_SET << (5 * (Rank - 1));
  558. /* Clear the old SQx bits for the selected rank */
  559. tmpreg1 &= ~tmpreg2;
  560. /* Calculate the mask to set */
  561. tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));
  562. /* Set the SQx bits for the selected rank */
  563. tmpreg1 |= tmpreg2;
  564. /* Store the new register value */
  565. ADCx->RSEQ3 = tmpreg1;
  566. }
  567. /* For Rank 7 to 12 */
  568. else if (Rank < 13)
  569. {
  570. /* Get the old register value */
  571. tmpreg1 = ADCx->RSEQ2;
  572. /* Calculate the mask to clear */
  573. tmpreg2 = SQR2_SEQ_SET << (5 * (Rank - 7));
  574. /* Clear the old SQx bits for the selected rank */
  575. tmpreg1 &= ~tmpreg2;
  576. /* Calculate the mask to set */
  577. tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));
  578. /* Set the SQx bits for the selected rank */
  579. tmpreg1 |= tmpreg2;
  580. /* Store the new register value */
  581. ADCx->RSEQ2 = tmpreg1;
  582. }
  583. /* For Rank 13 to 16 */
  584. else
  585. {
  586. /* Get the old register value */
  587. tmpreg1 = ADCx->RSEQ1;
  588. /* Calculate the mask to clear */
  589. tmpreg2 = SQR1_SEQ_SET << (5 * (Rank - 13));
  590. /* Clear the old SQx bits for the selected rank */
  591. tmpreg1 &= ~tmpreg2;
  592. /* Calculate the mask to set */
  593. tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));
  594. /* Set the SQx bits for the selected rank */
  595. tmpreg1 |= tmpreg2;
  596. /* Store the new register value */
  597. ADCx->RSEQ1 = tmpreg1;
  598. }
  599. }
  600. /**
  601. * @brief Enables or disables the ADCx conversion through external trigger.
  602. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  603. * @param Cmd new state of the selected ADC external trigger start of conversion.
  604. * This parameter can be: ENABLE or DISABLE.
  605. */
  606. void ADC_EnableExternalTrigConv(ADC_Module* ADCx, FunctionalState Cmd)
  607. {
  608. /* Check the parameters */
  609. assert_param(IsAdcModule(ADCx));
  610. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  611. if (Cmd != DISABLE)
  612. {
  613. /* Enable the selected ADC conversion on external event */
  614. ADCx->CTRL2 |= CTRL2_EXT_TRIG_SET;
  615. }
  616. else
  617. {
  618. /* Disable the selected ADC conversion on external event */
  619. ADCx->CTRL2 &= CTRL2_EXT_TRIG_RESET;
  620. }
  621. }
  622. /**
  623. * @brief Returns the last ADCx conversion result data for regular channel.
  624. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  625. * @return The Data conversion value.
  626. */
  627. uint16_t ADC_GetDat(ADC_Module* ADCx)
  628. {
  629. /* Check the parameters */
  630. assert_param(IsAdcModule(ADCx));
  631. /* Return the selected ADC conversion value */
  632. return (uint16_t)ADCx->DAT;
  633. }
  634. /**
  635. * @brief Returns the last ADC1 and ADC2 OR last ADC3 and ADC4 conversion result data in dual mode.
  636. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  637. * @return The Data conversion value.
  638. */
  639. uint32_t ADC_GetDualModeConversionDat(ADC_Module* ADCx)
  640. {
  641. /* Check the parameters */
  642. assert_param(IsAdcModule(ADCx));
  643. /* Return the dual mode conversion value */
  644. if((ADCx==ADC1) | (ADCx==ADC2))
  645. return (uint32_t)ADC1->DAT;
  646. else
  647. return (uint32_t)ADC3->DAT;
  648. }
  649. /**
  650. * @brief Enables or disables the selected ADC automatic injected group
  651. * conversion after regular one.
  652. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  653. * @param Cmd new state of the selected ADC auto injected conversion
  654. * This parameter can be: ENABLE or DISABLE.
  655. */
  656. void ADC_EnableAutoInjectedConv(ADC_Module* ADCx, FunctionalState Cmd)
  657. {
  658. /* Check the parameters */
  659. assert_param(IsAdcModule(ADCx));
  660. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  661. if (Cmd != DISABLE)
  662. {
  663. /* Enable the selected ADC automatic injected group conversion */
  664. ADCx->CTRL1 |= CR1_JAUTO_Set;
  665. }
  666. else
  667. {
  668. /* Disable the selected ADC automatic injected group conversion */
  669. ADCx->CTRL1 &= CR1_JAUTO_Reset;
  670. }
  671. }
  672. /**
  673. * @brief Enables or disables the discontinuous mode for injected group
  674. * channel for the specified ADC
  675. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  676. * @param Cmd new state of the selected ADC discontinuous mode
  677. * on injected group channel.
  678. * This parameter can be: ENABLE or DISABLE.
  679. */
  680. void ADC_EnableInjectedDiscMode(ADC_Module* ADCx, FunctionalState Cmd)
  681. {
  682. /* Check the parameters */
  683. assert_param(IsAdcModule(ADCx));
  684. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  685. if (Cmd != DISABLE)
  686. {
  687. /* Enable the selected ADC injected discontinuous mode */
  688. ADCx->CTRL1 |= CTRL1_INJ_DISC_EN_SET;
  689. }
  690. else
  691. {
  692. /* Disable the selected ADC injected discontinuous mode */
  693. ADCx->CTRL1 &= CTRL1_INJ_DISC_EN_RESET;
  694. }
  695. }
  696. /**
  697. * @brief Configures the ADCx external trigger for injected channels conversion.
  698. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  699. * @param ADC_ExternalTrigInjecConv specifies the ADC trigger to start injected conversion.
  700. * This parameter can be one of the following values:
  701. * @arg ADC_EXT_TRIG_INJ_CONV_T1_TRGO Timer1 TRGO event selected (for ADC1, ADC2 and ADC3)
  702. * @arg ADC_EXT_TRIG_INJ_CONV_T1_CC4 Timer1 capture compare4 selected (for ADC1, ADC2 and ADC3)
  703. * @arg ADC_EXT_TRIG_INJ_CONV_T2_TRGO Timer2 TRGO event selected (for ADC1 and ADC2)
  704. * @arg ADC_EXT_TRIG_INJ_CONV_T2_CC1 Timer2 capture compare1 selected (for ADC1 and ADC2)
  705. * @arg ADC_EXT_TRIG_INJ_CONV_T3_CC4 Timer3 capture compare4 selected (for ADC1 and ADC2)
  706. * @arg ADC_EXT_TRIG_INJ_CONV_T4_TRGO Timer4 TRGO event selected (for ADC1 and ADC2)
  707. * @arg ADC_EXT_TRIG_INJ_CONV_EXT_INT15_TIM8_CC4 External interrupt line 15 or Timer8
  708. * capture compare4 event selected (for ADC1 and ADC2)
  709. * @arg ADC_EXT_TRIG_INJ_CONV_T4_CC3 Timer4 capture compare3 selected (for ADC3 only)
  710. * @arg ADC_EXT_TRIG_INJ_CONV_T8_CC2 Timer8 capture compare2 selected (for ADC3 only)
  711. * @arg ADC_EXT_TRIG_INJ_CONV_T8_CC4 Timer8 capture compare4 selected (for ADC3 only)
  712. * @arg ADC_EXT_TRIG_INJ_CONV_T5_TRGO Timer5 TRGO event selected (for ADC3 only)
  713. * @arg ADC_EXT_TRIG_INJ_CONV_T5_CC4 Timer5 capture compare4 selected (for ADC3 only)
  714. * @arg ADC_EXT_TRIG_INJ_CONV_NONE Injected conversion started by software and not
  715. * by external trigger (for ADC1, ADC2 and ADC3)
  716. */
  717. void ADC_ConfigExternalTrigInjectedConv(ADC_Module* ADCx, uint32_t ADC_ExternalTrigInjecConv)
  718. {
  719. uint32_t tmpregister = 0;
  720. /* Check the parameters */
  721. assert_param(IsAdcModule(ADCx));
  722. assert_param(IsAdcExtInjTrig(ADC_ExternalTrigInjecConv));
  723. /* Get the old register value */
  724. tmpregister = ADCx->CTRL2;
  725. /* Clear the old external event selection for injected group */
  726. tmpregister &= CTRL2_INJ_EXT_SEL_RESET;
  727. /* Set the external event selection for injected group */
  728. tmpregister |= ADC_ExternalTrigInjecConv;
  729. /* Store the new register value */
  730. ADCx->CTRL2 = tmpregister;
  731. }
  732. /**
  733. * @brief Enables or disables the ADCx injected channels conversion through
  734. * external trigger
  735. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  736. * @param Cmd new state of the selected ADC external trigger start of
  737. * injected conversion.
  738. * This parameter can be: ENABLE or DISABLE.
  739. */
  740. void ADC_EnableExternalTrigInjectedConv(ADC_Module* ADCx, FunctionalState Cmd)
  741. {
  742. /* Check the parameters */
  743. assert_param(IsAdcModule(ADCx));
  744. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  745. if (Cmd != DISABLE)
  746. {
  747. /* Enable the selected ADC external event selection for injected group */
  748. ADCx->CTRL2 |= CTRL2_INJ_EXT_TRIG_SET;
  749. }
  750. else
  751. {
  752. /* Disable the selected ADC external event selection for injected group */
  753. ADCx->CTRL2 &= CTRL2_INJ_EXT_TRIG_RESET;
  754. }
  755. }
  756. /**
  757. * @brief Enables or disables the selected ADC start of the injected
  758. * channels conversion.
  759. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  760. * @param Cmd new state of the selected ADC software start injected conversion.
  761. * This parameter can be: ENABLE or DISABLE.
  762. */
  763. void ADC_EnableSoftwareStartInjectedConv(ADC_Module* ADCx, FunctionalState Cmd)
  764. {
  765. /* Check the parameters */
  766. assert_param(IsAdcModule(ADCx));
  767. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  768. if (Cmd != DISABLE)
  769. {
  770. /* Enable the selected ADC conversion for injected group on external event and start the selected
  771. ADC injected conversion */
  772. ADCx->CTRL2 |= CTRL2_INJ_EXT_TRIG_JSWSTART_SET;
  773. }
  774. else
  775. {
  776. /* Disable the selected ADC conversion on external event for injected group and stop the selected
  777. ADC injected conversion */
  778. ADCx->CTRL2 &= CTRL2_INJ_EXT_TRIG_JSWSTART_RESET;
  779. }
  780. }
  781. /**
  782. * @brief Gets the selected ADC Software start injected conversion Status.
  783. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  784. * @return The new state of ADC software start injected conversion (SET or RESET).
  785. */
  786. FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_Module* ADCx)
  787. {
  788. FlagStatus bitstatus = RESET;
  789. /* Check the parameters */
  790. assert_param(IsAdcModule(ADCx));
  791. /* Check the status of INJ_SWSTART bit */
  792. if ((ADCx->CTRL2 & CTRL2_INJ_SWSTART_SET) != (uint32_t)RESET)
  793. {
  794. /* INJ_SWSTART bit is set */
  795. bitstatus = SET;
  796. }
  797. else
  798. {
  799. /* INJ_SWSTART bit is reset */
  800. bitstatus = RESET;
  801. }
  802. /* Return the INJ_SWSTART bit status */
  803. return bitstatus;
  804. }
  805. /**
  806. * @brief Configures for the selected ADC injected channel its corresponding
  807. * rank in the sequencer and its sample time.
  808. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  809. * @param ADC_Channel the ADC channel to configure.
  810. * This parameter can be one of the following values:
  811. * @arg ADC_CH_0 ADC Channel0 selected
  812. * @arg ADC_CH_1 ADC Channel1 selected
  813. * @arg ADC_CH_2 ADC Channel2 selected
  814. * @arg ADC_CH_3 ADC Channel3 selected
  815. * @arg ADC_CH_4 ADC Channel4 selected
  816. * @arg ADC_CH_5 ADC Channel5 selected
  817. * @arg ADC_CH_6 ADC Channel6 selected
  818. * @arg ADC_CH_7 ADC Channel7 selected
  819. * @arg ADC_CH_8 ADC Channel8 selected
  820. * @arg ADC_CH_9 ADC Channel9 selected
  821. * @arg ADC_CH_10 ADC Channel10 selected
  822. * @arg ADC_CH_11 ADC Channel11 selected
  823. * @arg ADC_CH_12 ADC Channel12 selected
  824. * @arg ADC_CH_13 ADC Channel13 selected
  825. * @arg ADC_CH_14 ADC Channel14 selected
  826. * @arg ADC_CH_15 ADC Channel15 selected
  827. * @arg ADC_CH_16 ADC Channel16 selected
  828. * @arg ADC_CH_17 ADC Channel17 selected
  829. * @arg ADC_CH_18 ADC Channel18 selected
  830. * @param Rank The rank in the injected group sequencer. This parameter must be between 1 and 4.
  831. * @param ADC_SampleTime The sample time value to be set for the selected channel.
  832. * This parameter can be one of the following values:
  833. * @arg ADC_SAMP_TIME_1CYCLES5 Sample time equal to 1.5 cycles
  834. * @arg ADC_SAMP_TIME_7CYCLES5 Sample time equal to 7.5 cycles
  835. * @arg ADC_SAMP_TIME_13CYCLES5 Sample time equal to 13.5 cycles
  836. * @arg ADC_SAMP_TIME_28CYCLES5 Sample time equal to 28.5 cycles
  837. * @arg ADC_SAMP_TIME_41CYCLES5 Sample time equal to 41.5 cycles
  838. * @arg ADC_SAMP_TIME_55CYCLES5 Sample time equal to 55.5 cycles
  839. * @arg ADC_SAMP_TIME_71CYCLES5 Sample time equal to 71.5 cycles
  840. * @arg ADC_SAMP_TIME_239CYCLES5 Sample time equal to 239.5 cycles
  841. */
  842. void ADC_ConfigInjectedChannel(ADC_Module* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
  843. {
  844. uint32_t tmpreg1 = 0, tmpreg2 = 0, tmpreg3 = 0;
  845. /* Check the parameters */
  846. assert_param(IsAdcModule(ADCx));
  847. assert_param(IsAdcChannel(ADC_Channel));
  848. assert_param(IsAdcInjRankValid(Rank));
  849. assert_param(IsAdcSampleTime(ADC_SampleTime));
  850. if (ADC_Channel == ADC_CH_18)
  851. {
  852. tmpreg1 = ADCx->SAMPT3;
  853. tmpreg1 &= (~0x00000007);
  854. tmpreg1 |= ADC_SampleTime;
  855. ADCx->SAMPT3 = tmpreg1;
  856. }
  857. else if (ADC_Channel > ADC_CH_9) /* if ADC_CH_10 ... ADC_CH_17 is selected */
  858. {
  859. /* Get the old register value */
  860. tmpreg1 = ADCx->SAMPT1;
  861. /* Calculate the mask to clear */
  862. tmpreg2 = SAMPT1_SMP_SET << (3 * (ADC_Channel - 10));
  863. /* Clear the old channel sample time */
  864. tmpreg1 &= ~tmpreg2;
  865. /* Calculate the mask to set */
  866. tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));
  867. /* Set the new channel sample time */
  868. tmpreg1 |= tmpreg2;
  869. /* Store the new register value */
  870. ADCx->SAMPT1 = tmpreg1;
  871. }
  872. else /* ADC_Channel include in ADC_Channel_[0..9] */
  873. {
  874. /* Get the old register value */
  875. tmpreg1 = ADCx->SAMPT2;
  876. /* Calculate the mask to clear */
  877. tmpreg2 = SAMPT2_SMP_SET << (3 * ADC_Channel);
  878. /* Clear the old channel sample time */
  879. tmpreg1 &= ~tmpreg2;
  880. /* Calculate the mask to set */
  881. tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
  882. /* Set the new channel sample time */
  883. tmpreg1 |= tmpreg2;
  884. /* Store the new register value */
  885. ADCx->SAMPT2 = tmpreg1;
  886. }
  887. /* Rank configuration */
  888. /* Get the old register value */
  889. tmpreg1 = ADCx->JSEQ;
  890. /* Get INJ_LEN value: Number = INJ_LEN+1 */
  891. tmpreg3 = (tmpreg1 & JSEQ_INJ_LEN_SET) >> 20;
  892. /* Calculate the mask to clear: ((Rank-1)+(4-INJ_LEN-1)) */
  893. tmpreg2 = JSEQ_JSQ_SET << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
  894. /* Clear the old JSQx bits for the selected rank */
  895. tmpreg1 &= ~tmpreg2;
  896. /* Calculate the mask to set: ((Rank-1)+(4-INJ_LEN-1)) */
  897. tmpreg2 = (uint32_t)ADC_Channel << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
  898. /* Set the JSQx bits for the selected rank */
  899. tmpreg1 |= tmpreg2;
  900. /* Store the new register value */
  901. ADCx->JSEQ = tmpreg1;
  902. }
  903. /**
  904. * @brief Configures the sequencer length for injected channels
  905. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  906. * @param Length The sequencer length.
  907. * This parameter must be a number between 1 to 4.
  908. */
  909. void ADC_ConfigInjectedSequencerLength(ADC_Module* ADCx, uint8_t Length)
  910. {
  911. uint32_t tmpreg1 = 0;
  912. uint32_t tmpreg2 = 0;
  913. /* Check the parameters */
  914. assert_param(IsAdcModule(ADCx));
  915. assert_param(IsAdcInjLenValid(Length));
  916. /* Get the old register value */
  917. tmpreg1 = ADCx->JSEQ;
  918. /* Clear the old injected sequnence lenght INJ_LEN bits */
  919. tmpreg1 &= JSEQ_INJ_LEN_RESET;
  920. /* Set the injected sequnence lenght INJ_LEN bits */
  921. tmpreg2 = Length - 1;
  922. tmpreg1 |= tmpreg2 << 20;
  923. /* Store the new register value */
  924. ADCx->JSEQ = tmpreg1;
  925. }
  926. /**
  927. * @brief Set the injected channels conversion value offset
  928. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  929. * @param ADC_InjectedChannel the ADC injected channel to set its offset.
  930. * This parameter can be one of the following values:
  931. * @arg ADC_INJ_CH_1 Injected Channel1 selected
  932. * @arg ADC_INJ_CH_2 Injected Channel2 selected
  933. * @arg ADC_INJ_CH_3 Injected Channel3 selected
  934. * @arg ADC_INJ_CH_4 Injected Channel4 selected
  935. * @param Offset the offset value for the selected ADC injected channel
  936. * This parameter must be a 12bit value.
  937. */
  938. void ADC_SetInjectedOffsetDat(ADC_Module* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset)
  939. {
  940. __IO uint32_t tmp = 0;
  941. /* Check the parameters */
  942. assert_param(IsAdcModule(ADCx));
  943. assert_param(IsAdcInjCh(ADC_InjectedChannel));
  944. assert_param(IsAdcOffsetValid(Offset));
  945. tmp = (uint32_t)ADCx;
  946. tmp += ADC_InjectedChannel;
  947. /* Set the selected injected channel data offset */
  948. *(__IO uint32_t*)tmp = (uint32_t)Offset;
  949. }
  950. /**
  951. * @brief Returns the ADC injected channel conversion result
  952. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  953. * @param ADC_InjectedChannel the converted ADC injected channel.
  954. * This parameter can be one of the following values:
  955. * @arg ADC_INJ_CH_1 Injected Channel1 selected
  956. * @arg ADC_INJ_CH_2 Injected Channel2 selected
  957. * @arg ADC_INJ_CH_3 Injected Channel3 selected
  958. * @arg ADC_INJ_CH_4 Injected Channel4 selected
  959. * @return The Data conversion value.
  960. */
  961. uint16_t ADC_GetInjectedConversionDat(ADC_Module* ADCx, uint8_t ADC_InjectedChannel)
  962. {
  963. __IO uint32_t tmp = 0;
  964. /* Check the parameters */
  965. assert_param(IsAdcModule(ADCx));
  966. assert_param(IsAdcInjCh(ADC_InjectedChannel));
  967. tmp = (uint32_t)ADCx;
  968. tmp += ADC_InjectedChannel + JDAT_OFFSET;
  969. /* Returns the selected injected channel conversion data value */
  970. return (uint16_t)(*(__IO uint32_t*)tmp);
  971. }
  972. /**
  973. * @brief Enables or disables the analog watchdog on single/all regular
  974. * or injected channels
  975. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  976. * @param ADC_AnalogWatchdog the ADC analog watchdog configuration.
  977. * This parameter can be one of the following values:
  978. * @arg ADC_ANALOG_WTDG_SINGLEREG_ENABLE Analog watchdog on a single regular channel
  979. * @arg ADC_ANALOG_WTDG_SINGLEINJEC_ENABLE Analog watchdog on a single injected channel
  980. * @arg ADC_ANALOG_WTDG_SINGLEREG_OR_INJEC_ENABLE Analog watchdog on a single regular or injected channel
  981. * @arg ADC_ANALOG_WTDG_ALLREG_ENABLE Analog watchdog on all regular channel
  982. * @arg ADC_ANALOG_WTDG_ALLINJEC_ENABLE Analog watchdog on all injected channel
  983. * @arg ADC_ANALOG_WTDG_ALLREG_ALLINJEC_ENABLE Analog watchdog on all regular and injected channels
  984. * @arg ADC_ANALOG_WTDG_NONE No channel guarded by the analog watchdog
  985. */
  986. void ADC_ConfigAnalogWatchdogWorkChannelType(ADC_Module* ADCx, uint32_t ADC_AnalogWatchdog)
  987. {
  988. uint32_t tmpregister = 0;
  989. /* Check the parameters */
  990. assert_param(IsAdcModule(ADCx));
  991. assert_param(IsAdcAnalogWatchdog(ADC_AnalogWatchdog));
  992. /* Get the old register value */
  993. tmpregister = ADCx->CTRL1;
  994. /* Clear AWDEN, AWDENJ and AWDSGL bits */
  995. tmpregister &= CTRL1_AWDG_MODE_RESET;
  996. /* Set the analog watchdog enable mode */
  997. tmpregister |= ADC_AnalogWatchdog;
  998. /* Store the new register value */
  999. ADCx->CTRL1 = tmpregister;
  1000. }
  1001. /**
  1002. * @brief Configures the high and low thresholds of the analog watchdog.
  1003. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  1004. * @param HighThreshold the ADC analog watchdog High threshold value.
  1005. * This parameter must be a 12bit value.
  1006. * @param LowThreshold the ADC analog watchdog Low threshold value.
  1007. * This parameter must be a 12bit value.
  1008. */
  1009. void ADC_ConfigAnalogWatchdogThresholds(ADC_Module* ADCx, uint16_t HighThreshold, uint16_t LowThreshold)
  1010. {
  1011. /* Check the parameters */
  1012. assert_param(IsAdcModule(ADCx));
  1013. assert_param(IsAdcValid(HighThreshold));
  1014. assert_param(IsAdcValid(LowThreshold));
  1015. /* Set the ADCx high threshold */
  1016. ADCx->WDGHIGH = HighThreshold;
  1017. /* Set the ADCx low threshold */
  1018. ADCx->WDGLOW = LowThreshold;
  1019. }
  1020. /**
  1021. * @brief Configures the analog watchdog guarded single channel
  1022. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  1023. * @param ADC_Channel the ADC channel to configure for the analog watchdog.
  1024. * This parameter can be one of the following values:
  1025. * @arg ADC_CH_0 ADC Channel0 selected
  1026. * @arg ADC_CH_1 ADC Channel1 selected
  1027. * @arg ADC_CH_2 ADC Channel2 selected
  1028. * @arg ADC_CH_3 ADC Channel3 selected
  1029. * @arg ADC_CH_4 ADC Channel4 selected
  1030. * @arg ADC_CH_5 ADC Channel5 selected
  1031. * @arg ADC_CH_6 ADC Channel6 selected
  1032. * @arg ADC_CH_7 ADC Channel7 selected
  1033. * @arg ADC_CH_8 ADC Channel8 selected
  1034. * @arg ADC_CH_9 ADC Channel9 selected
  1035. * @arg ADC_CH_10 ADC Channel10 selected
  1036. * @arg ADC_CH_11 ADC Channel11 selected
  1037. * @arg ADC_CH_12 ADC Channel12 selected
  1038. * @arg ADC_CH_13 ADC Channel13 selected
  1039. * @arg ADC_CH_14 ADC Channel14 selected
  1040. * @arg ADC_CH_15 ADC Channel15 selected
  1041. * @arg ADC_CH_16 ADC Channel16 selected
  1042. * @arg ADC_CH_17 ADC Channel17 selected
  1043. * @arg ADC_CH_18 ADC Channel18 selected
  1044. */
  1045. void ADC_ConfigAnalogWatchdogSingleChannel(ADC_Module* ADCx, uint8_t ADC_Channel)
  1046. {
  1047. uint32_t tmpregister = 0;
  1048. /* Check the parameters */
  1049. assert_param(IsAdcModule(ADCx));
  1050. assert_param(IsAdcChannel(ADC_Channel));
  1051. /* Get the old register value */
  1052. tmpregister = ADCx->CTRL1;
  1053. /* Clear the Analog watchdog channel select bits */
  1054. tmpregister &= CTRL1_AWDG_CH_RESET;
  1055. /* Set the Analog watchdog channel */
  1056. tmpregister |= ADC_Channel;
  1057. /* Store the new register value */
  1058. ADCx->CTRL1 = tmpregister;
  1059. }
  1060. /**
  1061. * @brief Enables or disables the temperature sensor and Vrefint channel.
  1062. * @param Cmd new state of the temperature sensor.
  1063. * This parameter can be: ENABLE or DISABLE.
  1064. */
  1065. void ADC_EnableTempSensorVrefint(FunctionalState Cmd)
  1066. {
  1067. /* Check the parameters */
  1068. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1069. if (Cmd != DISABLE)
  1070. {
  1071. /* Enable the temperature sensor and Vrefint channel*/
  1072. ADC1->CTRL2 |= CTRL2_TSVREFE_SET;
  1073. _EnVref1p2()
  1074. }
  1075. else
  1076. {
  1077. /* Disable the temperature sensor and Vrefint channel*/
  1078. ADC1->CTRL2 &= CTRL2_TSVREFE_RESET;
  1079. _DisVref1p2()
  1080. }
  1081. }
  1082. /**
  1083. * @brief Checks whether the specified ADC flag is set or not.
  1084. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  1085. * @param ADC_FLAG specifies the flag to check.
  1086. * This parameter can be one of the following values:
  1087. * @arg ADC_FLAG_AWDG Analog watchdog flag
  1088. * @arg ADC_FLAG_ENDC End of conversion flag
  1089. * @arg ADC_FLAG_JENDC End of injected group conversion flag
  1090. * @arg ADC_FLAG_JSTR Start of injected group conversion flag
  1091. * @arg ADC_FLAG_STR Start of regular group conversion flag
  1092. * @return The new state of ADC_FLAG (SET or RESET).
  1093. */
  1094. FlagStatus ADC_GetFlagStatus(ADC_Module* ADCx, uint8_t ADC_FLAG)
  1095. {
  1096. FlagStatus bitstatus = RESET;
  1097. /* Check the parameters */
  1098. assert_param(IsAdcModule(ADCx));
  1099. assert_param(IsAdcGetFlag(ADC_FLAG));
  1100. /* Check the status of the specified ADC flag */
  1101. if ((ADCx->STS & ADC_FLAG) != (uint8_t)RESET)
  1102. {
  1103. /* ADC_FLAG is set */
  1104. bitstatus = SET;
  1105. }
  1106. else
  1107. {
  1108. /* ADC_FLAG is reset */
  1109. bitstatus = RESET;
  1110. }
  1111. /* Return the ADC_FLAG status */
  1112. return bitstatus;
  1113. }
  1114. /**
  1115. * @brief Clears the ADCx's pending flags.
  1116. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  1117. * @param ADC_FLAG specifies the flag to clear.
  1118. * This parameter can be any combination of the following values:
  1119. * @arg ADC_FLAG_AWDG Analog watchdog flag
  1120. * @arg ADC_FLAG_ENDC End of conversion flag
  1121. * @arg ADC_FLAG_JENDC End of injected group conversion flag
  1122. * @arg ADC_FLAG_JSTR Start of injected group conversion flag
  1123. * @arg ADC_FLAG_STR Start of regular group conversion flag
  1124. */
  1125. void ADC_ClearFlag(ADC_Module* ADCx, uint8_t ADC_FLAG)
  1126. {
  1127. /* Check the parameters */
  1128. assert_param(IsAdcModule(ADCx));
  1129. assert_param(IsAdcClrFlag(ADC_FLAG));
  1130. /* Clear the selected ADC flags */
  1131. ADCx->STS = (~(uint32_t)ADC_FLAG & ADC_STS_RESERVE_MASK);
  1132. }
  1133. /**
  1134. * @brief Checks whether the specified ADC interrupt has occurred or not.
  1135. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  1136. * @param ADC_IT specifies the ADC interrupt source to check.
  1137. * This parameter can be one of the following values:
  1138. * @arg ADC_INT_ENDC End of conversion interrupt mask
  1139. * @arg ADC_INT_AWD Analog watchdog interrupt mask
  1140. * @arg ADC_INT_JENDC End of injected conversion interrupt mask
  1141. * @return The new state of ADC_IT (SET or RESET).
  1142. */
  1143. INTStatus ADC_GetIntStatus(ADC_Module* ADCx, uint16_t ADC_IT)
  1144. {
  1145. INTStatus bitstatus = RESET;
  1146. uint32_t itmask = 0, enablestatus = 0;
  1147. /* Check the parameters */
  1148. assert_param(IsAdcModule(ADCx));
  1149. assert_param(IsAdcGetInt(ADC_IT));
  1150. /* Get the ADC IT index */
  1151. itmask = ADC_IT >> 8;
  1152. /* Get the ADC_IT enable bit status */
  1153. enablestatus = (ADCx->CTRL1 & (uint8_t)ADC_IT);
  1154. /* Check the status of the specified ADC interrupt */
  1155. if (((ADCx->STS & itmask) != (uint32_t)RESET) && enablestatus)
  1156. {
  1157. /* ADC_IT is set */
  1158. bitstatus = SET;
  1159. }
  1160. else
  1161. {
  1162. /* ADC_IT is reset */
  1163. bitstatus = RESET;
  1164. }
  1165. /* Return the ADC_IT status */
  1166. return bitstatus;
  1167. }
  1168. /**
  1169. * @brief Clears the ADCx's interrupt pending bits.
  1170. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  1171. * @param ADC_IT specifies the ADC interrupt pending bit to clear.
  1172. * This parameter can be any combination of the following values:
  1173. * @arg ADC_INT_ENDC End of conversion interrupt mask
  1174. * @arg ADC_INT_AWD Analog watchdog interrupt mask
  1175. * @arg ADC_INT_JENDC End of injected conversion interrupt mask
  1176. */
  1177. void ADC_ClearIntPendingBit(ADC_Module* ADCx, uint16_t ADC_IT)
  1178. {
  1179. uint8_t itmask = 0;
  1180. /* Check the parameters */
  1181. assert_param(IsAdcModule(ADCx));
  1182. assert_param(IsAdcInt(ADC_IT));
  1183. /* Get the ADC IT index */
  1184. itmask = (uint8_t)(ADC_IT >> 8);
  1185. /* Clear the selected ADC interrupt pending bits */
  1186. ADCx->STS = (~(uint32_t)itmask & ADC_STS_RESERVE_MASK);
  1187. }
  1188. /**
  1189. * @brief Initializes the ADCx peripheral according to the specified parameters
  1190. * in the ADC_InitStructEx.
  1191. * @param ADCx where x can be 1, 2 ,3 or 4 to select the ADC peripheral.
  1192. * @param ADC_InitStructEx pointer to an ADC_InitTypeEx structure that contains
  1193. * the configuration information for the specified ADC peripheral.
  1194. */
  1195. void ADC_InitEx(ADC_Module* ADCx, ADC_InitTypeEx* ADC_InitStructEx)
  1196. {
  1197. uint32_t tmpregister = 0;
  1198. /*ADC_SAMPT3 samp time sele ,as sam 103 or 303 style*/
  1199. if (ADC_InitStructEx->SampSecondStyle)
  1200. ADCx->SAMPT3 |= ADC_SAMPT3_SAMPSEL_MSK;
  1201. else
  1202. ADCx->SAMPT3 &= (~ADC_SAMPT3_SAMPSEL_MSK);
  1203. /*intial ADC_CTRL3 once initiall config*/
  1204. tmpregister = ADCx->CTRL3;
  1205. if (ADC_InitStructEx->VbatMinitEn)
  1206. {
  1207. tmpregister |= ADC_CTRL3_VABTMEN_MSK;
  1208. _EnVref1p2()
  1209. }
  1210. else
  1211. {
  1212. tmpregister &= (~ADC_CTRL3_VABTMEN_MSK);
  1213. _DisVref1p2()
  1214. }
  1215. if (ADC_InitStructEx->DeepPowerModEn)
  1216. tmpregister |= ADC_CTRL3_DPWMOD_MSK;
  1217. else
  1218. tmpregister &= (~ADC_CTRL3_DPWMOD_MSK);
  1219. if (ADC_InitStructEx->JendcIntEn)
  1220. tmpregister |= ADC_CTRL3_JENDCAIEN_MSK;
  1221. else
  1222. tmpregister &= (~ADC_CTRL3_JENDCAIEN_MSK);
  1223. if (ADC_InitStructEx->EndcIntEn)
  1224. tmpregister |= ADC_CTRL3_ENDCAIEN_MSK;
  1225. else
  1226. tmpregister &= (~ADC_CTRL3_ENDCAIEN_MSK);
  1227. if (ADC_InitStructEx->CalAtuoLoadEn)
  1228. tmpregister |= ADC_CTRL3_CALALD_MSK;
  1229. else
  1230. tmpregister &= (~ADC_CTRL3_CALALD_MSK);
  1231. if (ADC_InitStructEx->DifModCal)
  1232. tmpregister |= ADC_CTRL3_CALDIF_MSK;
  1233. else
  1234. tmpregister &= (~ADC_CTRL3_CALDIF_MSK);
  1235. tmpregister &= (~ADC_CTRL3_RES_MSK);
  1236. tmpregister |= ADC_InitStructEx->ResBit;
  1237. tmpregister &= (~ADC_CTRL3_CKMOD_MSK);
  1238. if(ADC_InitStructEx->ClkMode==ADC_CTRL3_CKMOD_PLL)
  1239. tmpregister |= ADC_CTRL3_CKMOD_MSK;
  1240. ADCx->CTRL3 = tmpregister;
  1241. }
  1242. /**
  1243. * @brief Configure differential channels enable.
  1244. * @param ADCx where x can be 1, 2 ,3 or 4 to select the ADC peripheral.
  1245. * @param DifChs differential channels,see @ADC_dif_sel_ch_definition. eg: ADC_DIFSEL_CHS_3|ADC_DIFSEL_CHS_4
  1246. */
  1247. void ADC_SetDifChs(ADC_Module* ADCx,uint32_t DifChs)
  1248. {
  1249. ADCx->DIFSEL = DifChs;
  1250. }
  1251. /**
  1252. * @brief Checks whether the specified ADC flag is set or not.
  1253. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  1254. * @param ADC_FLAG_NEW specifies the flag to check.
  1255. * This parameter can be one of the following values:
  1256. * @arg ADC_FLAG_RDY ADC ready flag
  1257. * @arg ADC_FLAG_PD_RDY ADC powerdown ready flag
  1258. * @return The new state of ADC_FLAG_NEW (SET or RESET).
  1259. */
  1260. FlagStatus ADC_GetFlagStatusNew(ADC_Module* ADCx, uint8_t ADC_FLAG_NEW)
  1261. {
  1262. FlagStatus bitstatus = RESET;
  1263. /* Check the parameters */
  1264. assert_param(IsAdcModule(ADCx));
  1265. assert_param(IsAdcGetFlag(ADC_FLAG_NEW));
  1266. /* Check the status of the specified ADC flag */
  1267. if ((ADCx->CTRL3 & ADC_FLAG_NEW) != (uint8_t)RESET)
  1268. {
  1269. /* ADC_FLAG_NEW is set */
  1270. bitstatus = SET;
  1271. }
  1272. else
  1273. {
  1274. /* ADC_FLAG_NEW is reset */
  1275. bitstatus = RESET;
  1276. }
  1277. /* Return the ADC_FLAG_NEW status */
  1278. return bitstatus;
  1279. }
  1280. /**
  1281. * @brief Set Adc calibration bypass or enable.
  1282. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  1283. * @param en enable bypass calibration.
  1284. * This parameter can be one of the following values:
  1285. * @arg true bypass calibration
  1286. * @arg false not bypass calibration
  1287. */
  1288. void ADC_SetBypassCalibration(ADC_Module* ADCx, FunctionalState en)
  1289. {
  1290. uint32_t tmpregister = 0;
  1291. tmpregister = ADCx->CTRL3;
  1292. if (en)
  1293. tmpregister |= ADC_CTRL3_BPCAL_MSK;
  1294. else
  1295. tmpregister &= (~ADC_CTRL3_BPCAL_MSK);
  1296. ADCx->CTRL3 = tmpregister;
  1297. }
  1298. /**
  1299. * @brief Set Adc trans bits width.
  1300. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  1301. * @param ResultBitNum specifies num with adc trans width.
  1302. * This parameter can be one of the following values:
  1303. * @arg ADC_RST_BIT_12 12 bit trans
  1304. * @arg ADC_RST_BIT_10 10 bit trans
  1305. * @arg ADC_RST_BIT_8 8 bit trans
  1306. * @arg ADC_RESULT_BIT_6 6 bit trans
  1307. */
  1308. void ADC_SetConvResultBitNum(ADC_Module* ADCx, uint32_t ResultBitNum)
  1309. {
  1310. uint32_t tmpregister = 0;
  1311. tmpregister = ADCx->CTRL3;
  1312. tmpregister &= 0xFFFFFFFC;
  1313. tmpregister |= ResultBitNum;
  1314. ADCx->CTRL3 = tmpregister;
  1315. return;
  1316. }
  1317. /**
  1318. * @brief Set Adc Clock bits for AHB .
  1319. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  1320. */
  1321. void ADC_AHB_Clock_Mode_Config(ADC_Module* ADCx)
  1322. {
  1323. ADCx->CTRL3 &= ADC_CLOCK_AHB;
  1324. }
  1325. /**
  1326. * @brief Set Adc Clock bits for PLL .
  1327. * @param ADCx where x can be 1, 2, 3 or 4 to select the ADC peripheral.
  1328. */
  1329. void ADC_PLL_Clock_Mode_Config(ADC_Module* ADCx)
  1330. {
  1331. ADCx->CTRL3 |= ADC_CLOCK_PLL;
  1332. }
  1333. /**
  1334. * @brief Configures the ADCHCLK prescaler.
  1335. * @param RCC_ADCHCLKPrescaler specifies the ADCHCLK prescaler.
  1336. * This parameter can be on of the following values:
  1337. * @arg RCC_ADCHCLK_DIV1 ADCHCLKPRE[3:0] = 0000, HCLK Clock Divided By 1
  1338. * @arg RCC_ADCHCLK_DIV2 ADCHCLKPRE[3:0] = 0001, HCLK Clock Divided By 2
  1339. * @arg RCC_ADCHCLK_DIV4 ADCHCLKPRE[3:0] = 0010, HCLK Clock Divided By 4
  1340. * @arg RCC_ADCHCLK_DIV6 ADCHCLKPRE[3:0] = 0011, HCLK Clock Divided By 6
  1341. * @arg RCC_ADCHCLK_DIV8 ADCHCLKPRE[3:0] = 0100, HCLK Clock Divided By 8
  1342. * @arg RCC_ADCHCLK_DIV10 ADCHCLKPRE[3:0] = 0101, HCLK Clock Divided By 10
  1343. * @arg RCC_ADCHCLK_DIV12 ADCHCLKPRE[3:0] = 0110, HCLK Clock Divided By 12
  1344. * @arg RCC_ADCHCLK_DIV16 ADCHCLKPRE[3:0] = 0111, HCLK Clock Divided By 16
  1345. * @arg RCC_ADCHCLK_DIV32 ADCHCLKPRE[3:0] = 1000, HCLK Clock Divided By 32
  1346. * @arg RCC_ADCHCLK_DIV32 ADCHCLKPRE[3:0] = others, HCLK Clock Divided By 32
  1347. * @arg RCC_ADCPLLCLK_DISABLE ADCPLLCLKPRES[4:0] = 0xxxx, ADC Pll Clock Disable
  1348. * @arg RCC_ADCPLLCLK_DIV1 ADCPLLCLKPRES[4:0] = 10000, Pll Clock Divided By 1
  1349. * @arg RCC_ADCPLLCLK_DIV2 ADCPLLCLKPRES[4:0] = 10001, Pll Clock Divided By 2
  1350. * @arg RCC_ADCPLLCLK_DIV4 ADCPLLCLKPRES[4:0] = 10010, Pll Clock Divided By 4
  1351. * @arg RCC_ADCPLLCLK_DIV6 ADCPLLCLKPRES[4:0] = 10011, Pll Clock Divided By 6
  1352. * @arg RCC_ADCPLLCLK_DIV8 ADCPLLCLKPRES[4:0] = 10100, Pll Clock Divided By 8
  1353. * @arg RCC_ADCPLLCLK_DIV10 ADCPLLCLKPRES[4:0] = 10101, Pll Clock Divided By 10
  1354. * @arg RCC_ADCPLLCLK_DIV12 ADCPLLCLKPRES[4:0] = 10110, Pll Clock Divided By 12
  1355. * @arg RCC_ADCPLLCLK_DIV16 ADCPLLCLKPRES[4:0] = 10111, Pll Clock Divided By 16
  1356. * @arg RCC_ADCPLLCLK_DIV32 ADCPLLCLKPRES[4:0] = 11000, Pll Clock Divided By 32
  1357. * @arg RCC_ADCPLLCLK_DIV64 ADCPLLCLKPRES[4:0] = 11001, Pll Clock Divided By 64
  1358. * @arg RCC_ADCPLLCLK_DIV128 ADCPLLCLKPRES[4:0] = 11010, Pll Clock Divided By 128
  1359. * @arg RCC_ADCPLLCLK_DIV256 ADCPLLCLKPRES[4:0] = 11011, Pll Clock Divided By 256
  1360. * @arg RCC_ADCPLLCLK_DIV256 ADCPLLCLKPRES[4:0] = others, Pll Clock Divided By 256
  1361. */
  1362. void ADC_ConfigClk(ADC_CTRL3_CKMOD ADC_ClkMode, uint32_t RCC_ADCHCLKPrescaler)
  1363. {
  1364. if(ADC_ClkMode==ADC_CTRL3_CKMOD_AHB)
  1365. {
  1366. RCC_ConfigAdcPllClk(RCC_ADCPLLCLK_DIV1, DISABLE);
  1367. RCC_ConfigAdcHclk(RCC_ADCHCLKPrescaler);
  1368. ADC_AHB_Clock_Mode_Config(ADC1);
  1369. ADC_AHB_Clock_Mode_Config(ADC2);
  1370. ADC_AHB_Clock_Mode_Config(ADC3);
  1371. ADC_AHB_Clock_Mode_Config(ADC4);
  1372. }
  1373. else
  1374. {
  1375. RCC_ConfigAdcPllClk(RCC_ADCHCLKPrescaler, ENABLE);
  1376. RCC_ConfigAdcHclk(RCC_ADCHCLK_DIV1);
  1377. ADC_PLL_Clock_Mode_Config(ADC1);
  1378. ADC_PLL_Clock_Mode_Config(ADC2);
  1379. ADC_PLL_Clock_Mode_Config(ADC3);
  1380. ADC_PLL_Clock_Mode_Config(ADC4);
  1381. }
  1382. }
  1383. /**
  1384. * @}
  1385. */
  1386. /**
  1387. * @}
  1388. */
  1389. /**
  1390. * @}
  1391. */