stm32f1xx_hal_adc.c 79 KB

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