stm32f2xx_adc.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_adc.c
  4. * @author MCD Application Team
  5. * @version V1.1.3
  6. * @date 31-December-2021
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Analog to Digital Convertor (ADC) peripheral:
  9. * - Initialization and Configuration (in addition to ADC multi mode
  10. * selection)
  11. * - Analog Watchdog configuration
  12. * - Temperature Sensor & Vrefint (Voltage Reference internal) & VBAT
  13. * management
  14. * - Regular Channels Configuration
  15. * - Regular Channels DMA Configuration
  16. * - Injected channels Configuration
  17. * - Interrupts and flags management
  18. *
  19. * @verbatim
  20. *
  21. * ===================================================================
  22. * How to use this driver
  23. * ===================================================================
  24. * 1. Enable the ADC interface clock using
  25. * RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADCx, ENABLE);
  26. *
  27. * 2. ADC pins configuration
  28. * - Enable the clock for the ADC GPIOs using the following function:
  29. * RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
  30. * - Configure these ADC pins in analog mode using GPIO_Init();
  31. *
  32. * 3. Configure the ADC Prescaler, conversion resolution and data
  33. * alignment using the ADC_Init() function.
  34. * 4. Activate the ADC peripheral using ADC_Cmd() function.
  35. *
  36. * Regular channels group configuration
  37. * ====================================
  38. * - To configure the ADC regular channels group features, use
  39. * ADC_Init() and ADC_RegularChannelConfig() functions.
  40. * - To activate the continuous mode, use the ADC_continuousModeCmd()
  41. * function.
  42. * - To configurate and activate the Discontinuous mode, use the
  43. * ADC_DiscModeChannelCountConfig() and ADC_DiscModeCmd() functions.
  44. * - To read the ADC converted values, use the ADC_GetConversionValue()
  45. * function.
  46. *
  47. * Multi mode ADCs Regular channels configuration
  48. * ===============================================
  49. * - Refer to "Regular channels group configuration" description to
  50. * configure the ADC1, ADC2 and ADC3 regular channels.
  51. * - Select the Multi mode ADC regular channels features (dual or
  52. * triple mode) using ADC_CommonInit() function and configure
  53. * the DMA mode using ADC_MultiModeDMARequestAfterLastTransferCmd()
  54. * functions.
  55. * - Read the ADCs converted values using the
  56. * ADC_GetMultiModeConversionValue() function.
  57. *
  58. * DMA for Regular channels group features configuration
  59. * ======================================================
  60. * - To enable the DMA mode for regular channels group, use the
  61. * ADC_DMACmd() function.
  62. * - To enable the generation of DMA requests continuously at the end
  63. * of the last DMA transfer, use the ADC_DMARequestAfterLastTransferCmd()
  64. * function.
  65. *
  66. * Injected channels group configuration
  67. * =====================================
  68. * - To configure the ADC Injected channels group features, use
  69. * ADC_InjectedChannelConfig() and ADC_InjectedSequencerLengthConfig()
  70. * functions.
  71. * - To activate the continuous mode, use the ADC_continuousModeCmd()
  72. * function.
  73. * - To activate the Injected Discontinuous mode, use the
  74. * ADC_InjectedDiscModeCmd() function.
  75. * - To activate the AutoInjected mode, use the ADC_AutoInjectedConvCmd()
  76. * function.
  77. * - To read the ADC converted values, use the ADC_GetInjectedConversionValue()
  78. * function.
  79. *
  80. * @endverbatim
  81. *
  82. ******************************************************************************
  83. * @attention
  84. *
  85. * Copyright (c) 2012 STMicroelectronics.
  86. * All rights reserved.
  87. *
  88. * This software is licensed under terms that can be found in the LICENSE file
  89. * in the root directory of this software component.
  90. * If no LICENSE file comes with this software, it is provided AS-IS.
  91. *
  92. ******************************************************************************
  93. */
  94. /* Includes ------------------------------------------------------------------*/
  95. #include "stm32f2xx_adc.h"
  96. #include "stm32f2xx_rcc.h"
  97. /** @addtogroup STM32F2xx_StdPeriph_Driver
  98. * @{
  99. */
  100. /** @defgroup ADC
  101. * @brief ADC driver modules
  102. * @{
  103. */
  104. /* Private typedef -----------------------------------------------------------*/
  105. /* Private define ------------------------------------------------------------*/
  106. /* ADC DISCNUM mask */
  107. #define CR1_DISCNUM_RESET ((uint32_t)0xFFFF1FFF)
  108. /* ADC AWDCH mask */
  109. #define CR1_AWDCH_RESET ((uint32_t)0xFFFFFFE0)
  110. /* ADC Analog watchdog enable mode mask */
  111. #define CR1_AWDMode_RESET ((uint32_t)0xFF3FFDFF)
  112. /* CR1 register Mask */
  113. #define CR1_CLEAR_MASK ((uint32_t)0xFCFFFEFF)
  114. /* ADC EXTEN mask */
  115. #define CR2_EXTEN_RESET ((uint32_t)0xCFFFFFFF)
  116. /* ADC JEXTEN mask */
  117. #define CR2_JEXTEN_RESET ((uint32_t)0xFFCFFFFF)
  118. /* ADC JEXTSEL mask */
  119. #define CR2_JEXTSEL_RESET ((uint32_t)0xFFF0FFFF)
  120. /* CR2 register Mask */
  121. #define CR2_CLEAR_MASK ((uint32_t)0xC0FFF7FD)
  122. /* ADC SQx mask */
  123. #define SQR3_SQ_SET ((uint32_t)0x0000001F)
  124. #define SQR2_SQ_SET ((uint32_t)0x0000001F)
  125. #define SQR1_SQ_SET ((uint32_t)0x0000001F)
  126. /* ADC L Mask */
  127. #define SQR1_L_RESET ((uint32_t)0xFF0FFFFF)
  128. /* ADC JSQx mask */
  129. #define JSQR_JSQ_SET ((uint32_t)0x0000001F)
  130. /* ADC JL mask */
  131. #define JSQR_JL_SET ((uint32_t)0x00300000)
  132. #define JSQR_JL_RESET ((uint32_t)0xFFCFFFFF)
  133. /* ADC SMPx mask */
  134. #define SMPR1_SMP_SET ((uint32_t)0x00000007)
  135. #define SMPR2_SMP_SET ((uint32_t)0x00000007)
  136. /* ADC JDRx registers offset */
  137. #define JDR_OFFSET ((uint8_t)0x28)
  138. /* ADC CDR register base address */
  139. #define CDR_ADDRESS ((uint32_t)0x40012308)
  140. /* ADC CCR register Mask */
  141. #define CR_CLEAR_MASK ((uint32_t)0xFFFC30E0)
  142. /* Private macro -------------------------------------------------------------*/
  143. /* Private variables ---------------------------------------------------------*/
  144. /* Private function prototypes -----------------------------------------------*/
  145. /* Private functions ---------------------------------------------------------*/
  146. /** @defgroup ADC_Private_Functions
  147. * @{
  148. */
  149. /** @defgroup ADC_Group1 Initialization and Configuration functions
  150. * @brief Initialization and Configuration functions
  151. *
  152. @verbatim
  153. ===============================================================================
  154. Initialization and Configuration functions
  155. ===============================================================================
  156. This section provides functions allowing to:
  157. - Initialize and configure the ADC Prescaler
  158. - ADC Conversion Resolution (12bit..6bit)
  159. - Scan Conversion Mode (multichannels or one channel) for regular group
  160. - ADC Continuous Conversion Mode (Continuous or Single conversion) for
  161. regular group
  162. - External trigger Edge and source of regular group,
  163. - Converted data alignment (left or right)
  164. - The number of ADC conversions that will be done using the sequencer for
  165. regular channel group
  166. - Multi ADC mode selection
  167. - Direct memory access mode selection for multi ADC mode
  168. - Delay between 2 sampling phases (used in dual or triple interleaved modes)
  169. - Enable or disable the ADC peripheral
  170. @endverbatim
  171. * @{
  172. */
  173. /**
  174. * @brief Deinitializes all ADCs peripherals registers to their default reset
  175. * values.
  176. * @param None
  177. * @retval None
  178. */
  179. void ADC_DeInit(void)
  180. {
  181. /* Enable all ADCs reset state */
  182. RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC, ENABLE);
  183. /* Release all ADCs from reset state */
  184. RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC, DISABLE);
  185. }
  186. /**
  187. * @brief Initializes the ADCx peripheral according to the specified parameters
  188. * in the ADC_InitStruct.
  189. * @note This function is used to configure the global features of the ADC (
  190. * Resolution and Data Alignment), however, the rest of the configuration
  191. * parameters are specific to the regular channels group (scan mode
  192. * activation, continuous mode activation, External trigger source and
  193. * edge, number of conversion in the regular channels group sequencer).
  194. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  195. * @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure that contains
  196. * the configuration information for the specified ADC peripheral.
  197. * @retval None
  198. */
  199. void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
  200. {
  201. uint32_t tmpreg1 = 0;
  202. uint8_t tmpreg2 = 0;
  203. /* Check the parameters */
  204. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  205. assert_param(IS_ADC_RESOLUTION(ADC_InitStruct->ADC_Resolution));
  206. assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode));
  207. assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode));
  208. assert_param(IS_ADC_EXT_TRIG_EDGE(ADC_InitStruct->ADC_ExternalTrigConvEdge));
  209. assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv));
  210. assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign));
  211. assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfConversion));
  212. /*---------------------------- ADCx CR1 Configuration -----------------*/
  213. /* Get the ADCx CR1 value */
  214. tmpreg1 = ADCx->CR1;
  215. /* Clear RES and SCAN bits */
  216. tmpreg1 &= CR1_CLEAR_MASK;
  217. /* Configure ADCx: scan conversion mode and resolution */
  218. /* Set SCAN bit according to ADC_ScanConvMode value */
  219. /* Set RES bit according to ADC_Resolution value */
  220. tmpreg1 |= (uint32_t)(((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8) | \
  221. ADC_InitStruct->ADC_Resolution);
  222. /* Write to ADCx CR1 */
  223. ADCx->CR1 = tmpreg1;
  224. /*---------------------------- ADCx CR2 Configuration -----------------*/
  225. /* Get the ADCx CR2 value */
  226. tmpreg1 = ADCx->CR2;
  227. /* Clear CONT, ALIGN, EXTEN and EXTSEL bits */
  228. tmpreg1 &= CR2_CLEAR_MASK;
  229. /* Configure ADCx: external trigger event and edge, data alignment and
  230. continuous conversion mode */
  231. /* Set ALIGN bit according to ADC_DataAlign value */
  232. /* Set EXTEN bits according to ADC_ExternalTrigConvEdge value */
  233. /* Set EXTSEL bits according to ADC_ExternalTrigConv value */
  234. /* Set CONT bit according to ADC_ContinuousConvMode value */
  235. tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | \
  236. ADC_InitStruct->ADC_ExternalTrigConv |
  237. ADC_InitStruct->ADC_ExternalTrigConvEdge | \
  238. ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));
  239. /* Write to ADCx CR2 */
  240. ADCx->CR2 = tmpreg1;
  241. /*---------------------------- ADCx SQR1 Configuration -----------------*/
  242. /* Get the ADCx SQR1 value */
  243. tmpreg1 = ADCx->SQR1;
  244. /* Clear L bits */
  245. tmpreg1 &= SQR1_L_RESET;
  246. /* Configure ADCx: regular channel sequence length */
  247. /* Set L bits according to ADC_NbrOfConversion value */
  248. tmpreg2 |= (uint8_t)(ADC_InitStruct->ADC_NbrOfConversion - (uint8_t)1);
  249. tmpreg1 |= ((uint32_t)tmpreg2 << 20);
  250. /* Write to ADCx SQR1 */
  251. ADCx->SQR1 = tmpreg1;
  252. }
  253. /**
  254. * @brief Fills each ADC_InitStruct member with its default value.
  255. * @note This function is used to initialize the global features of the ADC (
  256. * Resolution and Data Alignment), however, the rest of the configuration
  257. * parameters are specific to the regular channels group (scan mode
  258. * activation, continuous mode activation, External trigger source and
  259. * edge, number of conversion in the regular channels group sequencer).
  260. * @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure which will
  261. * be initialized.
  262. * @retval None
  263. */
  264. void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
  265. {
  266. /* Initialize the ADC_Mode member */
  267. ADC_InitStruct->ADC_Resolution = ADC_Resolution_12b;
  268. /* initialize the ADC_ScanConvMode member */
  269. ADC_InitStruct->ADC_ScanConvMode = DISABLE;
  270. /* Initialize the ADC_ContinuousConvMode member */
  271. ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;
  272. /* Initialize the ADC_ExternalTrigConvEdge member */
  273. ADC_InitStruct->ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  274. /* Initialize the ADC_ExternalTrigConv member */
  275. ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  276. /* Initialize the ADC_DataAlign member */
  277. ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;
  278. /* Initialize the ADC_NbrOfConversion member */
  279. ADC_InitStruct->ADC_NbrOfConversion = 1;
  280. }
  281. /**
  282. * @brief Initializes the ADCs peripherals according to the specified parameters
  283. * in the ADC_CommonInitStruct.
  284. * @param ADC_CommonInitStruct: pointer to an ADC_CommonInitTypeDef structure
  285. * that contains the configuration information for All ADCs peripherals.
  286. * @retval None
  287. */
  288. void ADC_CommonInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct)
  289. {
  290. uint32_t tmpreg1 = 0;
  291. /* Check the parameters */
  292. assert_param(IS_ADC_MODE(ADC_CommonInitStruct->ADC_Mode));
  293. assert_param(IS_ADC_PRESCALER(ADC_CommonInitStruct->ADC_Prescaler));
  294. assert_param(IS_ADC_DMA_ACCESS_MODE(ADC_CommonInitStruct->ADC_DMAAccessMode));
  295. assert_param(IS_ADC_SAMPLING_DELAY(ADC_CommonInitStruct->ADC_TwoSamplingDelay));
  296. /*---------------------------- ADC CCR Configuration -----------------*/
  297. /* Get the ADC CCR value */
  298. tmpreg1 = ADC->CCR;
  299. /* Clear MULTI, DELAY, DMA and ADCPRE bits */
  300. tmpreg1 &= CR_CLEAR_MASK;
  301. /* Configure ADCx: Multi mode, Delay between two sampling time, ADC prescaler,
  302. and DMA access mode for multimode */
  303. /* Set MULTI bits according to ADC_Mode value */
  304. /* Set ADCPRE bits according to ADC_Prescaler value */
  305. /* Set DMA bits according to ADC_DMAAccessMode value */
  306. /* Set DELAY bits according to ADC_TwoSamplingDelay value */
  307. tmpreg1 |= (uint32_t)(ADC_CommonInitStruct->ADC_Mode |
  308. ADC_CommonInitStruct->ADC_Prescaler |
  309. ADC_CommonInitStruct->ADC_DMAAccessMode |
  310. ADC_CommonInitStruct->ADC_TwoSamplingDelay);
  311. /* Write to ADC CCR */
  312. ADC->CCR = tmpreg1;
  313. }
  314. /**
  315. * @brief Fills each ADC_CommonInitStruct member with its default value.
  316. * @param ADC_CommonInitStruct: pointer to an ADC_CommonInitTypeDef structure
  317. * which will be initialized.
  318. * @retval None
  319. */
  320. void ADC_CommonStructInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct)
  321. {
  322. /* Initialize the ADC_Mode member */
  323. ADC_CommonInitStruct->ADC_Mode = ADC_Mode_Independent;
  324. /* initialize the ADC_Prescaler member */
  325. ADC_CommonInitStruct->ADC_Prescaler = ADC_Prescaler_Div2;
  326. /* Initialize the ADC_DMAAccessMode member */
  327. ADC_CommonInitStruct->ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  328. /* Initialize the ADC_TwoSamplingDelay member */
  329. ADC_CommonInitStruct->ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  330. }
  331. /**
  332. * @brief Enables or disables the specified ADC peripheral.
  333. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  334. * @param NewState: new state of the ADCx peripheral.
  335. * This parameter can be: ENABLE or DISABLE.
  336. * @retval None
  337. */
  338. void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  339. {
  340. /* Check the parameters */
  341. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  342. assert_param(IS_FUNCTIONAL_STATE(NewState));
  343. if (NewState != DISABLE)
  344. {
  345. /* Set the ADON bit to wake up the ADC from power down mode */
  346. ADCx->CR2 |= (uint32_t)ADC_CR2_ADON;
  347. }
  348. else
  349. {
  350. /* Disable the selected ADC peripheral */
  351. ADCx->CR2 &= (uint32_t)(~ADC_CR2_ADON);
  352. }
  353. }
  354. /**
  355. * @}
  356. */
  357. /** @defgroup ADC_Group2 Analog Watchdog configuration functions
  358. * @brief Analog Watchdog configuration functions
  359. *
  360. @verbatim
  361. ===============================================================================
  362. Analog Watchdog configuration functions
  363. ===============================================================================
  364. This section provides functions allowing to configure the Analog Watchdog
  365. (AWD) feature in the ADC.
  366. A typical configuration Analog Watchdog is done following these steps :
  367. 1. the ADC guarded channel(s) is (are) selected using the
  368. ADC_AnalogWatchdogSingleChannelConfig() function.
  369. 2. The Analog watchdog lower and higher threshold are configured using the
  370. ADC_AnalogWatchdogThresholdsConfig() function.
  371. 3. The Analog watchdog is enabled and configured to enable the check, on one
  372. or more channels, using the ADC_AnalogWatchdogCmd() function.
  373. @endverbatim
  374. * @{
  375. */
  376. /**
  377. * @brief Enables or disables the analog watchdog on single/all regular or
  378. * injected channels
  379. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  380. * @param ADC_AnalogWatchdog: the ADC analog watchdog configuration.
  381. * This parameter can be one of the following values:
  382. * @arg ADC_AnalogWatchdog_SingleRegEnable: Analog watchdog on a single regular channel
  383. * @arg ADC_AnalogWatchdog_SingleInjecEnable: Analog watchdog on a single injected channel
  384. * @arg ADC_AnalogWatchdog_SingleRegOrInjecEnable: Analog watchdog on a single regular or injected channel
  385. * @arg ADC_AnalogWatchdog_AllRegEnable: Analog watchdog on all regular channel
  386. * @arg ADC_AnalogWatchdog_AllInjecEnable: Analog watchdog on all injected channel
  387. * @arg ADC_AnalogWatchdog_AllRegAllInjecEnable: Analog watchdog on all regular and injected channels
  388. * @arg ADC_AnalogWatchdog_None: No channel guarded by the analog watchdog
  389. * @retval None
  390. */
  391. void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog)
  392. {
  393. uint32_t tmpreg = 0;
  394. /* Check the parameters */
  395. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  396. assert_param(IS_ADC_ANALOG_WATCHDOG(ADC_AnalogWatchdog));
  397. /* Get the old register value */
  398. tmpreg = ADCx->CR1;
  399. /* Clear AWDEN, JAWDEN and AWDSGL bits */
  400. tmpreg &= CR1_AWDMode_RESET;
  401. /* Set the analog watchdog enable mode */
  402. tmpreg |= ADC_AnalogWatchdog;
  403. /* Store the new register value */
  404. ADCx->CR1 = tmpreg;
  405. }
  406. /**
  407. * @brief Configures the high and low thresholds of the analog watchdog.
  408. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  409. * @param HighThreshold: the ADC analog watchdog High threshold value.
  410. * This parameter must be a 12-bit value.
  411. * @param LowThreshold: the ADC analog watchdog Low threshold value.
  412. * This parameter must be a 12-bit value.
  413. * @retval None
  414. */
  415. void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,
  416. uint16_t LowThreshold)
  417. {
  418. /* Check the parameters */
  419. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  420. assert_param(IS_ADC_THRESHOLD(HighThreshold));
  421. assert_param(IS_ADC_THRESHOLD(LowThreshold));
  422. /* Set the ADCx high threshold */
  423. ADCx->HTR = HighThreshold;
  424. /* Set the ADCx low threshold */
  425. ADCx->LTR = LowThreshold;
  426. }
  427. /**
  428. * @brief Configures the analog watchdog guarded single channel
  429. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  430. * @param ADC_Channel: the ADC channel to configure for the analog watchdog.
  431. * This parameter can be one of the following values:
  432. * @arg ADC_Channel_0: ADC Channel0 selected
  433. * @arg ADC_Channel_1: ADC Channel1 selected
  434. * @arg ADC_Channel_2: ADC Channel2 selected
  435. * @arg ADC_Channel_3: ADC Channel3 selected
  436. * @arg ADC_Channel_4: ADC Channel4 selected
  437. * @arg ADC_Channel_5: ADC Channel5 selected
  438. * @arg ADC_Channel_6: ADC Channel6 selected
  439. * @arg ADC_Channel_7: ADC Channel7 selected
  440. * @arg ADC_Channel_8: ADC Channel8 selected
  441. * @arg ADC_Channel_9: ADC Channel9 selected
  442. * @arg ADC_Channel_10: ADC Channel10 selected
  443. * @arg ADC_Channel_11: ADC Channel11 selected
  444. * @arg ADC_Channel_12: ADC Channel12 selected
  445. * @arg ADC_Channel_13: ADC Channel13 selected
  446. * @arg ADC_Channel_14: ADC Channel14 selected
  447. * @arg ADC_Channel_15: ADC Channel15 selected
  448. * @arg ADC_Channel_16: ADC Channel16 selected
  449. * @arg ADC_Channel_17: ADC Channel17 selected
  450. * @arg ADC_Channel_18: ADC Channel18 selected
  451. * @retval None
  452. */
  453. void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel)
  454. {
  455. uint32_t tmpreg = 0;
  456. /* Check the parameters */
  457. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  458. assert_param(IS_ADC_CHANNEL(ADC_Channel));
  459. /* Get the old register value */
  460. tmpreg = ADCx->CR1;
  461. /* Clear the Analog watchdog channel select bits */
  462. tmpreg &= CR1_AWDCH_RESET;
  463. /* Set the Analog watchdog channel */
  464. tmpreg |= ADC_Channel;
  465. /* Store the new register value */
  466. ADCx->CR1 = tmpreg;
  467. }
  468. /**
  469. * @}
  470. */
  471. /** @defgroup ADC_Group3 Temperature Sensor, Vrefint (Voltage Reference internal)
  472. * and VBAT (Voltage BATtery) management functions
  473. * @brief Temperature Sensor, Vrefint and VBAT management functions
  474. *
  475. @verbatim
  476. ===============================================================================
  477. Temperature Sensor, Vrefint and VBAT management functions
  478. ===============================================================================
  479. This section provides functions allowing to enable/ disable the internal
  480. connections between the ADC and the Temperature Sensor, the Vrefint and the
  481. Vbat sources.
  482. A typical configuration to get the Temperature sensor and Vrefint channels
  483. voltages is done following these steps :
  484. 1. Enable the internal connection of Temperature sensor and Vrefint sources
  485. with the ADC channels using ADC_TempSensorVrefintCmd() function.
  486. 2. Select the ADC_Channel_TempSensor and/or ADC_Channel_Vrefint using
  487. ADC_RegularChannelConfig() or ADC_InjectedChannelConfig() functions
  488. 3. Get the voltage values, using ADC_GetConversionValue() or
  489. ADC_GetInjectedConversionValue().
  490. A typical configuration to get the VBAT channel voltage is done following
  491. these steps :
  492. 1. Enable the internal connection of VBAT source with the ADC channel using
  493. ADC_VBATCmd() function.
  494. 2. Select the ADC_Channel_Vbat using ADC_RegularChannelConfig() or
  495. ADC_InjectedChannelConfig() functions
  496. 3. Get the voltage value, using ADC_GetConversionValue() or
  497. ADC_GetInjectedConversionValue().
  498. @endverbatim
  499. * @{
  500. */
  501. /**
  502. * @brief Enables or disables the temperature sensor and Vrefint channels.
  503. * @param NewState: new state of the temperature sensor and Vrefint channels.
  504. * This parameter can be: ENABLE or DISABLE.
  505. * @retval None
  506. */
  507. void ADC_TempSensorVrefintCmd(FunctionalState NewState)
  508. {
  509. /* Check the parameters */
  510. assert_param(IS_FUNCTIONAL_STATE(NewState));
  511. if (NewState != DISABLE)
  512. {
  513. /* Enable the temperature sensor and Vrefint channel*/
  514. ADC->CCR |= (uint32_t)ADC_CCR_TSVREFE;
  515. }
  516. else
  517. {
  518. /* Disable the temperature sensor and Vrefint channel*/
  519. ADC->CCR &= (uint32_t)(~ADC_CCR_TSVREFE);
  520. }
  521. }
  522. /**
  523. * @brief Enables or disables the VBAT (Voltage Battery) channel.
  524. * @param NewState: new state of the VBAT channel.
  525. * This parameter can be: ENABLE or DISABLE.
  526. * @retval None
  527. */
  528. void ADC_VBATCmd(FunctionalState NewState)
  529. {
  530. /* Check the parameters */
  531. assert_param(IS_FUNCTIONAL_STATE(NewState));
  532. if (NewState != DISABLE)
  533. {
  534. /* Enable the VBAT channel*/
  535. ADC->CCR |= (uint32_t)ADC_CCR_VBATE;
  536. }
  537. else
  538. {
  539. /* Disable the VBAT channel*/
  540. ADC->CCR &= (uint32_t)(~ADC_CCR_VBATE);
  541. }
  542. }
  543. /**
  544. * @}
  545. */
  546. /** @defgroup ADC_Group4 Regular Channels Configuration functions
  547. * @brief Regular Channels Configuration functions
  548. *
  549. @verbatim
  550. ===============================================================================
  551. Regular Channels Configuration functions
  552. ===============================================================================
  553. This section provides functions allowing to manage the ADC's regular channels,
  554. it is composed of 2 sub sections :
  555. 1. Configuration and management functions for regular channels: This subsection
  556. provides functions allowing to configure the ADC regular channels :
  557. - Configure the rank in the regular group sequencer for each channel
  558. - Configure the sampling time for each channel
  559. - select the conversion Trigger for regular channels
  560. - select the desired EOC event behavior configuration
  561. - Activate the continuous Mode (*)
  562. - Activate the Discontinuous Mode
  563. Please Note that the following features for regular channels are configurated
  564. using the ADC_Init() function :
  565. - scan mode activation
  566. - continuous mode activation (**)
  567. - External trigger source
  568. - External trigger edge
  569. - number of conversion in the regular channels group sequencer.
  570. @note (*) and (**) are performing the same configuration
  571. 2. Get the conversion data: This subsection provides an important function in
  572. the ADC peripheral since it returns the converted data of the current
  573. regular channel. When the Conversion value is read, the EOC Flag is
  574. automatically cleared.
  575. @note For multi ADC mode, the last ADC1, ADC2 and ADC3 regular conversions
  576. results data (in the selected multi mode) can be returned in the same
  577. time using ADC_GetMultiModeConversionValue() function.
  578. @endverbatim
  579. * @{
  580. */
  581. /**
  582. * @brief Configures for the selected ADC regular channel its corresponding
  583. * rank in the sequencer and its sample time.
  584. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  585. * @param ADC_Channel: the ADC channel to configure.
  586. * This parameter can be one of the following values:
  587. * @arg ADC_Channel_0: ADC Channel0 selected
  588. * @arg ADC_Channel_1: ADC Channel1 selected
  589. * @arg ADC_Channel_2: ADC Channel2 selected
  590. * @arg ADC_Channel_3: ADC Channel3 selected
  591. * @arg ADC_Channel_4: ADC Channel4 selected
  592. * @arg ADC_Channel_5: ADC Channel5 selected
  593. * @arg ADC_Channel_6: ADC Channel6 selected
  594. * @arg ADC_Channel_7: ADC Channel7 selected
  595. * @arg ADC_Channel_8: ADC Channel8 selected
  596. * @arg ADC_Channel_9: ADC Channel9 selected
  597. * @arg ADC_Channel_10: ADC Channel10 selected
  598. * @arg ADC_Channel_11: ADC Channel11 selected
  599. * @arg ADC_Channel_12: ADC Channel12 selected
  600. * @arg ADC_Channel_13: ADC Channel13 selected
  601. * @arg ADC_Channel_14: ADC Channel14 selected
  602. * @arg ADC_Channel_15: ADC Channel15 selected
  603. * @arg ADC_Channel_16: ADC Channel16 selected
  604. * @arg ADC_Channel_17: ADC Channel17 selected
  605. * @arg ADC_Channel_18: ADC Channel18 selected
  606. * @param Rank: The rank in the regular group sequencer.
  607. * This parameter must be between 1 to 16.
  608. * @param ADC_SampleTime: The sample time value to be set for the selected channel.
  609. * This parameter can be one of the following values:
  610. * @arg ADC_SampleTime_3Cycles: Sample time equal to 3 cycles
  611. * @arg ADC_SampleTime_15Cycles: Sample time equal to 15 cycles
  612. * @arg ADC_SampleTime_28Cycles: Sample time equal to 28 cycles
  613. * @arg ADC_SampleTime_56Cycles: Sample time equal to 56 cycles
  614. * @arg ADC_SampleTime_84Cycles: Sample time equal to 84 cycles
  615. * @arg ADC_SampleTime_112Cycles: Sample time equal to 112 cycles
  616. * @arg ADC_SampleTime_144Cycles: Sample time equal to 144 cycles
  617. * @arg ADC_SampleTime_480Cycles: Sample time equal to 480 cycles
  618. * @retval None
  619. */
  620. void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
  621. {
  622. uint32_t tmpreg1 = 0, tmpreg2 = 0;
  623. /* Check the parameters */
  624. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  625. assert_param(IS_ADC_CHANNEL(ADC_Channel));
  626. assert_param(IS_ADC_REGULAR_RANK(Rank));
  627. assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
  628. /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
  629. if (ADC_Channel > ADC_Channel_9)
  630. {
  631. /* Get the old register value */
  632. tmpreg1 = ADCx->SMPR1;
  633. /* Calculate the mask to clear */
  634. tmpreg2 = SMPR1_SMP_SET << (3 * (ADC_Channel - 10));
  635. /* Clear the old sample time */
  636. tmpreg1 &= ~tmpreg2;
  637. /* Calculate the mask to set */
  638. tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10));
  639. /* Set the new sample time */
  640. tmpreg1 |= tmpreg2;
  641. /* Store the new register value */
  642. ADCx->SMPR1 = tmpreg1;
  643. }
  644. else /* ADC_Channel include in ADC_Channel_[0..9] */
  645. {
  646. /* Get the old register value */
  647. tmpreg1 = ADCx->SMPR2;
  648. /* Calculate the mask to clear */
  649. tmpreg2 = SMPR2_SMP_SET << (3 * ADC_Channel);
  650. /* Clear the old sample time */
  651. tmpreg1 &= ~tmpreg2;
  652. /* Calculate the mask to set */
  653. tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
  654. /* Set the new sample time */
  655. tmpreg1 |= tmpreg2;
  656. /* Store the new register value */
  657. ADCx->SMPR2 = tmpreg1;
  658. }
  659. /* For Rank 1 to 6 */
  660. if (Rank < 7)
  661. {
  662. /* Get the old register value */
  663. tmpreg1 = ADCx->SQR3;
  664. /* Calculate the mask to clear */
  665. tmpreg2 = SQR3_SQ_SET << (5 * (Rank - 1));
  666. /* Clear the old SQx bits for the selected rank */
  667. tmpreg1 &= ~tmpreg2;
  668. /* Calculate the mask to set */
  669. tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));
  670. /* Set the SQx bits for the selected rank */
  671. tmpreg1 |= tmpreg2;
  672. /* Store the new register value */
  673. ADCx->SQR3 = tmpreg1;
  674. }
  675. /* For Rank 7 to 12 */
  676. else if (Rank < 13)
  677. {
  678. /* Get the old register value */
  679. tmpreg1 = ADCx->SQR2;
  680. /* Calculate the mask to clear */
  681. tmpreg2 = SQR2_SQ_SET << (5 * (Rank - 7));
  682. /* Clear the old SQx bits for the selected rank */
  683. tmpreg1 &= ~tmpreg2;
  684. /* Calculate the mask to set */
  685. tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));
  686. /* Set the SQx bits for the selected rank */
  687. tmpreg1 |= tmpreg2;
  688. /* Store the new register value */
  689. ADCx->SQR2 = tmpreg1;
  690. }
  691. /* For Rank 13 to 16 */
  692. else
  693. {
  694. /* Get the old register value */
  695. tmpreg1 = ADCx->SQR1;
  696. /* Calculate the mask to clear */
  697. tmpreg2 = SQR1_SQ_SET << (5 * (Rank - 13));
  698. /* Clear the old SQx bits for the selected rank */
  699. tmpreg1 &= ~tmpreg2;
  700. /* Calculate the mask to set */
  701. tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));
  702. /* Set the SQx bits for the selected rank */
  703. tmpreg1 |= tmpreg2;
  704. /* Store the new register value */
  705. ADCx->SQR1 = tmpreg1;
  706. }
  707. }
  708. /**
  709. * @brief Enables the selected ADC software start conversion of the regular channels.
  710. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  711. * @retval None
  712. */
  713. void ADC_SoftwareStartConv(ADC_TypeDef* ADCx)
  714. {
  715. /* Check the parameters */
  716. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  717. /* Enable the selected ADC conversion for regular group */
  718. ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  719. }
  720. /**
  721. * @brief Gets the selected ADC Software start regular conversion Status.
  722. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  723. * @retval The new state of ADC software start conversion (SET or RESET).
  724. */
  725. FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx)
  726. {
  727. FlagStatus bitstatus = RESET;
  728. /* Check the parameters */
  729. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  730. /* Check the status of SWSTART bit */
  731. if ((ADCx->CR2 & ADC_CR2_JSWSTART) != (uint32_t)RESET)
  732. {
  733. /* SWSTART bit is set */
  734. bitstatus = SET;
  735. }
  736. else
  737. {
  738. /* SWSTART bit is reset */
  739. bitstatus = RESET;
  740. }
  741. /* Return the SWSTART bit status */
  742. return bitstatus;
  743. }
  744. /**
  745. * @brief Enables or disables the EOC on each regular channel conversion
  746. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  747. * @param NewState: new state of the selected ADC EOC flag rising
  748. * This parameter can be: ENABLE or DISABLE.
  749. * @retval None
  750. */
  751. void ADC_EOCOnEachRegularChannelCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  752. {
  753. /* Check the parameters */
  754. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  755. assert_param(IS_FUNCTIONAL_STATE(NewState));
  756. if (NewState != DISABLE)
  757. {
  758. /* Enable the selected ADC EOC rising on each regular channel conversion */
  759. ADCx->CR2 |= (uint32_t)ADC_CR2_EOCS;
  760. }
  761. else
  762. {
  763. /* Disable the selected ADC EOC rising on each regular channel conversion */
  764. ADCx->CR2 &= (uint32_t)(~ADC_CR2_EOCS);
  765. }
  766. }
  767. /**
  768. * @brief Enables or disables the ADC continuous conversion mode
  769. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  770. * @param NewState: new state of the selected ADC continuous conversion mode
  771. * This parameter can be: ENABLE or DISABLE.
  772. * @retval None
  773. */
  774. void ADC_ContinuousModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  775. {
  776. /* Check the parameters */
  777. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  778. assert_param(IS_FUNCTIONAL_STATE(NewState));
  779. if (NewState != DISABLE)
  780. {
  781. /* Enable the selected ADC continuous conversion mode */
  782. ADCx->CR2 |= (uint32_t)ADC_CR2_CONT;
  783. }
  784. else
  785. {
  786. /* Disable the selected ADC continuous conversion mode */
  787. ADCx->CR2 &= (uint32_t)(~ADC_CR2_CONT);
  788. }
  789. }
  790. /**
  791. * @brief Configures the discontinuous mode for the selected ADC regular group
  792. * channel.
  793. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  794. * @param Number: specifies the discontinuous mode regular channel count value.
  795. * This number must be between 1 and 8.
  796. * @retval None
  797. */
  798. void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number)
  799. {
  800. uint32_t tmpreg1 = 0;
  801. uint32_t tmpreg2 = 0;
  802. /* Check the parameters */
  803. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  804. assert_param(IS_ADC_REGULAR_DISC_NUMBER(Number));
  805. /* Get the old register value */
  806. tmpreg1 = ADCx->CR1;
  807. /* Clear the old discontinuous mode channel count */
  808. tmpreg1 &= CR1_DISCNUM_RESET;
  809. /* Set the discontinuous mode channel count */
  810. tmpreg2 = Number - 1;
  811. tmpreg1 |= tmpreg2 << 13;
  812. /* Store the new register value */
  813. ADCx->CR1 = tmpreg1;
  814. }
  815. /**
  816. * @brief Enables or disables the discontinuous mode on regular group channel
  817. * for the specified ADC
  818. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  819. * @param NewState: new state of the selected ADC discontinuous mode on
  820. * regular group channel.
  821. * This parameter can be: ENABLE or DISABLE.
  822. * @retval None
  823. */
  824. void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  825. {
  826. /* Check the parameters */
  827. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  828. assert_param(IS_FUNCTIONAL_STATE(NewState));
  829. if (NewState != DISABLE)
  830. {
  831. /* Enable the selected ADC regular discontinuous mode */
  832. ADCx->CR1 |= (uint32_t)ADC_CR1_DISCEN;
  833. }
  834. else
  835. {
  836. /* Disable the selected ADC regular discontinuous mode */
  837. ADCx->CR1 &= (uint32_t)(~ADC_CR1_DISCEN);
  838. }
  839. }
  840. /**
  841. * @brief Returns the last ADCx conversion result data for regular channel.
  842. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  843. * @retval The Data conversion value.
  844. */
  845. uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
  846. {
  847. /* Check the parameters */
  848. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  849. /* Return the selected ADC conversion value */
  850. return (uint16_t) ADCx->DR;
  851. }
  852. /**
  853. * @brief Returns the last ADC1, ADC2 and ADC3 regular conversions results
  854. * data in the selected multi mode.
  855. * @param None
  856. * @retval The Data conversion value.
  857. * @note In dual mode, the value returned by this function is as following
  858. * Data[15:0] : these bits contain the regular data of ADC1.
  859. * Data[31:16]: these bits contain the regular data of ADC2.
  860. * @note In triple mode, the value returned by this function is as following
  861. * Data[15:0] : these bits contain alternatively the regular data of ADC1, ADC3 and ADC2.
  862. * Data[31:16]: these bits contain alternatively the regular data of ADC2, ADC1 and ADC3.
  863. */
  864. uint32_t ADC_GetMultiModeConversionValue(void)
  865. {
  866. /* Return the multi mode conversion value */
  867. return (*(__IO uint32_t *) CDR_ADDRESS);
  868. }
  869. /**
  870. * @}
  871. */
  872. /** @defgroup ADC_Group5 Regular Channels DMA Configuration functions
  873. * @brief Regular Channels DMA Configuration functions
  874. *
  875. @verbatim
  876. ===============================================================================
  877. Regular Channels DMA Configuration functions
  878. ===============================================================================
  879. This section provides functions allowing to configure the DMA for ADC regular
  880. channels.
  881. Since converted regular channel values are stored into a unique data register,
  882. it is useful to use DMA for conversion of more than one regular channel. This
  883. avoids the loss of the data already stored in the ADC Data register.
  884. When the DMA mode is enabled (using the ADC_DMACmd() function), after each
  885. conversion of a regular channel, a DMA request is generated.
  886. Depending on the "DMA disable selection for Independent ADC mode"
  887. configuration (using the ADC_DMARequestAfterLastTransferCmd() function),
  888. at the end of the last DMA transfer, two possibilities are allowed:
  889. - No new DMA request is issued to the DMA controller (feature DISABLED)
  890. - Requests can continue to be generated (feature ENABLED).
  891. Depending on the "DMA disable selection for multi ADC mode" configuration
  892. (using the void ADC_MultiModeDMARequestAfterLastTransferCmd() function),
  893. at the end of the last DMA transfer, two possibilities are allowed:
  894. - No new DMA request is issued to the DMA controller (feature DISABLED)
  895. - Requests can continue to be generated (feature ENABLED).
  896. @endverbatim
  897. * @{
  898. */
  899. /**
  900. * @brief Enables or disables the specified ADC DMA request.
  901. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  902. * @param NewState: new state of the selected ADC DMA transfer.
  903. * This parameter can be: ENABLE or DISABLE.
  904. * @retval None
  905. */
  906. void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  907. {
  908. /* Check the parameters */
  909. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  910. assert_param(IS_FUNCTIONAL_STATE(NewState));
  911. if (NewState != DISABLE)
  912. {
  913. /* Enable the selected ADC DMA request */
  914. ADCx->CR2 |= (uint32_t)ADC_CR2_DMA;
  915. }
  916. else
  917. {
  918. /* Disable the selected ADC DMA request */
  919. ADCx->CR2 &= (uint32_t)(~ADC_CR2_DMA);
  920. }
  921. }
  922. /**
  923. * @brief Enables or disables the ADC DMA request after last transfer (Single-ADC mode)
  924. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  925. * @param NewState: new state of the selected ADC DMA request after last transfer.
  926. * This parameter can be: ENABLE or DISABLE.
  927. * @retval None
  928. */
  929. void ADC_DMARequestAfterLastTransferCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  930. {
  931. /* Check the parameters */
  932. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  933. assert_param(IS_FUNCTIONAL_STATE(NewState));
  934. if (NewState != DISABLE)
  935. {
  936. /* Enable the selected ADC DMA request after last transfer */
  937. ADCx->CR2 |= (uint32_t)ADC_CR2_DDS;
  938. }
  939. else
  940. {
  941. /* Disable the selected ADC DMA request after last transfer */
  942. ADCx->CR2 &= (uint32_t)(~ADC_CR2_DDS);
  943. }
  944. }
  945. /**
  946. * @brief Enables or disables the ADC DMA request after last transfer in multi ADC mode
  947. * @param NewState: new state of the selected ADC DMA request after last transfer.
  948. * This parameter can be: ENABLE or DISABLE.
  949. * @note if Enabled, DMA requests are issued as long as data are converted and
  950. * DMA mode for multi ADC mode (selected using ADC_CommonInit() function
  951. * by ADC_CommonInitStruct.ADC_DMAAccessMode structure member) is
  952. * ADC_DMAAccessMode_1, ADC_DMAAccessMode_2 or ADC_DMAAccessMode_3.
  953. * @retval None
  954. */
  955. void ADC_MultiModeDMARequestAfterLastTransferCmd(FunctionalState NewState)
  956. {
  957. /* Check the parameters */
  958. assert_param(IS_FUNCTIONAL_STATE(NewState));
  959. if (NewState != DISABLE)
  960. {
  961. /* Enable the selected ADC DMA request after last transfer */
  962. ADC->CCR |= (uint32_t)ADC_CCR_DDS;
  963. }
  964. else
  965. {
  966. /* Disable the selected ADC DMA request after last transfer */
  967. ADC->CCR &= (uint32_t)(~ADC_CCR_DDS);
  968. }
  969. }
  970. /**
  971. * @}
  972. */
  973. /** @defgroup ADC_Group6 Injected channels Configuration functions
  974. * @brief Injected channels Configuration functions
  975. *
  976. @verbatim
  977. ===============================================================================
  978. Injected channels Configuration functions
  979. ===============================================================================
  980. This section provide functions allowing to configure the ADC Injected channels,
  981. it is composed of 2 sub sections :
  982. 1. Configuration functions for Injected channels: This subsection provides
  983. functions allowing to configure the ADC injected channels :
  984. - Configure the rank in the injected group sequencer for each channel
  985. - Configure the sampling time for each channel
  986. - Activate the Auto injected Mode
  987. - Activate the Discontinuous Mode
  988. - scan mode activation
  989. - External/software trigger source
  990. - External trigger edge
  991. - injected channels sequencer.
  992. 2. Get the Specified Injected channel conversion data: This subsection
  993. provides an important function in the ADC peripheral since it returns the
  994. converted data of the specific injected channel.
  995. @endverbatim
  996. * @{
  997. */
  998. /**
  999. * @brief Configures for the selected ADC injected channel its corresponding
  1000. * rank in the sequencer and its sample time.
  1001. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1002. * @param ADC_Channel: the ADC channel to configure.
  1003. * This parameter can be one of the following values:
  1004. * @arg ADC_Channel_0: ADC Channel0 selected
  1005. * @arg ADC_Channel_1: ADC Channel1 selected
  1006. * @arg ADC_Channel_2: ADC Channel2 selected
  1007. * @arg ADC_Channel_3: ADC Channel3 selected
  1008. * @arg ADC_Channel_4: ADC Channel4 selected
  1009. * @arg ADC_Channel_5: ADC Channel5 selected
  1010. * @arg ADC_Channel_6: ADC Channel6 selected
  1011. * @arg ADC_Channel_7: ADC Channel7 selected
  1012. * @arg ADC_Channel_8: ADC Channel8 selected
  1013. * @arg ADC_Channel_9: ADC Channel9 selected
  1014. * @arg ADC_Channel_10: ADC Channel10 selected
  1015. * @arg ADC_Channel_11: ADC Channel11 selected
  1016. * @arg ADC_Channel_12: ADC Channel12 selected
  1017. * @arg ADC_Channel_13: ADC Channel13 selected
  1018. * @arg ADC_Channel_14: ADC Channel14 selected
  1019. * @arg ADC_Channel_15: ADC Channel15 selected
  1020. * @arg ADC_Channel_16: ADC Channel16 selected
  1021. * @arg ADC_Channel_17: ADC Channel17 selected
  1022. * @arg ADC_Channel_18: ADC Channel18 selected
  1023. * @param Rank: The rank in the injected group sequencer.
  1024. * This parameter must be between 1 to 4.
  1025. * @param ADC_SampleTime: The sample time value to be set for the selected channel.
  1026. * This parameter can be one of the following values:
  1027. * @arg ADC_SampleTime_3Cycles: Sample time equal to 3 cycles
  1028. * @arg ADC_SampleTime_15Cycles: Sample time equal to 15 cycles
  1029. * @arg ADC_SampleTime_28Cycles: Sample time equal to 28 cycles
  1030. * @arg ADC_SampleTime_56Cycles: Sample time equal to 56 cycles
  1031. * @arg ADC_SampleTime_84Cycles: Sample time equal to 84 cycles
  1032. * @arg ADC_SampleTime_112Cycles: Sample time equal to 112 cycles
  1033. * @arg ADC_SampleTime_144Cycles: Sample time equal to 144 cycles
  1034. * @arg ADC_SampleTime_480Cycles: Sample time equal to 480 cycles
  1035. * @retval None
  1036. */
  1037. void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
  1038. {
  1039. uint32_t tmpreg1 = 0, tmpreg2 = 0, tmpreg3 = 0;
  1040. /* Check the parameters */
  1041. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1042. assert_param(IS_ADC_CHANNEL(ADC_Channel));
  1043. assert_param(IS_ADC_INJECTED_RANK(Rank));
  1044. assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
  1045. /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
  1046. if (ADC_Channel > ADC_Channel_9)
  1047. {
  1048. /* Get the old register value */
  1049. tmpreg1 = ADCx->SMPR1;
  1050. /* Calculate the mask to clear */
  1051. tmpreg2 = SMPR1_SMP_SET << (3*(ADC_Channel - 10));
  1052. /* Clear the old sample time */
  1053. tmpreg1 &= ~tmpreg2;
  1054. /* Calculate the mask to set */
  1055. tmpreg2 = (uint32_t)ADC_SampleTime << (3*(ADC_Channel - 10));
  1056. /* Set the new sample time */
  1057. tmpreg1 |= tmpreg2;
  1058. /* Store the new register value */
  1059. ADCx->SMPR1 = tmpreg1;
  1060. }
  1061. else /* ADC_Channel include in ADC_Channel_[0..9] */
  1062. {
  1063. /* Get the old register value */
  1064. tmpreg1 = ADCx->SMPR2;
  1065. /* Calculate the mask to clear */
  1066. tmpreg2 = SMPR2_SMP_SET << (3 * ADC_Channel);
  1067. /* Clear the old sample time */
  1068. tmpreg1 &= ~tmpreg2;
  1069. /* Calculate the mask to set */
  1070. tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
  1071. /* Set the new sample time */
  1072. tmpreg1 |= tmpreg2;
  1073. /* Store the new register value */
  1074. ADCx->SMPR2 = tmpreg1;
  1075. }
  1076. /* Rank configuration */
  1077. /* Get the old register value */
  1078. tmpreg1 = ADCx->JSQR;
  1079. /* Get JL value: Number = JL+1 */
  1080. tmpreg3 = (tmpreg1 & JSQR_JL_SET)>> 20;
  1081. /* Calculate the mask to clear: ((Rank-1)+(4-JL-1)) */
  1082. tmpreg2 = JSQR_JSQ_SET << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
  1083. /* Clear the old JSQx bits for the selected rank */
  1084. tmpreg1 &= ~tmpreg2;
  1085. /* Calculate the mask to set: ((Rank-1)+(4-JL-1)) */
  1086. tmpreg2 = (uint32_t)ADC_Channel << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
  1087. /* Set the JSQx bits for the selected rank */
  1088. tmpreg1 |= tmpreg2;
  1089. /* Store the new register value */
  1090. ADCx->JSQR = tmpreg1;
  1091. }
  1092. /**
  1093. * @brief Configures the sequencer length for injected channels
  1094. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1095. * @param Length: The sequencer length.
  1096. * This parameter must be a number between 1 to 4.
  1097. * @retval None
  1098. */
  1099. void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length)
  1100. {
  1101. uint32_t tmpreg1 = 0;
  1102. uint32_t tmpreg2 = 0;
  1103. /* Check the parameters */
  1104. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1105. assert_param(IS_ADC_INJECTED_LENGTH(Length));
  1106. /* Get the old register value */
  1107. tmpreg1 = ADCx->JSQR;
  1108. /* Clear the old injected sequence length JL bits */
  1109. tmpreg1 &= JSQR_JL_RESET;
  1110. /* Set the injected sequence length JL bits */
  1111. tmpreg2 = Length - 1;
  1112. tmpreg1 |= tmpreg2 << 20;
  1113. /* Store the new register value */
  1114. ADCx->JSQR = tmpreg1;
  1115. }
  1116. /**
  1117. * @brief Set the injected channels conversion value offset
  1118. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1119. * @param ADC_InjectedChannel: the ADC injected channel to set its offset.
  1120. * This parameter can be one of the following values:
  1121. * @arg ADC_InjectedChannel_1: Injected Channel1 selected
  1122. * @arg ADC_InjectedChannel_2: Injected Channel2 selected
  1123. * @arg ADC_InjectedChannel_3: Injected Channel3 selected
  1124. * @arg ADC_InjectedChannel_4: Injected Channel4 selected
  1125. * @param Offset: the offset value for the selected ADC injected channel
  1126. * This parameter must be a 12bit value.
  1127. * @retval None
  1128. */
  1129. void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset)
  1130. {
  1131. __IO uint32_t tmp = 0;
  1132. /* Check the parameters */
  1133. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1134. assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
  1135. assert_param(IS_ADC_OFFSET(Offset));
  1136. tmp = (uint32_t)ADCx;
  1137. tmp += ADC_InjectedChannel;
  1138. /* Set the selected injected channel data offset */
  1139. *(__IO uint32_t *) tmp = (uint32_t)Offset;
  1140. }
  1141. /**
  1142. * @brief Configures the ADCx external trigger for injected channels conversion.
  1143. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1144. * @param ADC_ExternalTrigInjecConv: specifies the ADC trigger to start injected conversion.
  1145. * This parameter can be one of the following values:
  1146. * @arg ADC_ExternalTrigInjecConv_T1_CC4: Timer1 capture compare4 selected
  1147. * @arg ADC_ExternalTrigInjecConv_T1_TRGO: Timer1 TRGO event selected
  1148. * @arg ADC_ExternalTrigInjecConv_T2_CC1: Timer2 capture compare1 selected
  1149. * @arg ADC_ExternalTrigInjecConv_T2_TRGO: Timer2 TRGO event selected
  1150. * @arg ADC_ExternalTrigInjecConv_T3_CC2: Timer3 capture compare2 selected
  1151. * @arg ADC_ExternalTrigInjecConv_T3_CC4: Timer3 capture compare4 selected
  1152. * @arg ADC_ExternalTrigInjecConv_T4_CC1: Timer4 capture compare1 selected
  1153. * @arg ADC_ExternalTrigInjecConv_T4_CC2: Timer4 capture compare2 selected
  1154. * @arg ADC_ExternalTrigInjecConv_T4_CC3: Timer4 capture compare3 selected
  1155. * @arg ADC_ExternalTrigInjecConv_T4_TRGO: Timer4 TRGO event selected
  1156. * @arg ADC_ExternalTrigInjecConv_T5_CC4: Timer5 capture compare4 selected
  1157. * @arg ADC_ExternalTrigInjecConv_T5_TRGO: Timer5 TRGO event selected
  1158. * @arg ADC_ExternalTrigInjecConv_T8_CC2: Timer8 capture compare2 selected
  1159. * @arg ADC_ExternalTrigInjecConv_T8_CC3: Timer8 capture compare3 selected
  1160. * @arg ADC_ExternalTrigInjecConv_T8_CC4: Timer8 capture compare4 selected
  1161. * @arg ADC_ExternalTrigInjecConv_Ext_IT15: External interrupt line 15 event selected
  1162. * @retval None
  1163. */
  1164. void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv)
  1165. {
  1166. uint32_t tmpreg = 0;
  1167. /* Check the parameters */
  1168. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1169. assert_param(IS_ADC_EXT_INJEC_TRIG(ADC_ExternalTrigInjecConv));
  1170. /* Get the old register value */
  1171. tmpreg = ADCx->CR2;
  1172. /* Clear the old external event selection for injected group */
  1173. tmpreg &= CR2_JEXTSEL_RESET;
  1174. /* Set the external event selection for injected group */
  1175. tmpreg |= ADC_ExternalTrigInjecConv;
  1176. /* Store the new register value */
  1177. ADCx->CR2 = tmpreg;
  1178. }
  1179. /**
  1180. * @brief Configures the ADCx external trigger edge for injected channels conversion.
  1181. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1182. * @param ADC_ExternalTrigInjecConvEdge: specifies the ADC external trigger edge
  1183. * to start injected conversion.
  1184. * This parameter can be one of the following values:
  1185. * @arg ADC_ExternalTrigInjecConvEdge_None: external trigger disabled for
  1186. * injected conversion
  1187. * @arg ADC_ExternalTrigInjecConvEdge_Rising: detection on rising edge
  1188. * @arg ADC_ExternalTrigInjecConvEdge_Falling: detection on falling edge
  1189. * @arg ADC_ExternalTrigInjecConvEdge_RisingFalling: detection on both rising
  1190. * and falling edge
  1191. * @retval None
  1192. */
  1193. void ADC_ExternalTrigInjectedConvEdgeConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConvEdge)
  1194. {
  1195. uint32_t tmpreg = 0;
  1196. /* Check the parameters */
  1197. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1198. assert_param(IS_ADC_EXT_INJEC_TRIG_EDGE(ADC_ExternalTrigInjecConvEdge));
  1199. /* Get the old register value */
  1200. tmpreg = ADCx->CR2;
  1201. /* Clear the old external trigger edge for injected group */
  1202. tmpreg &= CR2_JEXTEN_RESET;
  1203. /* Set the new external trigger edge for injected group */
  1204. tmpreg |= ADC_ExternalTrigInjecConvEdge;
  1205. /* Store the new register value */
  1206. ADCx->CR2 = tmpreg;
  1207. }
  1208. /**
  1209. * @brief Enables the selected ADC software start conversion of the injected channels.
  1210. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1211. * @retval None
  1212. */
  1213. void ADC_SoftwareStartInjectedConv(ADC_TypeDef* ADCx)
  1214. {
  1215. /* Check the parameters */
  1216. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1217. /* Enable the selected ADC conversion for injected group */
  1218. ADCx->CR2 |= (uint32_t)ADC_CR2_JSWSTART;
  1219. }
  1220. /**
  1221. * @brief Gets the selected ADC Software start injected conversion Status.
  1222. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1223. * @retval The new state of ADC software start injected conversion (SET or RESET).
  1224. */
  1225. FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx)
  1226. {
  1227. FlagStatus bitstatus = RESET;
  1228. /* Check the parameters */
  1229. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1230. /* Check the status of JSWSTART bit */
  1231. if ((ADCx->CR2 & ADC_CR2_JSWSTART) != (uint32_t)RESET)
  1232. {
  1233. /* JSWSTART bit is set */
  1234. bitstatus = SET;
  1235. }
  1236. else
  1237. {
  1238. /* JSWSTART bit is reset */
  1239. bitstatus = RESET;
  1240. }
  1241. /* Return the JSWSTART bit status */
  1242. return bitstatus;
  1243. }
  1244. /**
  1245. * @brief Enables or disables the selected ADC automatic injected group
  1246. * conversion after regular one.
  1247. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1248. * @param NewState: new state of the selected ADC auto injected conversion
  1249. * This parameter can be: ENABLE or DISABLE.
  1250. * @retval None
  1251. */
  1252. void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  1253. {
  1254. /* Check the parameters */
  1255. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1256. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1257. if (NewState != DISABLE)
  1258. {
  1259. /* Enable the selected ADC automatic injected group conversion */
  1260. ADCx->CR1 |= (uint32_t)ADC_CR1_JAUTO;
  1261. }
  1262. else
  1263. {
  1264. /* Disable the selected ADC automatic injected group conversion */
  1265. ADCx->CR1 &= (uint32_t)(~ADC_CR1_JAUTO);
  1266. }
  1267. }
  1268. /**
  1269. * @brief Enables or disables the discontinuous mode for injected group
  1270. * channel for the specified ADC
  1271. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1272. * @param NewState: new state of the selected ADC discontinuous mode on injected
  1273. * group channel.
  1274. * This parameter can be: ENABLE or DISABLE.
  1275. * @retval None
  1276. */
  1277. void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  1278. {
  1279. /* Check the parameters */
  1280. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1281. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1282. if (NewState != DISABLE)
  1283. {
  1284. /* Enable the selected ADC injected discontinuous mode */
  1285. ADCx->CR1 |= (uint32_t)ADC_CR1_JDISCEN;
  1286. }
  1287. else
  1288. {
  1289. /* Disable the selected ADC injected discontinuous mode */
  1290. ADCx->CR1 &= (uint32_t)(~ADC_CR1_JDISCEN);
  1291. }
  1292. }
  1293. /**
  1294. * @brief Returns the ADC injected channel conversion result
  1295. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1296. * @param ADC_InjectedChannel: the converted ADC injected channel.
  1297. * This parameter can be one of the following values:
  1298. * @arg ADC_InjectedChannel_1: Injected Channel1 selected
  1299. * @arg ADC_InjectedChannel_2: Injected Channel2 selected
  1300. * @arg ADC_InjectedChannel_3: Injected Channel3 selected
  1301. * @arg ADC_InjectedChannel_4: Injected Channel4 selected
  1302. * @retval The Data conversion value.
  1303. */
  1304. uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel)
  1305. {
  1306. __IO uint32_t tmp = 0;
  1307. /* Check the parameters */
  1308. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1309. assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
  1310. tmp = (uint32_t)ADCx;
  1311. tmp += ADC_InjectedChannel + JDR_OFFSET;
  1312. /* Returns the selected injected channel conversion data value */
  1313. return (uint16_t) (*(__IO uint32_t*) tmp);
  1314. }
  1315. /**
  1316. * @}
  1317. */
  1318. /** @defgroup ADC_Group7 Interrupts and flags management functions
  1319. * @brief Interrupts and flags management functions
  1320. *
  1321. @verbatim
  1322. ===============================================================================
  1323. Interrupts and flags management functions
  1324. ===============================================================================
  1325. This section provides functions allowing to configure the ADC Interrupts and
  1326. to get the status and clear flags and Interrupts pending bits.
  1327. Each ADC provides 4 Interrupts sources and 6 Flags which can be divided into
  1328. 3 groups:
  1329. I. Flags and Interrupts for ADC regular channels
  1330. =================================================
  1331. Flags :
  1332. ----------
  1333. 1. ADC_FLAG_OVR : Overrun detection when regular converted data are lost
  1334. 2. ADC_FLAG_EOC : Regular channel end of conversion ==> to indicate (depending
  1335. on EOCS bit, managed by ADC_EOCOnEachRegularChannelCmd() ) the end of:
  1336. ==> a regular CHANNEL conversion
  1337. ==> sequence of regular GROUP conversions .
  1338. 3. ADC_FLAG_STRT: Regular channel start ==> to indicate when regular CHANNEL
  1339. conversion starts.
  1340. Interrupts :
  1341. ------------
  1342. 1. ADC_IT_OVR : specifies the interrupt source for Overrun detection event.
  1343. 2. ADC_IT_EOC : specifies the interrupt source for Regular channel end of
  1344. conversion event.
  1345. II. Flags and Interrupts for ADC Injected channels
  1346. =================================================
  1347. Flags :
  1348. ----------
  1349. 1. ADC_FLAG_JEOC : Injected channel end of conversion ==> to indicate at
  1350. the end of injected GROUP conversion
  1351. 2. ADC_FLAG_JSTRT: Injected channel start ==> to indicate hardware when
  1352. injected GROUP conversion starts.
  1353. Interrupts :
  1354. ------------
  1355. 1. ADC_IT_JEOC : specifies the interrupt source for Injected channel end of
  1356. conversion event.
  1357. III. General Flags and Interrupts for the ADC
  1358. =================================================
  1359. Flags :
  1360. ----------
  1361. 1. ADC_FLAG_AWD: Analog watchdog ==> to indicate if the converted voltage
  1362. crosses the programmed thresholds values.
  1363. Interrupts :
  1364. ------------
  1365. 1. ADC_IT_AWD : specifies the interrupt source for Analog watchdog event.
  1366. The user should identify which mode will be used in his application to manage
  1367. the ADC controller events: Polling mode or Interrupt mode.
  1368. In the Polling Mode it is advised to use the following functions:
  1369. - ADC_GetFlagStatus() : to check if flags events occur.
  1370. - ADC_ClearFlag() : to clear the flags events.
  1371. In the Interrupt Mode it is advised to use the following functions:
  1372. - ADC_ITConfig() : to enable or disable the interrupt source.
  1373. - ADC_GetITStatus() : to check if Interrupt occurs.
  1374. - ADC_ClearITPendingBit() : to clear the Interrupt pending Bit
  1375. (corresponding Flag).
  1376. @endverbatim
  1377. * @{
  1378. */
  1379. /**
  1380. * @brief Enables or disables the specified ADC interrupts.
  1381. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1382. * @param ADC_IT: specifies the ADC interrupt sources to be enabled or disabled.
  1383. * This parameter can be one of the following values:
  1384. * @arg ADC_IT_EOC: End of conversion interrupt mask
  1385. * @arg ADC_IT_AWD: Analog watchdog interrupt mask
  1386. * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
  1387. * @arg ADC_IT_OVR: Overrun interrupt enable
  1388. * @param NewState: new state of the specified ADC interrupts.
  1389. * This parameter can be: ENABLE or DISABLE.
  1390. * @retval None
  1391. */
  1392. void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)
  1393. {
  1394. uint32_t itmask = 0;
  1395. /* Check the parameters */
  1396. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1397. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1398. assert_param(IS_ADC_IT(ADC_IT));
  1399. /* Get the ADC IT index */
  1400. itmask = (uint8_t)ADC_IT;
  1401. itmask = (uint32_t)0x01 << itmask;
  1402. if (NewState != DISABLE)
  1403. {
  1404. /* Enable the selected ADC interrupts */
  1405. ADCx->CR1 |= itmask;
  1406. }
  1407. else
  1408. {
  1409. /* Disable the selected ADC interrupts */
  1410. ADCx->CR1 &= (~(uint32_t)itmask);
  1411. }
  1412. }
  1413. /**
  1414. * @brief Checks whether the specified ADC flag is set or not.
  1415. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1416. * @param ADC_FLAG: specifies the flag to check.
  1417. * This parameter can be one of the following values:
  1418. * @arg ADC_FLAG_AWD: Analog watchdog flag
  1419. * @arg ADC_FLAG_EOC: End of conversion flag
  1420. * @arg ADC_FLAG_JEOC: End of injected group conversion flag
  1421. * @arg ADC_FLAG_JSTRT: Start of injected group conversion flag
  1422. * @arg ADC_FLAG_STRT: Start of regular group conversion flag
  1423. * @arg ADC_FLAG_OVR: Overrun flag
  1424. * @retval The new state of ADC_FLAG (SET or RESET).
  1425. */
  1426. FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
  1427. {
  1428. FlagStatus bitstatus = RESET;
  1429. /* Check the parameters */
  1430. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1431. assert_param(IS_ADC_GET_FLAG(ADC_FLAG));
  1432. /* Check the status of the specified ADC flag */
  1433. if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET)
  1434. {
  1435. /* ADC_FLAG is set */
  1436. bitstatus = SET;
  1437. }
  1438. else
  1439. {
  1440. /* ADC_FLAG is reset */
  1441. bitstatus = RESET;
  1442. }
  1443. /* Return the ADC_FLAG status */
  1444. return bitstatus;
  1445. }
  1446. /**
  1447. * @brief Clears the ADCx's pending flags.
  1448. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1449. * @param ADC_FLAG: specifies the flag to clear.
  1450. * This parameter can be any combination of the following values:
  1451. * @arg ADC_FLAG_AWD: Analog watchdog flag
  1452. * @arg ADC_FLAG_EOC: End of conversion flag
  1453. * @arg ADC_FLAG_JEOC: End of injected group conversion flag
  1454. * @arg ADC_FLAG_JSTRT: Start of injected group conversion flag
  1455. * @arg ADC_FLAG_STRT: Start of regular group conversion flag
  1456. * @arg ADC_FLAG_OVR: Overrun flag
  1457. * @retval None
  1458. */
  1459. void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
  1460. {
  1461. /* Check the parameters */
  1462. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1463. assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));
  1464. /* Clear the selected ADC flags */
  1465. ADCx->SR = ~(uint32_t)ADC_FLAG;
  1466. }
  1467. /**
  1468. * @brief Checks whether the specified ADC interrupt has occurred or not.
  1469. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1470. * @param ADC_IT: specifies the ADC interrupt source to check.
  1471. * This parameter can be one of the following values:
  1472. * @arg ADC_IT_EOC: End of conversion interrupt mask
  1473. * @arg ADC_IT_AWD: Analog watchdog interrupt mask
  1474. * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
  1475. * @arg ADC_IT_OVR: Overrun interrupt mask
  1476. * @retval The new state of ADC_IT (SET or RESET).
  1477. */
  1478. ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT)
  1479. {
  1480. ITStatus bitstatus = RESET;
  1481. uint32_t itmask = 0, enablestatus = 0;
  1482. /* Check the parameters */
  1483. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1484. assert_param(IS_ADC_IT(ADC_IT));
  1485. /* Get the ADC IT index */
  1486. itmask = ADC_IT >> 8;
  1487. /* Get the ADC_IT enable bit status */
  1488. enablestatus = (ADCx->CR1 & ((uint32_t)0x01 << (uint8_t)ADC_IT)) ;
  1489. /* Check the status of the specified ADC interrupt */
  1490. if (((ADCx->SR & itmask) != (uint32_t)RESET) && enablestatus)
  1491. {
  1492. /* ADC_IT is set */
  1493. bitstatus = SET;
  1494. }
  1495. else
  1496. {
  1497. /* ADC_IT is reset */
  1498. bitstatus = RESET;
  1499. }
  1500. /* Return the ADC_IT status */
  1501. return bitstatus;
  1502. }
  1503. /**
  1504. * @brief Clears the ADCx's interrupt pending bits.
  1505. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
  1506. * @param ADC_IT: specifies the ADC interrupt pending bit to clear.
  1507. * This parameter can be one of the following values:
  1508. * @arg ADC_IT_EOC: End of conversion interrupt mask
  1509. * @arg ADC_IT_AWD: Analog watchdog interrupt mask
  1510. * @arg ADC_IT_JEOC: End of injected conversion interrupt mask
  1511. * @arg ADC_IT_OVR: Overrun interrupt mask
  1512. * @retval None
  1513. */
  1514. void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT)
  1515. {
  1516. uint8_t itmask = 0;
  1517. /* Check the parameters */
  1518. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1519. assert_param(IS_ADC_IT(ADC_IT));
  1520. /* Get the ADC IT index */
  1521. itmask = (uint8_t)(ADC_IT >> 8);
  1522. /* Clear the selected ADC interrupt pending bits */
  1523. ADCx->SR = ~(uint32_t)itmask;
  1524. }
  1525. /**
  1526. * @}
  1527. */
  1528. /**
  1529. * @}
  1530. */
  1531. /**
  1532. * @}
  1533. */
  1534. /**
  1535. * @}
  1536. */