stm32f0xx_hal_adc.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_adc.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Analog to Digital Convertor (ADC)
  7. * peripheral:
  8. * + Initialization and de-initialization functions
  9. * + Peripheral Control functions
  10. * + Peripheral State functions
  11. * Other functions (extended functions) are available in file
  12. * "stm32f0xx_hal_adc_ex.c".
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2016 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ==============================================================================
  27. ##### ADC peripheral features #####
  28. ==============================================================================
  29. [..]
  30. (+) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution
  31. (+) Interrupt generation at the end of regular conversion and in case of
  32. analog watchdog or overrun events.
  33. (+) Single and continuous conversion modes.
  34. (+) Scan mode for conversion of several channels sequentially.
  35. (+) Data alignment with in-built data coherency.
  36. (+) Programmable sampling time (common for all channels)
  37. (+) ADC conversion of regular group.
  38. (+) External trigger (timer or EXTI) with configurable polarity
  39. (+) DMA request generation for transfer of conversions data of regular group.
  40. (+) ADC calibration
  41. (+) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at
  42. slower speed.
  43. (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to
  44. Vdda or to an external voltage reference).
  45. ##### How to use this driver #####
  46. ==============================================================================
  47. [..]
  48. *** Configuration of top level parameters related to ADC ***
  49. ============================================================
  50. [..]
  51. (#) Enable the ADC interface
  52. (++) As prerequisite, ADC clock must be configured at RCC top level.
  53. Caution: On STM32F0, ADC clock frequency max is 14MHz (refer
  54. to device datasheet).
  55. Therefore, ADC clock prescaler must be configured in
  56. function of ADC clock source frequency to remain below
  57. this maximum frequency.
  58. (++) Two clock settings are mandatory:
  59. (+++) ADC clock (core clock, also possibly conversion clock).
  60. (+++) ADC clock (conversions clock).
  61. Two possible clock sources: synchronous clock derived from APB clock
  62. or asynchronous clock derived from ADC dedicated HSI RC oscillator
  63. 14MHz.
  64. If asynchronous clock is selected, parameter "HSI14State" must be set either:
  65. - to "...HSI14State = RCC_HSI14_ADC_CONTROL" to let the ADC control
  66. the HSI14 oscillator enable/disable (if not used to supply the main
  67. system clock): feature used if ADC mode LowPowerAutoPowerOff is
  68. enabled.
  69. - to "...HSI14State = RCC_HSI14_ON" to maintain the HSI14 oscillator
  70. always enabled: can be used to supply the main system clock.
  71. (+++) Example:
  72. Into HAL_ADC_MspInit() (recommended code location) or with
  73. other device clock parameters configuration:
  74. (+++) __HAL_RCC_ADC1_CLK_ENABLE(); (mandatory)
  75. HI14 enable or let under control of ADC: (optional: if asynchronous clock selected)
  76. (+++) RCC_OscInitTypeDef RCC_OscInitStructure;
  77. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI14;
  78. (+++) RCC_OscInitStructure.HSI14CalibrationValue = RCC_HSI14CALIBRATION_DEFAULT;
  79. (+++) RCC_OscInitStructure.HSI14State = RCC_HSI14_ADC_CONTROL;
  80. (+++) RCC_OscInitStructure.PLL... (optional if used for system clock)
  81. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  82. (++) ADC clock source and clock prescaler are configured at ADC level with
  83. parameter "ClockPrescaler" using function HAL_ADC_Init().
  84. (#) ADC pins configuration
  85. (++) Enable the clock for the ADC GPIOs
  86. using macro __HAL_RCC_GPIOx_CLK_ENABLE()
  87. (++) Configure these ADC pins in analog mode
  88. using function HAL_GPIO_Init()
  89. (#) Optionally, in case of usage of ADC with interruptions:
  90. (++) Configure the NVIC for ADC
  91. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  92. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  93. into the function of corresponding ADC interruption vector
  94. ADCx_IRQHandler().
  95. (#) Optionally, in case of usage of DMA:
  96. (++) Configure the DMA (DMA channel, mode normal or circular, ...)
  97. using function HAL_DMA_Init().
  98. (++) Configure the NVIC for DMA
  99. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  100. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  101. into the function of corresponding DMA interruption vector
  102. DMAx_Channelx_IRQHandler().
  103. *** Configuration of ADC, group regular, channels parameters ***
  104. ================================================================
  105. [..]
  106. (#) Configure the ADC parameters (resolution, data alignment, ...)
  107. and regular group parameters (conversion trigger, sequencer, ...)
  108. using function HAL_ADC_Init().
  109. (#) Configure the channels for regular group parameters (channel number,
  110. channel rank into sequencer, ..., into regular group)
  111. using function HAL_ADC_ConfigChannel().
  112. (#) Optionally, configure the analog watchdog parameters (channels
  113. monitored, thresholds, ...)
  114. using function HAL_ADC_AnalogWDGConfig().
  115. *** Execution of ADC conversions ***
  116. ====================================
  117. [..]
  118. (#) Optionally, perform an automatic ADC calibration to improve the
  119. conversion accuracy
  120. using function HAL_ADCEx_Calibration_Start().
  121. (#) ADC driver can be used among three modes: polling, interruption,
  122. transfer by DMA.
  123. (++) ADC conversion by polling:
  124. (+++) Activate the ADC peripheral and start conversions
  125. using function HAL_ADC_Start()
  126. (+++) Wait for ADC conversion completion
  127. using function HAL_ADC_PollForConversion()
  128. (+++) Retrieve conversion results
  129. using function HAL_ADC_GetValue()
  130. (+++) Stop conversion and disable the ADC peripheral
  131. using function HAL_ADC_Stop()
  132. (++) ADC conversion by interruption:
  133. (+++) Activate the ADC peripheral and start conversions
  134. using function HAL_ADC_Start_IT()
  135. (+++) Wait for ADC conversion completion by call of function
  136. HAL_ADC_ConvCpltCallback()
  137. (this function must be implemented in user program)
  138. (+++) Retrieve conversion results
  139. using function HAL_ADC_GetValue()
  140. (+++) Stop conversion and disable the ADC peripheral
  141. using function HAL_ADC_Stop_IT()
  142. (++) ADC conversion with transfer by DMA:
  143. (+++) Activate the ADC peripheral and start conversions
  144. using function HAL_ADC_Start_DMA()
  145. (+++) Wait for ADC conversion completion by call of function
  146. HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
  147. (these functions must be implemented in user program)
  148. (+++) Conversion results are automatically transferred by DMA into
  149. destination variable address.
  150. (+++) Stop conversion and disable the ADC peripheral
  151. using function HAL_ADC_Stop_DMA()
  152. [..]
  153. (@) Callback functions must be implemented in user program:
  154. (+@) HAL_ADC_ErrorCallback()
  155. (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog)
  156. (+@) HAL_ADC_ConvCpltCallback()
  157. (+@) HAL_ADC_ConvHalfCpltCallback
  158. *** Deinitialization of ADC ***
  159. ============================================================
  160. [..]
  161. (#) Disable the ADC interface
  162. (++) ADC clock can be hard reset and disabled at RCC top level.
  163. (++) Hard reset of ADC peripherals
  164. using macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET().
  165. (++) ADC clock disable
  166. using the equivalent macro/functions as configuration step.
  167. (+++) Example:
  168. Into HAL_ADC_MspDeInit() (recommended code location) or with
  169. other device clock parameters configuration:
  170. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI14;
  171. (+++) RCC_OscInitStructure.HSI14State = RCC_HSI14_OFF; (if not used for system clock)
  172. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  173. (#) ADC pins configuration
  174. (++) Disable the clock for the ADC GPIOs
  175. using macro __HAL_RCC_GPIOx_CLK_DISABLE()
  176. (#) Optionally, in case of usage of ADC with interruptions:
  177. (++) Disable the NVIC for ADC
  178. using function HAL_NVIC_DisableIRQ(ADCx_IRQn)
  179. (#) Optionally, in case of usage of DMA:
  180. (++) Deinitialize the DMA
  181. using function HAL_DMA_DeInit().
  182. (++) Disable the NVIC for DMA
  183. using function HAL_NVIC_DisableIRQ(DMAx_Channelx_IRQn)
  184. [..]
  185. *** Callback registration ***
  186. =============================================
  187. [..]
  188. The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, when set to 1,
  189. allows the user to configure dynamically the driver callbacks.
  190. Use Functions HAL_ADC_RegisterCallback()
  191. to register an interrupt callback.
  192. [..]
  193. Function HAL_ADC_RegisterCallback() allows to register following callbacks:
  194. (+) ConvCpltCallback : ADC conversion complete callback
  195. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  196. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  197. (+) ErrorCallback : ADC error callback
  198. (+) MspInitCallback : ADC Msp Init callback
  199. (+) MspDeInitCallback : ADC Msp DeInit callback
  200. This function takes as parameters the HAL peripheral handle, the Callback ID
  201. and a pointer to the user callback function.
  202. [..]
  203. Use function HAL_ADC_UnRegisterCallback to reset a callback to the default
  204. weak function.
  205. [..]
  206. HAL_ADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
  207. and the Callback ID.
  208. This function allows to reset following callbacks:
  209. (+) ConvCpltCallback : ADC conversion complete callback
  210. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  211. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  212. (+) ErrorCallback : ADC error callback
  213. (+) MspInitCallback : ADC Msp Init callback
  214. (+) MspDeInitCallback : ADC Msp DeInit callback
  215. [..]
  216. By default, after the HAL_ADC_Init() and when the state is HAL_ADC_STATE_RESET
  217. all callbacks are set to the corresponding weak functions:
  218. examples HAL_ADC_ConvCpltCallback(), HAL_ADC_ErrorCallback().
  219. Exception done for MspInit and MspDeInit functions that are
  220. reset to the legacy weak functions in the HAL_ADC_Init()/ HAL_ADC_DeInit() only when
  221. these callbacks are null (not registered beforehand).
  222. [..]
  223. If MspInit or MspDeInit are not null, the HAL_ADC_Init()/ HAL_ADC_DeInit()
  224. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  225. [..]
  226. Callbacks can be registered/unregistered in HAL_ADC_STATE_READY state only.
  227. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  228. in HAL_ADC_STATE_READY or HAL_ADC_STATE_RESET state,
  229. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  230. [..]
  231. Then, the user first registers the MspInit/MspDeInit user callbacks
  232. using HAL_ADC_RegisterCallback() before calling HAL_ADC_DeInit()
  233. or HAL_ADC_Init() function.
  234. [..]
  235. When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or
  236. not defined, the callback registration feature is not available and all callbacks
  237. are set to the corresponding weak functions.
  238. @endverbatim
  239. */
  240. /* Includes ------------------------------------------------------------------*/
  241. #include "stm32f0xx_hal.h"
  242. /** @addtogroup STM32F0xx_HAL_Driver
  243. * @{
  244. */
  245. /** @defgroup ADC ADC
  246. * @brief ADC HAL module driver
  247. * @{
  248. */
  249. #ifdef HAL_ADC_MODULE_ENABLED
  250. /* Private typedef -----------------------------------------------------------*/
  251. /* Private define ------------------------------------------------------------*/
  252. /** @defgroup ADC_Private_Constants ADC Private Constants
  253. * @{
  254. */
  255. /* Fixed timeout values for ADC calibration, enable settling time, disable */
  256. /* settling time. */
  257. /* Values defined to be higher than worst cases: low clock frequency, */
  258. /* maximum prescaler. */
  259. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  260. /* prescaler 4, sampling time 7.5 ADC clock cycles, resolution 12 bits. */
  261. /* Unit: ms */
  262. #define ADC_ENABLE_TIMEOUT ( 2U)
  263. #define ADC_DISABLE_TIMEOUT ( 2U)
  264. #define ADC_STOP_CONVERSION_TIMEOUT ( 2U)
  265. /* Delay for ADC stabilization time. */
  266. /* Maximum delay is 1us (refer to device datasheet, parameter tSTAB). */
  267. /* Unit: us */
  268. #define ADC_STAB_DELAY_US ( 1U)
  269. /* Delay for temperature sensor stabilization time. */
  270. /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */
  271. /* Unit: us */
  272. #define ADC_TEMPSENSOR_DELAY_US ( 10U)
  273. /**
  274. * @}
  275. */
  276. /* Private macro -------------------------------------------------------------*/
  277. /* Private variables ---------------------------------------------------------*/
  278. /* Private function prototypes -----------------------------------------------*/
  279. /** @defgroup ADC_Private_Functions ADC Private Functions
  280. * @{
  281. */
  282. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc);
  283. static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc);
  284. static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc);
  285. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
  286. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
  287. static void ADC_DMAError(DMA_HandleTypeDef *hdma);
  288. /**
  289. * @}
  290. */
  291. /* Exported functions ---------------------------------------------------------*/
  292. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  293. * @{
  294. */
  295. /** @defgroup ADC_Exported_Functions_Group1 Initialization/de-initialization functions
  296. * @brief Initialization and Configuration functions
  297. *
  298. @verbatim
  299. ===============================================================================
  300. ##### Initialization and de-initialization functions #####
  301. ===============================================================================
  302. [..] This section provides functions allowing to:
  303. (+) Initialize and configure the ADC.
  304. (+) De-initialize the ADC
  305. @endverbatim
  306. * @{
  307. */
  308. /**
  309. * @brief Initializes the ADC peripheral and regular group according to
  310. * parameters specified in structure "ADC_InitTypeDef".
  311. * @note As prerequisite, ADC clock must be configured at RCC top level
  312. * depending on both possible clock sources: APB clock of HSI clock.
  313. * See commented example code below that can be copied and uncommented
  314. * into HAL_ADC_MspInit().
  315. * @note Possibility to update parameters on the fly:
  316. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  317. * coming from ADC state reset. Following calls to this function can
  318. * be used to reconfigure some parameters of ADC_InitTypeDef
  319. * structure on the fly, without modifying MSP configuration. If ADC
  320. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  321. * before HAL_ADC_Init().
  322. * The setting of these parameters is conditioned to ADC state.
  323. * For parameters constraints, see comments of structure
  324. * "ADC_InitTypeDef".
  325. * @note This function configures the ADC within 2 scopes: scope of entire
  326. * ADC and scope of regular group. For parameters details, see comments
  327. * of structure "ADC_InitTypeDef".
  328. * @param hadc ADC handle
  329. * @retval HAL status
  330. */
  331. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  332. {
  333. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  334. uint32_t tmpCFGR1 = 0U;
  335. /* Check ADC handle */
  336. if(hadc == NULL)
  337. {
  338. return HAL_ERROR;
  339. }
  340. /* Check the parameters */
  341. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  342. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  343. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  344. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  345. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  346. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  347. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  348. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  349. assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
  350. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  351. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  352. assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
  353. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
  354. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoPowerOff));
  355. /* As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured */
  356. /* at RCC top level depending on both possible clock sources: */
  357. /* APB clock or HSI clock. */
  358. /* Refer to header of this file for more details on clock enabling procedure*/
  359. /* Actions performed only if ADC is coming from state reset: */
  360. /* - Initialization of ADC MSP */
  361. /* - ADC voltage regulator enable */
  362. if (hadc->State == HAL_ADC_STATE_RESET)
  363. {
  364. /* Initialize ADC error code */
  365. ADC_CLEAR_ERRORCODE(hadc);
  366. /* Allocate lock resource and initialize it */
  367. hadc->Lock = HAL_UNLOCKED;
  368. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  369. /* Init the ADC Callback settings */
  370. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
  371. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
  372. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
  373. hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
  374. if (hadc->MspInitCallback == NULL)
  375. {
  376. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  377. }
  378. /* Init the low level hardware */
  379. hadc->MspInitCallback(hadc);
  380. #else
  381. /* Init the low level hardware */
  382. HAL_ADC_MspInit(hadc);
  383. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  384. }
  385. /* Configuration of ADC parameters if previous preliminary actions are */
  386. /* correctly completed. */
  387. /* and if there is no conversion on going on regular group (ADC can be */
  388. /* enabled anyway, in case of call of this function to update a parameter */
  389. /* on the fly). */
  390. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) &&
  391. (tmp_hal_status == HAL_OK) &&
  392. (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET) )
  393. {
  394. /* Set ADC state */
  395. ADC_STATE_CLR_SET(hadc->State,
  396. HAL_ADC_STATE_REG_BUSY,
  397. HAL_ADC_STATE_BUSY_INTERNAL);
  398. /* Parameters update conditioned to ADC state: */
  399. /* Parameters that can be updated only when ADC is disabled: */
  400. /* - ADC clock mode */
  401. /* - ADC clock prescaler */
  402. /* - ADC resolution */
  403. if (ADC_IS_ENABLE(hadc) == RESET)
  404. {
  405. /* Some parameters of this register are not reset, since they are set */
  406. /* by other functions and must be kept in case of usage of this */
  407. /* function on the fly (update of a parameter of ADC_InitTypeDef */
  408. /* without needing to reconfigure all other ADC groups/channels */
  409. /* parameters): */
  410. /* - internal measurement paths: Vbat, temperature sensor, Vref */
  411. /* (set into HAL_ADC_ConfigChannel() ) */
  412. /* Configuration of ADC resolution */
  413. MODIFY_REG(hadc->Instance->CFGR1,
  414. ADC_CFGR1_RES ,
  415. hadc->Init.Resolution );
  416. /* Configuration of ADC clock mode: clock source AHB or HSI with */
  417. /* selectable prescaler */
  418. MODIFY_REG(hadc->Instance->CFGR2 ,
  419. ADC_CFGR2_CKMODE ,
  420. hadc->Init.ClockPrescaler );
  421. }
  422. /* Configuration of ADC: */
  423. /* - discontinuous mode */
  424. /* - LowPowerAutoWait mode */
  425. /* - LowPowerAutoPowerOff mode */
  426. /* - continuous conversion mode */
  427. /* - overrun */
  428. /* - external trigger to start conversion */
  429. /* - external trigger polarity */
  430. /* - data alignment */
  431. /* - resolution */
  432. /* - scan direction */
  433. /* - DMA continuous request */
  434. hadc->Instance->CFGR1 &= ~( ADC_CFGR1_DISCEN |
  435. ADC_CFGR1_AUTOFF |
  436. ADC_CFGR1_AUTDLY |
  437. ADC_CFGR1_CONT |
  438. ADC_CFGR1_OVRMOD |
  439. ADC_CFGR1_EXTSEL |
  440. ADC_CFGR1_EXTEN |
  441. ADC_CFGR1_ALIGN |
  442. ADC_CFGR1_SCANDIR |
  443. ADC_CFGR1_DMACFG );
  444. tmpCFGR1 |= (ADC_CFGR1_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait) |
  445. ADC_CFGR1_AUTOOFF((uint32_t)hadc->Init.LowPowerAutoPowerOff) |
  446. ADC_CFGR1_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) |
  447. ADC_CFGR1_OVERRUN(hadc->Init.Overrun) |
  448. hadc->Init.DataAlign |
  449. ADC_SCANDIR(hadc->Init.ScanConvMode) |
  450. ADC_CFGR1_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests) );
  451. /* Enable discontinuous mode only if continuous mode is disabled */
  452. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  453. {
  454. if (hadc->Init.ContinuousConvMode == DISABLE)
  455. {
  456. /* Enable the selected ADC group regular discontinuous mode */
  457. tmpCFGR1 |= ADC_CFGR1_DISCEN;
  458. }
  459. else
  460. {
  461. /* ADC regular group discontinuous was intended to be enabled, */
  462. /* but ADC regular group modes continuous and sequencer discontinuous */
  463. /* cannot be enabled simultaneously. */
  464. /* Update ADC state machine to error */
  465. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  466. /* Set ADC error code to ADC IP internal error */
  467. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  468. }
  469. }
  470. /* Enable external trigger if trigger selection is different of software */
  471. /* start. */
  472. /* Note: This configuration keeps the hardware feature of parameter */
  473. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  474. /* software start. */
  475. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  476. {
  477. tmpCFGR1 |= ( hadc->Init.ExternalTrigConv |
  478. hadc->Init.ExternalTrigConvEdge );
  479. }
  480. /* Update ADC configuration register with previous settings */
  481. hadc->Instance->CFGR1 |= tmpCFGR1;
  482. /* Channel sampling time configuration */
  483. /* Management of parameters "SamplingTimeCommon" and "SamplingTime" */
  484. /* (obsolete): sampling time set in this function if parameter */
  485. /* "SamplingTimeCommon" has been set to a valid sampling time. */
  486. /* Otherwise, sampling time is set into ADC channel initialization */
  487. /* structure with parameter "SamplingTime" (obsolete). */
  488. if (IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon))
  489. {
  490. /* Channel sampling time configuration */
  491. /* Clear the old sample time */
  492. hadc->Instance->SMPR &= ~(ADC_SMPR_SMP);
  493. /* Set the new sample time */
  494. hadc->Instance->SMPR |= ADC_SMPR_SET(hadc->Init.SamplingTimeCommon);
  495. }
  496. /* Check back that ADC registers have effectively been configured to */
  497. /* ensure of no potential problem of ADC core IP clocking. */
  498. /* Check through register CFGR1 (excluding analog watchdog configuration: */
  499. /* set into separate dedicated function, and bits of ADC resolution set */
  500. /* out of temporary variable 'tmpCFGR1'). */
  501. if ((hadc->Instance->CFGR1 & ~(ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | ADC_CFGR1_RES))
  502. == tmpCFGR1)
  503. {
  504. /* Set ADC error code to none */
  505. ADC_CLEAR_ERRORCODE(hadc);
  506. /* Set the ADC state */
  507. ADC_STATE_CLR_SET(hadc->State,
  508. HAL_ADC_STATE_BUSY_INTERNAL,
  509. HAL_ADC_STATE_READY);
  510. }
  511. else
  512. {
  513. /* Update ADC state machine to error */
  514. ADC_STATE_CLR_SET(hadc->State,
  515. HAL_ADC_STATE_BUSY_INTERNAL,
  516. HAL_ADC_STATE_ERROR_INTERNAL);
  517. /* Set ADC error code to ADC IP internal error */
  518. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  519. tmp_hal_status = HAL_ERROR;
  520. }
  521. }
  522. else
  523. {
  524. /* Update ADC state machine to error */
  525. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  526. tmp_hal_status = HAL_ERROR;
  527. }
  528. /* Return function status */
  529. return tmp_hal_status;
  530. }
  531. /**
  532. * @brief Deinitialize the ADC peripheral registers to their default reset
  533. * values, with deinitialization of the ADC MSP.
  534. * @note For devices with several ADCs: reset of ADC common registers is done
  535. * only if all ADCs sharing the same common group are disabled.
  536. * If this is not the case, reset of these common parameters reset is
  537. * bypassed without error reporting: it can be the intended behaviour in
  538. * case of reset of a single ADC while the other ADCs sharing the same
  539. * common group is still running.
  540. * @param hadc ADC handle
  541. * @retval HAL status
  542. */
  543. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  544. {
  545. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  546. /* Check ADC handle */
  547. if(hadc == NULL)
  548. {
  549. return HAL_ERROR;
  550. }
  551. /* Check the parameters */
  552. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  553. /* Set ADC state */
  554. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  555. /* Stop potential conversion on going, on regular group */
  556. tmp_hal_status = ADC_ConversionStop(hadc);
  557. /* Disable ADC peripheral if conversions are effectively stopped */
  558. if (tmp_hal_status == HAL_OK)
  559. {
  560. /* Disable the ADC peripheral */
  561. tmp_hal_status = ADC_Disable(hadc);
  562. /* Check if ADC is effectively disabled */
  563. if (tmp_hal_status != HAL_ERROR)
  564. {
  565. /* Change ADC state */
  566. hadc->State = HAL_ADC_STATE_READY;
  567. }
  568. }
  569. /* Configuration of ADC parameters if previous preliminary actions are */
  570. /* correctly completed. */
  571. if (tmp_hal_status != HAL_ERROR)
  572. {
  573. /* ========== Reset ADC registers ========== */
  574. /* Reset register IER */
  575. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD | ADC_IT_OVR |
  576. ADC_IT_EOS | ADC_IT_EOC |
  577. ADC_IT_EOSMP | ADC_IT_RDY ) );
  578. /* Reset register ISR */
  579. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_OVR |
  580. ADC_FLAG_EOS | ADC_FLAG_EOC |
  581. ADC_FLAG_EOSMP | ADC_FLAG_RDY ) );
  582. /* Reset register CR */
  583. /* Bits ADC_CR_ADCAL, ADC_CR_ADSTP, ADC_CR_ADSTART are in access mode */
  584. /* "read-set": no direct reset applicable. */
  585. /* Reset register CFGR1 */
  586. hadc->Instance->CFGR1 &= ~(ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | ADC_CFGR1_DISCEN |
  587. ADC_CFGR1_AUTOFF | ADC_CFGR1_WAIT | ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD |
  588. ADC_CFGR1_EXTEN | ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RES |
  589. ADC_CFGR1_SCANDIR | ADC_CFGR1_DMACFG | ADC_CFGR1_DMAEN );
  590. /* Reset register CFGR2 */
  591. /* Note: Update of ADC clock mode is conditioned to ADC state disabled: */
  592. /* already done above. */
  593. hadc->Instance->CFGR2 &= ~ADC_CFGR2_CKMODE;
  594. /* Reset register SMPR */
  595. hadc->Instance->SMPR &= ~ADC_SMPR_SMP;
  596. /* Reset register TR1 */
  597. hadc->Instance->TR &= ~(ADC_TR_HT | ADC_TR_LT);
  598. /* Reset register CHSELR */
  599. hadc->Instance->CHSELR &= ~(ADC_CHSELR_CHSEL18 | ADC_CHSELR_CHSEL17 | ADC_CHSELR_CHSEL16 |
  600. ADC_CHSELR_CHSEL15 | ADC_CHSELR_CHSEL14 | ADC_CHSELR_CHSEL13 | ADC_CHSELR_CHSEL12 |
  601. ADC_CHSELR_CHSEL11 | ADC_CHSELR_CHSEL10 | ADC_CHSELR_CHSEL9 | ADC_CHSELR_CHSEL8 |
  602. ADC_CHSELR_CHSEL7 | ADC_CHSELR_CHSEL6 | ADC_CHSELR_CHSEL5 | ADC_CHSELR_CHSEL4 |
  603. ADC_CHSELR_CHSEL3 | ADC_CHSELR_CHSEL2 | ADC_CHSELR_CHSEL1 | ADC_CHSELR_CHSEL0 );
  604. /* Reset register DR */
  605. /* bits in access mode read only, no direct reset applicable*/
  606. /* Reset register CCR */
  607. ADC->CCR &= ~(ADC_CCR_ALL);
  608. /* ========== Hard reset ADC peripheral ========== */
  609. /* Performs a global reset of the entire ADC peripheral: ADC state is */
  610. /* forced to a similar state after device power-on. */
  611. /* If needed, copy-paste and uncomment the following reset code into */
  612. /* function "void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)": */
  613. /* */
  614. /* __HAL_RCC_ADC1_FORCE_RESET() */
  615. /* __HAL_RCC_ADC1_RELEASE_RESET() */
  616. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  617. if (hadc->MspDeInitCallback == NULL)
  618. {
  619. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  620. }
  621. /* DeInit the low level hardware */
  622. hadc->MspDeInitCallback(hadc);
  623. #else
  624. /* DeInit the low level hardware */
  625. HAL_ADC_MspDeInit(hadc);
  626. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  627. /* Set ADC error code to none */
  628. ADC_CLEAR_ERRORCODE(hadc);
  629. /* Set ADC state */
  630. hadc->State = HAL_ADC_STATE_RESET;
  631. }
  632. /* Process unlocked */
  633. __HAL_UNLOCK(hadc);
  634. /* Return function status */
  635. return tmp_hal_status;
  636. }
  637. /**
  638. * @brief Initializes the ADC MSP.
  639. * @param hadc ADC handle
  640. * @retval None
  641. */
  642. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  643. {
  644. /* Prevent unused argument(s) compilation warning */
  645. UNUSED(hadc);
  646. /* NOTE : This function should not be modified. When the callback is needed,
  647. function HAL_ADC_MspInit must be implemented in the user file.
  648. */
  649. }
  650. /**
  651. * @brief DeInitializes the ADC MSP.
  652. * @param hadc ADC handle
  653. * @retval None
  654. */
  655. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
  656. {
  657. /* Prevent unused argument(s) compilation warning */
  658. UNUSED(hadc);
  659. /* NOTE : This function should not be modified. When the callback is needed,
  660. function HAL_ADC_MspDeInit must be implemented in the user file.
  661. */
  662. }
  663. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  664. /**
  665. * @brief Register a User ADC Callback
  666. * To be used instead of the weak predefined callback
  667. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  668. * the configuration information for the specified ADC.
  669. * @param CallbackID ID of the callback to be registered
  670. * This parameter can be one of the following values:
  671. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  672. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion complete callback ID
  673. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  674. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  675. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  676. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  677. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  678. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  679. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  680. * @param pCallback pointer to the Callback function
  681. * @retval HAL status
  682. */
  683. HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback)
  684. {
  685. HAL_StatusTypeDef status = HAL_OK;
  686. if (pCallback == NULL)
  687. {
  688. /* Update the error code */
  689. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  690. return HAL_ERROR;
  691. }
  692. if ((hadc->State & HAL_ADC_STATE_READY) != 0)
  693. {
  694. switch (CallbackID)
  695. {
  696. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  697. hadc->ConvCpltCallback = pCallback;
  698. break;
  699. case HAL_ADC_CONVERSION_HALF_CB_ID :
  700. hadc->ConvHalfCpltCallback = pCallback;
  701. break;
  702. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  703. hadc->LevelOutOfWindowCallback = pCallback;
  704. break;
  705. case HAL_ADC_ERROR_CB_ID :
  706. hadc->ErrorCallback = pCallback;
  707. break;
  708. case HAL_ADC_MSPINIT_CB_ID :
  709. hadc->MspInitCallback = pCallback;
  710. break;
  711. case HAL_ADC_MSPDEINIT_CB_ID :
  712. hadc->MspDeInitCallback = pCallback;
  713. break;
  714. default :
  715. /* Update the error code */
  716. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  717. /* Return error status */
  718. status = HAL_ERROR;
  719. break;
  720. }
  721. }
  722. else if (HAL_ADC_STATE_RESET == hadc->State)
  723. {
  724. switch (CallbackID)
  725. {
  726. case HAL_ADC_MSPINIT_CB_ID :
  727. hadc->MspInitCallback = pCallback;
  728. break;
  729. case HAL_ADC_MSPDEINIT_CB_ID :
  730. hadc->MspDeInitCallback = pCallback;
  731. break;
  732. default :
  733. /* Update the error code */
  734. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  735. /* Return error status */
  736. status = HAL_ERROR;
  737. break;
  738. }
  739. }
  740. else
  741. {
  742. /* Update the error code */
  743. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  744. /* Return error status */
  745. status = HAL_ERROR;
  746. }
  747. return status;
  748. }
  749. /**
  750. * @brief Unregister a ADC Callback
  751. * ADC callback is redirected to the weak predefined callback
  752. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  753. * the configuration information for the specified ADC.
  754. * @param CallbackID ID of the callback to be unregistered
  755. * This parameter can be one of the following values:
  756. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  757. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion complete callback ID
  758. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  759. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  760. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  761. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  762. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  763. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  764. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  765. * @retval HAL status
  766. */
  767. HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
  768. {
  769. HAL_StatusTypeDef status = HAL_OK;
  770. if ((hadc->State & HAL_ADC_STATE_READY) != 0)
  771. {
  772. switch (CallbackID)
  773. {
  774. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  775. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
  776. break;
  777. case HAL_ADC_CONVERSION_HALF_CB_ID :
  778. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
  779. break;
  780. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  781. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
  782. break;
  783. case HAL_ADC_ERROR_CB_ID :
  784. hadc->ErrorCallback = HAL_ADC_ErrorCallback;
  785. break;
  786. case HAL_ADC_MSPINIT_CB_ID :
  787. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  788. break;
  789. case HAL_ADC_MSPDEINIT_CB_ID :
  790. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  791. break;
  792. default :
  793. /* Update the error code */
  794. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  795. /* Return error status */
  796. status = HAL_ERROR;
  797. break;
  798. }
  799. }
  800. else if (HAL_ADC_STATE_RESET == hadc->State)
  801. {
  802. switch (CallbackID)
  803. {
  804. case HAL_ADC_MSPINIT_CB_ID :
  805. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  806. break;
  807. case HAL_ADC_MSPDEINIT_CB_ID :
  808. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  809. break;
  810. default :
  811. /* Update the error code */
  812. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  813. /* Return error status */
  814. status = HAL_ERROR;
  815. break;
  816. }
  817. }
  818. else
  819. {
  820. /* Update the error code */
  821. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  822. /* Return error status */
  823. status = HAL_ERROR;
  824. }
  825. return status;
  826. }
  827. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  828. /**
  829. * @}
  830. */
  831. /** @defgroup ADC_Exported_Functions_Group2 IO operation functions
  832. * @brief IO operation functions
  833. *
  834. @verbatim
  835. ===============================================================================
  836. ##### IO operation functions #####
  837. ===============================================================================
  838. [..] This section provides functions allowing to:
  839. (+) Start conversion of regular group.
  840. (+) Stop conversion of regular group.
  841. (+) Poll for conversion complete on regular group.
  842. (+) Poll for conversion event.
  843. (+) Get result of regular channel conversion.
  844. (+) Start conversion of regular group and enable interruptions.
  845. (+) Stop conversion of regular group and disable interruptions.
  846. (+) Handle ADC interrupt request
  847. (+) Start conversion of regular group and enable DMA transfer.
  848. (+) Stop conversion of regular group and disable ADC DMA transfer.
  849. @endverbatim
  850. * @{
  851. */
  852. /**
  853. * @brief Enables ADC, starts conversion of regular group.
  854. * Interruptions enabled in this function: None.
  855. * @param hadc ADC handle
  856. * @retval HAL status
  857. */
  858. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  859. {
  860. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  861. /* Check the parameters */
  862. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  863. /* Perform ADC enable and conversion start if no conversion is on going */
  864. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  865. {
  866. /* Process locked */
  867. __HAL_LOCK(hadc);
  868. /* Enable the ADC peripheral */
  869. /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
  870. /* performed automatically by hardware. */
  871. if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
  872. {
  873. tmp_hal_status = ADC_Enable(hadc);
  874. }
  875. /* Start conversion if ADC is effectively enabled */
  876. if (tmp_hal_status == HAL_OK)
  877. {
  878. /* Set ADC state */
  879. /* - Clear state bitfield related to regular group conversion results */
  880. /* - Set state bitfield related to regular operation */
  881. ADC_STATE_CLR_SET(hadc->State,
  882. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  883. HAL_ADC_STATE_REG_BUSY);
  884. /* Reset ADC all error code fields */
  885. ADC_CLEAR_ERRORCODE(hadc);
  886. /* Process unlocked */
  887. /* Unlock before starting ADC conversions: in case of potential */
  888. /* interruption, to let the process to ADC IRQ Handler. */
  889. __HAL_UNLOCK(hadc);
  890. /* Clear regular group conversion flag and overrun flag */
  891. /* (To ensure of no unknown state from potential previous ADC */
  892. /* operations) */
  893. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  894. /* Enable conversion of regular group. */
  895. /* If software start has been selected, conversion starts immediately. */
  896. /* If external trigger has been selected, conversion will start at next */
  897. /* trigger event. */
  898. hadc->Instance->CR |= ADC_CR_ADSTART;
  899. }
  900. }
  901. else
  902. {
  903. tmp_hal_status = HAL_BUSY;
  904. }
  905. /* Return function status */
  906. return tmp_hal_status;
  907. }
  908. /**
  909. * @brief Stop ADC conversion of regular group, disable ADC peripheral.
  910. * @param hadc ADC handle
  911. * @retval HAL status.
  912. */
  913. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  914. {
  915. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  916. /* Check the parameters */
  917. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  918. /* Process locked */
  919. __HAL_LOCK(hadc);
  920. /* 1. Stop potential conversion on going, on regular group */
  921. tmp_hal_status = ADC_ConversionStop(hadc);
  922. /* Disable ADC peripheral if conversions are effectively stopped */
  923. if (tmp_hal_status == HAL_OK)
  924. {
  925. /* 2. Disable the ADC peripheral */
  926. tmp_hal_status = ADC_Disable(hadc);
  927. /* Check if ADC is effectively disabled */
  928. if (tmp_hal_status == HAL_OK)
  929. {
  930. /* Set ADC state */
  931. ADC_STATE_CLR_SET(hadc->State,
  932. HAL_ADC_STATE_REG_BUSY,
  933. HAL_ADC_STATE_READY);
  934. }
  935. }
  936. /* Process unlocked */
  937. __HAL_UNLOCK(hadc);
  938. /* Return function status */
  939. return tmp_hal_status;
  940. }
  941. /**
  942. * @brief Wait for regular group conversion to be completed.
  943. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  944. * conversion) are cleared by this function, with an exception:
  945. * if low power feature "LowPowerAutoWait" is enabled, flags are
  946. * not cleared to not interfere with this feature until data register
  947. * is read using function HAL_ADC_GetValue().
  948. * @note This function cannot be used in a particular setup: ADC configured
  949. * in DMA mode and polling for end of each conversion (ADC init
  950. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  951. * In this case, DMA resets the flag EOC and polling cannot be
  952. * performed on each conversion. Nevertheless, polling can still
  953. * be performed on the complete sequence (ADC init
  954. * parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
  955. * @param hadc ADC handle
  956. * @param Timeout Timeout value in millisecond.
  957. * @retval HAL status
  958. */
  959. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  960. {
  961. uint32_t tickstart;
  962. uint32_t tmp_Flag_EOC;
  963. /* Check the parameters */
  964. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  965. /* If end of conversion selected to end of sequence */
  966. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  967. {
  968. tmp_Flag_EOC = ADC_FLAG_EOS;
  969. }
  970. /* If end of conversion selected to end of each conversion */
  971. else /* ADC_EOC_SINGLE_CONV */
  972. {
  973. /* Verification that ADC configuration is compliant with polling for */
  974. /* each conversion: */
  975. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  976. /* several ranks and polling for end of each conversion. */
  977. /* For code simplicity sake, this particular case is generalized to */
  978. /* ADC configured in DMA mode and and polling for end of each conversion. */
  979. if (HAL_IS_BIT_SET(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN))
  980. {
  981. /* Update ADC state machine to error */
  982. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  983. /* Process unlocked */
  984. __HAL_UNLOCK(hadc);
  985. return HAL_ERROR;
  986. }
  987. else
  988. {
  989. tmp_Flag_EOC = (ADC_FLAG_EOC | ADC_FLAG_EOS);
  990. }
  991. }
  992. /* Get tick count */
  993. tickstart = HAL_GetTick();
  994. /* Wait until End of Conversion flag is raised */
  995. while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
  996. {
  997. /* Check if timeout is disabled (set to infinite wait) */
  998. if(Timeout != HAL_MAX_DELAY)
  999. {
  1000. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  1001. {
  1002. /* New check to avoid false timeout detection in case of preemption */
  1003. if(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
  1004. {
  1005. /* Update ADC state machine to timeout */
  1006. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1007. /* Process unlocked */
  1008. __HAL_UNLOCK(hadc);
  1009. return HAL_TIMEOUT;
  1010. }
  1011. }
  1012. }
  1013. }
  1014. /* Update ADC state machine */
  1015. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1016. /* Determine whether any further conversion upcoming on group regular */
  1017. /* by external trigger, continuous mode or scan sequence on going. */
  1018. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1019. (hadc->Init.ContinuousConvMode == DISABLE) )
  1020. {
  1021. /* If End of Sequence is reached, disable interrupts */
  1022. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  1023. {
  1024. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1025. /* ADSTART==0 (no conversion on going) */
  1026. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1027. {
  1028. /* Disable ADC end of single conversion interrupt on group regular */
  1029. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1030. /* HAL_Start_IT(), but is not disabled here because can be used */
  1031. /* by overrun IRQ process below. */
  1032. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1033. /* Set ADC state */
  1034. ADC_STATE_CLR_SET(hadc->State,
  1035. HAL_ADC_STATE_REG_BUSY,
  1036. HAL_ADC_STATE_READY);
  1037. }
  1038. else
  1039. {
  1040. /* Change ADC state to error state */
  1041. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1042. /* Set ADC error code to ADC IP internal error */
  1043. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1044. }
  1045. }
  1046. }
  1047. /* Clear end of conversion flag of regular group if low power feature */
  1048. /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
  1049. /* until data register is read using function HAL_ADC_GetValue(). */
  1050. if (hadc->Init.LowPowerAutoWait == DISABLE)
  1051. {
  1052. /* Clear regular group conversion flag */
  1053. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  1054. }
  1055. /* Return ADC state */
  1056. return HAL_OK;
  1057. }
  1058. /**
  1059. * @brief Poll for conversion event.
  1060. * @param hadc ADC handle
  1061. * @param EventType the ADC event type.
  1062. * This parameter can be one of the following values:
  1063. * @arg ADC_AWD_EVENT: ADC Analog watchdog event
  1064. * @arg ADC_OVR_EVENT: ADC Overrun event
  1065. * @param Timeout Timeout value in millisecond.
  1066. * @retval HAL status
  1067. */
  1068. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  1069. {
  1070. uint32_t tickstart=0;
  1071. /* Check the parameters */
  1072. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1073. assert_param(IS_ADC_EVENT_TYPE(EventType));
  1074. /* Get tick count */
  1075. tickstart = HAL_GetTick();
  1076. /* Check selected event flag */
  1077. while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  1078. {
  1079. /* Check if timeout is disabled (set to infinite wait) */
  1080. if(Timeout != HAL_MAX_DELAY)
  1081. {
  1082. if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
  1083. {
  1084. /* New check to avoid false timeout detection in case of preemption */
  1085. if(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  1086. {
  1087. /* Update ADC state machine to timeout */
  1088. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1089. /* Process unlocked */
  1090. __HAL_UNLOCK(hadc);
  1091. return HAL_TIMEOUT;
  1092. }
  1093. }
  1094. }
  1095. }
  1096. switch(EventType)
  1097. {
  1098. /* Analog watchdog (level out of window) event */
  1099. case ADC_AWD_EVENT:
  1100. /* Set ADC state */
  1101. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1102. /* Clear ADC analog watchdog flag */
  1103. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  1104. break;
  1105. /* Overrun event */
  1106. default: /* Case ADC_OVR_EVENT */
  1107. /* If overrun is set to overwrite previous data, overrun event is not */
  1108. /* considered as an error. */
  1109. /* (cf ref manual "Managing conversions without using the DMA and without */
  1110. /* overrun ") */
  1111. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1112. {
  1113. /* Set ADC state */
  1114. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1115. /* Set ADC error code to overrun */
  1116. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1117. }
  1118. /* Clear ADC Overrun flag */
  1119. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1120. break;
  1121. }
  1122. /* Return ADC state */
  1123. return HAL_OK;
  1124. }
  1125. /**
  1126. * @brief Enables ADC, starts conversion of regular group with interruption.
  1127. * Interruptions enabled in this function:
  1128. * - EOC (end of conversion of regular group) or EOS (end of
  1129. * sequence of regular group) depending on ADC initialization
  1130. * parameter "EOCSelection"
  1131. * - overrun (if available)
  1132. * Each of these interruptions has its dedicated callback function.
  1133. * @param hadc ADC handle
  1134. * @retval HAL status
  1135. */
  1136. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  1137. {
  1138. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1139. /* Check the parameters */
  1140. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1141. /* Perform ADC enable and conversion start if no conversion is on going */
  1142. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1143. {
  1144. /* Process locked */
  1145. __HAL_LOCK(hadc);
  1146. /* Enable the ADC peripheral */
  1147. /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
  1148. /* performed automatically by hardware. */
  1149. if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
  1150. {
  1151. tmp_hal_status = ADC_Enable(hadc);
  1152. }
  1153. /* Start conversion if ADC is effectively enabled */
  1154. if (tmp_hal_status == HAL_OK)
  1155. {
  1156. /* Set ADC state */
  1157. /* - Clear state bitfield related to regular group conversion results */
  1158. /* - Set state bitfield related to regular operation */
  1159. ADC_STATE_CLR_SET(hadc->State,
  1160. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1161. HAL_ADC_STATE_REG_BUSY);
  1162. /* Reset ADC all error code fields */
  1163. ADC_CLEAR_ERRORCODE(hadc);
  1164. /* Process unlocked */
  1165. /* Unlock before starting ADC conversions: in case of potential */
  1166. /* interruption, to let the process to ADC IRQ Handler. */
  1167. __HAL_UNLOCK(hadc);
  1168. /* Clear regular group conversion flag and overrun flag */
  1169. /* (To ensure of no unknown state from potential previous ADC */
  1170. /* operations) */
  1171. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1172. /* Enable ADC end of conversion interrupt */
  1173. /* Enable ADC overrun interrupt */
  1174. switch(hadc->Init.EOCSelection)
  1175. {
  1176. case ADC_EOC_SEQ_CONV:
  1177. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  1178. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOS | ADC_IT_OVR));
  1179. break;
  1180. /* case ADC_EOC_SINGLE_CONV */
  1181. default:
  1182. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1183. break;
  1184. }
  1185. /* Enable conversion of regular group. */
  1186. /* If software start has been selected, conversion starts immediately. */
  1187. /* If external trigger has been selected, conversion will start at next */
  1188. /* trigger event. */
  1189. hadc->Instance->CR |= ADC_CR_ADSTART;
  1190. }
  1191. }
  1192. else
  1193. {
  1194. tmp_hal_status = HAL_BUSY;
  1195. }
  1196. /* Return function status */
  1197. return tmp_hal_status;
  1198. }
  1199. /**
  1200. * @brief Stop ADC conversion of regular group, disable interruption of
  1201. * end-of-conversion, disable ADC peripheral.
  1202. * @param hadc ADC handle
  1203. * @retval HAL status.
  1204. */
  1205. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  1206. {
  1207. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1208. /* Check the parameters */
  1209. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1210. /* Process locked */
  1211. __HAL_LOCK(hadc);
  1212. /* 1. Stop potential conversion on going, on regular group */
  1213. tmp_hal_status = ADC_ConversionStop(hadc);
  1214. /* Disable ADC peripheral if conversions are effectively stopped */
  1215. if (tmp_hal_status == HAL_OK)
  1216. {
  1217. /* Disable ADC end of conversion interrupt for regular group */
  1218. /* Disable ADC overrun interrupt */
  1219. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1220. /* 2. Disable the ADC peripheral */
  1221. tmp_hal_status = ADC_Disable(hadc);
  1222. /* Check if ADC is effectively disabled */
  1223. if (tmp_hal_status == HAL_OK)
  1224. {
  1225. /* Set ADC state */
  1226. ADC_STATE_CLR_SET(hadc->State,
  1227. HAL_ADC_STATE_REG_BUSY,
  1228. HAL_ADC_STATE_READY);
  1229. }
  1230. }
  1231. /* Process unlocked */
  1232. __HAL_UNLOCK(hadc);
  1233. /* Return function status */
  1234. return tmp_hal_status;
  1235. }
  1236. /**
  1237. * @brief Enables ADC, starts conversion of regular group and transfers result
  1238. * through DMA.
  1239. * Interruptions enabled in this function:
  1240. * - DMA transfer complete
  1241. * - DMA half transfer
  1242. * - overrun
  1243. * Each of these interruptions has its dedicated callback function.
  1244. * @param hadc ADC handle
  1245. * @param pData The destination Buffer address.
  1246. * @param Length The length of data to be transferred from ADC peripheral to memory.
  1247. * @retval None
  1248. */
  1249. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  1250. {
  1251. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1252. /* Check the parameters */
  1253. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1254. /* Perform ADC enable and conversion start if no conversion is on going */
  1255. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1256. {
  1257. /* Process locked */
  1258. __HAL_LOCK(hadc);
  1259. /* Enable the ADC peripheral */
  1260. /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
  1261. /* performed automatically by hardware. */
  1262. if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
  1263. {
  1264. tmp_hal_status = ADC_Enable(hadc);
  1265. }
  1266. /* Start conversion if ADC is effectively enabled */
  1267. if (tmp_hal_status == HAL_OK)
  1268. {
  1269. /* Set ADC state */
  1270. /* - Clear state bitfield related to regular group conversion results */
  1271. /* - Set state bitfield related to regular operation */
  1272. ADC_STATE_CLR_SET(hadc->State,
  1273. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1274. HAL_ADC_STATE_REG_BUSY);
  1275. /* Reset ADC all error code fields */
  1276. ADC_CLEAR_ERRORCODE(hadc);
  1277. /* Process unlocked */
  1278. /* Unlock before starting ADC conversions: in case of potential */
  1279. /* interruption, to let the process to ADC IRQ Handler. */
  1280. __HAL_UNLOCK(hadc);
  1281. /* Set the DMA transfer complete callback */
  1282. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1283. /* Set the DMA half transfer complete callback */
  1284. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1285. /* Set the DMA error callback */
  1286. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1287. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  1288. /* start (in case of SW start): */
  1289. /* Clear regular group conversion flag and overrun flag */
  1290. /* (To ensure of no unknown state from potential previous ADC */
  1291. /* operations) */
  1292. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1293. /* Enable ADC overrun interrupt */
  1294. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1295. /* Enable ADC DMA mode */
  1296. hadc->Instance->CFGR1 |= ADC_CFGR1_DMAEN;
  1297. /* Start the DMA channel */
  1298. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1299. /* Enable conversion of regular group. */
  1300. /* If software start has been selected, conversion starts immediately. */
  1301. /* If external trigger has been selected, conversion will start at next */
  1302. /* trigger event. */
  1303. hadc->Instance->CR |= ADC_CR_ADSTART;
  1304. }
  1305. }
  1306. else
  1307. {
  1308. tmp_hal_status = HAL_BUSY;
  1309. }
  1310. /* Return function status */
  1311. return tmp_hal_status;
  1312. }
  1313. /**
  1314. * @brief Stop ADC conversion of regular group, disable ADC DMA transfer, disable
  1315. * ADC peripheral.
  1316. * Each of these interruptions has its dedicated callback function.
  1317. * @param hadc ADC handle
  1318. * @retval HAL status.
  1319. */
  1320. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  1321. {
  1322. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1323. /* Check the parameters */
  1324. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1325. /* Process locked */
  1326. __HAL_LOCK(hadc);
  1327. /* 1. Stop potential conversion on going, on regular group */
  1328. tmp_hal_status = ADC_ConversionStop(hadc);
  1329. /* Disable ADC peripheral if conversions are effectively stopped */
  1330. if (tmp_hal_status == HAL_OK)
  1331. {
  1332. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  1333. hadc->Instance->CFGR1 &= ~ADC_CFGR1_DMAEN;
  1334. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  1335. /* while DMA transfer is on going) */
  1336. if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY)
  1337. {
  1338. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1339. /* Check if DMA channel effectively disabled */
  1340. if (tmp_hal_status != HAL_OK)
  1341. {
  1342. /* Update ADC state machine to error */
  1343. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1344. }
  1345. }
  1346. /* Disable ADC overrun interrupt */
  1347. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1348. /* 2. Disable the ADC peripheral */
  1349. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep */
  1350. /* in memory a potential failing status. */
  1351. if (tmp_hal_status == HAL_OK)
  1352. {
  1353. tmp_hal_status = ADC_Disable(hadc);
  1354. }
  1355. else
  1356. {
  1357. ADC_Disable(hadc);
  1358. }
  1359. /* Check if ADC is effectively disabled */
  1360. if (tmp_hal_status == HAL_OK)
  1361. {
  1362. /* Set ADC state */
  1363. ADC_STATE_CLR_SET(hadc->State,
  1364. HAL_ADC_STATE_REG_BUSY,
  1365. HAL_ADC_STATE_READY);
  1366. }
  1367. }
  1368. /* Process unlocked */
  1369. __HAL_UNLOCK(hadc);
  1370. /* Return function status */
  1371. return tmp_hal_status;
  1372. }
  1373. /**
  1374. * @brief Get ADC regular group conversion result.
  1375. * @note Reading register DR automatically clears ADC flag EOC
  1376. * (ADC group regular end of unitary conversion).
  1377. * @note This function does not clear ADC flag EOS
  1378. * (ADC group regular end of sequence conversion).
  1379. * Occurrence of flag EOS rising:
  1380. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  1381. * to flag EOC.
  1382. * - If sequencer is composed of several ranks, during the scan
  1383. * sequence flag EOC only is raised, at the end of the scan sequence
  1384. * both flags EOC and EOS are raised.
  1385. * To clear this flag, either use function:
  1386. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1387. * model polling: @ref HAL_ADC_PollForConversion()
  1388. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  1389. * @param hadc ADC handle
  1390. * @retval ADC group regular conversion data
  1391. */
  1392. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  1393. {
  1394. /* Check the parameters */
  1395. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1396. /* Note: EOC flag is not cleared here by software because automatically */
  1397. /* cleared by hardware when reading register DR. */
  1398. /* Return ADC converted value */
  1399. return hadc->Instance->DR;
  1400. }
  1401. /**
  1402. * @brief Handles ADC interrupt request.
  1403. * @param hadc ADC handle
  1404. * @retval None
  1405. */
  1406. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  1407. {
  1408. uint32_t tmp_isr = hadc->Instance->ISR;
  1409. uint32_t tmp_ier = hadc->Instance->IER;
  1410. /* Check the parameters */
  1411. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1412. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  1413. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  1414. /* ========== Check End of Conversion flag for regular group ========== */
  1415. if( (((tmp_isr & ADC_FLAG_EOC) == ADC_FLAG_EOC) && ((tmp_ier & ADC_IT_EOC) == ADC_IT_EOC)) ||
  1416. (((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS) && ((tmp_ier & ADC_IT_EOS) == ADC_IT_EOS)) )
  1417. {
  1418. /* Update state machine on conversion status if not in error state */
  1419. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1420. {
  1421. /* Set ADC state */
  1422. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1423. }
  1424. /* Determine whether any further conversion upcoming on group regular */
  1425. /* by external trigger, continuous mode or scan sequence on going. */
  1426. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1427. (hadc->Init.ContinuousConvMode == DISABLE) )
  1428. {
  1429. /* If End of Sequence is reached, disable interrupts */
  1430. if((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS)
  1431. {
  1432. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1433. /* ADSTART==0 (no conversion on going) */
  1434. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1435. {
  1436. /* Disable ADC end of single conversion interrupt on group regular */
  1437. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1438. /* HAL_Start_IT(), but is not disabled here because can be used */
  1439. /* by overrun IRQ process below. */
  1440. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1441. /* Set ADC state */
  1442. ADC_STATE_CLR_SET(hadc->State,
  1443. HAL_ADC_STATE_REG_BUSY,
  1444. HAL_ADC_STATE_READY);
  1445. }
  1446. else
  1447. {
  1448. /* Change ADC state to error state */
  1449. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1450. /* Set ADC error code to ADC IP internal error */
  1451. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1452. }
  1453. }
  1454. }
  1455. /* Note: into callback, to determine if conversion has been triggered */
  1456. /* from EOC or EOS, possibility to use: */
  1457. /* " if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
  1458. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1459. hadc->ConvCpltCallback(hadc);
  1460. #else
  1461. HAL_ADC_ConvCpltCallback(hadc);
  1462. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1463. /* Clear regular group conversion flag */
  1464. /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of */
  1465. /* conversion flags clear induces the release of the preserved data.*/
  1466. /* Therefore, if the preserved data value is needed, it must be */
  1467. /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
  1468. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS) );
  1469. }
  1470. /* ========== Check Analog watchdog flags ========== */
  1471. if(((tmp_isr & ADC_FLAG_AWD) == ADC_FLAG_AWD) && ((tmp_ier & ADC_IT_AWD) == ADC_IT_AWD))
  1472. {
  1473. /* Set ADC state */
  1474. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1475. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1476. hadc->LevelOutOfWindowCallback(hadc);
  1477. #else
  1478. HAL_ADC_LevelOutOfWindowCallback(hadc);
  1479. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1480. /* Clear ADC Analog watchdog flag */
  1481. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  1482. }
  1483. /* ========== Check Overrun flag ========== */
  1484. if(((tmp_isr & ADC_FLAG_OVR) == ADC_FLAG_OVR) && ((tmp_ier & ADC_IT_OVR) == ADC_IT_OVR))
  1485. {
  1486. /* If overrun is set to overwrite previous data (default setting), */
  1487. /* overrun event is not considered as an error. */
  1488. /* (cf ref manual "Managing conversions without using the DMA and without */
  1489. /* overrun ") */
  1490. /* Exception for usage with DMA overrun event always considered as an */
  1491. /* error. */
  1492. if ((hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED) ||
  1493. HAL_IS_BIT_SET(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN) )
  1494. {
  1495. /* Set ADC error code to overrun */
  1496. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1497. /* Clear ADC overrun flag */
  1498. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1499. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1500. hadc->ErrorCallback(hadc);
  1501. #else
  1502. HAL_ADC_ErrorCallback(hadc);
  1503. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1504. }
  1505. /* Clear the Overrun flag */
  1506. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1507. }
  1508. }
  1509. /**
  1510. * @brief Conversion complete callback in non blocking mode
  1511. * @param hadc ADC handle
  1512. * @retval None
  1513. */
  1514. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  1515. {
  1516. /* Prevent unused argument(s) compilation warning */
  1517. UNUSED(hadc);
  1518. /* NOTE : This function should not be modified. When the callback is needed,
  1519. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  1520. */
  1521. }
  1522. /**
  1523. * @brief Conversion DMA half-transfer callback in non blocking mode
  1524. * @param hadc ADC handle
  1525. * @retval None
  1526. */
  1527. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
  1528. {
  1529. /* Prevent unused argument(s) compilation warning */
  1530. UNUSED(hadc);
  1531. /* NOTE : This function should not be modified. When the callback is needed,
  1532. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  1533. */
  1534. }
  1535. /**
  1536. * @brief Analog watchdog callback in non blocking mode.
  1537. * @param hadc ADC handle
  1538. * @retval None
  1539. */
  1540. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  1541. {
  1542. /* Prevent unused argument(s) compilation warning */
  1543. UNUSED(hadc);
  1544. /* NOTE : This function should not be modified. When the callback is needed,
  1545. function HAL_ADC_LevelOoutOfWindowCallback must be implemented in the user file.
  1546. */
  1547. }
  1548. /**
  1549. * @brief ADC error callback in non blocking mode
  1550. * (ADC conversion with interruption or transfer by DMA)
  1551. * @param hadc ADC handle
  1552. * @retval None
  1553. */
  1554. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  1555. {
  1556. /* Prevent unused argument(s) compilation warning */
  1557. UNUSED(hadc);
  1558. /* NOTE : This function should not be modified. When the callback is needed,
  1559. function HAL_ADC_ErrorCallback must be implemented in the user file.
  1560. */
  1561. }
  1562. /**
  1563. * @}
  1564. */
  1565. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  1566. * @brief Peripheral Control functions
  1567. *
  1568. @verbatim
  1569. ===============================================================================
  1570. ##### Peripheral Control functions #####
  1571. ===============================================================================
  1572. [..] This section provides functions allowing to:
  1573. (+) Configure channels on regular group
  1574. (+) Configure the analog watchdog
  1575. @endverbatim
  1576. * @{
  1577. */
  1578. /**
  1579. * @brief Configures the the selected channel to be linked to the regular
  1580. * group.
  1581. * @note In case of usage of internal measurement channels:
  1582. * VrefInt/Vbat/TempSensor.
  1583. * Sampling time constraints must be respected (sampling time can be
  1584. * adjusted in function of ADC clock frequency and sampling time
  1585. * setting).
  1586. * Refer to device datasheet for timings values, parameters TS_vrefint,
  1587. * TS_vbat, TS_temp (values rough order: 5us to 17us).
  1588. * These internal paths can be be disabled using function
  1589. * HAL_ADC_DeInit().
  1590. * @note Possibility to update parameters on the fly:
  1591. * This function initializes channel into regular group, following
  1592. * calls to this function can be used to reconfigure some parameters
  1593. * of structure "ADC_ChannelConfTypeDef" on the fly, without resetting
  1594. * the ADC.
  1595. * The setting of these parameters is conditioned to ADC state.
  1596. * For parameters constraints, see comments of structure
  1597. * "ADC_ChannelConfTypeDef".
  1598. * @param hadc ADC handle
  1599. * @param sConfig Structure of ADC channel for regular group.
  1600. * @retval HAL status
  1601. */
  1602. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  1603. {
  1604. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1605. __IO uint32_t wait_loop_index = 0U;
  1606. /* Check the parameters */
  1607. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1608. assert_param(IS_ADC_CHANNEL(sConfig->Channel));
  1609. assert_param(IS_ADC_RANK(sConfig->Rank));
  1610. if (! IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon))
  1611. {
  1612. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  1613. }
  1614. /* Process locked */
  1615. __HAL_LOCK(hadc);
  1616. /* Parameters update conditioned to ADC state: */
  1617. /* Parameters that can be updated when ADC is disabled or enabled without */
  1618. /* conversion on going on regular group: */
  1619. /* - Channel number */
  1620. /* - Channel sampling time */
  1621. /* - Management of internal measurement channels: VrefInt/TempSensor/Vbat */
  1622. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1623. {
  1624. /* Configure channel: depending on rank setting, add it or remove it from */
  1625. /* ADC conversion sequencer. */
  1626. if (sConfig->Rank != ADC_RANK_NONE)
  1627. {
  1628. /* Regular sequence configuration */
  1629. /* Set the channel selection register from the selected channel */
  1630. hadc->Instance->CHSELR |= ADC_CHSELR_CHANNEL(sConfig->Channel);
  1631. /* Channel sampling time configuration */
  1632. /* Management of parameters "SamplingTimeCommon" and "SamplingTime" */
  1633. /* (obsolete): sampling time set in this function with */
  1634. /* parameter "SamplingTime" (obsolete) only if not already set into */
  1635. /* ADC initialization structure with parameter "SamplingTimeCommon". */
  1636. if (! IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon))
  1637. {
  1638. /* Modify sampling time if needed (not needed in case of recurrence */
  1639. /* for several channels programmed consecutively into the sequencer) */
  1640. if (sConfig->SamplingTime != ADC_GET_SAMPLINGTIME(hadc))
  1641. {
  1642. /* Channel sampling time configuration */
  1643. /* Clear the old sample time */
  1644. hadc->Instance->SMPR &= ~(ADC_SMPR_SMP);
  1645. /* Set the new sample time */
  1646. hadc->Instance->SMPR |= ADC_SMPR_SET(sConfig->SamplingTime);
  1647. }
  1648. }
  1649. /* Management of internal measurement channels: VrefInt/TempSensor/Vbat */
  1650. /* internal measurement paths enable: If internal channel selected, */
  1651. /* enable dedicated internal buffers and path. */
  1652. /* Note: these internal measurement paths can be disabled using */
  1653. /* HAL_ADC_DeInit() or removing the channel from sequencer with */
  1654. /* channel configuration parameter "Rank". */
  1655. if(ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
  1656. {
  1657. /* If Channel_16 is selected, enable Temp. sensor measurement path. */
  1658. /* If Channel_17 is selected, enable VREFINT measurement path. */
  1659. /* If Channel_18 is selected, enable VBAT measurement path. */
  1660. ADC->CCR |= ADC_CHANNEL_INTERNAL_PATH(sConfig->Channel);
  1661. /* If Temp. sensor is selected, wait for stabilization delay */
  1662. if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
  1663. {
  1664. /* Delay for temperature sensor stabilization time */
  1665. /* Compute number of CPU cycles to wait for */
  1666. wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
  1667. while(wait_loop_index != 0U)
  1668. {
  1669. wait_loop_index--;
  1670. }
  1671. }
  1672. }
  1673. }
  1674. else
  1675. {
  1676. /* Regular sequence configuration */
  1677. /* Reset the channel selection register from the selected channel */
  1678. hadc->Instance->CHSELR &= ~ADC_CHSELR_CHANNEL(sConfig->Channel);
  1679. /* Management of internal measurement channels: VrefInt/TempSensor/Vbat */
  1680. /* internal measurement paths disable: If internal channel selected, */
  1681. /* disable dedicated internal buffers and path. */
  1682. if(ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
  1683. {
  1684. /* If Channel_16 is selected, disable Temp. sensor measurement path. */
  1685. /* If Channel_17 is selected, disable VREFINT measurement path. */
  1686. /* If Channel_18 is selected, disable VBAT measurement path. */
  1687. ADC->CCR &= ~ADC_CHANNEL_INTERNAL_PATH(sConfig->Channel);
  1688. }
  1689. }
  1690. }
  1691. /* If a conversion is on going on regular group, no update on regular */
  1692. /* channel could be done on neither of the channel configuration structure */
  1693. /* parameters. */
  1694. else
  1695. {
  1696. /* Update ADC state machine to error */
  1697. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1698. tmp_hal_status = HAL_ERROR;
  1699. }
  1700. /* Process unlocked */
  1701. __HAL_UNLOCK(hadc);
  1702. /* Return function status */
  1703. return tmp_hal_status;
  1704. }
  1705. /**
  1706. * @brief Configures the analog watchdog.
  1707. * @note Possibility to update parameters on the fly:
  1708. * This function initializes the selected analog watchdog, following
  1709. * calls to this function can be used to reconfigure some parameters
  1710. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
  1711. * the ADC.
  1712. * The setting of these parameters is conditioned to ADC state.
  1713. * For parameters constraints, see comments of structure
  1714. * "ADC_AnalogWDGConfTypeDef".
  1715. * @param hadc ADC handle
  1716. * @param AnalogWDGConfig Structure of ADC analog watchdog configuration
  1717. * @retval HAL status
  1718. */
  1719. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  1720. {
  1721. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1722. uint32_t tmpAWDHighThresholdShifted;
  1723. uint32_t tmpAWDLowThresholdShifted;
  1724. /* Check the parameters */
  1725. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1726. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  1727. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  1728. /* Verify if threshold is within the selected ADC resolution */
  1729. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
  1730. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
  1731. if(AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG)
  1732. {
  1733. assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
  1734. }
  1735. /* Process locked */
  1736. __HAL_LOCK(hadc);
  1737. /* Parameters update conditioned to ADC state: */
  1738. /* Parameters that can be updated when ADC is disabled or enabled without */
  1739. /* conversion on going on regular group: */
  1740. /* - Analog watchdog channels */
  1741. /* - Analog watchdog thresholds */
  1742. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1743. {
  1744. /* Configuration of analog watchdog: */
  1745. /* - Set the analog watchdog enable mode: one or overall group of */
  1746. /* channels. */
  1747. /* - Set the Analog watchdog channel (is not used if watchdog */
  1748. /* mode "all channels": ADC_CFGR_AWD1SGL=0). */
  1749. hadc->Instance->CFGR1 &= ~( ADC_CFGR1_AWDSGL |
  1750. ADC_CFGR1_AWDEN |
  1751. ADC_CFGR1_AWDCH );
  1752. hadc->Instance->CFGR1 |= ( AnalogWDGConfig->WatchdogMode |
  1753. ADC_CFGR_AWDCH(AnalogWDGConfig->Channel) );
  1754. /* Shift the offset in function of the selected ADC resolution: Thresholds*/
  1755. /* have to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  1756. tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  1757. tmpAWDLowThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  1758. /* Set the high and low thresholds */
  1759. hadc->Instance->TR &= ~(ADC_TR_HT | ADC_TR_LT);
  1760. hadc->Instance->TR |= ( ADC_TRX_HIGHTHRESHOLD (tmpAWDHighThresholdShifted) |
  1761. tmpAWDLowThresholdShifted );
  1762. /* Clear the ADC Analog watchdog flag (in case of left enabled by */
  1763. /* previous ADC operations) to be ready to use for HAL_ADC_IRQHandler() */
  1764. /* or HAL_ADC_PollForEvent(). */
  1765. __HAL_ADC_CLEAR_FLAG(hadc, ADC_IT_AWD);
  1766. /* Configure ADC Analog watchdog interrupt */
  1767. if(AnalogWDGConfig->ITMode == ENABLE)
  1768. {
  1769. /* Enable the ADC Analog watchdog interrupt */
  1770. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
  1771. }
  1772. else
  1773. {
  1774. /* Disable the ADC Analog watchdog interrupt */
  1775. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
  1776. }
  1777. }
  1778. /* If a conversion is on going on regular group, no update could be done */
  1779. /* on neither of the AWD configuration structure parameters. */
  1780. else
  1781. {
  1782. /* Update ADC state machine to error */
  1783. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1784. tmp_hal_status = HAL_ERROR;
  1785. }
  1786. /* Process unlocked */
  1787. __HAL_UNLOCK(hadc);
  1788. /* Return function status */
  1789. return tmp_hal_status;
  1790. }
  1791. /**
  1792. * @}
  1793. */
  1794. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  1795. * @brief Peripheral State functions
  1796. *
  1797. @verbatim
  1798. ===============================================================================
  1799. ##### Peripheral State and Errors functions #####
  1800. ===============================================================================
  1801. [..]
  1802. This subsection provides functions to get in run-time the status of the
  1803. peripheral.
  1804. (+) Check the ADC state
  1805. (+) Check the ADC error code
  1806. @endverbatim
  1807. * @{
  1808. */
  1809. /**
  1810. * @brief Return the ADC state
  1811. * @note ADC state machine is managed by bitfields, ADC status must be
  1812. * compared with states bits.
  1813. * For example:
  1814. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_REG_BUSY)) "
  1815. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD1) ) "
  1816. * @param hadc ADC handle
  1817. * @retval HAL state
  1818. */
  1819. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
  1820. {
  1821. /* Check the parameters */
  1822. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1823. /* Return ADC state */
  1824. return hadc->State;
  1825. }
  1826. /**
  1827. * @brief Return the ADC error code
  1828. * @param hadc ADC handle
  1829. * @retval ADC Error Code
  1830. */
  1831. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  1832. {
  1833. return hadc->ErrorCode;
  1834. }
  1835. /**
  1836. * @}
  1837. */
  1838. /**
  1839. * @}
  1840. */
  1841. /** @defgroup ADC_Private_Functions ADC Private Functions
  1842. * @{
  1843. */
  1844. /**
  1845. * @brief Enable the selected ADC.
  1846. * @note Prerequisite condition to use this function: ADC must be disabled
  1847. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  1848. * @note If low power mode AutoPowerOff is enabled, power-on/off phases are
  1849. * performed automatically by hardware.
  1850. * In this mode, this function is useless and must not be called because
  1851. * flag ADC_FLAG_RDY is not usable.
  1852. * Therefore, this function must be called under condition of
  1853. * "if (hadc->Init.LowPowerAutoPowerOff != ENABLE)".
  1854. * @param hadc ADC handle
  1855. * @retval HAL status.
  1856. */
  1857. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
  1858. {
  1859. uint32_t tickstart = 0U;
  1860. __IO uint32_t wait_loop_index = 0U;
  1861. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  1862. /* enabling phase not yet completed: flag ADC ready not yet set). */
  1863. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  1864. /* causes: ADC clock not running, ...). */
  1865. if (ADC_IS_ENABLE(hadc) == RESET)
  1866. {
  1867. /* Check if conditions to enable the ADC are fulfilled */
  1868. if (ADC_ENABLING_CONDITIONS(hadc) == RESET)
  1869. {
  1870. /* Update ADC state machine to error */
  1871. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1872. /* Set ADC error code to ADC IP internal error */
  1873. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1874. return HAL_ERROR;
  1875. }
  1876. /* Enable the ADC peripheral */
  1877. __HAL_ADC_ENABLE(hadc);
  1878. /* Delay for ADC stabilization time */
  1879. /* Compute number of CPU cycles to wait for */
  1880. wait_loop_index = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
  1881. while(wait_loop_index != 0U)
  1882. {
  1883. wait_loop_index--;
  1884. }
  1885. /* Get tick count */
  1886. tickstart = HAL_GetTick();
  1887. /* Wait for ADC effectively enabled */
  1888. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET)
  1889. {
  1890. if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  1891. {
  1892. /* New check to avoid false timeout detection in case of preemption */
  1893. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET)
  1894. {
  1895. /* Update ADC state machine to error */
  1896. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1897. /* Set ADC error code to ADC IP internal error */
  1898. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1899. return HAL_ERROR;
  1900. }
  1901. }
  1902. }
  1903. }
  1904. /* Return HAL status */
  1905. return HAL_OK;
  1906. }
  1907. /**
  1908. * @brief Disable the selected ADC.
  1909. * @note Prerequisite condition to use this function: ADC conversions must be
  1910. * stopped.
  1911. * @param hadc ADC handle
  1912. * @retval HAL status.
  1913. */
  1914. static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc)
  1915. {
  1916. uint32_t tickstart = 0U;
  1917. /* Verification if ADC is not already disabled: */
  1918. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  1919. /* disabled. */
  1920. if (ADC_IS_ENABLE(hadc) != RESET)
  1921. {
  1922. /* Check if conditions to disable the ADC are fulfilled */
  1923. if (ADC_DISABLING_CONDITIONS(hadc) != RESET)
  1924. {
  1925. /* Disable the ADC peripheral */
  1926. __HAL_ADC_DISABLE(hadc);
  1927. }
  1928. else
  1929. {
  1930. /* Update ADC state machine to error */
  1931. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1932. /* Set ADC error code to ADC IP internal error */
  1933. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1934. return HAL_ERROR;
  1935. }
  1936. /* Wait for ADC effectively disabled */
  1937. /* Get tick count */
  1938. tickstart = HAL_GetTick();
  1939. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
  1940. {
  1941. if((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  1942. {
  1943. /* New check to avoid false timeout detection in case of preemption */
  1944. if(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
  1945. {
  1946. /* Update ADC state machine to error */
  1947. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1948. /* Set ADC error code to ADC IP internal error */
  1949. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1950. return HAL_ERROR;
  1951. }
  1952. }
  1953. }
  1954. }
  1955. /* Return HAL status */
  1956. return HAL_OK;
  1957. }
  1958. /**
  1959. * @brief Stop ADC conversion.
  1960. * @note Prerequisite condition to use this function: ADC conversions must be
  1961. * stopped to disable the ADC.
  1962. * @param hadc ADC handle
  1963. * @retval HAL status.
  1964. */
  1965. static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc)
  1966. {
  1967. uint32_t tickstart = 0U;
  1968. /* Check the parameters */
  1969. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1970. /* Verification if ADC is not already stopped on regular group to bypass */
  1971. /* this function if not needed. */
  1972. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc))
  1973. {
  1974. /* Stop potential conversion on going on regular group */
  1975. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  1976. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) &&
  1977. HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
  1978. {
  1979. /* Stop conversions on regular group */
  1980. hadc->Instance->CR |= ADC_CR_ADSTP;
  1981. }
  1982. /* Wait for conversion effectively stopped */
  1983. /* Get tick count */
  1984. tickstart = HAL_GetTick();
  1985. while((hadc->Instance->CR & ADC_CR_ADSTART) != RESET)
  1986. {
  1987. if((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  1988. {
  1989. /* New check to avoid false timeout detection in case of preemption */
  1990. if((hadc->Instance->CR & ADC_CR_ADSTART) != RESET)
  1991. {
  1992. /* Update ADC state machine to error */
  1993. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1994. /* Set ADC error code to ADC IP internal error */
  1995. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1996. return HAL_ERROR;
  1997. }
  1998. }
  1999. }
  2000. }
  2001. /* Return HAL status */
  2002. return HAL_OK;
  2003. }
  2004. /**
  2005. * @brief DMA transfer complete callback.
  2006. * @param hdma pointer to DMA handle.
  2007. * @retval None
  2008. */
  2009. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  2010. {
  2011. /* Retrieve ADC handle corresponding to current DMA handle */
  2012. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2013. /* Update state machine on conversion status if not in error state */
  2014. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA))
  2015. {
  2016. /* Set ADC state */
  2017. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2018. /* Determine whether any further conversion upcoming on group regular */
  2019. /* by external trigger, continuous mode or scan sequence on going. */
  2020. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  2021. (hadc->Init.ContinuousConvMode == DISABLE) )
  2022. {
  2023. /* If End of Sequence is reached, disable interrupts */
  2024. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  2025. {
  2026. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  2027. /* ADSTART==0 (no conversion on going) */
  2028. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  2029. {
  2030. /* Disable ADC end of single conversion interrupt on group regular */
  2031. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  2032. /* HAL_Start_IT(), but is not disabled here because can be used */
  2033. /* by overrun IRQ process below. */
  2034. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  2035. /* Set ADC state */
  2036. ADC_STATE_CLR_SET(hadc->State,
  2037. HAL_ADC_STATE_REG_BUSY,
  2038. HAL_ADC_STATE_READY);
  2039. }
  2040. else
  2041. {
  2042. /* Change ADC state to error state */
  2043. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2044. /* Set ADC error code to ADC IP internal error */
  2045. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2046. }
  2047. }
  2048. }
  2049. /* Conversion complete callback */
  2050. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2051. hadc->ConvCpltCallback(hadc);
  2052. #else
  2053. HAL_ADC_ConvCpltCallback(hadc);
  2054. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2055. }
  2056. else
  2057. {
  2058. /* Call DMA error callback */
  2059. hadc->DMA_Handle->XferErrorCallback(hdma);
  2060. }
  2061. }
  2062. /**
  2063. * @brief DMA half transfer complete callback.
  2064. * @param hdma pointer to DMA handle.
  2065. * @retval None
  2066. */
  2067. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  2068. {
  2069. /* Retrieve ADC handle corresponding to current DMA handle */
  2070. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2071. /* Half conversion callback */
  2072. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2073. hadc->ConvHalfCpltCallback(hadc);
  2074. #else
  2075. HAL_ADC_ConvHalfCpltCallback(hadc);
  2076. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2077. }
  2078. /**
  2079. * @brief DMA error callback
  2080. * @param hdma pointer to DMA handle.
  2081. * @retval None
  2082. */
  2083. static void ADC_DMAError(DMA_HandleTypeDef *hdma)
  2084. {
  2085. /* Retrieve ADC handle corresponding to current DMA handle */
  2086. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2087. /* Set ADC state */
  2088. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  2089. /* Set ADC error code to DMA error */
  2090. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  2091. /* Error callback */
  2092. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2093. hadc->ErrorCallback(hadc);
  2094. #else
  2095. HAL_ADC_ErrorCallback(hadc);
  2096. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2097. }
  2098. /**
  2099. * @}
  2100. */
  2101. #endif /* HAL_ADC_MODULE_ENABLED */
  2102. /**
  2103. * @}
  2104. */
  2105. /**
  2106. * @}
  2107. */