stm32l4xx_hal_dfsdm.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_dfsdm.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Digital Filter for Sigma-Delta Modulators
  7. * (DFSDM) peripherals:
  8. * + Initialization and configuration of channels and filters
  9. * + Regular channels configuration
  10. * + Injected channels configuration
  11. * + Regular/Injected Channels DMA Configuration
  12. * + Interrupts and flags management
  13. * + Analog watchdog feature
  14. * + Short-circuit detector feature
  15. * + Extremes detector feature
  16. * + Clock absence detector feature
  17. * + Break generation on analog watchdog or short-circuit event
  18. *
  19. ******************************************************************************
  20. * @attention
  21. *
  22. * Copyright (c) 2017 STMicroelectronics.
  23. * All rights reserved.
  24. *
  25. * This software is licensed under terms that can be found in the LICENSE file
  26. * in the root directory of this software component.
  27. * If no LICENSE file comes with this software, it is provided AS-IS.
  28. *
  29. ******************************************************************************
  30. @verbatim
  31. ==============================================================================
  32. ##### How to use this driver #####
  33. ==============================================================================
  34. [..]
  35. *** Channel initialization ***
  36. ==============================
  37. [..]
  38. (#) User has first to initialize channels (before filters initialization).
  39. (#) As prerequisite, fill in the HAL_DFSDM_ChannelMspInit() :
  40. (++) Enable DFSDMz clock interface with __HAL_RCC_DFSDMz_CLK_ENABLE().
  41. (++) Enable the clocks for the DFSDMz GPIOS with __HAL_RCC_GPIOx_CLK_ENABLE().
  42. (++) Configure these DFSDMz pins in alternate mode using HAL_GPIO_Init().
  43. (++) If interrupt mode is used, enable and configure DFSDMz_FLT0 global
  44. interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
  45. (#) Configure the output clock, input, serial interface, analog watchdog,
  46. offset and data right bit shift parameters for this channel using the
  47. HAL_DFSDM_ChannelInit() function.
  48. *** Channel clock absence detector ***
  49. ======================================
  50. [..]
  51. (#) Start clock absence detector using HAL_DFSDM_ChannelCkabStart() or
  52. HAL_DFSDM_ChannelCkabStart_IT().
  53. (#) In polling mode, use HAL_DFSDM_ChannelPollForCkab() to detect the clock
  54. absence.
  55. (#) In interrupt mode, HAL_DFSDM_ChannelCkabCallback() will be called if
  56. clock absence is detected.
  57. (#) Stop clock absence detector using HAL_DFSDM_ChannelCkabStop() or
  58. HAL_DFSDM_ChannelCkabStop_IT().
  59. (#) Please note that the same mode (polling or interrupt) has to be used
  60. for all channels because the channels are sharing the same interrupt.
  61. (#) Please note also that in interrupt mode, if clock absence detector is
  62. stopped for one channel, interrupt will be disabled for all channels.
  63. *** Channel short circuit detector ***
  64. ======================================
  65. [..]
  66. (#) Start short circuit detector using HAL_DFSDM_ChannelScdStart() or
  67. or HAL_DFSDM_ChannelScdStart_IT().
  68. (#) In polling mode, use HAL_DFSDM_ChannelPollForScd() to detect short
  69. circuit.
  70. (#) In interrupt mode, HAL_DFSDM_ChannelScdCallback() will be called if
  71. short circuit is detected.
  72. (#) Stop short circuit detector using HAL_DFSDM_ChannelScdStop() or
  73. or HAL_DFSDM_ChannelScdStop_IT().
  74. (#) Please note that the same mode (polling or interrupt) has to be used
  75. for all channels because the channels are sharing the same interrupt.
  76. (#) Please note also that in interrupt mode, if short circuit detector is
  77. stopped for one channel, interrupt will be disabled for all channels.
  78. *** Channel analog watchdog value ***
  79. =====================================
  80. [..]
  81. (#) Get analog watchdog filter value of a channel using
  82. HAL_DFSDM_ChannelGetAwdValue().
  83. *** Channel offset value ***
  84. =====================================
  85. [..]
  86. (#) Modify offset value of a channel using HAL_DFSDM_ChannelModifyOffset().
  87. *** Filter initialization ***
  88. =============================
  89. [..]
  90. (#) After channel initialization, user has to init filters.
  91. (#) As prerequisite, fill in the HAL_DFSDM_FilterMspInit() :
  92. (++) If interrupt mode is used , enable and configure DFSDMz_FLTx global
  93. interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
  94. Please note that DFSDMz_FLT0 global interrupt could be already
  95. enabled if interrupt is used for channel.
  96. (++) If DMA mode is used, configure DMA with HAL_DMA_Init() and link it
  97. with DFSDMz filter handle using __HAL_LINKDMA().
  98. (#) Configure the regular conversion, injected conversion and filter
  99. parameters for this filter using the HAL_DFSDM_FilterInit() function.
  100. *** Filter regular channel conversion ***
  101. =========================================
  102. [..]
  103. (#) Select regular channel and enable/disable continuous mode using
  104. HAL_DFSDM_FilterConfigRegChannel().
  105. (#) Start regular conversion using HAL_DFSDM_FilterRegularStart(),
  106. HAL_DFSDM_FilterRegularStart_IT(), HAL_DFSDM_FilterRegularStart_DMA() or
  107. HAL_DFSDM_FilterRegularMsbStart_DMA().
  108. (#) In polling mode, use HAL_DFSDM_FilterPollForRegConversion() to detect
  109. the end of regular conversion.
  110. (#) In interrupt mode, HAL_DFSDM_FilterRegConvCpltCallback() will be called
  111. at the end of regular conversion.
  112. (#) Get value of regular conversion and corresponding channel using
  113. HAL_DFSDM_FilterGetRegularValue().
  114. (#) In DMA mode, HAL_DFSDM_FilterRegConvHalfCpltCallback() and
  115. HAL_DFSDM_FilterRegConvCpltCallback() will be called respectively at the
  116. half transfer and at the transfer complete. Please note that
  117. HAL_DFSDM_FilterRegConvHalfCpltCallback() will be called only in DMA
  118. circular mode.
  119. (#) Stop regular conversion using HAL_DFSDM_FilterRegularStop(),
  120. HAL_DFSDM_FilterRegularStop_IT() or HAL_DFSDM_FilterRegularStop_DMA().
  121. *** Filter injected channels conversion ***
  122. ===========================================
  123. [..]
  124. (#) Select injected channels using HAL_DFSDM_FilterConfigInjChannel().
  125. (#) Start injected conversion using HAL_DFSDM_FilterInjectedStart(),
  126. HAL_DFSDM_FilterInjectedStart_IT(), HAL_DFSDM_FilterInjectedStart_DMA() or
  127. HAL_DFSDM_FilterInjectedMsbStart_DMA().
  128. (#) In polling mode, use HAL_DFSDM_FilterPollForInjConversion() to detect
  129. the end of injected conversion.
  130. (#) In interrupt mode, HAL_DFSDM_FilterInjConvCpltCallback() will be called
  131. at the end of injected conversion.
  132. (#) Get value of injected conversion and corresponding channel using
  133. HAL_DFSDM_FilterGetInjectedValue().
  134. (#) In DMA mode, HAL_DFSDM_FilterInjConvHalfCpltCallback() and
  135. HAL_DFSDM_FilterInjConvCpltCallback() will be called respectively at the
  136. half transfer and at the transfer complete. Please note that
  137. HAL_DFSDM_FilterInjConvCpltCallback() will be called only in DMA
  138. circular mode.
  139. (#) Stop injected conversion using HAL_DFSDM_FilterInjectedStop(),
  140. HAL_DFSDM_FilterInjectedStop_IT() or HAL_DFSDM_FilterInjectedStop_DMA().
  141. *** Filter analog watchdog ***
  142. ==============================
  143. [..]
  144. (#) Start filter analog watchdog using HAL_DFSDM_FilterAwdStart_IT().
  145. (#) HAL_DFSDM_FilterAwdCallback() will be called if analog watchdog occurs.
  146. (#) Stop filter analog watchdog using HAL_DFSDM_FilterAwdStop_IT().
  147. *** Filter extreme detector ***
  148. ===============================
  149. [..]
  150. (#) Start filter extreme detector using HAL_DFSDM_FilterExdStart().
  151. (#) Get extreme detector maximum value using HAL_DFSDM_FilterGetExdMaxValue().
  152. (#) Get extreme detector minimum value using HAL_DFSDM_FilterGetExdMinValue().
  153. (#) Start filter extreme detector using HAL_DFSDM_FilterExdStop().
  154. *** Filter conversion time ***
  155. ==============================
  156. [..]
  157. (#) Get conversion time value using HAL_DFSDM_FilterGetConvTimeValue().
  158. *** Callback registration ***
  159. =============================
  160. [..]
  161. The compilation define USE_HAL_DFSDM_REGISTER_CALLBACKS when set to 1
  162. allows the user to configure dynamically the driver callbacks.
  163. Use functions HAL_DFSDM_Channel_RegisterCallback(),
  164. HAL_DFSDM_Filter_RegisterCallback() or
  165. HAL_DFSDM_Filter_RegisterAwdCallback() to register a user callback.
  166. [..]
  167. Function HAL_DFSDM_Channel_RegisterCallback() allows to register
  168. following callbacks:
  169. (+) CkabCallback : DFSDM channel clock absence detection callback.
  170. (+) ScdCallback : DFSDM channel short circuit detection callback.
  171. (+) MspInitCallback : DFSDM channel MSP init callback.
  172. (+) MspDeInitCallback : DFSDM channel MSP de-init callback.
  173. [..]
  174. This function takes as parameters the HAL peripheral handle, the Callback ID
  175. and a pointer to the user callback function.
  176. [..]
  177. Function HAL_DFSDM_Filter_RegisterCallback() allows to register
  178. following callbacks:
  179. (+) RegConvCpltCallback : DFSDM filter regular conversion complete callback.
  180. (+) RegConvHalfCpltCallback : DFSDM filter half regular conversion complete callback.
  181. (+) InjConvCpltCallback : DFSDM filter injected conversion complete callback.
  182. (+) InjConvHalfCpltCallback : DFSDM filter half injected conversion complete callback.
  183. (+) ErrorCallback : DFSDM filter error callback.
  184. (+) MspInitCallback : DFSDM filter MSP init callback.
  185. (+) MspDeInitCallback : DFSDM filter MSP de-init callback.
  186. [..]
  187. This function takes as parameters the HAL peripheral handle, the Callback ID
  188. and a pointer to the user callback function.
  189. [..]
  190. For specific DFSDM filter analog watchdog callback use dedicated register callback:
  191. HAL_DFSDM_Filter_RegisterAwdCallback().
  192. [..]
  193. Use functions HAL_DFSDM_Channel_UnRegisterCallback() or
  194. HAL_DFSDM_Filter_UnRegisterCallback() to reset a callback to the default
  195. weak function.
  196. [..]
  197. HAL_DFSDM_Channel_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  198. and the Callback ID.
  199. [..]
  200. This function allows to reset following callbacks:
  201. (+) CkabCallback : DFSDM channel clock absence detection callback.
  202. (+) ScdCallback : DFSDM channel short circuit detection callback.
  203. (+) MspInitCallback : DFSDM channel MSP init callback.
  204. (+) MspDeInitCallback : DFSDM channel MSP de-init callback.
  205. [..]
  206. HAL_DFSDM_Filter_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  207. and the Callback ID.
  208. [..]
  209. This function allows to reset following callbacks:
  210. (+) RegConvCpltCallback : DFSDM filter regular conversion complete callback.
  211. (+) RegConvHalfCpltCallback : DFSDM filter half regular conversion complete callback.
  212. (+) InjConvCpltCallback : DFSDM filter injected conversion complete callback.
  213. (+) InjConvHalfCpltCallback : DFSDM filter half injected conversion complete callback.
  214. (+) ErrorCallback : DFSDM filter error callback.
  215. (+) MspInitCallback : DFSDM filter MSP init callback.
  216. (+) MspDeInitCallback : DFSDM filter MSP de-init callback.
  217. [..]
  218. For specific DFSDM filter analog watchdog callback use dedicated unregister callback:
  219. HAL_DFSDM_Filter_UnRegisterAwdCallback().
  220. [..]
  221. By default, after the call of init function and if the state is RESET
  222. all callbacks are reset to the corresponding legacy weak functions:
  223. examples HAL_DFSDM_ChannelScdCallback(), HAL_DFSDM_FilterErrorCallback().
  224. Exception done for MspInit and MspDeInit callbacks that are respectively
  225. reset to the legacy weak functions in the init and de-init only when these
  226. callbacks are null (not registered beforehand).
  227. If not, MspInit or MspDeInit are not null, the init and de-init keep and use
  228. the user MspInit/MspDeInit callbacks (registered beforehand)
  229. [..]
  230. Callbacks can be registered/unregistered in READY state only.
  231. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  232. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  233. during the init/de-init.
  234. In that case first register the MspInit/MspDeInit user callbacks using
  235. HAL_DFSDM_Channel_RegisterCallback() or
  236. HAL_DFSDM_Filter_RegisterCallback() before calling init or de-init function.
  237. [..]
  238. When The compilation define USE_HAL_DFSDM_REGISTER_CALLBACKS is set to 0 or
  239. not defined, the callback registering feature is not available
  240. and weak callbacks are used.
  241. @endverbatim
  242. ******************************************************************************
  243. */
  244. /* Includes ------------------------------------------------------------------*/
  245. #include "stm32l4xx_hal.h"
  246. /** @addtogroup STM32L4xx_HAL_Driver
  247. * @{
  248. */
  249. #ifdef HAL_DFSDM_MODULE_ENABLED
  250. #if defined(STM32L451xx) || defined(STM32L452xx) || defined(STM32L462xx) || \
  251. defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || \
  252. defined(STM32L496xx) || defined(STM32L4A6xx) || \
  253. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) || \
  254. defined(STM32L4P5xx) || defined(STM32L4Q5xx)
  255. /** @defgroup DFSDM DFSDM
  256. * @brief DFSDM HAL driver module
  257. * @{
  258. */
  259. /* Private typedef -----------------------------------------------------------*/
  260. /* Private define ------------------------------------------------------------*/
  261. /** @defgroup DFSDM_Private_Define DFSDM Private Define
  262. * @{
  263. */
  264. #define DFSDM_FLTCR1_MSB_RCH_OFFSET 8
  265. #define DFSDM_MSB_MASK 0xFFFF0000U
  266. #define DFSDM_LSB_MASK 0x0000FFFFU
  267. #define DFSDM_CKAB_TIMEOUT 5000U
  268. #if defined(STM32L451xx) || defined(STM32L452xx) || defined(STM32L462xx) || \
  269. defined(STM32L4P5xx) || defined(STM32L4Q5xx)
  270. #define DFSDM1_CHANNEL_NUMBER 4U
  271. #else /* STM32L451xx || STM32L452xx || STM32L462xx || STM32L4P5xx || STM32L4Q5xx */
  272. #define DFSDM1_CHANNEL_NUMBER 8U
  273. #endif /* STM32L451xx || STM32L452xx || STM32L462xx || STM32L4P5xx || STM32L4Q5xx */
  274. /**
  275. * @}
  276. */
  277. /* Private macro -------------------------------------------------------------*/
  278. /* Private variables ---------------------------------------------------------*/
  279. /** @defgroup DFSDM_Private_Variables DFSDM Private Variables
  280. * @{
  281. */
  282. static __IO uint32_t v_dfsdm1ChannelCounter = 0;
  283. static DFSDM_Channel_HandleTypeDef *a_dfsdm1ChannelHandle[DFSDM1_CHANNEL_NUMBER] = {NULL};
  284. /**
  285. * @}
  286. */
  287. /* Private function prototypes -----------------------------------------------*/
  288. /** @defgroup DFSDM_Private_Functions DFSDM Private Functions
  289. * @{
  290. */
  291. static uint32_t DFSDM_GetInjChannelsNbr(uint32_t Channels);
  292. static uint32_t DFSDM_GetChannelFromInstance(const DFSDM_Channel_TypeDef *Instance);
  293. static void DFSDM_RegConvStart(DFSDM_Filter_HandleTypeDef *hdfsdm_filter);
  294. static void DFSDM_RegConvStop(DFSDM_Filter_HandleTypeDef *hdfsdm_filter);
  295. static void DFSDM_InjConvStart(DFSDM_Filter_HandleTypeDef *hdfsdm_filter);
  296. static void DFSDM_InjConvStop(DFSDM_Filter_HandleTypeDef *hdfsdm_filter);
  297. static void DFSDM_DMARegularHalfConvCplt(DMA_HandleTypeDef *hdma);
  298. static void DFSDM_DMARegularConvCplt(DMA_HandleTypeDef *hdma);
  299. static void DFSDM_DMAInjectedHalfConvCplt(DMA_HandleTypeDef *hdma);
  300. static void DFSDM_DMAInjectedConvCplt(DMA_HandleTypeDef *hdma);
  301. static void DFSDM_DMAError(DMA_HandleTypeDef *hdma);
  302. /**
  303. * @}
  304. */
  305. /* Exported functions --------------------------------------------------------*/
  306. /** @defgroup DFSDM_Exported_Functions DFSDM Exported Functions
  307. * @{
  308. */
  309. /** @defgroup DFSDM_Exported_Functions_Group1_Channel Channel initialization and de-initialization functions
  310. * @brief Channel initialization and de-initialization functions
  311. *
  312. @verbatim
  313. ==============================================================================
  314. ##### Channel initialization and de-initialization functions #####
  315. ==============================================================================
  316. [..] This section provides functions allowing to:
  317. (+) Initialize the DFSDM channel.
  318. (+) De-initialize the DFSDM channel.
  319. @endverbatim
  320. * @{
  321. */
  322. /**
  323. * @brief Initialize the DFSDM channel according to the specified parameters
  324. * in the DFSDM_ChannelInitTypeDef structure and initialize the associated handle.
  325. * @param hdfsdm_channel DFSDM channel handle.
  326. * @retval HAL status.
  327. */
  328. HAL_StatusTypeDef HAL_DFSDM_ChannelInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  329. {
  330. /* Check DFSDM Channel handle */
  331. if (hdfsdm_channel == NULL)
  332. {
  333. return HAL_ERROR;
  334. }
  335. /* Check parameters */
  336. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  337. assert_param(IS_FUNCTIONAL_STATE(hdfsdm_channel->Init.OutputClock.Activation));
  338. assert_param(IS_DFSDM_CHANNEL_INPUT(hdfsdm_channel->Init.Input.Multiplexer));
  339. assert_param(IS_DFSDM_CHANNEL_DATA_PACKING(hdfsdm_channel->Init.Input.DataPacking));
  340. assert_param(IS_DFSDM_CHANNEL_INPUT_PINS(hdfsdm_channel->Init.Input.Pins));
  341. assert_param(IS_DFSDM_CHANNEL_SERIAL_INTERFACE_TYPE(hdfsdm_channel->Init.SerialInterface.Type));
  342. assert_param(IS_DFSDM_CHANNEL_SPI_CLOCK(hdfsdm_channel->Init.SerialInterface.SpiClock));
  343. assert_param(IS_DFSDM_CHANNEL_FILTER_ORDER(hdfsdm_channel->Init.Awd.FilterOrder));
  344. assert_param(IS_DFSDM_CHANNEL_FILTER_OVS_RATIO(hdfsdm_channel->Init.Awd.Oversampling));
  345. assert_param(IS_DFSDM_CHANNEL_OFFSET(hdfsdm_channel->Init.Offset));
  346. assert_param(IS_DFSDM_CHANNEL_RIGHT_BIT_SHIFT(hdfsdm_channel->Init.RightBitShift));
  347. /* Check that channel has not been already initialized */
  348. if (a_dfsdm1ChannelHandle[DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance)] != NULL)
  349. {
  350. return HAL_ERROR;
  351. }
  352. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  353. /* Reset callback pointers to the weak predefined callbacks */
  354. hdfsdm_channel->CkabCallback = HAL_DFSDM_ChannelCkabCallback;
  355. hdfsdm_channel->ScdCallback = HAL_DFSDM_ChannelScdCallback;
  356. /* Call MSP init function */
  357. if (hdfsdm_channel->MspInitCallback == NULL)
  358. {
  359. hdfsdm_channel->MspInitCallback = HAL_DFSDM_ChannelMspInit;
  360. }
  361. hdfsdm_channel->MspInitCallback(hdfsdm_channel);
  362. #else
  363. /* Call MSP init function */
  364. HAL_DFSDM_ChannelMspInit(hdfsdm_channel);
  365. #endif
  366. /* Update the channel counter */
  367. v_dfsdm1ChannelCounter++;
  368. /* Configure output serial clock and enable global DFSDM interface only for first channel */
  369. if (v_dfsdm1ChannelCounter == 1U)
  370. {
  371. assert_param(IS_DFSDM_CHANNEL_OUTPUT_CLOCK(hdfsdm_channel->Init.OutputClock.Selection));
  372. /* Set the output serial clock source */
  373. DFSDM1_Channel0->CHCFGR1 &= ~(DFSDM_CHCFGR1_CKOUTSRC);
  374. DFSDM1_Channel0->CHCFGR1 |= hdfsdm_channel->Init.OutputClock.Selection;
  375. /* Reset clock divider */
  376. DFSDM1_Channel0->CHCFGR1 &= ~(DFSDM_CHCFGR1_CKOUTDIV);
  377. if (hdfsdm_channel->Init.OutputClock.Activation == ENABLE)
  378. {
  379. assert_param(IS_DFSDM_CHANNEL_OUTPUT_CLOCK_DIVIDER(hdfsdm_channel->Init.OutputClock.Divider));
  380. /* Set the output clock divider */
  381. DFSDM1_Channel0->CHCFGR1 |= (uint32_t)((hdfsdm_channel->Init.OutputClock.Divider - 1U) <<
  382. DFSDM_CHCFGR1_CKOUTDIV_Pos);
  383. }
  384. /* enable the DFSDM global interface */
  385. DFSDM1_Channel0->CHCFGR1 |= DFSDM_CHCFGR1_DFSDMEN;
  386. }
  387. /* Set channel input parameters */
  388. hdfsdm_channel->Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_DATPACK | DFSDM_CHCFGR1_DATMPX |
  389. DFSDM_CHCFGR1_CHINSEL);
  390. hdfsdm_channel->Instance->CHCFGR1 |= (hdfsdm_channel->Init.Input.Multiplexer |
  391. hdfsdm_channel->Init.Input.DataPacking |
  392. hdfsdm_channel->Init.Input.Pins);
  393. /* Set serial interface parameters */
  394. hdfsdm_channel->Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_SITP | DFSDM_CHCFGR1_SPICKSEL);
  395. hdfsdm_channel->Instance->CHCFGR1 |= (hdfsdm_channel->Init.SerialInterface.Type |
  396. hdfsdm_channel->Init.SerialInterface.SpiClock);
  397. /* Set analog watchdog parameters */
  398. hdfsdm_channel->Instance->CHAWSCDR &= ~(DFSDM_CHAWSCDR_AWFORD | DFSDM_CHAWSCDR_AWFOSR);
  399. hdfsdm_channel->Instance->CHAWSCDR |= (hdfsdm_channel->Init.Awd.FilterOrder |
  400. ((hdfsdm_channel->Init.Awd.Oversampling - 1U) << DFSDM_CHAWSCDR_AWFOSR_Pos));
  401. /* Set channel offset and right bit shift */
  402. hdfsdm_channel->Instance->CHCFGR2 &= ~(DFSDM_CHCFGR2_OFFSET | DFSDM_CHCFGR2_DTRBS);
  403. hdfsdm_channel->Instance->CHCFGR2 |= (((uint32_t) hdfsdm_channel->Init.Offset << DFSDM_CHCFGR2_OFFSET_Pos) |
  404. (hdfsdm_channel->Init.RightBitShift << DFSDM_CHCFGR2_DTRBS_Pos));
  405. /* Enable DFSDM channel */
  406. hdfsdm_channel->Instance->CHCFGR1 |= DFSDM_CHCFGR1_CHEN;
  407. /* Set DFSDM Channel to ready state */
  408. hdfsdm_channel->State = HAL_DFSDM_CHANNEL_STATE_READY;
  409. /* Store channel handle in DFSDM channel handle table */
  410. a_dfsdm1ChannelHandle[DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance)] = hdfsdm_channel;
  411. return HAL_OK;
  412. }
  413. /**
  414. * @brief De-initialize the DFSDM channel.
  415. * @param hdfsdm_channel DFSDM channel handle.
  416. * @retval HAL status.
  417. */
  418. HAL_StatusTypeDef HAL_DFSDM_ChannelDeInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  419. {
  420. /* Check DFSDM Channel handle */
  421. if (hdfsdm_channel == NULL)
  422. {
  423. return HAL_ERROR;
  424. }
  425. /* Check parameters */
  426. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  427. /* Check that channel has not been already deinitialized */
  428. if (a_dfsdm1ChannelHandle[DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance)] == NULL)
  429. {
  430. return HAL_ERROR;
  431. }
  432. /* Disable the DFSDM channel */
  433. hdfsdm_channel->Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_CHEN);
  434. /* Update the channel counter */
  435. v_dfsdm1ChannelCounter--;
  436. /* Disable global DFSDM at deinit of last channel */
  437. if (v_dfsdm1ChannelCounter == 0U)
  438. {
  439. DFSDM1_Channel0->CHCFGR1 &= ~(DFSDM_CHCFGR1_DFSDMEN);
  440. }
  441. /* Call MSP deinit function */
  442. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  443. if (hdfsdm_channel->MspDeInitCallback == NULL)
  444. {
  445. hdfsdm_channel->MspDeInitCallback = HAL_DFSDM_ChannelMspDeInit;
  446. }
  447. hdfsdm_channel->MspDeInitCallback(hdfsdm_channel);
  448. #else
  449. HAL_DFSDM_ChannelMspDeInit(hdfsdm_channel);
  450. #endif
  451. /* Set DFSDM Channel in reset state */
  452. hdfsdm_channel->State = HAL_DFSDM_CHANNEL_STATE_RESET;
  453. /* Reset channel handle in DFSDM channel handle table */
  454. a_dfsdm1ChannelHandle[DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance)] = (DFSDM_Channel_HandleTypeDef *) NULL;
  455. return HAL_OK;
  456. }
  457. /**
  458. * @brief Initialize the DFSDM channel MSP.
  459. * @param hdfsdm_channel DFSDM channel handle.
  460. * @retval None
  461. */
  462. __weak void HAL_DFSDM_ChannelMspInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  463. {
  464. /* Prevent unused argument(s) compilation warning */
  465. UNUSED(hdfsdm_channel);
  466. /* NOTE : This function should not be modified, when the function is needed,
  467. the HAL_DFSDM_ChannelMspInit could be implemented in the user file.
  468. */
  469. }
  470. /**
  471. * @brief De-initialize the DFSDM channel MSP.
  472. * @param hdfsdm_channel DFSDM channel handle.
  473. * @retval None
  474. */
  475. __weak void HAL_DFSDM_ChannelMspDeInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  476. {
  477. /* Prevent unused argument(s) compilation warning */
  478. UNUSED(hdfsdm_channel);
  479. /* NOTE : This function should not be modified, when the function is needed,
  480. the HAL_DFSDM_ChannelMspDeInit could be implemented in the user file.
  481. */
  482. }
  483. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  484. /**
  485. * @brief Register a user DFSDM channel callback
  486. * to be used instead of the weak predefined callback.
  487. * @param hdfsdm_channel DFSDM channel handle.
  488. * @param CallbackID ID of the callback to be registered.
  489. * This parameter can be one of the following values:
  490. * @arg @ref HAL_DFSDM_CHANNEL_CKAB_CB_ID clock absence detection callback ID.
  491. * @arg @ref HAL_DFSDM_CHANNEL_SCD_CB_ID short circuit detection callback ID.
  492. * @arg @ref HAL_DFSDM_CHANNEL_MSPINIT_CB_ID MSP init callback ID.
  493. * @arg @ref HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID MSP de-init callback ID.
  494. * @param pCallback pointer to the callback function.
  495. * @retval HAL status.
  496. */
  497. HAL_StatusTypeDef HAL_DFSDM_Channel_RegisterCallback(DFSDM_Channel_HandleTypeDef *hdfsdm_channel,
  498. HAL_DFSDM_Channel_CallbackIDTypeDef CallbackID,
  499. pDFSDM_Channel_CallbackTypeDef pCallback)
  500. {
  501. HAL_StatusTypeDef status = HAL_OK;
  502. if (pCallback == NULL)
  503. {
  504. /* update return status */
  505. status = HAL_ERROR;
  506. }
  507. else
  508. {
  509. if (HAL_DFSDM_CHANNEL_STATE_READY == hdfsdm_channel->State)
  510. {
  511. switch (CallbackID)
  512. {
  513. case HAL_DFSDM_CHANNEL_CKAB_CB_ID :
  514. hdfsdm_channel->CkabCallback = pCallback;
  515. break;
  516. case HAL_DFSDM_CHANNEL_SCD_CB_ID :
  517. hdfsdm_channel->ScdCallback = pCallback;
  518. break;
  519. case HAL_DFSDM_CHANNEL_MSPINIT_CB_ID :
  520. hdfsdm_channel->MspInitCallback = pCallback;
  521. break;
  522. case HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID :
  523. hdfsdm_channel->MspDeInitCallback = pCallback;
  524. break;
  525. default :
  526. /* update return status */
  527. status = HAL_ERROR;
  528. break;
  529. }
  530. }
  531. else if (HAL_DFSDM_CHANNEL_STATE_RESET == hdfsdm_channel->State)
  532. {
  533. switch (CallbackID)
  534. {
  535. case HAL_DFSDM_CHANNEL_MSPINIT_CB_ID :
  536. hdfsdm_channel->MspInitCallback = pCallback;
  537. break;
  538. case HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID :
  539. hdfsdm_channel->MspDeInitCallback = pCallback;
  540. break;
  541. default :
  542. /* update return status */
  543. status = HAL_ERROR;
  544. break;
  545. }
  546. }
  547. else
  548. {
  549. /* update return status */
  550. status = HAL_ERROR;
  551. }
  552. }
  553. return status;
  554. }
  555. /**
  556. * @brief Unregister a user DFSDM channel callback.
  557. * DFSDM channel callback is redirected to the weak predefined callback.
  558. * @param hdfsdm_channel DFSDM channel handle.
  559. * @param CallbackID ID of the callback to be unregistered.
  560. * This parameter can be one of the following values:
  561. * @arg @ref HAL_DFSDM_CHANNEL_CKAB_CB_ID clock absence detection callback ID.
  562. * @arg @ref HAL_DFSDM_CHANNEL_SCD_CB_ID short circuit detection callback ID.
  563. * @arg @ref HAL_DFSDM_CHANNEL_MSPINIT_CB_ID MSP init callback ID.
  564. * @arg @ref HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID MSP de-init callback ID.
  565. * @retval HAL status.
  566. */
  567. HAL_StatusTypeDef HAL_DFSDM_Channel_UnRegisterCallback(DFSDM_Channel_HandleTypeDef *hdfsdm_channel,
  568. HAL_DFSDM_Channel_CallbackIDTypeDef CallbackID)
  569. {
  570. HAL_StatusTypeDef status = HAL_OK;
  571. if (HAL_DFSDM_CHANNEL_STATE_READY == hdfsdm_channel->State)
  572. {
  573. switch (CallbackID)
  574. {
  575. case HAL_DFSDM_CHANNEL_CKAB_CB_ID :
  576. hdfsdm_channel->CkabCallback = HAL_DFSDM_ChannelCkabCallback;
  577. break;
  578. case HAL_DFSDM_CHANNEL_SCD_CB_ID :
  579. hdfsdm_channel->ScdCallback = HAL_DFSDM_ChannelScdCallback;
  580. break;
  581. case HAL_DFSDM_CHANNEL_MSPINIT_CB_ID :
  582. hdfsdm_channel->MspInitCallback = HAL_DFSDM_ChannelMspInit;
  583. break;
  584. case HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID :
  585. hdfsdm_channel->MspDeInitCallback = HAL_DFSDM_ChannelMspDeInit;
  586. break;
  587. default :
  588. /* update return status */
  589. status = HAL_ERROR;
  590. break;
  591. }
  592. }
  593. else if (HAL_DFSDM_CHANNEL_STATE_RESET == hdfsdm_channel->State)
  594. {
  595. switch (CallbackID)
  596. {
  597. case HAL_DFSDM_CHANNEL_MSPINIT_CB_ID :
  598. hdfsdm_channel->MspInitCallback = HAL_DFSDM_ChannelMspInit;
  599. break;
  600. case HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID :
  601. hdfsdm_channel->MspDeInitCallback = HAL_DFSDM_ChannelMspDeInit;
  602. break;
  603. default :
  604. /* update return status */
  605. status = HAL_ERROR;
  606. break;
  607. }
  608. }
  609. else
  610. {
  611. /* update return status */
  612. status = HAL_ERROR;
  613. }
  614. return status;
  615. }
  616. #endif /* USE_HAL_DFSDM_REGISTER_CALLBACKS */
  617. /**
  618. * @}
  619. */
  620. /** @defgroup DFSDM_Exported_Functions_Group2_Channel Channel operation functions
  621. * @brief Channel operation functions
  622. *
  623. @verbatim
  624. ==============================================================================
  625. ##### Channel operation functions #####
  626. ==============================================================================
  627. [..] This section provides functions allowing to:
  628. (+) Manage clock absence detector feature.
  629. (+) Manage short circuit detector feature.
  630. (+) Get analog watchdog value.
  631. (+) Modify offset value.
  632. @endverbatim
  633. * @{
  634. */
  635. /**
  636. * @brief This function allows to start clock absence detection in polling mode.
  637. * @note Same mode has to be used for all channels.
  638. * @note If clock is not available on this channel during 5 seconds,
  639. * clock absence detection will not be activated and function
  640. * will return HAL_TIMEOUT error.
  641. * @param hdfsdm_channel DFSDM channel handle.
  642. * @retval HAL status
  643. */
  644. HAL_StatusTypeDef HAL_DFSDM_ChannelCkabStart(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  645. {
  646. HAL_StatusTypeDef status = HAL_OK;
  647. uint32_t channel;
  648. uint32_t tickstart;
  649. /* Check parameters */
  650. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  651. /* Check DFSDM channel state */
  652. if (hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
  653. {
  654. /* Return error status */
  655. status = HAL_ERROR;
  656. }
  657. else
  658. {
  659. /* Get channel number from channel instance */
  660. channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
  661. /* Get timeout */
  662. tickstart = HAL_GetTick();
  663. /* Clear clock absence flag */
  664. while ((((DFSDM1_Filter0->FLTISR & DFSDM_FLTISR_CKABF) >> (DFSDM_FLTISR_CKABF_Pos + channel)) & 1U) != 0U)
  665. {
  666. DFSDM1_Filter0->FLTICR = (1UL << (DFSDM_FLTICR_CLRCKABF_Pos + channel));
  667. /* Check the Timeout */
  668. if ((HAL_GetTick() - tickstart) > DFSDM_CKAB_TIMEOUT)
  669. {
  670. /* Set timeout status */
  671. status = HAL_TIMEOUT;
  672. break;
  673. }
  674. }
  675. if (status == HAL_OK)
  676. {
  677. /* Start clock absence detection */
  678. hdfsdm_channel->Instance->CHCFGR1 |= DFSDM_CHCFGR1_CKABEN;
  679. }
  680. }
  681. /* Return function status */
  682. return status;
  683. }
  684. /**
  685. * @brief This function allows to poll for the clock absence detection.
  686. * @param hdfsdm_channel DFSDM channel handle.
  687. * @param Timeout Timeout value in milliseconds.
  688. * @retval HAL status
  689. */
  690. HAL_StatusTypeDef HAL_DFSDM_ChannelPollForCkab(const DFSDM_Channel_HandleTypeDef *hdfsdm_channel,
  691. uint32_t Timeout)
  692. {
  693. uint32_t tickstart;
  694. uint32_t channel;
  695. /* Check parameters */
  696. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  697. /* Check DFSDM channel state */
  698. if (hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
  699. {
  700. /* Return error status */
  701. return HAL_ERROR;
  702. }
  703. else
  704. {
  705. /* Get channel number from channel instance */
  706. channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
  707. /* Get timeout */
  708. tickstart = HAL_GetTick();
  709. /* Wait clock absence detection */
  710. while ((((DFSDM1_Filter0->FLTISR & DFSDM_FLTISR_CKABF) >> (DFSDM_FLTISR_CKABF_Pos + channel)) & 1U) == 0U)
  711. {
  712. /* Check the Timeout */
  713. if (Timeout != HAL_MAX_DELAY)
  714. {
  715. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  716. {
  717. /* Return timeout status */
  718. return HAL_TIMEOUT;
  719. }
  720. }
  721. }
  722. /* Clear clock absence detection flag */
  723. DFSDM1_Filter0->FLTICR = (1UL << (DFSDM_FLTICR_CLRCKABF_Pos + channel));
  724. /* Return function status */
  725. return HAL_OK;
  726. }
  727. }
  728. /**
  729. * @brief This function allows to stop clock absence detection in polling mode.
  730. * @param hdfsdm_channel DFSDM channel handle.
  731. * @retval HAL status
  732. */
  733. HAL_StatusTypeDef HAL_DFSDM_ChannelCkabStop(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  734. {
  735. HAL_StatusTypeDef status = HAL_OK;
  736. uint32_t channel;
  737. /* Check parameters */
  738. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  739. /* Check DFSDM channel state */
  740. if (hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
  741. {
  742. /* Return error status */
  743. status = HAL_ERROR;
  744. }
  745. else
  746. {
  747. /* Stop clock absence detection */
  748. hdfsdm_channel->Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_CKABEN);
  749. /* Clear clock absence flag */
  750. channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
  751. DFSDM1_Filter0->FLTICR = (1UL << (DFSDM_FLTICR_CLRCKABF_Pos + channel));
  752. }
  753. /* Return function status */
  754. return status;
  755. }
  756. /**
  757. * @brief This function allows to start clock absence detection in interrupt mode.
  758. * @note Same mode has to be used for all channels.
  759. * @note If clock is not available on this channel during 5 seconds,
  760. * clock absence detection will not be activated and function
  761. * will return HAL_TIMEOUT error.
  762. * @param hdfsdm_channel DFSDM channel handle.
  763. * @retval HAL status
  764. */
  765. HAL_StatusTypeDef HAL_DFSDM_ChannelCkabStart_IT(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  766. {
  767. HAL_StatusTypeDef status = HAL_OK;
  768. uint32_t channel;
  769. uint32_t tickstart;
  770. /* Check parameters */
  771. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  772. /* Check DFSDM channel state */
  773. if (hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
  774. {
  775. /* Return error status */
  776. status = HAL_ERROR;
  777. }
  778. else
  779. {
  780. /* Get channel number from channel instance */
  781. channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
  782. /* Get timeout */
  783. tickstart = HAL_GetTick();
  784. /* Clear clock absence flag */
  785. while ((((DFSDM1_Filter0->FLTISR & DFSDM_FLTISR_CKABF) >> (DFSDM_FLTISR_CKABF_Pos + channel)) & 1U) != 0U)
  786. {
  787. DFSDM1_Filter0->FLTICR = (1UL << (DFSDM_FLTICR_CLRCKABF_Pos + channel));
  788. /* Check the Timeout */
  789. if ((HAL_GetTick() - tickstart) > DFSDM_CKAB_TIMEOUT)
  790. {
  791. /* Set timeout status */
  792. status = HAL_TIMEOUT;
  793. break;
  794. }
  795. }
  796. if (status == HAL_OK)
  797. {
  798. /* Activate clock absence detection interrupt */
  799. DFSDM1_Filter0->FLTCR2 |= DFSDM_FLTCR2_CKABIE;
  800. /* Start clock absence detection */
  801. hdfsdm_channel->Instance->CHCFGR1 |= DFSDM_CHCFGR1_CKABEN;
  802. }
  803. }
  804. /* Return function status */
  805. return status;
  806. }
  807. /**
  808. * @brief Clock absence detection callback.
  809. * @param hdfsdm_channel DFSDM channel handle.
  810. * @retval None
  811. */
  812. __weak void HAL_DFSDM_ChannelCkabCallback(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  813. {
  814. /* Prevent unused argument(s) compilation warning */
  815. UNUSED(hdfsdm_channel);
  816. /* NOTE : This function should not be modified, when the callback is needed,
  817. the HAL_DFSDM_ChannelCkabCallback could be implemented in the user file
  818. */
  819. }
  820. /**
  821. * @brief This function allows to stop clock absence detection in interrupt mode.
  822. * @note Interrupt will be disabled for all channels
  823. * @param hdfsdm_channel DFSDM channel handle.
  824. * @retval HAL status
  825. */
  826. HAL_StatusTypeDef HAL_DFSDM_ChannelCkabStop_IT(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  827. {
  828. HAL_StatusTypeDef status = HAL_OK;
  829. uint32_t channel;
  830. /* Check parameters */
  831. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  832. /* Check DFSDM channel state */
  833. if (hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
  834. {
  835. /* Return error status */
  836. status = HAL_ERROR;
  837. }
  838. else
  839. {
  840. /* Stop clock absence detection */
  841. hdfsdm_channel->Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_CKABEN);
  842. /* Clear clock absence flag */
  843. channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
  844. DFSDM1_Filter0->FLTICR = (1UL << (DFSDM_FLTICR_CLRCKABF_Pos + channel));
  845. /* Disable clock absence detection interrupt */
  846. DFSDM1_Filter0->FLTCR2 &= ~(DFSDM_FLTCR2_CKABIE);
  847. }
  848. /* Return function status */
  849. return status;
  850. }
  851. /**
  852. * @brief This function allows to start short circuit detection in polling mode.
  853. * @note Same mode has to be used for all channels
  854. * @param hdfsdm_channel DFSDM channel handle.
  855. * @param Threshold Short circuit detector threshold.
  856. * This parameter must be a number between Min_Data = 0 and Max_Data = 255.
  857. * @param BreakSignal Break signals assigned to short circuit event.
  858. * This parameter can be a values combination of @ref DFSDM_BreakSignals.
  859. * @retval HAL status
  860. */
  861. HAL_StatusTypeDef HAL_DFSDM_ChannelScdStart(DFSDM_Channel_HandleTypeDef *hdfsdm_channel,
  862. uint32_t Threshold,
  863. uint32_t BreakSignal)
  864. {
  865. HAL_StatusTypeDef status = HAL_OK;
  866. /* Check parameters */
  867. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  868. assert_param(IS_DFSDM_CHANNEL_SCD_THRESHOLD(Threshold));
  869. assert_param(IS_DFSDM_BREAK_SIGNALS(BreakSignal));
  870. /* Check DFSDM channel state */
  871. if (hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
  872. {
  873. /* Return error status */
  874. status = HAL_ERROR;
  875. }
  876. else
  877. {
  878. /* Configure threshold and break signals */
  879. hdfsdm_channel->Instance->CHAWSCDR &= ~(DFSDM_CHAWSCDR_BKSCD | DFSDM_CHAWSCDR_SCDT);
  880. hdfsdm_channel->Instance->CHAWSCDR |= ((BreakSignal << DFSDM_CHAWSCDR_BKSCD_Pos) | \
  881. Threshold);
  882. /* Start short circuit detection */
  883. hdfsdm_channel->Instance->CHCFGR1 |= DFSDM_CHCFGR1_SCDEN;
  884. }
  885. /* Return function status */
  886. return status;
  887. }
  888. /**
  889. * @brief This function allows to poll for the short circuit detection.
  890. * @param hdfsdm_channel DFSDM channel handle.
  891. * @param Timeout Timeout value in milliseconds.
  892. * @retval HAL status
  893. */
  894. HAL_StatusTypeDef HAL_DFSDM_ChannelPollForScd(const DFSDM_Channel_HandleTypeDef *hdfsdm_channel,
  895. uint32_t Timeout)
  896. {
  897. uint32_t tickstart;
  898. uint32_t channel;
  899. /* Check parameters */
  900. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  901. /* Check DFSDM channel state */
  902. if (hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
  903. {
  904. /* Return error status */
  905. return HAL_ERROR;
  906. }
  907. else
  908. {
  909. /* Get channel number from channel instance */
  910. channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
  911. /* Get timeout */
  912. tickstart = HAL_GetTick();
  913. /* Wait short circuit detection */
  914. while (((DFSDM1_Filter0->FLTISR & DFSDM_FLTISR_SCDF) >> (DFSDM_FLTISR_SCDF_Pos + channel)) == 0U)
  915. {
  916. /* Check the Timeout */
  917. if (Timeout != HAL_MAX_DELAY)
  918. {
  919. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  920. {
  921. /* Return timeout status */
  922. return HAL_TIMEOUT;
  923. }
  924. }
  925. }
  926. /* Clear short circuit detection flag */
  927. DFSDM1_Filter0->FLTICR = (1UL << (DFSDM_FLTICR_CLRSCDF_Pos + channel));
  928. /* Return function status */
  929. return HAL_OK;
  930. }
  931. }
  932. /**
  933. * @brief This function allows to stop short circuit detection in polling mode.
  934. * @param hdfsdm_channel DFSDM channel handle.
  935. * @retval HAL status
  936. */
  937. HAL_StatusTypeDef HAL_DFSDM_ChannelScdStop(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  938. {
  939. HAL_StatusTypeDef status = HAL_OK;
  940. uint32_t channel;
  941. /* Check parameters */
  942. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  943. /* Check DFSDM channel state */
  944. if (hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
  945. {
  946. /* Return error status */
  947. status = HAL_ERROR;
  948. }
  949. else
  950. {
  951. /* Stop short circuit detection */
  952. hdfsdm_channel->Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_SCDEN);
  953. /* Clear short circuit detection flag */
  954. channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
  955. DFSDM1_Filter0->FLTICR = (1UL << (DFSDM_FLTICR_CLRSCDF_Pos + channel));
  956. }
  957. /* Return function status */
  958. return status;
  959. }
  960. /**
  961. * @brief This function allows to start short circuit detection in interrupt mode.
  962. * @note Same mode has to be used for all channels
  963. * @param hdfsdm_channel DFSDM channel handle.
  964. * @param Threshold Short circuit detector threshold.
  965. * This parameter must be a number between Min_Data = 0 and Max_Data = 255.
  966. * @param BreakSignal Break signals assigned to short circuit event.
  967. * This parameter can be a values combination of @ref DFSDM_BreakSignals.
  968. * @retval HAL status
  969. */
  970. HAL_StatusTypeDef HAL_DFSDM_ChannelScdStart_IT(DFSDM_Channel_HandleTypeDef *hdfsdm_channel,
  971. uint32_t Threshold,
  972. uint32_t BreakSignal)
  973. {
  974. HAL_StatusTypeDef status = HAL_OK;
  975. /* Check parameters */
  976. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  977. assert_param(IS_DFSDM_CHANNEL_SCD_THRESHOLD(Threshold));
  978. assert_param(IS_DFSDM_BREAK_SIGNALS(BreakSignal));
  979. /* Check DFSDM channel state */
  980. if (hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
  981. {
  982. /* Return error status */
  983. status = HAL_ERROR;
  984. }
  985. else
  986. {
  987. /* Activate short circuit detection interrupt */
  988. DFSDM1_Filter0->FLTCR2 |= DFSDM_FLTCR2_SCDIE;
  989. /* Configure threshold and break signals */
  990. hdfsdm_channel->Instance->CHAWSCDR &= ~(DFSDM_CHAWSCDR_BKSCD | DFSDM_CHAWSCDR_SCDT);
  991. hdfsdm_channel->Instance->CHAWSCDR |= ((BreakSignal << DFSDM_CHAWSCDR_BKSCD_Pos) | \
  992. Threshold);
  993. /* Start short circuit detection */
  994. hdfsdm_channel->Instance->CHCFGR1 |= DFSDM_CHCFGR1_SCDEN;
  995. }
  996. /* Return function status */
  997. return status;
  998. }
  999. /**
  1000. * @brief Short circuit detection callback.
  1001. * @param hdfsdm_channel DFSDM channel handle.
  1002. * @retval None
  1003. */
  1004. __weak void HAL_DFSDM_ChannelScdCallback(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  1005. {
  1006. /* Prevent unused argument(s) compilation warning */
  1007. UNUSED(hdfsdm_channel);
  1008. /* NOTE : This function should not be modified, when the callback is needed,
  1009. the HAL_DFSDM_ChannelScdCallback could be implemented in the user file
  1010. */
  1011. }
  1012. /**
  1013. * @brief This function allows to stop short circuit detection in interrupt mode.
  1014. * @note Interrupt will be disabled for all channels
  1015. * @param hdfsdm_channel DFSDM channel handle.
  1016. * @retval HAL status
  1017. */
  1018. HAL_StatusTypeDef HAL_DFSDM_ChannelScdStop_IT(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  1019. {
  1020. HAL_StatusTypeDef status = HAL_OK;
  1021. uint32_t channel;
  1022. /* Check parameters */
  1023. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  1024. /* Check DFSDM channel state */
  1025. if (hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
  1026. {
  1027. /* Return error status */
  1028. status = HAL_ERROR;
  1029. }
  1030. else
  1031. {
  1032. /* Stop short circuit detection */
  1033. hdfsdm_channel->Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_SCDEN);
  1034. /* Clear short circuit detection flag */
  1035. channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
  1036. DFSDM1_Filter0->FLTICR = (1UL << (DFSDM_FLTICR_CLRSCDF_Pos + channel));
  1037. /* Disable short circuit detection interrupt */
  1038. DFSDM1_Filter0->FLTCR2 &= ~(DFSDM_FLTCR2_SCDIE);
  1039. }
  1040. /* Return function status */
  1041. return status;
  1042. }
  1043. /**
  1044. * @brief This function allows to get channel analog watchdog value.
  1045. * @param hdfsdm_channel DFSDM channel handle.
  1046. * @retval Channel analog watchdog value.
  1047. */
  1048. int16_t HAL_DFSDM_ChannelGetAwdValue(const DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  1049. {
  1050. return (int16_t) hdfsdm_channel->Instance->CHWDATAR;
  1051. }
  1052. /**
  1053. * @brief This function allows to modify channel offset value.
  1054. * @param hdfsdm_channel DFSDM channel handle.
  1055. * @param Offset DFSDM channel offset.
  1056. * This parameter must be a number between Min_Data = -8388608 and Max_Data = 8388607.
  1057. * @retval HAL status.
  1058. */
  1059. HAL_StatusTypeDef HAL_DFSDM_ChannelModifyOffset(DFSDM_Channel_HandleTypeDef *hdfsdm_channel,
  1060. int32_t Offset)
  1061. {
  1062. HAL_StatusTypeDef status = HAL_OK;
  1063. /* Check parameters */
  1064. assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
  1065. assert_param(IS_DFSDM_CHANNEL_OFFSET(Offset));
  1066. /* Check DFSDM channel state */
  1067. if (hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
  1068. {
  1069. /* Return error status */
  1070. status = HAL_ERROR;
  1071. }
  1072. else
  1073. {
  1074. /* Modify channel offset */
  1075. hdfsdm_channel->Instance->CHCFGR2 &= ~(DFSDM_CHCFGR2_OFFSET);
  1076. hdfsdm_channel->Instance->CHCFGR2 |= ((uint32_t) Offset << DFSDM_CHCFGR2_OFFSET_Pos);
  1077. }
  1078. /* Return function status */
  1079. return status;
  1080. }
  1081. /**
  1082. * @}
  1083. */
  1084. /** @defgroup DFSDM_Exported_Functions_Group3_Channel Channel state function
  1085. * @brief Channel state function
  1086. *
  1087. @verbatim
  1088. ==============================================================================
  1089. ##### Channel state function #####
  1090. ==============================================================================
  1091. [..] This section provides function allowing to:
  1092. (+) Get channel handle state.
  1093. @endverbatim
  1094. * @{
  1095. */
  1096. /**
  1097. * @brief This function allows to get the current DFSDM channel handle state.
  1098. * @param hdfsdm_channel DFSDM channel handle.
  1099. * @retval DFSDM channel state.
  1100. */
  1101. HAL_DFSDM_Channel_StateTypeDef HAL_DFSDM_ChannelGetState(const DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
  1102. {
  1103. /* Return DFSDM channel handle state */
  1104. return hdfsdm_channel->State;
  1105. }
  1106. /**
  1107. * @}
  1108. */
  1109. /** @defgroup DFSDM_Exported_Functions_Group1_Filter Filter initialization and de-initialization functions
  1110. * @brief Filter initialization and de-initialization functions
  1111. *
  1112. @verbatim
  1113. ==============================================================================
  1114. ##### Filter initialization and de-initialization functions #####
  1115. ==============================================================================
  1116. [..] This section provides functions allowing to:
  1117. (+) Initialize the DFSDM filter.
  1118. (+) De-initialize the DFSDM filter.
  1119. @endverbatim
  1120. * @{
  1121. */
  1122. /**
  1123. * @brief Initialize the DFSDM filter according to the specified parameters
  1124. * in the DFSDM_FilterInitTypeDef structure and initialize the associated handle.
  1125. * @param hdfsdm_filter DFSDM filter handle.
  1126. * @retval HAL status.
  1127. */
  1128. HAL_StatusTypeDef HAL_DFSDM_FilterInit(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  1129. {
  1130. /* Check DFSDM Channel handle */
  1131. if (hdfsdm_filter == NULL)
  1132. {
  1133. return HAL_ERROR;
  1134. }
  1135. /* Check parameters */
  1136. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1137. assert_param(IS_DFSDM_FILTER_REG_TRIGGER(hdfsdm_filter->Init.RegularParam.Trigger));
  1138. assert_param(IS_FUNCTIONAL_STATE(hdfsdm_filter->Init.RegularParam.FastMode));
  1139. assert_param(IS_FUNCTIONAL_STATE(hdfsdm_filter->Init.RegularParam.DmaMode));
  1140. assert_param(IS_DFSDM_FILTER_INJ_TRIGGER(hdfsdm_filter->Init.InjectedParam.Trigger));
  1141. assert_param(IS_FUNCTIONAL_STATE(hdfsdm_filter->Init.InjectedParam.ScanMode));
  1142. assert_param(IS_FUNCTIONAL_STATE(hdfsdm_filter->Init.InjectedParam.DmaMode));
  1143. assert_param(IS_DFSDM_FILTER_SINC_ORDER(hdfsdm_filter->Init.FilterParam.SincOrder));
  1144. assert_param(IS_DFSDM_FILTER_OVS_RATIO(hdfsdm_filter->Init.FilterParam.Oversampling));
  1145. assert_param(IS_DFSDM_FILTER_INTEGRATOR_OVS_RATIO(hdfsdm_filter->Init.FilterParam.IntOversampling));
  1146. /* Check parameters compatibility */
  1147. if ((hdfsdm_filter->Instance == DFSDM1_Filter0) &&
  1148. ((hdfsdm_filter->Init.RegularParam.Trigger == DFSDM_FILTER_SYNC_TRIGGER) ||
  1149. (hdfsdm_filter->Init.InjectedParam.Trigger == DFSDM_FILTER_SYNC_TRIGGER)))
  1150. {
  1151. return HAL_ERROR;
  1152. }
  1153. /* Initialize DFSDM filter variables with default values */
  1154. hdfsdm_filter->RegularContMode = DFSDM_CONTINUOUS_CONV_OFF;
  1155. hdfsdm_filter->InjectedChannelsNbr = 1;
  1156. hdfsdm_filter->InjConvRemaining = 1;
  1157. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_NONE;
  1158. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  1159. /* Reset callback pointers to the weak predefined callbacks */
  1160. hdfsdm_filter->AwdCallback = HAL_DFSDM_FilterAwdCallback;
  1161. hdfsdm_filter->RegConvCpltCallback = HAL_DFSDM_FilterRegConvCpltCallback;
  1162. hdfsdm_filter->RegConvHalfCpltCallback = HAL_DFSDM_FilterRegConvHalfCpltCallback;
  1163. hdfsdm_filter->InjConvCpltCallback = HAL_DFSDM_FilterInjConvCpltCallback;
  1164. hdfsdm_filter->InjConvHalfCpltCallback = HAL_DFSDM_FilterInjConvHalfCpltCallback;
  1165. hdfsdm_filter->ErrorCallback = HAL_DFSDM_FilterErrorCallback;
  1166. /* Call MSP init function */
  1167. if (hdfsdm_filter->MspInitCallback == NULL)
  1168. {
  1169. hdfsdm_filter->MspInitCallback = HAL_DFSDM_FilterMspInit;
  1170. }
  1171. hdfsdm_filter->MspInitCallback(hdfsdm_filter);
  1172. #else
  1173. /* Call MSP init function */
  1174. HAL_DFSDM_FilterMspInit(hdfsdm_filter);
  1175. #endif
  1176. /* Set regular parameters */
  1177. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_RSYNC);
  1178. if (hdfsdm_filter->Init.RegularParam.FastMode == ENABLE)
  1179. {
  1180. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_FAST;
  1181. }
  1182. else
  1183. {
  1184. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_FAST);
  1185. }
  1186. if (hdfsdm_filter->Init.RegularParam.DmaMode == ENABLE)
  1187. {
  1188. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_RDMAEN;
  1189. }
  1190. else
  1191. {
  1192. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_RDMAEN);
  1193. }
  1194. /* Set injected parameters */
  1195. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_JSYNC | DFSDM_FLTCR1_JEXTEN | DFSDM_FLTCR1_JEXTSEL);
  1196. if (hdfsdm_filter->Init.InjectedParam.Trigger == DFSDM_FILTER_EXT_TRIGGER)
  1197. {
  1198. assert_param(IS_DFSDM_FILTER_EXT_TRIG(hdfsdm_filter->Init.InjectedParam.ExtTrigger));
  1199. assert_param(IS_DFSDM_FILTER_EXT_TRIG_EDGE(hdfsdm_filter->Init.InjectedParam.ExtTriggerEdge));
  1200. hdfsdm_filter->Instance->FLTCR1 |= (hdfsdm_filter->Init.InjectedParam.ExtTrigger);
  1201. }
  1202. if (hdfsdm_filter->Init.InjectedParam.ScanMode == ENABLE)
  1203. {
  1204. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_JSCAN;
  1205. }
  1206. else
  1207. {
  1208. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_JSCAN);
  1209. }
  1210. if (hdfsdm_filter->Init.InjectedParam.DmaMode == ENABLE)
  1211. {
  1212. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_JDMAEN;
  1213. }
  1214. else
  1215. {
  1216. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_JDMAEN);
  1217. }
  1218. /* Set filter parameters */
  1219. hdfsdm_filter->Instance->FLTFCR &= ~(DFSDM_FLTFCR_FORD | DFSDM_FLTFCR_FOSR | DFSDM_FLTFCR_IOSR);
  1220. hdfsdm_filter->Instance->FLTFCR |= (hdfsdm_filter->Init.FilterParam.SincOrder |
  1221. ((hdfsdm_filter->Init.FilterParam.Oversampling - 1U) << DFSDM_FLTFCR_FOSR_Pos) |
  1222. (hdfsdm_filter->Init.FilterParam.IntOversampling - 1U));
  1223. /* Store regular and injected triggers and injected scan mode*/
  1224. hdfsdm_filter->RegularTrigger = hdfsdm_filter->Init.RegularParam.Trigger;
  1225. hdfsdm_filter->InjectedTrigger = hdfsdm_filter->Init.InjectedParam.Trigger;
  1226. hdfsdm_filter->ExtTriggerEdge = hdfsdm_filter->Init.InjectedParam.ExtTriggerEdge;
  1227. hdfsdm_filter->InjectedScanMode = hdfsdm_filter->Init.InjectedParam.ScanMode;
  1228. /* Enable DFSDM filter */
  1229. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_DFEN;
  1230. /* Set DFSDM filter to ready state */
  1231. hdfsdm_filter->State = HAL_DFSDM_FILTER_STATE_READY;
  1232. return HAL_OK;
  1233. }
  1234. /**
  1235. * @brief De-initializes the DFSDM filter.
  1236. * @param hdfsdm_filter DFSDM filter handle.
  1237. * @retval HAL status.
  1238. */
  1239. HAL_StatusTypeDef HAL_DFSDM_FilterDeInit(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  1240. {
  1241. /* Check DFSDM filter handle */
  1242. if (hdfsdm_filter == NULL)
  1243. {
  1244. return HAL_ERROR;
  1245. }
  1246. /* Check parameters */
  1247. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1248. /* Disable the DFSDM filter */
  1249. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_DFEN);
  1250. /* Call MSP deinit function */
  1251. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  1252. if (hdfsdm_filter->MspDeInitCallback == NULL)
  1253. {
  1254. hdfsdm_filter->MspDeInitCallback = HAL_DFSDM_FilterMspDeInit;
  1255. }
  1256. hdfsdm_filter->MspDeInitCallback(hdfsdm_filter);
  1257. #else
  1258. HAL_DFSDM_FilterMspDeInit(hdfsdm_filter);
  1259. #endif
  1260. /* Set DFSDM filter in reset state */
  1261. hdfsdm_filter->State = HAL_DFSDM_FILTER_STATE_RESET;
  1262. return HAL_OK;
  1263. }
  1264. /**
  1265. * @brief Initializes the DFSDM filter MSP.
  1266. * @param hdfsdm_filter DFSDM filter handle.
  1267. * @retval None
  1268. */
  1269. __weak void HAL_DFSDM_FilterMspInit(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  1270. {
  1271. /* Prevent unused argument(s) compilation warning */
  1272. UNUSED(hdfsdm_filter);
  1273. /* NOTE : This function should not be modified, when the function is needed,
  1274. the HAL_DFSDM_FilterMspInit could be implemented in the user file.
  1275. */
  1276. }
  1277. /**
  1278. * @brief De-initializes the DFSDM filter MSP.
  1279. * @param hdfsdm_filter DFSDM filter handle.
  1280. * @retval None
  1281. */
  1282. __weak void HAL_DFSDM_FilterMspDeInit(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  1283. {
  1284. /* Prevent unused argument(s) compilation warning */
  1285. UNUSED(hdfsdm_filter);
  1286. /* NOTE : This function should not be modified, when the function is needed,
  1287. the HAL_DFSDM_FilterMspDeInit could be implemented in the user file.
  1288. */
  1289. }
  1290. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  1291. /**
  1292. * @brief Register a user DFSDM filter callback
  1293. * to be used instead of the weak predefined callback.
  1294. * @param hdfsdm_filter DFSDM filter handle.
  1295. * @param CallbackID ID of the callback to be registered.
  1296. * This parameter can be one of the following values:
  1297. * @arg @ref HAL_DFSDM_FILTER_REGCONV_COMPLETE_CB_ID regular conversion complete callback ID.
  1298. * @arg @ref HAL_DFSDM_FILTER_REGCONV_HALFCOMPLETE_CB_ID half regular conversion complete callback ID.
  1299. * @arg @ref HAL_DFSDM_FILTER_INJCONV_COMPLETE_CB_ID injected conversion complete callback ID.
  1300. * @arg @ref HAL_DFSDM_FILTER_INJCONV_HALFCOMPLETE_CB_ID half injected conversion complete callback ID.
  1301. * @arg @ref HAL_DFSDM_FILTER_ERROR_CB_ID error callback ID.
  1302. * @arg @ref HAL_DFSDM_FILTER_MSPINIT_CB_ID MSP init callback ID.
  1303. * @arg @ref HAL_DFSDM_FILTER_MSPDEINIT_CB_ID MSP de-init callback ID.
  1304. * @param pCallback pointer to the callback function.
  1305. * @retval HAL status.
  1306. */
  1307. HAL_StatusTypeDef HAL_DFSDM_Filter_RegisterCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  1308. HAL_DFSDM_Filter_CallbackIDTypeDef CallbackID,
  1309. pDFSDM_Filter_CallbackTypeDef pCallback)
  1310. {
  1311. HAL_StatusTypeDef status = HAL_OK;
  1312. if (pCallback == NULL)
  1313. {
  1314. /* update the error code */
  1315. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INVALID_CALLBACK;
  1316. /* update return status */
  1317. status = HAL_ERROR;
  1318. }
  1319. else
  1320. {
  1321. if (HAL_DFSDM_FILTER_STATE_READY == hdfsdm_filter->State)
  1322. {
  1323. switch (CallbackID)
  1324. {
  1325. case HAL_DFSDM_FILTER_REGCONV_COMPLETE_CB_ID :
  1326. hdfsdm_filter->RegConvCpltCallback = pCallback;
  1327. break;
  1328. case HAL_DFSDM_FILTER_REGCONV_HALFCOMPLETE_CB_ID :
  1329. hdfsdm_filter->RegConvHalfCpltCallback = pCallback;
  1330. break;
  1331. case HAL_DFSDM_FILTER_INJCONV_COMPLETE_CB_ID :
  1332. hdfsdm_filter->InjConvCpltCallback = pCallback;
  1333. break;
  1334. case HAL_DFSDM_FILTER_INJCONV_HALFCOMPLETE_CB_ID :
  1335. hdfsdm_filter->InjConvHalfCpltCallback = pCallback;
  1336. break;
  1337. case HAL_DFSDM_FILTER_ERROR_CB_ID :
  1338. hdfsdm_filter->ErrorCallback = pCallback;
  1339. break;
  1340. case HAL_DFSDM_FILTER_MSPINIT_CB_ID :
  1341. hdfsdm_filter->MspInitCallback = pCallback;
  1342. break;
  1343. case HAL_DFSDM_FILTER_MSPDEINIT_CB_ID :
  1344. hdfsdm_filter->MspDeInitCallback = pCallback;
  1345. break;
  1346. default :
  1347. /* update the error code */
  1348. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INVALID_CALLBACK;
  1349. /* update return status */
  1350. status = HAL_ERROR;
  1351. break;
  1352. }
  1353. }
  1354. else if (HAL_DFSDM_FILTER_STATE_RESET == hdfsdm_filter->State)
  1355. {
  1356. switch (CallbackID)
  1357. {
  1358. case HAL_DFSDM_FILTER_MSPINIT_CB_ID :
  1359. hdfsdm_filter->MspInitCallback = pCallback;
  1360. break;
  1361. case HAL_DFSDM_FILTER_MSPDEINIT_CB_ID :
  1362. hdfsdm_filter->MspDeInitCallback = pCallback;
  1363. break;
  1364. default :
  1365. /* update the error code */
  1366. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INVALID_CALLBACK;
  1367. /* update return status */
  1368. status = HAL_ERROR;
  1369. break;
  1370. }
  1371. }
  1372. else
  1373. {
  1374. /* update the error code */
  1375. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INVALID_CALLBACK;
  1376. /* update return status */
  1377. status = HAL_ERROR;
  1378. }
  1379. }
  1380. return status;
  1381. }
  1382. /**
  1383. * @brief Unregister a user DFSDM filter callback.
  1384. * DFSDM filter callback is redirected to the weak predefined callback.
  1385. * @param hdfsdm_filter DFSDM filter handle.
  1386. * @param CallbackID ID of the callback to be unregistered.
  1387. * This parameter can be one of the following values:
  1388. * @arg @ref HAL_DFSDM_FILTER_REGCONV_COMPLETE_CB_ID regular conversion complete callback ID.
  1389. * @arg @ref HAL_DFSDM_FILTER_REGCONV_HALFCOMPLETE_CB_ID half regular conversion complete callback ID.
  1390. * @arg @ref HAL_DFSDM_FILTER_INJCONV_COMPLETE_CB_ID injected conversion complete callback ID.
  1391. * @arg @ref HAL_DFSDM_FILTER_INJCONV_HALFCOMPLETE_CB_ID half injected conversion complete callback ID.
  1392. * @arg @ref HAL_DFSDM_FILTER_ERROR_CB_ID error callback ID.
  1393. * @arg @ref HAL_DFSDM_FILTER_MSPINIT_CB_ID MSP init callback ID.
  1394. * @arg @ref HAL_DFSDM_FILTER_MSPDEINIT_CB_ID MSP de-init callback ID.
  1395. * @retval HAL status.
  1396. */
  1397. HAL_StatusTypeDef HAL_DFSDM_Filter_UnRegisterCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  1398. HAL_DFSDM_Filter_CallbackIDTypeDef CallbackID)
  1399. {
  1400. HAL_StatusTypeDef status = HAL_OK;
  1401. if (HAL_DFSDM_FILTER_STATE_READY == hdfsdm_filter->State)
  1402. {
  1403. switch (CallbackID)
  1404. {
  1405. case HAL_DFSDM_FILTER_REGCONV_COMPLETE_CB_ID :
  1406. hdfsdm_filter->RegConvCpltCallback = HAL_DFSDM_FilterRegConvCpltCallback;
  1407. break;
  1408. case HAL_DFSDM_FILTER_REGCONV_HALFCOMPLETE_CB_ID :
  1409. hdfsdm_filter->RegConvHalfCpltCallback = HAL_DFSDM_FilterRegConvHalfCpltCallback;
  1410. break;
  1411. case HAL_DFSDM_FILTER_INJCONV_COMPLETE_CB_ID :
  1412. hdfsdm_filter->InjConvCpltCallback = HAL_DFSDM_FilterInjConvCpltCallback;
  1413. break;
  1414. case HAL_DFSDM_FILTER_INJCONV_HALFCOMPLETE_CB_ID :
  1415. hdfsdm_filter->InjConvHalfCpltCallback = HAL_DFSDM_FilterInjConvHalfCpltCallback;
  1416. break;
  1417. case HAL_DFSDM_FILTER_ERROR_CB_ID :
  1418. hdfsdm_filter->ErrorCallback = HAL_DFSDM_FilterErrorCallback;
  1419. break;
  1420. case HAL_DFSDM_FILTER_MSPINIT_CB_ID :
  1421. hdfsdm_filter->MspInitCallback = HAL_DFSDM_FilterMspInit;
  1422. break;
  1423. case HAL_DFSDM_FILTER_MSPDEINIT_CB_ID :
  1424. hdfsdm_filter->MspDeInitCallback = HAL_DFSDM_FilterMspDeInit;
  1425. break;
  1426. default :
  1427. /* update the error code */
  1428. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INVALID_CALLBACK;
  1429. /* update return status */
  1430. status = HAL_ERROR;
  1431. break;
  1432. }
  1433. }
  1434. else if (HAL_DFSDM_FILTER_STATE_RESET == hdfsdm_filter->State)
  1435. {
  1436. switch (CallbackID)
  1437. {
  1438. case HAL_DFSDM_FILTER_MSPINIT_CB_ID :
  1439. hdfsdm_filter->MspInitCallback = HAL_DFSDM_FilterMspInit;
  1440. break;
  1441. case HAL_DFSDM_FILTER_MSPDEINIT_CB_ID :
  1442. hdfsdm_filter->MspDeInitCallback = HAL_DFSDM_FilterMspDeInit;
  1443. break;
  1444. default :
  1445. /* update the error code */
  1446. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INVALID_CALLBACK;
  1447. /* update return status */
  1448. status = HAL_ERROR;
  1449. break;
  1450. }
  1451. }
  1452. else
  1453. {
  1454. /* update the error code */
  1455. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INVALID_CALLBACK;
  1456. /* update return status */
  1457. status = HAL_ERROR;
  1458. }
  1459. return status;
  1460. }
  1461. /**
  1462. * @brief Register a user DFSDM filter analog watchdog callback
  1463. * to be used instead of the weak predefined callback.
  1464. * @param hdfsdm_filter DFSDM filter handle.
  1465. * @param pCallback pointer to the DFSDM filter analog watchdog callback function.
  1466. * @retval HAL status.
  1467. */
  1468. HAL_StatusTypeDef HAL_DFSDM_Filter_RegisterAwdCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  1469. pDFSDM_Filter_AwdCallbackTypeDef pCallback)
  1470. {
  1471. HAL_StatusTypeDef status = HAL_OK;
  1472. if (pCallback == NULL)
  1473. {
  1474. /* update the error code */
  1475. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INVALID_CALLBACK;
  1476. /* update return status */
  1477. status = HAL_ERROR;
  1478. }
  1479. else
  1480. {
  1481. if (HAL_DFSDM_FILTER_STATE_READY == hdfsdm_filter->State)
  1482. {
  1483. hdfsdm_filter->AwdCallback = pCallback;
  1484. }
  1485. else
  1486. {
  1487. /* update the error code */
  1488. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INVALID_CALLBACK;
  1489. /* update return status */
  1490. status = HAL_ERROR;
  1491. }
  1492. }
  1493. return status;
  1494. }
  1495. /**
  1496. * @brief Unregister a user DFSDM filter analog watchdog callback.
  1497. * DFSDM filter AWD callback is redirected to the weak predefined callback.
  1498. * @param hdfsdm_filter DFSDM filter handle.
  1499. * @retval HAL status.
  1500. */
  1501. HAL_StatusTypeDef HAL_DFSDM_Filter_UnRegisterAwdCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  1502. {
  1503. HAL_StatusTypeDef status = HAL_OK;
  1504. if (HAL_DFSDM_FILTER_STATE_READY == hdfsdm_filter->State)
  1505. {
  1506. hdfsdm_filter->AwdCallback = HAL_DFSDM_FilterAwdCallback;
  1507. }
  1508. else
  1509. {
  1510. /* update the error code */
  1511. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INVALID_CALLBACK;
  1512. /* update return status */
  1513. status = HAL_ERROR;
  1514. }
  1515. return status;
  1516. }
  1517. #endif /* USE_HAL_DFSDM_REGISTER_CALLBACKS */
  1518. /**
  1519. * @}
  1520. */
  1521. /** @defgroup DFSDM_Exported_Functions_Group2_Filter Filter control functions
  1522. * @brief Filter control functions
  1523. *
  1524. @verbatim
  1525. ==============================================================================
  1526. ##### Filter control functions #####
  1527. ==============================================================================
  1528. [..] This section provides functions allowing to:
  1529. (+) Select channel and enable/disable continuous mode for regular conversion.
  1530. (+) Select channels for injected conversion.
  1531. @endverbatim
  1532. * @{
  1533. */
  1534. /**
  1535. * @brief This function allows to select channel and to enable/disable
  1536. * continuous mode for regular conversion.
  1537. * @param hdfsdm_filter DFSDM filter handle.
  1538. * @param Channel Channel for regular conversion.
  1539. * This parameter can be a value of @ref DFSDM_Channel_Selection.
  1540. * @param ContinuousMode Enable/disable continuous mode for regular conversion.
  1541. * This parameter can be a value of @ref DFSDM_ContinuousMode.
  1542. * @retval HAL status
  1543. */
  1544. HAL_StatusTypeDef HAL_DFSDM_FilterConfigRegChannel(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  1545. uint32_t Channel,
  1546. uint32_t ContinuousMode)
  1547. {
  1548. HAL_StatusTypeDef status = HAL_OK;
  1549. /* Check parameters */
  1550. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1551. assert_param(IS_DFSDM_REGULAR_CHANNEL(Channel));
  1552. assert_param(IS_DFSDM_CONTINUOUS_MODE(ContinuousMode));
  1553. /* Check DFSDM filter state */
  1554. if ((hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_RESET) &&
  1555. (hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_ERROR))
  1556. {
  1557. /* Configure channel and continuous mode for regular conversion */
  1558. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_RCH | DFSDM_FLTCR1_RCONT);
  1559. if (ContinuousMode == DFSDM_CONTINUOUS_CONV_ON)
  1560. {
  1561. hdfsdm_filter->Instance->FLTCR1 |= (uint32_t)(((Channel & DFSDM_MSB_MASK) << DFSDM_FLTCR1_MSB_RCH_OFFSET) |
  1562. DFSDM_FLTCR1_RCONT);
  1563. }
  1564. else
  1565. {
  1566. hdfsdm_filter->Instance->FLTCR1 |= (uint32_t)((Channel & DFSDM_MSB_MASK) << DFSDM_FLTCR1_MSB_RCH_OFFSET);
  1567. }
  1568. /* Store continuous mode information */
  1569. hdfsdm_filter->RegularContMode = ContinuousMode;
  1570. }
  1571. else
  1572. {
  1573. status = HAL_ERROR;
  1574. }
  1575. /* Return function status */
  1576. return status;
  1577. }
  1578. /**
  1579. * @brief This function allows to select channels for injected conversion.
  1580. * @param hdfsdm_filter DFSDM filter handle.
  1581. * @param Channel Channels for injected conversion.
  1582. * This parameter can be a values combination of @ref DFSDM_Channel_Selection.
  1583. * @retval HAL status
  1584. */
  1585. HAL_StatusTypeDef HAL_DFSDM_FilterConfigInjChannel(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  1586. uint32_t Channel)
  1587. {
  1588. HAL_StatusTypeDef status = HAL_OK;
  1589. /* Check parameters */
  1590. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1591. assert_param(IS_DFSDM_INJECTED_CHANNEL(Channel));
  1592. /* Check DFSDM filter state */
  1593. if ((hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_RESET) &&
  1594. (hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_ERROR))
  1595. {
  1596. /* Configure channel for injected conversion */
  1597. hdfsdm_filter->Instance->FLTJCHGR = (uint32_t)(Channel & DFSDM_LSB_MASK);
  1598. /* Store number of injected channels */
  1599. hdfsdm_filter->InjectedChannelsNbr = DFSDM_GetInjChannelsNbr(Channel);
  1600. /* Update number of injected channels remaining */
  1601. hdfsdm_filter->InjConvRemaining = (hdfsdm_filter->InjectedScanMode == ENABLE) ? \
  1602. hdfsdm_filter->InjectedChannelsNbr : 1U;
  1603. }
  1604. else
  1605. {
  1606. status = HAL_ERROR;
  1607. }
  1608. /* Return function status */
  1609. return status;
  1610. }
  1611. /**
  1612. * @}
  1613. */
  1614. /** @defgroup DFSDM_Exported_Functions_Group3_Filter Filter operation functions
  1615. * @brief Filter operation functions
  1616. *
  1617. @verbatim
  1618. ==============================================================================
  1619. ##### Filter operation functions #####
  1620. ==============================================================================
  1621. [..] This section provides functions allowing to:
  1622. (+) Start conversion of regular/injected channel.
  1623. (+) Poll for the end of regular/injected conversion.
  1624. (+) Stop conversion of regular/injected channel.
  1625. (+) Start conversion of regular/injected channel and enable interrupt.
  1626. (+) Call the callback functions at the end of regular/injected conversions.
  1627. (+) Stop conversion of regular/injected channel and disable interrupt.
  1628. (+) Start conversion of regular/injected channel and enable DMA transfer.
  1629. (+) Stop conversion of regular/injected channel and disable DMA transfer.
  1630. (+) Start analog watchdog and enable interrupt.
  1631. (+) Call the callback function when analog watchdog occurs.
  1632. (+) Stop analog watchdog and disable interrupt.
  1633. (+) Start extreme detector.
  1634. (+) Stop extreme detector.
  1635. (+) Get result of regular channel conversion.
  1636. (+) Get result of injected channel conversion.
  1637. (+) Get extreme detector maximum and minimum values.
  1638. (+) Get conversion time.
  1639. (+) Handle DFSDM interrupt request.
  1640. @endverbatim
  1641. * @{
  1642. */
  1643. /**
  1644. * @brief This function allows to start regular conversion in polling mode.
  1645. * @note This function should be called only when DFSDM filter instance is
  1646. * in idle state or if injected conversion is ongoing.
  1647. * @param hdfsdm_filter DFSDM filter handle.
  1648. * @retval HAL status
  1649. */
  1650. HAL_StatusTypeDef HAL_DFSDM_FilterRegularStart(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  1651. {
  1652. HAL_StatusTypeDef status = HAL_OK;
  1653. /* Check parameters */
  1654. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1655. /* Check DFSDM filter state */
  1656. if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_READY) || \
  1657. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_INJ))
  1658. {
  1659. /* Start regular conversion */
  1660. DFSDM_RegConvStart(hdfsdm_filter);
  1661. }
  1662. else
  1663. {
  1664. status = HAL_ERROR;
  1665. }
  1666. /* Return function status */
  1667. return status;
  1668. }
  1669. /**
  1670. * @brief This function allows to poll for the end of regular conversion.
  1671. * @note This function should be called only if regular conversion is ongoing.
  1672. * @param hdfsdm_filter DFSDM filter handle.
  1673. * @param Timeout Timeout value in milliseconds.
  1674. * @retval HAL status
  1675. */
  1676. HAL_StatusTypeDef HAL_DFSDM_FilterPollForRegConversion(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  1677. uint32_t Timeout)
  1678. {
  1679. uint32_t tickstart;
  1680. /* Check parameters */
  1681. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1682. /* Check DFSDM filter state */
  1683. if ((hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG) && \
  1684. (hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG_INJ))
  1685. {
  1686. /* Return error status */
  1687. return HAL_ERROR;
  1688. }
  1689. else
  1690. {
  1691. /* Get timeout */
  1692. tickstart = HAL_GetTick();
  1693. /* Wait end of regular conversion */
  1694. while ((hdfsdm_filter->Instance->FLTISR & DFSDM_FLTISR_REOCF) != DFSDM_FLTISR_REOCF)
  1695. {
  1696. /* Check the Timeout */
  1697. if (Timeout != HAL_MAX_DELAY)
  1698. {
  1699. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1700. {
  1701. /* Return timeout status */
  1702. return HAL_TIMEOUT;
  1703. }
  1704. }
  1705. }
  1706. /* Check if overrun occurs */
  1707. if ((hdfsdm_filter->Instance->FLTISR & DFSDM_FLTISR_ROVRF) == DFSDM_FLTISR_ROVRF)
  1708. {
  1709. /* Update error code and call error callback */
  1710. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_REGULAR_OVERRUN;
  1711. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  1712. hdfsdm_filter->ErrorCallback(hdfsdm_filter);
  1713. #else
  1714. HAL_DFSDM_FilterErrorCallback(hdfsdm_filter);
  1715. #endif
  1716. /* Clear regular overrun flag */
  1717. hdfsdm_filter->Instance->FLTICR = DFSDM_FLTICR_CLRROVRF;
  1718. }
  1719. /* Update DFSDM filter state only if not continuous conversion and SW trigger */
  1720. if ((hdfsdm_filter->RegularContMode == DFSDM_CONTINUOUS_CONV_OFF) && \
  1721. (hdfsdm_filter->RegularTrigger == DFSDM_FILTER_SW_TRIGGER))
  1722. {
  1723. hdfsdm_filter->State = (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_REG) ? \
  1724. HAL_DFSDM_FILTER_STATE_READY : HAL_DFSDM_FILTER_STATE_INJ;
  1725. }
  1726. /* Return function status */
  1727. return HAL_OK;
  1728. }
  1729. }
  1730. /**
  1731. * @brief This function allows to stop regular conversion in polling mode.
  1732. * @note This function should be called only if regular conversion is ongoing.
  1733. * @param hdfsdm_filter DFSDM filter handle.
  1734. * @retval HAL status
  1735. */
  1736. HAL_StatusTypeDef HAL_DFSDM_FilterRegularStop(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  1737. {
  1738. HAL_StatusTypeDef status = HAL_OK;
  1739. /* Check parameters */
  1740. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1741. /* Check DFSDM filter state */
  1742. if ((hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG) && \
  1743. (hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG_INJ))
  1744. {
  1745. /* Return error status */
  1746. status = HAL_ERROR;
  1747. }
  1748. else
  1749. {
  1750. /* Stop regular conversion */
  1751. DFSDM_RegConvStop(hdfsdm_filter);
  1752. }
  1753. /* Return function status */
  1754. return status;
  1755. }
  1756. /**
  1757. * @brief This function allows to start regular conversion in interrupt mode.
  1758. * @note This function should be called only when DFSDM filter instance is
  1759. * in idle state or if injected conversion is ongoing.
  1760. * @param hdfsdm_filter DFSDM filter handle.
  1761. * @retval HAL status
  1762. */
  1763. HAL_StatusTypeDef HAL_DFSDM_FilterRegularStart_IT(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  1764. {
  1765. HAL_StatusTypeDef status = HAL_OK;
  1766. /* Check parameters */
  1767. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1768. /* Check DFSDM filter state */
  1769. if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_READY) || \
  1770. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_INJ))
  1771. {
  1772. /* Enable interrupts for regular conversions */
  1773. hdfsdm_filter->Instance->FLTCR2 |= (DFSDM_FLTCR2_REOCIE | DFSDM_FLTCR2_ROVRIE);
  1774. /* Start regular conversion */
  1775. DFSDM_RegConvStart(hdfsdm_filter);
  1776. }
  1777. else
  1778. {
  1779. status = HAL_ERROR;
  1780. }
  1781. /* Return function status */
  1782. return status;
  1783. }
  1784. /**
  1785. * @brief This function allows to stop regular conversion in interrupt mode.
  1786. * @note This function should be called only if regular conversion is ongoing.
  1787. * @param hdfsdm_filter DFSDM filter handle.
  1788. * @retval HAL status
  1789. */
  1790. HAL_StatusTypeDef HAL_DFSDM_FilterRegularStop_IT(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  1791. {
  1792. HAL_StatusTypeDef status = HAL_OK;
  1793. /* Check parameters */
  1794. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1795. /* Check DFSDM filter state */
  1796. if ((hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG) && \
  1797. (hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG_INJ))
  1798. {
  1799. /* Return error status */
  1800. status = HAL_ERROR;
  1801. }
  1802. else
  1803. {
  1804. /* Disable interrupts for regular conversions */
  1805. hdfsdm_filter->Instance->FLTCR2 &= ~(DFSDM_FLTCR2_REOCIE | DFSDM_FLTCR2_ROVRIE);
  1806. /* Stop regular conversion */
  1807. DFSDM_RegConvStop(hdfsdm_filter);
  1808. }
  1809. /* Return function status */
  1810. return status;
  1811. }
  1812. /**
  1813. * @brief This function allows to start regular conversion in DMA mode.
  1814. * @note This function should be called only when DFSDM filter instance is
  1815. * in idle state or if injected conversion is ongoing.
  1816. * Please note that data on buffer will contain signed regular conversion
  1817. * value on 24 most significant bits and corresponding channel on 3 least
  1818. * significant bits.
  1819. * @param hdfsdm_filter DFSDM filter handle.
  1820. * @param pData The destination buffer address.
  1821. * @param Length The length of data to be transferred from DFSDM filter to memory.
  1822. * @retval HAL status
  1823. */
  1824. HAL_StatusTypeDef HAL_DFSDM_FilterRegularStart_DMA(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  1825. int32_t *pData,
  1826. uint32_t Length)
  1827. {
  1828. HAL_StatusTypeDef status = HAL_OK;
  1829. /* Check parameters */
  1830. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1831. /* Check destination address and length */
  1832. if ((pData == NULL) || (Length == 0U))
  1833. {
  1834. status = HAL_ERROR;
  1835. }
  1836. /* Check that DMA is enabled for regular conversion */
  1837. else if ((hdfsdm_filter->Instance->FLTCR1 & DFSDM_FLTCR1_RDMAEN) != DFSDM_FLTCR1_RDMAEN)
  1838. {
  1839. status = HAL_ERROR;
  1840. }
  1841. /* Check parameters compatibility */
  1842. else if ((hdfsdm_filter->RegularTrigger == DFSDM_FILTER_SW_TRIGGER) && \
  1843. (hdfsdm_filter->RegularContMode == DFSDM_CONTINUOUS_CONV_OFF) && \
  1844. (hdfsdm_filter->hdmaReg->Init.Mode == DMA_NORMAL) && \
  1845. (Length != 1U))
  1846. {
  1847. status = HAL_ERROR;
  1848. }
  1849. else if ((hdfsdm_filter->RegularTrigger == DFSDM_FILTER_SW_TRIGGER) && \
  1850. (hdfsdm_filter->RegularContMode == DFSDM_CONTINUOUS_CONV_OFF) && \
  1851. (hdfsdm_filter->hdmaReg->Init.Mode == DMA_CIRCULAR))
  1852. {
  1853. status = HAL_ERROR;
  1854. }
  1855. /* Check DFSDM filter state */
  1856. else if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_READY) || \
  1857. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_INJ))
  1858. {
  1859. /* Set callbacks on DMA handler */
  1860. hdfsdm_filter->hdmaReg->XferCpltCallback = DFSDM_DMARegularConvCplt;
  1861. hdfsdm_filter->hdmaReg->XferErrorCallback = DFSDM_DMAError;
  1862. hdfsdm_filter->hdmaReg->XferHalfCpltCallback = (hdfsdm_filter->hdmaReg->Init.Mode == DMA_CIRCULAR) ? \
  1863. DFSDM_DMARegularHalfConvCplt : NULL;
  1864. /* Start DMA in interrupt mode */
  1865. if (HAL_DMA_Start_IT(hdfsdm_filter->hdmaReg, (uint32_t)&hdfsdm_filter->Instance->FLTRDATAR, \
  1866. (uint32_t) pData, Length) != HAL_OK)
  1867. {
  1868. /* Set DFSDM filter in error state */
  1869. hdfsdm_filter->State = HAL_DFSDM_FILTER_STATE_ERROR;
  1870. status = HAL_ERROR;
  1871. }
  1872. else
  1873. {
  1874. /* Start regular conversion */
  1875. DFSDM_RegConvStart(hdfsdm_filter);
  1876. }
  1877. }
  1878. else
  1879. {
  1880. status = HAL_ERROR;
  1881. }
  1882. /* Return function status */
  1883. return status;
  1884. }
  1885. /**
  1886. * @brief This function allows to start regular conversion in DMA mode and to get
  1887. * only the 16 most significant bits of conversion.
  1888. * @note This function should be called only when DFSDM filter instance is
  1889. * in idle state or if injected conversion is ongoing.
  1890. * Please note that data on buffer will contain signed 16 most significant
  1891. * bits of regular conversion.
  1892. * @param hdfsdm_filter DFSDM filter handle.
  1893. * @param pData The destination buffer address.
  1894. * @param Length The length of data to be transferred from DFSDM filter to memory.
  1895. * @retval HAL status
  1896. */
  1897. HAL_StatusTypeDef HAL_DFSDM_FilterRegularMsbStart_DMA(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  1898. int16_t *pData,
  1899. uint32_t Length)
  1900. {
  1901. HAL_StatusTypeDef status = HAL_OK;
  1902. /* Check parameters */
  1903. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1904. /* Check destination address and length */
  1905. if ((pData == NULL) || (Length == 0U))
  1906. {
  1907. status = HAL_ERROR;
  1908. }
  1909. /* Check that DMA is enabled for regular conversion */
  1910. else if ((hdfsdm_filter->Instance->FLTCR1 & DFSDM_FLTCR1_RDMAEN) != DFSDM_FLTCR1_RDMAEN)
  1911. {
  1912. status = HAL_ERROR;
  1913. }
  1914. /* Check parameters compatibility */
  1915. else if ((hdfsdm_filter->RegularTrigger == DFSDM_FILTER_SW_TRIGGER) && \
  1916. (hdfsdm_filter->RegularContMode == DFSDM_CONTINUOUS_CONV_OFF) && \
  1917. (hdfsdm_filter->hdmaReg->Init.Mode == DMA_NORMAL) && \
  1918. (Length != 1U))
  1919. {
  1920. status = HAL_ERROR;
  1921. }
  1922. else if ((hdfsdm_filter->RegularTrigger == DFSDM_FILTER_SW_TRIGGER) && \
  1923. (hdfsdm_filter->RegularContMode == DFSDM_CONTINUOUS_CONV_OFF) && \
  1924. (hdfsdm_filter->hdmaReg->Init.Mode == DMA_CIRCULAR))
  1925. {
  1926. status = HAL_ERROR;
  1927. }
  1928. /* Check DFSDM filter state */
  1929. else if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_READY) || \
  1930. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_INJ))
  1931. {
  1932. /* Set callbacks on DMA handler */
  1933. hdfsdm_filter->hdmaReg->XferCpltCallback = DFSDM_DMARegularConvCplt;
  1934. hdfsdm_filter->hdmaReg->XferErrorCallback = DFSDM_DMAError;
  1935. hdfsdm_filter->hdmaReg->XferHalfCpltCallback = (hdfsdm_filter->hdmaReg->Init.Mode == DMA_CIRCULAR) ? \
  1936. DFSDM_DMARegularHalfConvCplt : NULL;
  1937. /* Start DMA in interrupt mode */
  1938. if (HAL_DMA_Start_IT(hdfsdm_filter->hdmaReg, (uint32_t)(&hdfsdm_filter->Instance->FLTRDATAR) + 2U, \
  1939. (uint32_t) pData, Length) != HAL_OK)
  1940. {
  1941. /* Set DFSDM filter in error state */
  1942. hdfsdm_filter->State = HAL_DFSDM_FILTER_STATE_ERROR;
  1943. status = HAL_ERROR;
  1944. }
  1945. else
  1946. {
  1947. /* Start regular conversion */
  1948. DFSDM_RegConvStart(hdfsdm_filter);
  1949. }
  1950. }
  1951. else
  1952. {
  1953. status = HAL_ERROR;
  1954. }
  1955. /* Return function status */
  1956. return status;
  1957. }
  1958. /**
  1959. * @brief This function allows to stop regular conversion in DMA mode.
  1960. * @note This function should be called only if regular conversion is ongoing.
  1961. * @param hdfsdm_filter DFSDM filter handle.
  1962. * @retval HAL status
  1963. */
  1964. HAL_StatusTypeDef HAL_DFSDM_FilterRegularStop_DMA(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  1965. {
  1966. HAL_StatusTypeDef status = HAL_OK;
  1967. /* Check parameters */
  1968. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  1969. /* Check DFSDM filter state */
  1970. if ((hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG) && \
  1971. (hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG_INJ))
  1972. {
  1973. /* Return error status */
  1974. status = HAL_ERROR;
  1975. }
  1976. else
  1977. {
  1978. /* Stop current DMA transfer */
  1979. /* No need to check the returned value of HAL_DMA_Abort. */
  1980. /* Only HAL_DMA_ERROR_NO_XFER can be returned in case of error and it's not an error for DFSDM. */
  1981. (void) HAL_DMA_Abort(hdfsdm_filter->hdmaReg);
  1982. /* Stop regular conversion */
  1983. DFSDM_RegConvStop(hdfsdm_filter);
  1984. }
  1985. /* Return function status */
  1986. return status;
  1987. }
  1988. /**
  1989. * @brief This function allows to get regular conversion value.
  1990. * @param hdfsdm_filter DFSDM filter handle.
  1991. * @param Channel Corresponding channel of regular conversion.
  1992. * @retval Regular conversion value
  1993. */
  1994. int32_t HAL_DFSDM_FilterGetRegularValue(const DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  1995. uint32_t *Channel)
  1996. {
  1997. uint32_t reg;
  1998. int32_t value;
  1999. /* Check parameters */
  2000. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2001. assert_param(Channel != (void *)0);
  2002. /* Get value of data register for regular channel */
  2003. reg = hdfsdm_filter->Instance->FLTRDATAR;
  2004. /* Extract channel and regular conversion value */
  2005. *Channel = (reg & DFSDM_FLTRDATAR_RDATACH);
  2006. /* Regular conversion value is a signed value located on 24 MSB of register */
  2007. /* So after applying a mask on these bits we have to perform a division by 256 (2 raised to the power of 8) */
  2008. reg &= DFSDM_FLTRDATAR_RDATA;
  2009. value = ((int32_t)reg) / 256;
  2010. /* return regular conversion value */
  2011. return value;
  2012. }
  2013. /**
  2014. * @brief This function allows to start injected conversion in polling mode.
  2015. * @note This function should be called only when DFSDM filter instance is
  2016. * in idle state or if regular conversion is ongoing.
  2017. * @param hdfsdm_filter DFSDM filter handle.
  2018. * @retval HAL status
  2019. */
  2020. HAL_StatusTypeDef HAL_DFSDM_FilterInjectedStart(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2021. {
  2022. HAL_StatusTypeDef status = HAL_OK;
  2023. /* Check parameters */
  2024. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2025. /* Check DFSDM filter state */
  2026. if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_READY) || \
  2027. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_REG))
  2028. {
  2029. /* Start injected conversion */
  2030. DFSDM_InjConvStart(hdfsdm_filter);
  2031. }
  2032. else
  2033. {
  2034. status = HAL_ERROR;
  2035. }
  2036. /* Return function status */
  2037. return status;
  2038. }
  2039. /**
  2040. * @brief This function allows to poll for the end of injected conversion.
  2041. * @note This function should be called only if injected conversion is ongoing.
  2042. * @param hdfsdm_filter DFSDM filter handle.
  2043. * @param Timeout Timeout value in milliseconds.
  2044. * @retval HAL status
  2045. */
  2046. HAL_StatusTypeDef HAL_DFSDM_FilterPollForInjConversion(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  2047. uint32_t Timeout)
  2048. {
  2049. uint32_t tickstart;
  2050. /* Check parameters */
  2051. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2052. /* Check DFSDM filter state */
  2053. if ((hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_INJ) && \
  2054. (hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG_INJ))
  2055. {
  2056. /* Return error status */
  2057. return HAL_ERROR;
  2058. }
  2059. else
  2060. {
  2061. /* Get timeout */
  2062. tickstart = HAL_GetTick();
  2063. /* Wait end of injected conversions */
  2064. while ((hdfsdm_filter->Instance->FLTISR & DFSDM_FLTISR_JEOCF) != DFSDM_FLTISR_JEOCF)
  2065. {
  2066. /* Check the Timeout */
  2067. if (Timeout != HAL_MAX_DELAY)
  2068. {
  2069. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  2070. {
  2071. /* Return timeout status */
  2072. return HAL_TIMEOUT;
  2073. }
  2074. }
  2075. }
  2076. /* Check if overrun occurs */
  2077. if ((hdfsdm_filter->Instance->FLTISR & DFSDM_FLTISR_JOVRF) == DFSDM_FLTISR_JOVRF)
  2078. {
  2079. /* Update error code and call error callback */
  2080. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INJECTED_OVERRUN;
  2081. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2082. hdfsdm_filter->ErrorCallback(hdfsdm_filter);
  2083. #else
  2084. HAL_DFSDM_FilterErrorCallback(hdfsdm_filter);
  2085. #endif
  2086. /* Clear injected overrun flag */
  2087. hdfsdm_filter->Instance->FLTICR = DFSDM_FLTICR_CLRJOVRF;
  2088. }
  2089. /* Update remaining injected conversions */
  2090. hdfsdm_filter->InjConvRemaining--;
  2091. if (hdfsdm_filter->InjConvRemaining == 0U)
  2092. {
  2093. /* Update DFSDM filter state only if trigger is software */
  2094. if (hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_SW_TRIGGER)
  2095. {
  2096. hdfsdm_filter->State = (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_INJ) ? \
  2097. HAL_DFSDM_FILTER_STATE_READY : HAL_DFSDM_FILTER_STATE_REG;
  2098. }
  2099. /* end of injected sequence, reset the value */
  2100. hdfsdm_filter->InjConvRemaining = (hdfsdm_filter->InjectedScanMode == ENABLE) ? \
  2101. hdfsdm_filter->InjectedChannelsNbr : 1U;
  2102. }
  2103. /* Return function status */
  2104. return HAL_OK;
  2105. }
  2106. }
  2107. /**
  2108. * @brief This function allows to stop injected conversion in polling mode.
  2109. * @note This function should be called only if injected conversion is ongoing.
  2110. * @param hdfsdm_filter DFSDM filter handle.
  2111. * @retval HAL status
  2112. */
  2113. HAL_StatusTypeDef HAL_DFSDM_FilterInjectedStop(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2114. {
  2115. HAL_StatusTypeDef status = HAL_OK;
  2116. /* Check parameters */
  2117. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2118. /* Check DFSDM filter state */
  2119. if ((hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_INJ) && \
  2120. (hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG_INJ))
  2121. {
  2122. /* Return error status */
  2123. status = HAL_ERROR;
  2124. }
  2125. else
  2126. {
  2127. /* Stop injected conversion */
  2128. DFSDM_InjConvStop(hdfsdm_filter);
  2129. }
  2130. /* Return function status */
  2131. return status;
  2132. }
  2133. /**
  2134. * @brief This function allows to start injected conversion in interrupt mode.
  2135. * @note This function should be called only when DFSDM filter instance is
  2136. * in idle state or if regular conversion is ongoing.
  2137. * @param hdfsdm_filter DFSDM filter handle.
  2138. * @retval HAL status
  2139. */
  2140. HAL_StatusTypeDef HAL_DFSDM_FilterInjectedStart_IT(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2141. {
  2142. HAL_StatusTypeDef status = HAL_OK;
  2143. /* Check parameters */
  2144. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2145. /* Check DFSDM filter state */
  2146. if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_READY) || \
  2147. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_REG))
  2148. {
  2149. /* Enable interrupts for injected conversions */
  2150. hdfsdm_filter->Instance->FLTCR2 |= (DFSDM_FLTCR2_JEOCIE | DFSDM_FLTCR2_JOVRIE);
  2151. /* Start injected conversion */
  2152. DFSDM_InjConvStart(hdfsdm_filter);
  2153. }
  2154. else
  2155. {
  2156. status = HAL_ERROR;
  2157. }
  2158. /* Return function status */
  2159. return status;
  2160. }
  2161. /**
  2162. * @brief This function allows to stop injected conversion in interrupt mode.
  2163. * @note This function should be called only if injected conversion is ongoing.
  2164. * @param hdfsdm_filter DFSDM filter handle.
  2165. * @retval HAL status
  2166. */
  2167. HAL_StatusTypeDef HAL_DFSDM_FilterInjectedStop_IT(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2168. {
  2169. HAL_StatusTypeDef status = HAL_OK;
  2170. /* Check parameters */
  2171. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2172. /* Check DFSDM filter state */
  2173. if ((hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_INJ) && \
  2174. (hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG_INJ))
  2175. {
  2176. /* Return error status */
  2177. status = HAL_ERROR;
  2178. }
  2179. else
  2180. {
  2181. /* Disable interrupts for injected conversions */
  2182. hdfsdm_filter->Instance->FLTCR2 &= ~(DFSDM_FLTCR2_JEOCIE | DFSDM_FLTCR2_JOVRIE);
  2183. /* Stop injected conversion */
  2184. DFSDM_InjConvStop(hdfsdm_filter);
  2185. }
  2186. /* Return function status */
  2187. return status;
  2188. }
  2189. /**
  2190. * @brief This function allows to start injected conversion in DMA mode.
  2191. * @note This function should be called only when DFSDM filter instance is
  2192. * in idle state or if regular conversion is ongoing.
  2193. * Please note that data on buffer will contain signed injected conversion
  2194. * value on 24 most significant bits and corresponding channel on 3 least
  2195. * significant bits.
  2196. * @param hdfsdm_filter DFSDM filter handle.
  2197. * @param pData The destination buffer address.
  2198. * @param Length The length of data to be transferred from DFSDM filter to memory.
  2199. * @retval HAL status
  2200. */
  2201. HAL_StatusTypeDef HAL_DFSDM_FilterInjectedStart_DMA(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  2202. int32_t *pData,
  2203. uint32_t Length)
  2204. {
  2205. HAL_StatusTypeDef status = HAL_OK;
  2206. /* Check parameters */
  2207. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2208. /* Check destination address and length */
  2209. if ((pData == NULL) || (Length == 0U))
  2210. {
  2211. status = HAL_ERROR;
  2212. }
  2213. /* Check that DMA is enabled for injected conversion */
  2214. else if ((hdfsdm_filter->Instance->FLTCR1 & DFSDM_FLTCR1_JDMAEN) != DFSDM_FLTCR1_JDMAEN)
  2215. {
  2216. status = HAL_ERROR;
  2217. }
  2218. /* Check parameters compatibility */
  2219. else if ((hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_SW_TRIGGER) && \
  2220. (hdfsdm_filter->hdmaInj->Init.Mode == DMA_NORMAL) && \
  2221. (Length > hdfsdm_filter->InjConvRemaining))
  2222. {
  2223. status = HAL_ERROR;
  2224. }
  2225. else if ((hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_SW_TRIGGER) && \
  2226. (hdfsdm_filter->hdmaInj->Init.Mode == DMA_CIRCULAR))
  2227. {
  2228. status = HAL_ERROR;
  2229. }
  2230. /* Check DFSDM filter state */
  2231. else if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_READY) || \
  2232. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_REG))
  2233. {
  2234. /* Set callbacks on DMA handler */
  2235. hdfsdm_filter->hdmaInj->XferCpltCallback = DFSDM_DMAInjectedConvCplt;
  2236. hdfsdm_filter->hdmaInj->XferErrorCallback = DFSDM_DMAError;
  2237. hdfsdm_filter->hdmaInj->XferHalfCpltCallback = (hdfsdm_filter->hdmaInj->Init.Mode == DMA_CIRCULAR) ? \
  2238. DFSDM_DMAInjectedHalfConvCplt : NULL;
  2239. /* Start DMA in interrupt mode */
  2240. if (HAL_DMA_Start_IT(hdfsdm_filter->hdmaInj, (uint32_t)&hdfsdm_filter->Instance->FLTJDATAR, \
  2241. (uint32_t) pData, Length) != HAL_OK)
  2242. {
  2243. /* Set DFSDM filter in error state */
  2244. hdfsdm_filter->State = HAL_DFSDM_FILTER_STATE_ERROR;
  2245. status = HAL_ERROR;
  2246. }
  2247. else
  2248. {
  2249. /* Start injected conversion */
  2250. DFSDM_InjConvStart(hdfsdm_filter);
  2251. }
  2252. }
  2253. else
  2254. {
  2255. status = HAL_ERROR;
  2256. }
  2257. /* Return function status */
  2258. return status;
  2259. }
  2260. /**
  2261. * @brief This function allows to start injected conversion in DMA mode and to get
  2262. * only the 16 most significant bits of conversion.
  2263. * @note This function should be called only when DFSDM filter instance is
  2264. * in idle state or if regular conversion is ongoing.
  2265. * Please note that data on buffer will contain signed 16 most significant
  2266. * bits of injected conversion.
  2267. * @param hdfsdm_filter DFSDM filter handle.
  2268. * @param pData The destination buffer address.
  2269. * @param Length The length of data to be transferred from DFSDM filter to memory.
  2270. * @retval HAL status
  2271. */
  2272. HAL_StatusTypeDef HAL_DFSDM_FilterInjectedMsbStart_DMA(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  2273. int16_t *pData,
  2274. uint32_t Length)
  2275. {
  2276. HAL_StatusTypeDef status = HAL_OK;
  2277. /* Check parameters */
  2278. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2279. /* Check destination address and length */
  2280. if ((pData == NULL) || (Length == 0U))
  2281. {
  2282. status = HAL_ERROR;
  2283. }
  2284. /* Check that DMA is enabled for injected conversion */
  2285. else if ((hdfsdm_filter->Instance->FLTCR1 & DFSDM_FLTCR1_JDMAEN) != DFSDM_FLTCR1_JDMAEN)
  2286. {
  2287. status = HAL_ERROR;
  2288. }
  2289. /* Check parameters compatibility */
  2290. else if ((hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_SW_TRIGGER) && \
  2291. (hdfsdm_filter->hdmaInj->Init.Mode == DMA_NORMAL) && \
  2292. (Length > hdfsdm_filter->InjConvRemaining))
  2293. {
  2294. status = HAL_ERROR;
  2295. }
  2296. else if ((hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_SW_TRIGGER) && \
  2297. (hdfsdm_filter->hdmaInj->Init.Mode == DMA_CIRCULAR))
  2298. {
  2299. status = HAL_ERROR;
  2300. }
  2301. /* Check DFSDM filter state */
  2302. else if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_READY) || \
  2303. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_REG))
  2304. {
  2305. /* Set callbacks on DMA handler */
  2306. hdfsdm_filter->hdmaInj->XferCpltCallback = DFSDM_DMAInjectedConvCplt;
  2307. hdfsdm_filter->hdmaInj->XferErrorCallback = DFSDM_DMAError;
  2308. hdfsdm_filter->hdmaInj->XferHalfCpltCallback = (hdfsdm_filter->hdmaInj->Init.Mode == DMA_CIRCULAR) ? \
  2309. DFSDM_DMAInjectedHalfConvCplt : NULL;
  2310. /* Start DMA in interrupt mode */
  2311. if (HAL_DMA_Start_IT(hdfsdm_filter->hdmaInj, (uint32_t)(&hdfsdm_filter->Instance->FLTJDATAR) + 2U, \
  2312. (uint32_t) pData, Length) != HAL_OK)
  2313. {
  2314. /* Set DFSDM filter in error state */
  2315. hdfsdm_filter->State = HAL_DFSDM_FILTER_STATE_ERROR;
  2316. status = HAL_ERROR;
  2317. }
  2318. else
  2319. {
  2320. /* Start injected conversion */
  2321. DFSDM_InjConvStart(hdfsdm_filter);
  2322. }
  2323. }
  2324. else
  2325. {
  2326. status = HAL_ERROR;
  2327. }
  2328. /* Return function status */
  2329. return status;
  2330. }
  2331. /**
  2332. * @brief This function allows to stop injected conversion in DMA mode.
  2333. * @note This function should be called only if injected conversion is ongoing.
  2334. * @param hdfsdm_filter DFSDM filter handle.
  2335. * @retval HAL status
  2336. */
  2337. HAL_StatusTypeDef HAL_DFSDM_FilterInjectedStop_DMA(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2338. {
  2339. HAL_StatusTypeDef status = HAL_OK;
  2340. /* Check parameters */
  2341. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2342. /* Check DFSDM filter state */
  2343. if ((hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_INJ) && \
  2344. (hdfsdm_filter->State != HAL_DFSDM_FILTER_STATE_REG_INJ))
  2345. {
  2346. /* Return error status */
  2347. status = HAL_ERROR;
  2348. }
  2349. else
  2350. {
  2351. /* Stop current DMA transfer */
  2352. /* No need to check the returned value of HAL_DMA_Abort. */
  2353. /* Only HAL_DMA_ERROR_NO_XFER can be returned in case of error and it's not an error for DFSDM. */
  2354. (void) HAL_DMA_Abort(hdfsdm_filter->hdmaInj);
  2355. /* Stop regular conversion */
  2356. DFSDM_InjConvStop(hdfsdm_filter);
  2357. }
  2358. /* Return function status */
  2359. return status;
  2360. }
  2361. /**
  2362. * @brief This function allows to get injected conversion value.
  2363. * @param hdfsdm_filter DFSDM filter handle.
  2364. * @param Channel Corresponding channel of injected conversion.
  2365. * @retval Injected conversion value
  2366. */
  2367. int32_t HAL_DFSDM_FilterGetInjectedValue(const DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  2368. uint32_t *Channel)
  2369. {
  2370. uint32_t reg;
  2371. int32_t value;
  2372. /* Check parameters */
  2373. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2374. assert_param(Channel != (void *)0);
  2375. /* Get value of data register for injected channel */
  2376. reg = hdfsdm_filter->Instance->FLTJDATAR;
  2377. /* Extract channel and injected conversion value */
  2378. *Channel = (reg & DFSDM_FLTJDATAR_JDATACH);
  2379. /* Injected conversion value is a signed value located on 24 MSB of register */
  2380. /* So after applying a mask on these bits we have to perform a division by 256 (2 raised to the power of 8) */
  2381. reg &= DFSDM_FLTJDATAR_JDATA;
  2382. value = ((int32_t)reg) / 256;
  2383. /* return regular conversion value */
  2384. return value;
  2385. }
  2386. /**
  2387. * @brief This function allows to start filter analog watchdog in interrupt mode.
  2388. * @param hdfsdm_filter DFSDM filter handle.
  2389. * @param awdParam DFSDM filter analog watchdog parameters.
  2390. * @retval HAL status
  2391. */
  2392. HAL_StatusTypeDef HAL_DFSDM_FilterAwdStart_IT(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  2393. const DFSDM_Filter_AwdParamTypeDef *awdParam)
  2394. {
  2395. HAL_StatusTypeDef status = HAL_OK;
  2396. /* Check parameters */
  2397. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2398. assert_param(IS_DFSDM_FILTER_AWD_DATA_SOURCE(awdParam->DataSource));
  2399. assert_param(IS_DFSDM_INJECTED_CHANNEL(awdParam->Channel));
  2400. assert_param(IS_DFSDM_FILTER_AWD_THRESHOLD(awdParam->HighThreshold));
  2401. assert_param(IS_DFSDM_FILTER_AWD_THRESHOLD(awdParam->LowThreshold));
  2402. assert_param(IS_DFSDM_BREAK_SIGNALS(awdParam->HighBreakSignal));
  2403. assert_param(IS_DFSDM_BREAK_SIGNALS(awdParam->LowBreakSignal));
  2404. /* Check DFSDM filter state */
  2405. if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_RESET) || \
  2406. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_ERROR))
  2407. {
  2408. /* Return error status */
  2409. status = HAL_ERROR;
  2410. }
  2411. else
  2412. {
  2413. /* Set analog watchdog data source */
  2414. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_AWFSEL);
  2415. hdfsdm_filter->Instance->FLTCR1 |= awdParam->DataSource;
  2416. /* Set thresholds and break signals */
  2417. hdfsdm_filter->Instance->FLTAWHTR &= ~(DFSDM_FLTAWHTR_AWHT | DFSDM_FLTAWHTR_BKAWH);
  2418. hdfsdm_filter->Instance->FLTAWHTR |= (((uint32_t) awdParam->HighThreshold << DFSDM_FLTAWHTR_AWHT_Pos) | \
  2419. awdParam->HighBreakSignal);
  2420. hdfsdm_filter->Instance->FLTAWLTR &= ~(DFSDM_FLTAWLTR_AWLT | DFSDM_FLTAWLTR_BKAWL);
  2421. hdfsdm_filter->Instance->FLTAWLTR |= (((uint32_t) awdParam->LowThreshold << DFSDM_FLTAWLTR_AWLT_Pos) | \
  2422. awdParam->LowBreakSignal);
  2423. /* Set channels and interrupt for analog watchdog */
  2424. hdfsdm_filter->Instance->FLTCR2 &= ~(DFSDM_FLTCR2_AWDCH);
  2425. hdfsdm_filter->Instance->FLTCR2 |= (((awdParam->Channel & DFSDM_LSB_MASK) << DFSDM_FLTCR2_AWDCH_Pos) | \
  2426. DFSDM_FLTCR2_AWDIE);
  2427. }
  2428. /* Return function status */
  2429. return status;
  2430. }
  2431. /**
  2432. * @brief This function allows to stop filter analog watchdog in interrupt mode.
  2433. * @param hdfsdm_filter DFSDM filter handle.
  2434. * @retval HAL status
  2435. */
  2436. HAL_StatusTypeDef HAL_DFSDM_FilterAwdStop_IT(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2437. {
  2438. HAL_StatusTypeDef status = HAL_OK;
  2439. /* Check parameters */
  2440. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2441. /* Check DFSDM filter state */
  2442. if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_RESET) || \
  2443. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_ERROR))
  2444. {
  2445. /* Return error status */
  2446. status = HAL_ERROR;
  2447. }
  2448. else
  2449. {
  2450. /* Reset channels for analog watchdog and deactivate interrupt */
  2451. hdfsdm_filter->Instance->FLTCR2 &= ~(DFSDM_FLTCR2_AWDCH | DFSDM_FLTCR2_AWDIE);
  2452. /* Clear all analog watchdog flags */
  2453. hdfsdm_filter->Instance->FLTAWCFR = (DFSDM_FLTAWCFR_CLRAWHTF | DFSDM_FLTAWCFR_CLRAWLTF);
  2454. /* Reset thresholds and break signals */
  2455. hdfsdm_filter->Instance->FLTAWHTR &= ~(DFSDM_FLTAWHTR_AWHT | DFSDM_FLTAWHTR_BKAWH);
  2456. hdfsdm_filter->Instance->FLTAWLTR &= ~(DFSDM_FLTAWLTR_AWLT | DFSDM_FLTAWLTR_BKAWL);
  2457. /* Reset analog watchdog data source */
  2458. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_AWFSEL);
  2459. }
  2460. /* Return function status */
  2461. return status;
  2462. }
  2463. /**
  2464. * @brief This function allows to start extreme detector feature.
  2465. * @param hdfsdm_filter DFSDM filter handle.
  2466. * @param Channel Channels where extreme detector is enabled.
  2467. * This parameter can be a values combination of @ref DFSDM_Channel_Selection.
  2468. * @retval HAL status
  2469. */
  2470. HAL_StatusTypeDef HAL_DFSDM_FilterExdStart(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  2471. uint32_t Channel)
  2472. {
  2473. HAL_StatusTypeDef status = HAL_OK;
  2474. /* Check parameters */
  2475. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2476. assert_param(IS_DFSDM_INJECTED_CHANNEL(Channel));
  2477. /* Check DFSDM filter state */
  2478. if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_RESET) || \
  2479. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_ERROR))
  2480. {
  2481. /* Return error status */
  2482. status = HAL_ERROR;
  2483. }
  2484. else
  2485. {
  2486. /* Set channels for extreme detector */
  2487. hdfsdm_filter->Instance->FLTCR2 &= ~(DFSDM_FLTCR2_EXCH);
  2488. hdfsdm_filter->Instance->FLTCR2 |= ((Channel & DFSDM_LSB_MASK) << DFSDM_FLTCR2_EXCH_Pos);
  2489. }
  2490. /* Return function status */
  2491. return status;
  2492. }
  2493. /**
  2494. * @brief This function allows to stop extreme detector feature.
  2495. * @param hdfsdm_filter DFSDM filter handle.
  2496. * @retval HAL status
  2497. */
  2498. HAL_StatusTypeDef HAL_DFSDM_FilterExdStop(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2499. {
  2500. HAL_StatusTypeDef status = HAL_OK;
  2501. __IO uint32_t reg1;
  2502. __IO uint32_t reg2;
  2503. /* Check parameters */
  2504. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2505. /* Check DFSDM filter state */
  2506. if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_RESET) || \
  2507. (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_ERROR))
  2508. {
  2509. /* Return error status */
  2510. status = HAL_ERROR;
  2511. }
  2512. else
  2513. {
  2514. /* Reset channels for extreme detector */
  2515. hdfsdm_filter->Instance->FLTCR2 &= ~(DFSDM_FLTCR2_EXCH);
  2516. /* Clear extreme detector values */
  2517. reg1 = hdfsdm_filter->Instance->FLTEXMAX;
  2518. reg2 = hdfsdm_filter->Instance->FLTEXMIN;
  2519. UNUSED(reg1); /* To avoid GCC warning */
  2520. UNUSED(reg2); /* To avoid GCC warning */
  2521. }
  2522. /* Return function status */
  2523. return status;
  2524. }
  2525. /**
  2526. * @brief This function allows to get extreme detector maximum value.
  2527. * @param hdfsdm_filter DFSDM filter handle.
  2528. * @param Channel Corresponding channel.
  2529. * @retval Extreme detector maximum value
  2530. * This value is between Min_Data = -8388608 and Max_Data = 8388607.
  2531. */
  2532. int32_t HAL_DFSDM_FilterGetExdMaxValue(const DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  2533. uint32_t *Channel)
  2534. {
  2535. uint32_t reg;
  2536. int32_t value;
  2537. /* Check parameters */
  2538. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2539. assert_param(Channel != (void *)0);
  2540. /* Get value of extreme detector maximum register */
  2541. reg = hdfsdm_filter->Instance->FLTEXMAX;
  2542. /* Extract channel and extreme detector maximum value */
  2543. *Channel = (reg & DFSDM_FLTEXMAX_EXMAXCH);
  2544. /* Extreme detector maximum value is a signed value located on 24 MSB of register */
  2545. /* So after applying a mask on these bits we have to perform a division by 256 (2 raised to the power of 8) */
  2546. reg &= DFSDM_FLTEXMAX_EXMAX;
  2547. value = ((int32_t)reg) / 256;
  2548. /* return extreme detector maximum value */
  2549. return value;
  2550. }
  2551. /**
  2552. * @brief This function allows to get extreme detector minimum value.
  2553. * @param hdfsdm_filter DFSDM filter handle.
  2554. * @param Channel Corresponding channel.
  2555. * @retval Extreme detector minimum value
  2556. * This value is between Min_Data = -8388608 and Max_Data = 8388607.
  2557. */
  2558. int32_t HAL_DFSDM_FilterGetExdMinValue(const DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  2559. uint32_t *Channel)
  2560. {
  2561. uint32_t reg;
  2562. int32_t value;
  2563. /* Check parameters */
  2564. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2565. assert_param(Channel != (void *)0);
  2566. /* Get value of extreme detector minimum register */
  2567. reg = hdfsdm_filter->Instance->FLTEXMIN;
  2568. /* Extract channel and extreme detector minimum value */
  2569. *Channel = (reg & DFSDM_FLTEXMIN_EXMINCH);
  2570. /* Extreme detector minimum value is a signed value located on 24 MSB of register */
  2571. /* So after applying a mask on these bits we have to perform a division by 256 (2 raised to the power of 8) */
  2572. reg &= DFSDM_FLTEXMIN_EXMIN;
  2573. value = ((int32_t)reg) / 256;
  2574. /* return extreme detector minimum value */
  2575. return value;
  2576. }
  2577. /**
  2578. * @brief This function allows to get conversion time value.
  2579. * @param hdfsdm_filter DFSDM filter handle.
  2580. * @retval Conversion time value
  2581. * @note To get time in second, this value has to be divided by DFSDM clock frequency.
  2582. */
  2583. uint32_t HAL_DFSDM_FilterGetConvTimeValue(const DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2584. {
  2585. uint32_t reg;
  2586. uint32_t value;
  2587. /* Check parameters */
  2588. assert_param(IS_DFSDM_FILTER_ALL_INSTANCE(hdfsdm_filter->Instance));
  2589. /* Get value of conversion timer register */
  2590. reg = hdfsdm_filter->Instance->FLTCNVTIMR;
  2591. /* Extract conversion time value */
  2592. value = ((reg & DFSDM_FLTCNVTIMR_CNVCNT) >> DFSDM_FLTCNVTIMR_CNVCNT_Pos);
  2593. /* return extreme detector minimum value */
  2594. return value;
  2595. }
  2596. /**
  2597. * @brief This function handles the DFSDM interrupts.
  2598. * @param hdfsdm_filter DFSDM filter handle.
  2599. * @retval None
  2600. */
  2601. void HAL_DFSDM_IRQHandler(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2602. {
  2603. /* Get FTLISR and FLTCR2 register values */
  2604. const uint32_t temp_fltisr = hdfsdm_filter->Instance->FLTISR;
  2605. const uint32_t temp_fltcr2 = hdfsdm_filter->Instance->FLTCR2;
  2606. /* Check if overrun occurs during regular conversion */
  2607. if (((temp_fltisr & DFSDM_FLTISR_ROVRF) != 0U) && \
  2608. ((temp_fltcr2 & DFSDM_FLTCR2_ROVRIE) != 0U))
  2609. {
  2610. /* Clear regular overrun flag */
  2611. hdfsdm_filter->Instance->FLTICR = DFSDM_FLTICR_CLRROVRF;
  2612. /* Update error code */
  2613. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_REGULAR_OVERRUN;
  2614. /* Call error callback */
  2615. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2616. hdfsdm_filter->ErrorCallback(hdfsdm_filter);
  2617. #else
  2618. HAL_DFSDM_FilterErrorCallback(hdfsdm_filter);
  2619. #endif
  2620. }
  2621. /* Check if overrun occurs during injected conversion */
  2622. else if (((temp_fltisr & DFSDM_FLTISR_JOVRF) != 0U) && \
  2623. ((temp_fltcr2 & DFSDM_FLTCR2_JOVRIE) != 0U))
  2624. {
  2625. /* Clear injected overrun flag */
  2626. hdfsdm_filter->Instance->FLTICR = DFSDM_FLTICR_CLRJOVRF;
  2627. /* Update error code */
  2628. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_INJECTED_OVERRUN;
  2629. /* Call error callback */
  2630. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2631. hdfsdm_filter->ErrorCallback(hdfsdm_filter);
  2632. #else
  2633. HAL_DFSDM_FilterErrorCallback(hdfsdm_filter);
  2634. #endif
  2635. }
  2636. /* Check if end of regular conversion */
  2637. else if (((temp_fltisr & DFSDM_FLTISR_REOCF) != 0U) && \
  2638. ((temp_fltcr2 & DFSDM_FLTCR2_REOCIE) != 0U))
  2639. {
  2640. /* Call regular conversion complete callback */
  2641. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2642. hdfsdm_filter->RegConvCpltCallback(hdfsdm_filter);
  2643. #else
  2644. HAL_DFSDM_FilterRegConvCpltCallback(hdfsdm_filter);
  2645. #endif
  2646. /* End of conversion if mode is not continuous and software trigger */
  2647. if ((hdfsdm_filter->RegularContMode == DFSDM_CONTINUOUS_CONV_OFF) && \
  2648. (hdfsdm_filter->RegularTrigger == DFSDM_FILTER_SW_TRIGGER))
  2649. {
  2650. /* Disable interrupts for regular conversions */
  2651. hdfsdm_filter->Instance->FLTCR2 &= ~(DFSDM_FLTCR2_REOCIE);
  2652. /* Update DFSDM filter state */
  2653. hdfsdm_filter->State = (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_REG) ? \
  2654. HAL_DFSDM_FILTER_STATE_READY : HAL_DFSDM_FILTER_STATE_INJ;
  2655. }
  2656. }
  2657. /* Check if end of injected conversion */
  2658. else if (((temp_fltisr & DFSDM_FLTISR_JEOCF) != 0U) && \
  2659. ((temp_fltcr2 & DFSDM_FLTCR2_JEOCIE) != 0U))
  2660. {
  2661. /* Call injected conversion complete callback */
  2662. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2663. hdfsdm_filter->InjConvCpltCallback(hdfsdm_filter);
  2664. #else
  2665. HAL_DFSDM_FilterInjConvCpltCallback(hdfsdm_filter);
  2666. #endif
  2667. /* Update remaining injected conversions */
  2668. hdfsdm_filter->InjConvRemaining--;
  2669. if (hdfsdm_filter->InjConvRemaining == 0U)
  2670. {
  2671. /* End of conversion if trigger is software */
  2672. if (hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_SW_TRIGGER)
  2673. {
  2674. /* Disable interrupts for injected conversions */
  2675. hdfsdm_filter->Instance->FLTCR2 &= ~(DFSDM_FLTCR2_JEOCIE);
  2676. /* Update DFSDM filter state */
  2677. hdfsdm_filter->State = (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_INJ) ? \
  2678. HAL_DFSDM_FILTER_STATE_READY : HAL_DFSDM_FILTER_STATE_REG;
  2679. }
  2680. /* end of injected sequence, reset the value */
  2681. hdfsdm_filter->InjConvRemaining = (hdfsdm_filter->InjectedScanMode == ENABLE) ? \
  2682. hdfsdm_filter->InjectedChannelsNbr : 1U;
  2683. }
  2684. }
  2685. /* Check if analog watchdog occurs */
  2686. else if (((temp_fltisr & DFSDM_FLTISR_AWDF) != 0U) && \
  2687. ((temp_fltcr2 & DFSDM_FLTCR2_AWDIE) != 0U))
  2688. {
  2689. uint32_t reg;
  2690. uint32_t threshold;
  2691. uint32_t channel = 0;
  2692. /* Get channel and threshold */
  2693. reg = hdfsdm_filter->Instance->FLTAWSR;
  2694. threshold = ((reg & DFSDM_FLTAWSR_AWLTF) != 0U) ? DFSDM_AWD_LOW_THRESHOLD : DFSDM_AWD_HIGH_THRESHOLD;
  2695. if (threshold == DFSDM_AWD_HIGH_THRESHOLD)
  2696. {
  2697. reg = reg >> DFSDM_FLTAWSR_AWHTF_Pos;
  2698. }
  2699. while (((reg & 1U) == 0U) && (channel < (DFSDM1_CHANNEL_NUMBER - 1U)))
  2700. {
  2701. channel++;
  2702. reg = reg >> 1;
  2703. }
  2704. /* Clear analog watchdog flag */
  2705. hdfsdm_filter->Instance->FLTAWCFR = (threshold == DFSDM_AWD_HIGH_THRESHOLD) ? \
  2706. (1UL << (DFSDM_FLTAWSR_AWHTF_Pos + channel)) : \
  2707. (1UL << channel);
  2708. /* Call analog watchdog callback */
  2709. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2710. hdfsdm_filter->AwdCallback(hdfsdm_filter, channel, threshold);
  2711. #else
  2712. HAL_DFSDM_FilterAwdCallback(hdfsdm_filter, channel, threshold);
  2713. #endif
  2714. }
  2715. /* Check if clock absence occurs */
  2716. else if ((hdfsdm_filter->Instance == DFSDM1_Filter0) && \
  2717. ((temp_fltisr & DFSDM_FLTISR_CKABF) != 0U) && \
  2718. ((temp_fltcr2 & DFSDM_FLTCR2_CKABIE) != 0U))
  2719. {
  2720. uint32_t reg;
  2721. uint32_t channel = 0;
  2722. reg = ((hdfsdm_filter->Instance->FLTISR & DFSDM_FLTISR_CKABF) >> DFSDM_FLTISR_CKABF_Pos);
  2723. while (channel < DFSDM1_CHANNEL_NUMBER)
  2724. {
  2725. /* Check if flag is set and corresponding channel is enabled */
  2726. if (((reg & 1U) != 0U) && (a_dfsdm1ChannelHandle[channel] != NULL))
  2727. {
  2728. /* Check clock absence has been enabled for this channel */
  2729. if ((a_dfsdm1ChannelHandle[channel]->Instance->CHCFGR1 & DFSDM_CHCFGR1_CKABEN) != 0U)
  2730. {
  2731. /* Clear clock absence flag */
  2732. hdfsdm_filter->Instance->FLTICR = (1UL << (DFSDM_FLTICR_CLRCKABF_Pos + channel));
  2733. /* Call clock absence callback */
  2734. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2735. a_dfsdm1ChannelHandle[channel]->CkabCallback(a_dfsdm1ChannelHandle[channel]);
  2736. #else
  2737. HAL_DFSDM_ChannelCkabCallback(a_dfsdm1ChannelHandle[channel]);
  2738. #endif
  2739. }
  2740. }
  2741. channel++;
  2742. reg = reg >> 1;
  2743. }
  2744. }
  2745. /* Check if short circuit detection occurs */
  2746. else if ((hdfsdm_filter->Instance == DFSDM1_Filter0) && \
  2747. ((temp_fltisr & DFSDM_FLTISR_SCDF) != 0U) && \
  2748. ((temp_fltcr2 & DFSDM_FLTCR2_SCDIE) != 0U))
  2749. {
  2750. uint32_t reg;
  2751. uint32_t channel = 0;
  2752. /* Get channel */
  2753. reg = ((hdfsdm_filter->Instance->FLTISR & DFSDM_FLTISR_SCDF) >> DFSDM_FLTISR_SCDF_Pos);
  2754. while (((reg & 1U) == 0U) && (channel < (DFSDM1_CHANNEL_NUMBER - 1U)))
  2755. {
  2756. channel++;
  2757. reg = reg >> 1;
  2758. }
  2759. /* Clear short circuit detection flag */
  2760. hdfsdm_filter->Instance->FLTICR = (1UL << (DFSDM_FLTICR_CLRSCDF_Pos + channel));
  2761. /* Call short circuit detection callback */
  2762. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2763. a_dfsdm1ChannelHandle[channel]->ScdCallback(a_dfsdm1ChannelHandle[channel]);
  2764. #else
  2765. HAL_DFSDM_ChannelScdCallback(a_dfsdm1ChannelHandle[channel]);
  2766. #endif
  2767. }
  2768. }
  2769. /**
  2770. * @brief Regular conversion complete callback.
  2771. * @note In interrupt mode, user has to read conversion value in this function
  2772. * using HAL_DFSDM_FilterGetRegularValue.
  2773. * @param hdfsdm_filter DFSDM filter handle.
  2774. * @retval None
  2775. */
  2776. __weak void HAL_DFSDM_FilterRegConvCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2777. {
  2778. /* Prevent unused argument(s) compilation warning */
  2779. UNUSED(hdfsdm_filter);
  2780. /* NOTE : This function should not be modified, when the callback is needed,
  2781. the HAL_DFSDM_FilterRegConvCpltCallback could be implemented in the user file.
  2782. */
  2783. }
  2784. /**
  2785. * @brief Half regular conversion complete callback.
  2786. * @param hdfsdm_filter DFSDM filter handle.
  2787. * @retval None
  2788. */
  2789. __weak void HAL_DFSDM_FilterRegConvHalfCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2790. {
  2791. /* Prevent unused argument(s) compilation warning */
  2792. UNUSED(hdfsdm_filter);
  2793. /* NOTE : This function should not be modified, when the callback is needed,
  2794. the HAL_DFSDM_FilterRegConvHalfCpltCallback could be implemented in the user file.
  2795. */
  2796. }
  2797. /**
  2798. * @brief Injected conversion complete callback.
  2799. * @note In interrupt mode, user has to read conversion value in this function
  2800. * using HAL_DFSDM_FilterGetInjectedValue.
  2801. * @param hdfsdm_filter DFSDM filter handle.
  2802. * @retval None
  2803. */
  2804. __weak void HAL_DFSDM_FilterInjConvCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2805. {
  2806. /* Prevent unused argument(s) compilation warning */
  2807. UNUSED(hdfsdm_filter);
  2808. /* NOTE : This function should not be modified, when the callback is needed,
  2809. the HAL_DFSDM_FilterInjConvCpltCallback could be implemented in the user file.
  2810. */
  2811. }
  2812. /**
  2813. * @brief Half injected conversion complete callback.
  2814. * @param hdfsdm_filter DFSDM filter handle.
  2815. * @retval None
  2816. */
  2817. __weak void HAL_DFSDM_FilterInjConvHalfCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2818. {
  2819. /* Prevent unused argument(s) compilation warning */
  2820. UNUSED(hdfsdm_filter);
  2821. /* NOTE : This function should not be modified, when the callback is needed,
  2822. the HAL_DFSDM_FilterInjConvHalfCpltCallback could be implemented in the user file.
  2823. */
  2824. }
  2825. /**
  2826. * @brief Filter analog watchdog callback.
  2827. * @param hdfsdm_filter DFSDM filter handle.
  2828. * @param Channel Corresponding channel.
  2829. * @param Threshold Low or high threshold has been reached.
  2830. * @retval None
  2831. */
  2832. __weak void HAL_DFSDM_FilterAwdCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter,
  2833. uint32_t Channel, uint32_t Threshold)
  2834. {
  2835. /* Prevent unused argument(s) compilation warning */
  2836. UNUSED(hdfsdm_filter);
  2837. UNUSED(Channel);
  2838. UNUSED(Threshold);
  2839. /* NOTE : This function should not be modified, when the callback is needed,
  2840. the HAL_DFSDM_FilterAwdCallback could be implemented in the user file.
  2841. */
  2842. }
  2843. /**
  2844. * @brief Error callback.
  2845. * @param hdfsdm_filter DFSDM filter handle.
  2846. * @retval None
  2847. */
  2848. __weak void HAL_DFSDM_FilterErrorCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2849. {
  2850. /* Prevent unused argument(s) compilation warning */
  2851. UNUSED(hdfsdm_filter);
  2852. /* NOTE : This function should not be modified, when the callback is needed,
  2853. the HAL_DFSDM_FilterErrorCallback could be implemented in the user file.
  2854. */
  2855. }
  2856. /**
  2857. * @}
  2858. */
  2859. /** @defgroup DFSDM_Exported_Functions_Group4_Filter Filter state functions
  2860. * @brief Filter state functions
  2861. *
  2862. @verbatim
  2863. ==============================================================================
  2864. ##### Filter state functions #####
  2865. ==============================================================================
  2866. [..] This section provides functions allowing to:
  2867. (+) Get the DFSDM filter state.
  2868. (+) Get the DFSDM filter error.
  2869. @endverbatim
  2870. * @{
  2871. */
  2872. /**
  2873. * @brief This function allows to get the current DFSDM filter handle state.
  2874. * @param hdfsdm_filter DFSDM filter handle.
  2875. * @retval DFSDM filter state.
  2876. */
  2877. HAL_DFSDM_Filter_StateTypeDef HAL_DFSDM_FilterGetState(const DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2878. {
  2879. /* Return DFSDM filter handle state */
  2880. return hdfsdm_filter->State;
  2881. }
  2882. /**
  2883. * @brief This function allows to get the current DFSDM filter error.
  2884. * @param hdfsdm_filter DFSDM filter handle.
  2885. * @retval DFSDM filter error code.
  2886. */
  2887. uint32_t HAL_DFSDM_FilterGetError(const DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  2888. {
  2889. return hdfsdm_filter->ErrorCode;
  2890. }
  2891. /**
  2892. * @}
  2893. */
  2894. /**
  2895. * @}
  2896. */
  2897. /* End of exported functions -------------------------------------------------*/
  2898. /* Private functions ---------------------------------------------------------*/
  2899. /** @addtogroup DFSDM_Private_Functions DFSDM Private Functions
  2900. * @{
  2901. */
  2902. /**
  2903. * @brief DMA half transfer complete callback for regular conversion.
  2904. * @param hdma DMA handle.
  2905. * @retval None
  2906. */
  2907. static void DFSDM_DMARegularHalfConvCplt(DMA_HandleTypeDef *hdma)
  2908. {
  2909. /* Get DFSDM filter handle */
  2910. DFSDM_Filter_HandleTypeDef *hdfsdm_filter = (DFSDM_Filter_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2911. /* Call regular half conversion complete callback */
  2912. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2913. hdfsdm_filter->RegConvHalfCpltCallback(hdfsdm_filter);
  2914. #else
  2915. HAL_DFSDM_FilterRegConvHalfCpltCallback(hdfsdm_filter);
  2916. #endif
  2917. }
  2918. /**
  2919. * @brief DMA transfer complete callback for regular conversion.
  2920. * @param hdma DMA handle.
  2921. * @retval None
  2922. */
  2923. static void DFSDM_DMARegularConvCplt(DMA_HandleTypeDef *hdma)
  2924. {
  2925. /* Get DFSDM filter handle */
  2926. DFSDM_Filter_HandleTypeDef *hdfsdm_filter = (DFSDM_Filter_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2927. /* Call regular conversion complete callback */
  2928. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2929. hdfsdm_filter->RegConvCpltCallback(hdfsdm_filter);
  2930. #else
  2931. HAL_DFSDM_FilterRegConvCpltCallback(hdfsdm_filter);
  2932. #endif
  2933. }
  2934. /**
  2935. * @brief DMA half transfer complete callback for injected conversion.
  2936. * @param hdma DMA handle.
  2937. * @retval None
  2938. */
  2939. static void DFSDM_DMAInjectedHalfConvCplt(DMA_HandleTypeDef *hdma)
  2940. {
  2941. /* Get DFSDM filter handle */
  2942. DFSDM_Filter_HandleTypeDef *hdfsdm_filter = (DFSDM_Filter_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2943. /* Call injected half conversion complete callback */
  2944. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2945. hdfsdm_filter->InjConvHalfCpltCallback(hdfsdm_filter);
  2946. #else
  2947. HAL_DFSDM_FilterInjConvHalfCpltCallback(hdfsdm_filter);
  2948. #endif
  2949. }
  2950. /**
  2951. * @brief DMA transfer complete callback for injected conversion.
  2952. * @param hdma DMA handle.
  2953. * @retval None
  2954. */
  2955. static void DFSDM_DMAInjectedConvCplt(DMA_HandleTypeDef *hdma)
  2956. {
  2957. /* Get DFSDM filter handle */
  2958. DFSDM_Filter_HandleTypeDef *hdfsdm_filter = (DFSDM_Filter_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2959. /* Call injected conversion complete callback */
  2960. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2961. hdfsdm_filter->InjConvCpltCallback(hdfsdm_filter);
  2962. #else
  2963. HAL_DFSDM_FilterInjConvCpltCallback(hdfsdm_filter);
  2964. #endif
  2965. }
  2966. /**
  2967. * @brief DMA error callback.
  2968. * @param hdma DMA handle.
  2969. * @retval None
  2970. */
  2971. static void DFSDM_DMAError(DMA_HandleTypeDef *hdma)
  2972. {
  2973. /* Get DFSDM filter handle */
  2974. DFSDM_Filter_HandleTypeDef *hdfsdm_filter = (DFSDM_Filter_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2975. /* Update error code */
  2976. hdfsdm_filter->ErrorCode = DFSDM_FILTER_ERROR_DMA;
  2977. /* Call error callback */
  2978. #if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
  2979. hdfsdm_filter->ErrorCallback(hdfsdm_filter);
  2980. #else
  2981. HAL_DFSDM_FilterErrorCallback(hdfsdm_filter);
  2982. #endif
  2983. }
  2984. /**
  2985. * @brief This function allows to get the number of injected channels.
  2986. * @param Channels bitfield of injected channels.
  2987. * @retval Number of injected channels.
  2988. */
  2989. static uint32_t DFSDM_GetInjChannelsNbr(uint32_t Channels)
  2990. {
  2991. uint32_t nbChannels = 0;
  2992. uint32_t tmp;
  2993. /* Get the number of channels from bitfield */
  2994. tmp = (uint32_t)(Channels & DFSDM_LSB_MASK);
  2995. while (tmp != 0U)
  2996. {
  2997. if ((tmp & 1U) != 0U)
  2998. {
  2999. nbChannels++;
  3000. }
  3001. tmp = (uint32_t)(tmp >> 1);
  3002. }
  3003. return nbChannels;
  3004. }
  3005. /**
  3006. * @brief This function allows to get the channel number from channel instance.
  3007. * @param Instance DFSDM channel instance.
  3008. * @retval Channel number.
  3009. */
  3010. static uint32_t DFSDM_GetChannelFromInstance(const DFSDM_Channel_TypeDef *Instance)
  3011. {
  3012. uint32_t channel;
  3013. /* Get channel from instance */
  3014. if (Instance == DFSDM1_Channel0)
  3015. {
  3016. channel = 0;
  3017. }
  3018. else if (Instance == DFSDM1_Channel1)
  3019. {
  3020. channel = 1;
  3021. }
  3022. else if (Instance == DFSDM1_Channel2)
  3023. {
  3024. channel = 2;
  3025. }
  3026. #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || \
  3027. defined(STM32L496xx) || defined(STM32L4A6xx) || \
  3028. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  3029. else if (Instance == DFSDM1_Channel4)
  3030. {
  3031. channel = 4;
  3032. }
  3033. else if (Instance == DFSDM1_Channel5)
  3034. {
  3035. channel = 5;
  3036. }
  3037. else if (Instance == DFSDM1_Channel6)
  3038. {
  3039. channel = 6;
  3040. }
  3041. else if (Instance == DFSDM1_Channel7)
  3042. {
  3043. channel = 7;
  3044. }
  3045. #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || STM32L496xx || STM32L4A6xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  3046. else /* DFSDM1_Channel3 */
  3047. {
  3048. channel = 3;
  3049. }
  3050. return channel;
  3051. }
  3052. /**
  3053. * @brief This function allows to really start regular conversion.
  3054. * @param hdfsdm_filter DFSDM filter handle.
  3055. * @retval None
  3056. */
  3057. static void DFSDM_RegConvStart(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  3058. {
  3059. /* Check regular trigger */
  3060. if (hdfsdm_filter->RegularTrigger == DFSDM_FILTER_SW_TRIGGER)
  3061. {
  3062. /* Software start of regular conversion */
  3063. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_RSWSTART;
  3064. }
  3065. else /* synchronous trigger */
  3066. {
  3067. /* Disable DFSDM filter */
  3068. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_DFEN);
  3069. /* Set RSYNC bit in DFSDM_FLTCR1 register */
  3070. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_RSYNC;
  3071. /* Enable DFSDM filter */
  3072. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_DFEN;
  3073. /* If injected conversion was in progress, restart it */
  3074. if (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_INJ)
  3075. {
  3076. if (hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_SW_TRIGGER)
  3077. {
  3078. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_JSWSTART;
  3079. }
  3080. /* Update remaining injected conversions */
  3081. hdfsdm_filter->InjConvRemaining = (hdfsdm_filter->InjectedScanMode == ENABLE) ? \
  3082. hdfsdm_filter->InjectedChannelsNbr : 1U;
  3083. }
  3084. }
  3085. /* Update DFSDM filter state */
  3086. hdfsdm_filter->State = (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_READY) ? \
  3087. HAL_DFSDM_FILTER_STATE_REG : HAL_DFSDM_FILTER_STATE_REG_INJ;
  3088. }
  3089. /**
  3090. * @brief This function allows to really stop regular conversion.
  3091. * @param hdfsdm_filter DFSDM filter handle.
  3092. * @retval None
  3093. */
  3094. static void DFSDM_RegConvStop(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  3095. {
  3096. /* Disable DFSDM filter */
  3097. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_DFEN);
  3098. /* If regular trigger was synchronous, reset RSYNC bit in DFSDM_FLTCR1 register */
  3099. if (hdfsdm_filter->RegularTrigger == DFSDM_FILTER_SYNC_TRIGGER)
  3100. {
  3101. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_RSYNC);
  3102. }
  3103. /* Enable DFSDM filter */
  3104. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_DFEN;
  3105. /* If injected conversion was in progress, restart it */
  3106. if (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_REG_INJ)
  3107. {
  3108. if (hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_SW_TRIGGER)
  3109. {
  3110. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_JSWSTART;
  3111. }
  3112. /* Update remaining injected conversions */
  3113. hdfsdm_filter->InjConvRemaining = (hdfsdm_filter->InjectedScanMode == ENABLE) ? \
  3114. hdfsdm_filter->InjectedChannelsNbr : 1U;
  3115. }
  3116. /* Update DFSDM filter state */
  3117. hdfsdm_filter->State = (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_REG) ? \
  3118. HAL_DFSDM_FILTER_STATE_READY : HAL_DFSDM_FILTER_STATE_INJ;
  3119. }
  3120. /**
  3121. * @brief This function allows to really start injected conversion.
  3122. * @param hdfsdm_filter DFSDM filter handle.
  3123. * @retval None
  3124. */
  3125. static void DFSDM_InjConvStart(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  3126. {
  3127. /* Check injected trigger */
  3128. if (hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_SW_TRIGGER)
  3129. {
  3130. /* Software start of injected conversion */
  3131. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_JSWSTART;
  3132. }
  3133. else /* external or synchronous trigger */
  3134. {
  3135. /* Disable DFSDM filter */
  3136. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_DFEN);
  3137. if (hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_SYNC_TRIGGER)
  3138. {
  3139. /* Set JSYNC bit in DFSDM_FLTCR1 register */
  3140. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_JSYNC;
  3141. }
  3142. else /* external trigger */
  3143. {
  3144. /* Set JEXTEN[1:0] bits in DFSDM_FLTCR1 register */
  3145. hdfsdm_filter->Instance->FLTCR1 |= hdfsdm_filter->ExtTriggerEdge;
  3146. }
  3147. /* Enable DFSDM filter */
  3148. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_DFEN;
  3149. /* If regular conversion was in progress, restart it */
  3150. if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_REG) && \
  3151. (hdfsdm_filter->RegularTrigger == DFSDM_FILTER_SW_TRIGGER))
  3152. {
  3153. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_RSWSTART;
  3154. }
  3155. }
  3156. /* Update DFSDM filter state */
  3157. hdfsdm_filter->State = (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_READY) ? \
  3158. HAL_DFSDM_FILTER_STATE_INJ : HAL_DFSDM_FILTER_STATE_REG_INJ;
  3159. }
  3160. /**
  3161. * @brief This function allows to really stop injected conversion.
  3162. * @param hdfsdm_filter DFSDM filter handle.
  3163. * @retval None
  3164. */
  3165. static void DFSDM_InjConvStop(DFSDM_Filter_HandleTypeDef *hdfsdm_filter)
  3166. {
  3167. /* Disable DFSDM filter */
  3168. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_DFEN);
  3169. /* If injected trigger was synchronous, reset JSYNC bit in DFSDM_FLTCR1 register */
  3170. if (hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_SYNC_TRIGGER)
  3171. {
  3172. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_JSYNC);
  3173. }
  3174. else if (hdfsdm_filter->InjectedTrigger == DFSDM_FILTER_EXT_TRIGGER)
  3175. {
  3176. /* Reset JEXTEN[1:0] bits in DFSDM_FLTCR1 register */
  3177. hdfsdm_filter->Instance->FLTCR1 &= ~(DFSDM_FLTCR1_JEXTEN);
  3178. }
  3179. else
  3180. {
  3181. /* Nothing to do */
  3182. }
  3183. /* Enable DFSDM filter */
  3184. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_DFEN;
  3185. /* If regular conversion was in progress, restart it */
  3186. if ((hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_REG_INJ) && \
  3187. (hdfsdm_filter->RegularTrigger == DFSDM_FILTER_SW_TRIGGER))
  3188. {
  3189. hdfsdm_filter->Instance->FLTCR1 |= DFSDM_FLTCR1_RSWSTART;
  3190. }
  3191. /* Update remaining injected conversions */
  3192. hdfsdm_filter->InjConvRemaining = (hdfsdm_filter->InjectedScanMode == ENABLE) ? \
  3193. hdfsdm_filter->InjectedChannelsNbr : 1U;
  3194. /* Update DFSDM filter state */
  3195. hdfsdm_filter->State = (hdfsdm_filter->State == HAL_DFSDM_FILTER_STATE_INJ) ? \
  3196. HAL_DFSDM_FILTER_STATE_READY : HAL_DFSDM_FILTER_STATE_REG;
  3197. }
  3198. /**
  3199. * @}
  3200. */
  3201. /* End of private functions --------------------------------------------------*/
  3202. /**
  3203. * @}
  3204. */
  3205. #endif /* STM32L451xx || STM32L452xx || STM32L462xx || STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || STM32L496xx || STM32L4A6xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx || STM32L4P5xx || STM32L4Q5xx */
  3206. #endif /* HAL_DFSDM_MODULE_ENABLED */
  3207. /**
  3208. * @}
  3209. */