stm32f4xx_hal_dfsdm.c 129 KB

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