stm32l0xx_hal_adc.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_adc.c
  4. * @author MCD Application Team
  5. * @version V1.7.0
  6. * @date 31-May-2016
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Analog to Digital Convertor (ADC)
  9. * peripheral:
  10. * + Initialization and de-initialization functions
  11. * ++ Initialization and Configuration of ADC
  12. * + Operation functions
  13. * ++ Start, stop, get result of conversions of regular
  14. * group, using 3 possible modes : polling, interruption or DMA.
  15. * + Control functions
  16. * ++ Channels configuration on regular group
  17. * ++ Analog Watchdog configuration
  18. * + State functions
  19. * ++ ADC state machine management
  20. * ++ Interrupts and flags management
  21. * Other functions (extended functions) are available in file
  22. * "stm32l0xx_hal_adc_ex.c".
  23. *
  24. @verbatim
  25. ==============================================================================
  26. ##### ADC peripheral features #####
  27. ==============================================================================
  28. [..]
  29. (+) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution
  30. (+) A built-in hardware oversampler can handle multiple conversions and average
  31. them into a single data with increased data width, up to 16-bit.
  32. (+) Interrupt generation at the end of regular conversion and in case of
  33. analog watchdog or overrun events.
  34. (+) Single and continuous conversion modes.
  35. (+) Scan mode for conversion of several channels sequentially.
  36. (+) Data alignment with in-built data coherency.
  37. (+) Programmable sampling time (common for all channels)
  38. (+) ADC conversion of regular group.
  39. (+) External trigger (timer or EXTI) with configurable polarity
  40. (+) DMA request generation for transfer of conversions data of regular group.
  41. (+) ADC calibration
  42. (+) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at
  43. slower speed.
  44. (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to
  45. Vdda or to an external voltage reference).
  46. ##### How to use this driver #####
  47. ==============================================================================
  48. [..]
  49. *** Configuration of top level parameters related to ADC ***
  50. ============================================================
  51. [..]
  52. (#) Enable the ADC interface
  53. (++) As prerequisite, ADC clock must be configured at RCC top level.
  54. Caution: On STM32L0, ADC clock frequency max is 16MHz (refer
  55. to device datasheet).
  56. Therefore, ADC clock prescaler must be configured in
  57. function of ADC clock source frequency to remain below
  58. this maximum frequency.
  59. (++) Two clock settings are mandatory:
  60. (+++) ADC clock (core clock, also possibly conversion clock).
  61. (+++) ADC clock (conversions clock).
  62. Two possible clock sources: synchronous clock derived from APB clock
  63. or asynchronous clock derived from ADC dedicated HSI RC oscillator
  64. 16MHz.
  65. If asynchronous clock is selected, parameter "HSIState" must be set either:
  66. - to "...HSIState = RCC_HSI_ON" to maintain the HSI16 oscillator
  67. always enabled: can be used to supply the main system clock.
  68. (+++) Example:
  69. Into HAL_ADC_MspInit() (recommended code location) or with
  70. other device clock parameters configuration:
  71. (+++) __HAL_RCC_ADC1_CLK_ENABLE(); (mandatory)
  72. HSI16 enable : (optional: if asynchronous clock selected)
  73. (+++) RCC_OscInitTypeDef RCC_OscInitStructure;
  74. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  75. (+++) RCC_OscInitStructure.HSI16CalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  76. (+++) RCC_OscInitStructure.HSIState = RCC_HSI_ON;
  77. (+++) RCC_OscInitStructure.PLL... (optional if used for system clock)
  78. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  79. (++) ADC clock source and clock prescaler are configured at ADC level with
  80. parameter "ClockPrescaler" using function HAL_ADC_Init().
  81. (#) ADC pins configuration
  82. (++) Enable the clock for the ADC GPIOs
  83. using macro __HAL_RCC_GPIOx_CLK_ENABLE()
  84. (++) Configure these ADC pins in analog mode
  85. using function HAL_GPIO_Init()
  86. (#) Optionally, in case of usage of ADC with interruptions:
  87. (++) Configure the NVIC for ADC
  88. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  89. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  90. into the function of corresponding ADC interruption vector
  91. ADCx_IRQHandler().
  92. (#) Optionally, in case of usage of DMA:
  93. (++) Configure the DMA (DMA channel, mode normal or circular, ...)
  94. using function HAL_DMA_Init().
  95. (++) Configure the NVIC for DMA
  96. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  97. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  98. into the function of corresponding DMA interruption vector
  99. DMAx_Channelx_IRQHandler().
  100. *** Configuration of ADC, group regular, channels parameters ***
  101. ================================================================
  102. [..]
  103. (#) Configure the ADC parameters (resolution, data alignment, oversampler, continuous mode, ...)
  104. and regular group parameters (conversion trigger, sequencer, ...)
  105. using function HAL_ADC_Init().
  106. (#) Configure the channels for regular group parameters (channel number,
  107. channel rank into sequencer, ..., into regular group)
  108. using function HAL_ADC_ConfigChannel().
  109. (#) Optionally, configure the analog watchdog parameters (channels
  110. monitored, thresholds, ...)
  111. using function HAL_ADC_AnalogWDGConfig().
  112. (#) When device is in mode low-power (low-power run, low-power sleep or stop mode),
  113. function "HAL_ADCEx_EnableVREFINT()" must be called before function HAL_ADC_Init().
  114. In case of internal temperature sensor to be measured:
  115. function "HAL_ADCEx_EnableVREFINTTempSensor()" must be called similarilly
  116. *** Execution of ADC conversions ***
  117. ====================================
  118. [..]
  119. (#) Optionally, perform an automatic ADC calibration to improve the
  120. conversion accuracy
  121. using function HAL_ADCEx_Calibration_Start().
  122. (#) ADC driver can be used among three modes: polling, interruption,
  123. transfer by DMA.
  124. (++) ADC conversion by polling:
  125. (+++) Activate the ADC peripheral and start conversions
  126. using function HAL_ADC_Start()
  127. (+++) Wait for ADC conversion completion
  128. using function HAL_ADC_PollForConversion()
  129. (+++) Retrieve conversion results
  130. using function HAL_ADC_GetValue()
  131. (+++) Stop conversion and disable the ADC peripheral
  132. using function HAL_ADC_Stop()
  133. (++) ADC conversion by interruption:
  134. (+++) Activate the ADC peripheral and start conversions
  135. using function HAL_ADC_Start_IT()
  136. (+++) Wait for ADC conversion completion by call of function
  137. HAL_ADC_ConvCpltCallback()
  138. (this function must be implemented in user program)
  139. (+++) Retrieve conversion results
  140. using function HAL_ADC_GetValue()
  141. (+++) Stop conversion and disable the ADC peripheral
  142. using function HAL_ADC_Stop_IT()
  143. (++) ADC conversion with transfer by DMA:
  144. (+++) Activate the ADC peripheral and start conversions
  145. using function HAL_ADC_Start_DMA()
  146. (+++) Wait for ADC conversion completion by call of function
  147. HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
  148. (these functions must be implemented in user program)
  149. (+++) Conversion results are automatically transferred by DMA into
  150. destination variable address.
  151. (+++) Stop conversion and disable the ADC peripheral
  152. using function HAL_ADC_Stop_DMA()
  153. [..]
  154. (@) Callback functions must be implemented in user program:
  155. (+@) HAL_ADC_ErrorCallback()
  156. (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog)
  157. (+@) HAL_ADC_ConvCpltCallback()
  158. (+@) HAL_ADC_ConvHalfCpltCallback
  159. *** Deinitialization of ADC ***
  160. ============================================================
  161. [..]
  162. (#) Disable the ADC interface
  163. (++) ADC clock can be hard reset and disabled at RCC top level.
  164. (++) Hard reset of ADC peripherals
  165. using macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET().
  166. (++) ADC clock disable
  167. using the equivalent macro/functions as configuration step.
  168. (+++) Example:
  169. Into HAL_ADC_MspDeInit() (recommended code location) or with
  170. other device clock parameters configuration:
  171. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  172. (+++) RCC_OscInitStructure.HSIState = RCC_HSI_OFF; (if not used for system clock)
  173. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  174. (#) ADC pins configuration
  175. (++) Disable the clock for the ADC GPIOs
  176. using macro __HAL_RCC_GPIOx_CLK_DISABLE()
  177. (#) Optionally, in case of usage of ADC with interruptions:
  178. (++) Disable the NVIC for ADC
  179. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  180. (#) Optionally, in case of usage of DMA:
  181. (++) Deinitialize the DMA
  182. using function HAL_DMA_Init().
  183. (++) Disable the NVIC for DMA
  184. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  185. [..]
  186. @endverbatim
  187. ******************************************************************************
  188. * @attention
  189. *
  190. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  191. *
  192. * Redistribution and use in source and binary forms, with or without modification,
  193. * are permitted provided that the following conditions are met:
  194. * 1. Redistributions of source code must retain the above copyright notice,
  195. * this list of conditions and the following disclaimer.
  196. * 2. Redistributions in binary form must reproduce the above copyright notice,
  197. * this list of conditions and the following disclaimer in the documentation
  198. * and/or other materials provided with the distribution.
  199. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  200. * may be used to endorse or promote products derived from this software
  201. * without specific prior written permission.
  202. *
  203. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  204. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  205. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  206. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  207. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  208. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  209. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  210. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  211. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  212. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  213. *
  214. ******************************************************************************
  215. */
  216. /* Includes ------------------------------------------------------------------*/
  217. #include "stm32l0xx_hal.h"
  218. /** @addtogroup STM32L0xx_HAL_Driver
  219. * @{
  220. */
  221. #ifdef HAL_ADC_MODULE_ENABLED
  222. /** @addtogroup ADC
  223. * @brief ADC driver modules
  224. * @{
  225. */
  226. /** @addtogroup ADC_Private
  227. * @{
  228. */
  229. /* Private typedef -----------------------------------------------------------*/
  230. /* Private define ------------------------------------------------------------*/
  231. /* Delay for ADC stabilization time. */
  232. /* Maximum delay is 1us (refer to device datasheet, parameter tSTART). */
  233. /* Unit: us */
  234. #define ADC_STAB_DELAY_US ((uint32_t) 1U)
  235. /* Delay for temperature sensor stabilization time. */
  236. /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */
  237. /* Unit: us */
  238. #define ADC_TEMPSENSOR_DELAY_US ((uint32_t) 10U)
  239. /**
  240. * @}
  241. */
  242. /* Private macro -------------------------------------------------------------*/
  243. /* Private variables ---------------------------------------------------------*/
  244. /* Private function prototypes -----------------------------------------------*/
  245. /** @addtogroup ADC_Private
  246. * @{
  247. */
  248. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc);
  249. static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc);
  250. static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc);
  251. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
  252. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
  253. static void ADC_DMAError(DMA_HandleTypeDef *hdma);
  254. static void ADC_DelayMicroSecond(uint32_t microSecond);
  255. /**
  256. * @}
  257. */
  258. /** @addtogroup ADC_Exported_Functions
  259. * @{
  260. */
  261. /** @addtogroup ADC_Exported_Functions_Group1
  262. * @brief Initialization and Configuration functions
  263. *
  264. @verbatim
  265. ===============================================================================
  266. ##### Initialization and de-initialization functions #####
  267. ===============================================================================
  268. [..] This section provides functions allowing to:
  269. (+) Initialize and configure the ADC.
  270. (+) De-initialize the ADC.
  271. @endverbatim
  272. * @{
  273. */
  274. /**
  275. * @brief Initializes the ADC peripheral and regular group according to
  276. * parameters specified in structure "ADC_InitTypeDef".
  277. * @note As prerequisite, ADC clock must be configured at RCC top level
  278. * depending on both possible clock sources: APB clock of HSI clock.
  279. * See commented example code below that can be copied and uncommented
  280. * into HAL_ADC_MspInit().
  281. * @note Possibility to update parameters on the fly:
  282. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  283. * coming from ADC state reset. Following calls to this function can
  284. * be used to reconfigure some parameters of ADC_InitTypeDef
  285. * structure on the fly, without modifying MSP configuration. If ADC
  286. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  287. * before HAL_ADC_Init().
  288. * The setting of these parameters is conditioned to ADC state.
  289. * For parameters constraints, see comments of structure
  290. * "ADC_InitTypeDef".
  291. * @note This function configures the ADC within 2 scopes: scope of entire
  292. * ADC and scope of regular group. For parameters details, see comments
  293. * of structure "ADC_InitTypeDef".
  294. * @note When device is in mode low-power (low-power run, low-power sleep or stop mode),
  295. * function "HAL_ADCEx_EnableVREFINT()" must be called before function HAL_ADC_Init()
  296. * (in case of previous ADC operations: function HAL_ADC_DeInit() must be called first).
  297. * In case of internal temperature sensor to be measured:
  298. * function "HAL_ADCEx_EnableVREFINTTempSensor()" must be called similarilly.
  299. * @param hadc: ADC handle
  300. * @retval HAL status
  301. */
  302. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  303. {
  304. /* Check ADC handle */
  305. if(hadc == NULL)
  306. {
  307. return HAL_ERROR;
  308. }
  309. /* Check the parameters */
  310. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  311. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  312. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  313. assert_param(IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTime));
  314. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  315. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  316. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  317. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  318. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  319. assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
  320. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  321. assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
  322. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
  323. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerFrequencyMode));
  324. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoPowerOff));
  325. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
  326. /* As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured */
  327. /* at RCC top level depending on both possible clock sources: */
  328. /* APB clock or HSI clock. */
  329. /* Refer to header of this file for more details on clock enabling procedure*/
  330. /* Actions performed only if ADC is coming from state reset: */
  331. /* - Initialization of ADC MSP */
  332. /* - ADC voltage regulator enable */
  333. if(hadc->State == HAL_ADC_STATE_RESET)
  334. {
  335. /* Initialize ADC error code */
  336. ADC_CLEAR_ERRORCODE(hadc);
  337. /* Allocate lock resource and initialize it */
  338. hadc->Lock = HAL_UNLOCKED;
  339. /* Init the low level hardware */
  340. HAL_ADC_MspInit(hadc);
  341. }
  342. /* Configuration of ADC parameters if previous preliminary actions are */
  343. /* correctly completed. */
  344. /* and if there is no conversion on going on regular group (ADC can be */
  345. /* enabled anyway, in case of call of this function to update a parameter */
  346. /* on the fly). */
  347. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) ||
  348. (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) != RESET) )
  349. {
  350. /* Update ADC state machine to error */
  351. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  352. /* Process unlocked */
  353. __HAL_UNLOCK(hadc);
  354. return HAL_ERROR;
  355. }
  356. /* Set ADC state */
  357. ADC_STATE_CLR_SET(hadc->State,
  358. HAL_ADC_STATE_REG_BUSY,
  359. HAL_ADC_STATE_BUSY_INTERNAL);
  360. /* Parameters update conditioned to ADC state: */
  361. /* Parameters that can be updated only when ADC is disabled: */
  362. /* - ADC clock mode */
  363. /* - ADC clock prescaler */
  364. /* - ADC Resolution */
  365. if (ADC_IS_ENABLE(hadc) == RESET)
  366. {
  367. /* Some parameters of this register are not reset, since they are set */
  368. /* by other functions and must be kept in case of usage of this */
  369. /* function on the fly (update of a parameter of ADC_InitTypeDef */
  370. /* without needing to reconfigure all other ADC groups/channels */
  371. /* parameters): */
  372. /* - internal measurement paths: Vbat, temperature sensor, Vref */
  373. /* (set into HAL_ADC_ConfigChannel() ) */
  374. /* Configuration of ADC clock: clock source PCLK or asynchronous with
  375. selectable prescaler */
  376. __HAL_ADC_CLOCK_PRESCALER(hadc);
  377. /* Configuration of ADC: */
  378. /* - Resolution */
  379. hadc->Instance->CFGR1 &= ~( ADC_CFGR1_RES);
  380. hadc->Instance->CFGR1 |= hadc->Init.Resolution;
  381. }
  382. /* Set the Low Frequency mode */
  383. ADC->CCR &= (uint32_t)~ADC_CCR_LFMEN;
  384. ADC->CCR |=__HAL_ADC_CCR_LOWFREQUENCY(hadc->Init.LowPowerFrequencyMode);
  385. /* Enable voltage regulator (if disabled at this step) */
  386. if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN))
  387. {
  388. /* Set ADVREGEN bit */
  389. hadc->Instance->CR |= ADC_CR_ADVREGEN;
  390. }
  391. /* Configuration of ADC: */
  392. /* - Resolution */
  393. /* - Data alignment */
  394. /* - Scan direction */
  395. /* - External trigger to start conversion */
  396. /* - External trigger polarity */
  397. /* - Continuous conversion mode */
  398. /* - DMA continuous request */
  399. /* - Overrun */
  400. /* - AutoDelay feature */
  401. /* - Discontinuous mode */
  402. hadc->Instance->CFGR1 &= ~(ADC_CFGR1_ALIGN |
  403. ADC_CFGR1_SCANDIR |
  404. ADC_CFGR1_EXTSEL |
  405. ADC_CFGR1_EXTEN |
  406. ADC_CFGR1_CONT |
  407. ADC_CFGR1_DMACFG |
  408. ADC_CFGR1_OVRMOD |
  409. ADC_CFGR1_AUTDLY |
  410. ADC_CFGR1_AUTOFF |
  411. ADC_CFGR1_DISCEN);
  412. hadc->Instance->CFGR1 |= (hadc->Init.DataAlign |
  413. ADC_SCANDIR(hadc->Init.ScanConvMode) |
  414. ADC_CONTINUOUS(hadc->Init.ContinuousConvMode) |
  415. ADC_DMACONTREQ(hadc->Init.DMAContinuousRequests) |
  416. hadc->Init.Overrun |
  417. __HAL_ADC_CFGR1_AutoDelay(hadc->Init.LowPowerAutoWait) |
  418. __HAL_ADC_CFGR1_AUTOFF(hadc->Init.LowPowerAutoPowerOff));
  419. /* Enable external trigger if trigger selection is different of software */
  420. /* start. */
  421. /* Note: This configuration keeps the hardware feature of parameter */
  422. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  423. /* software start. */
  424. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  425. {
  426. hadc->Instance->CFGR1 |= hadc->Init.ExternalTrigConv |
  427. hadc->Init.ExternalTrigConvEdge;
  428. }
  429. /* Enable discontinuous mode only if continuous mode is disabled */
  430. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  431. {
  432. if (hadc->Init.ContinuousConvMode == DISABLE)
  433. {
  434. /* Enable the selected ADC group regular discontinuous mode */
  435. hadc->Instance->CFGR1 |= (ADC_CFGR1_DISCEN);
  436. }
  437. else
  438. {
  439. /* ADC regular group discontinuous was intended to be enabled, */
  440. /* but ADC regular group modes continuous and sequencer discontinuous */
  441. /* cannot be enabled simultaneously. */
  442. /* Update ADC state machine to error */
  443. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  444. /* Set ADC error code to ADC IP internal error */
  445. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  446. }
  447. }
  448. if (hadc->Init.OversamplingMode == ENABLE)
  449. {
  450. assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversample.Ratio));
  451. assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversample.RightBitShift));
  452. assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversample.TriggeredMode));
  453. /* Configuration of Oversampler: */
  454. /* - Oversampling Ratio */
  455. /* - Right bit shift */
  456. /* - Triggered mode */
  457. hadc->Instance->CFGR2 &= ~( ADC_CFGR2_OVSR |
  458. ADC_CFGR2_OVSS |
  459. ADC_CFGR2_TOVS );
  460. hadc->Instance->CFGR2 |= ( hadc->Init.Oversample.Ratio |
  461. hadc->Init.Oversample.RightBitShift |
  462. hadc->Init.Oversample.TriggeredMode );
  463. /* Enable OverSampling mode */
  464. hadc->Instance->CFGR2 |= ADC_CFGR2_OVSE;
  465. }
  466. else
  467. {
  468. if(HAL_IS_BIT_SET(hadc->Instance->CFGR2, ADC_CFGR2_OVSE))
  469. {
  470. /* Disable OverSampling mode if needed */
  471. hadc->Instance->CFGR2 &= ~ADC_CFGR2_OVSE;
  472. }
  473. }
  474. /* Clear the old sampling time */
  475. hadc->Instance->SMPR &= (uint32_t)(~ADC_SMPR_SMPR);
  476. /* Set the new sample time */
  477. hadc->Instance->SMPR |= hadc->Init.SamplingTime;
  478. /* Clear ADC error code */
  479. ADC_CLEAR_ERRORCODE(hadc);
  480. /* Set the ADC state */
  481. ADC_STATE_CLR_SET(hadc->State,
  482. HAL_ADC_STATE_BUSY_INTERNAL,
  483. HAL_ADC_STATE_READY);
  484. /* Return function status */
  485. return HAL_OK;
  486. }
  487. /**
  488. * @brief Deinitialize the ADC peripheral registers to their default reset
  489. * values, with deinitialization of the ADC MSP.
  490. * @note For devices with several ADCs: reset of ADC common registers is done
  491. * only if all ADCs sharing the same common group are disabled.
  492. * If this is not the case, reset of these common parameters reset is
  493. * bypassed without error reporting: it can be the intended behavior in
  494. * case of reset of a single ADC while the other ADCs sharing the same
  495. * common group is still running.
  496. * @param hadc: ADC handle
  497. * @retval HAL status
  498. */
  499. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  500. {
  501. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  502. /* Check ADC handle */
  503. if(hadc == NULL)
  504. {
  505. return HAL_ERROR;
  506. }
  507. /* Check the parameters */
  508. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  509. /* Set ADC state */
  510. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  511. /* Stop potential conversion on going, on regular group */
  512. tmp_hal_status = ADC_ConversionStop(hadc);
  513. /* Disable ADC peripheral if conversions are effectively stopped */
  514. if (tmp_hal_status == HAL_OK)
  515. {
  516. /* Disable the ADC peripheral */
  517. tmp_hal_status = ADC_Disable(hadc);
  518. /* Check if ADC is effectively disabled */
  519. if (tmp_hal_status != HAL_ERROR)
  520. {
  521. /* Change ADC state */
  522. hadc->State = HAL_ADC_STATE_READY;
  523. }
  524. }
  525. /* Configuration of ADC parameters if previous preliminary actions are */
  526. /* correctly completed. */
  527. if (tmp_hal_status != HAL_ERROR)
  528. {
  529. /* ========== Reset ADC registers ========== */
  530. /* Reset register IER */
  531. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD | ADC_IT_OVR | ADC_IT_EOCAL | ADC_IT_EOS | \
  532. ADC_IT_EOC | ADC_IT_RDY | ADC_IT_EOSMP ));
  533. /* Reset register ISR */
  534. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_EOCAL | ADC_FLAG_OVR | ADC_FLAG_EOS | \
  535. ADC_FLAG_EOC | ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  536. /* Reset register CR */
  537. /* Disable voltage regulator */
  538. /* Note: Regulator disable useful for power saving */
  539. /* Reset ADVREGEN bit */
  540. hadc->Instance->CR &= ~ADC_CR_ADVREGEN;
  541. /* Bits ADC_CR_ADSTP, ADC_CR_ADSTART are in access mode "read-set": no direct reset applicable */
  542. /* No action */
  543. /* Reset register CFGR1 */
  544. hadc->Instance->CFGR1 &= ~(ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | \
  545. ADC_CFGR1_DISCEN | ADC_CFGR1_AUTOFF | ADC_CFGR1_AUTDLY | \
  546. ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD | ADC_CFGR1_EXTEN | \
  547. ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RES | \
  548. ADC_CFGR1_SCANDIR| ADC_CFGR1_DMACFG | ADC_CFGR1_DMAEN);
  549. /* Reset register CFGR2 */
  550. hadc->Instance->CFGR2 &= ~(ADC_CFGR2_TOVS | ADC_CFGR2_OVSS | ADC_CFGR2_OVSR | \
  551. ADC_CFGR2_OVSE | ADC_CFGR2_CKMODE );
  552. /* Reset register SMPR */
  553. hadc->Instance->SMPR &= ~(ADC_SMPR_SMPR);
  554. /* Reset register TR */
  555. hadc->Instance->TR &= ~(ADC_TR_LT | ADC_TR_HT);
  556. /* Reset register CALFACT */
  557. hadc->Instance->CALFACT &= ~(ADC_CALFACT_CALFACT);
  558. /* Reset register DR */
  559. /* bits in access mode read only, no direct reset applicable*/
  560. /* Reset register CALFACT */
  561. hadc->Instance->CALFACT &= ~(ADC_CALFACT_CALFACT);
  562. /* ========== Hard reset ADC peripheral ========== */
  563. /* Performs a global reset of the entire ADC peripheral: ADC state is */
  564. /* forced to a similar state after device power-on. */
  565. /* If needed, copy-paste and uncomment the following reset code into */
  566. /* function "void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)": */
  567. /* */
  568. /* __HAL_RCC_ADC1_FORCE_RESET() */
  569. /* __HAL_RCC_ADC1_RELEASE_RESET() */
  570. /* DeInit the low level hardware */
  571. HAL_ADC_MspDeInit(hadc);
  572. /* Set ADC error code to none */
  573. ADC_CLEAR_ERRORCODE(hadc);
  574. /* Set ADC state */
  575. hadc->State = HAL_ADC_STATE_RESET;
  576. }
  577. /* Process unlocked */
  578. __HAL_UNLOCK(hadc);
  579. /* Return function status */
  580. return tmp_hal_status;
  581. }
  582. /**
  583. * @brief Initializes the ADC MSP.
  584. * @param hadc: ADC handle
  585. * @retval None
  586. */
  587. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  588. {
  589. /* Prevent unused argument(s) compilation warning */
  590. UNUSED(hadc);
  591. /* NOTE : This function Should not be modified, when the callback is needed,
  592. the HAL_ADC_MspInit could be implemented in the user file
  593. */
  594. }
  595. /**
  596. * @brief DeInitializes the ADC MSP.
  597. * @param hadc: ADC handle
  598. * @retval None
  599. */
  600. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
  601. {
  602. /* Prevent unused argument(s) compilation warning */
  603. UNUSED(hadc);
  604. /* NOTE : This function should not be modified. When the callback is needed,
  605. function HAL_ADC_MspDeInit must be implemented in the user file.
  606. */
  607. }
  608. /**
  609. * @}
  610. */
  611. /** @addtogroup ADC_Exported_Functions_Group2
  612. * @brief I/O operation functions
  613. *
  614. @verbatim
  615. ===============================================================================
  616. ##### IO operation functions #####
  617. ===============================================================================
  618. [..] This section provides functions allowing to:
  619. (+) Start conversion of regular group.
  620. (+) Stop conversion of regular group.
  621. (+) Poll for conversion complete on regular group.
  622. (+) poll for conversion event.
  623. (+) Get result of regular channel conversion.
  624. (+) Start conversion of regular group and enable interruptions.
  625. (+) Stop conversion of regular group and disable interruptions.
  626. (+) Handle ADC interrupt request
  627. (+) Start conversion of regular group and enable DMA transfer.
  628. (+) Stop conversion of regular group and disable ADC DMA transfer.
  629. @endverbatim
  630. * @{
  631. */
  632. /**
  633. * @brief Enables ADC, starts conversion of regular group.
  634. * Interruptions enabled in this function: None.
  635. * @param hadc: ADC handle
  636. * @retval HAL status
  637. */
  638. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  639. {
  640. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  641. /* Check the parameters */
  642. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  643. /* Perform ADC enable and conversion start if no conversion is on going */
  644. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  645. {
  646. /* Process locked */
  647. __HAL_LOCK(hadc);
  648. /* Enable the ADC peripheral */
  649. /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
  650. /* performed automatically by hardware. */
  651. if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
  652. {
  653. tmp_hal_status = ADC_Enable(hadc);
  654. }
  655. /* Start conversion if ADC is effectively enabled */
  656. if (tmp_hal_status == HAL_OK)
  657. {
  658. /* Set ADC state */
  659. /* - Clear state bitfield related to regular group conversion results */
  660. /* - Set state bitfield related to regular operation */
  661. ADC_STATE_CLR_SET(hadc->State,
  662. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  663. HAL_ADC_STATE_REG_BUSY);
  664. /* Reset ADC all error code fields */
  665. ADC_CLEAR_ERRORCODE(hadc);
  666. /* Process unlocked */
  667. /* Unlock before starting ADC conversions: in case of potential */
  668. /* interruption, to let the process to ADC IRQ Handler. */
  669. __HAL_UNLOCK(hadc);
  670. /* Clear regular group conversion flag and overrun flag */
  671. /* (To ensure of no unknown state from potential previous ADC */
  672. /* operations) */
  673. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  674. /* Enable conversion of regular group. */
  675. /* If software start has been selected, conversion starts immediately. */
  676. /* If external trigger has been selected, conversion will start at next */
  677. /* trigger event. */
  678. hadc->Instance->CR |= ADC_CR_ADSTART;
  679. }
  680. }
  681. else
  682. {
  683. tmp_hal_status = HAL_BUSY;
  684. }
  685. /* Return function status */
  686. return tmp_hal_status;
  687. }
  688. /**
  689. * @brief Stop ADC conversion of regular group, disable ADC peripheral.
  690. * @param hadc: ADC handle
  691. * @retval HAL status.
  692. */
  693. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  694. {
  695. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  696. /* Check the parameters */
  697. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  698. /* Process locked */
  699. __HAL_LOCK(hadc);
  700. /* 1. Stop potential conversion on going, on regular group */
  701. tmp_hal_status = ADC_ConversionStop(hadc);
  702. /* Disable ADC peripheral if conversions are effectively stopped */
  703. if (tmp_hal_status == HAL_OK)
  704. {
  705. /* 2. Disable the ADC peripheral */
  706. tmp_hal_status = ADC_Disable(hadc);
  707. /* Check if ADC is effectively disabled */
  708. if (tmp_hal_status == HAL_OK)
  709. {
  710. /* Set ADC state */
  711. ADC_STATE_CLR_SET(hadc->State,
  712. HAL_ADC_STATE_REG_BUSY,
  713. HAL_ADC_STATE_READY);
  714. }
  715. }
  716. /* Process unlocked */
  717. __HAL_UNLOCK(hadc);
  718. /* Return function status */
  719. return tmp_hal_status;
  720. }
  721. /**
  722. * @brief Wait for regular group conversion to be completed.
  723. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  724. * conversion) are cleared by this function, with an exception:
  725. * if low power feature "LowPowerAutoWait" is enabled, flags are
  726. * not cleared to not interfere with this feature until data register
  727. * is read using function HAL_ADC_GetValue().
  728. * @note This function cannot be used in a particular setup: ADC configured
  729. * in DMA mode and polling for end of each conversion (ADC init
  730. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  731. * In this case, DMA resets the flag EOC and polling cannot be
  732. * performed on each conversion. Nevertheless, polling can still
  733. * be performed on the complete sequence (ADC init
  734. * parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
  735. * @param hadc: ADC handle
  736. * @param Timeout: Timeout value in millisecond.
  737. * @retval HAL status
  738. */
  739. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  740. {
  741. uint32_t tickstart;
  742. uint32_t tmp_Flag_EOC;
  743. /* Check the parameters */
  744. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  745. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  746. /* If end of conversion selected to end of sequence */
  747. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  748. {
  749. tmp_Flag_EOC = ADC_FLAG_EOS;
  750. }
  751. /* If end of conversion selected to end of each conversion */
  752. else /* ADC_EOC_SINGLE_CONV */
  753. {
  754. /* Verification that ADC configuration is compliant with polling for */
  755. /* each conversion: */
  756. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  757. /* several ranks and polling for end of each conversion. */
  758. /* For code simplicity sake, this particular case is generalized to */
  759. /* ADC configured in DMA mode and and polling for end of each conversion. */
  760. if (HAL_IS_BIT_SET(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN))
  761. {
  762. /* Update ADC state machine to error */
  763. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  764. /* Process unlocked */
  765. __HAL_UNLOCK(hadc);
  766. return HAL_ERROR;
  767. }
  768. else
  769. {
  770. tmp_Flag_EOC = (ADC_FLAG_EOC | ADC_FLAG_EOS);
  771. }
  772. }
  773. /* Get tick count */
  774. tickstart = HAL_GetTick();
  775. /* Wait until End of Conversion flag is raised */
  776. while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
  777. {
  778. /* Check if timeout is disabled (set to infinite wait) */
  779. if(Timeout != HAL_MAX_DELAY)
  780. {
  781. if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
  782. {
  783. /* Update ADC state machine to timeout */
  784. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  785. /* Process unlocked */
  786. __HAL_UNLOCK(hadc);
  787. return HAL_TIMEOUT;
  788. }
  789. }
  790. }
  791. /* Update ADC state machine */
  792. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  793. /* Determine whether any further conversion upcoming on group regular */
  794. /* by external trigger, continuous mode or scan sequence on going. */
  795. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  796. (hadc->Init.ContinuousConvMode == DISABLE) )
  797. {
  798. /* If End of Sequence is reached, disable interrupts */
  799. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  800. {
  801. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  802. /* ADSTART==0 (no conversion on going) */
  803. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  804. {
  805. /* Disable ADC end of single conversion interrupt on group regular */
  806. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  807. /* HAL_Start_IT(), but is not disabled here because can be used */
  808. /* by overrun IRQ process below. */
  809. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  810. /* Set ADC state */
  811. ADC_STATE_CLR_SET(hadc->State,
  812. HAL_ADC_STATE_REG_BUSY,
  813. HAL_ADC_STATE_READY);
  814. }
  815. else
  816. {
  817. /* Change ADC state to error state */
  818. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  819. /* Set ADC error code to ADC IP internal error */
  820. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  821. }
  822. }
  823. }
  824. /* Clear end of conversion flag of regular group if low power feature */
  825. /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
  826. /* until data register is read using function HAL_ADC_GetValue(). */
  827. if (hadc->Init.LowPowerAutoWait == DISABLE)
  828. {
  829. /* Clear regular group conversion flag */
  830. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  831. }
  832. /* Return ADC state */
  833. return HAL_OK;
  834. }
  835. /**
  836. * @brief Poll for conversion event.
  837. * @param hadc: ADC handle
  838. * @param EventType: the ADC event type.
  839. * This parameter can be one of the following values:
  840. * @arg ADC_AWD_EVENT: ADC Analog watchdog event
  841. * @arg ADC_OVR_EVENT: ADC Overrun event
  842. * @param Timeout: Timeout value in millisecond.
  843. * @retval HAL status
  844. */
  845. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  846. {
  847. uint32_t tickstart = 0U;
  848. /* Check the parameters */
  849. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  850. assert_param(IS_ADC_EVENT_TYPE(EventType));
  851. /* Get tick count */
  852. tickstart = HAL_GetTick();
  853. /* Check selected event flag */
  854. while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  855. {
  856. /* Check if timeout is disabled (set to infinite wait) */
  857. if(Timeout != HAL_MAX_DELAY)
  858. {
  859. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  860. {
  861. /* Update ADC state machine to timeout */
  862. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  863. /* Process unlocked */
  864. __HAL_UNLOCK(hadc);
  865. return HAL_TIMEOUT;
  866. }
  867. }
  868. }
  869. switch(EventType)
  870. {
  871. /* Analog watchdog (level out of window) event */
  872. case ADC_AWD_EVENT:
  873. /* Set ADC state */
  874. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  875. /* Clear ADC analog watchdog flag */
  876. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  877. break;
  878. /* Overrun event */
  879. default: /* Case ADC_OVR_EVENT */
  880. /* If overrun is set to overwrite previous data, overrun event is not */
  881. /* considered as an error. */
  882. /* (cf ref manual "Managing conversions without using the DMA and without */
  883. /* overrun ") */
  884. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  885. {
  886. /* Set ADC state */
  887. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  888. /* Set ADC error code to overrun */
  889. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  890. }
  891. /* Clear ADC Overrun flag */
  892. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  893. break;
  894. }
  895. /* Return ADC state */
  896. return HAL_OK;
  897. }
  898. /**
  899. * @brief Enables ADC, starts conversion of regular group with interruption.
  900. * Interruptions enabled in this function:
  901. * - EOC (end of conversion of regular group) or EOS (end of
  902. * sequence of regular group) depending on ADC initialization
  903. * parameter "EOCSelection"
  904. * - overrun (if available)
  905. * Each of these interruptions has its dedicated callback function.
  906. * @param hadc: ADC handle
  907. * @retval HAL status
  908. */
  909. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  910. {
  911. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  912. /* Check the parameters */
  913. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  914. /* Perform ADC enable and conversion start if no conversion is on going */
  915. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  916. {
  917. /* Process locked */
  918. __HAL_LOCK(hadc);
  919. /* Enable the ADC peripheral */
  920. /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
  921. /* performed automatically by hardware. */
  922. if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
  923. {
  924. tmp_hal_status = ADC_Enable(hadc);
  925. }
  926. /* Start conversion if ADC is effectively enabled */
  927. if (tmp_hal_status == HAL_OK)
  928. {
  929. /* Set ADC state */
  930. /* - Clear state bitfield related to regular group conversion results */
  931. /* - Set state bitfield related to regular operation */
  932. ADC_STATE_CLR_SET(hadc->State,
  933. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  934. HAL_ADC_STATE_REG_BUSY);
  935. /* Reset ADC all error code fields */
  936. ADC_CLEAR_ERRORCODE(hadc);
  937. /* Process unlocked */
  938. /* Unlock before starting ADC conversions: in case of potential */
  939. /* interruption, to let the process to ADC IRQ Handler. */
  940. __HAL_UNLOCK(hadc);
  941. /* Clear regular group conversion flag and overrun flag */
  942. /* (To ensure of no unknown state from potential previous ADC */
  943. /* operations) */
  944. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  945. /* Enable ADC end of conversion interrupt */
  946. /* Enable ADC overrun interrupt */
  947. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  948. switch(hadc->Init.EOCSelection)
  949. {
  950. case ADC_EOC_SEQ_CONV:
  951. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  952. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOS | ADC_IT_OVR));
  953. break;
  954. /* case ADC_EOC_SINGLE_CONV */
  955. default:
  956. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  957. break;
  958. }
  959. /* Enable conversion of regular group. */
  960. /* If software start has been selected, conversion starts immediately. */
  961. /* If external trigger has been selected, conversion will start at next */
  962. /* trigger event. */
  963. hadc->Instance->CR |= ADC_CR_ADSTART;
  964. }
  965. }
  966. else
  967. {
  968. tmp_hal_status = HAL_BUSY;
  969. }
  970. /* Return function status */
  971. return tmp_hal_status;
  972. }
  973. /**
  974. * @brief Stop ADC conversion of regular group, disable interruption of
  975. * end-of-conversion, disable ADC peripheral.
  976. * @param hadc: ADC handle
  977. * @retval HAL status.
  978. */
  979. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  980. {
  981. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  982. /* Check the parameters */
  983. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  984. /* Process locked */
  985. __HAL_LOCK(hadc);
  986. /* 1. Stop potential conversion on going, on regular group */
  987. tmp_hal_status = ADC_ConversionStop(hadc);
  988. /* Disable ADC peripheral if conversions are effectively stopped */
  989. if (tmp_hal_status == HAL_OK)
  990. {
  991. /* Disable ADC end of conversion interrupt for regular group */
  992. /* Disable ADC overrun interrupt */
  993. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  994. /* 2. Disable the ADC peripheral */
  995. tmp_hal_status = ADC_Disable(hadc);
  996. /* Check if ADC is effectively disabled */
  997. if (tmp_hal_status == HAL_OK)
  998. {
  999. /* Set ADC state */
  1000. ADC_STATE_CLR_SET(hadc->State,
  1001. HAL_ADC_STATE_REG_BUSY,
  1002. HAL_ADC_STATE_READY);
  1003. }
  1004. }
  1005. /* Process unlocked */
  1006. __HAL_UNLOCK(hadc);
  1007. /* Return function status */
  1008. return tmp_hal_status;
  1009. }
  1010. /**
  1011. * @brief Enables ADC, starts conversion of regular group and transfers result
  1012. * through DMA.
  1013. * Interruptions enabled in this function:
  1014. * - DMA transfer complete
  1015. * - DMA half transfer
  1016. * - overrun
  1017. * Each of these interruptions has its dedicated callback function.
  1018. * @param hadc: ADC handle
  1019. * @param pData: The destination Buffer address.
  1020. * @param Length: The length of data to be transferred from ADC peripheral to memory.
  1021. * @retval None
  1022. */
  1023. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  1024. {
  1025. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1026. /* Check the parameters */
  1027. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1028. /* Perform ADC enable and conversion start if no conversion is on going */
  1029. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1030. {
  1031. /* Process locked */
  1032. __HAL_LOCK(hadc);
  1033. /* Enable the ADC peripheral */
  1034. /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
  1035. /* performed automatically by hardware. */
  1036. if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
  1037. {
  1038. tmp_hal_status = ADC_Enable(hadc);
  1039. }
  1040. /* Start conversion if ADC is effectively enabled */
  1041. if (tmp_hal_status == HAL_OK)
  1042. {
  1043. /* Set ADC state */
  1044. /* - Clear state bitfield related to regular group conversion results */
  1045. /* - Set state bitfield related to regular operation */
  1046. ADC_STATE_CLR_SET(hadc->State,
  1047. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1048. HAL_ADC_STATE_REG_BUSY);
  1049. /* Reset ADC all error code fields */
  1050. ADC_CLEAR_ERRORCODE(hadc);
  1051. /* Process unlocked */
  1052. /* Unlock before starting ADC conversions: in case of potential */
  1053. /* interruption, to let the process to ADC IRQ Handler. */
  1054. __HAL_UNLOCK(hadc);
  1055. /* Set the DMA transfer complete callback */
  1056. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1057. /* Set the DMA half transfer complete callback */
  1058. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1059. /* Set the DMA error callback */
  1060. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1061. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  1062. /* start (in case of SW start): */
  1063. /* Clear regular group conversion flag and overrun flag */
  1064. /* (To ensure of no unknown state from potential previous ADC */
  1065. /* operations) */
  1066. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1067. /* Enable ADC overrun interrupt */
  1068. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1069. /* Enable ADC DMA mode */
  1070. hadc->Instance->CFGR1 |= ADC_CFGR1_DMAEN;
  1071. /* Start the DMA channel */
  1072. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1073. /* Enable conversion of regular group. */
  1074. /* If software start has been selected, conversion starts immediately. */
  1075. /* If external trigger has been selected, conversion will start at next */
  1076. /* trigger event. */
  1077. hadc->Instance->CR |= ADC_CR_ADSTART;
  1078. }
  1079. }
  1080. else
  1081. {
  1082. tmp_hal_status = HAL_BUSY;
  1083. }
  1084. /* Return function status */
  1085. return tmp_hal_status;
  1086. }
  1087. /**
  1088. * @brief Stop ADC conversion of regular group, disable ADC DMA transfer, disable
  1089. * ADC peripheral.
  1090. * Each of these interruptions has its dedicated callback function.
  1091. * @param hadc: ADC handle
  1092. * @retval HAL status.
  1093. */
  1094. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  1095. {
  1096. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1097. /* Check the parameters */
  1098. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1099. /* Process locked */
  1100. __HAL_LOCK(hadc);
  1101. /* 1. Stop potential conversion on going, on regular group */
  1102. tmp_hal_status = ADC_ConversionStop(hadc);
  1103. /* Disable ADC peripheral if conversions are effectively stopped */
  1104. if (tmp_hal_status == HAL_OK)
  1105. {
  1106. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  1107. hadc->Instance->CFGR1 &= ~ADC_CFGR1_DMAEN;
  1108. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  1109. /* while DMA transfer is on going) */
  1110. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1111. /* Check if DMA channel effectively disabled */
  1112. if (tmp_hal_status != HAL_OK)
  1113. {
  1114. /* Update ADC state machine to error */
  1115. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1116. }
  1117. /* Disable ADC overrun interrupt */
  1118. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1119. /* 2. Disable the ADC peripheral */
  1120. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep */
  1121. /* in memory a potential failing status. */
  1122. if (tmp_hal_status == HAL_OK)
  1123. {
  1124. tmp_hal_status = ADC_Disable(hadc);
  1125. }
  1126. else
  1127. {
  1128. ADC_Disable(hadc);
  1129. }
  1130. /* Check if ADC is effectively disabled */
  1131. if (tmp_hal_status == HAL_OK)
  1132. {
  1133. /* Set ADC state */
  1134. ADC_STATE_CLR_SET(hadc->State,
  1135. HAL_ADC_STATE_REG_BUSY,
  1136. HAL_ADC_STATE_READY);
  1137. }
  1138. }
  1139. /* Process unlocked */
  1140. __HAL_UNLOCK(hadc);
  1141. /* Return function status */
  1142. return tmp_hal_status;
  1143. }
  1144. /**
  1145. * @brief Get ADC regular group conversion result.
  1146. * @note Reading DR register automatically clears EOC (end of conversion of
  1147. * regular group) flag.
  1148. * @note This function does not clear ADC flag EOS
  1149. * (ADC group regular end of sequence conversion).
  1150. * Occurrence of flag EOS rising:
  1151. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  1152. * to flag EOC.
  1153. * - If sequencer is composed of several ranks, during the scan
  1154. * sequence flag EOC only is raised, at the end of the scan sequence
  1155. * both flags EOC and EOS are raised.
  1156. * To clear this flag, either use function:
  1157. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1158. * model polling: @ref HAL_ADC_PollForConversion()
  1159. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  1160. * @param hadc: ADC handle
  1161. * @retval Converted value
  1162. */
  1163. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  1164. {
  1165. /* Check the parameters */
  1166. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1167. /* Note: EOC flag is not cleared here by software because automatically */
  1168. /* cleared by hardware when reading register DR. */
  1169. /* Return ADC converted value */
  1170. return hadc->Instance->DR;
  1171. }
  1172. /**
  1173. * @brief Handles ADC interrupt request.
  1174. * @param hadc: ADC handle
  1175. * @retval None
  1176. */
  1177. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  1178. {
  1179. /* Check the parameters */
  1180. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1181. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  1182. /* ========== Check End of Conversion flag for regular group ========== */
  1183. if( (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC)) ||
  1184. (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOS)) )
  1185. {
  1186. /* Update state machine on conversion status if not in error state */
  1187. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1188. {
  1189. /* Set ADC state */
  1190. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1191. }
  1192. /* Determine whether any further conversion upcoming on group regular */
  1193. /* by external trigger, continuous mode or scan sequence on going. */
  1194. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1195. (hadc->Init.ContinuousConvMode == DISABLE) )
  1196. {
  1197. /* If End of Sequence is reached, disable interrupts */
  1198. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  1199. {
  1200. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1201. /* ADSTART==0 (no conversion on going) */
  1202. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1203. {
  1204. /* Disable ADC end of single conversion interrupt on group regular */
  1205. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1206. /* HAL_Start_IT(), but is not disabled here because can be used */
  1207. /* by overrun IRQ process below. */
  1208. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1209. /* Set ADC state */
  1210. ADC_STATE_CLR_SET(hadc->State,
  1211. HAL_ADC_STATE_REG_BUSY,
  1212. HAL_ADC_STATE_READY);
  1213. }
  1214. else
  1215. {
  1216. /* Change ADC state to error state */
  1217. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1218. /* Set ADC error code to ADC IP internal error */
  1219. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1220. }
  1221. }
  1222. }
  1223. /* Conversion complete callback */
  1224. /* Note: into callback, to determine if conversion has been triggered */
  1225. /* from EOC or EOS, possibility to use: */
  1226. /* " if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
  1227. HAL_ADC_ConvCpltCallback(hadc);
  1228. /* Clear regular group conversion flag */
  1229. /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of */
  1230. /* conversion flags clear induces the release of the preserved data.*/
  1231. /* Therefore, if the preserved data value is needed, it must be */
  1232. /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
  1233. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS) );
  1234. }
  1235. /* ========== Check Analog watchdog flags ========== */
  1236. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD))
  1237. {
  1238. /* Set ADC state */
  1239. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1240. /* Level out of window callback */
  1241. HAL_ADC_LevelOutOfWindowCallback(hadc);
  1242. /* Clear ADC Analog watchdog flag */
  1243. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  1244. }
  1245. /* ========== Check Overrun flag ========== */
  1246. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_OVR) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_OVR))
  1247. {
  1248. /* If overrun is set to overwrite previous data (default setting), */
  1249. /* overrun event is not considered as an error. */
  1250. /* (cf ref manual "Managing conversions without using the DMA and without */
  1251. /* overrun ") */
  1252. /* Exception for usage with DMA overrun event always considered as an */
  1253. /* error. */
  1254. if ((hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED) ||
  1255. HAL_IS_BIT_SET(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN) )
  1256. {
  1257. /* Set ADC error code to overrun */
  1258. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1259. /* Clear ADC overrun flag */
  1260. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1261. /* Error callback */
  1262. HAL_ADC_ErrorCallback(hadc);
  1263. }
  1264. /* Clear the Overrun flag */
  1265. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1266. }
  1267. }
  1268. /**
  1269. * @brief Conversion complete callback in non blocking mode
  1270. * @param hadc: ADC handle
  1271. * @retval None
  1272. */
  1273. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  1274. {
  1275. /* Prevent unused argument(s) compilation warning */
  1276. UNUSED(hadc);
  1277. /* NOTE : This function should not be modified. When the callback is needed,
  1278. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  1279. */
  1280. }
  1281. /**
  1282. * @brief Conversion DMA half-transfer callback in non blocking mode
  1283. * @param hadc: ADC handle
  1284. * @retval None
  1285. */
  1286. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
  1287. {
  1288. /* Prevent unused argument(s) compilation warning */
  1289. UNUSED(hadc);
  1290. /* NOTE : This function should not be modified. When the callback is needed,
  1291. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  1292. */
  1293. }
  1294. /**
  1295. * @brief Analog watchdog callback in non blocking mode.
  1296. * @param hadc: ADC handle
  1297. * @retval None
  1298. */
  1299. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  1300. {
  1301. /* Prevent unused argument(s) compilation warning */
  1302. UNUSED(hadc);
  1303. /* NOTE : This function should not be modified. When the callback is needed,
  1304. function HAL_ADC_LevelOoutOfWindowCallback must be implemented in the user file.
  1305. */
  1306. }
  1307. /**
  1308. * @brief ADC error callback in non blocking mode
  1309. * (ADC conversion with interruption or transfer by DMA)
  1310. * @param hadc: ADC handle
  1311. * @retval None
  1312. */
  1313. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  1314. {
  1315. /* Prevent unused argument(s) compilation warning */
  1316. UNUSED(hadc);
  1317. /* NOTE : This function should not be modified. When the callback is needed,
  1318. function HAL_ADC_ErrorCallback must be implemented in the user file.
  1319. */
  1320. }
  1321. /**
  1322. * @}
  1323. */
  1324. /** @addtogroup ADC_Exported_Functions_Group3
  1325. * @brief Peripheral Control functions
  1326. *
  1327. @verbatim
  1328. ===============================================================================
  1329. ##### Peripheral Control functions #####
  1330. ===============================================================================
  1331. [..] This section provides functions allowing to:
  1332. (+) Configure channels on regular group
  1333. (+) Configure the analog watchdog
  1334. @endverbatim
  1335. * @{
  1336. */
  1337. /**
  1338. * @brief Configures the the selected channel to be linked to the regular
  1339. * group.
  1340. * @note In case of usage of internal measurement channels:
  1341. * VrefInt/Vlcd(STM32L0x3xx only)/TempSensor.
  1342. * Sampling time constraints must be respected (sampling time can be
  1343. * adjusted in function of ADC clock frequency and sampling time
  1344. * setting).
  1345. * Refer to device datasheet for timings values, parameters TS_vrefint,
  1346. * TS_vlcd (STM32L0x3xx only), TS_temp (values rough order: 5us to 17us).
  1347. * These internal paths can be be disabled using function
  1348. * HAL_ADC_DeInit().
  1349. * @note Possibility to update parameters on the fly:
  1350. * This function initializes channel into regular group, following
  1351. * calls to this function can be used to reconfigure some parameters
  1352. * of structure "ADC_ChannelConfTypeDef" on the fly, without reseting
  1353. * the ADC.
  1354. * The setting of these parameters is conditioned to ADC state.
  1355. * For parameters constraints, see comments of structure
  1356. * "ADC_ChannelConfTypeDef".
  1357. * @param hadc: ADC handle
  1358. * @param sConfig: Structure of ADC channel for regular group.
  1359. * @retval HAL status
  1360. */
  1361. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  1362. {
  1363. /* Check the parameters */
  1364. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1365. assert_param(IS_ADC_CHANNEL(sConfig->Channel));
  1366. assert_param(IS_ADC_RANK(sConfig->Rank));
  1367. /* Process locked */
  1368. __HAL_LOCK(hadc);
  1369. /* Parameters update conditioned to ADC state: */
  1370. /* Parameters that can be updated when ADC is disabled or enabled without */
  1371. /* conversion on going on regular group: */
  1372. /* - Channel number */
  1373. /* - Management of internal measurement channels: Vbat/VrefInt/TempSensor */
  1374. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) != RESET)
  1375. {
  1376. /* Update ADC state machine to error */
  1377. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1378. /* Process unlocked */
  1379. __HAL_UNLOCK(hadc);
  1380. return HAL_ERROR;
  1381. }
  1382. if (sConfig->Rank != ADC_RANK_NONE)
  1383. {
  1384. /* Enable selected channels */
  1385. hadc->Instance->CHSELR |= (uint32_t)(sConfig->Channel & ADC_CHANNEL_MASK);
  1386. /* Management of internal measurement channels: Vlcd (STM32L0x3xx only)/VrefInt/TempSensor */
  1387. /* internal measurement paths enable: If internal channel selected, enable */
  1388. /* dedicated internal buffers and path. */
  1389. /* If Temperature sensor channel is selected, then enable the internal */
  1390. /* buffers and path */
  1391. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_TEMPSENSOR ) == (ADC_CHANNEL_TEMPSENSOR & ADC_CHANNEL_MASK))
  1392. {
  1393. ADC->CCR |= ADC_CCR_TSEN;
  1394. /* Delay for temperature sensor stabilization time */
  1395. ADC_DelayMicroSecond(ADC_TEMPSENSOR_DELAY_US);
  1396. }
  1397. /* If VRefInt channel is selected, then enable the internal buffers and path */
  1398. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VREFINT) == (ADC_CHANNEL_VREFINT & ADC_CHANNEL_MASK))
  1399. {
  1400. ADC->CCR |= ADC_CCR_VREFEN;
  1401. }
  1402. #if defined (STM32L053xx) || defined (STM32L063xx) || defined (STM32L073xx) || defined (STM32L083xx)
  1403. /* If Vlcd channel is selected, then enable the internal buffers and path */
  1404. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VLCD) == (ADC_CHANNEL_VLCD & ADC_CHANNEL_MASK))
  1405. {
  1406. ADC->CCR |= ADC_CCR_VLCDEN;
  1407. }
  1408. #endif
  1409. }
  1410. else
  1411. {
  1412. /* Regular sequence configuration */
  1413. /* Reset the channel selection register from the selected channel */
  1414. hadc->Instance->CHSELR &= ~((uint32_t)(sConfig->Channel & ADC_CHANNEL_MASK));
  1415. /* Management of internal measurement channels: VrefInt/TempSensor/Vbat */
  1416. /* internal measurement paths disable: If internal channel selected, */
  1417. /* disable dedicated internal buffers and path. */
  1418. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_TEMPSENSOR ) == (ADC_CHANNEL_TEMPSENSOR & ADC_CHANNEL_MASK))
  1419. {
  1420. ADC->CCR &= ~ADC_CCR_TSEN;
  1421. }
  1422. /* If VRefInt channel is selected, then enable the internal buffers and path */
  1423. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VREFINT) == (ADC_CHANNEL_VREFINT & ADC_CHANNEL_MASK))
  1424. {
  1425. ADC->CCR &= ~ADC_CCR_VREFEN;
  1426. }
  1427. #if defined (STM32L053xx) || defined (STM32L063xx) || defined (STM32L073xx) || defined (STM32L083xx)
  1428. /* If Vlcd channel is selected, then enable the internal buffers and path */
  1429. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VLCD) == (ADC_CHANNEL_VLCD & ADC_CHANNEL_MASK))
  1430. {
  1431. ADC->CCR &= ~ADC_CCR_VLCDEN;
  1432. }
  1433. #endif
  1434. }
  1435. /* Process unlocked */
  1436. __HAL_UNLOCK(hadc);
  1437. /* Return function status */
  1438. return HAL_OK;
  1439. }
  1440. /**
  1441. * @brief Configures the analog watchdog.
  1442. * @note Possibility to update parameters on the fly:
  1443. * This function initializes the selected analog watchdog, following
  1444. * calls to this function can be used to reconfigure some parameters
  1445. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without reseting
  1446. * the ADC.
  1447. * The setting of these parameters is conditioned to ADC state.
  1448. * For parameters constraints, see comments of structure
  1449. * "ADC_AnalogWDGConfTypeDef".
  1450. * @param hadc: ADC handle
  1451. * @param AnalogWDGConfig: Structure of ADC analog watchdog configuration
  1452. * @retval HAL status
  1453. */
  1454. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  1455. {
  1456. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1457. uint32_t tmpAWDHighThresholdShifted;
  1458. uint32_t tmpAWDLowThresholdShifted;
  1459. /* Check the parameters */
  1460. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1461. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  1462. assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
  1463. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  1464. /* Verify if threshold is within the selected ADC resolution */
  1465. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
  1466. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
  1467. if(AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG)
  1468. {
  1469. assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
  1470. }
  1471. /* Process locked */
  1472. __HAL_LOCK(hadc);
  1473. /* Parameters update conditioned to ADC state: */
  1474. /* Parameters that can be updated when ADC is disabled or enabled without */
  1475. /* conversion on going on regular group: */
  1476. /* - Analog watchdog channels */
  1477. /* - Analog watchdog thresholds */
  1478. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1479. {
  1480. /* Configure ADC Analog watchdog interrupt */
  1481. if(AnalogWDGConfig->ITMode == ENABLE)
  1482. {
  1483. /* Enable the ADC Analog watchdog interrupt */
  1484. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
  1485. }
  1486. else
  1487. {
  1488. /* Disable the ADC Analog watchdog interrupt */
  1489. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
  1490. }
  1491. /* Configuration of analog watchdog: */
  1492. /* - Set the analog watchdog mode */
  1493. /* - Set the Analog watchdog channel (is not used if watchdog */
  1494. /* mode "all channels": ADC_CFGR1_AWD1SGL=0) */
  1495. hadc->Instance->CFGR1 &= ~( ADC_CFGR1_AWDSGL |
  1496. ADC_CFGR1_AWDEN |
  1497. ADC_CFGR1_AWDCH);
  1498. hadc->Instance->CFGR1 |= ( AnalogWDGConfig->WatchdogMode |
  1499. (AnalogWDGConfig->Channel & ADC_CHANNEL_AWD_MASK));
  1500. /* Shift the offset in function of the selected ADC resolution: Thresholds */
  1501. /* have to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  1502. tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  1503. tmpAWDLowThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  1504. /* Clear High & Low high thresholds */
  1505. hadc->Instance->TR &= (uint32_t) ~ (ADC_TR_HT | ADC_TR_LT);
  1506. /* Set the high threshold */
  1507. hadc->Instance->TR = ADC_TRX_HIGHTHRESHOLD (tmpAWDHighThresholdShifted);
  1508. /* Set the low threshold */
  1509. hadc->Instance->TR |= tmpAWDLowThresholdShifted;
  1510. }
  1511. else
  1512. {
  1513. /* Update ADC state machine to error */
  1514. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1515. tmp_hal_status = HAL_ERROR;
  1516. }
  1517. /* Process unlocked */
  1518. __HAL_UNLOCK(hadc);
  1519. /* Return function status */
  1520. return tmp_hal_status;
  1521. }
  1522. /**
  1523. * @}
  1524. */
  1525. /** @addtogroup ADC_Exported_Functions_Group4
  1526. * @brief ADC Peripheral State functions
  1527. *
  1528. @verbatim
  1529. ===============================================================================
  1530. ##### ADC Peripheral State functions #####
  1531. ===============================================================================
  1532. [..]
  1533. This subsection provides functions allowing to
  1534. (+) Check the ADC state.
  1535. (+) handle ADC interrupt request.
  1536. @endverbatim
  1537. * @{
  1538. */
  1539. /**
  1540. * @brief return the ADC state
  1541. * @param hadc: ADC handle
  1542. * @retval HAL state
  1543. */
  1544. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
  1545. {
  1546. /* Check the parameters */
  1547. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1548. /* Return ADC state */
  1549. return hadc->State;
  1550. }
  1551. /**
  1552. * @brief Return the ADC error code
  1553. * @param hadc: ADC handle
  1554. * @retval ADC Error Code
  1555. */
  1556. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  1557. {
  1558. return hadc->ErrorCode;
  1559. }
  1560. /**
  1561. * @}
  1562. */
  1563. /**
  1564. * @}
  1565. */
  1566. /** @addtogroup ADC_Private
  1567. * @{
  1568. */
  1569. /**
  1570. * @brief Enable the selected ADC.
  1571. * @note Prerequisite condition to use this function: ADC must be disabled
  1572. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  1573. * @note If low power mode AutoPowerOff is enabled, power-on/off phases are
  1574. * performed automatically by hardware.
  1575. * In this mode, this function is useless and must not be called because
  1576. * flag ADC_FLAG_RDY is not usable.
  1577. * Therefore, this function must be called under condition of
  1578. * "if (hadc->Init.LowPowerAutoPowerOff != ENABLE)".
  1579. * @param hadc: ADC handle
  1580. * @retval HAL status.
  1581. */
  1582. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
  1583. {
  1584. uint32_t tickstart = 0U;
  1585. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  1586. /* enabling phase not yet completed: flag ADC ready not yet set). */
  1587. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  1588. /* causes: ADC clock not running, ...). */
  1589. if (ADC_IS_ENABLE(hadc) == RESET)
  1590. {
  1591. /* Check if conditions to enable the ADC are fulfilled */
  1592. if (ADC_ENABLING_CONDITIONS(hadc) == RESET)
  1593. {
  1594. /* Update ADC state machine to error */
  1595. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1596. /* Set ADC error code to ADC IP internal error */
  1597. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1598. return HAL_ERROR;
  1599. }
  1600. /* Enable the ADC peripheral */
  1601. __HAL_ADC_ENABLE(hadc);
  1602. /* Delay for ADC stabilization time. */
  1603. ADC_DelayMicroSecond(ADC_STAB_DELAY_US);
  1604. /* Get tick count */
  1605. tickstart = HAL_GetTick();
  1606. /* Wait for ADC effectively enabled */
  1607. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET)
  1608. {
  1609. if((HAL_GetTick() - tickstart ) > ADC_ENABLE_TIMEOUT)
  1610. {
  1611. /* Update ADC state machine to error */
  1612. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1613. /* Set ADC error code to ADC IP internal error */
  1614. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1615. return HAL_ERROR;
  1616. }
  1617. }
  1618. }
  1619. /* Return HAL status */
  1620. return HAL_OK;
  1621. }
  1622. /**
  1623. * @brief Disable the selected ADC.
  1624. * @note Prerequisite condition to use this function: ADC conversions must be
  1625. * stopped.
  1626. * @param hadc: ADC handle
  1627. * @retval HAL status.
  1628. */
  1629. static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc)
  1630. {
  1631. uint32_t tickstart = 0U;
  1632. /* Verification if ADC is not already disabled: */
  1633. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  1634. /* disabled. */
  1635. if (ADC_IS_ENABLE(hadc) != RESET )
  1636. {
  1637. /* Check if conditions to disable the ADC are fulfilled */
  1638. if (ADC_DISABLING_CONDITIONS(hadc) != RESET)
  1639. {
  1640. /* Disable the ADC peripheral */
  1641. __HAL_ADC_DISABLE(hadc);
  1642. }
  1643. else
  1644. {
  1645. /* Update ADC state machine to error */
  1646. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1647. /* Set ADC error code to ADC IP internal error */
  1648. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1649. return HAL_ERROR;
  1650. }
  1651. /* Wait for ADC effectively disabled */
  1652. /* Get tick count */
  1653. tickstart = HAL_GetTick();
  1654. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
  1655. {
  1656. if((HAL_GetTick() - tickstart ) > ADC_DISABLE_TIMEOUT)
  1657. {
  1658. /* Update ADC state machine to error */
  1659. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1660. /* Set ADC error code to ADC IP internal error */
  1661. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1662. return HAL_ERROR;
  1663. }
  1664. }
  1665. }
  1666. /* Return HAL status */
  1667. return HAL_OK;
  1668. }
  1669. /**
  1670. * @brief Stop ADC conversion.
  1671. * @note Prerequisite condition to use this function: ADC conversions must be
  1672. * stopped to disable the ADC.
  1673. * @param hadc: ADC handle
  1674. * @retval HAL status.
  1675. */
  1676. static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc)
  1677. {
  1678. uint32_t tickstart = 0U;
  1679. /* Check the parameters */
  1680. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1681. /* Verification if ADC is not already stopped on regular group to bypass */
  1682. /* this function if not needed. */
  1683. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc))
  1684. {
  1685. /* Stop potential conversion on going on regular group */
  1686. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  1687. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) &&
  1688. HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
  1689. {
  1690. /* Stop conversions on regular group */
  1691. hadc->Instance->CR |= ADC_CR_ADSTP;
  1692. }
  1693. /* Wait for conversion effectively stopped */
  1694. /* Get tick count */
  1695. tickstart = HAL_GetTick();
  1696. while((hadc->Instance->CR & ADC_CR_ADSTART) != RESET)
  1697. {
  1698. if((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  1699. {
  1700. /* Update ADC state machine to error */
  1701. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1702. /* Set ADC error code to ADC IP internal error */
  1703. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1704. return HAL_ERROR;
  1705. }
  1706. }
  1707. }
  1708. /* Return HAL status */
  1709. return HAL_OK;
  1710. }
  1711. /**
  1712. * @brief DMA transfer complete callback.
  1713. * @param hdma: pointer to DMA handle.
  1714. * @retval None
  1715. */
  1716. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  1717. {
  1718. /* Retrieve ADC handle corresponding to current DMA handle */
  1719. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1720. /* Update state machine on conversion status if not in error state */
  1721. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA))
  1722. {
  1723. /* Set ADC state */
  1724. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1725. /* Determine whether any further conversion upcoming on group regular */
  1726. /* by external trigger, continuous mode or scan sequence on going. */
  1727. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1728. (hadc->Init.ContinuousConvMode == DISABLE) )
  1729. {
  1730. /* If End of Sequence is reached, disable interrupts */
  1731. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  1732. {
  1733. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1734. /* ADSTART==0 (no conversion on going) */
  1735. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1736. {
  1737. /* Disable ADC end of single conversion interrupt on group regular */
  1738. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1739. /* HAL_Start_IT(), but is not disabled here because can be used */
  1740. /* by overrun IRQ process below. */
  1741. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1742. /* Set ADC state */
  1743. ADC_STATE_CLR_SET(hadc->State,
  1744. HAL_ADC_STATE_REG_BUSY,
  1745. HAL_ADC_STATE_READY);
  1746. }
  1747. else
  1748. {
  1749. /* Change ADC state to error state */
  1750. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1751. /* Set ADC error code to ADC IP internal error */
  1752. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1753. }
  1754. }
  1755. }
  1756. /* Conversion complete callback */
  1757. HAL_ADC_ConvCpltCallback(hadc);
  1758. }
  1759. else
  1760. {
  1761. /* Call DMA error callback */
  1762. hadc->DMA_Handle->XferErrorCallback(hdma);
  1763. }
  1764. }
  1765. /**
  1766. * @brief DMA half transfer complete callback.
  1767. * @param hdma: pointer to DMA handle.
  1768. * @retval None
  1769. */
  1770. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  1771. {
  1772. /* Retrieve ADC handle corresponding to current DMA handle */
  1773. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1774. /* Half conversion callback */
  1775. HAL_ADC_ConvHalfCpltCallback(hadc);
  1776. }
  1777. /**
  1778. * @brief DMA error callback
  1779. * @param hdma: pointer to DMA handle.
  1780. * @retval None
  1781. */
  1782. static void ADC_DMAError(DMA_HandleTypeDef *hdma)
  1783. {
  1784. /* Retrieve ADC handle corresponding to current DMA handle */
  1785. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1786. /* Set ADC state */
  1787. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1788. /* Set ADC error code to DMA error */
  1789. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  1790. /* Error callback */
  1791. HAL_ADC_ErrorCallback(hadc);
  1792. }
  1793. /**
  1794. * @brief Delay micro seconds
  1795. * @param microSecond : delay
  1796. * @retval None
  1797. */
  1798. static void ADC_DelayMicroSecond(uint32_t microSecond)
  1799. {
  1800. /* Compute number of CPU cycles to wait for */
  1801. __IO uint32_t waitLoopIndex = (microSecond * (SystemCoreClock / 1000000U));
  1802. while(waitLoopIndex != 0U)
  1803. {
  1804. waitLoopIndex--;
  1805. }
  1806. }
  1807. /**
  1808. * @}
  1809. */
  1810. /**
  1811. * @}
  1812. */
  1813. #endif /* HAL_ADC_MODULE_ENABLED */
  1814. /**
  1815. * @}
  1816. */
  1817. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/