stm32l4xx_hal_adc.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_adc.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Analog to Digital Convertor (ADC)
  7. * peripheral:
  8. * + Initialization and de-initialization functions
  9. * ++ Initialization and Configuration of ADC
  10. * + Operation functions
  11. * ++ Start, stop, get result of conversions of regular
  12. * group, using 3 possible modes: polling, interruption or DMA.
  13. * + Control functions
  14. * ++ Channels configuration on regular group
  15. * ++ Analog Watchdog configuration
  16. * + State functions
  17. * ++ ADC state machine management
  18. * ++ Interrupts and flags management
  19. * Other functions (extended functions) are available in file
  20. * "stm32l4xx_hal_adc_ex.c".
  21. *
  22. @verbatim
  23. ==============================================================================
  24. ##### ADC peripheral features #####
  25. ==============================================================================
  26. [..]
  27. (+) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution.
  28. (+) Interrupt generation at the end of regular conversion and in case of
  29. analog watchdog or overrun events.
  30. (+) Single and continuous conversion modes.
  31. (+) Scan mode for conversion of several channels sequentially.
  32. (+) Data alignment with in-built data coherency.
  33. (+) Programmable sampling time (channel wise)
  34. (+) External trigger (timer or EXTI) with configurable polarity
  35. (+) DMA request generation for transfer of conversions data of regular group.
  36. (+) Configurable delay between conversions in Dual interleaved mode.
  37. (+) ADC channels selectable single/differential input.
  38. (+) ADC offset shared on 4 offset instances.
  39. (+) ADC calibration
  40. (+) ADC conversion of regular group.
  41. (+) ADC supply requirements: 1.62 V to 3.6 V.
  42. (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to
  43. Vdda or to an external voltage reference).
  44. ##### How to use this driver #####
  45. ==============================================================================
  46. [..]
  47. *** Configuration of top level parameters related to ADC ***
  48. ============================================================
  49. [..]
  50. (#) Enable the ADC interface
  51. (++) As prerequisite, ADC clock must be configured at RCC top level.
  52. (++) Two clock settings are mandatory:
  53. (+++) ADC clock (core clock, also possibly conversion clock).
  54. (+++) ADC clock (conversions clock).
  55. Two possible clock sources: synchronous clock derived from APB clock
  56. or asynchronous clock derived from system clock, PLLSAI1 or the PLLSAI2
  57. running up to 80MHz.
  58. (+++) Example:
  59. Into HAL_ADC_MspInit() (recommended code location) or with
  60. other device clock parameters configuration:
  61. (+++) __HAL_RCC_ADC_CLK_ENABLE(); (mandatory)
  62. RCC_ADCCLKSOURCE_PLL enable: (optional: if asynchronous clock selected)
  63. (+++) RCC_PeriphClkInitTypeDef RCC_PeriphClkInit;
  64. (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  65. (+++) PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLL;
  66. (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
  67. (++) ADC clock source and clock prescaler are configured at ADC level with
  68. parameter "ClockPrescaler" using function HAL_ADC_Init().
  69. (#) ADC pins configuration
  70. (++) Enable the clock for the ADC GPIOs
  71. using macro __HAL_RCC_GPIOx_CLK_ENABLE()
  72. (++) Configure these ADC pins in analog mode
  73. using function HAL_GPIO_Init()
  74. (#) Optionally, in case of usage of ADC with interruptions:
  75. (++) Configure the NVIC for ADC
  76. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  77. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  78. into the function of corresponding ADC interruption vector
  79. ADCx_IRQHandler().
  80. (#) Optionally, in case of usage of DMA:
  81. (++) Configure the DMA (DMA channel, mode normal or circular, ...)
  82. using function HAL_DMA_Init().
  83. (++) Configure the NVIC for DMA
  84. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  85. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  86. into the function of corresponding DMA interruption vector
  87. DMAx_Channelx_IRQHandler().
  88. *** Configuration of ADC, group regular, channels parameters ***
  89. ================================================================
  90. [..]
  91. (#) Configure the ADC parameters (resolution, data alignment, ...)
  92. and regular group parameters (conversion trigger, sequencer, ...)
  93. using function HAL_ADC_Init().
  94. (#) Configure the channels for regular group parameters (channel number,
  95. channel rank into sequencer, ..., into regular group)
  96. using function HAL_ADC_ConfigChannel().
  97. (#) Optionally, configure the analog watchdog parameters (channels
  98. monitored, thresholds, ...)
  99. using function HAL_ADC_AnalogWDGConfig().
  100. *** Execution of ADC conversions ***
  101. ====================================
  102. [..]
  103. (#) Optionally, perform an automatic ADC calibration to improve the
  104. conversion accuracy
  105. using function HAL_ADCEx_Calibration_Start().
  106. (#) ADC driver can be used among three modes: polling, interruption,
  107. transfer by DMA.
  108. (++) ADC conversion by polling:
  109. (+++) Activate the ADC peripheral and start conversions
  110. using function HAL_ADC_Start()
  111. (+++) Wait for ADC conversion completion
  112. using function HAL_ADC_PollForConversion()
  113. (+++) Retrieve conversion results
  114. using function HAL_ADC_GetValue()
  115. (+++) Stop conversion and disable the ADC peripheral
  116. using function HAL_ADC_Stop()
  117. (++) ADC conversion by interruption:
  118. (+++) Activate the ADC peripheral and start conversions
  119. using function HAL_ADC_Start_IT()
  120. (+++) Wait for ADC conversion completion by call of function
  121. HAL_ADC_ConvCpltCallback()
  122. (this function must be implemented in user program)
  123. (+++) Retrieve conversion results
  124. using function HAL_ADC_GetValue()
  125. (+++) Stop conversion and disable the ADC peripheral
  126. using function HAL_ADC_Stop_IT()
  127. (++) ADC conversion with transfer by DMA:
  128. (+++) Activate the ADC peripheral and start conversions
  129. using function HAL_ADC_Start_DMA()
  130. (+++) Wait for ADC conversion completion by call of function
  131. HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
  132. (these functions must be implemented in user program)
  133. (+++) Conversion results are automatically transferred by DMA into
  134. destination variable address.
  135. (+++) Stop conversion and disable the ADC peripheral
  136. using function HAL_ADC_Stop_DMA()
  137. [..]
  138. (@) Callback functions must be implemented in user program:
  139. (+@) HAL_ADC_ErrorCallback()
  140. (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog)
  141. (+@) HAL_ADC_ConvCpltCallback()
  142. (+@) HAL_ADC_ConvHalfCpltCallback
  143. *** Deinitialization of ADC ***
  144. ============================================================
  145. [..]
  146. (#) Disable the ADC interface
  147. (++) ADC clock can be hard reset and disabled at RCC top level.
  148. (++) Hard reset of ADC peripherals
  149. using macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET().
  150. (++) ADC clock disable
  151. using the equivalent macro/functions as configuration step.
  152. (+++) Example:
  153. Into HAL_ADC_MspDeInit() (recommended code location) or with
  154. other device clock parameters configuration:
  155. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI14;
  156. (+++) RCC_OscInitStructure.HSI14State = RCC_HSI14_OFF; (if not used for system clock)
  157. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  158. (#) ADC pins configuration
  159. (++) Disable the clock for the ADC GPIOs
  160. using macro __HAL_RCC_GPIOx_CLK_DISABLE()
  161. (#) Optionally, in case of usage of ADC with interruptions:
  162. (++) Disable the NVIC for ADC
  163. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  164. (#) Optionally, in case of usage of DMA:
  165. (++) Deinitialize the DMA
  166. using function HAL_DMA_Init().
  167. (++) Disable the NVIC for DMA
  168. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  169. [..]
  170. @endverbatim
  171. ******************************************************************************
  172. * @attention
  173. *
  174. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  175. *
  176. * Redistribution and use in source and binary forms, with or without modification,
  177. * are permitted provided that the following conditions are met:
  178. * 1. Redistributions of source code must retain the above copyright notice,
  179. * this list of conditions and the following disclaimer.
  180. * 2. Redistributions in binary form must reproduce the above copyright notice,
  181. * this list of conditions and the following disclaimer in the documentation
  182. * and/or other materials provided with the distribution.
  183. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  184. * may be used to endorse or promote products derived from this software
  185. * without specific prior written permission.
  186. *
  187. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  188. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  189. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  190. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  191. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  192. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  193. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  194. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  195. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  196. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  197. *
  198. ******************************************************************************
  199. */
  200. /* Includes ------------------------------------------------------------------*/
  201. #include "stm32l4xx_hal.h"
  202. /** @addtogroup STM32L4xx_HAL_Driver
  203. * @{
  204. */
  205. /** @defgroup ADC ADC
  206. * @brief ADC HAL module driver
  207. * @{
  208. */
  209. #ifdef HAL_ADC_MODULE_ENABLED
  210. /* Private typedef -----------------------------------------------------------*/
  211. /* Private define ------------------------------------------------------------*/
  212. /** @defgroup ADC_Private_Constants ADC Private Constants
  213. * @{
  214. */
  215. #define ADC_CFGR_FIELDS_1 ((uint32_t)(ADC_CFGR_RES | ADC_CFGR_ALIGN |\
  216. ADC_CFGR_CONT | ADC_CFGR_OVRMOD |\
  217. ADC_CFGR_DISCEN | ADC_CFGR_DISCNUM |\
  218. ADC_CFGR_EXTEN | ADC_CFGR_EXTSEL)) /*!< ADC_CFGR fields of parameters that can be updated
  219. when no regular conversion is on-going */
  220. /* Delay to wait before setting ADEN once ADCAL has been reset
  221. must be at least 4 ADC clock cycles.
  222. Assuming lowest ADC clock (140 KHz according to DS), this
  223. 4 ADC clock cycles duration is equal to
  224. 4 / 140,000 = 0.028 ms.
  225. ADC_ENABLE_TIMEOUT set to 2 is a margin large enough to ensure
  226. the 4 ADC clock cycles have elapsed while waiting for ADRDY
  227. to become 1 */
  228. #define ADC_ENABLE_TIMEOUT ((uint32_t) 2) /*!< ADC enable time-out value */
  229. #define ADC_DISABLE_TIMEOUT ((uint32_t) 2) /*!< ADC disable time-out value */
  230. /* Timeout to wait for current conversion on going to be completed. */
  231. /* Timeout fixed to longest ADC conversion possible, for 1 channel: */
  232. /* - maximum sampling time (640.5 adc_clk) */
  233. /* - ADC resolution (Tsar 12 bits= 12.5 adc_clk) */
  234. /* - ADC clock with prescaler 256 */
  235. /* (from asynchronous clock, assuming clock frequency same as CPU for */
  236. /* this calculation) */
  237. /* - ADC oversampling ratio 256 */
  238. /* Calculation: 653 * 256 * 256 = 42795008 CPU clock cycles max */
  239. /* Unit: cycles of CPU clock. */
  240. #define ADC_CONVERSION_TIME_MAX_CPU_CYCLES ((uint32_t) 42795008) /*!< ADC conversion completion time-out value */
  241. /**
  242. * @}
  243. */
  244. /* Private macro -------------------------------------------------------------*/
  245. /* Private variables ---------------------------------------------------------*/
  246. /* Private function prototypes -----------------------------------------------*/
  247. /* Exported functions --------------------------------------------------------*/
  248. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  249. * @{
  250. */
  251. /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions
  252. * @brief ADC Initialization and Configuration functions
  253. *
  254. @verbatim
  255. ===============================================================================
  256. ##### Initialization and de-initialization functions #####
  257. ===============================================================================
  258. [..] This section provides functions allowing to:
  259. (+) Initialize and configure the ADC.
  260. (+) De-initialize the ADC.
  261. @endverbatim
  262. * @{
  263. */
  264. /**
  265. * @brief Initialize the ADC peripheral and regular group according to
  266. * parameters specified in structure "ADC_InitTypeDef".
  267. * @note As prerequisite, ADC clock must be configured at RCC top level
  268. * (refer to description of RCC configuration for ADC
  269. * in header of this file).
  270. * @note Possibility to update parameters on the fly:
  271. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  272. * coming from ADC state reset. Following calls to this function can
  273. * be used to reconfigure some parameters of ADC_InitTypeDef
  274. * structure on the fly, without modifying MSP configuration. If ADC
  275. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  276. * before HAL_ADC_Init().
  277. * The setting of these parameters is conditioned to ADC state.
  278. * For parameters constraints, see comments of structure
  279. * "ADC_InitTypeDef".
  280. * @note This function configures the ADC within 2 scopes: scope of entire
  281. * ADC and scope of regular group. For parameters details, see comments
  282. * of structure "ADC_InitTypeDef".
  283. * @note Parameters related to common ADC registers (ADC clock mode) are set
  284. * only if all ADCs are disabled.
  285. * If this is not the case, these common parameters setting are
  286. * bypassed without error reporting: it can be the intended behaviour in
  287. * case of update of a parameter of ADC_InitTypeDef on the fly,
  288. * without disabling the other ADCs.
  289. * @param hadc ADC handle
  290. * @retval HAL status
  291. */
  292. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  293. {
  294. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  295. uint32_t tmpCFGR = 0U;
  296. __IO uint32_t wait_loop_index = 0;
  297. /* Check ADC handle */
  298. if(hadc == NULL)
  299. {
  300. return HAL_ERROR;
  301. }
  302. /* Check the parameters */
  303. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  304. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  305. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  306. assert_param(IS_ADC_DFSDMCFG_MODE(hadc));
  307. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  308. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  309. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  310. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  311. assert_param(IS_ADC_EXTTRIG(hadc, hadc->Init.ExternalTrigConv));
  312. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  313. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  314. assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
  315. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
  316. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
  317. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  318. {
  319. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  320. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  321. if(hadc->Init.DiscontinuousConvMode == ENABLE)
  322. {
  323. assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
  324. }
  325. }
  326. /* DISCEN and CONT bits cannot be set at the same time */
  327. assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (hadc->Init.ContinuousConvMode == ENABLE)));
  328. /* Actions performed only if ADC is coming from state reset: */
  329. /* - Initialization of ADC MSP */
  330. if(hadc->State == HAL_ADC_STATE_RESET)
  331. {
  332. /* Init the low level hardware */
  333. HAL_ADC_MspInit(hadc);
  334. /* Set ADC error code to none */
  335. ADC_CLEAR_ERRORCODE(hadc);
  336. /* Initialize Lock */
  337. hadc->Lock = HAL_UNLOCKED;
  338. }
  339. /* - Exit from deep-power-down mode and ADC voltage regulator enable */
  340. if(LL_ADC_IsDeepPowerDownEnabled(hadc->Instance) != 0U)
  341. {
  342. /* Disable ADC deep power down mode */
  343. LL_ADC_DisableDeepPowerDown(hadc->Instance);
  344. /* System was in deep power down mode, calibration must
  345. be relaunched or a previously saved calibration factor
  346. re-applied once the ADC voltage regulator is enabled */
  347. }
  348. if(LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0U)
  349. {
  350. /* Enable ADC internal voltage regulator */
  351. LL_ADC_EnableInternalRegulator(hadc->Instance);
  352. /* Delay for ADC stabilization time */
  353. /* Wait loop initialization and execution */
  354. /* Note: Variable divided by 2 to compensate partially */
  355. /* CPU processing cycles. */
  356. wait_loop_index = (LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (1000000 * 2)));
  357. while(wait_loop_index != 0)
  358. {
  359. wait_loop_index--;
  360. }
  361. }
  362. /* Verification that ADC voltage regulator is correctly enabled, whether */
  363. /* or not ADC is coming from state reset (if any potential problem of */
  364. /* clocking, voltage regulator would not be enabled). */
  365. if(LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0U)
  366. {
  367. /* Update ADC state machine to error */
  368. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  369. /* Set ADC error code to ADC IP internal error */
  370. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  371. tmp_hal_status = HAL_ERROR;
  372. }
  373. /* Configuration of ADC parameters if previous preliminary actions are */
  374. /* correctly completed and if there is no conversion on going on regular */
  375. /* group (ADC may already be enabled at this point if HAL_ADC_Init() is */
  376. /* called to update a parameter on the fly). */
  377. if( (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  378. && (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  379. )
  380. {
  381. /* Set ADC state */
  382. ADC_STATE_CLR_SET(hadc->State,
  383. HAL_ADC_STATE_REG_BUSY,
  384. HAL_ADC_STATE_BUSY_INTERNAL);
  385. /* Configuration of common ADC parameters */
  386. /* Parameters update conditioned to ADC state: */
  387. /* Parameters that can be updated only when ADC is disabled: */
  388. /* - clock configuration */
  389. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  390. (ADC_ANY_OTHER_ENABLED(hadc) == RESET) )
  391. {
  392. /* Reset configuration of ADC common register CCR: */
  393. /* */
  394. /* - ADC clock mode and ACC prescaler (CKMODE and PRESC bits)are set */
  395. /* according to adc->Init.ClockPrescaler. It selects the clock */
  396. /* source and sets the clock division factor. */
  397. /* */
  398. /* Some parameters of this register are not reset, since they are set */
  399. /* by other functions and must be kept in case of usage of this */
  400. /* function on the fly (update of a parameter of ADC_InitTypeDef */
  401. /* without needing to reconfigure all other ADC groups/channels */
  402. /* parameters): */
  403. /* - when multimode feature is available, multimode-related */
  404. /* parameters: MDMA, DMACFG, DELAY, DUAL (set by API */
  405. /* HAL_ADCEx_MultiModeConfigChannel() ) */
  406. /* - internal measurement paths: Vbat, temperature sensor, Vref */
  407. /* (set into HAL_ADC_ConfigChannel() or */
  408. /* HAL_ADCEx_InjectedConfigChannel() ) */
  409. LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(hadc->Instance), hadc->Init.ClockPrescaler);
  410. }
  411. /* Configuration of ADC: */
  412. /* - resolution Init.Resolution */
  413. /* - data alignment Init.DataAlign */
  414. /* - external trigger to start conversion Init.ExternalTrigConv */
  415. /* - external trigger polarity Init.ExternalTrigConvEdge */
  416. /* - continuous conversion mode Init.ContinuousConvMode */
  417. /* - overrun Init.Overrun */
  418. /* - discontinuous mode Init.DiscontinuousConvMode */
  419. /* - discontinuous mode channel count Init.NbrOfDiscConversion */
  420. tmpCFGR = (ADC_CFGR_CONTINUOUS(hadc->Init.ContinuousConvMode) |
  421. hadc->Init.Overrun |
  422. hadc->Init.DataAlign |
  423. hadc->Init.Resolution |
  424. ADC_CFGR_REG_DISCONTINUOUS(hadc->Init.DiscontinuousConvMode) );
  425. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  426. {
  427. tmpCFGR |= ADC_CFGR_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion);
  428. }
  429. /* Enable external trigger if trigger selection is different of software */
  430. /* start. */
  431. /* Note: This configuration keeps the hardware feature of parameter */
  432. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  433. /* software start. */
  434. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  435. {
  436. tmpCFGR |= ( (hadc->Init.ExternalTrigConv & ADC_CFGR_EXTSEL)
  437. | hadc->Init.ExternalTrigConvEdge
  438. );
  439. }
  440. /* Update Configuration Register CFGR */
  441. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_1, tmpCFGR);
  442. /* Parameters update conditioned to ADC state: */
  443. /* Parameters that can be updated when ADC is disabled or enabled without */
  444. /* conversion on going on regular and injected groups: */
  445. /* - DMA continuous request Init.DMAContinuousRequests */
  446. /* - LowPowerAutoWait feature Init.LowPowerAutoWait */
  447. /* - Oversampling parameters Init.Oversampling */
  448. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  449. {
  450. tmpCFGR = ( ADC_CFGR_DFSDM(hadc) |
  451. ADC_CFGR_AUTOWAIT(hadc->Init.LowPowerAutoWait) |
  452. ADC_CFGR_DMACONTREQ(hadc->Init.DMAContinuousRequests) );
  453. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_2, tmpCFGR);
  454. if (hadc->Init.OversamplingMode == ENABLE)
  455. {
  456. assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversampling.Ratio));
  457. assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversampling.RightBitShift));
  458. assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversampling.TriggeredMode));
  459. assert_param(IS_ADC_REGOVERSAMPLING_MODE(hadc->Init.Oversampling.OversamplingStopReset));
  460. /* Configuration of Oversampler: */
  461. /* - Oversampling Ratio */
  462. /* - Right bit shift */
  463. /* - Triggered mode */
  464. /* - Oversampling mode (continued/resumed) */
  465. MODIFY_REG(hadc->Instance->CFGR2,
  466. ADC_CFGR2_OVSR |
  467. ADC_CFGR2_OVSS |
  468. ADC_CFGR2_TROVS |
  469. ADC_CFGR2_ROVSM,
  470. ADC_CFGR2_ROVSE |
  471. hadc->Init.Oversampling.Ratio |
  472. hadc->Init.Oversampling.RightBitShift |
  473. hadc->Init.Oversampling.TriggeredMode |
  474. hadc->Init.Oversampling.OversamplingStopReset
  475. );
  476. }
  477. else
  478. {
  479. /* Disable ADC oversampling scope on ADC group regular */
  480. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE);
  481. }
  482. } /* if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET) */
  483. /* Configuration of regular group sequencer: */
  484. /* - if scan mode is disabled, regular channels sequence length is set to */
  485. /* 0x00: 1 channel converted (channel on regular rank 1) */
  486. /* Parameter "NbrOfConversion" is discarded. */
  487. /* Note: Scan mode is not present by hardware on this device, but */
  488. /* emulated by software for alignment over all STM32 devices. */
  489. /* - if scan mode is enabled, regular channels sequence length is set to */
  490. /* parameter "NbrOfConversion". */
  491. if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
  492. {
  493. /* Set number of ranks in regular group sequencer */
  494. MODIFY_REG(hadc->Instance->SQR1, ADC_SQR1_L, (hadc->Init.NbrOfConversion - (uint8_t)1));
  495. }
  496. else
  497. {
  498. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L);
  499. }
  500. /* Initialize the ADC state */
  501. /* Clear HAL_ADC_STATE_BUSY_INTERNAL bit, set HAL_ADC_STATE_READY bit */
  502. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL, HAL_ADC_STATE_READY);
  503. }
  504. else
  505. {
  506. /* Update ADC state machine to error */
  507. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  508. tmp_hal_status = HAL_ERROR;
  509. }
  510. /* Return function status */
  511. return tmp_hal_status;
  512. }
  513. /**
  514. * @brief Deinitialize the ADC peripheral registers to their default reset
  515. * values, with deinitialization of the ADC MSP.
  516. * @note For devices with several ADCs: reset of ADC common registers is done
  517. * only if all ADCs sharing the same common group are disabled.
  518. * (function "HAL_ADC_MspDeInit()" is also called under the same conditions:
  519. * all ADC instances use the same core clock at RCC level, disabling
  520. * the core clock reset all ADC instances).
  521. * If this is not the case, reset of these common parameters reset is
  522. * bypassed without error reporting: it can be the intended behavior in
  523. * case of reset of a single ADC while the other ADCs sharing the same
  524. * common group is still running.
  525. * @note By default, HAL_ADC_DeInit() set ADC in mode deep power-down:
  526. * this saves more power by reducing leakage currents
  527. * and is particularly interesting before entering MCU low-power modes.
  528. * @param hadc ADC handle
  529. * @retval HAL status
  530. */
  531. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  532. {
  533. /* Check ADC handle */
  534. if(hadc == NULL)
  535. {
  536. return HAL_ERROR;
  537. }
  538. /* Check the parameters */
  539. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  540. /* Set ADC state */
  541. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  542. /* Stop potential conversion on going, on regular and injected groups */
  543. /* Note: No check on ADC_ConversionStop() return status, */
  544. /* if the conversion stop failed, it is up to */
  545. /* HAL_ADC_MspDeInit() to reset the ADC IP. */
  546. ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  547. /* Disable ADC peripheral if conversions are effectively stopped */
  548. /* Flush register JSQR: reset the queue sequencer when injected */
  549. /* queue sequencer is enabled and ADC disabled. */
  550. /* The software and hardware triggers of the injected sequence are both */
  551. /* internally disabled just after the completion of the last valid */
  552. /* injected sequence. */
  553. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM);
  554. /* Disable the ADC peripheral */
  555. /* No check on ADC_Disable() return status, if the ADC disabling process
  556. failed, it is up to HAL_ADC_MspDeInit() to reset the ADC IP */
  557. ADC_Disable(hadc);
  558. /* ========== Reset ADC registers ========== */
  559. /* Reset register IER */
  560. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD3 | ADC_IT_AWD2 | ADC_IT_AWD1 |
  561. ADC_IT_JQOVF | ADC_IT_OVR |
  562. ADC_IT_JEOS | ADC_IT_JEOC |
  563. ADC_IT_EOS | ADC_IT_EOC |
  564. ADC_IT_EOSMP | ADC_IT_RDY ) );
  565. /* Reset register ISR */
  566. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD3 | ADC_FLAG_AWD2 | ADC_FLAG_AWD1 |
  567. ADC_FLAG_JQOVF | ADC_FLAG_OVR |
  568. ADC_FLAG_JEOS | ADC_FLAG_JEOC |
  569. ADC_FLAG_EOS | ADC_FLAG_EOC |
  570. ADC_FLAG_EOSMP | ADC_FLAG_RDY ) );
  571. /* Reset register CR */
  572. /* Bits ADC_CR_JADSTP, ADC_CR_ADSTP, ADC_CR_JADSTART, ADC_CR_ADSTART,
  573. ADC_CR_ADCAL, ADC_CR_ADDIS and ADC_CR_ADEN are in access mode "read-set":
  574. no direct reset applicable.
  575. Update CR register to reset value where doable by software */
  576. CLEAR_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN | ADC_CR_ADCALDIF);
  577. SET_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
  578. /* Reset register CFGR */
  579. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_FIELDS);
  580. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  581. /* Reset register CFGR2 */
  582. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSM | ADC_CFGR2_TROVS | ADC_CFGR2_OVSS |
  583. ADC_CFGR2_OVSR | ADC_CFGR2_JOVSE | ADC_CFGR2_ROVSE );
  584. /* Reset register SMPR1 */
  585. CLEAR_BIT(hadc->Instance->SMPR1, ADC_SMPR1_FIELDS);
  586. /* Reset register SMPR2 */
  587. CLEAR_BIT(hadc->Instance->SMPR2, ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17 | ADC_SMPR2_SMP16 |
  588. ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14 | ADC_SMPR2_SMP13 |
  589. ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11 | ADC_SMPR2_SMP10 );
  590. /* Reset register TR1 */
  591. CLEAR_BIT(hadc->Instance->TR1, ADC_TR1_HT1 | ADC_TR1_LT1);
  592. /* Reset register TR2 */
  593. CLEAR_BIT(hadc->Instance->TR2, ADC_TR2_HT2 | ADC_TR2_LT2);
  594. /* Reset register TR3 */
  595. CLEAR_BIT(hadc->Instance->TR3, ADC_TR3_HT3 | ADC_TR3_LT3);
  596. /* Reset register SQR1 */
  597. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_SQ4 | ADC_SQR1_SQ3 | ADC_SQR1_SQ2 |
  598. ADC_SQR1_SQ1 | ADC_SQR1_L);
  599. /* Reset register SQR2 */
  600. CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 |
  601. ADC_SQR2_SQ6 | ADC_SQR2_SQ5);
  602. /* Reset register SQR3 */
  603. CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ14 | ADC_SQR3_SQ13 | ADC_SQR3_SQ12 |
  604. ADC_SQR3_SQ11 | ADC_SQR3_SQ10);
  605. /* Reset register SQR4 */
  606. CLEAR_BIT(hadc->Instance->SQR4, ADC_SQR4_SQ16 | ADC_SQR4_SQ15);
  607. /* Register JSQR was reset when the ADC was disabled */
  608. /* Reset register DR */
  609. /* bits in access mode read only, no direct reset applicable*/
  610. /* Reset register OFR1 */
  611. CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_OFFSET1_EN | ADC_OFR1_OFFSET1_CH | ADC_OFR1_OFFSET1);
  612. /* Reset register OFR2 */
  613. CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_OFFSET2_EN | ADC_OFR2_OFFSET2_CH | ADC_OFR2_OFFSET2);
  614. /* Reset register OFR3 */
  615. CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_OFFSET3_EN | ADC_OFR3_OFFSET3_CH | ADC_OFR3_OFFSET3);
  616. /* Reset register OFR4 */
  617. CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_OFFSET4_EN | ADC_OFR4_OFFSET4_CH | ADC_OFR4_OFFSET4);
  618. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  619. /* bits in access mode read only, no direct reset applicable*/
  620. /* Reset register AWD2CR */
  621. CLEAR_BIT(hadc->Instance->AWD2CR, ADC_AWD2CR_AWD2CH);
  622. /* Reset register AWD3CR */
  623. CLEAR_BIT(hadc->Instance->AWD3CR, ADC_AWD3CR_AWD3CH);
  624. /* Reset register DIFSEL */
  625. CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_DIFSEL);
  626. /* Reset register CALFACT */
  627. CLEAR_BIT(hadc->Instance->CALFACT, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
  628. /* ========== Reset common ADC registers ========== */
  629. /* Software is allowed to change common parameters only when all the other
  630. ADCs are disabled. */
  631. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  632. (ADC_ANY_OTHER_ENABLED(hadc) == RESET) )
  633. {
  634. /* Reset configuration of ADC common register CCR:
  635. - clock mode: CKMODE, PRESCEN
  636. - multimode related parameters (when this feature is available): MDMA,
  637. DMACFG, DELAY, DUAL (set by HAL_ADCEx_MultiModeConfigChannel() API)
  638. - internal measurement paths: Vbat, temperature sensor, Vref (set into
  639. HAL_ADC_ConfigChannel() or HAL_ADCEx_InjectedConfigChannel() )
  640. */
  641. ADC_CLEAR_COMMON_CONTROL_REGISTER(hadc);
  642. }
  643. /* DeInit the low level hardware.
  644. For example:
  645. __HAL_RCC_ADC_FORCE_RESET();
  646. __HAL_RCC_ADC_RELEASE_RESET();
  647. __HAL_RCC_ADC_CLK_DISABLE();
  648. Keep in mind that all ADCs use the same clock: disabling
  649. the clock will reset all ADCs.
  650. */
  651. HAL_ADC_MspDeInit(hadc);
  652. /* Set ADC error code to none */
  653. ADC_CLEAR_ERRORCODE(hadc);
  654. /* Reset injected channel configuration parameters */
  655. hadc->InjectionConfig.ContextQueue = 0;
  656. hadc->InjectionConfig.ChannelCount = 0;
  657. /* Set ADC state */
  658. hadc->State = HAL_ADC_STATE_RESET;
  659. /* Process unlocked */
  660. __HAL_UNLOCK(hadc);
  661. /* Return function status */
  662. return HAL_OK;
  663. }
  664. /**
  665. * @brief Initialize the ADC MSP.
  666. * @param hadc ADC handle
  667. * @retval None
  668. */
  669. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  670. {
  671. /* Prevent unused argument(s) compilation warning */
  672. UNUSED(hadc);
  673. /* NOTE : This function should not be modified. When the callback is needed,
  674. function HAL_ADC_MspInit must be implemented in the user file.
  675. */
  676. }
  677. /**
  678. * @brief DeInitialize the ADC MSP.
  679. * @param hadc ADC handle
  680. * @note All ADC instances use the same core clock at RCC level, disabling
  681. * the core clock reset all ADC instances).
  682. * @retval None
  683. */
  684. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
  685. {
  686. /* Prevent unused argument(s) compilation warning */
  687. UNUSED(hadc);
  688. /* NOTE : This function should not be modified. When the callback is needed,
  689. function HAL_ADC_MspDeInit must be implemented in the user file.
  690. */
  691. }
  692. /**
  693. * @}
  694. */
  695. /** @defgroup ADC_Exported_Functions_Group2 ADC Input and Output operation functions
  696. * @brief ADC IO operation functions
  697. *
  698. @verbatim
  699. ===============================================================================
  700. ##### IO operation functions #####
  701. ===============================================================================
  702. [..] This section provides functions allowing to:
  703. (+) Start conversion of regular group.
  704. (+) Stop conversion of regular group.
  705. (+) Poll for conversion complete on regular group.
  706. (+) Poll for conversion event.
  707. (+) Get result of regular channel conversion.
  708. (+) Start conversion of regular group and enable interruptions.
  709. (+) Stop conversion of regular group and disable interruptions.
  710. (+) Handle ADC interrupt request
  711. (+) Start conversion of regular group and enable DMA transfer.
  712. (+) Stop conversion of regular group and disable ADC DMA transfer.
  713. @endverbatim
  714. * @{
  715. */
  716. /**
  717. * @brief Enable ADC, start conversion of regular group.
  718. * @note Interruptions enabled in this function: None.
  719. * @note Case of multimode enabled (when multimode feature is available):
  720. * if ADC is Slave, ADC is enabled but conversion is not started,
  721. * if ADC is master, ADC is enabled and multimode conversion is started.
  722. * @param hadc ADC handle
  723. * @retval HAL status
  724. */
  725. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  726. {
  727. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  728. ADC_TypeDef *tmpADC_Master;
  729. /* Check the parameters */
  730. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  731. /* Perform ADC enable and conversion start if no conversion is on going */
  732. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  733. {
  734. /* Process locked */
  735. __HAL_LOCK(hadc);
  736. /* Enable the ADC peripheral */
  737. tmp_hal_status = ADC_Enable(hadc);
  738. /* Start conversion if ADC is effectively enabled */
  739. if (tmp_hal_status == HAL_OK)
  740. {
  741. /* Set ADC state */
  742. /* - Clear state bitfield related to regular group conversion results */
  743. /* - Set state bitfield related to regular operation */
  744. ADC_STATE_CLR_SET(hadc->State,
  745. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  746. HAL_ADC_STATE_REG_BUSY);
  747. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  748. - by default if ADC is Master or Independent or if multimode feature is not available
  749. - if multimode setting is set to independent mode (no dual regular or injected conversions are configured) */
  750. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  751. {
  752. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  753. }
  754. /* Set ADC error code */
  755. /* Check if a conversion is on going on ADC group injected */
  756. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  757. {
  758. /* Reset ADC error code fields related to regular conversions only */
  759. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  760. }
  761. else
  762. {
  763. /* Reset all ADC error code fields */
  764. ADC_CLEAR_ERRORCODE(hadc);
  765. }
  766. /* Clear ADC group regular conversion flag and overrun flag */
  767. /* (To ensure of no unknown state from potential previous ADC operations) */
  768. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  769. /* Process unlocked */
  770. /* Unlock before starting ADC conversions: in case of potential */
  771. /* interruption, to let the process to ADC IRQ Handler. */
  772. __HAL_UNLOCK(hadc);
  773. /* Enable conversion of regular group. */
  774. /* If software start has been selected, conversion starts immediately. */
  775. /* If external trigger has been selected, conversion will start at next */
  776. /* trigger event. */
  777. /* Case of multimode enabled (when multimode feature is available): */
  778. /* - if ADC is slave and dual regular conversions are enabled, ADC is */
  779. /* enabled only (conversion is not started), */
  780. /* - if ADC is master, ADC is enabled and conversion is started. */
  781. if (ADC_INDEPENDENT_OR_NONMULTIMODEREGULAR_SLAVE(hadc))
  782. {
  783. /* Multimode feature is not available or ADC Instance is Independent or Master,
  784. or is not Slave ADC with dual regular conversions enabled.
  785. Then, set HAL_ADC_STATE_INJ_BUSY bit and reset HAL_ADC_STATE_INJ_EOC bit if JAUTO is set. */
  786. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != RESET)
  787. {
  788. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  789. }
  790. /* Start ADC group regular conversion */
  791. LL_ADC_REG_StartConversion(hadc->Instance);
  792. }
  793. else
  794. {
  795. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  796. /* if Master ADC JAUTO bit is set, update Slave State in setting
  797. HAL_ADC_STATE_INJ_BUSY bit and in resetting HAL_ADC_STATE_INJ_EOC bit */
  798. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  799. if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != RESET)
  800. {
  801. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  802. }
  803. }
  804. }
  805. else
  806. {
  807. /* Process unlocked */
  808. __HAL_UNLOCK(hadc);
  809. }
  810. }
  811. else
  812. {
  813. tmp_hal_status = HAL_BUSY;
  814. }
  815. /* Return function status */
  816. return tmp_hal_status;
  817. }
  818. /**
  819. * @brief Stop ADC conversion of regular group (and injected channels in
  820. * case of auto_injection mode), disable ADC peripheral.
  821. * @note: ADC peripheral disable is forcing stop of potential
  822. * conversion on injected group. If injected group is under use, it
  823. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  824. * @param hadc ADC handle
  825. * @retval HAL status.
  826. */
  827. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  828. {
  829. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  830. /* Check the parameters */
  831. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  832. /* Process locked */
  833. __HAL_LOCK(hadc);
  834. /* 1. Stop potential conversion on going, on ADC groups regular and injected */
  835. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  836. /* Disable ADC peripheral if conversions are effectively stopped */
  837. if (tmp_hal_status == HAL_OK)
  838. {
  839. /* 2. Disable the ADC peripheral */
  840. tmp_hal_status = ADC_Disable(hadc);
  841. /* Check if ADC is effectively disabled */
  842. if (tmp_hal_status == HAL_OK)
  843. {
  844. /* Set ADC state */
  845. ADC_STATE_CLR_SET(hadc->State,
  846. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  847. HAL_ADC_STATE_READY);
  848. }
  849. }
  850. /* Process unlocked */
  851. __HAL_UNLOCK(hadc);
  852. /* Return function status */
  853. return tmp_hal_status;
  854. }
  855. /**
  856. * @brief Wait for regular group conversion to be completed.
  857. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  858. * conversion) are cleared by this function, with an exception:
  859. * if low power feature "LowPowerAutoWait" is enabled, flags are
  860. * not cleared to not interfere with this feature until data register
  861. * is read using function HAL_ADC_GetValue().
  862. * @note This function cannot be used in a particular setup: ADC configured
  863. * in DMA mode and polling for end of each conversion (ADC init
  864. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  865. * In this case, DMA resets the flag EOC and polling cannot be
  866. * performed on each conversion. Nevertheless, polling can still
  867. * be performed on the complete sequence (ADC init
  868. * parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
  869. * @param hadc ADC handle
  870. * @param Timeout Timeout value in millisecond.
  871. * @retval HAL status
  872. */
  873. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  874. {
  875. uint32_t tickstart = 0U;
  876. uint32_t tmp_Flag_End = 0U;
  877. uint32_t tmp_cfgr = 0U;
  878. ADC_TypeDef *tmpADC_Master;
  879. /* Check the parameters */
  880. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  881. /* If end of conversion selected to end of sequence conversions */
  882. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  883. {
  884. tmp_Flag_End = ADC_FLAG_EOS;
  885. }
  886. /* If end of conversion selected to end of unitary conversion */
  887. else /* ADC_EOC_SINGLE_CONV */
  888. {
  889. /* Verification that ADC configuration is compliant with polling for */
  890. /* each conversion: */
  891. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  892. /* several ranks and polling for end of each conversion. */
  893. /* For code simplicity sake, this particular case is generalized to */
  894. /* ADC configured in DMA mode and and polling for end of each conversion. */
  895. if(ADC_IS_DUAL_REGULAR_CONVERSION_ENABLE(hadc) == RESET)
  896. {
  897. /* Check ADC DMA mode in independant mode */
  898. if(READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN) != RESET)
  899. {
  900. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  901. return HAL_ERROR;
  902. }
  903. else
  904. {
  905. tmp_Flag_End = (ADC_FLAG_EOC);
  906. }
  907. }
  908. else
  909. {
  910. /* Check ADC DMA mode in multimode */
  911. if(ADC_MULTIMODE_DMA_ENABLED(hadc))
  912. {
  913. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  914. return HAL_ERROR;
  915. }
  916. else
  917. {
  918. tmp_Flag_End = (ADC_FLAG_EOC);
  919. }
  920. }
  921. }
  922. /* Get tick count */
  923. tickstart = HAL_GetTick();
  924. /* Wait until End of unitary conversion or sequence conversions flag is raised */
  925. while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_End))
  926. {
  927. /* Check if timeout is disabled (set to infinite wait) */
  928. if(Timeout != HAL_MAX_DELAY)
  929. {
  930. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  931. {
  932. /* Update ADC state machine to timeout */
  933. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  934. /* Process unlocked */
  935. __HAL_UNLOCK(hadc);
  936. return HAL_TIMEOUT;
  937. }
  938. }
  939. }
  940. /* Update ADC state machine */
  941. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  942. /* Determine whether any further conversion upcoming on group regular */
  943. /* by external trigger, continuous mode or scan sequence on going. */
  944. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  945. (hadc->Init.ContinuousConvMode == DISABLE) )
  946. {
  947. /* Check whether end of sequence is reached */
  948. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  949. {
  950. /* Set ADC state */
  951. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  952. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  953. {
  954. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  955. }
  956. }
  957. }
  958. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  959. /* in function of multimode state (for devices with multimode */
  960. /* available). */
  961. if (ADC_INDEPENDENT_OR_NONMULTIMODEREGULAR_SLAVE(hadc))
  962. {
  963. /* Retrieve handle ADC CFGR register */
  964. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  965. }
  966. else
  967. {
  968. /* Retrieve Master ADC CFGR register */
  969. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  970. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  971. }
  972. /* Clear polled flag */
  973. if (tmp_Flag_End == ADC_FLAG_EOS)
  974. {
  975. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOS);
  976. }
  977. else
  978. {
  979. /* Clear end of conversion EOC flag of regular group if low power feature */
  980. /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
  981. /* until data register is read using function HAL_ADC_GetValue(). */
  982. if (READ_BIT(tmp_cfgr, ADC_CFGR_AUTDLY) == RESET)
  983. {
  984. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  985. }
  986. }
  987. /* Return function status */
  988. return HAL_OK;
  989. }
  990. /**
  991. * @brief Poll for ADC event.
  992. * @param hadc ADC handle
  993. * @param EventType the ADC event type.
  994. * This parameter can be one of the following values:
  995. * @arg @ref ADC_EOSMP_EVENT ADC End of Sampling event
  996. * @arg @ref ADC_AWD1_EVENT ADC Analog watchdog 1 event (main analog watchdog, present on all STM32 devices)
  997. * @arg @ref ADC_AWD2_EVENT ADC Analog watchdog 2 event (additional analog watchdog, not present on all STM32 families)
  998. * @arg @ref ADC_AWD3_EVENT ADC Analog watchdog 3 event (additional analog watchdog, not present on all STM32 families)
  999. * @arg @ref ADC_OVR_EVENT ADC Overrun event
  1000. * @arg @ref ADC_JQOVF_EVENT ADC Injected context queue overflow event
  1001. * @param Timeout Timeout value in millisecond.
  1002. * @note The relevant flag is cleared if found to be set, except for ADC_FLAG_OVR.
  1003. * Indeed, the latter is reset only if hadc->Init.Overrun field is set
  1004. * to ADC_OVR_DATA_OVERWRITTEN. Otherwise, data register may be potentially overwritten
  1005. * by a new converted data as soon as OVR is cleared.
  1006. * To reset OVR flag once the preserved data is retrieved, the user can resort
  1007. * to macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1008. * @retval HAL status
  1009. */
  1010. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  1011. {
  1012. uint32_t tickstart = 0;
  1013. /* Check the parameters */
  1014. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1015. assert_param(IS_ADC_EVENT_TYPE(EventType));
  1016. /* Get tick count */
  1017. tickstart = HAL_GetTick();
  1018. /* Check selected event flag */
  1019. while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  1020. {
  1021. /* Check if timeout is disabled (set to infinite wait) */
  1022. if(Timeout != HAL_MAX_DELAY)
  1023. {
  1024. if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  1025. {
  1026. /* Update ADC state machine to timeout */
  1027. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1028. /* Process unlocked */
  1029. __HAL_UNLOCK(hadc);
  1030. return HAL_TIMEOUT;
  1031. }
  1032. }
  1033. }
  1034. switch(EventType)
  1035. {
  1036. /* End Of Sampling event */
  1037. case ADC_EOSMP_EVENT:
  1038. /* Set ADC state */
  1039. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  1040. /* Clear the End Of Sampling flag */
  1041. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
  1042. break;
  1043. /* Analog watchdog (level out of window) event */
  1044. /* Note: In case of several analog watchdog enabled, if needed to know */
  1045. /* which one triggered and on which ADCx, test ADC state of analog watchdog */
  1046. /* flags HAL_ADC_STATE_AWD1/2/3 using function "HAL_ADC_GetState()". */
  1047. /* For example: */
  1048. /* " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD1)) " */
  1049. /* " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD2)) " */
  1050. /* " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD3)) " */
  1051. /* Check analog watchdog 1 flag */
  1052. case ADC_AWD_EVENT:
  1053. /* Set ADC state */
  1054. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1055. /* Clear ADC analog watchdog flag */
  1056. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  1057. break;
  1058. /* Check analog watchdog 2 flag */
  1059. case ADC_AWD2_EVENT:
  1060. /* Set ADC state */
  1061. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  1062. /* Clear ADC analog watchdog flag */
  1063. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  1064. break;
  1065. /* Check analog watchdog 3 flag */
  1066. case ADC_AWD3_EVENT:
  1067. /* Set ADC state */
  1068. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  1069. /* Clear ADC analog watchdog flag */
  1070. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  1071. break;
  1072. /* Injected context queue overflow event */
  1073. case ADC_JQOVF_EVENT:
  1074. /* Set ADC state */
  1075. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  1076. /* Set ADC error code to Injected context queue overflow */
  1077. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  1078. /* Clear ADC Injected context queue overflow flag */
  1079. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  1080. break;
  1081. /* Overrun event */
  1082. default: /* Case ADC_OVR_EVENT */
  1083. /* If overrun is set to overwrite previous data, overrun event is not */
  1084. /* considered as an error. */
  1085. /* (cf ref manual "Managing conversions without using the DMA and without */
  1086. /* overrun ") */
  1087. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1088. {
  1089. /* Set ADC state */
  1090. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1091. /* Set ADC error code to overrun */
  1092. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1093. }
  1094. else
  1095. {
  1096. /* Clear ADC Overrun flag only if Overrun is set to ADC_OVR_DATA_OVERWRITTEN
  1097. otherwise, data register is potentially overwritten by new converted data as soon
  1098. as OVR is cleared. */
  1099. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1100. }
  1101. break;
  1102. }
  1103. /* Return function status */
  1104. return HAL_OK;
  1105. }
  1106. /**
  1107. * @brief Enable ADC, start conversion of regular group with interruption.
  1108. * @note Interruptions enabled in this function according to initialization
  1109. * setting : EOC (end of conversion), EOS (end of sequence),
  1110. * OVR overrun.
  1111. * Each of these interruptions has its dedicated callback function.
  1112. * @note Case of multimode enabled (when multimode feature is available):
  1113. * HAL_ADC_Start_IT() must be called for ADC Slave first, then for
  1114. * ADC Master.
  1115. * For ADC Slave, ADC is enabled only (conversion is not started).
  1116. * For ADC Master, ADC is enabled and multimode conversion is started.
  1117. * @note To guarantee a proper reset of all interruptions once all the needed
  1118. * conversions are obtained, HAL_ADC_Stop_IT() must be called to ensure
  1119. * a correct stop of the IT-based conversions.
  1120. * @note By default, HAL_ADC_Start_IT() does not enable the End Of Sampling
  1121. * interruption. If required (e.g. in case of oversampling with trigger
  1122. * mode), the user must:
  1123. * 1. first clear the EOSMP flag if set with macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP)
  1124. * 2. then enable the EOSMP interrupt with macro __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOSMP)
  1125. * before calling HAL_ADC_Start_IT().
  1126. * @param hadc ADC handle
  1127. * @retval HAL status
  1128. */
  1129. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  1130. {
  1131. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1132. ADC_TypeDef *tmpADC_Master;
  1133. /* Check the parameters */
  1134. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1135. /* Perform ADC enable and conversion start if no conversion is on going */
  1136. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1137. {
  1138. /* Process locked */
  1139. __HAL_LOCK(hadc);
  1140. /* Enable the ADC peripheral */
  1141. tmp_hal_status = ADC_Enable(hadc);
  1142. /* Start conversion if ADC is effectively enabled */
  1143. if (tmp_hal_status == HAL_OK)
  1144. {
  1145. /* Set ADC state */
  1146. /* - Clear state bitfield related to regular group conversion results */
  1147. /* - Set state bitfield related to regular operation */
  1148. ADC_STATE_CLR_SET(hadc->State,
  1149. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1150. HAL_ADC_STATE_REG_BUSY);
  1151. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  1152. - by default if ADC is Master or Independent or if multimode feature is not available
  1153. - if multimode setting is set to independent mode (no dual regular or injected conversions are configured) */
  1154. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  1155. {
  1156. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1157. }
  1158. /* Set ADC error code */
  1159. /* Check if a conversion is on going on ADC group injected */
  1160. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1161. {
  1162. /* Reset ADC error code fields related to regular conversions only */
  1163. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR|HAL_ADC_ERROR_DMA));
  1164. }
  1165. else
  1166. {
  1167. /* Reset all ADC error code fields */
  1168. ADC_CLEAR_ERRORCODE(hadc);
  1169. }
  1170. /* Clear ADC group regular conversion flag and overrun flag */
  1171. /* (To ensure of no unknown state from potential previous ADC operations) */
  1172. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1173. /* Process unlocked */
  1174. /* Unlock before starting ADC conversions: in case of potential */
  1175. /* interruption, to let the process to ADC IRQ Handler. */
  1176. __HAL_UNLOCK(hadc);
  1177. /* Disable all interruptions before enabling the desired ones */
  1178. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1179. /* Enable ADC end of conversion interrupt */
  1180. switch(hadc->Init.EOCSelection)
  1181. {
  1182. case ADC_EOC_SEQ_CONV:
  1183. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOS);
  1184. break;
  1185. /* case ADC_EOC_SINGLE_CONV */
  1186. default:
  1187. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);
  1188. break;
  1189. }
  1190. /* Enable ADC overrun interrupt */
  1191. /* If hadc->Init.Overrun is set to ADC_OVR_DATA_PRESERVED, only then is
  1192. ADC_IT_OVR enabled; otherwise data overwrite is considered as normal
  1193. behavior and no CPU time is lost for a non-processed interruption */
  1194. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1195. {
  1196. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1197. }
  1198. /* Enable conversion of regular group. */
  1199. /* If software start has been selected, conversion starts immediately. */
  1200. /* If external trigger has been selected, conversion will start at next */
  1201. /* trigger event. */
  1202. /* Case of multimode enabled (when multimode feature is available): */
  1203. /* - if ADC is slave and dual regular conversions are enabled, ADC is */
  1204. /* enabled only (conversion is not started), */
  1205. /* - if ADC is master, ADC is enabled and conversion is started. */
  1206. if(ADC_INDEPENDENT_OR_NONMULTIMODEREGULAR_SLAVE(hadc))
  1207. {
  1208. /* Multimode feature is not available or ADC Instance is Independent or Master,
  1209. or is not Slave ADC with dual regular conversions enabled.
  1210. Then set HAL_ADC_STATE_INJ_BUSY and reset HAL_ADC_STATE_INJ_EOC if JAUTO is set. */
  1211. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != RESET)
  1212. {
  1213. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1214. /* Enable as well injected interruptions in case
  1215. HAL_ADCEx_InjectedStart_IT() has not been called beforehand. This
  1216. allows to start regular and injected conversions when JAUTO is
  1217. set with a single call to HAL_ADC_Start_IT() */
  1218. switch(hadc->Init.EOCSelection)
  1219. {
  1220. case ADC_EOC_SEQ_CONV:
  1221. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  1222. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  1223. break;
  1224. /* case ADC_EOC_SINGLE_CONV */
  1225. default:
  1226. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  1227. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  1228. break;
  1229. }
  1230. }
  1231. /* Start ADC group regular conversion */
  1232. LL_ADC_REG_StartConversion(hadc->Instance);
  1233. }
  1234. else
  1235. {
  1236. /* hadc is the handle of a Slave ADC with dual regular conversions
  1237. enabled. Therefore, ADC_CR_ADSTART is NOT set */
  1238. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1239. /* if Master ADC JAUTO bit is set, Slave injected interruptions
  1240. are enabled nevertheless (for same reason as above) */
  1241. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  1242. if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != RESET)
  1243. {
  1244. /* First, update Slave State in setting HAL_ADC_STATE_INJ_BUSY bit
  1245. and in resetting HAL_ADC_STATE_INJ_EOC bit */
  1246. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1247. /* Next, set Slave injected interruptions */
  1248. switch(hadc->Init.EOCSelection)
  1249. {
  1250. case ADC_EOC_SEQ_CONV:
  1251. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  1252. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  1253. break;
  1254. /* case ADC_EOC_SINGLE_CONV */
  1255. default:
  1256. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  1257. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  1258. break;
  1259. }
  1260. }
  1261. }
  1262. }
  1263. else
  1264. {
  1265. /* Process unlocked */
  1266. __HAL_UNLOCK(hadc);
  1267. }
  1268. }
  1269. else
  1270. {
  1271. tmp_hal_status = HAL_BUSY;
  1272. }
  1273. /* Return function status */
  1274. return tmp_hal_status;
  1275. }
  1276. /**
  1277. * @brief Stop ADC conversion of regular group (and injected group in
  1278. * case of auto_injection mode), disable interrution of
  1279. * end-of-conversion, disable ADC peripheral.
  1280. * @param hadc ADC handle
  1281. * @retval HAL status.
  1282. */
  1283. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  1284. {
  1285. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1286. /* Check the parameters */
  1287. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1288. /* Process locked */
  1289. __HAL_LOCK(hadc);
  1290. /* 1. Stop potential conversion on going, on ADC groups regular and injected */
  1291. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1292. /* Disable ADC peripheral if conversions are effectively stopped */
  1293. if (tmp_hal_status == HAL_OK)
  1294. {
  1295. /* Disable ADC end of conversion interrupt for regular group */
  1296. /* Disable ADC overrun interrupt */
  1297. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1298. /* 2. Disable the ADC peripheral */
  1299. tmp_hal_status = ADC_Disable(hadc);
  1300. /* Check if ADC is effectively disabled */
  1301. if (tmp_hal_status == HAL_OK)
  1302. {
  1303. /* Set ADC state */
  1304. ADC_STATE_CLR_SET(hadc->State,
  1305. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1306. HAL_ADC_STATE_READY);
  1307. }
  1308. }
  1309. /* Process unlocked */
  1310. __HAL_UNLOCK(hadc);
  1311. /* Return function status */
  1312. return tmp_hal_status;
  1313. }
  1314. /**
  1315. * @brief Enable ADC, start conversion of regular group and transfer result through DMA.
  1316. * @note Interruptions enabled in this function:
  1317. * overrun (if applicable), DMA half transfer, DMA transfer complete.
  1318. * Each of these interruptions has its dedicated callback function.
  1319. * @note Case of multimode enabled (when multimode feature is available): HAL_ADC_Start_DMA()
  1320. * is designed for single-ADC mode only. For multimode, the dedicated
  1321. * HAL_ADCEx_MultiModeStart_DMA() function must be used.
  1322. * @param hadc ADC handle
  1323. * @param pData Destination Buffer address.
  1324. * @param Length Number of data to be transferred from ADC peripheral to memory
  1325. * @retval HAL status.
  1326. */
  1327. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  1328. {
  1329. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1330. /* Check the parameters */
  1331. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1332. /* Perform ADC enable and conversion start if no conversion is on going */
  1333. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1334. {
  1335. /* Process locked */
  1336. __HAL_LOCK(hadc);
  1337. /* Ensure that dual regular conversions are not enabled or unavailable. */
  1338. /* Otherwise, dedicated API HAL_ADCEx_MultiModeStart_DMA() must be used. */
  1339. if (ADC_IS_DUAL_REGULAR_CONVERSION_ENABLE(hadc) == RESET)
  1340. {
  1341. /* Enable the ADC peripheral */
  1342. tmp_hal_status = ADC_Enable(hadc);
  1343. /* Start conversion if ADC is effectively enabled */
  1344. if (tmp_hal_status == HAL_OK)
  1345. {
  1346. /* Set ADC state */
  1347. /* - Clear state bitfield related to regular group conversion results */
  1348. /* - Set state bitfield related to regular operation */
  1349. ADC_STATE_CLR_SET(hadc->State,
  1350. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1351. HAL_ADC_STATE_REG_BUSY);
  1352. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  1353. - by default if ADC is Master or Independent or if multimode feature is not available
  1354. - if multimode setting is set to independent mode (no dual regular or injected conversions are configured) */
  1355. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  1356. {
  1357. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1358. }
  1359. /* Check if a conversion is on going on ADC group injected */
  1360. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1361. {
  1362. /* Reset ADC error code fields related to regular conversions only */
  1363. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1364. }
  1365. else
  1366. {
  1367. /* Reset all ADC error code fields */
  1368. ADC_CLEAR_ERRORCODE(hadc);
  1369. }
  1370. /* Set the DMA transfer complete callback */
  1371. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1372. /* Set the DMA half transfer complete callback */
  1373. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1374. /* Set the DMA error callback */
  1375. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1376. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, */
  1377. /* ADC start (in case of SW start): */
  1378. /* Clear regular group conversion flag and overrun flag */
  1379. /* (To ensure of no unknown state from potential previous ADC */
  1380. /* operations) */
  1381. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1382. /* Process unlocked */
  1383. /* Unlock before starting ADC conversions: in case of potential */
  1384. /* interruption, to let the process to ADC IRQ Handler. */
  1385. __HAL_UNLOCK(hadc);
  1386. /* With DMA, overrun event is always considered as an error even if
  1387. hadc->Init.Overrun is set to ADC_OVR_DATA_OVERWRITTEN. Therefore,
  1388. ADC_IT_OVR is enabled. */
  1389. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1390. /* Enable ADC DMA mode */
  1391. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1392. /* Start the DMA channel */
  1393. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1394. /* Enable conversion of regular group. */
  1395. /* If software start has been selected, conversion starts immediately. */
  1396. /* If external trigger has been selected, conversion will start at next */
  1397. /* trigger event. */
  1398. /* Start ADC group regular conversion */
  1399. LL_ADC_REG_StartConversion(hadc->Instance);
  1400. }
  1401. else
  1402. {
  1403. /* Process unlocked */
  1404. __HAL_UNLOCK(hadc);
  1405. }
  1406. }
  1407. else
  1408. {
  1409. tmp_hal_status = HAL_ERROR;
  1410. /* Process unlocked */
  1411. __HAL_UNLOCK(hadc);
  1412. }
  1413. }
  1414. else
  1415. {
  1416. tmp_hal_status = HAL_BUSY;
  1417. }
  1418. /* Return function status */
  1419. return tmp_hal_status;
  1420. }
  1421. /**
  1422. * @brief Stop ADC conversion of regular group (and injected group in
  1423. * case of auto_injection mode), disable ADC DMA transfer, disable
  1424. * ADC peripheral.
  1425. * @note: ADC peripheral disable is forcing stop of potential
  1426. * conversion on ADC group injected. If ADC group injected is under use, it
  1427. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  1428. * @note Case of multimode enabled (when multimode feature is available):
  1429. * HAL_ADC_Stop_DMA() function is dedicated to single-ADC mode only.
  1430. * For multimode, the dedicated HAL_ADCEx_MultiModeStop_DMA() API must be used.
  1431. * @param hadc ADC handle
  1432. * @retval HAL status.
  1433. */
  1434. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  1435. {
  1436. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1437. /* Check the parameters */
  1438. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1439. /* Process locked */
  1440. __HAL_LOCK(hadc);
  1441. /* 1. Stop potential ADC group regular conversion on going */
  1442. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1443. /* Disable ADC peripheral if conversions are effectively stopped */
  1444. if (tmp_hal_status == HAL_OK)
  1445. {
  1446. /* Disable ADC DMA (ADC DMA configuration of continous requests is kept) */
  1447. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1448. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1449. /* while DMA transfer is on going) */
  1450. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1451. /* Check if DMA channel effectively disabled */
  1452. if (tmp_hal_status != HAL_OK)
  1453. {
  1454. /* Update ADC state machine to error */
  1455. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1456. }
  1457. /* Disable ADC overrun interrupt */
  1458. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1459. /* 2. Disable the ADC peripheral */
  1460. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep */
  1461. /* in memory a potential failing status. */
  1462. if (tmp_hal_status == HAL_OK)
  1463. {
  1464. tmp_hal_status = ADC_Disable(hadc);
  1465. }
  1466. else
  1467. {
  1468. ADC_Disable(hadc);
  1469. }
  1470. /* Check if ADC is effectively disabled */
  1471. if (tmp_hal_status == HAL_OK)
  1472. {
  1473. /* Set ADC state */
  1474. ADC_STATE_CLR_SET(hadc->State,
  1475. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1476. HAL_ADC_STATE_READY);
  1477. }
  1478. }
  1479. /* Process unlocked */
  1480. __HAL_UNLOCK(hadc);
  1481. /* Return function status */
  1482. return tmp_hal_status;
  1483. }
  1484. /**
  1485. * @brief Get ADC regular group conversion result.
  1486. * @note Reading register DR automatically clears ADC flag EOC
  1487. * (ADC group regular end of unitary conversion).
  1488. * @note This function does not clear ADC flag EOS
  1489. * (ADC group regular end of sequence conversion).
  1490. * Occurrence of flag EOS rising:
  1491. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  1492. * to flag EOC.
  1493. * - If sequencer is composed of several ranks, during the scan
  1494. * sequence flag EOC only is raised, at the end of the scan sequence
  1495. * both flags EOC and EOS are raised.
  1496. * To clear this flag, either use function:
  1497. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1498. * model polling: @ref HAL_ADC_PollForConversion()
  1499. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  1500. * @param hadc ADC handle
  1501. * @retval ADC group regular conversion data
  1502. */
  1503. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  1504. {
  1505. /* Check the parameters */
  1506. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1507. /* Note: EOC flag is not cleared here by software because automatically */
  1508. /* cleared by hardware when reading register DR. */
  1509. /* Return ADC converted value */
  1510. return hadc->Instance->DR;
  1511. }
  1512. /**
  1513. * @brief Handle ADC interrupt request.
  1514. * @param hadc ADC handle
  1515. * @retval None
  1516. */
  1517. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  1518. {
  1519. uint32_t overrun_error = 0; /* flag set if overrun occurrence has to be considered as an error */
  1520. uint32_t tmp_isr = hadc->Instance->ISR;
  1521. uint32_t tmp_ier = hadc->Instance->IER;
  1522. uint32_t tmp_cfgr = 0x0;
  1523. ADC_TypeDef *tmpADC_Master;
  1524. /* Check the parameters */
  1525. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1526. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  1527. /* ========== Check End of Sampling flag for ADC group regular ========== */
  1528. if(((tmp_isr & ADC_FLAG_EOSMP) == ADC_FLAG_EOSMP) && ((tmp_ier & ADC_IT_EOSMP) == ADC_IT_EOSMP))
  1529. {
  1530. /* Update state machine on end of sampling status if not in error state */
  1531. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1532. {
  1533. /* Set ADC state */
  1534. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  1535. }
  1536. /* End Of Sampling callback */
  1537. HAL_ADCEx_EndOfSamplingCallback(hadc);
  1538. /* Clear regular group conversion flag */
  1539. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP );
  1540. }
  1541. /* ====== Check ADC group regular end of unitary conversion sequence conversions ===== */
  1542. if((((tmp_isr & ADC_FLAG_EOC) == ADC_FLAG_EOC) && ((tmp_ier & ADC_IT_EOC) == ADC_IT_EOC)) ||
  1543. (((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS) && ((tmp_ier & ADC_IT_EOS) == ADC_IT_EOS)) )
  1544. {
  1545. /* Update state machine on conversion status if not in error state */
  1546. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1547. {
  1548. /* Set ADC state */
  1549. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1550. }
  1551. /* Determine whether any further conversion upcoming on group regular */
  1552. /* by external trigger, continuous mode or scan sequence on going */
  1553. /* to disable interruption. */
  1554. if(ADC_IS_SOFTWARE_START_REGULAR(hadc))
  1555. {
  1556. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  1557. /* in function of multimode state (for devices with multimode */
  1558. /* available). */
  1559. if (ADC_INDEPENDENT_OR_NONMULTIMODEREGULAR_SLAVE(hadc))
  1560. {
  1561. /* check CONT bit directly in handle ADC CFGR register */
  1562. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  1563. }
  1564. else
  1565. {
  1566. /* else need to check Master ADC CONT bit */
  1567. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  1568. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  1569. }
  1570. /* Carry on if continuous mode is disabled */
  1571. if (READ_BIT (tmp_cfgr, ADC_CFGR_CONT) != ADC_CFGR_CONT)
  1572. {
  1573. /* If End of Sequence is reached, disable interrupts */
  1574. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  1575. {
  1576. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1577. /* ADSTART==0 (no conversion on going) */
  1578. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1579. {
  1580. /* Disable ADC end of sequence conversion interrupt */
  1581. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1582. /* HAL_Start_IT(), but is not disabled here because can be used */
  1583. /* by overrun IRQ process below. */
  1584. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1585. /* Set ADC state */
  1586. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1587. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1588. {
  1589. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1590. }
  1591. }
  1592. else
  1593. {
  1594. /* Change ADC state to error state */
  1595. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1596. /* Set ADC error code to ADC IP internal error */
  1597. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1598. }
  1599. }
  1600. }
  1601. }
  1602. /* Conversion complete callback */
  1603. /* Note: Into callback function "HAL_ADC_ConvCpltCallback()", */
  1604. /* to determine if conversion has been triggered from EOC or EOS, */
  1605. /* possibility to use: */
  1606. /* " if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
  1607. HAL_ADC_ConvCpltCallback(hadc);
  1608. /* Clear regular group conversion flag */
  1609. /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of */
  1610. /* conversion flags clear induces the release of the preserved data.*/
  1611. /* Therefore, if the preserved data value is needed, it must be */
  1612. /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
  1613. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS) );
  1614. }
  1615. /* ====== Check ADC group injected end of unitary conversion sequence conversions ===== */
  1616. if( (((tmp_isr & ADC_FLAG_JEOC) == ADC_FLAG_JEOC) && ((tmp_ier & ADC_IT_JEOC) == ADC_IT_JEOC)) ||
  1617. (((tmp_isr & ADC_FLAG_JEOS) == ADC_FLAG_JEOS) && ((tmp_ier & ADC_IT_JEOS) == ADC_IT_JEOS)) )
  1618. {
  1619. /* Update state machine on conversion status if not in error state */
  1620. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1621. {
  1622. /* Set ADC state */
  1623. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  1624. }
  1625. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  1626. /* in function of multimode state (for devices with multimode */
  1627. /* available). */
  1628. if (ADC_INDEPENDENT_OR_NONMULTIMODEINJECTED_SLAVE(hadc))
  1629. {
  1630. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  1631. }
  1632. else
  1633. {
  1634. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  1635. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  1636. }
  1637. /* Disable interruption if no further conversion upcoming by injected */
  1638. /* external trigger or by automatic injected conversion with regular */
  1639. /* group having no further conversion upcoming (same conditions as */
  1640. /* regular group interruption disabling above), */
  1641. /* and if injected scan sequence is completed. */
  1642. if(ADC_IS_SOFTWARE_START_INJECTED(hadc) ||
  1643. ((READ_BIT (tmp_cfgr, ADC_CFGR_JAUTO) == RESET) &&
  1644. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1645. (READ_BIT (tmp_cfgr, ADC_CFGR_CONT) == RESET) ) ) )
  1646. {
  1647. /* If End of Sequence is reached, disable interrupts */
  1648. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
  1649. {
  1650. /* Particular case if injected contexts queue is enabled: */
  1651. /* when the last context has been fully processed, JSQR is reset */
  1652. /* by the hardware. Even if no injected conversion is planned to come */
  1653. /* (queue empty, triggers are ignored), it can start again */
  1654. /* immediately after setting a new context (JADSTART is still set). */
  1655. /* Therefore, state of HAL ADC injected group is kept to busy. */
  1656. if(READ_BIT(tmp_cfgr, ADC_CFGR_JQM) == RESET)
  1657. {
  1658. /* Allowed to modify bits ADC_IT_JEOC/ADC_IT_JEOS only if bit */
  1659. /* JADSTART==0 (no conversion on going) */
  1660. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET)
  1661. {
  1662. /* Disable ADC end of sequence conversion interrupt */
  1663. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC | ADC_IT_JEOS);
  1664. /* Set ADC state */
  1665. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1666. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  1667. {
  1668. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1669. }
  1670. }
  1671. else
  1672. {
  1673. /* Update ADC state machine to error */
  1674. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1675. /* Set ADC error code to ADC IP internal error */
  1676. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1677. }
  1678. }
  1679. }
  1680. }
  1681. /* Injected Conversion complete callback */
  1682. /* Note: HAL_ADCEx_InjectedConvCpltCallback can resort to
  1683. if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOS)) or
  1684. if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOC)) to determine whether
  1685. interruption has been triggered by end of conversion or end of
  1686. sequence. */
  1687. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  1688. /* Clear injected group conversion flag */
  1689. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC | ADC_FLAG_JEOS);
  1690. }
  1691. /* ========== Check Analog watchdog 1 flag ========== */
  1692. if (((tmp_isr & ADC_FLAG_AWD1) == ADC_FLAG_AWD1) && ((tmp_ier & ADC_IT_AWD1) == ADC_IT_AWD1))
  1693. {
  1694. /* Set ADC state */
  1695. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1696. /* Level out of window 1 callback */
  1697. HAL_ADC_LevelOutOfWindowCallback(hadc);
  1698. /* Clear ADC analog watchdog flag */
  1699. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  1700. }
  1701. /* ========== Check analog watchdog 2 flag ========== */
  1702. if (((tmp_isr & ADC_FLAG_AWD2) == ADC_FLAG_AWD2) && ((tmp_ier & ADC_IT_AWD2) == ADC_IT_AWD2))
  1703. {
  1704. /* Set ADC state */
  1705. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  1706. /* Level out of window 2 callback */
  1707. HAL_ADCEx_LevelOutOfWindow2Callback(hadc);
  1708. /* Clear ADC analog watchdog flag */
  1709. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  1710. }
  1711. /* ========== Check analog watchdog 3 flag ========== */
  1712. if (((tmp_isr & ADC_FLAG_AWD3) == ADC_FLAG_AWD3) && ((tmp_ier & ADC_IT_AWD3) == ADC_IT_AWD3))
  1713. {
  1714. /* Set ADC state */
  1715. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  1716. /* Level out of window 3 callback */
  1717. HAL_ADCEx_LevelOutOfWindow3Callback(hadc);
  1718. /* Clear ADC analog watchdog flag */
  1719. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  1720. }
  1721. /* ========== Check Overrun flag ========== */
  1722. if (((tmp_isr & ADC_FLAG_OVR) == ADC_FLAG_OVR) && ((tmp_ier & ADC_IT_OVR) == ADC_IT_OVR))
  1723. {
  1724. /* If overrun is set to overwrite previous data (default setting), */
  1725. /* overrun event is not considered as an error. */
  1726. /* (cf ref manual "Managing conversions without using the DMA and without */
  1727. /* overrun ") */
  1728. /* Exception for usage with DMA overrun event always considered as an */
  1729. /* error. */
  1730. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1731. {
  1732. overrun_error = 1;
  1733. }
  1734. else
  1735. {
  1736. /* Check DMA configuration */
  1737. if (ADC_IS_DUAL_CONVERSION_ENABLE(hadc) == RESET)
  1738. {
  1739. /* Multimode not set or feature not available or ADC independent */
  1740. if (HAL_IS_BIT_SET(hadc->Instance->CFGR, ADC_CFGR_DMAEN))
  1741. {
  1742. overrun_error = 1;
  1743. }
  1744. }
  1745. else
  1746. {
  1747. /* Multimode (when feature is available) is enabled,
  1748. Common Control Register MDMA bits must be checked. */
  1749. if (ADC_MULTIMODE_DMA_ENABLED(hadc))
  1750. {
  1751. overrun_error = 1;
  1752. }
  1753. }
  1754. }
  1755. if (overrun_error == 1)
  1756. {
  1757. /* Change ADC state to error state */
  1758. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1759. /* Set ADC error code to overrun */
  1760. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1761. /* Error callback */
  1762. /* Note: In case of overrun, ADC conversion data is preserved until */
  1763. /* flag OVR is reset. */
  1764. /* Therefore, old ADC conversion data can be retrieved in */
  1765. /* function "HAL_ADC_ErrorCallback()". */
  1766. HAL_ADC_ErrorCallback(hadc);
  1767. }
  1768. /* Clear ADC overrun flag */
  1769. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1770. }
  1771. /* ========== Check Injected context queue overflow flag ========== */
  1772. if (((tmp_isr & ADC_FLAG_JQOVF) == ADC_FLAG_JQOVF) && ((tmp_ier & ADC_IT_JQOVF) == ADC_IT_JQOVF))
  1773. {
  1774. /* Change ADC state to overrun state */
  1775. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  1776. /* Set ADC error code to Injected context queue overflow */
  1777. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  1778. /* Clear the Injected context queue overflow flag */
  1779. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  1780. /* Error callback */
  1781. HAL_ADCEx_InjectedQueueOverflowCallback(hadc);
  1782. }
  1783. }
  1784. /**
  1785. * @brief Conversion complete callback in non-blocking mode.
  1786. * @param hadc ADC handle
  1787. * @retval None
  1788. */
  1789. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  1790. {
  1791. /* Prevent unused argument(s) compilation warning */
  1792. UNUSED(hadc);
  1793. /* NOTE : This function should not be modified. When the callback is needed,
  1794. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  1795. */
  1796. }
  1797. /**
  1798. * @brief Conversion DMA half-transfer callback in non-blocking mode.
  1799. * @param hadc ADC handle
  1800. * @retval None
  1801. */
  1802. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
  1803. {
  1804. /* Prevent unused argument(s) compilation warning */
  1805. UNUSED(hadc);
  1806. /* NOTE : This function should not be modified. When the callback is needed,
  1807. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  1808. */
  1809. }
  1810. /**
  1811. * @brief Analog watchdog 1 callback in non-blocking mode.
  1812. * @param hadc ADC handle
  1813. * @retval None
  1814. */
  1815. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  1816. {
  1817. /* Prevent unused argument(s) compilation warning */
  1818. UNUSED(hadc);
  1819. /* NOTE : This function should not be modified. When the callback is needed,
  1820. function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
  1821. */
  1822. }
  1823. /**
  1824. * @brief ADC error callback in non-blocking mode
  1825. * (ADC conversion with interruption or transfer by DMA).
  1826. * @note In case of error due to overrun when using ADC with DMA transfer
  1827. * (HAL ADC handle paramater "ErrorCode" to state "HAL_ADC_ERROR_OVR"):
  1828. * - Reinitialize the DMA using function "HAL_ADC_Stop_DMA()".
  1829. * - If needed, restart a new ADC conversion using function
  1830. * "HAL_ADC_Start_DMA()"
  1831. * (this function is also clearing overrun flag)
  1832. * @param hadc ADC handle
  1833. * @retval None
  1834. */
  1835. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  1836. {
  1837. /* Prevent unused argument(s) compilation warning */
  1838. UNUSED(hadc);
  1839. /* NOTE : This function should not be modified. When the callback is needed,
  1840. function HAL_ADC_ErrorCallback must be implemented in the user file.
  1841. */
  1842. }
  1843. /**
  1844. * @}
  1845. */
  1846. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  1847. * @brief Peripheral Control functions
  1848. *
  1849. @verbatim
  1850. ===============================================================================
  1851. ##### Peripheral Control functions #####
  1852. ===============================================================================
  1853. [..] This section provides functions allowing to:
  1854. (+) Configure channels on regular group
  1855. (+) Configure the analog watchdog
  1856. @endverbatim
  1857. * @{
  1858. */
  1859. /**
  1860. * @brief Configure a channel to be assigned to ADC group regular.
  1861. * @note In case of usage of internal measurement channels:
  1862. * Vbat/VrefInt/TempSensor.
  1863. * These internal paths can be disabled using function
  1864. * HAL_ADC_DeInit().
  1865. * @note Possibility to update parameters on the fly:
  1866. * This function initializes channel into ADC group regular,
  1867. * following calls to this function can be used to reconfigure
  1868. * some parameters of structure "ADC_ChannelConfTypeDef" on the fly,
  1869. * without resetting the ADC.
  1870. * The setting of these parameters is conditioned to ADC state:
  1871. * Refer to comments of structure "ADC_ChannelConfTypeDef".
  1872. * @param hadc ADC handle
  1873. * @param sConfig Structure of ADC channel assigned to ADC group regular.
  1874. * @retval HAL status
  1875. */
  1876. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  1877. {
  1878. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1879. uint32_t tmpOffsetShifted;
  1880. __IO uint32_t wait_loop_index = 0;
  1881. /* Check the parameters */
  1882. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1883. assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
  1884. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  1885. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfig->SingleDiff));
  1886. assert_param(IS_ADC_OFFSET_NUMBER(sConfig->OffsetNumber));
  1887. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfig->Offset));
  1888. /* if ROVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
  1889. ignored (considered as reset) */
  1890. assert_param(!((sConfig->OffsetNumber != ADC_OFFSET_NONE) && (hadc->Init.OversamplingMode == ENABLE)));
  1891. /* Verification of channel number */
  1892. if (sConfig->SingleDiff != ADC_DIFFERENTIAL_ENDED)
  1893. {
  1894. assert_param(IS_ADC_CHANNEL(hadc, sConfig->Channel));
  1895. }
  1896. else
  1897. {
  1898. assert_param(IS_ADC_DIFF_CHANNEL(hadc, sConfig->Channel));
  1899. }
  1900. /* Process locked */
  1901. __HAL_LOCK(hadc);
  1902. /* Parameters update conditioned to ADC state: */
  1903. /* Parameters that can be updated when ADC is disabled or enabled without */
  1904. /* conversion on going on regular group: */
  1905. /* - Channel number */
  1906. /* - Channel rank */
  1907. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1908. {
  1909. #if !defined (USE_FULL_ASSERT)
  1910. /* Correspondance for compatibility with legacy definition of */
  1911. /* sequencer ranks in direct number format. This correspondance can */
  1912. /* be done only on ranks 1 to 5 due to literal values. */
  1913. /* Note: Sequencer ranks in direct number format are no more used */
  1914. /* and are detected by activating USE_FULL_ASSERT feature. */
  1915. if (sConfig->Rank <= 5U)
  1916. {
  1917. switch (sConfig->Rank)
  1918. {
  1919. case 2U: sConfig->Rank = ADC_REGULAR_RANK_2; break;
  1920. case 3U: sConfig->Rank = ADC_REGULAR_RANK_3; break;
  1921. case 4U: sConfig->Rank = ADC_REGULAR_RANK_4; break;
  1922. case 5U: sConfig->Rank = ADC_REGULAR_RANK_5; break;
  1923. /* case 1U */
  1924. default: sConfig->Rank = ADC_REGULAR_RANK_1;
  1925. }
  1926. }
  1927. #endif
  1928. /* Set ADC group regular sequence: channel on the selected scan sequence rank */
  1929. LL_ADC_REG_SetSequencerRanks(hadc->Instance, sConfig->Rank, sConfig->Channel);
  1930. /* Parameters update conditioned to ADC state: */
  1931. /* Parameters that can be updated when ADC is disabled or enabled without */
  1932. /* conversion on going on regular group: */
  1933. /* - Channel sampling time */
  1934. /* - Channel offset */
  1935. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  1936. {
  1937. #if defined(ADC_SMPR1_SMPPLUS)
  1938. /* Manage specific case of sampling time 3.5 cycles replacing 2.5 cyles */
  1939. if(sConfig->SamplingTime == ADC_SAMPLETIME_3CYCLES_5)
  1940. {
  1941. /* Set sampling time of the selected ADC channel */
  1942. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfig->Channel, LL_ADC_SAMPLINGTIME_2CYCLES_5);
  1943. /* Set ADC sampling time common configuration */
  1944. LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_3C5_REPL_2C5);
  1945. }
  1946. else
  1947. {
  1948. /* Set sampling time of the selected ADC channel */
  1949. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfig->Channel, sConfig->SamplingTime);
  1950. /* Set ADC sampling time common configuration */
  1951. LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_DEFAULT);
  1952. }
  1953. #else
  1954. /* Set sampling time of the selected ADC channel */
  1955. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfig->Channel, sConfig->SamplingTime);
  1956. #endif
  1957. /* Configure the offset: offset enable/disable, channel, offset value */
  1958. /* Shift the offset with respect to the selected ADC resolution. */
  1959. /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  1960. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, sConfig->Offset);
  1961. if(sConfig->OffsetNumber != ADC_OFFSET_NONE)
  1962. {
  1963. /* Set ADC selected offset number */
  1964. LL_ADC_SetOffset(hadc->Instance, sConfig->OffsetNumber, sConfig->Channel, tmpOffsetShifted);
  1965. }
  1966. else
  1967. {
  1968. /* Scan each offset register to check if the selected channel is targeted. */
  1969. /* If this is the case, the corresponding offset number is disabled. */
  1970. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  1971. {
  1972. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
  1973. }
  1974. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  1975. {
  1976. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
  1977. }
  1978. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  1979. {
  1980. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
  1981. }
  1982. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  1983. {
  1984. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
  1985. }
  1986. }
  1987. }
  1988. /* Parameters update conditioned to ADC state: */
  1989. /* Parameters that can be updated only when ADC is disabled: */
  1990. /* - Single or differential mode */
  1991. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  1992. if (ADC_IS_ENABLE(hadc) == RESET)
  1993. {
  1994. /* Set mode single-ended or differential input of the selected ADC channel */
  1995. LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfig->Channel, sConfig->SingleDiff);
  1996. /* Configuration of differential mode */
  1997. if (sConfig->SingleDiff == ADC_DIFFERENTIAL_ENDED)
  1998. {
  1999. /* Set sampling time of the selected ADC channel */
  2000. LL_ADC_SetChannelSamplingTime(hadc->Instance, __LL_ADC_DECIMAL_NB_TO_CHANNEL(__LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel) + 1), sConfig->SamplingTime);
  2001. }
  2002. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor. */
  2003. /* If internal channel selected, enable dedicated internal buffers and */
  2004. /* paths. */
  2005. /* Note: these internal measurement paths can be disabled using */
  2006. /* HAL_ADC_DeInit(). */
  2007. /* Configuration of common ADC parameters */
  2008. /* If the requested internal measurement path has already been enabled, */
  2009. /* bypass the configuration processing. */
  2010. if (( (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) &&
  2011. ((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0U)) ||
  2012. ( (sConfig->Channel == ADC_CHANNEL_VBAT) &&
  2013. ((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_VBAT) == 0U)) ||
  2014. ( (sConfig->Channel == ADC_CHANNEL_VREFINT) &&
  2015. ((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_VREFINT) == 0U))
  2016. )
  2017. {
  2018. /* Configuration of common ADC parameters (continuation) */
  2019. /* Software is allowed to change common parameters only when all ADCs */
  2020. /* of the common group are disabled. */
  2021. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  2022. (ADC_ANY_OTHER_ENABLED(hadc) == RESET) )
  2023. {
  2024. if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
  2025. {
  2026. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  2027. {
  2028. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
  2029. /* Delay for temperature sensor stabilization time */
  2030. /* Wait loop initialization and execution */
  2031. /* Note: Variable divided by 2 to compensate partially */
  2032. /* CPU processing cycles. */
  2033. wait_loop_index = (LL_ADC_DELAY_TEMPSENSOR_STAB_US * (SystemCoreClock / (1000000 * 2)));
  2034. while(wait_loop_index != 0)
  2035. {
  2036. wait_loop_index--;
  2037. }
  2038. }
  2039. }
  2040. else if (sConfig->Channel == ADC_CHANNEL_VBAT)
  2041. {
  2042. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  2043. {
  2044. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
  2045. }
  2046. }
  2047. else if (sConfig->Channel == ADC_CHANNEL_VREFINT)
  2048. {
  2049. if (ADC_VREFINT_INSTANCE(hadc))
  2050. {
  2051. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
  2052. }
  2053. }
  2054. }
  2055. /* If the requested internal measurement path has already been */
  2056. /* enabled and other ADC of the common group are enabled, internal */
  2057. /* measurement paths cannot be enabled. */
  2058. else
  2059. {
  2060. /* Update ADC state machine to error */
  2061. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2062. tmp_hal_status = HAL_ERROR;
  2063. }
  2064. }
  2065. }
  2066. }
  2067. /* If a conversion is on going on regular group, no update on regular */
  2068. /* channel could be done on neither of the channel configuration structure */
  2069. /* parameters. */
  2070. else
  2071. {
  2072. /* Update ADC state machine to error */
  2073. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2074. tmp_hal_status = HAL_ERROR;
  2075. }
  2076. /* Process unlocked */
  2077. __HAL_UNLOCK(hadc);
  2078. /* Return function status */
  2079. return tmp_hal_status;
  2080. }
  2081. /**
  2082. * @brief Configure the analog watchdog.
  2083. * @note Possibility to update parameters on the fly:
  2084. * This function initializes the selected analog watchdog, successive
  2085. * calls to this function can be used to reconfigure some parameters
  2086. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
  2087. * the ADC.
  2088. * The setting of these parameters is conditioned to ADC state.
  2089. * For parameters constraints, see comments of structure
  2090. * "ADC_AnalogWDGConfTypeDef".
  2091. * @note On this STM32 serie, analog watchdog thresholds cannot be modified
  2092. * while ADC conversion is on going.
  2093. * @param hadc ADC handle
  2094. * @param AnalogWDGConfig Structure of ADC analog watchdog configuration
  2095. * @retval HAL status
  2096. */
  2097. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  2098. {
  2099. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2100. uint32_t tmpAWDHighThresholdShifted = 0U;
  2101. uint32_t tmpAWDLowThresholdShifted = 0U;
  2102. /* Check the parameters */
  2103. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2104. assert_param(IS_ADC_ANALOG_WATCHDOG_NUMBER(AnalogWDGConfig->WatchdogNumber));
  2105. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  2106. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  2107. if((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) ||
  2108. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) ||
  2109. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC) )
  2110. {
  2111. assert_param(IS_ADC_CHANNEL(hadc, AnalogWDGConfig->Channel));
  2112. }
  2113. /* Verify if threshold is within the selected ADC resolution */
  2114. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
  2115. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
  2116. /* Process locked */
  2117. __HAL_LOCK(hadc);
  2118. /* Parameters update conditioned to ADC state: */
  2119. /* Parameters that can be updated when ADC is disabled or enabled without */
  2120. /* conversion on going on ADC groups regular and injected: */
  2121. /* - Analog watchdog channels */
  2122. /* - Analog watchdog thresholds */
  2123. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  2124. {
  2125. /* Analog watchdog configuration */
  2126. if(AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
  2127. {
  2128. /* Configuration of analog watchdog: */
  2129. /* - Set the analog watchdog enable mode: one or overall group of */
  2130. /* channels, on groups regular and-or injected. */
  2131. switch(AnalogWDGConfig->WatchdogMode)
  2132. {
  2133. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2134. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel, LL_ADC_GROUP_REGULAR));
  2135. break;
  2136. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2137. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel, LL_ADC_GROUP_INJECTED));
  2138. break;
  2139. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2140. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel, LL_ADC_GROUP_REGULAR_INJECTED));
  2141. break;
  2142. case ADC_ANALOGWATCHDOG_ALL_REG:
  2143. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG);
  2144. break;
  2145. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2146. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_INJ);
  2147. break;
  2148. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2149. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2150. break;
  2151. default: /* ADC_ANALOGWATCHDOG_NONE */
  2152. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_DISABLE);
  2153. break;
  2154. }
  2155. /* Shift the offset in function of the selected ADC resolution: */
  2156. /* Thresholds have to be left-aligned on bit 11, the LSB (right bits) */
  2157. /* are set to 0 */
  2158. tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  2159. tmpAWDLowThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  2160. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2161. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, AnalogWDGConfig->WatchdogNumber, tmpAWDHighThresholdShifted, tmpAWDLowThresholdShifted);
  2162. /* Update state, clear previous result related to AWD1 */
  2163. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2164. /* Clear flag ADC analog watchdog */
  2165. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2166. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2167. /* (in case left enabled by previous ADC operations). */
  2168. LL_ADC_ClearFlag_AWD1(hadc->Instance);
  2169. /* Configure ADC analog watchdog interrupt */
  2170. if(AnalogWDGConfig->ITMode == ENABLE)
  2171. {
  2172. LL_ADC_EnableIT_AWD1(hadc->Instance);
  2173. }
  2174. else
  2175. {
  2176. LL_ADC_DisableIT_AWD1(hadc->Instance);
  2177. }
  2178. }
  2179. /* Case of ADC_ANALOGWATCHDOG_2 or ADC_ANALOGWATCHDOG_3 */
  2180. else
  2181. {
  2182. switch(AnalogWDGConfig->WatchdogMode)
  2183. {
  2184. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2185. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2186. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2187. /* Update AWD by bitfield to keep the possibility to monitor */
  2188. /* several channels by successive calls of this function. */
  2189. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2190. {
  2191. SET_BIT(hadc->Instance->AWD2CR, (1U << __LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel)));
  2192. }
  2193. else
  2194. {
  2195. SET_BIT(hadc->Instance->AWD3CR, (1U << __LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel)));
  2196. }
  2197. break;
  2198. case ADC_ANALOGWATCHDOG_ALL_REG:
  2199. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2200. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2201. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2202. break;
  2203. default: /* ADC_ANALOGWATCHDOG_NONE */
  2204. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_DISABLE);
  2205. break;
  2206. }
  2207. /* Shift the thresholds in function of the selected ADC resolution */
  2208. /* have to be left-aligned on bit 7, the LSB (right bits) are set to 0 */
  2209. tmpAWDHighThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  2210. tmpAWDLowThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  2211. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2212. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, AnalogWDGConfig->WatchdogNumber, tmpAWDHighThresholdShifted, tmpAWDLowThresholdShifted);
  2213. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2214. {
  2215. /* Update state, clear previous result related to AWD2 */
  2216. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2217. /* Clear flag ADC analog watchdog */
  2218. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2219. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2220. /* (in case left enabled by previous ADC operations). */
  2221. LL_ADC_ClearFlag_AWD2(hadc->Instance);
  2222. /* Configure ADC analog watchdog interrupt */
  2223. if(AnalogWDGConfig->ITMode == ENABLE)
  2224. {
  2225. LL_ADC_EnableIT_AWD2(hadc->Instance);
  2226. }
  2227. else
  2228. {
  2229. LL_ADC_DisableIT_AWD2(hadc->Instance);
  2230. }
  2231. }
  2232. /* (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_3) */
  2233. else
  2234. {
  2235. /* Update state, clear previous result related to AWD3 */
  2236. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2237. /* Clear flag ADC analog watchdog */
  2238. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2239. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2240. /* (in case left enabled by previous ADC operations). */
  2241. LL_ADC_ClearFlag_AWD3(hadc->Instance);
  2242. /* Configure ADC analog watchdog interrupt */
  2243. if(AnalogWDGConfig->ITMode == ENABLE)
  2244. {
  2245. LL_ADC_EnableIT_AWD3(hadc->Instance);
  2246. }
  2247. else
  2248. {
  2249. LL_ADC_DisableIT_AWD3(hadc->Instance);
  2250. }
  2251. }
  2252. }
  2253. }
  2254. /* If a conversion is on going on ADC group regular or injected, no update */
  2255. /* could be done on neither of the AWD configuration structure parameters. */
  2256. else
  2257. {
  2258. /* Update ADC state machine to error */
  2259. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2260. tmp_hal_status = HAL_ERROR;
  2261. }
  2262. /* Process unlocked */
  2263. __HAL_UNLOCK(hadc);
  2264. /* Return function status */
  2265. return tmp_hal_status;
  2266. }
  2267. /**
  2268. * @}
  2269. */
  2270. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  2271. * @brief ADC Peripheral State functions
  2272. *
  2273. @verbatim
  2274. ===============================================================================
  2275. ##### Peripheral state and errors functions #####
  2276. ===============================================================================
  2277. [..]
  2278. This subsection provides functions to get in run-time the status of the
  2279. peripheral.
  2280. (+) Check the ADC state
  2281. (+) Check the ADC error code
  2282. @endverbatim
  2283. * @{
  2284. */
  2285. /**
  2286. * @brief Return the ADC handle state.
  2287. * @note ADC state machine is managed by bitfields, ADC status must be
  2288. * compared with states bits.
  2289. * For example:
  2290. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_REG_BUSY)) "
  2291. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD1) ) "
  2292. * @param hadc ADC handle
  2293. * @retval ADC handle state (bitfield on 32 bits)
  2294. */
  2295. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
  2296. {
  2297. /* Check the parameters */
  2298. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2299. /* Return ADC handle state */
  2300. return hadc->State;
  2301. }
  2302. /**
  2303. * @brief Return the ADC error code.
  2304. * @param hadc ADC handle
  2305. * @retval ADC error code (bitfield on 32 bits)
  2306. */
  2307. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  2308. {
  2309. /* Check the parameters */
  2310. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2311. return hadc->ErrorCode;
  2312. }
  2313. /**
  2314. * @}
  2315. */
  2316. /**
  2317. * @}
  2318. */
  2319. /** @defgroup ADC_Private_Functions ADC Private Functions
  2320. * @{
  2321. */
  2322. /**
  2323. * @brief Stop ADC conversion.
  2324. * @param hadc ADC handle
  2325. * @param ConversionGroup ADC group regular and/or injected.
  2326. * This parameter can be one of the following values:
  2327. * @arg @ref ADC_REGULAR_GROUP ADC regular conversion type.
  2328. * @arg @ref ADC_INJECTED_GROUP ADC injected conversion type.
  2329. * @arg @ref ADC_REGULAR_INJECTED_GROUP ADC regular and injected conversion type.
  2330. * @retval HAL status.
  2331. */
  2332. HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc, uint32_t ConversionGroup)
  2333. {
  2334. uint32_t tmp_ADC_CR_ADSTART_JADSTART = 0;
  2335. uint32_t tickstart = 0;
  2336. uint32_t Conversion_Timeout_CPU_cycles = 0;
  2337. /* Check the parameters */
  2338. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2339. assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
  2340. /* Verification if ADC is not already stopped (on regular and injected */
  2341. /* groups) to bypass this function if not needed. */
  2342. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc))
  2343. {
  2344. /* Particular case of continuous auto-injection mode combined with */
  2345. /* auto-delay mode. */
  2346. /* In auto-injection mode, regular group stop ADC_CR_ADSTP is used (not */
  2347. /* injected group stop ADC_CR_JADSTP). */
  2348. /* Procedure to be followed: Wait until JEOS=1, clear JEOS, set ADSTP=1 */
  2349. /* (see reference manual). */
  2350. if ((HAL_IS_BIT_SET(hadc->Instance->CFGR, ADC_CFGR_JAUTO))
  2351. && (hadc->Init.ContinuousConvMode==ENABLE)
  2352. && (hadc->Init.LowPowerAutoWait==ENABLE))
  2353. {
  2354. /* Use stop of regular group */
  2355. ConversionGroup = ADC_REGULAR_GROUP;
  2356. /* Wait until JEOS=1 (maximum Timeout: 4 injected conversions) */
  2357. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) == RESET)
  2358. {
  2359. if (Conversion_Timeout_CPU_cycles >= (ADC_CONVERSION_TIME_MAX_CPU_CYCLES *4))
  2360. {
  2361. /* Update ADC state machine to error */
  2362. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2363. /* Set ADC error code to ADC IP internal error */
  2364. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2365. return HAL_ERROR;
  2366. }
  2367. Conversion_Timeout_CPU_cycles ++;
  2368. }
  2369. /* Clear JEOS */
  2370. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOS);
  2371. }
  2372. /* Stop potential conversion on going on regular group */
  2373. if (ConversionGroup != ADC_INJECTED_GROUP)
  2374. {
  2375. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  2376. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) &&
  2377. HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
  2378. {
  2379. /* Stop conversions on regular group */
  2380. LL_ADC_REG_StopConversion(hadc->Instance);
  2381. }
  2382. }
  2383. /* Stop potential conversion on going on injected group */
  2384. if (ConversionGroup != ADC_REGULAR_GROUP)
  2385. {
  2386. /* Software is allowed to set JADSTP only when JADSTART=1 and ADDIS=0 */
  2387. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_JADSTART) &&
  2388. HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
  2389. {
  2390. /* Stop conversions on injected group */
  2391. SET_BIT(hadc->Instance->CR, ADC_CR_JADSTP);
  2392. }
  2393. }
  2394. /* Selection of start and stop bits with respect to the regular or injected group */
  2395. switch(ConversionGroup)
  2396. {
  2397. case ADC_REGULAR_INJECTED_GROUP:
  2398. tmp_ADC_CR_ADSTART_JADSTART = (ADC_CR_ADSTART | ADC_CR_JADSTART);
  2399. break;
  2400. case ADC_INJECTED_GROUP:
  2401. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_JADSTART;
  2402. break;
  2403. /* Case ADC_REGULAR_GROUP only*/
  2404. default:
  2405. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_ADSTART;
  2406. break;
  2407. }
  2408. /* Wait for conversion effectively stopped */
  2409. tickstart = HAL_GetTick();
  2410. while((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != RESET)
  2411. {
  2412. if((HAL_GetTick()-tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  2413. {
  2414. /* Update ADC state machine to error */
  2415. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2416. /* Set ADC error code to ADC IP internal error */
  2417. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2418. return HAL_ERROR;
  2419. }
  2420. }
  2421. }
  2422. /* Return HAL status */
  2423. return HAL_OK;
  2424. }
  2425. /**
  2426. * @brief Enable the selected ADC.
  2427. * @note Prerequisite condition to use this function: ADC must be disabled
  2428. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  2429. * @param hadc ADC handle
  2430. * @retval HAL status.
  2431. */
  2432. HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
  2433. {
  2434. uint32_t tickstart = 0;
  2435. __IO uint32_t wait_loop_index = 0;
  2436. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  2437. /* enabling phase not yet completed: flag ADC ready not yet set). */
  2438. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  2439. /* causes: ADC clock not running, ...). */
  2440. if (ADC_IS_ENABLE(hadc) == RESET)
  2441. {
  2442. /* Check if conditions to enable the ADC are fulfilled */
  2443. if (ADC_ENABLING_CONDITIONS(hadc) == RESET)
  2444. {
  2445. /* Update ADC state machine to error */
  2446. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2447. /* Set ADC error code to ADC IP internal error */
  2448. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2449. return HAL_ERROR;
  2450. }
  2451. /* Enable the ADC peripheral */
  2452. LL_ADC_Enable(hadc->Instance);
  2453. /* Delay for ADC stabilization time */
  2454. /* Wait loop initialization and execution */
  2455. /* Note: Variable divided by 2 to compensate partially */
  2456. /* CPU processing cycles. */
  2457. wait_loop_index = (LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (1000000 * 2)));
  2458. while(wait_loop_index != 0)
  2459. {
  2460. wait_loop_index--;
  2461. }
  2462. /* Wait for ADC effectively enabled */
  2463. tickstart = HAL_GetTick();
  2464. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET)
  2465. {
  2466. /* If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
  2467. has been cleared (after a calibration), ADEN bit is reset by the
  2468. calibration logic.
  2469. The workaround is to continue setting ADEN until ADRDY is becomes 1.
  2470. Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
  2471. 4 ADC clock cycle duration */
  2472. /* Note: Test of ADC enabled required due to hardware constraint to */
  2473. /* not enable ADC if already enabled. */
  2474. if(LL_ADC_IsEnabled(hadc->Instance) == 0)
  2475. {
  2476. LL_ADC_Enable(hadc->Instance);
  2477. }
  2478. if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  2479. {
  2480. /* Update ADC state machine to error */
  2481. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2482. /* Set ADC error code to ADC IP internal error */
  2483. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2484. return HAL_ERROR;
  2485. }
  2486. }
  2487. }
  2488. /* Return HAL status */
  2489. return HAL_OK;
  2490. }
  2491. /**
  2492. * @brief Disable the selected ADC.
  2493. * @note Prerequisite condition to use this function: ADC conversions must be
  2494. * stopped.
  2495. * @param hadc ADC handle
  2496. * @retval HAL status.
  2497. */
  2498. HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc)
  2499. {
  2500. uint32_t tickstart = 0;
  2501. /* Verification if ADC is not already disabled: */
  2502. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  2503. /* disabled. */
  2504. if (ADC_IS_ENABLE(hadc) != RESET)
  2505. {
  2506. /* Check if conditions to disable the ADC are fulfilled */
  2507. if (ADC_DISABLING_CONDITIONS(hadc) != RESET)
  2508. {
  2509. /* Disable the ADC peripheral */
  2510. LL_ADC_Disable(hadc->Instance);
  2511. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  2512. }
  2513. else
  2514. {
  2515. /* Update ADC state machine to error */
  2516. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2517. /* Set ADC error code to ADC IP internal error */
  2518. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2519. return HAL_ERROR;
  2520. }
  2521. /* Wait for ADC effectively disabled */
  2522. /* Get tick count */
  2523. tickstart = HAL_GetTick();
  2524. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
  2525. {
  2526. if((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  2527. {
  2528. /* Update ADC state machine to error */
  2529. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2530. /* Set ADC error code to ADC IP internal error */
  2531. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2532. return HAL_ERROR;
  2533. }
  2534. }
  2535. }
  2536. /* Return HAL status */
  2537. return HAL_OK;
  2538. }
  2539. /**
  2540. * @brief DMA transfer complete callback.
  2541. * @param hdma pointer to DMA handle.
  2542. * @retval None
  2543. */
  2544. void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  2545. {
  2546. /* Retrieve ADC handle corresponding to current DMA handle */
  2547. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2548. /* Update state machine on conversion status if not in error state */
  2549. if(HAL_IS_BIT_CLR(hadc->State, (HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)))
  2550. {
  2551. /* Set ADC state */
  2552. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2553. /* Determine whether any further conversion upcoming on group regular */
  2554. /* by external trigger, continuous mode or scan sequence on going */
  2555. /* to disable interruption. */
  2556. /* Is it the end of the regular sequence ? */
  2557. if(HAL_IS_BIT_SET(hadc->Instance->ISR, ADC_FLAG_EOS))
  2558. {
  2559. /* Are conversions software-triggered ? */
  2560. if(ADC_IS_SOFTWARE_START_REGULAR(hadc))
  2561. {
  2562. /* Is CONT bit set ? */
  2563. if(READ_BIT(hadc->Instance->CFGR, ADC_CFGR_CONT) == RESET)
  2564. {
  2565. /* CONT bit is not set, no more conversions expected */
  2566. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2567. if(HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  2568. {
  2569. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2570. }
  2571. }
  2572. }
  2573. }
  2574. else
  2575. {
  2576. /* DMA End of Transfer interrupt was triggered but conversions sequence
  2577. is not over. If DMACFG is set to 0, conversions are stopped. */
  2578. if(READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMACFG) == RESET)
  2579. {
  2580. /* DMACFG bit is not set, conversions are stopped. */
  2581. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2582. if(HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  2583. {
  2584. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2585. }
  2586. }
  2587. }
  2588. /* Conversion complete callback */
  2589. HAL_ADC_ConvCpltCallback(hadc);
  2590. }
  2591. else /* DMA and-or internal error occurred */
  2592. {
  2593. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  2594. {
  2595. /* Call HAL ADC Error Callback function */
  2596. HAL_ADC_ErrorCallback(hadc);
  2597. }
  2598. else
  2599. {
  2600. /* Call ADC DMA error callback */
  2601. hadc->DMA_Handle->XferErrorCallback(hdma);
  2602. }
  2603. }
  2604. }
  2605. /**
  2606. * @brief DMA half transfer complete callback.
  2607. * @param hdma pointer to DMA handle.
  2608. * @retval None
  2609. */
  2610. void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  2611. {
  2612. /* Retrieve ADC handle corresponding to current DMA handle */
  2613. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2614. /* Half conversion callback */
  2615. HAL_ADC_ConvHalfCpltCallback(hadc);
  2616. }
  2617. /**
  2618. * @brief DMA error callback.
  2619. * @param hdma pointer to DMA handle.
  2620. * @retval None
  2621. */
  2622. void ADC_DMAError(DMA_HandleTypeDef *hdma)
  2623. {
  2624. /* Retrieve ADC handle corresponding to current DMA handle */
  2625. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2626. /* Set ADC state */
  2627. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  2628. /* Set ADC error code to DMA error */
  2629. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  2630. /* Error callback */
  2631. HAL_ADC_ErrorCallback(hadc);
  2632. }
  2633. /**
  2634. * @}
  2635. */
  2636. #endif /* HAL_ADC_MODULE_ENABLED */
  2637. /**
  2638. * @}
  2639. */
  2640. /**
  2641. * @}
  2642. */
  2643. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/