1
0

stm32f1xx_hal_adc.c 79 KB

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