stm32f3xx_hal_sdadc.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_hal_sdadc.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Sigma-Delta Analog to Digital Converter
  7. * (SDADC) peripherals:
  8. * + Initialization and Configuration
  9. * + Regular Channels Configuration
  10. * + Injected channels Configuration
  11. * + Power saving
  12. * + Regular/Injected Channels DMA Configuration
  13. * + Interrupts and flags management
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2016 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ==============================================================================
  27. ##### SDADC specific features #####
  28. ==============================================================================
  29. [..]
  30. (#) 16-bit sigma delta architecture.
  31. (#) Self calibration.
  32. (#) Interrupt generation at the end of calibration, regular/injected conversion
  33. and in case of overrun events.
  34. (#) Single and continuous conversion modes.
  35. (#) External trigger option with configurable polarity for injected conversion.
  36. (#) Multi mode (synchronized another SDADC with SDADC1).
  37. (#) DMA request generation during regular or injected channel conversion.
  38. ##### How to use this driver #####
  39. ==============================================================================
  40. [..]
  41. *** Initialization ***
  42. ======================
  43. [..]
  44. (#) As prerequisite, fill in the HAL_SDADC_MspInit() :
  45. (++) Enable SDADCx clock interface with __SDADCx_CLK_ENABLE().
  46. (++) Configure SDADCx clock divider with HAL_RCCEx_PeriphCLKConfig.
  47. (++) Enable power on SDADC with HAL_PWREx_EnableSDADC().
  48. (++) Enable the clocks for the SDADC GPIOS with __HAL_RCC_GPIOx_CLK_ENABLE().
  49. (++) Configure these SDADC pins in analog mode using HAL_GPIO_Init().
  50. (++) If interrupt mode is used, enable and configure SDADC global
  51. interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
  52. (++) If DMA mode is used, configure DMA with HAL_DMA_Init and link it
  53. with SDADC handle using __HAL_LINKDMA.
  54. (#) Configure the SDADC low power mode, fast conversion mode, slow clock
  55. mode and SDADC1 reference voltage using the HAL_SDADC_Init() function.
  56. Note: Common reference voltage. is common to all SDADC instances.
  57. (#) Prepare channel configurations (input mode, common mode, gain and
  58. offset) using HAL_SDADC_PrepareChannelConfig and associate channel
  59. with one configuration using HAL_SDADC_AssociateChannelConfig.
  60. *** Calibration ***
  61. ============================================
  62. [..]
  63. (#) Start calibration using HAL_SDADC_StartCalibration or
  64. HAL_SDADC_CalibrationStart_IT.
  65. (#) In polling mode, use HAL_SDADC_PollForCalibEvent to detect the end of
  66. calibration.
  67. (#) In interrupt mode, HAL_SDADC_CalibrationCpltCallback will be called at
  68. the end of calibration.
  69. *** Regular channel conversion ***
  70. ============================================
  71. [..]
  72. (#) Select trigger for regular conversion using
  73. HAL_SDADC_SelectRegularTrigger.
  74. (#) Select regular channel and enable/disable continuous mode using
  75. HAL_SDADC_ConfigChannel.
  76. (#) Start regular conversion using HAL_SDADC_Start, HAL_SDADC_Start_IT
  77. or HAL_SDADC_Start_DMA.
  78. (#) In polling mode, use HAL_SDADC_PollForConversion to detect the end of
  79. regular conversion.
  80. (#) In interrupt mode, HAL_SDADC_ConvCpltCallback will be called at the
  81. end of regular conversion.
  82. (#) Get value of regular conversion using HAL_SDADC_GetValue.
  83. (#) In DMA mode, HAL_SDADC_ConvHalfCpltCallback and
  84. HAL_SDADC_ConvCpltCallback will be called respectively at the half
  85. transfer and at the transfer complete.
  86. (#) Stop regular conversion using HAL_SDADC_Stop, HAL_SDADC_Stop_IT
  87. or HAL_SDADC_Stop_DMA.
  88. *** Injected channels conversion ***
  89. ============================================
  90. [..]
  91. (#) Enable/disable delay on injected conversion using
  92. HAL_SDADC_SelectInjectedDelay.
  93. (#) If external trigger is used for injected conversion, configure this
  94. trigger using HAL_SDADC_SelectInjectedExtTrigger.
  95. (#) Select trigger for injected conversion using
  96. HAL_SDADC_SelectInjectedTrigger.
  97. (#) Select injected channels and enable/disable continuous mode using
  98. HAL_SDADC_InjectedConfigChannel.
  99. (#) Start injected conversion using HAL_SDADC_InjectedStart,
  100. HAL_SDADC_InjectedStart_IT or HAL_SDADC_InjectedStart_DMA.
  101. (#) In polling mode, use HAL_SDADC_PollForInjectedConversion to detect the
  102. end of injected conversion.
  103. (#) In interrupt mode, HAL_SDADC_InjectedConvCpltCallback will be called
  104. at the end of injected conversion.
  105. (#) Get value of injected conversion and corresponding channel using
  106. HAL_SDADC_InjectedGetValue.
  107. (#) In DMA mode, HAL_SDADC_InjectedConvHalfCpltCallback and
  108. HAL_SDADC_InjectedConvCpltCallback will be called respectively at the
  109. half transfer and at the transfer complete.
  110. (#) Stop injected conversion using HAL_SDADC_InjectedStop,
  111. HAL_SDADC_InjectedStop_IT or HAL_SDADC_InjectedStop_DMA.
  112. *** Multi mode regular channels conversions ***
  113. ======================================================
  114. [..]
  115. (#) Select type of multimode (SDADC1/SDADC2 or SDADC1/SDADC3) using
  116. HAL_SDADC_MultiModeConfigChannel.
  117. (#) Select software trigger for SDADC1 and synchronized trigger for
  118. SDADC2 (or SDADC3) using HAL_SDADC_SelectRegularTrigger.
  119. (#) Select regular channel for SDADC1 and SDADC2 (or SDADC3) using
  120. HAL_SDADC_ConfigChannel.
  121. (#) Start regular conversion for SDADC2 (or SDADC3) with HAL_SDADC_Start.
  122. (#) Start regular conversion for SDADC1 using HAL_SDADC_Start,
  123. HAL_SDADC_Start_IT or HAL_SDADC_MultiModeStart_DMA.
  124. (#) In polling mode, use HAL_SDADC_PollForConversion to detect the end of
  125. regular conversion for SDADC1.
  126. (#) In interrupt mode, HAL_SDADC_ConvCpltCallback will be called at the
  127. end of regular conversion for SDADC1.
  128. (#) Get value of regular conversions using HAL_SDADC_MultiModeGetValue.
  129. (#) In DMA mode, HAL_SDADC_ConvHalfCpltCallback and
  130. HAL_SDADC_ConvCpltCallback will be called respectively at the half
  131. transfer and at the transfer complete for SDADC1.
  132. (#) Stop regular conversion using HAL_SDADC_Stop, HAL_SDADC_Stop_IT
  133. or HAL_SDADC_MultiModeStop_DMA for SDADC1.
  134. (#) Stop regular conversion using HAL_SDADC_Stop for SDADC2 (or SDADC3).
  135. *** Multi mode injected channels conversions ***
  136. ======================================================
  137. [..]
  138. (#) Select type of multimode (SDADC1/SDADC2 or SDADC1/SDADC3) using
  139. HAL_SDADC_InjectedMultiModeConfigChannel.
  140. (#) Select software or external trigger for SDADC1 and synchronized
  141. trigger for SDADC2 (or SDADC3) using HAL_SDADC_SelectInjectedTrigger.
  142. (#) Select injected channels for SDADC1 and SDADC2 (or SDADC3) using
  143. HAL_SDADC_InjectedConfigChannel.
  144. (#) Start injected conversion for SDADC2 (or SDADC3) with
  145. HAL_SDADC_InjectedStart.
  146. (#) Start injected conversion for SDADC1 using HAL_SDADC_InjectedStart,
  147. HAL_SDADC_InjectedStart_IT or HAL_SDADC_InjectedMultiModeStart_DMA.
  148. (#) In polling mode, use HAL_SDADC_InjectedPollForConversion to detect
  149. the end of injected conversion for SDADC1.
  150. (#) In interrupt mode, HAL_SDADC_InjectedConvCpltCallback will be called
  151. at the end of injected conversion for SDADC1.
  152. (#) Get value of injected conversions using
  153. HAL_SDADC_InjectedMultiModeGetValue.
  154. (#) In DMA mode, HAL_SDADC_InjectedConvHalfCpltCallback and
  155. HAL_SDADC_InjectedConvCpltCallback will be called respectively at the
  156. half transfer and at the transfer complete for SDADC1.
  157. (#) Stop injected conversion using HAL_SDADC_InjectedStop,
  158. HAL_SDADC_InjectedStop_IT or HAL_SDADC_InjecteddMultiModeStop_DMA
  159. for SDADC1.
  160. (#) Stop injected conversion using HAL_SDADC_InjectedStop for SDADC2
  161. (or SDADC3).
  162. *** Callback registration ***
  163. =============================================
  164. [..]
  165. The compilation flag USE_HAL_SDADC_REGISTER_CALLBACKS, when set to 1,
  166. allows the user to configure dynamically the driver callbacks.
  167. Use Functions HAL_SDADC_RegisterCallback()
  168. to register an interrupt callback.
  169. [..]
  170. Function HAL_SDADC_RegisterCallback() allows to register following callbacks:
  171. (+) ConvHalfCpltCallback : callback for half regular conversion complete.
  172. (+) ConvCpltCallback : callback for regular conversion complete
  173. (+) InjectedConvHalfCpltCallback : callback for half injected conversion complete
  174. (+) InjectedConvCpltCallback : callback for injected conversion complete
  175. (+) CalibrationCpltCallback : callback for calibration
  176. (+) ErrorCallback : callback for error detection.
  177. (+) MspInitCallback : callback for Msp Init.
  178. (+) MspDeInitCallback : callback for Msp DeInit.
  179. This function takes as parameters the HAL peripheral handle, the Callback ID
  180. and a pointer to the user callback function.
  181. [..]
  182. Use function HAL_SDADC_UnRegisterCallback to reset a callback to the default
  183. weak function.
  184. [..]
  185. HAL_SDADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
  186. and the Callback ID.
  187. This function allows to reset following callbacks:
  188. (+) ConvHalfCpltCallback : callback for half regular conversion complete.
  189. (+) ConvCpltCallback : callback for regular conversion complete
  190. (+) InjectedConvHalfCpltCallback : callback for half injected conversion complete
  191. (+) InjectedConvCpltCallback : callback for injected conversion complete
  192. (+) CalibrationCpltCallback : callback for calibration
  193. (+) ErrorCallback : callback for error detection.
  194. (+) MspInitCallback : callback for Msp Init.
  195. (+) MspDeInitCallback : callback for Msp DeInit.
  196. [..]
  197. By default, after the HAL_SDADC_Init() and when the state is HAL_SDADC_STATE_RESET
  198. all callbacks are set to the corresponding weak functions:
  199. examples HAL_SDADC_ConvCpltCallback(), HAL_SDADC_ErrorCallback().
  200. Exception done for MspInit and MspDeInit functions that are
  201. reset to the legacy weak functions in the HAL_SDADC_Init()/ HAL_SDADC_DeInit() only when
  202. these callbacks are null (not registered beforehand).
  203. [..]
  204. If MspInit or MspDeInit are not null, the HAL_SDADC_Init()/ HAL_SDADC_DeInit()
  205. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  206. [..]
  207. Callbacks can be registered/unregistered in HAL_SDADC_STATE_READY state only.
  208. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  209. in HAL_SDADC_STATE_READY or HAL_SDADC_STATE_RESET state,
  210. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  211. [..]
  212. Then, the user first registers the MspInit/MspDeInit user callbacks
  213. using HAL_SDADC_RegisterCallback() before calling HAL_SDADC_DeInit()
  214. or HAL_SDADC_Init() function.
  215. [..]
  216. When the compilation flag USE_HAL_SDADC_REGISTER_CALLBACKS is set to 0 or
  217. not defined, the callback registration feature is not available and all callbacks
  218. are set to the corresponding weak functions.
  219. @endverbatim
  220. */
  221. /* Includes ------------------------------------------------------------------*/
  222. #include "stm32f3xx_hal.h"
  223. /** @addtogroup STM32F3xx_HAL_Driver
  224. * @{
  225. */
  226. #ifdef HAL_SDADC_MODULE_ENABLED
  227. #if defined(SDADC1) || defined(SDADC2) || defined(SDADC3)
  228. /** @defgroup SDADC SDADC
  229. * @brief SDADC HAL driver modules
  230. * @{
  231. */
  232. /* Private typedef -----------------------------------------------------------*/
  233. /* Private define ------------------------------------------------------------*/
  234. /** @defgroup SDADC_Private_Define SDADC Private Define
  235. * @{
  236. */
  237. #define SDADC_TIMEOUT 200UL
  238. #define SDADC_CONFREG_OFFSET 0x00000020UL
  239. #define SDADC_JDATAR_CH_OFFSET 24UL
  240. #define SDADC_MSB_MASK 0xFFFF0000UL
  241. #define SDADC_LSB_MASK 0x0000FFFFUL
  242. /**
  243. * @}
  244. */
  245. /* Private macro -------------------------------------------------------------*/
  246. /* Private variables ---------------------------------------------------------*/
  247. /* Private function prototypes -----------------------------------------------*/
  248. /** @defgroup SDADC_Private_Functions SDADC Private Functions
  249. * @{
  250. */
  251. static HAL_StatusTypeDef SDADC_EnterInitMode(SDADC_HandleTypeDef* hsdadc);
  252. static void SDADC_ExitInitMode(SDADC_HandleTypeDef* hsdadc);
  253. static uint32_t SDADC_GetInjChannelsNbr(uint32_t Channels);
  254. static HAL_StatusTypeDef SDADC_RegConvStart(SDADC_HandleTypeDef* hsdadc);
  255. static HAL_StatusTypeDef SDADC_RegConvStop(SDADC_HandleTypeDef* hsdadc);
  256. static HAL_StatusTypeDef SDADC_InjConvStart(SDADC_HandleTypeDef* hsdadc);
  257. static HAL_StatusTypeDef SDADC_InjConvStop(SDADC_HandleTypeDef* hsdadc);
  258. static void SDADC_DMARegularHalfConvCplt(DMA_HandleTypeDef *hdma);
  259. static void SDADC_DMARegularConvCplt(DMA_HandleTypeDef *hdma);
  260. static void SDADC_DMAInjectedHalfConvCplt(DMA_HandleTypeDef *hdma);
  261. static void SDADC_DMAInjectedConvCplt(DMA_HandleTypeDef *hdma);
  262. static void SDADC_DMAError(DMA_HandleTypeDef *hdma);
  263. /**
  264. * @}
  265. */
  266. /* Exported functions ---------------------------------------------------------*/
  267. /** @defgroup SDADC_Exported_Functions SDADC Exported Functions
  268. * @{
  269. */
  270. /** @defgroup SDADC_Exported_Functions_Group1 Initialization and de-initialization functions
  271. * @brief Initialization and de-initialization functions
  272. *
  273. @verbatim
  274. ===============================================================================
  275. ##### Initialization and de-initialization functions #####
  276. ===============================================================================
  277. [..] This section provides functions allowing to:
  278. (+) Initialize the SDADC.
  279. (+) De-initialize the SDADC.
  280. @endverbatim
  281. * @{
  282. */
  283. /**
  284. * @brief Initializes the SDADC according to the specified
  285. * parameters in the SDADC_InitTypeDef structure.
  286. * @note If multiple SDADC are used, please configure first SDADC1 to set
  287. * the common reference voltage.
  288. * @param hsdadc SDADC handle.
  289. * @retval HAL status.
  290. */
  291. HAL_StatusTypeDef HAL_SDADC_Init(SDADC_HandleTypeDef* hsdadc)
  292. {
  293. uint32_t tickstart;
  294. /* Check SDADC handle */
  295. if(hsdadc == NULL)
  296. {
  297. return HAL_ERROR;
  298. }
  299. /* Check parameters */
  300. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  301. assert_param(IS_SDADC_LOWPOWER_MODE(hsdadc->Init.IdleLowPowerMode));
  302. assert_param(IS_SDADC_FAST_CONV_MODE(hsdadc->Init.FastConversionMode));
  303. assert_param(IS_SDADC_SLOW_CLOCK_MODE(hsdadc->Init.SlowClockMode));
  304. assert_param(IS_SDADC_VREF(hsdadc->Init.ReferenceVoltage));
  305. /* Initialize SDADC variables with default values */
  306. hsdadc->RegularContMode = SDADC_CONTINUOUS_CONV_OFF;
  307. hsdadc->InjectedContMode = SDADC_CONTINUOUS_CONV_OFF;
  308. hsdadc->InjectedChannelsNbr = 1U;
  309. hsdadc->InjConvRemaining = 1U;
  310. hsdadc->RegularTrigger = SDADC_SOFTWARE_TRIGGER;
  311. hsdadc->InjectedTrigger = SDADC_SOFTWARE_TRIGGER;
  312. hsdadc->ExtTriggerEdge = SDADC_EXT_TRIG_RISING_EDGE;
  313. hsdadc->RegularMultimode = SDADC_MULTIMODE_SDADC1_SDADC2;
  314. hsdadc->InjectedMultimode = SDADC_MULTIMODE_SDADC1_SDADC2;
  315. hsdadc->ErrorCode = SDADC_ERROR_NONE;
  316. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  317. if(hsdadc->State == HAL_SDADC_STATE_RESET)
  318. {
  319. /* Init the SDADC Callback settings */
  320. hsdadc->ConvHalfCpltCallback = HAL_SDADC_ConvHalfCpltCallback;
  321. hsdadc->ConvCpltCallback = HAL_SDADC_ConvCpltCallback;
  322. hsdadc->InjectedConvHalfCpltCallback = HAL_SDADC_InjectedConvHalfCpltCallback;
  323. hsdadc->InjectedConvCpltCallback = HAL_SDADC_InjectedConvCpltCallback;
  324. hsdadc->CalibrationCpltCallback = HAL_SDADC_CalibrationCpltCallback;
  325. hsdadc->ErrorCallback = HAL_SDADC_ErrorCallback;
  326. }
  327. if (hsdadc->MspInitCallback == NULL)
  328. {
  329. hsdadc->MspInitCallback = HAL_SDADC_MspInit; /* Legacy weak MspInit */
  330. }
  331. /* Init the low level hardware */
  332. hsdadc->MspInitCallback(hsdadc);
  333. #else
  334. /* Init the low level hardware */
  335. HAL_SDADC_MspInit(hsdadc);
  336. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  337. /* Set idle low power and slow clock modes */
  338. hsdadc->Instance->CR1 &= ~(SDADC_CR1_SBI|SDADC_CR1_PDI|SDADC_CR1_SLOWCK);
  339. hsdadc->Instance->CR1 |= (hsdadc->Init.IdleLowPowerMode | \
  340. hsdadc->Init.SlowClockMode);
  341. /* Set fast conversion mode */
  342. hsdadc->Instance->CR2 &= ~(SDADC_CR2_FAST);
  343. hsdadc->Instance->CR2 |= hsdadc->Init.FastConversionMode;
  344. /* Set reference voltage common to all SDADC instances */
  345. /* Update this parameter only if needed to avoid unnecessary settling time */
  346. if((SDADC1->CR1 & SDADC_CR1_REFV) != hsdadc->Init.ReferenceVoltage)
  347. {
  348. /* Voltage reference bits are common to all SADC instances but are */
  349. /* present in SDADC1 register. */
  350. SDADC1->CR1 &= ~(SDADC_CR1_REFV);
  351. SDADC1->CR1 |= hsdadc->Init.ReferenceVoltage;
  352. /* Wait at least 2ms before setting ADON */
  353. HAL_Delay(2U);
  354. }
  355. /* Enable SDADC */
  356. hsdadc->Instance->CR2 |= SDADC_CR2_ADON;
  357. /* Wait end of stabilization */
  358. tickstart = HAL_GetTick();
  359. while((hsdadc->Instance->ISR & SDADC_ISR_STABIP) != 0UL)
  360. {
  361. if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
  362. {
  363. return HAL_TIMEOUT;
  364. }
  365. }
  366. /* Set SDADC to ready state */
  367. hsdadc->State = HAL_SDADC_STATE_READY;
  368. /* Return HAL status */
  369. return HAL_OK;
  370. }
  371. /**
  372. * @brief De-initializes the SDADC.
  373. * @param hsdadc SDADC handle.
  374. * @retval HAL status.
  375. */
  376. HAL_StatusTypeDef HAL_SDADC_DeInit(SDADC_HandleTypeDef* hsdadc)
  377. {
  378. /* Check SDADC handle */
  379. if(hsdadc == NULL)
  380. {
  381. return HAL_ERROR;
  382. }
  383. /* Check parameters */
  384. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  385. /* Disable the SDADC */
  386. hsdadc->Instance->CR2 &= ~(SDADC_CR2_ADON);
  387. /* Reset all registers */
  388. hsdadc->Instance->CR1 = 0x00000000UL;
  389. hsdadc->Instance->CR2 = 0x00000000UL;
  390. hsdadc->Instance->JCHGR = 0x00000001UL;
  391. hsdadc->Instance->CONF0R = 0x00000000UL;
  392. hsdadc->Instance->CONF1R = 0x00000000UL;
  393. hsdadc->Instance->CONF2R = 0x00000000UL;
  394. hsdadc->Instance->CONFCHR1 = 0x00000000UL;
  395. hsdadc->Instance->CONFCHR2 = 0x00000000UL;
  396. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  397. if (hsdadc->MspDeInitCallback == NULL)
  398. {
  399. hsdadc->MspDeInitCallback = HAL_SDADC_MspDeInit; /* Legacy weak MspDeInit */
  400. }
  401. /* DeInit the low level hardware */
  402. hsdadc->MspDeInitCallback(hsdadc);
  403. #else
  404. /* DeInit the low level hardware */
  405. HAL_SDADC_MspDeInit(hsdadc);
  406. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  407. /* Set SDADC in reset state */
  408. hsdadc->State = HAL_SDADC_STATE_RESET;
  409. /* Return function status */
  410. return HAL_OK;
  411. }
  412. /**
  413. * @brief Initializes the SDADC MSP.
  414. * @param hsdadc SDADC handle
  415. * @retval None
  416. */
  417. __weak void HAL_SDADC_MspInit(SDADC_HandleTypeDef* hsdadc)
  418. {
  419. /* Prevent unused argument(s) compilation warning */
  420. UNUSED(hsdadc);
  421. /* NOTE : This function should not be modified, when the callback is needed,
  422. the HAL_SDADC_MspInit could be implemented in the user file.
  423. */
  424. }
  425. /**
  426. * @brief De-initializes the SDADC MSP.
  427. * @param hsdadc SDADC handle
  428. * @retval None
  429. */
  430. __weak void HAL_SDADC_MspDeInit(SDADC_HandleTypeDef* hsdadc)
  431. {
  432. /* Prevent unused argument(s) compilation warning */
  433. UNUSED(hsdadc);
  434. /* NOTE : This function should not be modified, when the callback is needed,
  435. the HAL_SDADC_MspDeInit could be implemented in the user file.
  436. */
  437. }
  438. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  439. /**
  440. * @brief Register a User SDADC Callback
  441. * To be used instead of the weak predefined callback
  442. * @param hsdadc Pointer to a SDADC_HandleTypeDef structure that contains
  443. * the configuration information for the specified SDADC.
  444. * @param CallbackID ID of the callback to be registered
  445. * This parameter can be one of the following values:
  446. * @arg @ref HAL_SDADC_CONVERSION_HALF_CB_ID SDADC half regular conversion complete callback ID
  447. * @arg @ref HAL_SDADC_CONVERSION_COMPLETE_CB_ID SDADC regular conversion complete callback ID
  448. * @arg @ref HAL_SDADC_INJ_CONVERSION_HALF_CB_ID SDADC half injected conversion complete callback ID
  449. * @arg @ref HAL_SDADC_INJ_CONVERSION_COMPLETE_CB_ID SDADC injected conversion complete callback ID
  450. * @arg @ref HAL_SDADC_CALIBRATION_COMPLETE_CB_ID SDADC calibration callback ID
  451. * @arg @ref HAL_SDADC_ERROR_CB_ID SDADC error callback ID
  452. * @arg @ref HAL_SDADC_MSPINIT_CB_ID SDADC Msp Init callback ID
  453. * @arg @ref HAL_SDADC_MSPDEINIT_CB_ID SDADC Msp DeInit callback ID
  454. * @param pCallback pointer to the Callback function
  455. * @retval HAL status
  456. */
  457. HAL_StatusTypeDef HAL_SDADC_RegisterCallback(SDADC_HandleTypeDef *hsdadc, HAL_SDADC_CallbackIDTypeDef CallbackID, pSDADC_CallbackTypeDef pCallback)
  458. {
  459. HAL_StatusTypeDef status = HAL_OK;
  460. if (pCallback == NULL)
  461. {
  462. /* Update the error code */
  463. hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
  464. return HAL_ERROR;
  465. }
  466. if (HAL_SDADC_STATE_READY == hsdadc->State)
  467. {
  468. switch (CallbackID)
  469. {
  470. case HAL_SDADC_CONVERSION_HALF_CB_ID :
  471. hsdadc->ConvHalfCpltCallback = pCallback;
  472. break;
  473. case HAL_SDADC_CONVERSION_COMPLETE_CB_ID :
  474. hsdadc->ConvCpltCallback = pCallback;
  475. break;
  476. case HAL_SDADC_INJ_CONVERSION_HALF_CB_ID :
  477. hsdadc->InjectedConvHalfCpltCallback = pCallback;
  478. break;
  479. case HAL_SDADC_INJ_CONVERSION_COMPLETE_CB_ID :
  480. hsdadc->InjectedConvCpltCallback = pCallback;
  481. break;
  482. case HAL_SDADC_CALIBRATION_COMPLETE_CB_ID :
  483. hsdadc->CalibrationCpltCallback = pCallback;
  484. break;
  485. case HAL_SDADC_ERROR_CB_ID :
  486. hsdadc->ErrorCallback = pCallback;
  487. break;
  488. case HAL_SDADC_MSPINIT_CB_ID :
  489. hsdadc->MspInitCallback = pCallback;
  490. break;
  491. case HAL_SDADC_MSPDEINIT_CB_ID :
  492. hsdadc->MspDeInitCallback = pCallback;
  493. break;
  494. default :
  495. /* Update the error code */
  496. hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
  497. /* Return error status */
  498. status = HAL_ERROR;
  499. break;
  500. }
  501. }
  502. else if (HAL_SDADC_STATE_RESET == hsdadc->State)
  503. {
  504. switch (CallbackID)
  505. {
  506. case HAL_SDADC_MSPINIT_CB_ID :
  507. hsdadc->MspInitCallback = pCallback;
  508. break;
  509. case HAL_SDADC_MSPDEINIT_CB_ID :
  510. hsdadc->MspDeInitCallback = pCallback;
  511. break;
  512. default :
  513. /* Update the error code */
  514. hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
  515. /* Return error status */
  516. status = HAL_ERROR;
  517. break;
  518. }
  519. }
  520. else
  521. {
  522. /* Update the error code */
  523. hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
  524. /* Return error status */
  525. status = HAL_ERROR;
  526. }
  527. return status;
  528. }
  529. /**
  530. * @brief Unregister a SDADC Callback
  531. * ADC callback is redirected to the weak predefined callback
  532. * @param hsdadc Pointer to a SDADC_HandleTypeDef structure that contains
  533. * the configuration information for the specified ADC.
  534. * @param CallbackID ID of the callback to be unregistered
  535. * This parameter can be one of the following values:
  536. * @arg @ref HAL_SDADC_CONVERSION_HALF_CB_ID SDADC half regular conversion complete callback ID
  537. * @arg @ref HAL_SDADC_CONVERSION_COMPLETE_CB_ID SDADC regular conversion complete callback ID
  538. * @arg @ref HAL_SDADC_INJ_CONVERSION_HALF_CB_ID SDADC half injected conversion complete callback ID
  539. * @arg @ref HAL_SDADC_INJ_CONVERSION_COMPLETE_CB_ID SDADC injected conversion complete callback ID
  540. * @arg @ref HAL_SDADC_CALIBRATION_COMPLETE_CB_ID SDADC calibration callback ID
  541. * @arg @ref HAL_SDADC_ERROR_CB_ID SDADC error callback ID
  542. * @arg @ref HAL_SDADC_MSPINIT_CB_ID SDADC Msp Init callback ID
  543. * @arg @ref HAL_SDADC_MSPDEINIT_CB_ID SDADC Msp DeInit callback ID
  544. * @retval HAL status
  545. */
  546. HAL_StatusTypeDef HAL_SDADC_UnRegisterCallback(SDADC_HandleTypeDef *hsdadc, HAL_SDADC_CallbackIDTypeDef CallbackID)
  547. {
  548. HAL_StatusTypeDef status = HAL_OK;
  549. if (HAL_SDADC_STATE_READY == hsdadc->State)
  550. {
  551. switch (CallbackID)
  552. {
  553. case HAL_SDADC_CONVERSION_HALF_CB_ID :
  554. hsdadc->ConvHalfCpltCallback = HAL_SDADC_ConvHalfCpltCallback;
  555. break;
  556. case HAL_SDADC_CONVERSION_COMPLETE_CB_ID :
  557. hsdadc->ConvCpltCallback = HAL_SDADC_ConvCpltCallback;
  558. break;
  559. case HAL_SDADC_INJ_CONVERSION_HALF_CB_ID :
  560. hsdadc->InjectedConvHalfCpltCallback = HAL_SDADC_InjectedConvHalfCpltCallback;
  561. break;
  562. case HAL_SDADC_INJ_CONVERSION_COMPLETE_CB_ID :
  563. hsdadc->InjectedConvCpltCallback = HAL_SDADC_InjectedConvCpltCallback;
  564. break;
  565. case HAL_SDADC_CALIBRATION_COMPLETE_CB_ID :
  566. hsdadc->CalibrationCpltCallback = HAL_SDADC_CalibrationCpltCallback;
  567. break;
  568. case HAL_SDADC_ERROR_CB_ID :
  569. hsdadc->ErrorCallback = HAL_SDADC_ErrorCallback;
  570. break;
  571. case HAL_SDADC_MSPINIT_CB_ID :
  572. hsdadc->MspInitCallback = HAL_SDADC_MspInit;
  573. break;
  574. case HAL_SDADC_MSPDEINIT_CB_ID :
  575. hsdadc->MspDeInitCallback = HAL_SDADC_MspDeInit;
  576. break;
  577. default :
  578. /* Update the error code */
  579. hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
  580. /* Return error status */
  581. status = HAL_ERROR;
  582. break;
  583. }
  584. }
  585. else if (HAL_SDADC_STATE_RESET == hsdadc->State)
  586. {
  587. switch (CallbackID)
  588. {
  589. case HAL_SDADC_MSPINIT_CB_ID :
  590. hsdadc->MspInitCallback = HAL_SDADC_MspInit; /* Legacy weak MspInit */
  591. break;
  592. case HAL_SDADC_MSPDEINIT_CB_ID :
  593. hsdadc->MspDeInitCallback = HAL_SDADC_MspDeInit; /* Legacy weak MspDeInit */
  594. break;
  595. default :
  596. /* Update the error code */
  597. hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
  598. /* Return error status */
  599. status = HAL_ERROR;
  600. break;
  601. }
  602. }
  603. else
  604. {
  605. /* Update the error code */
  606. hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
  607. /* Return error status */
  608. status = HAL_ERROR;
  609. }
  610. return status;
  611. }
  612. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  613. /**
  614. * @}
  615. */
  616. /** @defgroup SDADC_Exported_Functions_Group2 peripheral control functions
  617. * @brief Peripheral control functions
  618. *
  619. @verbatim
  620. ===============================================================================
  621. ##### Peripheral control functions #####
  622. ===============================================================================
  623. [..] This section provides functions allowing to:
  624. (+) Program one of the three different configurations for channels.
  625. (+) Associate channel to one of configurations.
  626. (+) Select regular and injected channels.
  627. (+) Enable/disable continuous mode for regular and injected conversions.
  628. (+) Select regular and injected triggers.
  629. (+) Select and configure injected external trigger.
  630. (+) Enable/disable delay addition for injected conversions.
  631. (+) Configure multimode.
  632. @endverbatim
  633. * @{
  634. */
  635. /**
  636. * @brief This function allows the user to set parameters for a configuration.
  637. * Parameters are input mode, common mode, gain and offset.
  638. * @note This function should be called only when SDADC instance is in idle state
  639. * (neither calibration nor regular or injected conversion ongoing)
  640. * @param hsdadc SDADC handle.
  641. * @param ConfIndex Index of configuration to modify.
  642. * This parameter can be a value of @ref SDADC_ConfIndex.
  643. * @param ConfParamStruct Parameters to apply for this configuration.
  644. * @retval HAL status
  645. */
  646. HAL_StatusTypeDef HAL_SDADC_PrepareChannelConfig(SDADC_HandleTypeDef *hsdadc,
  647. uint32_t ConfIndex,
  648. SDADC_ConfParamTypeDef* ConfParamStruct)
  649. {
  650. HAL_StatusTypeDef status = HAL_OK;
  651. uint32_t tmp;
  652. /* Check parameters */
  653. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  654. assert_param(IS_SDADC_CONF_INDEX(ConfIndex));
  655. assert_param(ConfParamStruct != ((void*) 0));
  656. assert_param(IS_SDADC_INPUT_MODE(ConfParamStruct->InputMode));
  657. assert_param(IS_SDADC_GAIN(ConfParamStruct->Gain));
  658. assert_param(IS_SDADC_COMMON_MODE(ConfParamStruct->CommonMode));
  659. assert_param(IS_SDADC_OFFSET_VALUE(ConfParamStruct->Offset));
  660. /* Check SDADC state is ready */
  661. if(hsdadc->State != HAL_SDADC_STATE_READY)
  662. {
  663. status = HAL_ERROR;
  664. }
  665. else
  666. {
  667. /* Enter init mode */
  668. if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
  669. {
  670. /* Set SDADC in error state */
  671. hsdadc->State = HAL_SDADC_STATE_ERROR;
  672. status = HAL_TIMEOUT;
  673. }
  674. else
  675. {
  676. /* Program configuration register with parameters */
  677. tmp = (uint32_t)((uint32_t)(hsdadc->Instance) + \
  678. SDADC_CONFREG_OFFSET + \
  679. (uint32_t)(ConfIndex << 2UL));
  680. *(__IO uint32_t *) (tmp) = (uint32_t) (ConfParamStruct->InputMode | \
  681. ConfParamStruct->Gain | \
  682. ConfParamStruct->CommonMode | \
  683. ConfParamStruct->Offset);
  684. /* Exit init mode */
  685. SDADC_ExitInitMode(hsdadc);
  686. }
  687. }
  688. /* Return function status */
  689. return status;
  690. }
  691. /**
  692. * @brief This function allows the user to associate a channel with one of the
  693. * available configurations.
  694. * @note This function should be called only when SDADC instance is in idle state
  695. * (neither calibration nor regular or injected conversion ongoing)
  696. * @param hsdadc SDADC handle.
  697. * @param Channel Channel to associate with configuration.
  698. * This parameter can be a value of @ref SDADC_Channel_Selection.
  699. * @param ConfIndex Index of configuration to associate with channel.
  700. * This parameter can be a value of @ref SDADC_ConfIndex.
  701. * @retval HAL status
  702. */
  703. HAL_StatusTypeDef HAL_SDADC_AssociateChannelConfig(SDADC_HandleTypeDef *hsdadc,
  704. uint32_t Channel,
  705. uint32_t ConfIndex)
  706. {
  707. HAL_StatusTypeDef status = HAL_OK;
  708. uint32_t channelnum;
  709. /* Check parameters */
  710. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  711. assert_param(IS_SDADC_REGULAR_CHANNEL(Channel));
  712. assert_param(IS_SDADC_CONF_INDEX(ConfIndex));
  713. /* Check SDADC state is ready */
  714. if(hsdadc->State != HAL_SDADC_STATE_READY)
  715. {
  716. status = HAL_ERROR;
  717. }
  718. else
  719. {
  720. /* Enter init mode */
  721. if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
  722. {
  723. /* Set SDADC in error state */
  724. hsdadc->State = HAL_SDADC_STATE_ERROR;
  725. status = HAL_TIMEOUT;
  726. }
  727. else
  728. {
  729. /* Program channel configuration register according parameters */
  730. if(Channel != SDADC_CHANNEL_8)
  731. {
  732. /* Get channel number */
  733. channelnum = (uint32_t)(Channel>>16UL);
  734. /* Set the channel configuration */
  735. hsdadc->Instance->CONFCHR1 &= (uint32_t) ~((uint32_t)SDADC_CONFCHR1_CONFCH0 << ((channelnum << 2UL) & 0x1FUL));
  736. hsdadc->Instance->CONFCHR1 |= (uint32_t) (ConfIndex << ((channelnum << 2UL) & 0x1FUL));
  737. }
  738. else
  739. {
  740. hsdadc->Instance->CONFCHR2 = (uint32_t) (ConfIndex);
  741. }
  742. /* Exit init mode */
  743. SDADC_ExitInitMode(hsdadc);
  744. }
  745. }
  746. /* Return function status */
  747. return status;
  748. }
  749. /**
  750. * @brief This function allows to select channel for regular conversion and
  751. * to enable/disable continuous mode for regular conversion.
  752. * @param hsdadc SDADC handle.
  753. * @param Channel Channel for regular conversion.
  754. * This parameter can be a value of @ref SDADC_Channel_Selection.
  755. * @param ContinuousMode Enable/disable continuous mode for regular conversion.
  756. * This parameter can be a value of @ref SDADC_ContinuousMode.
  757. * @retval HAL status
  758. */
  759. HAL_StatusTypeDef HAL_SDADC_ConfigChannel(SDADC_HandleTypeDef *hsdadc,
  760. uint32_t Channel,
  761. uint32_t ContinuousMode)
  762. {
  763. HAL_StatusTypeDef status = HAL_OK;
  764. /* Check parameters */
  765. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  766. assert_param(IS_SDADC_REGULAR_CHANNEL(Channel));
  767. assert_param(IS_SDADC_CONTINUOUS_MODE(ContinuousMode));
  768. /* Check SDADC state */
  769. if((hsdadc->State != HAL_SDADC_STATE_RESET) && (hsdadc->State != HAL_SDADC_STATE_ERROR))
  770. {
  771. /* Set RCH[3:0] and RCONT bits in SDADC_CR2 */
  772. hsdadc->Instance->CR2 &= (uint32_t) ~(SDADC_CR2_RCH | SDADC_CR2_RCONT);
  773. if(ContinuousMode == SDADC_CONTINUOUS_CONV_ON)
  774. {
  775. hsdadc->Instance->CR2 |= (uint32_t) ((Channel & SDADC_MSB_MASK) | SDADC_CR2_RCONT);
  776. }
  777. else
  778. {
  779. hsdadc->Instance->CR2 |= (uint32_t) ((Channel & SDADC_MSB_MASK));
  780. }
  781. /* Store continuous mode information */
  782. hsdadc->RegularContMode = ContinuousMode;
  783. }
  784. else
  785. {
  786. status = HAL_ERROR;
  787. }
  788. /* Return function status */
  789. return status;
  790. }
  791. /**
  792. * @brief This function allows to select channels for injected conversion and
  793. * to enable/disable continuous mode for injected conversion.
  794. * @param hsdadc SDADC handle.
  795. * @param Channel Channels for injected conversion.
  796. * This parameter can be a values combination of @ref SDADC_Channel_Selection.
  797. * @param ContinuousMode Enable/disable continuous mode for injected conversion.
  798. * This parameter can be a value of @ref SDADC_ContinuousMode.
  799. * @retval HAL status
  800. */
  801. HAL_StatusTypeDef HAL_SDADC_InjectedConfigChannel(SDADC_HandleTypeDef *hsdadc,
  802. uint32_t Channel,
  803. uint32_t ContinuousMode)
  804. {
  805. HAL_StatusTypeDef status = HAL_OK;
  806. /* Check parameters */
  807. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  808. assert_param(IS_SDADC_INJECTED_CHANNEL(Channel));
  809. assert_param(IS_SDADC_CONTINUOUS_MODE(ContinuousMode));
  810. /* Check SDADC state */
  811. if((hsdadc->State != HAL_SDADC_STATE_RESET) && (hsdadc->State != HAL_SDADC_STATE_ERROR))
  812. {
  813. /* Set JCHG[8:0] bits in SDADC_JCHG */
  814. hsdadc->Instance->JCHGR = (uint32_t) (Channel & SDADC_LSB_MASK);
  815. /* Set or clear JCONT bit in SDADC_CR2 */
  816. if(ContinuousMode == SDADC_CONTINUOUS_CONV_ON)
  817. {
  818. hsdadc->Instance->CR2 |= SDADC_CR2_JCONT;
  819. }
  820. else
  821. {
  822. hsdadc->Instance->CR2 &= ~(SDADC_CR2_JCONT);
  823. }
  824. /* Store continuous mode information */
  825. hsdadc->InjectedContMode = ContinuousMode;
  826. /* Store number of injected channels */
  827. hsdadc->InjectedChannelsNbr = SDADC_GetInjChannelsNbr(Channel);
  828. }
  829. else
  830. {
  831. status = HAL_ERROR;
  832. }
  833. /* Return function status */
  834. return status;
  835. }
  836. /**
  837. * @brief This function allows to select trigger for regular conversions.
  838. * @note This function should not be called if regular conversion is ongoing.
  839. * @param hsdadc SDADC handle.
  840. * @param Trigger Trigger for regular conversions.
  841. * This parameter can be one of the following value :
  842. * @arg SDADC_SOFTWARE_TRIGGER : Software trigger.
  843. * @arg SDADC_SYNCHRONOUS_TRIGGER : Synchronous with SDADC1 (only for SDADC2 and SDADC3).
  844. * @retval HAL status
  845. */
  846. HAL_StatusTypeDef HAL_SDADC_SelectRegularTrigger(SDADC_HandleTypeDef *hsdadc, uint32_t Trigger)
  847. {
  848. HAL_StatusTypeDef status = HAL_OK;
  849. /* Check parameters */
  850. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  851. assert_param(IS_SDADC_REGULAR_TRIGGER(Trigger));
  852. /* Check parameters compatibility */
  853. if((hsdadc->Instance == SDADC1) && (Trigger == SDADC_SYNCHRONOUS_TRIGGER))
  854. {
  855. status = HAL_ERROR;
  856. }
  857. /* Check SDADC state */
  858. else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  859. (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
  860. (hsdadc->State == HAL_SDADC_STATE_INJ))
  861. {
  862. /* Store regular trigger information */
  863. hsdadc->RegularTrigger = Trigger;
  864. }
  865. else
  866. {
  867. status = HAL_ERROR;
  868. }
  869. /* Return function status */
  870. return status;
  871. }
  872. /**
  873. * @brief This function allows to select trigger for injected conversions.
  874. * @note This function should not be called if injected conversion is ongoing.
  875. * @param hsdadc SDADC handle.
  876. * @param Trigger Trigger for injected conversions.
  877. * This parameter can be one of the following value :
  878. * @arg SDADC_SOFTWARE_TRIGGER : Software trigger.
  879. * @arg SDADC_SYNCHRONOUS_TRIGGER : Synchronous with SDADC1 (only for SDADC2 and SDADC3).
  880. * @arg SDADC_EXTERNAL_TRIGGER : External trigger.
  881. * @retval HAL status
  882. */
  883. HAL_StatusTypeDef HAL_SDADC_SelectInjectedTrigger(SDADC_HandleTypeDef *hsdadc, uint32_t Trigger)
  884. {
  885. HAL_StatusTypeDef status = HAL_OK;
  886. /* Check parameters */
  887. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  888. assert_param(IS_SDADC_INJECTED_TRIGGER(Trigger));
  889. /* Check parameters compatibility */
  890. if((hsdadc->Instance == SDADC1) && (Trigger == SDADC_SYNCHRONOUS_TRIGGER))
  891. {
  892. status = HAL_ERROR;
  893. }
  894. /* Check SDADC state */
  895. else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  896. (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
  897. (hsdadc->State == HAL_SDADC_STATE_REG))
  898. {
  899. /* Store regular trigger information */
  900. hsdadc->InjectedTrigger = Trigger;
  901. }
  902. else
  903. {
  904. status = HAL_ERROR;
  905. }
  906. /* Return function status */
  907. return status;
  908. }
  909. /**
  910. * @brief This function allows to select and configure injected external trigger.
  911. * @note This function should be called only when SDADC instance is in idle state
  912. * (neither calibration nor regular or injected conversion ongoing)
  913. * @param hsdadc SDADC handle.
  914. * @param InjectedExtTrigger External trigger for injected conversions.
  915. * This parameter can be a value of @ref SDADC_InjectedExtTrigger.
  916. * @param ExtTriggerEdge Edge of external injected trigger.
  917. * This parameter can be a value of @ref SDADC_ExtTriggerEdge.
  918. * @retval HAL status
  919. */
  920. HAL_StatusTypeDef HAL_SDADC_SelectInjectedExtTrigger(SDADC_HandleTypeDef *hsdadc,
  921. uint32_t InjectedExtTrigger,
  922. uint32_t ExtTriggerEdge)
  923. {
  924. HAL_StatusTypeDef status = HAL_OK;
  925. /* Check parameters */
  926. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  927. assert_param(IS_SDADC_EXT_INJEC_TRIG(InjectedExtTrigger));
  928. assert_param(IS_SDADC_EXT_TRIG_EDGE(ExtTriggerEdge));
  929. /* Check SDADC state */
  930. if(hsdadc->State == HAL_SDADC_STATE_READY)
  931. {
  932. /* Enter init mode */
  933. if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
  934. {
  935. /* Set SDADC in error state */
  936. hsdadc->State = HAL_SDADC_STATE_ERROR;
  937. status = HAL_TIMEOUT;
  938. }
  939. else
  940. {
  941. /* Set JEXTSEL[2:0] bits in SDADC_CR2 register */
  942. hsdadc->Instance->CR2 &= ~(SDADC_CR2_JEXTSEL);
  943. hsdadc->Instance->CR2 |= InjectedExtTrigger;
  944. /* Store external trigger edge information */
  945. hsdadc->ExtTriggerEdge = ExtTriggerEdge;
  946. /* Exit init mode */
  947. SDADC_ExitInitMode(hsdadc);
  948. }
  949. }
  950. else
  951. {
  952. status = HAL_ERROR;
  953. }
  954. /* Return function status */
  955. return status;
  956. }
  957. /**
  958. * @brief This function allows to enable/disable delay addition for injected conversions.
  959. * @note This function should be called only when SDADC instance is in idle state
  960. * (neither calibration nor regular or injected conversion ongoing)
  961. * @param hsdadc SDADC handle.
  962. * @param InjectedDelay Enable/disable delay for injected conversions.
  963. * This parameter can be a value of @ref SDADC_InjectedDelay.
  964. * @retval HAL status
  965. */
  966. HAL_StatusTypeDef HAL_SDADC_SelectInjectedDelay(SDADC_HandleTypeDef *hsdadc,
  967. uint32_t InjectedDelay)
  968. {
  969. HAL_StatusTypeDef status = HAL_OK;
  970. /* Check parameters */
  971. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  972. assert_param(IS_SDADC_INJECTED_DELAY(InjectedDelay));
  973. /* Check SDADC state */
  974. if(hsdadc->State == HAL_SDADC_STATE_READY)
  975. {
  976. /* Enter init mode */
  977. if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
  978. {
  979. /* Set SDADC in error state */
  980. hsdadc->State = HAL_SDADC_STATE_ERROR;
  981. status = HAL_TIMEOUT;
  982. }
  983. else
  984. {
  985. /* Set JDS bit in SDADC_CR2 register */
  986. hsdadc->Instance->CR2 &= ~(SDADC_CR2_JDS);
  987. hsdadc->Instance->CR2 |= InjectedDelay;
  988. /* Exit init mode */
  989. SDADC_ExitInitMode(hsdadc);
  990. }
  991. }
  992. else
  993. {
  994. status = HAL_ERROR;
  995. }
  996. /* Return function status */
  997. return status;
  998. }
  999. /**
  1000. * @brief This function allows to configure multimode for regular conversions.
  1001. * @note This function should not be called if regular conversion is ongoing
  1002. * and should be could only for SDADC1.
  1003. * @param hsdadc SDADC handle.
  1004. * @param MultimodeType Type of multimode for regular conversions.
  1005. * This parameter can be a value of @ref SDADC_MultimodeType.
  1006. * @retval HAL status
  1007. */
  1008. HAL_StatusTypeDef HAL_SDADC_MultiModeConfigChannel(SDADC_HandleTypeDef* hsdadc,
  1009. uint32_t MultimodeType)
  1010. {
  1011. HAL_StatusTypeDef status = HAL_OK;
  1012. /* Check parameters */
  1013. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1014. assert_param(IS_SDADC_MULTIMODE_TYPE(MultimodeType));
  1015. /* Check instance is SDADC1 */
  1016. if(hsdadc->Instance != SDADC1)
  1017. {
  1018. status = HAL_ERROR;
  1019. }
  1020. /* Check SDADC state */
  1021. else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  1022. (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
  1023. (hsdadc->State == HAL_SDADC_STATE_INJ))
  1024. {
  1025. /* Store regular trigger information */
  1026. hsdadc->RegularMultimode = MultimodeType;
  1027. }
  1028. else
  1029. {
  1030. status = HAL_ERROR;
  1031. }
  1032. /* Return function status */
  1033. return status;
  1034. }
  1035. /**
  1036. * @brief This function allows to configure multimode for injected conversions.
  1037. * @note This function should not be called if injected conversion is ongoing
  1038. * and should be could only for SDADC1.
  1039. * @param hsdadc SDADC handle.
  1040. * @param MultimodeType Type of multimode for injected conversions.
  1041. * This parameter can be a value of @ref SDADC_MultimodeType.
  1042. * @retval HAL status
  1043. */
  1044. HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeConfigChannel(SDADC_HandleTypeDef* hsdadc,
  1045. uint32_t MultimodeType)
  1046. {
  1047. HAL_StatusTypeDef status = HAL_OK;
  1048. /* Check parameters */
  1049. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1050. assert_param(IS_SDADC_MULTIMODE_TYPE(MultimodeType));
  1051. /* Check instance is SDADC1 */
  1052. if(hsdadc->Instance != SDADC1)
  1053. {
  1054. status = HAL_ERROR;
  1055. }
  1056. /* Check SDADC state */
  1057. else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  1058. (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
  1059. (hsdadc->State == HAL_SDADC_STATE_REG))
  1060. {
  1061. /* Store regular trigger information */
  1062. hsdadc->InjectedMultimode = MultimodeType;
  1063. }
  1064. else
  1065. {
  1066. status = HAL_ERROR;
  1067. }
  1068. /* Return function status */
  1069. return status;
  1070. }
  1071. /**
  1072. * @}
  1073. */
  1074. /** @defgroup SDADC_Exported_Functions_Group3 Input and Output operation functions
  1075. * @brief IO operation Control functions
  1076. *
  1077. @verbatim
  1078. ===============================================================================
  1079. ##### IO operation functions #####
  1080. ===============================================================================
  1081. [..] This section provides functions allowing to:
  1082. (+) Start calibration.
  1083. (+) Poll for the end of calibration.
  1084. (+) Start calibration and enable interrupt.
  1085. (+) Start conversion of regular/injected channel.
  1086. (+) Poll for the end of regular/injected conversion.
  1087. (+) Stop conversion of regular/injected channel.
  1088. (+) Start conversion of regular/injected channel and enable interrupt.
  1089. (+) Stop conversion of regular/injected channel and disable interrupt.
  1090. (+) Start conversion of regular/injected channel and enable DMA transfer.
  1091. (+) Stop conversion of regular/injected channel and disable DMA transfer.
  1092. (+) Start multimode and enable DMA transfer for regular/injected conversion.
  1093. (+) Stop multimode and disable DMA transfer for regular/injected conversion..
  1094. (+) Get result of regular channel conversion.
  1095. (+) Get result of injected channel conversion.
  1096. (+) Get result of multimode conversion.
  1097. (+) Handle SDADC interrupt request.
  1098. (+) Callbacks for calibration and regular/injected conversions.
  1099. @endverbatim
  1100. * @{
  1101. */
  1102. /**
  1103. * @brief This function allows to start calibration in polling mode.
  1104. * @note This function should be called only when SDADC instance is in idle state
  1105. * (neither calibration nor regular or injected conversion ongoing).
  1106. * @param hsdadc SDADC handle.
  1107. * @param CalibrationSequence Calibration sequence.
  1108. * This parameter can be a value of @ref SDADC_CalibrationSequence.
  1109. * @retval HAL status
  1110. */
  1111. HAL_StatusTypeDef HAL_SDADC_CalibrationStart(SDADC_HandleTypeDef *hsdadc,
  1112. uint32_t CalibrationSequence)
  1113. {
  1114. HAL_StatusTypeDef status = HAL_OK;
  1115. /* Check parameters */
  1116. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1117. assert_param(IS_SDADC_CALIB_SEQUENCE(CalibrationSequence));
  1118. /* Check SDADC state */
  1119. if(hsdadc->State == HAL_SDADC_STATE_READY)
  1120. {
  1121. /* Enter init mode */
  1122. if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
  1123. {
  1124. /* Set SDADC in error state */
  1125. hsdadc->State = HAL_SDADC_STATE_ERROR;
  1126. status = HAL_TIMEOUT;
  1127. }
  1128. else
  1129. {
  1130. /* Set CALIBCNT[1:0] bits in SDADC_CR2 register */
  1131. hsdadc->Instance->CR2 &= ~(SDADC_CR2_CALIBCNT);
  1132. hsdadc->Instance->CR2 |= CalibrationSequence;
  1133. /* Exit init mode */
  1134. SDADC_ExitInitMode(hsdadc);
  1135. /* Set STARTCALIB in SDADC_CR2 */
  1136. hsdadc->Instance->CR2 |= SDADC_CR2_STARTCALIB;
  1137. /* Set SDADC in calibration state */
  1138. hsdadc->State = HAL_SDADC_STATE_CALIB;
  1139. }
  1140. }
  1141. else
  1142. {
  1143. status = HAL_ERROR;
  1144. }
  1145. /* Return function status */
  1146. return status;
  1147. }
  1148. /**
  1149. * @brief This function allows to poll for the end of calibration.
  1150. * @note This function should be called only if calibration is ongoing.
  1151. * @param hsdadc SDADC handle.
  1152. * @param Timeout Timeout value in milliseconds.
  1153. * @retval HAL status
  1154. */
  1155. HAL_StatusTypeDef HAL_SDADC_PollForCalibEvent(SDADC_HandleTypeDef* hsdadc, uint32_t Timeout)
  1156. {
  1157. uint32_t tickstart;
  1158. /* Check parameters */
  1159. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1160. /* Check SDADC state */
  1161. if(hsdadc->State != HAL_SDADC_STATE_CALIB)
  1162. {
  1163. /* Return error status */
  1164. return HAL_ERROR;
  1165. }
  1166. else
  1167. {
  1168. /* Get timeout */
  1169. tickstart = HAL_GetTick();
  1170. /* Wait EOCALF bit in SDADC_ISR register */
  1171. while((hsdadc->Instance->ISR & SDADC_ISR_EOCALF) != SDADC_ISR_EOCALF)
  1172. {
  1173. /* Check the Timeout */
  1174. if(Timeout != HAL_MAX_DELAY)
  1175. {
  1176. if(((HAL_GetTick()-tickstart) > Timeout) || (Timeout == 0UL))
  1177. {
  1178. /* Return timeout status */
  1179. return HAL_TIMEOUT;
  1180. }
  1181. }
  1182. }
  1183. /* Set CLREOCALF bit in SDADC_CLRISR register */
  1184. hsdadc->Instance->CLRISR |= SDADC_ISR_CLREOCALF;
  1185. /* Set SDADC in ready state */
  1186. hsdadc->State = HAL_SDADC_STATE_READY;
  1187. /* Return function status */
  1188. return HAL_OK;
  1189. }
  1190. }
  1191. /**
  1192. * @brief This function allows to start calibration in interrupt mode.
  1193. * @note This function should be called only when SDADC instance is in idle state
  1194. * (neither calibration nor regular or injected conversion ongoing).
  1195. * @param hsdadc SDADC handle.
  1196. * @param CalibrationSequence Calibration sequence.
  1197. * This parameter can be a value of @ref SDADC_CalibrationSequence.
  1198. * @retval HAL status
  1199. */
  1200. HAL_StatusTypeDef HAL_SDADC_CalibrationStart_IT(SDADC_HandleTypeDef *hsdadc,
  1201. uint32_t CalibrationSequence)
  1202. {
  1203. HAL_StatusTypeDef status = HAL_OK;
  1204. /* Check parameters */
  1205. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1206. assert_param(IS_SDADC_CALIB_SEQUENCE(CalibrationSequence));
  1207. /* Check SDADC state */
  1208. if(hsdadc->State == HAL_SDADC_STATE_READY)
  1209. {
  1210. /* Enter init mode */
  1211. if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
  1212. {
  1213. /* Set SDADC in error state */
  1214. hsdadc->State = HAL_SDADC_STATE_ERROR;
  1215. status = HAL_TIMEOUT;
  1216. }
  1217. else
  1218. {
  1219. /* Set CALIBCNT[1:0] bits in SDADC_CR2 register */
  1220. hsdadc->Instance->CR2 &= ~(SDADC_CR2_CALIBCNT);
  1221. hsdadc->Instance->CR2 |= CalibrationSequence;
  1222. /* Exit init mode */
  1223. SDADC_ExitInitMode(hsdadc);
  1224. /* Set EOCALIE bit in SDADC_CR1 register */
  1225. hsdadc->Instance->CR1 |= SDADC_CR1_EOCALIE;
  1226. /* Set STARTCALIB in SDADC_CR2 */
  1227. hsdadc->Instance->CR2 |= SDADC_CR2_STARTCALIB;
  1228. /* Set SDADC in calibration state */
  1229. hsdadc->State = HAL_SDADC_STATE_CALIB;
  1230. }
  1231. }
  1232. else
  1233. {
  1234. status = HAL_ERROR;
  1235. }
  1236. /* Return function status */
  1237. return status;
  1238. }
  1239. /**
  1240. * @brief This function allows to start regular conversion in polling mode.
  1241. * @note This function should be called only when SDADC instance is in idle state
  1242. * or if injected conversion is ongoing.
  1243. * @param hsdadc SDADC handle.
  1244. * @retval HAL status
  1245. */
  1246. HAL_StatusTypeDef HAL_SDADC_Start(SDADC_HandleTypeDef *hsdadc)
  1247. {
  1248. HAL_StatusTypeDef status;
  1249. /* Check parameters */
  1250. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1251. /* Check SDADC state */
  1252. if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  1253. (hsdadc->State == HAL_SDADC_STATE_INJ))
  1254. {
  1255. /* Start regular conversion */
  1256. status = SDADC_RegConvStart(hsdadc);
  1257. }
  1258. else
  1259. {
  1260. status = HAL_ERROR;
  1261. }
  1262. /* Return function status */
  1263. return status;
  1264. }
  1265. /**
  1266. * @brief This function allows to poll for the end of regular conversion.
  1267. * @note This function should be called only if regular conversion is ongoing.
  1268. * @param hsdadc SDADC handle.
  1269. * @param Timeout Timeout value in milliseconds.
  1270. * @retval HAL status
  1271. */
  1272. HAL_StatusTypeDef HAL_SDADC_PollForConversion(SDADC_HandleTypeDef* hsdadc, uint32_t Timeout)
  1273. {
  1274. uint32_t tickstart;
  1275. /* Check parameters */
  1276. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1277. /* Check SDADC state */
  1278. if((hsdadc->State != HAL_SDADC_STATE_REG) && \
  1279. (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
  1280. {
  1281. /* Return error status */
  1282. return HAL_ERROR;
  1283. }
  1284. else
  1285. {
  1286. /* Get timeout */
  1287. tickstart = HAL_GetTick();
  1288. /* Wait REOCF bit in SDADC_ISR register */
  1289. while((hsdadc->Instance->ISR & SDADC_ISR_REOCF) != SDADC_ISR_REOCF)
  1290. {
  1291. /* Check the Timeout */
  1292. if(Timeout != HAL_MAX_DELAY)
  1293. {
  1294. if(((HAL_GetTick()-tickstart) > Timeout) || (Timeout == 0UL))
  1295. {
  1296. /* Return timeout status */
  1297. return HAL_TIMEOUT;
  1298. }
  1299. }
  1300. }
  1301. /* Check if overrun occurs */
  1302. if((hsdadc->Instance->ISR & SDADC_ISR_ROVRF) == SDADC_ISR_ROVRF)
  1303. {
  1304. /* Update error code and call error callback */
  1305. hsdadc->ErrorCode = SDADC_ERROR_REGULAR_OVERRUN;
  1306. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  1307. hsdadc->ErrorCallback(hsdadc);
  1308. #else
  1309. HAL_SDADC_ErrorCallback(hsdadc);
  1310. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  1311. /* Set CLRROVRF bit in SDADC_CLRISR register */
  1312. hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
  1313. }
  1314. /* Update SDADC state only if not continuous conversion and SW trigger */
  1315. if((hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  1316. (hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER))
  1317. {
  1318. hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
  1319. HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
  1320. }
  1321. /* Return function status */
  1322. return HAL_OK;
  1323. }
  1324. }
  1325. /**
  1326. * @brief This function allows to stop regular conversion in polling mode.
  1327. * @note This function should be called only if regular conversion is ongoing.
  1328. * @param hsdadc SDADC handle.
  1329. * @retval HAL status
  1330. */
  1331. HAL_StatusTypeDef HAL_SDADC_Stop(SDADC_HandleTypeDef *hsdadc)
  1332. {
  1333. HAL_StatusTypeDef status;
  1334. /* Check parameters */
  1335. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1336. /* Check SDADC state */
  1337. if((hsdadc->State != HAL_SDADC_STATE_REG) && \
  1338. (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
  1339. {
  1340. /* Return error status */
  1341. status = HAL_ERROR;
  1342. }
  1343. else
  1344. {
  1345. /* Stop regular conversion */
  1346. status = SDADC_RegConvStop(hsdadc);
  1347. }
  1348. /* Return function status */
  1349. return status;
  1350. }
  1351. /**
  1352. * @brief This function allows to start regular conversion in interrupt mode.
  1353. * @note This function should be called only when SDADC instance is in idle state
  1354. * or if injected conversion is ongoing.
  1355. * @param hsdadc SDADC handle.
  1356. * @retval HAL status
  1357. */
  1358. HAL_StatusTypeDef HAL_SDADC_Start_IT(SDADC_HandleTypeDef *hsdadc)
  1359. {
  1360. HAL_StatusTypeDef status;
  1361. /* Check parameters */
  1362. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1363. /* Check SDADC state */
  1364. if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  1365. (hsdadc->State == HAL_SDADC_STATE_INJ))
  1366. {
  1367. /* Set REOCIE and ROVRIE bits in SDADC_CR1 register */
  1368. hsdadc->Instance->CR1 |= (uint32_t) (SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
  1369. /* Start regular conversion */
  1370. status = SDADC_RegConvStart(hsdadc);
  1371. }
  1372. else
  1373. {
  1374. status = HAL_ERROR;
  1375. }
  1376. /* Return function status */
  1377. return status;
  1378. }
  1379. /**
  1380. * @brief This function allows to stop regular conversion in interrupt mode.
  1381. * @note This function should be called only if regular conversion is ongoing.
  1382. * @param hsdadc SDADC handle.
  1383. * @retval HAL status
  1384. */
  1385. HAL_StatusTypeDef HAL_SDADC_Stop_IT(SDADC_HandleTypeDef *hsdadc)
  1386. {
  1387. HAL_StatusTypeDef status;
  1388. /* Check parameters */
  1389. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1390. /* Check SDADC state */
  1391. if((hsdadc->State != HAL_SDADC_STATE_REG) && \
  1392. (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
  1393. {
  1394. /* Return error status */
  1395. status = HAL_ERROR;
  1396. }
  1397. else
  1398. {
  1399. /* Clear REOCIE and ROVRIE bits in SDADC_CR1 register */
  1400. hsdadc->Instance->CR1 &= (uint32_t) ~(SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
  1401. /* Stop regular conversion */
  1402. status = SDADC_RegConvStop(hsdadc);
  1403. }
  1404. /* Return function status */
  1405. return status;
  1406. }
  1407. /**
  1408. * @brief This function allows to start regular conversion in DMA mode.
  1409. * @note This function should be called only when SDADC instance is in idle state
  1410. * or if injected conversion is ongoing.
  1411. * @param hsdadc SDADC handle.
  1412. * @param pData The destination buffer address.
  1413. * @param Length The length of data to be transferred from SDADC peripheral to memory.
  1414. * @retval HAL status
  1415. */
  1416. HAL_StatusTypeDef HAL_SDADC_Start_DMA(SDADC_HandleTypeDef *hsdadc, uint32_t *pData,
  1417. uint32_t Length)
  1418. {
  1419. HAL_StatusTypeDef status;
  1420. /* Check parameters */
  1421. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1422. assert_param(pData != ((void*) 0));
  1423. assert_param(Length != 0UL);
  1424. /* Check that DMA is not enabled for injected conversion */
  1425. if((hsdadc->Instance->CR1 & SDADC_CR1_JDMAEN) == SDADC_CR1_JDMAEN)
  1426. {
  1427. status = HAL_ERROR;
  1428. }
  1429. /* Check parameters compatibility */
  1430. else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
  1431. (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  1432. (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
  1433. (Length != 1U))
  1434. {
  1435. status = HAL_ERROR;
  1436. }
  1437. else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
  1438. (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  1439. (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
  1440. {
  1441. status = HAL_ERROR;
  1442. }
  1443. /* Check SDADC state */
  1444. else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  1445. (hsdadc->State == HAL_SDADC_STATE_INJ))
  1446. {
  1447. /* Set callbacks on DMA handler */
  1448. hsdadc->hdma->XferCpltCallback = SDADC_DMARegularConvCplt;
  1449. hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
  1450. if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
  1451. {
  1452. hsdadc->hdma->XferHalfCpltCallback = SDADC_DMARegularHalfConvCplt;
  1453. }
  1454. /* Set RDMAEN bit in SDADC_CR1 register */
  1455. hsdadc->Instance->CR1 |= SDADC_CR1_RDMAEN;
  1456. /* Start DMA in interrupt mode */
  1457. if(HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->RDATAR, \
  1458. (uint32_t) pData, Length) != HAL_OK)
  1459. {
  1460. /* Set SDADC in error state */
  1461. hsdadc->State = HAL_SDADC_STATE_ERROR;
  1462. status = HAL_ERROR;
  1463. }
  1464. else
  1465. {
  1466. /* Start regular conversion */
  1467. status = SDADC_RegConvStart(hsdadc);
  1468. }
  1469. }
  1470. else
  1471. {
  1472. status = HAL_ERROR;
  1473. }
  1474. /* Return function status */
  1475. return status;
  1476. }
  1477. /**
  1478. * @brief This function allows to stop regular conversion in DMA mode.
  1479. * @note This function should be called only if regular conversion is ongoing.
  1480. * @param hsdadc SDADC handle.
  1481. * @retval HAL status
  1482. */
  1483. HAL_StatusTypeDef HAL_SDADC_Stop_DMA(SDADC_HandleTypeDef *hsdadc)
  1484. {
  1485. HAL_StatusTypeDef status;
  1486. /* Check parameters */
  1487. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1488. /* Check SDADC state */
  1489. if((hsdadc->State != HAL_SDADC_STATE_REG) && \
  1490. (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
  1491. {
  1492. /* Return error status */
  1493. status = HAL_ERROR;
  1494. }
  1495. else
  1496. {
  1497. /* Clear RDMAEN bit in SDADC_CR1 register */
  1498. hsdadc->Instance->CR1 &= ~(SDADC_CR1_RDMAEN);
  1499. /* Stop current DMA transfer */
  1500. if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
  1501. {
  1502. /* Set SDADC in error state */
  1503. hsdadc->State = HAL_SDADC_STATE_ERROR;
  1504. status = HAL_ERROR;
  1505. }
  1506. else
  1507. {
  1508. /* Stop regular conversion */
  1509. status = SDADC_RegConvStop(hsdadc);
  1510. }
  1511. }
  1512. /* Return function status */
  1513. return status;
  1514. }
  1515. /**
  1516. * @brief This function allows to get regular conversion value.
  1517. * @param hsdadc SDADC handle.
  1518. * @retval Regular conversion value
  1519. */
  1520. uint32_t HAL_SDADC_GetValue(SDADC_HandleTypeDef *hsdadc)
  1521. {
  1522. /* Check parameters */
  1523. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1524. /* Return regular conversion value */
  1525. return hsdadc->Instance->RDATAR;
  1526. }
  1527. /**
  1528. * @brief This function allows to start injected conversion in polling mode.
  1529. * @note This function should be called only when SDADC instance is in idle state
  1530. * or if regular conversion is ongoing.
  1531. * @param hsdadc SDADC handle.
  1532. * @retval HAL status
  1533. */
  1534. HAL_StatusTypeDef HAL_SDADC_InjectedStart(SDADC_HandleTypeDef *hsdadc)
  1535. {
  1536. HAL_StatusTypeDef status;
  1537. /* Check parameters */
  1538. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1539. /* Check SDADC state */
  1540. if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  1541. (hsdadc->State == HAL_SDADC_STATE_REG))
  1542. {
  1543. /* Start injected conversion */
  1544. status = SDADC_InjConvStart(hsdadc);
  1545. }
  1546. else
  1547. {
  1548. status = HAL_ERROR;
  1549. }
  1550. /* Return function status */
  1551. return status;
  1552. }
  1553. /**
  1554. * @brief This function allows to poll for the end of injected conversion.
  1555. * @note This function should be called only if injected conversion is ongoing.
  1556. * @param hsdadc SDADC handle.
  1557. * @param Timeout Timeout value in milliseconds.
  1558. * @retval HAL status
  1559. */
  1560. HAL_StatusTypeDef HAL_SDADC_PollForInjectedConversion(SDADC_HandleTypeDef* hsdadc,
  1561. uint32_t Timeout)
  1562. {
  1563. uint32_t tickstart;
  1564. /* Check parameters */
  1565. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1566. /* Check SDADC state */
  1567. if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
  1568. (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
  1569. {
  1570. /* Return error status */
  1571. return HAL_ERROR;
  1572. }
  1573. else
  1574. {
  1575. /* Get timeout */
  1576. tickstart = HAL_GetTick();
  1577. /* Wait JEOCF bit in SDADC_ISR register */
  1578. while((hsdadc->Instance->ISR & SDADC_ISR_JEOCF) != SDADC_ISR_JEOCF)
  1579. {
  1580. /* Check the Timeout */
  1581. if(Timeout != HAL_MAX_DELAY)
  1582. {
  1583. if(((HAL_GetTick()-tickstart) > Timeout) || (Timeout == 0UL))
  1584. {
  1585. /* Return timeout status */
  1586. return HAL_TIMEOUT;
  1587. }
  1588. }
  1589. }
  1590. /* Check if overrun occurs */
  1591. if((hsdadc->Instance->ISR & SDADC_ISR_JOVRF) == SDADC_ISR_JOVRF)
  1592. {
  1593. /* Update error code and call error callback */
  1594. hsdadc->ErrorCode = SDADC_ERROR_INJECTED_OVERRUN;
  1595. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  1596. hsdadc->ErrorCallback(hsdadc);
  1597. #else
  1598. HAL_SDADC_ErrorCallback(hsdadc);
  1599. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  1600. /* Set CLRJOVRF bit in SDADC_CLRISR register */
  1601. hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
  1602. }
  1603. /* Update remaining injected conversions */
  1604. hsdadc->InjConvRemaining--;
  1605. if(hsdadc->InjConvRemaining == 0UL)
  1606. {
  1607. /* end of injected sequence, reset the value */
  1608. hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
  1609. }
  1610. /* Update SDADC state only if not continuous conversion, SW trigger */
  1611. /* and end of injected sequence */
  1612. if((hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  1613. (hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
  1614. (hsdadc->InjConvRemaining == hsdadc->InjectedChannelsNbr))
  1615. {
  1616. hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
  1617. HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
  1618. }
  1619. /* Return function status */
  1620. return HAL_OK;
  1621. }
  1622. }
  1623. /**
  1624. * @brief This function allows to stop injected conversion in polling mode.
  1625. * @note This function should be called only if injected conversion is ongoing.
  1626. * @param hsdadc SDADC handle.
  1627. * @retval HAL status
  1628. */
  1629. HAL_StatusTypeDef HAL_SDADC_InjectedStop(SDADC_HandleTypeDef *hsdadc)
  1630. {
  1631. HAL_StatusTypeDef status;
  1632. /* Check parameters */
  1633. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1634. /* Check SDADC state */
  1635. if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
  1636. (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
  1637. {
  1638. /* Return error status */
  1639. status = HAL_ERROR;
  1640. }
  1641. else
  1642. {
  1643. /* Stop injected conversion */
  1644. status = SDADC_InjConvStop(hsdadc);
  1645. }
  1646. /* Return function status */
  1647. return status;
  1648. }
  1649. /**
  1650. * @brief This function allows to start injected conversion in interrupt mode.
  1651. * @note This function should be called only when SDADC instance is in idle state
  1652. * or if regular conversion is ongoing.
  1653. * @param hsdadc SDADC handle.
  1654. * @retval HAL status
  1655. */
  1656. HAL_StatusTypeDef HAL_SDADC_InjectedStart_IT(SDADC_HandleTypeDef *hsdadc)
  1657. {
  1658. HAL_StatusTypeDef status;
  1659. /* Check parameters */
  1660. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1661. /* Check SDADC state */
  1662. if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  1663. (hsdadc->State == HAL_SDADC_STATE_REG))
  1664. {
  1665. /* Set JEOCIE and JOVRIE bits in SDADC_CR1 register */
  1666. hsdadc->Instance->CR1 |= (uint32_t) (SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
  1667. /* Start injected conversion */
  1668. status = SDADC_InjConvStart(hsdadc);
  1669. }
  1670. else
  1671. {
  1672. status = HAL_ERROR;
  1673. }
  1674. /* Return function status */
  1675. return status;
  1676. }
  1677. /**
  1678. * @brief This function allows to stop injected conversion in interrupt mode.
  1679. * @note This function should be called only if injected conversion is ongoing.
  1680. * @param hsdadc SDADC handle.
  1681. * @retval HAL status
  1682. */
  1683. HAL_StatusTypeDef HAL_SDADC_InjectedStop_IT(SDADC_HandleTypeDef *hsdadc)
  1684. {
  1685. HAL_StatusTypeDef status;
  1686. /* Check parameters */
  1687. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1688. /* Check SDADC state */
  1689. if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
  1690. (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
  1691. {
  1692. /* Return error status */
  1693. status = HAL_ERROR;
  1694. }
  1695. else
  1696. {
  1697. /* Clear JEOCIE and JOVRIE bits in SDADC_CR1 register */
  1698. hsdadc->Instance->CR1 &= (uint32_t) ~(SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
  1699. /* Stop injected conversion */
  1700. status = SDADC_InjConvStop(hsdadc);
  1701. }
  1702. /* Return function status */
  1703. return status;
  1704. }
  1705. /**
  1706. * @brief This function allows to start injected conversion in DMA mode.
  1707. * @note This function should be called only when SDADC instance is in idle state
  1708. * or if regular conversion is ongoing.
  1709. * @param hsdadc SDADC handle.
  1710. * @param pData The destination buffer address.
  1711. * @param Length The length of data to be transferred from SDADC peripheral to memory.
  1712. * @retval HAL status
  1713. */
  1714. HAL_StatusTypeDef HAL_SDADC_InjectedStart_DMA(SDADC_HandleTypeDef *hsdadc, uint32_t *pData,
  1715. uint32_t Length)
  1716. {
  1717. HAL_StatusTypeDef status;
  1718. /* Check parameters */
  1719. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1720. assert_param(pData != ((void*) 0));
  1721. assert_param(Length != 0UL);
  1722. /* Check that DMA is not enabled for regular conversion */
  1723. if((hsdadc->Instance->CR1 & SDADC_CR1_RDMAEN) == SDADC_CR1_RDMAEN)
  1724. {
  1725. status = HAL_ERROR;
  1726. }
  1727. /* Check parameters compatibility */
  1728. else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
  1729. (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  1730. (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
  1731. (Length > hsdadc->InjectedChannelsNbr))
  1732. {
  1733. status = HAL_ERROR;
  1734. }
  1735. else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
  1736. (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  1737. (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
  1738. {
  1739. status = HAL_ERROR;
  1740. }
  1741. /* Check SDADC state */
  1742. else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  1743. (hsdadc->State == HAL_SDADC_STATE_REG))
  1744. {
  1745. /* Set callbacks on DMA handler */
  1746. hsdadc->hdma->XferCpltCallback = SDADC_DMAInjectedConvCplt;
  1747. hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
  1748. if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
  1749. {
  1750. hsdadc->hdma->XferHalfCpltCallback = SDADC_DMAInjectedHalfConvCplt;
  1751. }
  1752. /* Set JDMAEN bit in SDADC_CR1 register */
  1753. hsdadc->Instance->CR1 |= SDADC_CR1_JDMAEN;
  1754. /* Start DMA in interrupt mode */
  1755. if(HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->JDATAR, \
  1756. (uint32_t) pData, Length) != HAL_OK)
  1757. {
  1758. /* Set SDADC in error state */
  1759. hsdadc->State = HAL_SDADC_STATE_ERROR;
  1760. status = HAL_ERROR;
  1761. }
  1762. else
  1763. {
  1764. /* Start injected conversion */
  1765. status = SDADC_InjConvStart(hsdadc);
  1766. }
  1767. }
  1768. else
  1769. {
  1770. status = HAL_ERROR;
  1771. }
  1772. /* Return function status */
  1773. return status;
  1774. }
  1775. /**
  1776. * @brief This function allows to stop injected conversion in DMA mode.
  1777. * @note This function should be called only if injected conversion is ongoing.
  1778. * @param hsdadc SDADC handle.
  1779. * @retval HAL status
  1780. */
  1781. HAL_StatusTypeDef HAL_SDADC_InjectedStop_DMA(SDADC_HandleTypeDef *hsdadc)
  1782. {
  1783. HAL_StatusTypeDef status;
  1784. /* Check parameters */
  1785. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1786. /* Check SDADC state */
  1787. if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
  1788. (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
  1789. {
  1790. /* Return error status */
  1791. status = HAL_ERROR;
  1792. }
  1793. else
  1794. {
  1795. /* Clear JDMAEN bit in SDADC_CR1 register */
  1796. hsdadc->Instance->CR1 &= ~(SDADC_CR1_JDMAEN);
  1797. /* Stop current DMA transfer */
  1798. if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
  1799. {
  1800. /* Set SDADC in error state */
  1801. hsdadc->State = HAL_SDADC_STATE_ERROR;
  1802. status = HAL_ERROR;
  1803. }
  1804. else
  1805. {
  1806. /* Stop injected conversion */
  1807. status = SDADC_InjConvStop(hsdadc);
  1808. }
  1809. }
  1810. /* Return function status */
  1811. return status;
  1812. }
  1813. /**
  1814. * @brief This function allows to get injected conversion value.
  1815. * @param hsdadc SDADC handle.
  1816. * @param Channel Corresponding channel of injected conversion.
  1817. * @retval Injected conversion value
  1818. */
  1819. uint32_t HAL_SDADC_InjectedGetValue(SDADC_HandleTypeDef *hsdadc, uint32_t* Channel)
  1820. {
  1821. uint32_t value;
  1822. /* Check parameters */
  1823. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1824. assert_param(Channel != ((void*) 0));
  1825. /* Read SDADC_JDATAR register and extract channel and conversion value */
  1826. value = hsdadc->Instance->JDATAR;
  1827. *Channel = ((value & SDADC_JDATAR_JDATACH) >> SDADC_JDATAR_CH_OFFSET);
  1828. value &= SDADC_JDATAR_JDATA;
  1829. /* Return injected conversion value */
  1830. return value;
  1831. }
  1832. /**
  1833. * @brief This function allows to start multimode regular conversions in DMA mode.
  1834. * @note This function should be called only when SDADC instance is in idle state
  1835. * or if injected conversion is ongoing.
  1836. * @param hsdadc SDADC handle.
  1837. * @param pData The destination buffer address.
  1838. * @param Length The length of data to be transferred from SDADC peripheral to memory.
  1839. * @retval HAL status
  1840. */
  1841. HAL_StatusTypeDef HAL_SDADC_MultiModeStart_DMA(SDADC_HandleTypeDef* hsdadc, uint32_t* pData,
  1842. uint32_t Length)
  1843. {
  1844. HAL_StatusTypeDef status;
  1845. /* Check parameters */
  1846. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1847. assert_param(pData != ((void*) 0));
  1848. assert_param(Length != 0UL);
  1849. /* Check instance is SDADC1 */
  1850. if(hsdadc->Instance != SDADC1)
  1851. {
  1852. status = HAL_ERROR;
  1853. }
  1854. /* Check that DMA is not enabled for injected conversion */
  1855. else if((hsdadc->Instance->CR1 & SDADC_CR1_JDMAEN) == SDADC_CR1_JDMAEN)
  1856. {
  1857. status = HAL_ERROR;
  1858. }
  1859. /* Check parameters compatibility */
  1860. else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
  1861. (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  1862. (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
  1863. (Length != 1U))
  1864. {
  1865. status = HAL_ERROR;
  1866. }
  1867. else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
  1868. (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  1869. (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
  1870. {
  1871. status = HAL_ERROR;
  1872. }
  1873. /* Check SDADC state */
  1874. else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  1875. (hsdadc->State == HAL_SDADC_STATE_INJ))
  1876. {
  1877. /* Set callbacks on DMA handler */
  1878. hsdadc->hdma->XferCpltCallback = SDADC_DMARegularConvCplt;
  1879. hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
  1880. if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
  1881. {
  1882. hsdadc->hdma->XferHalfCpltCallback = SDADC_DMARegularHalfConvCplt;
  1883. }
  1884. /* Set RDMAEN bit in SDADC_CR1 register */
  1885. hsdadc->Instance->CR1 |= SDADC_CR1_RDMAEN;
  1886. /* Start DMA in interrupt mode */
  1887. if(hsdadc->RegularMultimode == SDADC_MULTIMODE_SDADC1_SDADC2)
  1888. {
  1889. status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->RDATA12R, \
  1890. (uint32_t) pData, Length);
  1891. }
  1892. else
  1893. {
  1894. status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->RDATA13R, \
  1895. (uint32_t) pData, Length);
  1896. }
  1897. if(status != HAL_OK)
  1898. {
  1899. /* Set SDADC in error state */
  1900. hsdadc->State = HAL_SDADC_STATE_ERROR;
  1901. status = HAL_ERROR;
  1902. }
  1903. else
  1904. {
  1905. /* Start regular conversion */
  1906. status = SDADC_RegConvStart(hsdadc);
  1907. }
  1908. }
  1909. else
  1910. {
  1911. status = HAL_ERROR;
  1912. }
  1913. /* Return function status */
  1914. return status;
  1915. }
  1916. /**
  1917. * @brief This function allows to stop multimode regular conversions in DMA mode.
  1918. * @note This function should be called only if regular conversion is ongoing.
  1919. * @param hsdadc SDADC handle.
  1920. * @retval HAL status
  1921. */
  1922. HAL_StatusTypeDef HAL_SDADC_MultiModeStop_DMA(SDADC_HandleTypeDef* hsdadc)
  1923. {
  1924. HAL_StatusTypeDef status;
  1925. /* Check parameters */
  1926. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1927. /* Check instance is SDADC1 */
  1928. if(hsdadc->Instance != SDADC1)
  1929. {
  1930. status = HAL_ERROR;
  1931. }
  1932. /* Check SDADC state */
  1933. else if((hsdadc->State != HAL_SDADC_STATE_REG) && \
  1934. (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
  1935. {
  1936. /* Return error status */
  1937. status = HAL_ERROR;
  1938. }
  1939. else
  1940. {
  1941. /* Clear RDMAEN bit in SDADC_CR1 register */
  1942. hsdadc->Instance->CR1 &= ~(SDADC_CR1_RDMAEN);
  1943. /* Stop current DMA transfer */
  1944. if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
  1945. {
  1946. /* Set SDADC in error state */
  1947. hsdadc->State = HAL_SDADC_STATE_ERROR;
  1948. status = HAL_ERROR;
  1949. }
  1950. else
  1951. {
  1952. /* Stop regular conversion */
  1953. status = SDADC_RegConvStop(hsdadc);
  1954. }
  1955. }
  1956. /* Return function status */
  1957. return status;
  1958. }
  1959. /**
  1960. * @brief This function allows to get multimode regular conversion value.
  1961. * @param hsdadc SDADC handle.
  1962. * @retval Multimode regular conversion value
  1963. */
  1964. uint32_t HAL_SDADC_MultiModeGetValue(SDADC_HandleTypeDef* hsdadc)
  1965. {
  1966. uint32_t value;
  1967. /* Check parameters and check instance is SDADC1 */
  1968. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1969. assert_param(hsdadc->Instance == SDADC1);
  1970. /* read multimode regular value */
  1971. value = (hsdadc->RegularMultimode == SDADC_MULTIMODE_SDADC1_SDADC2) ? \
  1972. hsdadc->Instance->RDATA12R : hsdadc->Instance->RDATA13R;
  1973. /* Return multimode regular conversions value */
  1974. return value;
  1975. }
  1976. /**
  1977. * @brief This function allows to start multimode injected conversions in DMA mode.
  1978. * @note This function should be called only when SDADC instance is in idle state
  1979. * or if regular conversion is ongoing.
  1980. * @param hsdadc SDADC handle.
  1981. * @param pData The destination buffer address.
  1982. * @param Length The length of data to be transferred from SDADC peripheral to memory.
  1983. * @retval HAL status
  1984. */
  1985. HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeStart_DMA(SDADC_HandleTypeDef* hsdadc,
  1986. uint32_t* pData, uint32_t Length)
  1987. {
  1988. HAL_StatusTypeDef status;
  1989. /* Check parameters */
  1990. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  1991. assert_param(pData != ((void*) 0));
  1992. assert_param(Length != 0UL);
  1993. /* Check instance is SDADC1 */
  1994. if(hsdadc->Instance != SDADC1)
  1995. {
  1996. status = HAL_ERROR;
  1997. }
  1998. /* Check that DMA is not enabled for regular conversion */
  1999. else if((hsdadc->Instance->CR1 & SDADC_CR1_RDMAEN) == SDADC_CR1_RDMAEN)
  2000. {
  2001. status = HAL_ERROR;
  2002. }
  2003. /* Check parameters compatibility */
  2004. else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
  2005. (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  2006. (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
  2007. (Length > (hsdadc->InjectedChannelsNbr << 1U)))
  2008. {
  2009. status = HAL_ERROR;
  2010. }
  2011. else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
  2012. (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  2013. (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
  2014. {
  2015. status = HAL_ERROR;
  2016. }
  2017. /* Check SDADC state */
  2018. else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
  2019. (hsdadc->State == HAL_SDADC_STATE_REG))
  2020. {
  2021. /* Set callbacks on DMA handler */
  2022. hsdadc->hdma->XferCpltCallback = SDADC_DMAInjectedConvCplt;
  2023. hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
  2024. if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
  2025. {
  2026. hsdadc->hdma->XferHalfCpltCallback = SDADC_DMAInjectedHalfConvCplt;
  2027. }
  2028. /* Set JDMAEN bit in SDADC_CR1 register */
  2029. hsdadc->Instance->CR1 |= SDADC_CR1_JDMAEN;
  2030. /* Start DMA in interrupt mode */
  2031. if(hsdadc->InjectedMultimode == SDADC_MULTIMODE_SDADC1_SDADC2)
  2032. {
  2033. status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->JDATA12R, \
  2034. (uint32_t) pData, Length);
  2035. }
  2036. else
  2037. {
  2038. status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->JDATA13R, \
  2039. (uint32_t) pData, Length);
  2040. }
  2041. if(status != HAL_OK)
  2042. {
  2043. /* Set SDADC in error state */
  2044. hsdadc->State = HAL_SDADC_STATE_ERROR;
  2045. status = HAL_ERROR;
  2046. }
  2047. else
  2048. {
  2049. /* Start injected conversion */
  2050. status = SDADC_InjConvStart(hsdadc);
  2051. }
  2052. }
  2053. else
  2054. {
  2055. status = HAL_ERROR;
  2056. }
  2057. /* Return function status */
  2058. return status;
  2059. }
  2060. /**
  2061. * @brief This function allows to stop multimode injected conversions in DMA mode.
  2062. * @note This function should be called only if injected conversion is ongoing.
  2063. * @param hsdadc SDADC handle.
  2064. * @retval HAL status
  2065. */
  2066. HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeStop_DMA(SDADC_HandleTypeDef* hsdadc)
  2067. {
  2068. HAL_StatusTypeDef status;
  2069. /* Check parameters */
  2070. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  2071. /* Check instance is SDADC1 */
  2072. if(hsdadc->Instance != SDADC1)
  2073. {
  2074. status = HAL_ERROR;
  2075. }
  2076. /* Check SDADC state */
  2077. else if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
  2078. (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
  2079. {
  2080. /* Return error status */
  2081. status = HAL_ERROR;
  2082. }
  2083. else
  2084. {
  2085. /* Clear JDMAEN bit in SDADC_CR1 register */
  2086. hsdadc->Instance->CR1 &= ~(SDADC_CR1_JDMAEN);
  2087. /* Stop current DMA transfer */
  2088. if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
  2089. {
  2090. /* Set SDADC in error state */
  2091. hsdadc->State = HAL_SDADC_STATE_ERROR;
  2092. status = HAL_ERROR;
  2093. }
  2094. else
  2095. {
  2096. /* Stop injected conversion */
  2097. status = SDADC_InjConvStop(hsdadc);
  2098. }
  2099. }
  2100. /* Return function status */
  2101. return status;
  2102. }
  2103. /**
  2104. * @brief This function allows to get multimode injected conversion value.
  2105. * @param hsdadc SDADC handle.
  2106. * @retval Multimode injected conversion value
  2107. */
  2108. uint32_t HAL_SDADC_InjectedMultiModeGetValue(SDADC_HandleTypeDef* hsdadc)
  2109. {
  2110. uint32_t value;
  2111. /* Check parameters and check instance is SDADC1 */
  2112. assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
  2113. assert_param(hsdadc->Instance == SDADC1);
  2114. /* read multimode injected value */
  2115. value = (hsdadc->InjectedMultimode == SDADC_MULTIMODE_SDADC1_SDADC2) ? \
  2116. hsdadc->Instance->JDATA12R : hsdadc->Instance->JDATA13R;
  2117. /* Return multimode injected conversions value */
  2118. return value;
  2119. }
  2120. /**
  2121. * @brief This function handles the SDADC interrupts.
  2122. * @param hsdadc SDADC handle.
  2123. * @retval None
  2124. */
  2125. void HAL_SDADC_IRQHandler(SDADC_HandleTypeDef* hsdadc)
  2126. {
  2127. uint32_t tmp_isr = hsdadc->Instance->ISR;
  2128. uint32_t tmp_cr1 = hsdadc->Instance->CR1;
  2129. /* Check if end of regular conversion */
  2130. if(((tmp_cr1 & SDADC_CR1_REOCIE) == SDADC_CR1_REOCIE) &&
  2131. ((tmp_isr & SDADC_ISR_REOCF) == SDADC_ISR_REOCF))
  2132. {
  2133. /* Call regular conversion complete callback */
  2134. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  2135. hsdadc->ConvCpltCallback(hsdadc);
  2136. #else
  2137. HAL_SDADC_ConvCpltCallback(hsdadc);
  2138. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  2139. /* End of conversion if mode is not continuous and software trigger */
  2140. if((hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  2141. (hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER))
  2142. {
  2143. /* Clear REOCIE and ROVRIE bits in SDADC_CR1 register */
  2144. hsdadc->Instance->CR1 &= ~(SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
  2145. /* Update SDADC state */
  2146. hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
  2147. HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
  2148. }
  2149. }
  2150. /* Check if end of injected conversion */
  2151. else if(((tmp_cr1 & SDADC_CR1_JEOCIE) == SDADC_CR1_JEOCIE) &&
  2152. ((tmp_isr & SDADC_ISR_JEOCF) == SDADC_ISR_JEOCF))
  2153. {
  2154. /* Call injected conversion complete callback */
  2155. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  2156. hsdadc->InjectedConvCpltCallback(hsdadc);
  2157. #else
  2158. HAL_SDADC_InjectedConvCpltCallback(hsdadc);
  2159. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  2160. /* Update remaining injected conversions */
  2161. hsdadc->InjConvRemaining--;
  2162. if(hsdadc->InjConvRemaining ==0UL)
  2163. {
  2164. /* end of injected sequence, reset the value */
  2165. hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
  2166. }
  2167. /* End of conversion if mode is not continuous, software trigger */
  2168. /* and end of injected sequence */
  2169. if((hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
  2170. (hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
  2171. (hsdadc->InjConvRemaining == hsdadc->InjectedChannelsNbr))
  2172. {
  2173. /* Clear JEOCIE and JOVRIE bits in SDADC_CR1 register */
  2174. hsdadc->Instance->CR1 &= ~(SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
  2175. /* Update SDADC state */
  2176. hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
  2177. HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
  2178. }
  2179. }
  2180. /* Check if end of calibration */
  2181. else if(((tmp_cr1 & SDADC_CR1_EOCALIE) == SDADC_CR1_EOCALIE) &&
  2182. ((tmp_isr & SDADC_ISR_EOCALF) == SDADC_ISR_EOCALF))
  2183. {
  2184. /* Clear EOCALIE bit in SDADC_CR1 register */
  2185. hsdadc->Instance->CR1 &= ~(SDADC_CR1_EOCALIE);
  2186. /* Set CLREOCALF bit in SDADC_CLRISR register */
  2187. hsdadc->Instance->CLRISR |= SDADC_ISR_CLREOCALF;
  2188. /* Call calibration callback */
  2189. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  2190. hsdadc->CalibrationCpltCallback(hsdadc);
  2191. #else
  2192. HAL_SDADC_CalibrationCpltCallback(hsdadc);
  2193. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  2194. /* Update SDADC state */
  2195. hsdadc->State = HAL_SDADC_STATE_READY;
  2196. }
  2197. /* Check if overrun occurs during regular conversion */
  2198. else if(((tmp_cr1 & SDADC_CR1_ROVRIE) == SDADC_CR1_ROVRIE) &&
  2199. ((tmp_isr & SDADC_ISR_ROVRF) == SDADC_ISR_ROVRF))
  2200. {
  2201. /* Set CLRROVRF bit in SDADC_CLRISR register */
  2202. hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
  2203. /* Update error code */
  2204. hsdadc->ErrorCode = SDADC_ERROR_REGULAR_OVERRUN;
  2205. /* Call error callback */
  2206. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  2207. hsdadc->ErrorCallback(hsdadc);
  2208. #else
  2209. HAL_SDADC_ErrorCallback(hsdadc);
  2210. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  2211. }
  2212. /* Check if overrun occurs during injected conversion */
  2213. else if(((tmp_cr1 & SDADC_CR1_JOVRIE) == SDADC_CR1_JOVRIE) &&
  2214. ((tmp_isr & SDADC_ISR_JOVRF) == SDADC_ISR_JOVRF))
  2215. {
  2216. /* Set CLRJOVRF bit in SDADC_CLRISR register */
  2217. hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
  2218. /* Update error code */
  2219. hsdadc->ErrorCode = SDADC_ERROR_INJECTED_OVERRUN;
  2220. /* Call error callback */
  2221. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  2222. hsdadc->ErrorCallback(hsdadc);
  2223. #else
  2224. HAL_SDADC_ErrorCallback(hsdadc);
  2225. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  2226. }
  2227. else
  2228. {
  2229. /* No additional IRQ source */
  2230. }
  2231. return;
  2232. }
  2233. /**
  2234. * @brief Calibration complete callback.
  2235. * @param hsdadc SDADC handle.
  2236. * @retval None
  2237. */
  2238. __weak void HAL_SDADC_CalibrationCpltCallback(SDADC_HandleTypeDef* hsdadc)
  2239. {
  2240. /* Prevent unused argument(s) compilation warning */
  2241. UNUSED(hsdadc);
  2242. /* NOTE : This function should not be modified, when the callback is needed,
  2243. the HAL_SDADC_CalibrationCpltCallback could be implemented in the user file
  2244. */
  2245. }
  2246. /**
  2247. * @brief Half regular conversion complete callback.
  2248. * @param hsdadc SDADC handle.
  2249. * @retval None
  2250. */
  2251. __weak void HAL_SDADC_ConvHalfCpltCallback(SDADC_HandleTypeDef* hsdadc)
  2252. {
  2253. /* Prevent unused argument(s) compilation warning */
  2254. UNUSED(hsdadc);
  2255. /* NOTE : This function should not be modified, when the callback is needed,
  2256. the HAL_SDADC_ConvHalfCpltCallback could be implemented in the user file
  2257. */
  2258. }
  2259. /**
  2260. * @brief Regular conversion complete callback.
  2261. * @note In interrupt mode, user has to read conversion value in this function
  2262. using HAL_SDADC_GetValue or HAL_SDADC_MultiModeGetValue.
  2263. * @param hsdadc SDADC handle.
  2264. * @retval None
  2265. */
  2266. __weak void HAL_SDADC_ConvCpltCallback(SDADC_HandleTypeDef* hsdadc)
  2267. {
  2268. /* Prevent unused argument(s) compilation warning */
  2269. UNUSED(hsdadc);
  2270. /* NOTE : This function should not be modified, when the callback is needed,
  2271. the HAL_SDADC_ConvCpltCallback could be implemented in the user file.
  2272. */
  2273. }
  2274. /**
  2275. * @brief Half injected conversion complete callback.
  2276. * @param hsdadc SDADC handle.
  2277. * @retval None
  2278. */
  2279. __weak void HAL_SDADC_InjectedConvHalfCpltCallback(SDADC_HandleTypeDef* hsdadc)
  2280. {
  2281. /* Prevent unused argument(s) compilation warning */
  2282. UNUSED(hsdadc);
  2283. /* NOTE : This function should not be modified, when the callback is needed,
  2284. the HAL_SDADC_InjectedConvHalfCpltCallback could be implemented in the user file.
  2285. */
  2286. }
  2287. /**
  2288. * @brief Injected conversion complete callback.
  2289. * @note In interrupt mode, user has to read conversion value in this function
  2290. using HAL_SDADC_InjectedGetValue or HAL_SDADC_InjectedMultiModeGetValue.
  2291. * @param hsdadc SDADC handle.
  2292. * @retval None
  2293. */
  2294. __weak void HAL_SDADC_InjectedConvCpltCallback(SDADC_HandleTypeDef* hsdadc)
  2295. {
  2296. /* Prevent unused argument(s) compilation warning */
  2297. UNUSED(hsdadc);
  2298. /* NOTE : This function should not be modified, when the callback is needed,
  2299. the HAL_SDADC_InjectedConvCpltCallback could be implemented in the user file.
  2300. */
  2301. }
  2302. /**
  2303. * @brief Error callback.
  2304. * @param hsdadc SDADC handle.
  2305. * @retval None
  2306. */
  2307. __weak void HAL_SDADC_ErrorCallback(SDADC_HandleTypeDef* hsdadc)
  2308. {
  2309. /* Prevent unused argument(s) compilation warning */
  2310. UNUSED(hsdadc);
  2311. /* NOTE : This function should not be modified, when the callback is needed,
  2312. the HAL_SDADC_ErrorCallback could be implemented in the user file.
  2313. */
  2314. }
  2315. /**
  2316. * @brief DMA half transfer complete callback for regular conversion.
  2317. * @param hdma DMA handle.
  2318. * @retval None
  2319. */
  2320. static void SDADC_DMARegularHalfConvCplt(DMA_HandleTypeDef *hdma)
  2321. {
  2322. /* Get SDADC handle */
  2323. SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
  2324. /* Call regular half conversion complete callback */
  2325. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  2326. hsdadc->ConvHalfCpltCallback(hsdadc);
  2327. #else
  2328. HAL_SDADC_ConvHalfCpltCallback(hsdadc);
  2329. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  2330. }
  2331. /**
  2332. * @brief DMA transfer complete callback for regular conversion.
  2333. * @param hdma DMA handle.
  2334. * @retval None
  2335. */
  2336. static void SDADC_DMARegularConvCplt(DMA_HandleTypeDef *hdma)
  2337. {
  2338. /* Get SDADC handle */
  2339. SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
  2340. /* Call regular conversion complete callback */
  2341. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  2342. hsdadc->ConvCpltCallback(hsdadc);
  2343. #else
  2344. HAL_SDADC_ConvCpltCallback(hsdadc);
  2345. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  2346. }
  2347. /**
  2348. * @brief DMA half transfer complete callback for injected conversion.
  2349. * @param hdma DMA handle.
  2350. * @retval None
  2351. */
  2352. static void SDADC_DMAInjectedHalfConvCplt(DMA_HandleTypeDef *hdma)
  2353. {
  2354. /* Get SDADC handle */
  2355. SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
  2356. /* Call injected half conversion complete callback */
  2357. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  2358. hsdadc->InjectedConvHalfCpltCallback(hsdadc);
  2359. #else
  2360. HAL_SDADC_InjectedConvHalfCpltCallback(hsdadc);
  2361. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  2362. }
  2363. /**
  2364. * @brief DMA transfer complete callback for injected conversion.
  2365. * @param hdma DMA handle.
  2366. * @retval None
  2367. */
  2368. static void SDADC_DMAInjectedConvCplt(DMA_HandleTypeDef *hdma)
  2369. {
  2370. /* Get SDADC handle */
  2371. SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
  2372. /* Call injected conversion complete callback */
  2373. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  2374. hsdadc->InjectedConvCpltCallback(hsdadc);
  2375. #else
  2376. HAL_SDADC_InjectedConvCpltCallback(hsdadc);
  2377. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  2378. }
  2379. /**
  2380. * @brief DMA error callback.
  2381. * @param hdma DMA handle.
  2382. * @retval None
  2383. */
  2384. static void SDADC_DMAError(DMA_HandleTypeDef *hdma)
  2385. {
  2386. /* Get SDADC handle */
  2387. SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
  2388. /* Update error code */
  2389. hsdadc->ErrorCode = SDADC_ERROR_DMA;
  2390. /* Call error callback */
  2391. #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
  2392. hsdadc->ErrorCallback(hsdadc);
  2393. #else
  2394. HAL_SDADC_ErrorCallback(hsdadc);
  2395. #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
  2396. }
  2397. /**
  2398. * @}
  2399. */
  2400. /** @defgroup SDADC_Exported_Functions_Group4 Peripheral State functions
  2401. * @brief SDADC Peripheral State functions
  2402. *
  2403. @verbatim
  2404. ===============================================================================
  2405. ##### ADC Peripheral State functions #####
  2406. ===============================================================================
  2407. [..] This subsection provides functions allowing to
  2408. (+) Get the SDADC state
  2409. (+) Get the SDADC Error
  2410. @endverbatim
  2411. * @{
  2412. */
  2413. /**
  2414. * @brief This function allows to get the current SDADC state.
  2415. * @param hsdadc SDADC handle.
  2416. * @retval SDADC state.
  2417. */
  2418. HAL_SDADC_StateTypeDef HAL_SDADC_GetState(SDADC_HandleTypeDef* hsdadc)
  2419. {
  2420. return hsdadc->State;
  2421. }
  2422. /**
  2423. * @brief This function allows to get the current SDADC error code.
  2424. * @param hsdadc SDADC handle.
  2425. * @retval SDADC error code.
  2426. */
  2427. uint32_t HAL_SDADC_GetError(SDADC_HandleTypeDef* hsdadc)
  2428. {
  2429. return hsdadc->ErrorCode;
  2430. }
  2431. /**
  2432. * @}
  2433. */
  2434. /** @addtogroup SDADC_Private_Functions SDADC Private Functions
  2435. * @{
  2436. */
  2437. /**
  2438. * @brief This function allows to enter in init mode for SDADC instance.
  2439. * @param hsdadc SDADC handle.
  2440. * @retval HAL status.
  2441. */
  2442. static HAL_StatusTypeDef SDADC_EnterInitMode(SDADC_HandleTypeDef* hsdadc)
  2443. {
  2444. uint32_t tickstart;
  2445. /* Set INIT bit on SDADC_CR1 register */
  2446. hsdadc->Instance->CR1 |= SDADC_CR1_INIT;
  2447. /* Wait INITRDY bit on SDADC_ISR */
  2448. tickstart = HAL_GetTick();
  2449. while((hsdadc->Instance->ISR & SDADC_ISR_INITRDY) == (uint32_t)RESET)
  2450. {
  2451. if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
  2452. {
  2453. return HAL_TIMEOUT;
  2454. }
  2455. }
  2456. /* Return HAL status */
  2457. return HAL_OK;
  2458. }
  2459. /**
  2460. * @brief This function allows to exit from init mode for SDADC instance.
  2461. * @param hsdadc SDADC handle.
  2462. * @retval None.
  2463. */
  2464. static void SDADC_ExitInitMode(SDADC_HandleTypeDef* hsdadc)
  2465. {
  2466. /* Reset INIT bit in SDADC_CR1 register */
  2467. hsdadc->Instance->CR1 &= ~(SDADC_CR1_INIT);
  2468. }
  2469. /**
  2470. * @brief This function allows to get the number of injected channels.
  2471. * @param Channels bitfield of injected channels.
  2472. * @retval Number of injected channels.
  2473. */
  2474. static uint32_t SDADC_GetInjChannelsNbr(uint32_t Channels)
  2475. {
  2476. uint32_t nbChannels = 0UL;
  2477. uint32_t tmp,i;
  2478. /* Get the number of channels from bitfield */
  2479. tmp = (uint32_t) (Channels & SDADC_LSB_MASK);
  2480. for(i = 0UL ; i < 9UL ; i++)
  2481. {
  2482. if((tmp & 0x00000001UL) != 0UL)
  2483. {
  2484. nbChannels++;
  2485. }
  2486. tmp = (uint32_t) (tmp >> 1UL);
  2487. }
  2488. return nbChannels;
  2489. }
  2490. /**
  2491. * @brief This function allows to really start regular conversion.
  2492. * @param hsdadc SDADC handle.
  2493. * @retval HAL status.
  2494. */
  2495. static HAL_StatusTypeDef SDADC_RegConvStart(SDADC_HandleTypeDef* hsdadc)
  2496. {
  2497. HAL_StatusTypeDef status = HAL_OK;
  2498. /* Check regular trigger */
  2499. if(hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER)
  2500. {
  2501. /* Set RSWSTART bit in SDADC_CR2 register */
  2502. hsdadc->Instance->CR2 |= SDADC_CR2_RSWSTART;
  2503. }
  2504. else /* synchronous trigger */
  2505. {
  2506. /* Enter init mode */
  2507. if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
  2508. {
  2509. /* Set SDADC in error state */
  2510. hsdadc->State = HAL_SDADC_STATE_ERROR;
  2511. status = HAL_TIMEOUT;
  2512. }
  2513. else
  2514. {
  2515. /* Set RSYNC bit in SDADC_CR1 register */
  2516. hsdadc->Instance->CR1 |= SDADC_CR1_RSYNC;
  2517. /* Exit init mode */
  2518. SDADC_ExitInitMode(hsdadc);
  2519. }
  2520. }
  2521. /* Update SDADC state only if status is OK */
  2522. if(status == HAL_OK)
  2523. {
  2524. hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_READY) ? \
  2525. HAL_SDADC_STATE_REG : HAL_SDADC_STATE_REG_INJ;
  2526. }
  2527. /* Return function status */
  2528. return status;
  2529. }
  2530. /**
  2531. * @brief This function allows to really stop regular conversion.
  2532. * @param hsdadc SDADC handle.
  2533. * @retval HAL status.
  2534. */
  2535. static HAL_StatusTypeDef SDADC_RegConvStop(SDADC_HandleTypeDef* hsdadc)
  2536. {
  2537. uint32_t tickstart;
  2538. __IO uint32_t dummy_read_for_register_reset;
  2539. /* Check continuous mode */
  2540. if(hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_ON)
  2541. {
  2542. /* Clear REOCF by reading SDADC_RDATAR register */
  2543. dummy_read_for_register_reset = hsdadc->Instance->RDATAR;
  2544. UNUSED(dummy_read_for_register_reset);
  2545. /* Clear RCONT bit in SDADC_CR2 register */
  2546. hsdadc->Instance->CR2 &= ~(SDADC_CR2_RCONT);
  2547. }
  2548. /* Wait for the end of regular conversion */
  2549. tickstart = HAL_GetTick();
  2550. while((hsdadc->Instance->ISR & SDADC_ISR_RCIP) != 0UL)
  2551. {
  2552. if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
  2553. {
  2554. /* Set SDADC in error state and return timeout status */
  2555. hsdadc->State = HAL_SDADC_STATE_ERROR;
  2556. return HAL_TIMEOUT;
  2557. }
  2558. }
  2559. /* Check if trigger is synchronous */
  2560. if(hsdadc->RegularTrigger == SDADC_SYNCHRONOUS_TRIGGER)
  2561. {
  2562. /* Enter init mode */
  2563. if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
  2564. {
  2565. /* Set SDADC in error state and return timeout status */
  2566. hsdadc->State = HAL_SDADC_STATE_ERROR;
  2567. return HAL_TIMEOUT;
  2568. }
  2569. else
  2570. {
  2571. /* Clear RSYNC bit in SDADC_CR1 register */
  2572. hsdadc->Instance->CR1 &= ~(SDADC_CR1_RSYNC);
  2573. /* Exit init mode */
  2574. SDADC_ExitInitMode(hsdadc);
  2575. }
  2576. }
  2577. /* Check if continuous mode */
  2578. if(hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_ON)
  2579. {
  2580. /* Restore RCONT bit in SDADC_CR2 register */
  2581. hsdadc->Instance->CR2 |= SDADC_CR2_RCONT;
  2582. }
  2583. /* Clear REOCF by reading SDADC_RDATAR register */
  2584. dummy_read_for_register_reset = hsdadc->Instance->RDATAR;
  2585. UNUSED(dummy_read_for_register_reset);
  2586. /* Set CLRROVRF bit in SDADC_CLRISR register */
  2587. hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
  2588. /* Update SDADC state */
  2589. hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
  2590. HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
  2591. /* Return function status */
  2592. return HAL_OK;
  2593. }
  2594. /**
  2595. * @brief This function allows to really start injected conversion.
  2596. * @param hsdadc SDADC handle.
  2597. * @retval HAL status.
  2598. */
  2599. static HAL_StatusTypeDef SDADC_InjConvStart(SDADC_HandleTypeDef* hsdadc)
  2600. {
  2601. HAL_StatusTypeDef status = HAL_OK;
  2602. /* Initialize number of injected conversions remaining */
  2603. hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
  2604. /* Check injected trigger */
  2605. if(hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER)
  2606. {
  2607. /* Set JSWSTART bit in SDADC_CR2 register */
  2608. hsdadc->Instance->CR2 |= SDADC_CR2_JSWSTART;
  2609. }
  2610. else /* external or synchronous trigger */
  2611. {
  2612. /* Enter init mode */
  2613. if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
  2614. {
  2615. /* Set SDADC in error state */
  2616. hsdadc->State = HAL_SDADC_STATE_ERROR;
  2617. status = HAL_TIMEOUT;
  2618. }
  2619. else
  2620. {
  2621. if(hsdadc->InjectedTrigger == SDADC_SYNCHRONOUS_TRIGGER)
  2622. {
  2623. /* Set JSYNC bit in SDADC_CR1 register */
  2624. hsdadc->Instance->CR1 |= SDADC_CR1_JSYNC;
  2625. }
  2626. else /* external trigger */
  2627. {
  2628. /* Set JEXTEN[1:0] bits in SDADC_CR2 register */
  2629. hsdadc->Instance->CR2 |= hsdadc->ExtTriggerEdge;
  2630. }
  2631. /* Exit init mode */
  2632. SDADC_ExitInitMode(hsdadc);
  2633. }
  2634. }
  2635. /* Update SDADC state only if status is OK */
  2636. if(status == HAL_OK)
  2637. {
  2638. hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_READY) ? \
  2639. HAL_SDADC_STATE_INJ : HAL_SDADC_STATE_REG_INJ;
  2640. }
  2641. /* Return function status */
  2642. return status;
  2643. }
  2644. /**
  2645. * @brief This function allows to really stop injected conversion.
  2646. * @param hsdadc SDADC handle.
  2647. * @retval HAL status.
  2648. */
  2649. static HAL_StatusTypeDef SDADC_InjConvStop(SDADC_HandleTypeDef* hsdadc)
  2650. {
  2651. uint32_t tickstart;
  2652. __IO uint32_t dummy_read_for_register_reset;
  2653. /* Check continuous mode */
  2654. if(hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_ON)
  2655. {
  2656. /* Clear JEOCF by reading SDADC_JDATAR register */
  2657. dummy_read_for_register_reset = hsdadc->Instance->JDATAR;
  2658. UNUSED(dummy_read_for_register_reset);
  2659. /* Clear JCONT bit in SDADC_CR2 register */
  2660. hsdadc->Instance->CR2 &= ~(SDADC_CR2_JCONT);
  2661. }
  2662. /* Wait for the end of injected conversion */
  2663. tickstart = HAL_GetTick();
  2664. while((hsdadc->Instance->ISR & SDADC_ISR_JCIP) != 0UL)
  2665. {
  2666. if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
  2667. {
  2668. /* Set SDADC in error state and return timeout status */
  2669. hsdadc->State = HAL_SDADC_STATE_ERROR;
  2670. return HAL_TIMEOUT;
  2671. }
  2672. }
  2673. /* Check if trigger is not software */
  2674. if(hsdadc->InjectedTrigger != SDADC_SOFTWARE_TRIGGER)
  2675. {
  2676. /* Enter init mode */
  2677. if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
  2678. {
  2679. /* Set SDADC in error state and return timeout status */
  2680. hsdadc->State = HAL_SDADC_STATE_ERROR;
  2681. return HAL_TIMEOUT;
  2682. }
  2683. else
  2684. {
  2685. /* Check if trigger is synchronous */
  2686. if(hsdadc->InjectedTrigger == SDADC_SYNCHRONOUS_TRIGGER)
  2687. {
  2688. /* Clear JSYNC bit in SDADC_CR1 register */
  2689. hsdadc->Instance->CR1 &= ~(SDADC_CR1_JSYNC);
  2690. }
  2691. else /* external trigger */
  2692. {
  2693. /* Clear JEXTEN[1:0] bits in SDADC_CR2 register */
  2694. hsdadc->Instance->CR2 &= ~(SDADC_CR2_JEXTEN);
  2695. }
  2696. /* Exit init mode */
  2697. SDADC_ExitInitMode(hsdadc);
  2698. }
  2699. }
  2700. /* Check if continuous mode */
  2701. if(hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_ON)
  2702. {
  2703. /* Restore JCONT bit in SDADC_CR2 register */
  2704. hsdadc->Instance->CR2 |= SDADC_CR2_JCONT;
  2705. }
  2706. /* Clear JEOCF by reading SDADC_JDATAR register */
  2707. dummy_read_for_register_reset = hsdadc->Instance->JDATAR;
  2708. UNUSED(dummy_read_for_register_reset);
  2709. /* Set CLRJOVRF bit in SDADC_CLRISR register */
  2710. hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
  2711. /* Update SDADC state */
  2712. hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
  2713. HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
  2714. /* Return function status */
  2715. return HAL_OK;
  2716. }
  2717. /**
  2718. * @}
  2719. */
  2720. /**
  2721. * @}
  2722. */
  2723. /**
  2724. * @}
  2725. */
  2726. #endif /* SDADC1 || SDADC2 || SDADC3 */
  2727. #endif /* HAL_SDADC_MODULE_ENABLED */
  2728. /**
  2729. * @}
  2730. */