py32f002b_hal_adc.c 77 KB

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