stm32h7xx_hal_adc.c 126 KB

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