stm32f0xx_adc.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_adc.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 05-December-2014
  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
  10. * + Power saving
  11. * + Analog Watchdog configuration
  12. * + Temperature Sensor, Vrefint (Internal Reference Voltage) and
  13. * Vbat (Voltage battery) management
  14. * + ADC Channels Configuration
  15. * + ADC Channels DMA Configuration
  16. * + Interrupts and flags management
  17. *
  18. * @verbatim
  19. ================================================================================
  20. ##### How to use this driver #####
  21. ================================================================================
  22. [..]
  23. (#) Enable the ADC interface clock using
  24. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  25. (#) ADC pins configuration
  26. (++) Enable the clock for the ADC GPIOs using the following function:
  27. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOx, ENABLE);
  28. (++) Configure these ADC pins in analog mode using GPIO_Init();
  29. (#) Configure the ADC conversion resolution, data alignment, external
  30. trigger and edge, scan direction and Enable/Disable the continuous mode
  31. using the ADC_Init() function.
  32. (#) Activate the ADC peripheral using ADC_Cmd() function.
  33. *** ADC channels group configuration ***
  34. ============================================
  35. [..]
  36. (+) To configure the ADC channels features, use ADC_Init() and
  37. ADC_ChannelConfig() functions.
  38. (+) To activate the continuous mode, use the ADC_ContinuousModeCmd()
  39. function.
  40. (+) To activate the Discontinuous mode, use the ADC_DiscModeCmd() functions.
  41. (+) To activate the overrun mode, use the ADC_OverrunModeCmd() functions.
  42. (+) To activate the calibration mode, use the ADC_GetCalibrationFactor() functions.
  43. (+) To read the ADC converted values, use the ADC_GetConversionValue()
  44. function.
  45. *** DMA for ADC channels features configuration ***
  46. =============================================================
  47. [..]
  48. (+) To enable the DMA mode for ADC channels group, use the ADC_DMACmd() function.
  49. (+) To configure the DMA transfer request, use ADC_DMARequestModeConfig() function.
  50. * @endverbatim
  51. *
  52. ******************************************************************************
  53. * @attention
  54. *
  55. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  56. *
  57. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  58. * You may not use this file except in compliance with the License.
  59. * You may obtain a copy of the License at:
  60. *
  61. * http://www.st.com/software_license_agreement_liberty_v2
  62. *
  63. * Unless required by applicable law or agreed to in writing, software
  64. * distributed under the License is distributed on an "AS IS" BASIS,
  65. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  66. * See the License for the specific language governing permissions and
  67. * limitations under the License.
  68. *
  69. ******************************************************************************
  70. */
  71. /* Includes ------------------------------------------------------------------*/
  72. #include "stm32f0xx_adc.h"
  73. #include "stm32f0xx_rcc.h"
  74. /** @addtogroup STM32F0xx_StdPeriph_Driver
  75. * @{
  76. */
  77. /** @defgroup ADC
  78. * @brief ADC driver modules
  79. * @{
  80. */
  81. /* Private typedef -----------------------------------------------------------*/
  82. /* Private define ------------------------------------------------------------*/
  83. /* ADC CFGR mask */
  84. #define CFGR1_CLEAR_MASK ((uint32_t)0xFFFFD203)
  85. /* Calibration time out */
  86. #define CALIBRATION_TIMEOUT ((uint32_t)0x0000F000)
  87. /* Private macro -------------------------------------------------------------*/
  88. /* Private variables ---------------------------------------------------------*/
  89. /* Private function prototypes -----------------------------------------------*/
  90. /* Private functions ---------------------------------------------------------*/
  91. /** @defgroup ADC_Private_Functions
  92. * @{
  93. */
  94. /** @defgroup ADC_Group1 Initialization and Configuration functions
  95. * @brief Initialization and Configuration functions
  96. *
  97. @verbatim
  98. ===============================================================================
  99. ##### Initialization and Configuration functions #####
  100. ===============================================================================
  101. [..] This section provides functions allowing to:
  102. (+) Initialize and configure the ADC Prescaler
  103. (+) ADC Conversion Resolution (12bit..6bit)
  104. (+) ADC Continuous Conversion Mode (Continuous or Single conversion)
  105. (+) External trigger Edge and source
  106. (+) Converted data alignment (left or right)
  107. (+) The direction in which the channels will be scanned in the sequence
  108. (+) Enable or disable the ADC peripheral
  109. @endverbatim
  110. * @{
  111. */
  112. /**
  113. * @brief Deinitializes ADC1 peripheral registers to their default reset values.
  114. * @param ADCx: where x can be 1 to select the ADC peripheral.
  115. * @retval None
  116. */
  117. void ADC_DeInit(ADC_TypeDef* ADCx)
  118. {
  119. /* Check the parameters */
  120. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  121. if(ADCx == ADC1)
  122. {
  123. /* Enable ADC1 reset state */
  124. RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
  125. /* Release ADC1 from reset state */
  126. RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);
  127. }
  128. }
  129. /**
  130. * @brief Initializes the ADCx peripheral according to the specified parameters
  131. * in the ADC_InitStruct.
  132. * @note This function is used to configure the global features of the ADC (
  133. * Resolution, Data Alignment, continuous mode activation, External
  134. * trigger source and edge, Sequence Scan Direction).
  135. * @param ADCx: where x can be 1 to select the ADC peripheral.
  136. * @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure that contains
  137. * the configuration information for the specified ADC peripheral.
  138. * @retval None
  139. */
  140. void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
  141. {
  142. uint32_t tmpreg = 0;
  143. /* Check the parameters */
  144. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  145. assert_param(IS_ADC_RESOLUTION(ADC_InitStruct->ADC_Resolution));
  146. assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode));
  147. assert_param(IS_ADC_EXT_TRIG_EDGE(ADC_InitStruct->ADC_ExternalTrigConvEdge));
  148. assert_param(IS_ADC_EXTERNAL_TRIG_CONV(ADC_InitStruct->ADC_ExternalTrigConv));
  149. assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign));
  150. assert_param(IS_ADC_SCAN_DIRECTION(ADC_InitStruct->ADC_ScanDirection));
  151. /* Get the ADCx CFGR value */
  152. tmpreg = ADCx->CFGR1;
  153. /* Clear SCANDIR, RES[1:0], ALIGN, EXTSEL[2:0], EXTEN[1:0] and CONT bits */
  154. tmpreg &= CFGR1_CLEAR_MASK;
  155. /*---------------------------- ADCx CFGR Configuration ---------------------*/
  156. /* Set RES[1:0] bits according to ADC_Resolution value */
  157. /* Set CONT bit according to ADC_ContinuousConvMode value */
  158. /* Set EXTEN[1:0] bits according to ADC_ExternalTrigConvEdge value */
  159. /* Set EXTSEL[2:0] bits according to ADC_ExternalTrigConv value */
  160. /* Set ALIGN bit according to ADC_DataAlign value */
  161. /* Set SCANDIR bit according to ADC_ScanDirection value */
  162. tmpreg |= (uint32_t)(ADC_InitStruct->ADC_Resolution | ((uint32_t)(ADC_InitStruct->ADC_ContinuousConvMode) << 13) |
  163. ADC_InitStruct->ADC_ExternalTrigConvEdge | ADC_InitStruct->ADC_ExternalTrigConv |
  164. ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ScanDirection);
  165. /* Write to ADCx CFGR */
  166. ADCx->CFGR1 = tmpreg;
  167. }
  168. /**
  169. * @brief Fills each ADC_InitStruct member with its default value.
  170. * @note This function is used to initialize the global features of the ADC (
  171. * Resolution, Data Alignment, continuous mode activation, External
  172. * trigger source and edge, Sequence Scan Direction).
  173. * @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure which will
  174. * be initialized.
  175. * @retval None
  176. */
  177. void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
  178. {
  179. /* Reset ADC init structure parameters values */
  180. /* Initialize the ADC_Resolution member */
  181. ADC_InitStruct->ADC_Resolution = ADC_Resolution_12b;
  182. /* Initialize the ADC_ContinuousConvMode member */
  183. ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;
  184. /* Initialize the ADC_ExternalTrigConvEdge member */
  185. ADC_InitStruct->ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  186. /* Initialize the ADC_ExternalTrigConv member */
  187. ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO;
  188. /* Initialize the ADC_DataAlign member */
  189. ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;
  190. /* Initialize the ADC_ScanDirection member */
  191. ADC_InitStruct->ADC_ScanDirection = ADC_ScanDirection_Upward;
  192. }
  193. /**
  194. * @brief Enables or disables the specified ADC peripheral.
  195. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  196. * @param NewState: new state of the ADCx peripheral.
  197. * This parameter can be: ENABLE or DISABLE.
  198. * @retval None
  199. */
  200. void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  201. {
  202. /* Check the parameters */
  203. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  204. assert_param(IS_FUNCTIONAL_STATE(NewState));
  205. if (NewState != DISABLE)
  206. {
  207. /* Set the ADEN bit to Enable the ADC peripheral */
  208. ADCx->CR |= (uint32_t)ADC_CR_ADEN;
  209. }
  210. else
  211. {
  212. /* Set the ADDIS to Disable the ADC peripheral */
  213. ADCx->CR |= (uint32_t)ADC_CR_ADDIS;
  214. }
  215. }
  216. /**
  217. * @brief Configure the ADC to either be clocked by the asynchronous clock(which is
  218. * independent, the dedicated 14MHz clock) or the synchronous clock derived from
  219. * the APB clock of the ADC bus interface divided by 2 or 4
  220. * @note This function can be called only when ADC is disabled.
  221. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  222. * @param ADC_ClockMode: This parameter can be :
  223. * @arg ADC_ClockMode_AsynClk: ADC clocked by the dedicated 14MHz clock
  224. * @arg ADC_ClockMode_SynClkDiv2: ADC clocked by PCLK/2
  225. * @arg ADC_ClockMode_SynClkDiv4: ADC clocked by PCLK/4
  226. * @retval None
  227. */
  228. void ADC_ClockModeConfig(ADC_TypeDef* ADCx, uint32_t ADC_ClockMode)
  229. {
  230. /* Check the parameters */
  231. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  232. assert_param(IS_ADC_CLOCKMODE(ADC_ClockMode));
  233. /* Configure the ADC Clock mode according to ADC_ClockMode */
  234. ADCx->CFGR2 = (uint32_t)ADC_ClockMode;
  235. }
  236. /**
  237. * @brief Enables or disables the jitter when the ADC is clocked by PCLK div2
  238. * or div4
  239. * @note This function is obsolete and maintained for legacy purpose only. ADC_ClockModeConfig()
  240. * function should be used instead.
  241. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  242. * @param ADC_JitterOff: This parameter can be :
  243. * @arg ADC_JitterOff_PCLKDiv2: Remove jitter when ADC is clocked by PLCK divided by 2
  244. * @arg ADC_JitterOff_PCLKDiv4: Remove jitter when ADC is clocked by PLCK divided by 4
  245. * @param NewState: new state of the ADCx jitter.
  246. * This parameter can be: ENABLE or DISABLE.
  247. * @retval None
  248. */
  249. void ADC_JitterCmd(ADC_TypeDef* ADCx, uint32_t ADC_JitterOff, FunctionalState NewState)
  250. {
  251. /* Check the parameters */
  252. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  253. assert_param(IS_ADC_JITTEROFF(ADC_JitterOff));
  254. assert_param(IS_FUNCTIONAL_STATE(NewState));
  255. if (NewState != DISABLE)
  256. {
  257. /* Disable Jitter */
  258. ADCx->CFGR2 |= (uint32_t)ADC_JitterOff;
  259. }
  260. else
  261. {
  262. /* Enable Jitter */
  263. ADCx->CFGR2 &= (uint32_t)(~ADC_JitterOff);
  264. }
  265. }
  266. /**
  267. * @}
  268. */
  269. /** @defgroup ADC_Group2 Power saving functions
  270. * @brief Power saving functions
  271. *
  272. @verbatim
  273. ===============================================================================
  274. ##### Power saving functions #####
  275. ===============================================================================
  276. [..] This section provides functions allowing to reduce power consumption.
  277. [..] The two function must be combined to get the maximal benefits:
  278. When the ADC frequency is higher than the CPU one, it is recommended to
  279. (#) Enable the Auto Delayed Conversion mode :
  280. ==> using ADC_WaitModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  281. (#) Enable the power off in Delay phases :
  282. ==> using ADC_AutoPowerOffCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  283. @endverbatim
  284. * @{
  285. */
  286. /**
  287. * @brief Enables or disables the ADC Power Off.
  288. * @note ADC power-on and power-off can be managed by hardware to cut the
  289. * consumption when the ADC is not converting.
  290. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  291. * @note The ADC can be powered down:
  292. * - During the Auto delay phase: The ADC is powered on again at the end
  293. * of the delay (until the previous data is read from the ADC data register).
  294. * - During the ADC is waiting for a trigger event: The ADC is powered up
  295. * at the next trigger event (when the conversion is started).
  296. * @param NewState: new state of the ADCx power Off.
  297. * This parameter can be: ENABLE or DISABLE.
  298. * @retval None
  299. */
  300. void ADC_AutoPowerOffCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  301. {
  302. /* Check the parameters */
  303. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  304. assert_param(IS_FUNCTIONAL_STATE(NewState));
  305. if (NewState != DISABLE)
  306. {
  307. /* Enable the ADC Automatic Power-Off */
  308. ADCx->CFGR1 |= ADC_CFGR1_AUTOFF;
  309. }
  310. else
  311. {
  312. /* Disable the ADC Automatic Power-Off */
  313. ADCx->CFGR1 &= (uint32_t)~ADC_CFGR1_AUTOFF;
  314. }
  315. }
  316. /**
  317. * @brief Enables or disables the Wait conversion mode.
  318. * @note When the CPU clock is not fast enough to manage the data rate, a
  319. * Hardware delay can be introduced between ADC conversions to reduce
  320. * this data rate.
  321. * @note The Hardware delay is inserted after each conversions and until the
  322. * previous data is read from the ADC data register
  323. * @note This is a way to automatically adapt the speed of the ADC to the speed
  324. * of the system which will read the data.
  325. * @note Any hardware triggers wich occur while a conversion is on going or
  326. * while the automatic Delay is applied are ignored
  327. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  328. * @param NewState: new state of the ADCx Auto-Delay.
  329. * This parameter can be: ENABLE or DISABLE.
  330. * @retval None
  331. */
  332. void ADC_WaitModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  333. {
  334. /* Check the parameters */
  335. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  336. assert_param(IS_FUNCTIONAL_STATE(NewState));
  337. if (NewState != DISABLE)
  338. {
  339. /* Enable the ADC Automatic Delayed conversion */
  340. ADCx->CFGR1 |= ADC_CFGR1_WAIT;
  341. }
  342. else
  343. {
  344. /* Disable the ADC Automatic Delayed conversion */
  345. ADCx->CFGR1 &= (uint32_t)~ADC_CFGR1_WAIT;
  346. }
  347. }
  348. /**
  349. * @}
  350. */
  351. /** @defgroup ADC_Group3 Analog Watchdog configuration functions
  352. * @brief Analog Watchdog configuration functions
  353. *
  354. @verbatim
  355. ===============================================================================
  356. ##### Analog Watchdog configuration functions #####
  357. ===============================================================================
  358. [..] This section provides functions allowing to configure the Analog Watchdog
  359. (AWD) feature in the ADC.
  360. [..] A typical configuration Analog Watchdog is done following these steps :
  361. (#) the ADC guarded channel(s) is (are) selected using the
  362. ADC_AnalogWatchdogSingleChannelConfig() function.
  363. (#) The Analog watchdog lower and higher threshold are configured using the
  364. ADC_AnalogWatchdogThresholdsConfig() function.
  365. (#) The Analog watchdog is enabled and configured to enable the check, on one
  366. or more channels, using the ADC_AnalogWatchdogCmd() function.
  367. (#) Enable the analog watchdog on the selected channel using
  368. ADC_AnalogWatchdogSingleChannelCmd() function
  369. @endverbatim
  370. * @{
  371. */
  372. /**
  373. * @brief Enables or disables the analog watchdog
  374. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  375. * @param NewState: new state of the ADCx Analog Watchdog.
  376. * This parameter can be: ENABLE or DISABLE.
  377. * @retval None
  378. */
  379. void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  380. {
  381. /* Check the parameters */
  382. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  383. assert_param(IS_FUNCTIONAL_STATE(NewState));
  384. if (NewState != DISABLE)
  385. {
  386. /* Enable the ADC Analog Watchdog */
  387. ADCx->CFGR1 |= ADC_CFGR1_AWDEN;
  388. }
  389. else
  390. {
  391. /* Disable the ADC Analog Watchdog */
  392. ADCx->CFGR1 &= (uint32_t)~ADC_CFGR1_AWDEN;
  393. }
  394. }
  395. /**
  396. * @brief Configures the high and low thresholds of the analog watchdog.
  397. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  398. * @param HighThreshold: the ADC analog watchdog High threshold value.
  399. * This parameter must be a 12bit value.
  400. * @param LowThreshold: the ADC analog watchdog Low threshold value.
  401. * This parameter must be a 12bit value.
  402. * @retval None
  403. */
  404. void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,
  405. uint16_t LowThreshold)
  406. {
  407. /* Check the parameters */
  408. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  409. assert_param(IS_ADC_THRESHOLD(HighThreshold));
  410. assert_param(IS_ADC_THRESHOLD(LowThreshold));
  411. /* Set the ADCx high and low threshold */
  412. ADCx->TR = LowThreshold | ((uint32_t)HighThreshold << 16);
  413. }
  414. /**
  415. * @brief Configures the analog watchdog guarded single channel
  416. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  417. * @param ADC_AnalogWatchdog_Channel: the ADC channel to configure for the analog watchdog.
  418. * This parameter can be one of the following values:
  419. * @arg ADC_AnalogWatchdog_Channel_0: ADC Channel0 selected
  420. * @arg ADC_AnalogWatchdog_Channel_1: ADC Channel1 selected
  421. * @arg ADC_AnalogWatchdog_Channel_2: ADC Channel2 selected
  422. * @arg ADC_AnalogWatchdog_Channel_3: ADC Channel3 selected
  423. * @arg ADC_AnalogWatchdog_Channel_4: ADC Channel4 selected
  424. * @arg ADC_AnalogWatchdog_Channel_5: ADC Channel5 selected
  425. * @arg ADC_AnalogWatchdog_Channel_6: ADC Channel6 selected
  426. * @arg ADC_AnalogWatchdog_Channel_7: ADC Channel7 selected
  427. * @arg ADC_AnalogWatchdog_Channel_8: ADC Channel8 selected
  428. * @arg ADC_AnalogWatchdog_Channel_9: ADC Channel9 selected
  429. * @arg ADC_AnalogWatchdog_Channel_10: ADC Channel10 selected, not available for STM32F031 devices
  430. * @arg ADC_AnalogWatchdog_Channel_11: ADC Channel11 selected, not available for STM32F031 devices
  431. * @arg ADC_AnalogWatchdog_Channel_12: ADC Channel12 selected, not available for STM32F031 devices
  432. * @arg ADC_AnalogWatchdog_Channel_13: ADC Channel13 selected, not available for STM32F031 devices
  433. * @arg ADC_AnalogWatchdog_Channel_14: ADC Channel14 selected, not available for STM32F031 devices
  434. * @arg ADC_AnalogWatchdog_Channel_15: ADC Channel15 selected, not available for STM32F031 devices
  435. * @arg ADC_AnalogWatchdog_Channel_16: ADC Channel16 selected
  436. * @arg ADC_AnalogWatchdog_Channel_17: ADC Channel17 selected
  437. * @arg ADC_AnalogWatchdog_Channel_18: ADC Channel18 selected, not available for STM32F030 devices
  438. * @note The channel selected on the AWDCH must be also set into the CHSELR
  439. * register
  440. * @retval None
  441. */
  442. void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog_Channel)
  443. {
  444. uint32_t tmpreg = 0;
  445. /* Check the parameters */
  446. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  447. assert_param(IS_ADC_ANALOG_WATCHDOG_CHANNEL(ADC_AnalogWatchdog_Channel));
  448. /* Get the old register value */
  449. tmpreg = ADCx->CFGR1;
  450. /* Clear the Analog watchdog channel select bits */
  451. tmpreg &= ~ADC_CFGR1_AWDCH;
  452. /* Set the Analog watchdog channel */
  453. tmpreg |= ADC_AnalogWatchdog_Channel;
  454. /* Store the new register value */
  455. ADCx->CFGR1 = tmpreg;
  456. }
  457. /**
  458. * @brief Enables or disables the ADC Analog Watchdog Single Channel.
  459. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  460. * @param NewState: new state of the ADCx ADC Analog Watchdog Single Channel.
  461. * This parameter can be: ENABLE or DISABLE.
  462. * @retval None
  463. */
  464. void ADC_AnalogWatchdogSingleChannelCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  465. {
  466. /* Check the parameters */
  467. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  468. assert_param(IS_FUNCTIONAL_STATE(NewState));
  469. if (NewState != DISABLE)
  470. {
  471. /* Enable the ADC Analog Watchdog Single Channel */
  472. ADCx->CFGR1 |= ADC_CFGR1_AWDSGL;
  473. }
  474. else
  475. {
  476. /* Disable the ADC Analog Watchdog Single Channel */
  477. ADCx->CFGR1 &= (uint32_t)~ADC_CFGR1_AWDSGL;
  478. }
  479. }
  480. /**
  481. * @}
  482. */
  483. /** @defgroup ADC_Group4 Temperature Sensor, Vrefint and Vbat management functions
  484. * @brief Temperature Sensor, Vrefint and Vbat management functions
  485. *
  486. @verbatim
  487. ===============================================================================
  488. ##### Temperature Sensor, Vrefint and Vbat management function #####
  489. ===============================================================================
  490. [..] This section provides a function allowing to enable/disable the internal
  491. connections between the ADC and the Temperature Sensor, the Vrefint and
  492. Vbat source.
  493. [..] A typical configuration to get the Temperature sensor, Vrefint and Vbat channels
  494. voltages is done following these steps :
  495. (#) Enable the internal connection of Temperature sensor, Vrefint or Vbat sources
  496. with the ADC channels using ADC_TempSensorCmd(), ADC_VrefintCmd() or ADC_VbatCmd()
  497. functions.
  498. (#) select the ADC_Channel_16(Temperature sensor), ADC_Channel_17(Vrefint)
  499. or ADC_Channel_18(Voltage battery) using ADC_ChannelConfig() function
  500. (#) Get the voltage values, using ADC_GetConversionValue() function
  501. @endverbatim
  502. * @{
  503. */
  504. /**
  505. * @brief Enables or disables the temperature sensor channel.
  506. * @param NewState: new state of the temperature sensor input channel.
  507. * This parameter can be: ENABLE or DISABLE.
  508. * @retval None
  509. */
  510. void ADC_TempSensorCmd(FunctionalState NewState)
  511. {
  512. /* Check the parameters */
  513. assert_param(IS_FUNCTIONAL_STATE(NewState));
  514. if (NewState != DISABLE)
  515. {
  516. /* Enable the temperature sensor channel*/
  517. ADC->CCR |= (uint32_t)ADC_CCR_TSEN;
  518. }
  519. else
  520. {
  521. /* Disable the temperature sensor channel*/
  522. ADC->CCR &= (uint32_t)(~ADC_CCR_TSEN);
  523. }
  524. }
  525. /**
  526. * @brief Enables or disables the Vrefint channel.
  527. * @param NewState: new state of the Vref input channel.
  528. * This parameter can be: ENABLE or DISABLE.
  529. * @retval None
  530. */
  531. void ADC_VrefintCmd(FunctionalState NewState)
  532. {
  533. /* Check the parameters */
  534. assert_param(IS_FUNCTIONAL_STATE(NewState));
  535. if (NewState != DISABLE)
  536. {
  537. /* Enable the Vrefint channel*/
  538. ADC->CCR |= (uint32_t)ADC_CCR_VREFEN;
  539. }
  540. else
  541. {
  542. /* Disable the Vrefint channel*/
  543. ADC->CCR &= (uint32_t)(~ADC_CCR_VREFEN);
  544. }
  545. }
  546. /**
  547. * @brief Enables or disables the Vbat channel.
  548. * @note This feature is not applicable for STM32F030 devices.
  549. * @param NewState: new state of the Vbat input channel.
  550. * This parameter can be: ENABLE or DISABLE.
  551. * @retval None
  552. */
  553. void ADC_VbatCmd(FunctionalState NewState)
  554. {
  555. /* Check the parameters */
  556. assert_param(IS_FUNCTIONAL_STATE(NewState));
  557. if (NewState != DISABLE)
  558. {
  559. /* Enable the Vbat channel*/
  560. ADC->CCR |= (uint32_t)ADC_CCR_VBATEN;
  561. }
  562. else
  563. {
  564. /* Disable the Vbat channel*/
  565. ADC->CCR &= (uint32_t)(~ADC_CCR_VBATEN);
  566. }
  567. }
  568. /**
  569. * @}
  570. */
  571. /** @defgroup ADC_Group5 Channels Configuration functions
  572. * @brief Channels Configuration functions
  573. *
  574. @verbatim
  575. ===============================================================================
  576. ##### Channels Configuration functions #####
  577. ===============================================================================
  578. [..] This section provides functions allowing to manage the ADC channels,
  579. it is composed of 3 sub sections :
  580. (#) Configuration and management functions for ADC channels: This subsection
  581. provides functions allowing to configure the ADC channels :
  582. (++) Select the ADC channels
  583. (++) Activate ADC Calibration
  584. (++) Activate the Overrun Mode.
  585. (++) Activate the Discontinuous Mode
  586. (++) Activate the Continuous Mode.
  587. (++) Configure the sampling time for each channel
  588. (++) Select the conversion Trigger and Edge for ADC channels
  589. (++) Select the scan direction.
  590. -@@- Please Note that the following features for ADC channels are configurated
  591. using the ADC_Init() function :
  592. (+@@) Activate the Continuous Mode (can be also activated by ADC_OverrunModeCmd().
  593. (+@@) Select the conversion Trigger and Edge for ADC channels
  594. (+@@) Select the scan direction.
  595. (#) Control the ADC peripheral : This subsection permits to command the ADC:
  596. (++) Stop or discard an on-going conversion (ADSTP command)
  597. (++) Start the ADC conversion .
  598. (#) Get the conversion data: This subsection provides an important function in
  599. the ADC peripheral since it returns the converted data of the current
  600. ADC channel. When the Conversion value is read, the EOC Flag is
  601. automatically cleared.
  602. @endverbatim
  603. * @{
  604. */
  605. /**
  606. * @brief Configures for the selected ADC and its sampling time.
  607. * @param ADCx: where x can be 1 to select the ADC peripheral.
  608. * @param ADC_Channel: the ADC channel to configure.
  609. * This parameter can be any combination of the following values:
  610. * @arg ADC_Channel_0: ADC Channel0 selected
  611. * @arg ADC_Channel_1: ADC Channel1 selected
  612. * @arg ADC_Channel_2: ADC Channel2 selected
  613. * @arg ADC_Channel_3: ADC Channel3 selected
  614. * @arg ADC_Channel_4: ADC Channel4 selected
  615. * @arg ADC_Channel_5: ADC Channel5 selected
  616. * @arg ADC_Channel_6: ADC Channel6 selected
  617. * @arg ADC_Channel_7: ADC Channel7 selected
  618. * @arg ADC_Channel_8: ADC Channel8 selected
  619. * @arg ADC_Channel_9: ADC Channel9 selected
  620. * @arg ADC_Channel_10: ADC Channel10 selected, not available for STM32F031 devices
  621. * @arg ADC_Channel_11: ADC Channel11 selected, not available for STM32F031 devices
  622. * @arg ADC_Channel_12: ADC Channel12 selected, not available for STM32F031 devices
  623. * @arg ADC_Channel_13: ADC Channel13 selected, not available for STM32F031 devices
  624. * @arg ADC_Channel_14: ADC Channel14 selected, not available for STM32F031 devices
  625. * @arg ADC_Channel_15: ADC Channel15 selected, not available for STM32F031 devices
  626. * @arg ADC_Channel_16: ADC Channel16 selected
  627. * @arg ADC_Channel_17: ADC Channel17 selected
  628. * @arg ADC_Channel_18: ADC Channel18 selected, not available for STM32F030 devices
  629. * @param ADC_SampleTime: The sample time value to be set for the selected channel.
  630. * This parameter can be one of the following values:
  631. * @arg ADC_SampleTime_1_5Cycles: Sample time equal to 1.5 cycles
  632. * @arg ADC_SampleTime_7_5Cycles: Sample time equal to 7.5 cycles
  633. * @arg ADC_SampleTime_13_5Cycles: Sample time equal to 13.5 cycles
  634. * @arg ADC_SampleTime_28_5Cycles: Sample time equal to 28.5 cycles
  635. * @arg ADC_SampleTime_41_5Cycles: Sample time equal to 41.5 cycles
  636. * @arg ADC_SampleTime_55_5Cycles: Sample time equal to 55.5 cycles
  637. * @arg ADC_SampleTime_71_5Cycles: Sample time equal to 71.5 cycles
  638. * @arg ADC_SampleTime_239_5Cycles: Sample time equal to 239.5 cycles
  639. * @retval None
  640. */
  641. void ADC_ChannelConfig(ADC_TypeDef* ADCx, uint32_t ADC_Channel, uint32_t ADC_SampleTime)
  642. {
  643. uint32_t tmpreg = 0;
  644. /* Check the parameters */
  645. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  646. assert_param(IS_ADC_CHANNEL(ADC_Channel));
  647. assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
  648. /* Configure the ADC Channel */
  649. ADCx->CHSELR |= (uint32_t)ADC_Channel;
  650. /* Clear the Sampling time Selection bits */
  651. tmpreg &= ~ADC_SMPR1_SMPR;
  652. /* Set the ADC Sampling Time register */
  653. tmpreg |= (uint32_t)ADC_SampleTime;
  654. /* Configure the ADC Sample time register */
  655. ADCx->SMPR = tmpreg ;
  656. }
  657. /**
  658. * @brief Enable the Continuous mode for the selected ADCx channels.
  659. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  660. * @param NewState: new state of the Continuous mode.
  661. * This parameter can be: ENABLE or DISABLE.
  662. * @note It is not possible to have both discontinuous mode and continuous mode
  663. * enabled. In this case (If DISCEN and CONT are Set), the ADC behaves
  664. * as if continuous mode was disabled
  665. * @retval None
  666. */
  667. void ADC_ContinuousModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  668. {
  669. /* Check the parameters */
  670. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  671. assert_param(IS_FUNCTIONAL_STATE(NewState));
  672. if (NewState != DISABLE)
  673. {
  674. /* Enable the Continuous mode*/
  675. ADCx->CFGR1 |= (uint32_t)ADC_CFGR1_CONT;
  676. }
  677. else
  678. {
  679. /* Disable the Continuous mode */
  680. ADCx->CFGR1 &= (uint32_t)(~ADC_CFGR1_CONT);
  681. }
  682. }
  683. /**
  684. * @brief Enable the discontinuous mode for the selected ADC channels.
  685. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  686. * @param NewState: new state of the discontinuous mode.
  687. * This parameter can be: ENABLE or DISABLE.
  688. * @note It is not possible to have both discontinuous mode and continuous mode
  689. * enabled. In this case (If DISCEN and CONT are Set), the ADC behaves
  690. * as if continuous mode was disabled
  691. * @retval None
  692. */
  693. void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  694. {
  695. /* Check the parameters */
  696. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  697. assert_param(IS_FUNCTIONAL_STATE(NewState));
  698. if (NewState != DISABLE)
  699. {
  700. /* Enable the Discontinuous mode */
  701. ADCx->CFGR1 |= (uint32_t)ADC_CFGR1_DISCEN;
  702. }
  703. else
  704. {
  705. /* Disable the Discontinuous mode */
  706. ADCx->CFGR1 &= (uint32_t)(~ADC_CFGR1_DISCEN);
  707. }
  708. }
  709. /**
  710. * @brief Enable the Overrun mode for the selected ADC channels.
  711. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  712. * @param NewState: new state of the Overrun mode.
  713. * This parameter can be: ENABLE or DISABLE.
  714. * @retval None
  715. */
  716. void ADC_OverrunModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  717. {
  718. /* Check the parameters */
  719. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  720. assert_param(IS_FUNCTIONAL_STATE(NewState));
  721. if (NewState != DISABLE)
  722. {
  723. /* Enable the Overrun mode */
  724. ADCx->CFGR1 |= (uint32_t)ADC_CFGR1_OVRMOD;
  725. }
  726. else
  727. {
  728. /* Disable the Overrun mode */
  729. ADCx->CFGR1 &= (uint32_t)(~ADC_CFGR1_OVRMOD);
  730. }
  731. }
  732. /**
  733. * @brief Active the Calibration operation for the selected ADC.
  734. * @note The Calibration can be initiated only when ADC is still in the
  735. * reset configuration (ADEN must be equal to 0).
  736. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  737. * @retval ADC Calibration factor
  738. */
  739. uint32_t ADC_GetCalibrationFactor(ADC_TypeDef* ADCx)
  740. {
  741. uint32_t tmpreg = 0, calibrationcounter = 0, calibrationstatus = 0;
  742. /* Check the parameters */
  743. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  744. /* Set the ADC calibartion */
  745. ADCx->CR |= (uint32_t)ADC_CR_ADCAL;
  746. /* Wait until no ADC calibration is completed */
  747. do
  748. {
  749. calibrationstatus = ADCx->CR & ADC_CR_ADCAL;
  750. calibrationcounter++;
  751. } while((calibrationcounter != CALIBRATION_TIMEOUT) && (calibrationstatus != 0x00));
  752. if((uint32_t)(ADCx->CR & ADC_CR_ADCAL) == RESET)
  753. {
  754. /*Get the calibration factor from the ADC data register */
  755. tmpreg = ADCx->DR;
  756. }
  757. else
  758. {
  759. /* Error factor */
  760. tmpreg = 0x00000000;
  761. }
  762. return tmpreg;
  763. }
  764. /**
  765. * @brief Stop the on going conversions for the selected ADC.
  766. * @note When ADSTP is set, any on going conversion is aborted, and the ADC
  767. * data register is not updated with current conversion.
  768. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  769. * @retval None
  770. */
  771. void ADC_StopOfConversion(ADC_TypeDef* ADCx)
  772. {
  773. /* Check the parameters */
  774. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  775. ADCx->CR |= (uint32_t)ADC_CR_ADSTP;
  776. }
  777. /**
  778. * @brief Start Conversion for the selected ADC channels.
  779. * @note In continuous mode, ADSTART is not cleared by hardware with the
  780. * assertion of EOSEQ because the sequence is automatic relaunched
  781. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  782. * @retval None
  783. */
  784. void ADC_StartOfConversion(ADC_TypeDef* ADCx)
  785. {
  786. /* Check the parameters */
  787. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  788. ADCx->CR |= (uint32_t)ADC_CR_ADSTART;
  789. }
  790. /**
  791. * @brief Returns the last ADCx conversion result data for ADC channel.
  792. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  793. * @retval The Data conversion value.
  794. */
  795. uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx)
  796. {
  797. /* Check the parameters */
  798. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  799. /* Return the selected ADC conversion value */
  800. return (uint16_t) ADCx->DR;
  801. }
  802. /**
  803. * @}
  804. */
  805. /** @defgroup ADC_Group6 DMA Configuration functions
  806. * @brief Regular Channels DMA Configuration functions
  807. *
  808. @verbatim
  809. ===============================================================================
  810. ##### DMA Configuration functions #####
  811. ===============================================================================
  812. [..] This section provides functions allowing to configure the DMA for ADC hannels.
  813. Since converted channel values are stored into a unique data register,
  814. it is useful to use DMA for conversion of more than one channel. This
  815. avoids the loss of the data already stored in the ADC Data register.
  816. When the DMA mode is enabled (using the ADC_DMACmd() function), after each
  817. conversion of a channel, a DMA request is generated.
  818. [..] Depending on the "DMA disable selection" configuration (using the
  819. ADC_DMARequestModeConfig() function), at the end of the last DMA
  820. transfer, two possibilities are allowed:
  821. (+) No new DMA request is issued to the DMA controller (One Shot Mode)
  822. (+) Requests can continue to be generated (Circular Mode).
  823. @endverbatim
  824. * @{
  825. */
  826. /**
  827. * @brief Enables or disables the specified ADC DMA request.
  828. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  829. * @param NewState: new state of the selected ADC DMA transfer.
  830. * This parameter can be: ENABLE or DISABLE.
  831. * @retval None
  832. */
  833. void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState)
  834. {
  835. /* Check the parameters */
  836. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  837. assert_param(IS_FUNCTIONAL_STATE(NewState));
  838. if (NewState != DISABLE)
  839. {
  840. /* Enable the selected ADC DMA request */
  841. ADCx->CFGR1 |= (uint32_t)ADC_CFGR1_DMAEN;
  842. }
  843. else
  844. {
  845. /* Disable the selected ADC DMA request */
  846. ADCx->CFGR1 &= (uint32_t)(~ADC_CFGR1_DMAEN);
  847. }
  848. }
  849. /**
  850. * @brief Enables or disables the ADC DMA request after last transfer (Single-ADC mode)
  851. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  852. * @param ADC_DMARequestMode: the ADC channel to configure.
  853. * This parameter can be one of the following values:
  854. * @arg ADC_DMAMode_OneShot: DMA One Shot Mode
  855. * @arg ADC_DMAMode_Circular: DMA Circular Mode
  856. * @retval None
  857. */
  858. void ADC_DMARequestModeConfig(ADC_TypeDef* ADCx, uint32_t ADC_DMARequestMode)
  859. {
  860. /* Check the parameters */
  861. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  862. ADCx->CFGR1 &= (uint32_t)~ADC_CFGR1_DMACFG;
  863. ADCx->CFGR1 |= (uint32_t)ADC_DMARequestMode;
  864. }
  865. /**
  866. * @}
  867. */
  868. /** @defgroup ADC_Group7 Interrupts and flags management functions
  869. * @brief Interrupts and flags management functions.
  870. *
  871. @verbatim
  872. ===============================================================================
  873. ##### Interrupts and flags management functions #####
  874. ===============================================================================
  875. [..] This section provides functions allowing to configure the ADC Interrupts
  876. and get the status and clear flags and Interrupts pending bits.
  877. [..] The ADC provide 6 Interrupts sources and 11 Flags which can be divided into
  878. 3 groups:
  879. *** Flags for ADC status ***
  880. ======================================================
  881. [..]
  882. (+)Flags :
  883. (##) ADC_FLAG_ADRDY : This flag is set after the ADC has been enabled (bit ADEN=1)
  884. and when the ADC reaches a state where it is ready to accept conversion requests
  885. (##) ADC_FLAG_ADEN : This flag is set by software to enable the ADC.
  886. The ADC will be effectively ready to operate once the ADRDY flag has been set.
  887. (##) ADC_FLAG_ADDIS : This flag is cleared once the ADC is effectively
  888. disabled.
  889. (##) ADC_FLAG_ADSTART : This flag is cleared after the execution of
  890. ADC_StopOfConversion() function, at the same time as the ADSTP bit is
  891. cleared by hardware
  892. (##) ADC_FLAG_ADSTP : This flag is cleared by hardware when the conversion
  893. is effectively discarded and the ADC is ready to accept a new start conversion
  894. (##) ADC_FLAG_ADCAL : This flag is set once the calibration is complete.
  895. (+)Interrupts
  896. (##) ADC_IT_ADRDY : specifies the interrupt source for ADC ready event.
  897. *** Flags and Interrupts for ADC channel conversion ***
  898. =====================================================
  899. [..]
  900. (+)Flags :
  901. (##) ADC_FLAG_EOC : This flag is set by hardware at the end of each conversion
  902. of a channel when a new data result is available in the data register
  903. (##) ADC_FLAG_EOSEQ : This bit is set by hardware at the end of the conversion
  904. of a sequence of channels selected by ADC_ChannelConfig() function.
  905. (##) ADC_FLAG_EOSMP : This bit is set by hardware at the end of the sampling phase.
  906. (##) ADC_FLAG_OVR : This flag is set by hardware when an overrun occurs,
  907. meaning that a new conversion has complete while the EOC flag was already set.
  908. (+)Interrupts :
  909. (##) ADC_IT_EOC : specifies the interrupt source for end of conversion event.
  910. (##) ADC_IT_EOSEQ : specifies the interrupt source for end of sequence event.
  911. (##) ADC_IT_EOSMP : specifies the interrupt source for end of sampling event.
  912. (##) ADC_IT_OVR : specifies the interrupt source for Overrun detection
  913. event.
  914. *** Flags and Interrupts for the Analog Watchdog ***
  915. ================================================
  916. [..]
  917. (+)Flags :
  918. (##) ADC_FLAG_AWD: This flag is set by hardware when the converted
  919. voltage crosses the values programmed thrsholds
  920. (+)Interrupts :
  921. (##) ADC_IT_AWD : specifies the interrupt source for Analog watchdog
  922. event.
  923. [..] The user should identify which mode will be used in his application to
  924. manage the ADC controller events: Polling mode or Interrupt mode.
  925. [..] In the Polling Mode it is advised to use the following functions:
  926. (+) ADC_GetFlagStatus() : to check if flags events occur.
  927. (+) ADC_ClearFlag() : to clear the flags events.
  928. [..] In the Interrupt Mode it is advised to use the following functions:
  929. (+) ADC_ITConfig() : to enable or disable the interrupt source.
  930. (+) ADC_GetITStatus() : to check if Interrupt occurs.
  931. (+) ADC_ClearITPendingBit() : to clear the Interrupt pending Bit
  932. (corresponding Flag).
  933. @endverbatim
  934. * @{
  935. */
  936. /**
  937. * @brief Enables or disables the specified ADC interrupts.
  938. * @param ADCx: where x can be 1 to select the ADC peripheral.
  939. * @param ADC_IT: specifies the ADC interrupt sources to be enabled or disabled.
  940. * This parameter can be one of the following values:
  941. * @arg ADC_IT_ADRDY: ADC ready interrupt
  942. * @arg ADC_IT_EOSMP: End of sampling interrupt
  943. * @arg ADC_IT_EOC: End of conversion interrupt
  944. * @arg ADC_IT_EOSEQ: End of sequence of conversion interrupt
  945. * @arg ADC_IT_OVR: overrun interrupt
  946. * @arg ADC_IT_AWD: Analog watchdog interrupt
  947. * @param NewState: new state of the specified ADC interrupts.
  948. * This parameter can be: ENABLE or DISABLE.
  949. * @retval None
  950. */
  951. void ADC_ITConfig(ADC_TypeDef* ADCx, uint32_t ADC_IT, FunctionalState NewState)
  952. {
  953. /* Check the parameters */
  954. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  955. assert_param(IS_FUNCTIONAL_STATE(NewState));
  956. assert_param(IS_ADC_CONFIG_IT(ADC_IT));
  957. if (NewState != DISABLE)
  958. {
  959. /* Enable the selected ADC interrupts */
  960. ADCx->IER |= ADC_IT;
  961. }
  962. else
  963. {
  964. /* Disable the selected ADC interrupts */
  965. ADCx->IER &= (~(uint32_t)ADC_IT);
  966. }
  967. }
  968. /**
  969. * @brief Checks whether the specified ADC flag is set or not.
  970. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  971. * @param ADC_FLAG: specifies the flag to check.
  972. * This parameter can be one of the following values:
  973. * @arg ADC_FLAG_AWD: Analog watchdog flag
  974. * @arg ADC_FLAG_OVR: Overrun flag
  975. * @arg ADC_FLAG_EOSEQ: End of Sequence flag
  976. * @arg ADC_FLAG_EOC: End of conversion flag
  977. * @arg ADC_FLAG_EOSMP: End of sampling flag
  978. * @arg ADC_FLAG_ADRDY: ADC Ready flag
  979. * @arg ADC_FLAG_ADEN: ADC enable flag
  980. * @arg ADC_FLAG_ADDIS: ADC disable flag
  981. * @arg ADC_FLAG_ADSTART: ADC start flag
  982. * @arg ADC_FLAG_ADSTP: ADC stop flag
  983. * @arg ADC_FLAG_ADCAL: ADC Calibration flag
  984. * @retval The new state of ADC_FLAG (SET or RESET).
  985. */
  986. FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint32_t ADC_FLAG)
  987. {
  988. FlagStatus bitstatus = RESET;
  989. uint32_t tmpreg = 0;
  990. /* Check the parameters */
  991. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  992. assert_param(IS_ADC_GET_FLAG(ADC_FLAG));
  993. if((uint32_t)(ADC_FLAG & 0x01000000))
  994. {
  995. tmpreg = ADCx->CR & 0xFEFFFFFF;
  996. }
  997. else
  998. {
  999. tmpreg = ADCx->ISR;
  1000. }
  1001. /* Check the status of the specified ADC flag */
  1002. if ((tmpreg & ADC_FLAG) != (uint32_t)RESET)
  1003. {
  1004. /* ADC_FLAG is set */
  1005. bitstatus = SET;
  1006. }
  1007. else
  1008. {
  1009. /* ADC_FLAG is reset */
  1010. bitstatus = RESET;
  1011. }
  1012. /* Return the ADC_FLAG status */
  1013. return bitstatus;
  1014. }
  1015. /**
  1016. * @brief Clears the ADCx's pending flags.
  1017. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  1018. * @param ADC_FLAG: specifies the flag to clear.
  1019. * This parameter can be any combination of the following values:
  1020. * @arg ADC_FLAG_AWD: Analog watchdog flag
  1021. * @arg ADC_FLAG_EOC: End of conversion flag
  1022. * @arg ADC_FLAG_ADRDY: ADC Ready flag
  1023. * @arg ADC_FLAG_EOSMP: End of sampling flag
  1024. * @arg ADC_FLAG_EOSEQ: End of Sequence flag
  1025. * @arg ADC_FLAG_OVR: Overrun flag
  1026. * @retval None
  1027. */
  1028. void ADC_ClearFlag(ADC_TypeDef* ADCx, uint32_t ADC_FLAG)
  1029. {
  1030. /* Check the parameters */
  1031. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1032. assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));
  1033. /* Clear the selected ADC flags */
  1034. ADCx->ISR = (uint32_t)ADC_FLAG;
  1035. }
  1036. /**
  1037. * @brief Checks whether the specified ADC interrupt has occurred or not.
  1038. * @param ADCx: where x can be 1 to select the ADC1 peripheral
  1039. * @param ADC_IT: specifies the ADC interrupt source to check.
  1040. * This parameter can be one of the following values:
  1041. * @arg ADC_IT_ADRDY: ADC ready interrupt
  1042. * @arg ADC_IT_EOSMP: End of sampling interrupt
  1043. * @arg ADC_IT_EOC: End of conversion interrupt
  1044. * @arg ADC_IT_EOSEQ: End of sequence of conversion interrupt
  1045. * @arg ADC_IT_OVR: overrun interrupt
  1046. * @arg ADC_IT_AWD: Analog watchdog interrupt
  1047. * @retval The new state of ADC_IT (SET or RESET).
  1048. */
  1049. ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint32_t ADC_IT)
  1050. {
  1051. ITStatus bitstatus = RESET;
  1052. uint32_t enablestatus = 0;
  1053. /* Check the parameters */
  1054. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1055. assert_param(IS_ADC_GET_IT(ADC_IT));
  1056. /* Get the ADC_IT enable bit status */
  1057. enablestatus = (uint32_t)(ADCx->IER & ADC_IT);
  1058. /* Check the status of the specified ADC interrupt */
  1059. if (((uint32_t)(ADCx->ISR & ADC_IT) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
  1060. {
  1061. /* ADC_IT is set */
  1062. bitstatus = SET;
  1063. }
  1064. else
  1065. {
  1066. /* ADC_IT is reset */
  1067. bitstatus = RESET;
  1068. }
  1069. /* Return the ADC_IT status */
  1070. return bitstatus;
  1071. }
  1072. /**
  1073. * @brief Clears the ADCx's interrupt pending bits.
  1074. * @param ADCx: where x can be 1 to select the ADC1 peripheral.
  1075. * @param ADC_IT: specifies the ADC interrupt pending bit to clear.
  1076. * This parameter can be one of the following values:
  1077. * @arg ADC_IT_ADRDY: ADC ready interrupt
  1078. * @arg ADC_IT_EOSMP: End of sampling interrupt
  1079. * @arg ADC_IT_EOC: End of conversion interrupt
  1080. * @arg ADC_IT_EOSEQ: End of sequence of conversion interrupt
  1081. * @arg ADC_IT_OVR: overrun interrupt
  1082. * @arg ADC_IT_AWD: Analog watchdog interrupt
  1083. * @retval None
  1084. */
  1085. void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint32_t ADC_IT)
  1086. {
  1087. /* Check the parameters */
  1088. assert_param(IS_ADC_ALL_PERIPH(ADCx));
  1089. assert_param(IS_ADC_CLEAR_IT(ADC_IT));
  1090. /* Clear the selected ADC interrupt pending bits */
  1091. ADCx->ISR = (uint32_t)ADC_IT;
  1092. }
  1093. /**
  1094. * @}
  1095. */
  1096. /**
  1097. * @}
  1098. */
  1099. /**
  1100. * @}
  1101. */
  1102. /**
  1103. * @}
  1104. */
  1105. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/