stm32l4xx_hal_adc.c 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056
  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. if (ADC_IS_ENABLE(hadc) == RESET)
  1992. {
  1993. /* Set mode single-ended or differential input of the selected ADC channel */
  1994. LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfig->Channel, sConfig->SingleDiff);
  1995. /* Configuration of differential mode */
  1996. if (sConfig->SingleDiff == ADC_DIFFERENTIAL_ENDED)
  1997. {
  1998. /* Set sampling time of the selected ADC channel */
  1999. LL_ADC_SetChannelSamplingTime(hadc->Instance, __LL_ADC_DECIMAL_NB_TO_CHANNEL(__LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel) + 1), sConfig->SamplingTime);
  2000. }
  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. if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
  2020. {
  2021. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  2022. {
  2023. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
  2024. /* Delay for temperature sensor stabilization time */
  2025. /* Wait loop initialization and execution */
  2026. /* Note: Variable divided by 2 to compensate partially */
  2027. /* CPU processing cycles. */
  2028. wait_loop_index = (LL_ADC_DELAY_TEMPSENSOR_STAB_US * (SystemCoreClock / (1000000 * 2)));
  2029. while(wait_loop_index != 0)
  2030. {
  2031. wait_loop_index--;
  2032. }
  2033. }
  2034. }
  2035. else if (sConfig->Channel == ADC_CHANNEL_VBAT)
  2036. {
  2037. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  2038. {
  2039. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
  2040. }
  2041. }
  2042. else if (sConfig->Channel == ADC_CHANNEL_VREFINT)
  2043. {
  2044. if (ADC_VREFINT_INSTANCE(hadc))
  2045. {
  2046. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
  2047. }
  2048. }
  2049. }
  2050. }
  2051. /* If a conversion is on going on regular group, no update on regular */
  2052. /* channel could be done on neither of the channel configuration structure */
  2053. /* parameters. */
  2054. else
  2055. {
  2056. /* Update ADC state machine to error */
  2057. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2058. tmp_hal_status = HAL_ERROR;
  2059. }
  2060. /* Process unlocked */
  2061. __HAL_UNLOCK(hadc);
  2062. /* Return function status */
  2063. return tmp_hal_status;
  2064. }
  2065. /**
  2066. * @brief Configure the analog watchdog.
  2067. * @note Possibility to update parameters on the fly:
  2068. * This function initializes the selected analog watchdog, successive
  2069. * calls to this function can be used to reconfigure some parameters
  2070. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
  2071. * the ADC.
  2072. * The setting of these parameters is conditioned to ADC state.
  2073. * For parameters constraints, see comments of structure
  2074. * "ADC_AnalogWDGConfTypeDef".
  2075. * @note On this STM32 serie, analog watchdog thresholds cannot be modified
  2076. * while ADC conversion is on going.
  2077. * @param hadc ADC handle
  2078. * @param AnalogWDGConfig Structure of ADC analog watchdog configuration
  2079. * @retval HAL status
  2080. */
  2081. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  2082. {
  2083. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2084. uint32_t tmpAWDHighThresholdShifted = 0U;
  2085. uint32_t tmpAWDLowThresholdShifted = 0U;
  2086. /* Check the parameters */
  2087. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2088. assert_param(IS_ADC_ANALOG_WATCHDOG_NUMBER(AnalogWDGConfig->WatchdogNumber));
  2089. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  2090. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  2091. if((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) ||
  2092. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) ||
  2093. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC) )
  2094. {
  2095. assert_param(IS_ADC_CHANNEL(hadc, AnalogWDGConfig->Channel));
  2096. }
  2097. /* Verify if threshold is within the selected ADC resolution */
  2098. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
  2099. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
  2100. /* Process locked */
  2101. __HAL_LOCK(hadc);
  2102. /* Parameters update conditioned to ADC state: */
  2103. /* Parameters that can be updated when ADC is disabled or enabled without */
  2104. /* conversion on going on ADC groups regular and injected: */
  2105. /* - Analog watchdog channels */
  2106. /* - Analog watchdog thresholds */
  2107. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  2108. {
  2109. /* Analog watchdog configuration */
  2110. if(AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
  2111. {
  2112. /* Configuration of analog watchdog: */
  2113. /* - Set the analog watchdog enable mode: one or overall group of */
  2114. /* channels, on groups regular and-or injected. */
  2115. switch(AnalogWDGConfig->WatchdogMode)
  2116. {
  2117. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2118. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel, LL_ADC_GROUP_REGULAR));
  2119. break;
  2120. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2121. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel, LL_ADC_GROUP_INJECTED));
  2122. break;
  2123. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2124. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel, LL_ADC_GROUP_REGULAR_INJECTED));
  2125. break;
  2126. case ADC_ANALOGWATCHDOG_ALL_REG:
  2127. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG);
  2128. break;
  2129. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2130. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_INJ);
  2131. break;
  2132. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2133. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2134. break;
  2135. default: /* ADC_ANALOGWATCHDOG_NONE */
  2136. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_DISABLE);
  2137. break;
  2138. }
  2139. /* Shift the offset in function of the selected ADC resolution: */
  2140. /* Thresholds have to be left-aligned on bit 11, the LSB (right bits) */
  2141. /* are set to 0 */
  2142. tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  2143. tmpAWDLowThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  2144. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2145. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, AnalogWDGConfig->WatchdogNumber, tmpAWDHighThresholdShifted, tmpAWDLowThresholdShifted);
  2146. /* Update state, clear previous result related to AWD1 */
  2147. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2148. /* Clear flag ADC analog watchdog */
  2149. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2150. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2151. /* (in case left enabled by previous ADC operations). */
  2152. LL_ADC_ClearFlag_AWD1(hadc->Instance);
  2153. /* Configure ADC analog watchdog interrupt */
  2154. if(AnalogWDGConfig->ITMode == ENABLE)
  2155. {
  2156. LL_ADC_EnableIT_AWD1(hadc->Instance);
  2157. }
  2158. else
  2159. {
  2160. LL_ADC_DisableIT_AWD1(hadc->Instance);
  2161. }
  2162. }
  2163. /* Case of ADC_ANALOGWATCHDOG_2 or ADC_ANALOGWATCHDOG_3 */
  2164. else
  2165. {
  2166. switch(AnalogWDGConfig->WatchdogMode)
  2167. {
  2168. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2169. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2170. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2171. /* Update AWD by bitfield to keep the possibility to monitor */
  2172. /* several channels by successive calls of this function. */
  2173. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2174. {
  2175. SET_BIT(hadc->Instance->AWD2CR, (1U << __LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel)));
  2176. }
  2177. else
  2178. {
  2179. SET_BIT(hadc->Instance->AWD3CR, (1U << __LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel)));
  2180. }
  2181. break;
  2182. case ADC_ANALOGWATCHDOG_ALL_REG:
  2183. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2184. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2185. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2186. break;
  2187. default: /* ADC_ANALOGWATCHDOG_NONE */
  2188. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_DISABLE);
  2189. break;
  2190. }
  2191. /* Shift the thresholds in function of the selected ADC resolution */
  2192. /* have to be left-aligned on bit 7, the LSB (right bits) are set to 0 */
  2193. tmpAWDHighThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  2194. tmpAWDLowThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  2195. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2196. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, AnalogWDGConfig->WatchdogNumber, tmpAWDHighThresholdShifted, tmpAWDLowThresholdShifted);
  2197. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2198. {
  2199. /* Update state, clear previous result related to AWD2 */
  2200. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2201. /* Clear flag ADC analog watchdog */
  2202. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2203. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2204. /* (in case left enabled by previous ADC operations). */
  2205. LL_ADC_ClearFlag_AWD2(hadc->Instance);
  2206. /* Configure ADC analog watchdog interrupt */
  2207. if(AnalogWDGConfig->ITMode == ENABLE)
  2208. {
  2209. LL_ADC_EnableIT_AWD2(hadc->Instance);
  2210. }
  2211. else
  2212. {
  2213. LL_ADC_DisableIT_AWD2(hadc->Instance);
  2214. }
  2215. }
  2216. /* (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_3) */
  2217. else
  2218. {
  2219. /* Update state, clear previous result related to AWD3 */
  2220. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2221. /* Clear flag ADC analog watchdog */
  2222. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2223. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2224. /* (in case left enabled by previous ADC operations). */
  2225. LL_ADC_ClearFlag_AWD3(hadc->Instance);
  2226. /* Configure ADC analog watchdog interrupt */
  2227. if(AnalogWDGConfig->ITMode == ENABLE)
  2228. {
  2229. LL_ADC_EnableIT_AWD3(hadc->Instance);
  2230. }
  2231. else
  2232. {
  2233. LL_ADC_DisableIT_AWD3(hadc->Instance);
  2234. }
  2235. }
  2236. }
  2237. }
  2238. /* If a conversion is on going on ADC group regular or injected, no update */
  2239. /* could be done on neither of the AWD configuration structure parameters. */
  2240. else
  2241. {
  2242. /* Update ADC state machine to error */
  2243. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2244. tmp_hal_status = HAL_ERROR;
  2245. }
  2246. /* Process unlocked */
  2247. __HAL_UNLOCK(hadc);
  2248. /* Return function status */
  2249. return tmp_hal_status;
  2250. }
  2251. /**
  2252. * @}
  2253. */
  2254. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  2255. * @brief ADC Peripheral State functions
  2256. *
  2257. @verbatim
  2258. ===============================================================================
  2259. ##### Peripheral state and errors functions #####
  2260. ===============================================================================
  2261. [..]
  2262. This subsection provides functions to get in run-time the status of the
  2263. peripheral.
  2264. (+) Check the ADC state
  2265. (+) Check the ADC error code
  2266. @endverbatim
  2267. * @{
  2268. */
  2269. /**
  2270. * @brief Return the ADC handle state.
  2271. * @note ADC state machine is managed by bitfields, ADC status must be
  2272. * compared with states bits.
  2273. * For example:
  2274. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_REG_BUSY)) "
  2275. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD1) ) "
  2276. * @param hadc ADC handle
  2277. * @retval ADC handle state (bitfield on 32 bits)
  2278. */
  2279. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
  2280. {
  2281. /* Check the parameters */
  2282. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2283. /* Return ADC handle state */
  2284. return hadc->State;
  2285. }
  2286. /**
  2287. * @brief Return the ADC error code.
  2288. * @param hadc ADC handle
  2289. * @retval ADC error code (bitfield on 32 bits)
  2290. */
  2291. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  2292. {
  2293. /* Check the parameters */
  2294. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2295. return hadc->ErrorCode;
  2296. }
  2297. /**
  2298. * @}
  2299. */
  2300. /**
  2301. * @}
  2302. */
  2303. /** @defgroup ADC_Private_Functions ADC Private Functions
  2304. * @{
  2305. */
  2306. /**
  2307. * @brief Stop ADC conversion.
  2308. * @param hadc ADC handle
  2309. * @param ConversionGroup ADC group regular and/or injected.
  2310. * This parameter can be one of the following values:
  2311. * @arg @ref ADC_REGULAR_GROUP ADC regular conversion type.
  2312. * @arg @ref ADC_INJECTED_GROUP ADC injected conversion type.
  2313. * @arg @ref ADC_REGULAR_INJECTED_GROUP ADC regular and injected conversion type.
  2314. * @retval HAL status.
  2315. */
  2316. HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc, uint32_t ConversionGroup)
  2317. {
  2318. uint32_t tmp_ADC_CR_ADSTART_JADSTART = 0;
  2319. uint32_t tickstart = 0;
  2320. uint32_t Conversion_Timeout_CPU_cycles = 0;
  2321. /* Check the parameters */
  2322. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2323. assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
  2324. /* Verification if ADC is not already stopped (on regular and injected */
  2325. /* groups) to bypass this function if not needed. */
  2326. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc))
  2327. {
  2328. /* Particular case of continuous auto-injection mode combined with */
  2329. /* auto-delay mode. */
  2330. /* In auto-injection mode, regular group stop ADC_CR_ADSTP is used (not */
  2331. /* injected group stop ADC_CR_JADSTP). */
  2332. /* Procedure to be followed: Wait until JEOS=1, clear JEOS, set ADSTP=1 */
  2333. /* (see reference manual). */
  2334. if ((HAL_IS_BIT_SET(hadc->Instance->CFGR, ADC_CFGR_JAUTO))
  2335. && (hadc->Init.ContinuousConvMode==ENABLE)
  2336. && (hadc->Init.LowPowerAutoWait==ENABLE))
  2337. {
  2338. /* Use stop of regular group */
  2339. ConversionGroup = ADC_REGULAR_GROUP;
  2340. /* Wait until JEOS=1 (maximum Timeout: 4 injected conversions) */
  2341. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) == RESET)
  2342. {
  2343. if (Conversion_Timeout_CPU_cycles >= (ADC_CONVERSION_TIME_MAX_CPU_CYCLES *4))
  2344. {
  2345. /* Update ADC state machine to error */
  2346. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2347. /* Set ADC error code to ADC IP internal error */
  2348. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2349. return HAL_ERROR;
  2350. }
  2351. Conversion_Timeout_CPU_cycles ++;
  2352. }
  2353. /* Clear JEOS */
  2354. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOS);
  2355. }
  2356. /* Stop potential conversion on going on regular group */
  2357. if (ConversionGroup != ADC_INJECTED_GROUP)
  2358. {
  2359. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  2360. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) &&
  2361. HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
  2362. {
  2363. /* Stop conversions on regular group */
  2364. LL_ADC_REG_StopConversion(hadc->Instance);
  2365. }
  2366. }
  2367. /* Stop potential conversion on going on injected group */
  2368. if (ConversionGroup != ADC_REGULAR_GROUP)
  2369. {
  2370. /* Software is allowed to set JADSTP only when JADSTART=1 and ADDIS=0 */
  2371. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_JADSTART) &&
  2372. HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
  2373. {
  2374. /* Stop conversions on injected group */
  2375. SET_BIT(hadc->Instance->CR, ADC_CR_JADSTP);
  2376. }
  2377. }
  2378. /* Selection of start and stop bits with respect to the regular or injected group */
  2379. switch(ConversionGroup)
  2380. {
  2381. case ADC_REGULAR_INJECTED_GROUP:
  2382. tmp_ADC_CR_ADSTART_JADSTART = (ADC_CR_ADSTART | ADC_CR_JADSTART);
  2383. break;
  2384. case ADC_INJECTED_GROUP:
  2385. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_JADSTART;
  2386. break;
  2387. /* Case ADC_REGULAR_GROUP only*/
  2388. default:
  2389. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_ADSTART;
  2390. break;
  2391. }
  2392. /* Wait for conversion effectively stopped */
  2393. tickstart = HAL_GetTick();
  2394. while((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != RESET)
  2395. {
  2396. if((HAL_GetTick()-tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  2397. {
  2398. /* Update ADC state machine to error */
  2399. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2400. /* Set ADC error code to ADC IP internal error */
  2401. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2402. return HAL_ERROR;
  2403. }
  2404. }
  2405. }
  2406. /* Return HAL status */
  2407. return HAL_OK;
  2408. }
  2409. /**
  2410. * @brief Enable the selected ADC.
  2411. * @note Prerequisite condition to use this function: ADC must be disabled
  2412. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  2413. * @param hadc ADC handle
  2414. * @retval HAL status.
  2415. */
  2416. HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
  2417. {
  2418. uint32_t tickstart = 0;
  2419. __IO uint32_t wait_loop_index = 0;
  2420. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  2421. /* enabling phase not yet completed: flag ADC ready not yet set). */
  2422. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  2423. /* causes: ADC clock not running, ...). */
  2424. if (ADC_IS_ENABLE(hadc) == RESET)
  2425. {
  2426. /* Check if conditions to enable the ADC are fulfilled */
  2427. if (ADC_ENABLING_CONDITIONS(hadc) == RESET)
  2428. {
  2429. /* Update ADC state machine to error */
  2430. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2431. /* Set ADC error code to ADC IP internal error */
  2432. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2433. return HAL_ERROR;
  2434. }
  2435. /* Enable the ADC peripheral */
  2436. LL_ADC_Enable(hadc->Instance);
  2437. /* Delay for ADC stabilization time */
  2438. /* Wait loop initialization and execution */
  2439. /* Note: Variable divided by 2 to compensate partially */
  2440. /* CPU processing cycles. */
  2441. wait_loop_index = (LL_ADC_DELAY_INTERNAL_REGUL_STAB_US * (SystemCoreClock / (1000000 * 2)));
  2442. while(wait_loop_index != 0)
  2443. {
  2444. wait_loop_index--;
  2445. }
  2446. /* Wait for ADC effectively enabled */
  2447. tickstart = HAL_GetTick();
  2448. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET)
  2449. {
  2450. /* If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
  2451. has been cleared (after a calibration), ADEN bit is reset by the
  2452. calibration logic.
  2453. The workaround is to continue setting ADEN until ADRDY is becomes 1.
  2454. Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
  2455. 4 ADC clock cycle duration */
  2456. /* Note: Test of ADC enabled required due to hardware constraint to */
  2457. /* not enable ADC if already enabled. */
  2458. if(LL_ADC_IsEnabled(hadc->Instance) == 0)
  2459. {
  2460. LL_ADC_Enable(hadc->Instance);
  2461. }
  2462. if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  2463. {
  2464. /* Update ADC state machine to error */
  2465. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2466. /* Set ADC error code to ADC IP internal error */
  2467. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2468. return HAL_ERROR;
  2469. }
  2470. }
  2471. }
  2472. /* Return HAL status */
  2473. return HAL_OK;
  2474. }
  2475. /**
  2476. * @brief Disable the selected ADC.
  2477. * @note Prerequisite condition to use this function: ADC conversions must be
  2478. * stopped.
  2479. * @param hadc ADC handle
  2480. * @retval HAL status.
  2481. */
  2482. HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc)
  2483. {
  2484. uint32_t tickstart = 0;
  2485. /* Verification if ADC is not already disabled: */
  2486. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  2487. /* disabled. */
  2488. if (ADC_IS_ENABLE(hadc) != RESET)
  2489. {
  2490. /* Check if conditions to disable the ADC are fulfilled */
  2491. if (ADC_DISABLING_CONDITIONS(hadc) != RESET)
  2492. {
  2493. /* Disable the ADC peripheral */
  2494. LL_ADC_Disable(hadc->Instance);
  2495. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  2496. }
  2497. else
  2498. {
  2499. /* Update ADC state machine to error */
  2500. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2501. /* Set ADC error code to ADC IP internal error */
  2502. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2503. return HAL_ERROR;
  2504. }
  2505. /* Wait for ADC effectively disabled */
  2506. /* Get tick count */
  2507. tickstart = HAL_GetTick();
  2508. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
  2509. {
  2510. if((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  2511. {
  2512. /* Update ADC state machine to error */
  2513. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2514. /* Set ADC error code to ADC IP internal error */
  2515. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2516. return HAL_ERROR;
  2517. }
  2518. }
  2519. }
  2520. /* Return HAL status */
  2521. return HAL_OK;
  2522. }
  2523. /**
  2524. * @brief DMA transfer complete callback.
  2525. * @param hdma pointer to DMA handle.
  2526. * @retval None
  2527. */
  2528. void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  2529. {
  2530. /* Retrieve ADC handle corresponding to current DMA handle */
  2531. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2532. /* Update state machine on conversion status if not in error state */
  2533. if(HAL_IS_BIT_CLR(hadc->State, (HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)))
  2534. {
  2535. /* Set ADC state */
  2536. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2537. /* Determine whether any further conversion upcoming on group regular */
  2538. /* by external trigger, continuous mode or scan sequence on going */
  2539. /* to disable interruption. */
  2540. /* Is it the end of the regular sequence ? */
  2541. if(HAL_IS_BIT_SET(hadc->Instance->ISR, ADC_FLAG_EOS))
  2542. {
  2543. /* Are conversions software-triggered ? */
  2544. if(ADC_IS_SOFTWARE_START_REGULAR(hadc))
  2545. {
  2546. /* Is CONT bit set ? */
  2547. if(READ_BIT(hadc->Instance->CFGR, ADC_CFGR_CONT) == RESET)
  2548. {
  2549. /* CONT bit is not set, no more conversions expected */
  2550. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2551. if(HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  2552. {
  2553. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2554. }
  2555. }
  2556. }
  2557. }
  2558. else
  2559. {
  2560. /* DMA End of Transfer interrupt was triggered but conversions sequence
  2561. is not over. If DMACFG is set to 0, conversions are stopped. */
  2562. if(READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMACFG) == RESET)
  2563. {
  2564. /* DMACFG bit is not set, conversions are stopped. */
  2565. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2566. if(HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  2567. {
  2568. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2569. }
  2570. }
  2571. }
  2572. /* Conversion complete callback */
  2573. HAL_ADC_ConvCpltCallback(hadc);
  2574. }
  2575. else /* DMA and-or internal error occurred */
  2576. {
  2577. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  2578. {
  2579. /* Call HAL ADC Error Callback function */
  2580. HAL_ADC_ErrorCallback(hadc);
  2581. }
  2582. else
  2583. {
  2584. /* Call ADC DMA error callback */
  2585. hadc->DMA_Handle->XferErrorCallback(hdma);
  2586. }
  2587. }
  2588. }
  2589. /**
  2590. * @brief DMA half transfer complete callback.
  2591. * @param hdma pointer to DMA handle.
  2592. * @retval None
  2593. */
  2594. void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  2595. {
  2596. /* Retrieve ADC handle corresponding to current DMA handle */
  2597. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2598. /* Half conversion callback */
  2599. HAL_ADC_ConvHalfCpltCallback(hadc);
  2600. }
  2601. /**
  2602. * @brief DMA error callback.
  2603. * @param hdma pointer to DMA handle.
  2604. * @retval None
  2605. */
  2606. void ADC_DMAError(DMA_HandleTypeDef *hdma)
  2607. {
  2608. /* Retrieve ADC handle corresponding to current DMA handle */
  2609. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2610. /* Set ADC state */
  2611. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  2612. /* Set ADC error code to DMA error */
  2613. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  2614. /* Error callback */
  2615. HAL_ADC_ErrorCallback(hadc);
  2616. }
  2617. /**
  2618. * @}
  2619. */
  2620. #endif /* HAL_ADC_MODULE_ENABLED */
  2621. /**
  2622. * @}
  2623. */
  2624. /**
  2625. * @}
  2626. */
  2627. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/