stm32f0xx_hal_spi.c 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_spi.c
  4. * @author MCD Application Team
  5. * @brief SPI HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Serial Peripheral Interface (SPI) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### How to use this driver #####
  16. ==============================================================================
  17. [..]
  18. The SPI HAL driver can be used as follows:
  19. (#) Declare a SPI_HandleTypeDef handle structure, for example:
  20. SPI_HandleTypeDef hspi;
  21. (#)Initialize the SPI low level resources by implementing the HAL_SPI_MspInit() API:
  22. (##) Enable the SPIx interface clock
  23. (##) SPI pins configuration
  24. (+++) Enable the clock for the SPI GPIOs
  25. (+++) Configure these SPI pins as alternate function push-pull
  26. (##) NVIC configuration if you need to use interrupt process
  27. (+++) Configure the SPIx interrupt priority
  28. (+++) Enable the NVIC SPI IRQ handle
  29. (##) DMA Configuration if you need to use DMA process
  30. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive Stream/Channel
  31. (+++) Enable the DMAx clock
  32. (+++) Configure the DMA handle parameters
  33. (+++) Configure the DMA Tx or Rx Stream/Channel
  34. (+++) Associate the initialized hdma_tx(or _rx) handle to the hspi DMA Tx or Rx handle
  35. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx Stream/Channel
  36. (#) Program the Mode, BidirectionalMode , Data size, Baudrate Prescaler, NSS
  37. management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure.
  38. (#) Initialize the SPI registers by calling the HAL_SPI_Init() API:
  39. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  40. by calling the customized HAL_SPI_MspInit() API.
  41. [..]
  42. Circular mode restriction:
  43. (#) The DMA circular mode cannot be used when the SPI is configured in these modes:
  44. (##) Master 2Lines RxOnly
  45. (##) Master 1Line Rx
  46. (#) The CRC feature is not managed when the DMA circular mode is enabled
  47. (#) When the SPI DMA Pause/Stop features are used, we must use the following APIs
  48. the HAL_SPI_DMAPause()/ HAL_SPI_DMAStop() only under the SPI callbacks
  49. [..]
  50. Master Receive mode restriction:
  51. (#) In Master unidirectional receive-only mode (MSTR =1, BIDIMODE=0, RXONLY=1) or
  52. bidirectional receive mode (MSTR=1, BIDIMODE=1, BIDIOE=0), to ensure that the SPI
  53. does not initiate a new transfer the following procedure has to be respected:
  54. (##) HAL_SPI_DeInit()
  55. (##) HAL_SPI_Init()
  56. [..]
  57. Callback registration:
  58. (#) The compilation flag USE_HAL_SPI_REGISTER_CALLBACKS when set to 1U
  59. allows the user to configure dynamically the driver callbacks.
  60. Use Functions HAL_SPI_RegisterCallback() to register an interrupt callback.
  61. Function HAL_SPI_RegisterCallback() allows to register following callbacks:
  62. (++) TxCpltCallback : SPI Tx Completed callback
  63. (++) RxCpltCallback : SPI Rx Completed callback
  64. (++) TxRxCpltCallback : SPI TxRx Completed callback
  65. (++) TxHalfCpltCallback : SPI Tx Half Completed callback
  66. (++) RxHalfCpltCallback : SPI Rx Half Completed callback
  67. (++) TxRxHalfCpltCallback : SPI TxRx Half Completed callback
  68. (++) ErrorCallback : SPI Error callback
  69. (++) AbortCpltCallback : SPI Abort callback
  70. (++) MspInitCallback : SPI Msp Init callback
  71. (++) MspDeInitCallback : SPI Msp DeInit callback
  72. This function takes as parameters the HAL peripheral handle, the Callback ID
  73. and a pointer to the user callback function.
  74. (#) Use function HAL_SPI_UnRegisterCallback to reset a callback to the default
  75. weak function.
  76. HAL_SPI_UnRegisterCallback takes as parameters the HAL peripheral handle,
  77. and the Callback ID.
  78. This function allows to reset following callbacks:
  79. (++) TxCpltCallback : SPI Tx Completed callback
  80. (++) RxCpltCallback : SPI Rx Completed callback
  81. (++) TxRxCpltCallback : SPI TxRx Completed callback
  82. (++) TxHalfCpltCallback : SPI Tx Half Completed callback
  83. (++) RxHalfCpltCallback : SPI Rx Half Completed callback
  84. (++) TxRxHalfCpltCallback : SPI TxRx Half Completed callback
  85. (++) ErrorCallback : SPI Error callback
  86. (++) AbortCpltCallback : SPI Abort callback
  87. (++) MspInitCallback : SPI Msp Init callback
  88. (++) MspDeInitCallback : SPI Msp DeInit callback
  89. [..]
  90. By default, after the HAL_SPI_Init() and when the state is HAL_SPI_STATE_RESET
  91. all callbacks are set to the corresponding weak functions:
  92. examples HAL_SPI_MasterTxCpltCallback(), HAL_SPI_MasterRxCpltCallback().
  93. Exception done for MspInit and MspDeInit functions that are
  94. reset to the legacy weak functions in the HAL_SPI_Init()/ HAL_SPI_DeInit() only when
  95. these callbacks are null (not registered beforehand).
  96. If MspInit or MspDeInit are not null, the HAL_SPI_Init()/ HAL_SPI_DeInit()
  97. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  98. [..]
  99. Callbacks can be registered/unregistered in HAL_SPI_STATE_READY state only.
  100. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  101. in HAL_SPI_STATE_READY or HAL_SPI_STATE_RESET state,
  102. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  103. Then, the user first registers the MspInit/MspDeInit user callbacks
  104. using HAL_SPI_RegisterCallback() before calling HAL_SPI_DeInit()
  105. or HAL_SPI_Init() function.
  106. [..]
  107. When the compilation define USE_HAL_PPP_REGISTER_CALLBACKS is set to 0 or
  108. not defined, the callback registering feature is not available
  109. and weak (surcharged) callbacks are used.
  110. [..]
  111. Using the HAL it is not possible to reach all supported SPI frequency with the different SPI Modes,
  112. the following table resume the max SPI frequency reached with data size 8bits/16bits,
  113. according to frequency of the APBx Peripheral Clock (fPCLK) used by the SPI instance.
  114. @endverbatim
  115. Additional table :
  116. DataSize = SPI_DATASIZE_8BIT:
  117. +----------------------------------------------------------------------------------------------+
  118. | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line |
  119. | Process | Transfer mode |---------------------|----------------------|----------------------|
  120. | | | Master | Slave | Master | Slave | Master | Slave |
  121. |==============================================================================================|
  122. | T | Polling | Fpclk/4 | Fpclk/8 | NA | NA | NA | NA |
  123. | X |----------------|----------|----------|-----------|----------|-----------|----------|
  124. | / | Interrupt | Fpclk/4 | Fpclk/16 | NA | NA | NA | NA |
  125. | R |----------------|----------|----------|-----------|----------|-----------|----------|
  126. | X | DMA | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA |
  127. |=========|================|==========|==========|===========|==========|===========|==========|
  128. | | Polling | Fpclk/4 | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 |
  129. | |----------------|----------|----------|-----------|----------|-----------|----------|
  130. | R | Interrupt | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 | Fpclk/4 |
  131. | X |----------------|----------|----------|-----------|----------|-----------|----------|
  132. | | DMA | Fpclk/4 | Fpclk/2 | Fpclk/2 | Fpclk/16 | Fpclk/2 | Fpclk/16 |
  133. |=========|================|==========|==========|===========|==========|===========|==========|
  134. | | Polling | Fpclk/8 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/8 |
  135. | |----------------|----------|----------|-----------|----------|-----------|----------|
  136. | T | Interrupt | Fpclk/2 | Fpclk/4 | NA | NA | Fpclk/16 | Fpclk/8 |
  137. | X |----------------|----------|----------|-----------|----------|-----------|----------|
  138. | | DMA | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/16 |
  139. +----------------------------------------------------------------------------------------------+
  140. DataSize = SPI_DATASIZE_16BIT:
  141. +----------------------------------------------------------------------------------------------+
  142. | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line |
  143. | Process | Transfer mode |---------------------|----------------------|----------------------|
  144. | | | Master | Slave | Master | Slave | Master | Slave |
  145. |==============================================================================================|
  146. | T | Polling | Fpclk/4 | Fpclk/8 | NA | NA | NA | NA |
  147. | X |----------------|----------|----------|-----------|----------|-----------|----------|
  148. | / | Interrupt | Fpclk/4 | Fpclk/16 | NA | NA | NA | NA |
  149. | R |----------------|----------|----------|-----------|----------|-----------|----------|
  150. | X | DMA | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA |
  151. |=========|================|==========|==========|===========|==========|===========|==========|
  152. | | Polling | Fpclk/4 | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 |
  153. | |----------------|----------|----------|-----------|----------|-----------|----------|
  154. | R | Interrupt | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 | Fpclk/4 |
  155. | X |----------------|----------|----------|-----------|----------|-----------|----------|
  156. | | DMA | Fpclk/4 | Fpclk/2 | Fpclk/2 | Fpclk/16 | Fpclk/2 | Fpclk/16 |
  157. |=========|================|==========|==========|===========|==========|===========|==========|
  158. | | Polling | Fpclk/8 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/8 |
  159. | |----------------|----------|----------|-----------|----------|-----------|----------|
  160. | T | Interrupt | Fpclk/2 | Fpclk/4 | NA | NA | Fpclk/16 | Fpclk/8 |
  161. | X |----------------|----------|----------|-----------|----------|-----------|----------|
  162. | | DMA | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/16 |
  163. +----------------------------------------------------------------------------------------------+
  164. @note The max SPI frequency depend on SPI data size (4bits, 5bits,..., 8bits,...15bits, 16bits),
  165. SPI mode(2 Lines fullduplex, 2 lines RxOnly, 1 line TX/RX) and Process mode (Polling, IT, DMA).
  166. @note
  167. (#) TX/RX processes are HAL_SPI_TransmitReceive(), HAL_SPI_TransmitReceive_IT() and HAL_SPI_TransmitReceive_DMA()
  168. (#) RX processes are HAL_SPI_Receive(), HAL_SPI_Receive_IT() and HAL_SPI_Receive_DMA()
  169. (#) TX processes are HAL_SPI_Transmit(), HAL_SPI_Transmit_IT() and HAL_SPI_Transmit_DMA()
  170. ******************************************************************************
  171. * @attention
  172. *
  173. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  174. * All rights reserved.</center></h2>
  175. *
  176. * This software component is licensed by ST under BSD 3-Clause license,
  177. * the "License"; You may not use this file except in compliance with the
  178. * License. You may obtain a copy of the License at:
  179. * opensource.org/licenses/BSD-3-Clause
  180. *
  181. ******************************************************************************
  182. */
  183. /* Includes ------------------------------------------------------------------*/
  184. #include "stm32f0xx_hal.h"
  185. /** @addtogroup STM32F0xx_HAL_Driver
  186. * @{
  187. */
  188. /** @defgroup SPI SPI
  189. * @brief SPI HAL module driver
  190. * @{
  191. */
  192. #ifdef HAL_SPI_MODULE_ENABLED
  193. /* Private typedef -----------------------------------------------------------*/
  194. /* Private defines -----------------------------------------------------------*/
  195. /** @defgroup SPI_Private_Constants SPI Private Constants
  196. * @{
  197. */
  198. #define SPI_DEFAULT_TIMEOUT 100U
  199. /**
  200. * @}
  201. */
  202. /* Private macros ------------------------------------------------------------*/
  203. /* Private variables ---------------------------------------------------------*/
  204. /* Private function prototypes -----------------------------------------------*/
  205. /** @defgroup SPI_Private_Functions SPI Private Functions
  206. * @{
  207. */
  208. static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  209. static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  210. static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma);
  211. static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma);
  212. static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma);
  213. static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma);
  214. static void SPI_DMAError(DMA_HandleTypeDef *hdma);
  215. static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  216. static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  217. static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  218. static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State,
  219. uint32_t Timeout, uint32_t Tickstart);
  220. static HAL_StatusTypeDef SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Fifo, uint32_t State,
  221. uint32_t Timeout, uint32_t Tickstart);
  222. static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  223. static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  224. static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  225. static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  226. static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  227. static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  228. static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  229. static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  230. #if (USE_SPI_CRC != 0U)
  231. static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
  232. static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
  233. static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
  234. static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
  235. #endif /* USE_SPI_CRC */
  236. static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi);
  237. static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi);
  238. static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi);
  239. static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi);
  240. static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi);
  241. static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart);
  242. static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart);
  243. /**
  244. * @}
  245. */
  246. /* Exported functions --------------------------------------------------------*/
  247. /** @defgroup SPI_Exported_Functions SPI Exported Functions
  248. * @{
  249. */
  250. /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions
  251. * @brief Initialization and Configuration functions
  252. *
  253. @verbatim
  254. ===============================================================================
  255. ##### Initialization and de-initialization functions #####
  256. ===============================================================================
  257. [..] This subsection provides a set of functions allowing to initialize and
  258. de-initialize the SPIx peripheral:
  259. (+) User must implement HAL_SPI_MspInit() function in which he configures
  260. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  261. (+) Call the function HAL_SPI_Init() to configure the selected device with
  262. the selected configuration:
  263. (++) Mode
  264. (++) Direction
  265. (++) Data Size
  266. (++) Clock Polarity and Phase
  267. (++) NSS Management
  268. (++) BaudRate Prescaler
  269. (++) FirstBit
  270. (++) TIMode
  271. (++) CRC Calculation
  272. (++) CRC Polynomial if CRC enabled
  273. (++) CRC Length, used only with Data8 and Data16
  274. (++) FIFO reception threshold
  275. (+) Call the function HAL_SPI_DeInit() to restore the default configuration
  276. of the selected SPIx peripheral.
  277. @endverbatim
  278. * @{
  279. */
  280. /**
  281. * @brief Initialize the SPI according to the specified parameters
  282. * in the SPI_InitTypeDef and initialize the associated handle.
  283. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  284. * the configuration information for SPI module.
  285. * @retval HAL status
  286. */
  287. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
  288. {
  289. uint32_t frxth;
  290. /* Check the SPI handle allocation */
  291. if (hspi == NULL)
  292. {
  293. return HAL_ERROR;
  294. }
  295. /* Check the parameters */
  296. assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
  297. assert_param(IS_SPI_MODE(hspi->Init.Mode));
  298. assert_param(IS_SPI_DIRECTION(hspi->Init.Direction));
  299. assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
  300. assert_param(IS_SPI_NSS(hspi->Init.NSS));
  301. assert_param(IS_SPI_NSSP(hspi->Init.NSSPMode));
  302. assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
  303. assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
  304. assert_param(IS_SPI_TIMODE(hspi->Init.TIMode));
  305. if (hspi->Init.TIMode == SPI_TIMODE_DISABLE)
  306. {
  307. assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
  308. assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
  309. if (hspi->Init.Mode == SPI_MODE_MASTER)
  310. {
  311. assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
  312. }
  313. else
  314. {
  315. /* Baudrate prescaler not use in Motoraola Slave mode. force to default value */
  316. hspi->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  317. }
  318. }
  319. else
  320. {
  321. assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
  322. /* Force polarity and phase to TI protocaol requirements */
  323. hspi->Init.CLKPolarity = SPI_POLARITY_LOW;
  324. hspi->Init.CLKPhase = SPI_PHASE_1EDGE;
  325. }
  326. #if (USE_SPI_CRC != 0U)
  327. assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
  328. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  329. {
  330. assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
  331. assert_param(IS_SPI_CRC_LENGTH(hspi->Init.CRCLength));
  332. }
  333. #else
  334. hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  335. #endif /* USE_SPI_CRC */
  336. if (hspi->State == HAL_SPI_STATE_RESET)
  337. {
  338. /* Allocate lock resource and initialize it */
  339. hspi->Lock = HAL_UNLOCKED;
  340. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  341. /* Init the SPI Callback settings */
  342. hspi->TxCpltCallback = HAL_SPI_TxCpltCallback; /* Legacy weak TxCpltCallback */
  343. hspi->RxCpltCallback = HAL_SPI_RxCpltCallback; /* Legacy weak RxCpltCallback */
  344. hspi->TxRxCpltCallback = HAL_SPI_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
  345. hspi->TxHalfCpltCallback = HAL_SPI_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  346. hspi->RxHalfCpltCallback = HAL_SPI_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  347. hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
  348. hspi->ErrorCallback = HAL_SPI_ErrorCallback; /* Legacy weak ErrorCallback */
  349. hspi->AbortCpltCallback = HAL_SPI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  350. if (hspi->MspInitCallback == NULL)
  351. {
  352. hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
  353. }
  354. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  355. hspi->MspInitCallback(hspi);
  356. #else
  357. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  358. HAL_SPI_MspInit(hspi);
  359. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  360. }
  361. hspi->State = HAL_SPI_STATE_BUSY;
  362. /* Disable the selected SPI peripheral */
  363. __HAL_SPI_DISABLE(hspi);
  364. /* Align by default the rs fifo threshold on the data size */
  365. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  366. {
  367. frxth = SPI_RXFIFO_THRESHOLD_HF;
  368. }
  369. else
  370. {
  371. frxth = SPI_RXFIFO_THRESHOLD_QF;
  372. }
  373. /* CRC calculation is valid only for 16Bit and 8 Bit */
  374. if ((hspi->Init.DataSize != SPI_DATASIZE_16BIT) && (hspi->Init.DataSize != SPI_DATASIZE_8BIT))
  375. {
  376. /* CRC must be disabled */
  377. hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  378. }
  379. /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/
  380. /* Configure : SPI Mode, Communication Mode, Clock polarity and phase, NSS management,
  381. Communication speed, First bit and CRC calculation state */
  382. WRITE_REG(hspi->Instance->CR1, ((hspi->Init.Mode & (SPI_CR1_MSTR | SPI_CR1_SSI)) |
  383. (hspi->Init.Direction & (SPI_CR1_RXONLY | SPI_CR1_BIDIMODE)) |
  384. (hspi->Init.CLKPolarity & SPI_CR1_CPOL) |
  385. (hspi->Init.CLKPhase & SPI_CR1_CPHA) |
  386. (hspi->Init.NSS & SPI_CR1_SSM) |
  387. (hspi->Init.BaudRatePrescaler & SPI_CR1_BR_Msk) |
  388. (hspi->Init.FirstBit & SPI_CR1_LSBFIRST) |
  389. (hspi->Init.CRCCalculation & SPI_CR1_CRCEN)));
  390. #if (USE_SPI_CRC != 0U)
  391. /*---------------------------- SPIx CRCL Configuration -------------------*/
  392. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  393. {
  394. /* Align the CRC Length on the data size */
  395. if (hspi->Init.CRCLength == SPI_CRC_LENGTH_DATASIZE)
  396. {
  397. /* CRC Length aligned on the data size : value set by default */
  398. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  399. {
  400. hspi->Init.CRCLength = SPI_CRC_LENGTH_16BIT;
  401. }
  402. else
  403. {
  404. hspi->Init.CRCLength = SPI_CRC_LENGTH_8BIT;
  405. }
  406. }
  407. /* Configure : CRC Length */
  408. if (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
  409. {
  410. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCL);
  411. }
  412. }
  413. #endif /* USE_SPI_CRC */
  414. /* Configure : NSS management, TI Mode, NSS Pulse, Data size and Rx Fifo threshold */
  415. WRITE_REG(hspi->Instance->CR2, (((hspi->Init.NSS >> 16U) & SPI_CR2_SSOE) |
  416. (hspi->Init.TIMode & SPI_CR2_FRF) |
  417. (hspi->Init.NSSPMode & SPI_CR2_NSSP) |
  418. (hspi->Init.DataSize & SPI_CR2_DS_Msk) |
  419. (frxth & SPI_CR2_FRXTH)));
  420. #if (USE_SPI_CRC != 0U)
  421. /*---------------------------- SPIx CRCPOLY Configuration ------------------*/
  422. /* Configure : CRC Polynomial */
  423. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  424. {
  425. WRITE_REG(hspi->Instance->CRCPR, (hspi->Init.CRCPolynomial & SPI_CRCPR_CRCPOLY_Msk));
  426. }
  427. #endif /* USE_SPI_CRC */
  428. #if defined(SPI_I2SCFGR_I2SMOD)
  429. /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */
  430. CLEAR_BIT(hspi->Instance->I2SCFGR, SPI_I2SCFGR_I2SMOD);
  431. #endif /* SPI_I2SCFGR_I2SMOD */
  432. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  433. hspi->State = HAL_SPI_STATE_READY;
  434. return HAL_OK;
  435. }
  436. /**
  437. * @brief De-Initialize the SPI peripheral.
  438. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  439. * the configuration information for SPI module.
  440. * @retval HAL status
  441. */
  442. HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
  443. {
  444. /* Check the SPI handle allocation */
  445. if (hspi == NULL)
  446. {
  447. return HAL_ERROR;
  448. }
  449. /* Check SPI Instance parameter */
  450. assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
  451. hspi->State = HAL_SPI_STATE_BUSY;
  452. /* Disable the SPI Peripheral Clock */
  453. __HAL_SPI_DISABLE(hspi);
  454. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  455. if (hspi->MspDeInitCallback == NULL)
  456. {
  457. hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
  458. }
  459. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  460. hspi->MspDeInitCallback(hspi);
  461. #else
  462. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  463. HAL_SPI_MspDeInit(hspi);
  464. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  465. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  466. hspi->State = HAL_SPI_STATE_RESET;
  467. /* Release Lock */
  468. __HAL_UNLOCK(hspi);
  469. return HAL_OK;
  470. }
  471. /**
  472. * @brief Initialize the SPI MSP.
  473. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  474. * the configuration information for SPI module.
  475. * @retval None
  476. */
  477. __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
  478. {
  479. /* Prevent unused argument(s) compilation warning */
  480. UNUSED(hspi);
  481. /* NOTE : This function should not be modified, when the callback is needed,
  482. the HAL_SPI_MspInit should be implemented in the user file
  483. */
  484. }
  485. /**
  486. * @brief De-Initialize the SPI MSP.
  487. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  488. * the configuration information for SPI module.
  489. * @retval None
  490. */
  491. __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
  492. {
  493. /* Prevent unused argument(s) compilation warning */
  494. UNUSED(hspi);
  495. /* NOTE : This function should not be modified, when the callback is needed,
  496. the HAL_SPI_MspDeInit should be implemented in the user file
  497. */
  498. }
  499. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  500. /**
  501. * @brief Register a User SPI Callback
  502. * To be used instead of the weak predefined callback
  503. * @param hspi Pointer to a SPI_HandleTypeDef structure that contains
  504. * the configuration information for the specified SPI.
  505. * @param CallbackID ID of the callback to be registered
  506. * @param pCallback pointer to the Callback function
  507. * @retval HAL status
  508. */
  509. HAL_StatusTypeDef HAL_SPI_RegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID,
  510. pSPI_CallbackTypeDef pCallback)
  511. {
  512. HAL_StatusTypeDef status = HAL_OK;
  513. if (pCallback == NULL)
  514. {
  515. /* Update the error code */
  516. hspi->ErrorCode |= HAL_SPI_ERROR_INVALID_CALLBACK;
  517. return HAL_ERROR;
  518. }
  519. /* Process locked */
  520. __HAL_LOCK(hspi);
  521. if (HAL_SPI_STATE_READY == hspi->State)
  522. {
  523. switch (CallbackID)
  524. {
  525. case HAL_SPI_TX_COMPLETE_CB_ID :
  526. hspi->TxCpltCallback = pCallback;
  527. break;
  528. case HAL_SPI_RX_COMPLETE_CB_ID :
  529. hspi->RxCpltCallback = pCallback;
  530. break;
  531. case HAL_SPI_TX_RX_COMPLETE_CB_ID :
  532. hspi->TxRxCpltCallback = pCallback;
  533. break;
  534. case HAL_SPI_TX_HALF_COMPLETE_CB_ID :
  535. hspi->TxHalfCpltCallback = pCallback;
  536. break;
  537. case HAL_SPI_RX_HALF_COMPLETE_CB_ID :
  538. hspi->RxHalfCpltCallback = pCallback;
  539. break;
  540. case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID :
  541. hspi->TxRxHalfCpltCallback = pCallback;
  542. break;
  543. case HAL_SPI_ERROR_CB_ID :
  544. hspi->ErrorCallback = pCallback;
  545. break;
  546. case HAL_SPI_ABORT_CB_ID :
  547. hspi->AbortCpltCallback = pCallback;
  548. break;
  549. case HAL_SPI_MSPINIT_CB_ID :
  550. hspi->MspInitCallback = pCallback;
  551. break;
  552. case HAL_SPI_MSPDEINIT_CB_ID :
  553. hspi->MspDeInitCallback = pCallback;
  554. break;
  555. default :
  556. /* Update the error code */
  557. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
  558. /* Return error status */
  559. status = HAL_ERROR;
  560. break;
  561. }
  562. }
  563. else if (HAL_SPI_STATE_RESET == hspi->State)
  564. {
  565. switch (CallbackID)
  566. {
  567. case HAL_SPI_MSPINIT_CB_ID :
  568. hspi->MspInitCallback = pCallback;
  569. break;
  570. case HAL_SPI_MSPDEINIT_CB_ID :
  571. hspi->MspDeInitCallback = pCallback;
  572. break;
  573. default :
  574. /* Update the error code */
  575. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
  576. /* Return error status */
  577. status = HAL_ERROR;
  578. break;
  579. }
  580. }
  581. else
  582. {
  583. /* Update the error code */
  584. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
  585. /* Return error status */
  586. status = HAL_ERROR;
  587. }
  588. /* Release Lock */
  589. __HAL_UNLOCK(hspi);
  590. return status;
  591. }
  592. /**
  593. * @brief Unregister an SPI Callback
  594. * SPI callback is redirected to the weak predefined callback
  595. * @param hspi Pointer to a SPI_HandleTypeDef structure that contains
  596. * the configuration information for the specified SPI.
  597. * @param CallbackID ID of the callback to be unregistered
  598. * @retval HAL status
  599. */
  600. HAL_StatusTypeDef HAL_SPI_UnRegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID)
  601. {
  602. HAL_StatusTypeDef status = HAL_OK;
  603. /* Process locked */
  604. __HAL_LOCK(hspi);
  605. if (HAL_SPI_STATE_READY == hspi->State)
  606. {
  607. switch (CallbackID)
  608. {
  609. case HAL_SPI_TX_COMPLETE_CB_ID :
  610. hspi->TxCpltCallback = HAL_SPI_TxCpltCallback; /* Legacy weak TxCpltCallback */
  611. break;
  612. case HAL_SPI_RX_COMPLETE_CB_ID :
  613. hspi->RxCpltCallback = HAL_SPI_RxCpltCallback; /* Legacy weak RxCpltCallback */
  614. break;
  615. case HAL_SPI_TX_RX_COMPLETE_CB_ID :
  616. hspi->TxRxCpltCallback = HAL_SPI_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
  617. break;
  618. case HAL_SPI_TX_HALF_COMPLETE_CB_ID :
  619. hspi->TxHalfCpltCallback = HAL_SPI_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
  620. break;
  621. case HAL_SPI_RX_HALF_COMPLETE_CB_ID :
  622. hspi->RxHalfCpltCallback = HAL_SPI_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
  623. break;
  624. case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID :
  625. hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
  626. break;
  627. case HAL_SPI_ERROR_CB_ID :
  628. hspi->ErrorCallback = HAL_SPI_ErrorCallback; /* Legacy weak ErrorCallback */
  629. break;
  630. case HAL_SPI_ABORT_CB_ID :
  631. hspi->AbortCpltCallback = HAL_SPI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  632. break;
  633. case HAL_SPI_MSPINIT_CB_ID :
  634. hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
  635. break;
  636. case HAL_SPI_MSPDEINIT_CB_ID :
  637. hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
  638. break;
  639. default :
  640. /* Update the error code */
  641. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
  642. /* Return error status */
  643. status = HAL_ERROR;
  644. break;
  645. }
  646. }
  647. else if (HAL_SPI_STATE_RESET == hspi->State)
  648. {
  649. switch (CallbackID)
  650. {
  651. case HAL_SPI_MSPINIT_CB_ID :
  652. hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
  653. break;
  654. case HAL_SPI_MSPDEINIT_CB_ID :
  655. hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
  656. break;
  657. default :
  658. /* Update the error code */
  659. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
  660. /* Return error status */
  661. status = HAL_ERROR;
  662. break;
  663. }
  664. }
  665. else
  666. {
  667. /* Update the error code */
  668. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
  669. /* Return error status */
  670. status = HAL_ERROR;
  671. }
  672. /* Release Lock */
  673. __HAL_UNLOCK(hspi);
  674. return status;
  675. }
  676. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  677. /**
  678. * @}
  679. */
  680. /** @defgroup SPI_Exported_Functions_Group2 IO operation functions
  681. * @brief Data transfers functions
  682. *
  683. @verbatim
  684. ==============================================================================
  685. ##### IO operation functions #####
  686. ===============================================================================
  687. [..]
  688. This subsection provides a set of functions allowing to manage the SPI
  689. data transfers.
  690. [..] The SPI supports master and slave mode :
  691. (#) There are two modes of transfer:
  692. (++) Blocking mode: The communication is performed in polling mode.
  693. The HAL status of all data processing is returned by the same function
  694. after finishing transfer.
  695. (++) No-Blocking mode: The communication is performed using Interrupts
  696. or DMA, These APIs return the HAL status.
  697. The end of the data processing will be indicated through the
  698. dedicated SPI IRQ when using Interrupt mode or the DMA IRQ when
  699. using DMA mode.
  700. The HAL_SPI_TxCpltCallback(), HAL_SPI_RxCpltCallback() and HAL_SPI_TxRxCpltCallback() user callbacks
  701. will be executed respectively at the end of the transmit or Receive process
  702. The HAL_SPI_ErrorCallback()user callback will be executed when a communication error is detected
  703. (#) APIs provided for these 2 transfer modes (Blocking mode or Non blocking mode using either Interrupt or DMA)
  704. exist for 1Line (simplex) and 2Lines (full duplex) modes.
  705. @endverbatim
  706. * @{
  707. */
  708. /**
  709. * @brief Transmit an amount of data in blocking mode.
  710. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  711. * the configuration information for SPI module.
  712. * @param pData pointer to data buffer
  713. * @param Size amount of data to be sent
  714. * @param Timeout Timeout duration
  715. * @retval HAL status
  716. */
  717. HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  718. {
  719. uint32_t tickstart;
  720. HAL_StatusTypeDef errorcode = HAL_OK;
  721. uint16_t initial_TxXferCount;
  722. /* Check Direction parameter */
  723. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  724. /* Process Locked */
  725. __HAL_LOCK(hspi);
  726. /* Init tickstart for timeout management*/
  727. tickstart = HAL_GetTick();
  728. initial_TxXferCount = Size;
  729. if (hspi->State != HAL_SPI_STATE_READY)
  730. {
  731. errorcode = HAL_BUSY;
  732. goto error;
  733. }
  734. if ((pData == NULL) || (Size == 0U))
  735. {
  736. errorcode = HAL_ERROR;
  737. goto error;
  738. }
  739. /* Set the transaction information */
  740. hspi->State = HAL_SPI_STATE_BUSY_TX;
  741. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  742. hspi->pTxBuffPtr = (uint8_t *)pData;
  743. hspi->TxXferSize = Size;
  744. hspi->TxXferCount = Size;
  745. /*Init field not used in handle to zero */
  746. hspi->pRxBuffPtr = (uint8_t *)NULL;
  747. hspi->RxXferSize = 0U;
  748. hspi->RxXferCount = 0U;
  749. hspi->TxISR = NULL;
  750. hspi->RxISR = NULL;
  751. /* Configure communication direction : 1Line */
  752. if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
  753. {
  754. /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
  755. __HAL_SPI_DISABLE(hspi);
  756. SPI_1LINE_TX(hspi);
  757. }
  758. #if (USE_SPI_CRC != 0U)
  759. /* Reset CRC Calculation */
  760. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  761. {
  762. SPI_RESET_CRC(hspi);
  763. }
  764. #endif /* USE_SPI_CRC */
  765. /* Check if the SPI is already enabled */
  766. if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  767. {
  768. /* Enable SPI peripheral */
  769. __HAL_SPI_ENABLE(hspi);
  770. }
  771. /* Transmit data in 16 Bit mode */
  772. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  773. {
  774. if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
  775. {
  776. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  777. hspi->pTxBuffPtr += sizeof(uint16_t);
  778. hspi->TxXferCount--;
  779. }
  780. /* Transmit data in 16 Bit mode */
  781. while (hspi->TxXferCount > 0U)
  782. {
  783. /* Wait until TXE flag is set to send data */
  784. if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE))
  785. {
  786. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  787. hspi->pTxBuffPtr += sizeof(uint16_t);
  788. hspi->TxXferCount--;
  789. }
  790. else
  791. {
  792. /* Timeout management */
  793. if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
  794. {
  795. errorcode = HAL_TIMEOUT;
  796. goto error;
  797. }
  798. }
  799. }
  800. }
  801. /* Transmit data in 8 Bit mode */
  802. else
  803. {
  804. if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
  805. {
  806. *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr);
  807. hspi->pTxBuffPtr += sizeof(uint8_t);
  808. hspi->TxXferCount--;
  809. }
  810. while (hspi->TxXferCount > 0U)
  811. {
  812. /* Wait until TXE flag is set to send data */
  813. if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE))
  814. {
  815. *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr);
  816. hspi->pTxBuffPtr += sizeof(uint8_t);
  817. hspi->TxXferCount--;
  818. }
  819. else
  820. {
  821. /* Timeout management */
  822. if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
  823. {
  824. errorcode = HAL_TIMEOUT;
  825. goto error;
  826. }
  827. }
  828. }
  829. }
  830. #if (USE_SPI_CRC != 0U)
  831. /* Enable CRC Transmission */
  832. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  833. {
  834. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  835. }
  836. #endif /* USE_SPI_CRC */
  837. /* Check the end of the transaction */
  838. if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK)
  839. {
  840. hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
  841. }
  842. /* Clear overrun flag in 2 Lines communication mode because received is not read */
  843. if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
  844. {
  845. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  846. }
  847. if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  848. {
  849. errorcode = HAL_ERROR;
  850. }
  851. error:
  852. hspi->State = HAL_SPI_STATE_READY;
  853. /* Process Unlocked */
  854. __HAL_UNLOCK(hspi);
  855. return errorcode;
  856. }
  857. /**
  858. * @brief Receive an amount of data in blocking mode.
  859. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  860. * the configuration information for SPI module.
  861. * @param pData pointer to data buffer
  862. * @param Size amount of data to be received
  863. * @param Timeout Timeout duration
  864. * @retval HAL status
  865. */
  866. HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  867. {
  868. #if (USE_SPI_CRC != 0U)
  869. __IO uint32_t tmpreg = 0U;
  870. __IO uint8_t * ptmpreg8;
  871. __IO uint8_t tmpreg8 = 0;
  872. #endif /* USE_SPI_CRC */
  873. uint32_t tickstart;
  874. HAL_StatusTypeDef errorcode = HAL_OK;
  875. if ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
  876. {
  877. hspi->State = HAL_SPI_STATE_BUSY_RX;
  878. /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
  879. return HAL_SPI_TransmitReceive(hspi, pData, pData, Size, Timeout);
  880. }
  881. /* Process Locked */
  882. __HAL_LOCK(hspi);
  883. /* Init tickstart for timeout management*/
  884. tickstart = HAL_GetTick();
  885. if (hspi->State != HAL_SPI_STATE_READY)
  886. {
  887. errorcode = HAL_BUSY;
  888. goto error;
  889. }
  890. if ((pData == NULL) || (Size == 0U))
  891. {
  892. errorcode = HAL_ERROR;
  893. goto error;
  894. }
  895. /* Set the transaction information */
  896. hspi->State = HAL_SPI_STATE_BUSY_RX;
  897. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  898. hspi->pRxBuffPtr = (uint8_t *)pData;
  899. hspi->RxXferSize = Size;
  900. hspi->RxXferCount = Size;
  901. /*Init field not used in handle to zero */
  902. hspi->pTxBuffPtr = (uint8_t *)NULL;
  903. hspi->TxXferSize = 0U;
  904. hspi->TxXferCount = 0U;
  905. hspi->RxISR = NULL;
  906. hspi->TxISR = NULL;
  907. #if (USE_SPI_CRC != 0U)
  908. /* Reset CRC Calculation */
  909. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  910. {
  911. SPI_RESET_CRC(hspi);
  912. /* this is done to handle the CRCNEXT before the latest data */
  913. hspi->RxXferCount--;
  914. }
  915. #endif /* USE_SPI_CRC */
  916. /* Set the Rx Fifo threshold */
  917. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  918. {
  919. /* Set RX Fifo threshold according the reception data length: 16bit */
  920. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  921. }
  922. else
  923. {
  924. /* Set RX Fifo threshold according the reception data length: 8bit */
  925. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  926. }
  927. /* Configure communication direction: 1Line */
  928. if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
  929. {
  930. /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
  931. __HAL_SPI_DISABLE(hspi);
  932. SPI_1LINE_RX(hspi);
  933. }
  934. /* Check if the SPI is already enabled */
  935. if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  936. {
  937. /* Enable SPI peripheral */
  938. __HAL_SPI_ENABLE(hspi);
  939. }
  940. /* Receive data in 8 Bit mode */
  941. if (hspi->Init.DataSize <= SPI_DATASIZE_8BIT)
  942. {
  943. /* Transfer loop */
  944. while (hspi->RxXferCount > 0U)
  945. {
  946. /* Check the RXNE flag */
  947. if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))
  948. {
  949. /* read the received data */
  950. (* (uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
  951. hspi->pRxBuffPtr += sizeof(uint8_t);
  952. hspi->RxXferCount--;
  953. }
  954. else
  955. {
  956. /* Timeout management */
  957. if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
  958. {
  959. errorcode = HAL_TIMEOUT;
  960. goto error;
  961. }
  962. }
  963. }
  964. }
  965. else
  966. {
  967. /* Transfer loop */
  968. while (hspi->RxXferCount > 0U)
  969. {
  970. /* Check the RXNE flag */
  971. if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))
  972. {
  973. *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
  974. hspi->pRxBuffPtr += sizeof(uint16_t);
  975. hspi->RxXferCount--;
  976. }
  977. else
  978. {
  979. /* Timeout management */
  980. if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
  981. {
  982. errorcode = HAL_TIMEOUT;
  983. goto error;
  984. }
  985. }
  986. }
  987. }
  988. #if (USE_SPI_CRC != 0U)
  989. /* Handle the CRC Transmission */
  990. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  991. {
  992. /* freeze the CRC before the latest data */
  993. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  994. /* Read the latest data */
  995. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
  996. {
  997. /* the latest data has not been received */
  998. errorcode = HAL_TIMEOUT;
  999. goto error;
  1000. }
  1001. /* Receive last data in 16 Bit mode */
  1002. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1003. {
  1004. *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
  1005. }
  1006. /* Receive last data in 8 Bit mode */
  1007. else
  1008. {
  1009. (*(uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
  1010. }
  1011. /* Wait the CRC data */
  1012. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
  1013. {
  1014. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  1015. errorcode = HAL_TIMEOUT;
  1016. goto error;
  1017. }
  1018. /* Read CRC to Flush DR and RXNE flag */
  1019. if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
  1020. {
  1021. /* Read 16bit CRC */
  1022. tmpreg = READ_REG(hspi->Instance->DR);
  1023. /* To avoid GCC warning */
  1024. UNUSED(tmpreg);
  1025. }
  1026. else
  1027. {
  1028. /* Initialize the 8bit temporary pointer */
  1029. ptmpreg8 = (__IO uint8_t *)&hspi->Instance->DR;
  1030. /* Read 8bit CRC */
  1031. tmpreg8 = *ptmpreg8;
  1032. /* To avoid GCC warning */
  1033. UNUSED(tmpreg8);
  1034. if ((hspi->Init.DataSize == SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
  1035. {
  1036. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
  1037. {
  1038. /* Error on the CRC reception */
  1039. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  1040. errorcode = HAL_TIMEOUT;
  1041. goto error;
  1042. }
  1043. /* Read 8bit CRC again in case of 16bit CRC in 8bit Data mode */
  1044. tmpreg8 = *ptmpreg8;
  1045. /* To avoid GCC warning */
  1046. UNUSED(tmpreg8);
  1047. }
  1048. }
  1049. }
  1050. #endif /* USE_SPI_CRC */
  1051. /* Check the end of the transaction */
  1052. if (SPI_EndRxTransaction(hspi, Timeout, tickstart) != HAL_OK)
  1053. {
  1054. hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
  1055. }
  1056. #if (USE_SPI_CRC != 0U)
  1057. /* Check if CRC error occurred */
  1058. if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR))
  1059. {
  1060. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  1061. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  1062. }
  1063. #endif /* USE_SPI_CRC */
  1064. if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  1065. {
  1066. errorcode = HAL_ERROR;
  1067. }
  1068. error :
  1069. hspi->State = HAL_SPI_STATE_READY;
  1070. __HAL_UNLOCK(hspi);
  1071. return errorcode;
  1072. }
  1073. /**
  1074. * @brief Transmit and Receive an amount of data in blocking mode.
  1075. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  1076. * the configuration information for SPI module.
  1077. * @param pTxData pointer to transmission data buffer
  1078. * @param pRxData pointer to reception data buffer
  1079. * @param Size amount of data to be sent and received
  1080. * @param Timeout Timeout duration
  1081. * @retval HAL status
  1082. */
  1083. HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size,
  1084. uint32_t Timeout)
  1085. {
  1086. uint16_t initial_TxXferCount;
  1087. uint32_t tmp_mode;
  1088. HAL_SPI_StateTypeDef tmp_state;
  1089. uint32_t tickstart;
  1090. #if (USE_SPI_CRC != 0U)
  1091. __IO uint32_t tmpreg = 0U;
  1092. uint32_t spi_cr1;
  1093. uint32_t spi_cr2;
  1094. __IO uint8_t * ptmpreg8;
  1095. __IO uint8_t tmpreg8 = 0;
  1096. #endif /* USE_SPI_CRC */
  1097. /* Variable used to alternate Rx and Tx during transfer */
  1098. uint32_t txallowed = 1U;
  1099. HAL_StatusTypeDef errorcode = HAL_OK;
  1100. /* Check Direction parameter */
  1101. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  1102. /* Process Locked */
  1103. __HAL_LOCK(hspi);
  1104. /* Init tickstart for timeout management*/
  1105. tickstart = HAL_GetTick();
  1106. /* Init temporary variables */
  1107. tmp_state = hspi->State;
  1108. tmp_mode = hspi->Init.Mode;
  1109. initial_TxXferCount = Size;
  1110. #if (USE_SPI_CRC != 0U)
  1111. spi_cr1 = READ_REG(hspi->Instance->CR1);
  1112. spi_cr2 = READ_REG(hspi->Instance->CR2);
  1113. #endif /* USE_SPI_CRC */
  1114. if (!((tmp_state == HAL_SPI_STATE_READY) || \
  1115. ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
  1116. {
  1117. errorcode = HAL_BUSY;
  1118. goto error;
  1119. }
  1120. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  1121. {
  1122. errorcode = HAL_ERROR;
  1123. goto error;
  1124. }
  1125. /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
  1126. if (hspi->State != HAL_SPI_STATE_BUSY_RX)
  1127. {
  1128. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  1129. }
  1130. /* Set the transaction information */
  1131. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1132. hspi->pRxBuffPtr = (uint8_t *)pRxData;
  1133. hspi->RxXferCount = Size;
  1134. hspi->RxXferSize = Size;
  1135. hspi->pTxBuffPtr = (uint8_t *)pTxData;
  1136. hspi->TxXferCount = Size;
  1137. hspi->TxXferSize = Size;
  1138. /*Init field not used in handle to zero */
  1139. hspi->RxISR = NULL;
  1140. hspi->TxISR = NULL;
  1141. #if (USE_SPI_CRC != 0U)
  1142. /* Reset CRC Calculation */
  1143. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1144. {
  1145. SPI_RESET_CRC(hspi);
  1146. }
  1147. #endif /* USE_SPI_CRC */
  1148. /* Set the Rx Fifo threshold */
  1149. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1150. {
  1151. /* Set fiforxthreshold according the reception data length: 16bit */
  1152. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1153. }
  1154. else
  1155. {
  1156. /* Set fiforxthreshold according the reception data length: 8bit */
  1157. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1158. }
  1159. /* Check if the SPI is already enabled */
  1160. if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1161. {
  1162. /* Enable SPI peripheral */
  1163. __HAL_SPI_ENABLE(hspi);
  1164. }
  1165. /* Transmit and Receive data in 16 Bit mode */
  1166. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1167. {
  1168. if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
  1169. {
  1170. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  1171. hspi->pTxBuffPtr += sizeof(uint16_t);
  1172. hspi->TxXferCount--;
  1173. }
  1174. while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))
  1175. {
  1176. /* Check TXE flag */
  1177. if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U))
  1178. {
  1179. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  1180. hspi->pTxBuffPtr += sizeof(uint16_t);
  1181. hspi->TxXferCount--;
  1182. /* Next Data is a reception (Rx). Tx not allowed */
  1183. txallowed = 0U;
  1184. #if (USE_SPI_CRC != 0U)
  1185. /* Enable CRC Transmission */
  1186. if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  1187. {
  1188. /* Set NSS Soft to received correctly the CRC on slave mode with NSS pulse activated */
  1189. if ((READ_BIT(spi_cr1, SPI_CR1_MSTR) == 0U) && (READ_BIT(spi_cr2, SPI_CR2_NSSP) == SPI_CR2_NSSP))
  1190. {
  1191. SET_BIT(hspi->Instance->CR1, SPI_CR1_SSM);
  1192. }
  1193. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  1194. }
  1195. #endif /* USE_SPI_CRC */
  1196. }
  1197. /* Check RXNE flag */
  1198. if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U))
  1199. {
  1200. *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
  1201. hspi->pRxBuffPtr += sizeof(uint16_t);
  1202. hspi->RxXferCount--;
  1203. /* Next Data is a Transmission (Tx). Tx is allowed */
  1204. txallowed = 1U;
  1205. }
  1206. if (((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY))
  1207. {
  1208. errorcode = HAL_TIMEOUT;
  1209. goto error;
  1210. }
  1211. }
  1212. }
  1213. /* Transmit and Receive data in 8 Bit mode */
  1214. else
  1215. {
  1216. if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
  1217. {
  1218. *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr);
  1219. hspi->pTxBuffPtr += sizeof(uint8_t);
  1220. hspi->TxXferCount--;
  1221. }
  1222. while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))
  1223. {
  1224. /* Check TXE flag */
  1225. if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U))
  1226. {
  1227. *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
  1228. hspi->pTxBuffPtr++;
  1229. hspi->TxXferCount--;
  1230. /* Next Data is a reception (Rx). Tx not allowed */
  1231. txallowed = 0U;
  1232. #if (USE_SPI_CRC != 0U)
  1233. /* Enable CRC Transmission */
  1234. if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  1235. {
  1236. /* Set NSS Soft to received correctly the CRC on slave mode with NSS pulse activated */
  1237. if ((READ_BIT(spi_cr1, SPI_CR1_MSTR) == 0U) && (READ_BIT(spi_cr2, SPI_CR2_NSSP) == SPI_CR2_NSSP))
  1238. {
  1239. SET_BIT(hspi->Instance->CR1, SPI_CR1_SSM);
  1240. }
  1241. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  1242. }
  1243. #endif /* USE_SPI_CRC */
  1244. }
  1245. /* Wait until RXNE flag is reset */
  1246. if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U))
  1247. {
  1248. (*(uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
  1249. hspi->pRxBuffPtr++;
  1250. hspi->RxXferCount--;
  1251. /* Next Data is a Transmission (Tx). Tx is allowed */
  1252. txallowed = 1U;
  1253. }
  1254. if ((((HAL_GetTick() - tickstart) >= Timeout) && ((Timeout != HAL_MAX_DELAY))) || (Timeout == 0U))
  1255. {
  1256. errorcode = HAL_TIMEOUT;
  1257. goto error;
  1258. }
  1259. }
  1260. }
  1261. #if (USE_SPI_CRC != 0U)
  1262. /* Read CRC from DR to close CRC calculation process */
  1263. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1264. {
  1265. /* Wait until TXE flag */
  1266. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
  1267. {
  1268. /* Error on the CRC reception */
  1269. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  1270. errorcode = HAL_TIMEOUT;
  1271. goto error;
  1272. }
  1273. /* Read CRC */
  1274. if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
  1275. {
  1276. /* Read 16bit CRC */
  1277. tmpreg = READ_REG(hspi->Instance->DR);
  1278. /* To avoid GCC warning */
  1279. UNUSED(tmpreg);
  1280. }
  1281. else
  1282. {
  1283. /* Initialize the 8bit temporary pointer */
  1284. ptmpreg8 = (__IO uint8_t *)&hspi->Instance->DR;
  1285. /* Read 8bit CRC */
  1286. tmpreg8 = *ptmpreg8;
  1287. /* To avoid GCC warning */
  1288. UNUSED(tmpreg8);
  1289. if (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
  1290. {
  1291. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
  1292. {
  1293. /* Error on the CRC reception */
  1294. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  1295. errorcode = HAL_TIMEOUT;
  1296. goto error;
  1297. }
  1298. /* Read 8bit CRC again in case of 16bit CRC in 8bit Data mode */
  1299. tmpreg8 = *ptmpreg8;
  1300. /* To avoid GCC warning */
  1301. UNUSED(tmpreg8);
  1302. }
  1303. }
  1304. }
  1305. /* Check if CRC error occurred */
  1306. if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR))
  1307. {
  1308. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  1309. /* Clear CRC Flag */
  1310. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  1311. errorcode = HAL_ERROR;
  1312. }
  1313. #endif /* USE_SPI_CRC */
  1314. /* Check the end of the transaction */
  1315. if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK)
  1316. {
  1317. errorcode = HAL_ERROR;
  1318. hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
  1319. }
  1320. error :
  1321. hspi->State = HAL_SPI_STATE_READY;
  1322. __HAL_UNLOCK(hspi);
  1323. return errorcode;
  1324. }
  1325. /**
  1326. * @brief Transmit an amount of data in non-blocking mode with Interrupt.
  1327. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  1328. * the configuration information for SPI module.
  1329. * @param pData pointer to data buffer
  1330. * @param Size amount of data to be sent
  1331. * @retval HAL status
  1332. */
  1333. HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  1334. {
  1335. HAL_StatusTypeDef errorcode = HAL_OK;
  1336. /* Check Direction parameter */
  1337. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  1338. /* Process Locked */
  1339. __HAL_LOCK(hspi);
  1340. if ((pData == NULL) || (Size == 0U))
  1341. {
  1342. errorcode = HAL_ERROR;
  1343. goto error;
  1344. }
  1345. if (hspi->State != HAL_SPI_STATE_READY)
  1346. {
  1347. errorcode = HAL_BUSY;
  1348. goto error;
  1349. }
  1350. /* Set the transaction information */
  1351. hspi->State = HAL_SPI_STATE_BUSY_TX;
  1352. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1353. hspi->pTxBuffPtr = (uint8_t *)pData;
  1354. hspi->TxXferSize = Size;
  1355. hspi->TxXferCount = Size;
  1356. /* Init field not used in handle to zero */
  1357. hspi->pRxBuffPtr = (uint8_t *)NULL;
  1358. hspi->RxXferSize = 0U;
  1359. hspi->RxXferCount = 0U;
  1360. hspi->RxISR = NULL;
  1361. /* Set the function for IT treatment */
  1362. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1363. {
  1364. hspi->TxISR = SPI_TxISR_16BIT;
  1365. }
  1366. else
  1367. {
  1368. hspi->TxISR = SPI_TxISR_8BIT;
  1369. }
  1370. /* Configure communication direction : 1Line */
  1371. if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
  1372. {
  1373. /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
  1374. __HAL_SPI_DISABLE(hspi);
  1375. SPI_1LINE_TX(hspi);
  1376. }
  1377. #if (USE_SPI_CRC != 0U)
  1378. /* Reset CRC Calculation */
  1379. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1380. {
  1381. SPI_RESET_CRC(hspi);
  1382. }
  1383. #endif /* USE_SPI_CRC */
  1384. /* Enable TXE and ERR interrupt */
  1385. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
  1386. /* Check if the SPI is already enabled */
  1387. if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1388. {
  1389. /* Enable SPI peripheral */
  1390. __HAL_SPI_ENABLE(hspi);
  1391. }
  1392. error :
  1393. __HAL_UNLOCK(hspi);
  1394. return errorcode;
  1395. }
  1396. /**
  1397. * @brief Receive an amount of data in non-blocking mode with Interrupt.
  1398. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  1399. * the configuration information for SPI module.
  1400. * @param pData pointer to data buffer
  1401. * @param Size amount of data to be sent
  1402. * @retval HAL status
  1403. */
  1404. HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  1405. {
  1406. HAL_StatusTypeDef errorcode = HAL_OK;
  1407. if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
  1408. {
  1409. hspi->State = HAL_SPI_STATE_BUSY_RX;
  1410. /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
  1411. return HAL_SPI_TransmitReceive_IT(hspi, pData, pData, Size);
  1412. }
  1413. /* Process Locked */
  1414. __HAL_LOCK(hspi);
  1415. if (hspi->State != HAL_SPI_STATE_READY)
  1416. {
  1417. errorcode = HAL_BUSY;
  1418. goto error;
  1419. }
  1420. if ((pData == NULL) || (Size == 0U))
  1421. {
  1422. errorcode = HAL_ERROR;
  1423. goto error;
  1424. }
  1425. /* Set the transaction information */
  1426. hspi->State = HAL_SPI_STATE_BUSY_RX;
  1427. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1428. hspi->pRxBuffPtr = (uint8_t *)pData;
  1429. hspi->RxXferSize = Size;
  1430. hspi->RxXferCount = Size;
  1431. /* Init field not used in handle to zero */
  1432. hspi->pTxBuffPtr = (uint8_t *)NULL;
  1433. hspi->TxXferSize = 0U;
  1434. hspi->TxXferCount = 0U;
  1435. hspi->TxISR = NULL;
  1436. /* Check the data size to adapt Rx threshold and the set the function for IT treatment */
  1437. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1438. {
  1439. /* Set RX Fifo threshold according the reception data length: 16 bit */
  1440. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1441. hspi->RxISR = SPI_RxISR_16BIT;
  1442. }
  1443. else
  1444. {
  1445. /* Set RX Fifo threshold according the reception data length: 8 bit */
  1446. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1447. hspi->RxISR = SPI_RxISR_8BIT;
  1448. }
  1449. /* Configure communication direction : 1Line */
  1450. if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
  1451. {
  1452. /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
  1453. __HAL_SPI_DISABLE(hspi);
  1454. SPI_1LINE_RX(hspi);
  1455. }
  1456. #if (USE_SPI_CRC != 0U)
  1457. /* Reset CRC Calculation */
  1458. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1459. {
  1460. hspi->CRCSize = 1U;
  1461. if ((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
  1462. {
  1463. hspi->CRCSize = 2U;
  1464. }
  1465. SPI_RESET_CRC(hspi);
  1466. }
  1467. else
  1468. {
  1469. hspi->CRCSize = 0U;
  1470. }
  1471. #endif /* USE_SPI_CRC */
  1472. /* Enable TXE and ERR interrupt */
  1473. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  1474. /* Note : The SPI must be enabled after unlocking current process
  1475. to avoid the risk of SPI interrupt handle execution before current
  1476. process unlock */
  1477. /* Check if the SPI is already enabled */
  1478. if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1479. {
  1480. /* Enable SPI peripheral */
  1481. __HAL_SPI_ENABLE(hspi);
  1482. }
  1483. error :
  1484. /* Process Unlocked */
  1485. __HAL_UNLOCK(hspi);
  1486. return errorcode;
  1487. }
  1488. /**
  1489. * @brief Transmit and Receive an amount of data in non-blocking mode with Interrupt.
  1490. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  1491. * the configuration information for SPI module.
  1492. * @param pTxData pointer to transmission data buffer
  1493. * @param pRxData pointer to reception data buffer
  1494. * @param Size amount of data to be sent and received
  1495. * @retval HAL status
  1496. */
  1497. HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  1498. {
  1499. uint32_t tmp_mode;
  1500. HAL_SPI_StateTypeDef tmp_state;
  1501. HAL_StatusTypeDef errorcode = HAL_OK;
  1502. /* Check Direction parameter */
  1503. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  1504. /* Process locked */
  1505. __HAL_LOCK(hspi);
  1506. /* Init temporary variables */
  1507. tmp_state = hspi->State;
  1508. tmp_mode = hspi->Init.Mode;
  1509. if (!((tmp_state == HAL_SPI_STATE_READY) || \
  1510. ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
  1511. {
  1512. errorcode = HAL_BUSY;
  1513. goto error;
  1514. }
  1515. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  1516. {
  1517. errorcode = HAL_ERROR;
  1518. goto error;
  1519. }
  1520. /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
  1521. if (hspi->State != HAL_SPI_STATE_BUSY_RX)
  1522. {
  1523. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  1524. }
  1525. /* Set the transaction information */
  1526. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1527. hspi->pTxBuffPtr = (uint8_t *)pTxData;
  1528. hspi->TxXferSize = Size;
  1529. hspi->TxXferCount = Size;
  1530. hspi->pRxBuffPtr = (uint8_t *)pRxData;
  1531. hspi->RxXferSize = Size;
  1532. hspi->RxXferCount = Size;
  1533. /* Set the function for IT treatment */
  1534. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1535. {
  1536. hspi->RxISR = SPI_2linesRxISR_16BIT;
  1537. hspi->TxISR = SPI_2linesTxISR_16BIT;
  1538. }
  1539. else
  1540. {
  1541. hspi->RxISR = SPI_2linesRxISR_8BIT;
  1542. hspi->TxISR = SPI_2linesTxISR_8BIT;
  1543. }
  1544. #if (USE_SPI_CRC != 0U)
  1545. /* Reset CRC Calculation */
  1546. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1547. {
  1548. hspi->CRCSize = 1U;
  1549. if ((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
  1550. {
  1551. hspi->CRCSize = 2U;
  1552. }
  1553. SPI_RESET_CRC(hspi);
  1554. }
  1555. else
  1556. {
  1557. hspi->CRCSize = 0U;
  1558. }
  1559. #endif /* USE_SPI_CRC */
  1560. /* Check if packing mode is enabled and if there is more than 2 data to receive */
  1561. if ((hspi->Init.DataSize > SPI_DATASIZE_8BIT) || (Size >= 2U))
  1562. {
  1563. /* Set RX Fifo threshold according the reception data length: 16 bit */
  1564. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1565. }
  1566. else
  1567. {
  1568. /* Set RX Fifo threshold according the reception data length: 8 bit */
  1569. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1570. }
  1571. /* Enable TXE, RXNE and ERR interrupt */
  1572. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  1573. /* Check if the SPI is already enabled */
  1574. if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1575. {
  1576. /* Enable SPI peripheral */
  1577. __HAL_SPI_ENABLE(hspi);
  1578. }
  1579. error :
  1580. /* Process Unlocked */
  1581. __HAL_UNLOCK(hspi);
  1582. return errorcode;
  1583. }
  1584. /**
  1585. * @brief Transmit an amount of data in non-blocking mode with DMA.
  1586. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  1587. * the configuration information for SPI module.
  1588. * @param pData pointer to data buffer
  1589. * @param Size amount of data to be sent
  1590. * @retval HAL status
  1591. */
  1592. HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  1593. {
  1594. HAL_StatusTypeDef errorcode = HAL_OK;
  1595. /* Check tx dma handle */
  1596. assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
  1597. /* Check Direction parameter */
  1598. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  1599. /* Process Locked */
  1600. __HAL_LOCK(hspi);
  1601. if (hspi->State != HAL_SPI_STATE_READY)
  1602. {
  1603. errorcode = HAL_BUSY;
  1604. goto error;
  1605. }
  1606. if ((pData == NULL) || (Size == 0U))
  1607. {
  1608. errorcode = HAL_ERROR;
  1609. goto error;
  1610. }
  1611. /* Set the transaction information */
  1612. hspi->State = HAL_SPI_STATE_BUSY_TX;
  1613. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1614. hspi->pTxBuffPtr = (uint8_t *)pData;
  1615. hspi->TxXferSize = Size;
  1616. hspi->TxXferCount = Size;
  1617. /* Init field not used in handle to zero */
  1618. hspi->pRxBuffPtr = (uint8_t *)NULL;
  1619. hspi->TxISR = NULL;
  1620. hspi->RxISR = NULL;
  1621. hspi->RxXferSize = 0U;
  1622. hspi->RxXferCount = 0U;
  1623. /* Configure communication direction : 1Line */
  1624. if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
  1625. {
  1626. /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
  1627. __HAL_SPI_DISABLE(hspi);
  1628. SPI_1LINE_TX(hspi);
  1629. }
  1630. #if (USE_SPI_CRC != 0U)
  1631. /* Reset CRC Calculation */
  1632. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1633. {
  1634. SPI_RESET_CRC(hspi);
  1635. }
  1636. #endif /* USE_SPI_CRC */
  1637. /* Set the SPI TxDMA Half transfer complete callback */
  1638. hspi->hdmatx->XferHalfCpltCallback = SPI_DMAHalfTransmitCplt;
  1639. /* Set the SPI TxDMA transfer complete callback */
  1640. hspi->hdmatx->XferCpltCallback = SPI_DMATransmitCplt;
  1641. /* Set the DMA error callback */
  1642. hspi->hdmatx->XferErrorCallback = SPI_DMAError;
  1643. /* Set the DMA AbortCpltCallback */
  1644. hspi->hdmatx->XferAbortCallback = NULL;
  1645. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1646. /* Packing mode is enabled only if the DMA setting is HALWORD */
  1647. if ((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD))
  1648. {
  1649. /* Check the even/odd of the data size + crc if enabled */
  1650. if ((hspi->TxXferCount & 0x1U) == 0U)
  1651. {
  1652. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1653. hspi->TxXferCount = (hspi->TxXferCount >> 1U);
  1654. }
  1655. else
  1656. {
  1657. SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1658. hspi->TxXferCount = (hspi->TxXferCount >> 1U) + 1U;
  1659. }
  1660. }
  1661. /* Enable the Tx DMA Stream/Channel */
  1662. if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR,
  1663. hspi->TxXferCount))
  1664. {
  1665. /* Update SPI error code */
  1666. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
  1667. errorcode = HAL_ERROR;
  1668. hspi->State = HAL_SPI_STATE_READY;
  1669. goto error;
  1670. }
  1671. /* Check if the SPI is already enabled */
  1672. if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1673. {
  1674. /* Enable SPI peripheral */
  1675. __HAL_SPI_ENABLE(hspi);
  1676. }
  1677. /* Enable the SPI Error Interrupt Bit */
  1678. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
  1679. /* Enable Tx DMA Request */
  1680. SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
  1681. error :
  1682. /* Process Unlocked */
  1683. __HAL_UNLOCK(hspi);
  1684. return errorcode;
  1685. }
  1686. /**
  1687. * @brief Receive an amount of data in non-blocking mode with DMA.
  1688. * @note In case of MASTER mode and SPI_DIRECTION_2LINES direction, hdmatx shall be defined.
  1689. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  1690. * the configuration information for SPI module.
  1691. * @param pData pointer to data buffer
  1692. * @note When the CRC feature is enabled the pData Length must be Size + 1.
  1693. * @param Size amount of data to be sent
  1694. * @retval HAL status
  1695. */
  1696. HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  1697. {
  1698. HAL_StatusTypeDef errorcode = HAL_OK;
  1699. /* Check rx dma handle */
  1700. assert_param(IS_SPI_DMA_HANDLE(hspi->hdmarx));
  1701. if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
  1702. {
  1703. hspi->State = HAL_SPI_STATE_BUSY_RX;
  1704. /* Check tx dma handle */
  1705. assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
  1706. /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
  1707. return HAL_SPI_TransmitReceive_DMA(hspi, pData, pData, Size);
  1708. }
  1709. /* Process Locked */
  1710. __HAL_LOCK(hspi);
  1711. if (hspi->State != HAL_SPI_STATE_READY)
  1712. {
  1713. errorcode = HAL_BUSY;
  1714. goto error;
  1715. }
  1716. if ((pData == NULL) || (Size == 0U))
  1717. {
  1718. errorcode = HAL_ERROR;
  1719. goto error;
  1720. }
  1721. /* Set the transaction information */
  1722. hspi->State = HAL_SPI_STATE_BUSY_RX;
  1723. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1724. hspi->pRxBuffPtr = (uint8_t *)pData;
  1725. hspi->RxXferSize = Size;
  1726. hspi->RxXferCount = Size;
  1727. /*Init field not used in handle to zero */
  1728. hspi->RxISR = NULL;
  1729. hspi->TxISR = NULL;
  1730. hspi->TxXferSize = 0U;
  1731. hspi->TxXferCount = 0U;
  1732. /* Configure communication direction : 1Line */
  1733. if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
  1734. {
  1735. /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
  1736. __HAL_SPI_DISABLE(hspi);
  1737. SPI_1LINE_RX(hspi);
  1738. }
  1739. #if (USE_SPI_CRC != 0U)
  1740. /* Reset CRC Calculation */
  1741. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1742. {
  1743. SPI_RESET_CRC(hspi);
  1744. }
  1745. #endif /* USE_SPI_CRC */
  1746. #if defined (STM32F030x6) || defined (STM32F030x8) || defined (STM32F031x6)|| defined (STM32F038xx) || defined (STM32F051x8) || defined (STM32F058xx)
  1747. /* Packing mode management is enabled by the DMA settings */
  1748. if ((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD))
  1749. {
  1750. /* Restriction the DMA data received is not allowed in this mode */
  1751. errorcode = HAL_ERROR;
  1752. goto error;
  1753. }
  1754. #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F038xx || STM32F051x8 || STM32F058xx */
  1755. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1756. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1757. {
  1758. /* Set RX Fifo threshold according the reception data length: 16bit */
  1759. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1760. }
  1761. else
  1762. {
  1763. /* Set RX Fifo threshold according the reception data length: 8bit */
  1764. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1765. if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
  1766. {
  1767. /* Set RX Fifo threshold according the reception data length: 16bit */
  1768. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1769. if ((hspi->RxXferCount & 0x1U) == 0x0U)
  1770. {
  1771. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1772. hspi->RxXferCount = hspi->RxXferCount >> 1U;
  1773. }
  1774. else
  1775. {
  1776. SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1777. hspi->RxXferCount = (hspi->RxXferCount >> 1U) + 1U;
  1778. }
  1779. }
  1780. }
  1781. /* Set the SPI RxDMA Half transfer complete callback */
  1782. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
  1783. /* Set the SPI Rx DMA transfer complete callback */
  1784. hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
  1785. /* Set the DMA error callback */
  1786. hspi->hdmarx->XferErrorCallback = SPI_DMAError;
  1787. /* Set the DMA AbortCpltCallback */
  1788. hspi->hdmarx->XferAbortCallback = NULL;
  1789. /* Enable the Rx DMA Stream/Channel */
  1790. if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr,
  1791. hspi->RxXferCount))
  1792. {
  1793. /* Update SPI error code */
  1794. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
  1795. errorcode = HAL_ERROR;
  1796. hspi->State = HAL_SPI_STATE_READY;
  1797. goto error;
  1798. }
  1799. /* Check if the SPI is already enabled */
  1800. if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1801. {
  1802. /* Enable SPI peripheral */
  1803. __HAL_SPI_ENABLE(hspi);
  1804. }
  1805. /* Enable the SPI Error Interrupt Bit */
  1806. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
  1807. /* Enable Rx DMA Request */
  1808. SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
  1809. error:
  1810. /* Process Unlocked */
  1811. __HAL_UNLOCK(hspi);
  1812. return errorcode;
  1813. }
  1814. /**
  1815. * @brief Transmit and Receive an amount of data in non-blocking mode with DMA.
  1816. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  1817. * the configuration information for SPI module.
  1818. * @param pTxData pointer to transmission data buffer
  1819. * @param pRxData pointer to reception data buffer
  1820. * @note When the CRC feature is enabled the pRxData Length must be Size + 1
  1821. * @param Size amount of data to be sent
  1822. * @retval HAL status
  1823. */
  1824. HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,
  1825. uint16_t Size)
  1826. {
  1827. uint32_t tmp_mode;
  1828. HAL_SPI_StateTypeDef tmp_state;
  1829. HAL_StatusTypeDef errorcode = HAL_OK;
  1830. /* Check rx & tx dma handles */
  1831. assert_param(IS_SPI_DMA_HANDLE(hspi->hdmarx));
  1832. assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
  1833. /* Check Direction parameter */
  1834. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  1835. /* Process locked */
  1836. __HAL_LOCK(hspi);
  1837. /* Init temporary variables */
  1838. tmp_state = hspi->State;
  1839. tmp_mode = hspi->Init.Mode;
  1840. if (!((tmp_state == HAL_SPI_STATE_READY) ||
  1841. ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
  1842. {
  1843. errorcode = HAL_BUSY;
  1844. goto error;
  1845. }
  1846. if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
  1847. {
  1848. errorcode = HAL_ERROR;
  1849. goto error;
  1850. }
  1851. /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
  1852. if (hspi->State != HAL_SPI_STATE_BUSY_RX)
  1853. {
  1854. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  1855. }
  1856. /* Set the transaction information */
  1857. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1858. hspi->pTxBuffPtr = (uint8_t *)pTxData;
  1859. hspi->TxXferSize = Size;
  1860. hspi->TxXferCount = Size;
  1861. hspi->pRxBuffPtr = (uint8_t *)pRxData;
  1862. hspi->RxXferSize = Size;
  1863. hspi->RxXferCount = Size;
  1864. /* Init field not used in handle to zero */
  1865. hspi->RxISR = NULL;
  1866. hspi->TxISR = NULL;
  1867. #if (USE_SPI_CRC != 0U)
  1868. /* Reset CRC Calculation */
  1869. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1870. {
  1871. SPI_RESET_CRC(hspi);
  1872. }
  1873. #endif /* USE_SPI_CRC */
  1874. #if defined (STM32F030x6) || defined (STM32F030x8) || defined (STM32F031x6) || defined (STM32F038xx) || defined (STM32F051x8) || defined (STM32F058xx)
  1875. /* Packing mode management is enabled by the DMA settings */
  1876. if ((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD))
  1877. {
  1878. /* Restriction the DMA data received is not allowed in this mode */
  1879. errorcode = HAL_ERROR;
  1880. goto error;
  1881. }
  1882. #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F038xx || STM32F051x8 || STM32F058xx */
  1883. /* Reset the threshold bit */
  1884. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX | SPI_CR2_LDMARX);
  1885. /* The packing mode management is enabled by the DMA settings according the spi data size */
  1886. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1887. {
  1888. /* Set fiforxthreshold according the reception data length: 16bit */
  1889. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1890. }
  1891. else
  1892. {
  1893. /* Set RX Fifo threshold according the reception data length: 8bit */
  1894. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1895. if (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
  1896. {
  1897. if ((hspi->TxXferSize & 0x1U) == 0x0U)
  1898. {
  1899. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1900. hspi->TxXferCount = hspi->TxXferCount >> 1U;
  1901. }
  1902. else
  1903. {
  1904. SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1905. hspi->TxXferCount = (hspi->TxXferCount >> 1U) + 1U;
  1906. }
  1907. }
  1908. if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
  1909. {
  1910. /* Set RX Fifo threshold according the reception data length: 16bit */
  1911. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1912. if ((hspi->RxXferCount & 0x1U) == 0x0U)
  1913. {
  1914. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1915. hspi->RxXferCount = hspi->RxXferCount >> 1U;
  1916. }
  1917. else
  1918. {
  1919. SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1920. hspi->RxXferCount = (hspi->RxXferCount >> 1U) + 1U;
  1921. }
  1922. }
  1923. }
  1924. /* Check if we are in Rx only or in Rx/Tx Mode and configure the DMA transfer complete callback */
  1925. if (hspi->State == HAL_SPI_STATE_BUSY_RX)
  1926. {
  1927. /* Set the SPI Rx DMA Half transfer complete callback */
  1928. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
  1929. hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
  1930. }
  1931. else
  1932. {
  1933. /* Set the SPI Tx/Rx DMA Half transfer complete callback */
  1934. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfTransmitReceiveCplt;
  1935. hspi->hdmarx->XferCpltCallback = SPI_DMATransmitReceiveCplt;
  1936. }
  1937. /* Set the DMA error callback */
  1938. hspi->hdmarx->XferErrorCallback = SPI_DMAError;
  1939. /* Set the DMA AbortCpltCallback */
  1940. hspi->hdmarx->XferAbortCallback = NULL;
  1941. /* Enable the Rx DMA Stream/Channel */
  1942. if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr,
  1943. hspi->RxXferCount))
  1944. {
  1945. /* Update SPI error code */
  1946. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
  1947. errorcode = HAL_ERROR;
  1948. hspi->State = HAL_SPI_STATE_READY;
  1949. goto error;
  1950. }
  1951. /* Enable Rx DMA Request */
  1952. SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
  1953. /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing
  1954. is performed in DMA reception complete callback */
  1955. hspi->hdmatx->XferHalfCpltCallback = NULL;
  1956. hspi->hdmatx->XferCpltCallback = NULL;
  1957. hspi->hdmatx->XferErrorCallback = NULL;
  1958. hspi->hdmatx->XferAbortCallback = NULL;
  1959. /* Enable the Tx DMA Stream/Channel */
  1960. if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR,
  1961. hspi->TxXferCount))
  1962. {
  1963. /* Update SPI error code */
  1964. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
  1965. errorcode = HAL_ERROR;
  1966. hspi->State = HAL_SPI_STATE_READY;
  1967. goto error;
  1968. }
  1969. /* Check if the SPI is already enabled */
  1970. if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1971. {
  1972. /* Enable SPI peripheral */
  1973. __HAL_SPI_ENABLE(hspi);
  1974. }
  1975. /* Enable the SPI Error Interrupt Bit */
  1976. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
  1977. /* Enable Tx DMA Request */
  1978. SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
  1979. error :
  1980. /* Process Unlocked */
  1981. __HAL_UNLOCK(hspi);
  1982. return errorcode;
  1983. }
  1984. /**
  1985. * @brief Abort ongoing transfer (blocking mode).
  1986. * @param hspi SPI handle.
  1987. * @note This procedure could be used for aborting any ongoing transfer (Tx and Rx),
  1988. * started in Interrupt or DMA mode.
  1989. * This procedure performs following operations :
  1990. * - Disable SPI Interrupts (depending of transfer direction)
  1991. * - Disable the DMA transfer in the peripheral register (if enabled)
  1992. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1993. * - Set handle State to READY
  1994. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1995. * @retval HAL status
  1996. */
  1997. HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi)
  1998. {
  1999. HAL_StatusTypeDef errorcode;
  2000. __IO uint32_t count;
  2001. __IO uint32_t resetcount;
  2002. /* Initialized local variable */
  2003. errorcode = HAL_OK;
  2004. resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
  2005. count = resetcount;
  2006. /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
  2007. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE);
  2008. /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */
  2009. if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE))
  2010. {
  2011. hspi->TxISR = SPI_AbortTx_ISR;
  2012. /* Wait HAL_SPI_STATE_ABORT state */
  2013. do
  2014. {
  2015. if (count == 0U)
  2016. {
  2017. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
  2018. break;
  2019. }
  2020. count--;
  2021. } while (hspi->State != HAL_SPI_STATE_ABORT);
  2022. /* Reset Timeout Counter */
  2023. count = resetcount;
  2024. }
  2025. if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
  2026. {
  2027. hspi->RxISR = SPI_AbortRx_ISR;
  2028. /* Wait HAL_SPI_STATE_ABORT state */
  2029. do
  2030. {
  2031. if (count == 0U)
  2032. {
  2033. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
  2034. break;
  2035. }
  2036. count--;
  2037. } while (hspi->State != HAL_SPI_STATE_ABORT);
  2038. /* Reset Timeout Counter */
  2039. count = resetcount;
  2040. }
  2041. /* Disable the SPI DMA Tx request if enabled */
  2042. if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
  2043. {
  2044. /* Abort the SPI DMA Tx Stream/Channel : use blocking DMA Abort API (no callback) */
  2045. if (hspi->hdmatx != NULL)
  2046. {
  2047. /* Set the SPI DMA Abort callback :
  2048. will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
  2049. hspi->hdmatx->XferAbortCallback = NULL;
  2050. /* Abort DMA Tx Handle linked to SPI Peripheral */
  2051. if (HAL_DMA_Abort(hspi->hdmatx) != HAL_OK)
  2052. {
  2053. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  2054. }
  2055. /* Disable Tx DMA Request */
  2056. CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN));
  2057. if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  2058. {
  2059. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  2060. }
  2061. /* Disable SPI Peripheral */
  2062. __HAL_SPI_DISABLE(hspi);
  2063. /* Empty the FRLVL fifo */
  2064. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  2065. {
  2066. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  2067. }
  2068. }
  2069. }
  2070. /* Disable the SPI DMA Rx request if enabled */
  2071. if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
  2072. {
  2073. /* Abort the SPI DMA Rx Stream/Channel : use blocking DMA Abort API (no callback) */
  2074. if (hspi->hdmarx != NULL)
  2075. {
  2076. /* Set the SPI DMA Abort callback :
  2077. will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
  2078. hspi->hdmarx->XferAbortCallback = NULL;
  2079. /* Abort DMA Rx Handle linked to SPI Peripheral */
  2080. if (HAL_DMA_Abort(hspi->hdmarx) != HAL_OK)
  2081. {
  2082. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  2083. }
  2084. /* Disable peripheral */
  2085. __HAL_SPI_DISABLE(hspi);
  2086. /* Control the BSY flag */
  2087. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  2088. {
  2089. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  2090. }
  2091. /* Empty the FRLVL fifo */
  2092. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  2093. {
  2094. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  2095. }
  2096. /* Disable Rx DMA Request */
  2097. CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_RXDMAEN));
  2098. }
  2099. }
  2100. /* Reset Tx and Rx transfer counters */
  2101. hspi->RxXferCount = 0U;
  2102. hspi->TxXferCount = 0U;
  2103. /* Check error during Abort procedure */
  2104. if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT)
  2105. {
  2106. /* return HAL_Error in case of error during Abort procedure */
  2107. errorcode = HAL_ERROR;
  2108. }
  2109. else
  2110. {
  2111. /* Reset errorCode */
  2112. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  2113. }
  2114. /* Clear the Error flags in the SR register */
  2115. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  2116. __HAL_SPI_CLEAR_FREFLAG(hspi);
  2117. /* Restore hspi->state to ready */
  2118. hspi->State = HAL_SPI_STATE_READY;
  2119. return errorcode;
  2120. }
  2121. /**
  2122. * @brief Abort ongoing transfer (Interrupt mode).
  2123. * @param hspi SPI handle.
  2124. * @note This procedure could be used for aborting any ongoing transfer (Tx and Rx),
  2125. * started in Interrupt or DMA mode.
  2126. * This procedure performs following operations :
  2127. * - Disable SPI Interrupts (depending of transfer direction)
  2128. * - Disable the DMA transfer in the peripheral register (if enabled)
  2129. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  2130. * - Set handle State to READY
  2131. * - At abort completion, call user abort complete callback
  2132. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  2133. * considered as completed only when user abort complete callback is executed (not when exiting function).
  2134. * @retval HAL status
  2135. */
  2136. HAL_StatusTypeDef HAL_SPI_Abort_IT(SPI_HandleTypeDef *hspi)
  2137. {
  2138. HAL_StatusTypeDef errorcode;
  2139. uint32_t abortcplt ;
  2140. __IO uint32_t count;
  2141. __IO uint32_t resetcount;
  2142. /* Initialized local variable */
  2143. errorcode = HAL_OK;
  2144. abortcplt = 1U;
  2145. resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
  2146. count = resetcount;
  2147. /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
  2148. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE);
  2149. /* Change Rx and Tx Irq Handler to Disable TXEIE, RXNEIE and ERRIE interrupts */
  2150. if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE))
  2151. {
  2152. hspi->TxISR = SPI_AbortTx_ISR;
  2153. /* Wait HAL_SPI_STATE_ABORT state */
  2154. do
  2155. {
  2156. if (count == 0U)
  2157. {
  2158. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
  2159. break;
  2160. }
  2161. count--;
  2162. } while (hspi->State != HAL_SPI_STATE_ABORT);
  2163. /* Reset Timeout Counter */
  2164. count = resetcount;
  2165. }
  2166. if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
  2167. {
  2168. hspi->RxISR = SPI_AbortRx_ISR;
  2169. /* Wait HAL_SPI_STATE_ABORT state */
  2170. do
  2171. {
  2172. if (count == 0U)
  2173. {
  2174. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
  2175. break;
  2176. }
  2177. count--;
  2178. } while (hspi->State != HAL_SPI_STATE_ABORT);
  2179. /* Reset Timeout Counter */
  2180. count = resetcount;
  2181. }
  2182. /* If DMA Tx and/or DMA Rx Handles are associated to SPI Handle, DMA Abort complete callbacks should be initialised
  2183. before any call to DMA Abort functions */
  2184. /* DMA Tx Handle is valid */
  2185. if (hspi->hdmatx != NULL)
  2186. {
  2187. /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
  2188. Otherwise, set it to NULL */
  2189. if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
  2190. {
  2191. hspi->hdmatx->XferAbortCallback = SPI_DMATxAbortCallback;
  2192. }
  2193. else
  2194. {
  2195. hspi->hdmatx->XferAbortCallback = NULL;
  2196. }
  2197. }
  2198. /* DMA Rx Handle is valid */
  2199. if (hspi->hdmarx != NULL)
  2200. {
  2201. /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
  2202. Otherwise, set it to NULL */
  2203. if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
  2204. {
  2205. hspi->hdmarx->XferAbortCallback = SPI_DMARxAbortCallback;
  2206. }
  2207. else
  2208. {
  2209. hspi->hdmarx->XferAbortCallback = NULL;
  2210. }
  2211. }
  2212. /* Disable the SPI DMA Tx request if enabled */
  2213. if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
  2214. {
  2215. /* Abort the SPI DMA Tx Stream/Channel */
  2216. if (hspi->hdmatx != NULL)
  2217. {
  2218. /* Abort DMA Tx Handle linked to SPI Peripheral */
  2219. if (HAL_DMA_Abort_IT(hspi->hdmatx) != HAL_OK)
  2220. {
  2221. hspi->hdmatx->XferAbortCallback = NULL;
  2222. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  2223. }
  2224. else
  2225. {
  2226. abortcplt = 0U;
  2227. }
  2228. }
  2229. }
  2230. /* Disable the SPI DMA Rx request if enabled */
  2231. if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
  2232. {
  2233. /* Abort the SPI DMA Rx Stream/Channel */
  2234. if (hspi->hdmarx != NULL)
  2235. {
  2236. /* Abort DMA Rx Handle linked to SPI Peripheral */
  2237. if (HAL_DMA_Abort_IT(hspi->hdmarx) != HAL_OK)
  2238. {
  2239. hspi->hdmarx->XferAbortCallback = NULL;
  2240. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  2241. }
  2242. else
  2243. {
  2244. abortcplt = 0U;
  2245. }
  2246. }
  2247. }
  2248. if (abortcplt == 1U)
  2249. {
  2250. /* Reset Tx and Rx transfer counters */
  2251. hspi->RxXferCount = 0U;
  2252. hspi->TxXferCount = 0U;
  2253. /* Check error during Abort procedure */
  2254. if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT)
  2255. {
  2256. /* return HAL_Error in case of error during Abort procedure */
  2257. errorcode = HAL_ERROR;
  2258. }
  2259. else
  2260. {
  2261. /* Reset errorCode */
  2262. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  2263. }
  2264. /* Clear the Error flags in the SR register */
  2265. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  2266. __HAL_SPI_CLEAR_FREFLAG(hspi);
  2267. /* Restore hspi->State to Ready */
  2268. hspi->State = HAL_SPI_STATE_READY;
  2269. /* As no DMA to be aborted, call directly user Abort complete callback */
  2270. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2271. hspi->AbortCpltCallback(hspi);
  2272. #else
  2273. HAL_SPI_AbortCpltCallback(hspi);
  2274. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2275. }
  2276. return errorcode;
  2277. }
  2278. /**
  2279. * @brief Pause the DMA Transfer.
  2280. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2281. * the configuration information for the specified SPI module.
  2282. * @retval HAL status
  2283. */
  2284. HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi)
  2285. {
  2286. /* Process Locked */
  2287. __HAL_LOCK(hspi);
  2288. /* Disable the SPI DMA Tx & Rx requests */
  2289. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  2290. /* Process Unlocked */
  2291. __HAL_UNLOCK(hspi);
  2292. return HAL_OK;
  2293. }
  2294. /**
  2295. * @brief Resume the DMA Transfer.
  2296. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2297. * the configuration information for the specified SPI module.
  2298. * @retval HAL status
  2299. */
  2300. HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi)
  2301. {
  2302. /* Process Locked */
  2303. __HAL_LOCK(hspi);
  2304. /* Enable the SPI DMA Tx & Rx requests */
  2305. SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  2306. /* Process Unlocked */
  2307. __HAL_UNLOCK(hspi);
  2308. return HAL_OK;
  2309. }
  2310. /**
  2311. * @brief Stop the DMA Transfer.
  2312. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2313. * the configuration information for the specified SPI module.
  2314. * @retval HAL status
  2315. */
  2316. HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi)
  2317. {
  2318. HAL_StatusTypeDef errorcode = HAL_OK;
  2319. /* The Lock is not implemented on this API to allow the user application
  2320. to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback():
  2321. when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  2322. and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback()
  2323. */
  2324. /* Abort the SPI DMA tx Stream/Channel */
  2325. if (hspi->hdmatx != NULL)
  2326. {
  2327. if (HAL_OK != HAL_DMA_Abort(hspi->hdmatx))
  2328. {
  2329. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
  2330. errorcode = HAL_ERROR;
  2331. }
  2332. }
  2333. /* Abort the SPI DMA rx Stream/Channel */
  2334. if (hspi->hdmarx != NULL)
  2335. {
  2336. if (HAL_OK != HAL_DMA_Abort(hspi->hdmarx))
  2337. {
  2338. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
  2339. errorcode = HAL_ERROR;
  2340. }
  2341. }
  2342. /* Disable the SPI DMA Tx & Rx requests */
  2343. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  2344. hspi->State = HAL_SPI_STATE_READY;
  2345. return errorcode;
  2346. }
  2347. /**
  2348. * @brief Handle SPI interrupt request.
  2349. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2350. * the configuration information for the specified SPI module.
  2351. * @retval None
  2352. */
  2353. void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
  2354. {
  2355. uint32_t itsource = hspi->Instance->CR2;
  2356. uint32_t itflag = hspi->Instance->SR;
  2357. /* SPI in mode Receiver ----------------------------------------------------*/
  2358. if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) == RESET) &&
  2359. (SPI_CHECK_FLAG(itflag, SPI_FLAG_RXNE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_RXNE) != RESET))
  2360. {
  2361. hspi->RxISR(hspi);
  2362. return;
  2363. }
  2364. /* SPI in mode Transmitter -------------------------------------------------*/
  2365. if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_TXE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_TXE) != RESET))
  2366. {
  2367. hspi->TxISR(hspi);
  2368. return;
  2369. }
  2370. /* SPI in Error Treatment --------------------------------------------------*/
  2371. if (((SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET) || (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET)
  2372. || (SPI_CHECK_FLAG(itflag, SPI_FLAG_FRE) != RESET)) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_ERR) != RESET))
  2373. {
  2374. /* SPI Overrun error interrupt occurred ----------------------------------*/
  2375. if (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET)
  2376. {
  2377. if (hspi->State != HAL_SPI_STATE_BUSY_TX)
  2378. {
  2379. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_OVR);
  2380. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  2381. }
  2382. else
  2383. {
  2384. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  2385. return;
  2386. }
  2387. }
  2388. /* SPI Mode Fault error interrupt occurred -------------------------------*/
  2389. if (SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET)
  2390. {
  2391. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_MODF);
  2392. __HAL_SPI_CLEAR_MODFFLAG(hspi);
  2393. }
  2394. /* SPI Frame error interrupt occurred ------------------------------------*/
  2395. if (SPI_CHECK_FLAG(itflag, SPI_FLAG_FRE) != RESET)
  2396. {
  2397. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FRE);
  2398. __HAL_SPI_CLEAR_FREFLAG(hspi);
  2399. }
  2400. if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  2401. {
  2402. /* Disable all interrupts */
  2403. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR);
  2404. hspi->State = HAL_SPI_STATE_READY;
  2405. /* Disable the SPI DMA requests if enabled */
  2406. if ((HAL_IS_BIT_SET(itsource, SPI_CR2_TXDMAEN)) || (HAL_IS_BIT_SET(itsource, SPI_CR2_RXDMAEN)))
  2407. {
  2408. CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN));
  2409. /* Abort the SPI DMA Rx channel */
  2410. if (hspi->hdmarx != NULL)
  2411. {
  2412. /* Set the SPI DMA Abort callback :
  2413. will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
  2414. hspi->hdmarx->XferAbortCallback = SPI_DMAAbortOnError;
  2415. if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmarx))
  2416. {
  2417. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
  2418. }
  2419. }
  2420. /* Abort the SPI DMA Tx channel */
  2421. if (hspi->hdmatx != NULL)
  2422. {
  2423. /* Set the SPI DMA Abort callback :
  2424. will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
  2425. hspi->hdmatx->XferAbortCallback = SPI_DMAAbortOnError;
  2426. if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmatx))
  2427. {
  2428. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
  2429. }
  2430. }
  2431. }
  2432. else
  2433. {
  2434. /* Call user error callback */
  2435. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2436. hspi->ErrorCallback(hspi);
  2437. #else
  2438. HAL_SPI_ErrorCallback(hspi);
  2439. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2440. }
  2441. }
  2442. return;
  2443. }
  2444. }
  2445. /**
  2446. * @brief Tx Transfer completed callback.
  2447. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2448. * the configuration information for SPI module.
  2449. * @retval None
  2450. */
  2451. __weak void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
  2452. {
  2453. /* Prevent unused argument(s) compilation warning */
  2454. UNUSED(hspi);
  2455. /* NOTE : This function should not be modified, when the callback is needed,
  2456. the HAL_SPI_TxCpltCallback should be implemented in the user file
  2457. */
  2458. }
  2459. /**
  2460. * @brief Rx Transfer completed callback.
  2461. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2462. * the configuration information for SPI module.
  2463. * @retval None
  2464. */
  2465. __weak void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
  2466. {
  2467. /* Prevent unused argument(s) compilation warning */
  2468. UNUSED(hspi);
  2469. /* NOTE : This function should not be modified, when the callback is needed,
  2470. the HAL_SPI_RxCpltCallback should be implemented in the user file
  2471. */
  2472. }
  2473. /**
  2474. * @brief Tx and Rx Transfer completed callback.
  2475. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2476. * the configuration information for SPI module.
  2477. * @retval None
  2478. */
  2479. __weak void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
  2480. {
  2481. /* Prevent unused argument(s) compilation warning */
  2482. UNUSED(hspi);
  2483. /* NOTE : This function should not be modified, when the callback is needed,
  2484. the HAL_SPI_TxRxCpltCallback should be implemented in the user file
  2485. */
  2486. }
  2487. /**
  2488. * @brief Tx Half Transfer completed callback.
  2489. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2490. * the configuration information for SPI module.
  2491. * @retval None
  2492. */
  2493. __weak void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  2494. {
  2495. /* Prevent unused argument(s) compilation warning */
  2496. UNUSED(hspi);
  2497. /* NOTE : This function should not be modified, when the callback is needed,
  2498. the HAL_SPI_TxHalfCpltCallback should be implemented in the user file
  2499. */
  2500. }
  2501. /**
  2502. * @brief Rx Half Transfer completed callback.
  2503. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2504. * the configuration information for SPI module.
  2505. * @retval None
  2506. */
  2507. __weak void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  2508. {
  2509. /* Prevent unused argument(s) compilation warning */
  2510. UNUSED(hspi);
  2511. /* NOTE : This function should not be modified, when the callback is needed,
  2512. the HAL_SPI_RxHalfCpltCallback() should be implemented in the user file
  2513. */
  2514. }
  2515. /**
  2516. * @brief Tx and Rx Half Transfer callback.
  2517. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2518. * the configuration information for SPI module.
  2519. * @retval None
  2520. */
  2521. __weak void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  2522. {
  2523. /* Prevent unused argument(s) compilation warning */
  2524. UNUSED(hspi);
  2525. /* NOTE : This function should not be modified, when the callback is needed,
  2526. the HAL_SPI_TxRxHalfCpltCallback() should be implemented in the user file
  2527. */
  2528. }
  2529. /**
  2530. * @brief SPI error callback.
  2531. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2532. * the configuration information for SPI module.
  2533. * @retval None
  2534. */
  2535. __weak void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
  2536. {
  2537. /* Prevent unused argument(s) compilation warning */
  2538. UNUSED(hspi);
  2539. /* NOTE : This function should not be modified, when the callback is needed,
  2540. the HAL_SPI_ErrorCallback should be implemented in the user file
  2541. */
  2542. /* NOTE : The ErrorCode parameter in the hspi handle is updated by the SPI processes
  2543. and user can use HAL_SPI_GetError() API to check the latest error occurred
  2544. */
  2545. }
  2546. /**
  2547. * @brief SPI Abort Complete callback.
  2548. * @param hspi SPI handle.
  2549. * @retval None
  2550. */
  2551. __weak void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef *hspi)
  2552. {
  2553. /* Prevent unused argument(s) compilation warning */
  2554. UNUSED(hspi);
  2555. /* NOTE : This function should not be modified, when the callback is needed,
  2556. the HAL_SPI_AbortCpltCallback can be implemented in the user file.
  2557. */
  2558. }
  2559. /**
  2560. * @}
  2561. */
  2562. /** @defgroup SPI_Exported_Functions_Group3 Peripheral State and Errors functions
  2563. * @brief SPI control functions
  2564. *
  2565. @verbatim
  2566. ===============================================================================
  2567. ##### Peripheral State and Errors functions #####
  2568. ===============================================================================
  2569. [..]
  2570. This subsection provides a set of functions allowing to control the SPI.
  2571. (+) HAL_SPI_GetState() API can be helpful to check in run-time the state of the SPI peripheral
  2572. (+) HAL_SPI_GetError() check in run-time Errors occurring during communication
  2573. @endverbatim
  2574. * @{
  2575. */
  2576. /**
  2577. * @brief Return the SPI handle state.
  2578. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2579. * the configuration information for SPI module.
  2580. * @retval SPI state
  2581. */
  2582. HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi)
  2583. {
  2584. /* Return SPI handle state */
  2585. return hspi->State;
  2586. }
  2587. /**
  2588. * @brief Return the SPI error code.
  2589. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  2590. * the configuration information for SPI module.
  2591. * @retval SPI error code in bitmap format
  2592. */
  2593. uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi)
  2594. {
  2595. /* Return SPI ErrorCode */
  2596. return hspi->ErrorCode;
  2597. }
  2598. /**
  2599. * @}
  2600. */
  2601. /**
  2602. * @}
  2603. */
  2604. /** @addtogroup SPI_Private_Functions
  2605. * @brief Private functions
  2606. * @{
  2607. */
  2608. /**
  2609. * @brief DMA SPI transmit process complete callback.
  2610. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2611. * the configuration information for the specified DMA module.
  2612. * @retval None
  2613. */
  2614. static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2615. {
  2616. SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  2617. uint32_t tickstart;
  2618. /* Init tickstart for timeout management*/
  2619. tickstart = HAL_GetTick();
  2620. /* DMA Normal Mode */
  2621. if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
  2622. {
  2623. /* Disable ERR interrupt */
  2624. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
  2625. /* Disable Tx DMA Request */
  2626. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
  2627. /* Check the end of the transaction */
  2628. if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
  2629. {
  2630. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
  2631. }
  2632. /* Clear overrun flag in 2 Lines communication mode because received data is not read */
  2633. if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
  2634. {
  2635. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  2636. }
  2637. hspi->TxXferCount = 0U;
  2638. hspi->State = HAL_SPI_STATE_READY;
  2639. if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  2640. {
  2641. /* Call user error callback */
  2642. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2643. hspi->ErrorCallback(hspi);
  2644. #else
  2645. HAL_SPI_ErrorCallback(hspi);
  2646. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2647. return;
  2648. }
  2649. }
  2650. /* Call user Tx complete callback */
  2651. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2652. hspi->TxCpltCallback(hspi);
  2653. #else
  2654. HAL_SPI_TxCpltCallback(hspi);
  2655. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2656. }
  2657. /**
  2658. * @brief DMA SPI receive process complete callback.
  2659. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2660. * the configuration information for the specified DMA module.
  2661. * @retval None
  2662. */
  2663. static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2664. {
  2665. SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  2666. uint32_t tickstart;
  2667. #if (USE_SPI_CRC != 0U)
  2668. __IO uint32_t tmpreg = 0U;
  2669. __IO uint8_t * ptmpreg8;
  2670. __IO uint8_t tmpreg8 = 0;
  2671. #endif /* USE_SPI_CRC */
  2672. /* Init tickstart for timeout management*/
  2673. tickstart = HAL_GetTick();
  2674. /* DMA Normal Mode */
  2675. if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
  2676. {
  2677. /* Disable ERR interrupt */
  2678. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
  2679. #if (USE_SPI_CRC != 0U)
  2680. /* CRC handling */
  2681. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2682. {
  2683. /* Wait until RXNE flag */
  2684. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
  2685. {
  2686. /* Error on the CRC reception */
  2687. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  2688. }
  2689. /* Read CRC */
  2690. if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  2691. {
  2692. /* Read 16bit CRC */
  2693. tmpreg = READ_REG(hspi->Instance->DR);
  2694. /* To avoid GCC warning */
  2695. UNUSED(tmpreg);
  2696. }
  2697. else
  2698. {
  2699. /* Initialize the 8bit temporary pointer */
  2700. ptmpreg8 = (__IO uint8_t *)&hspi->Instance->DR;
  2701. /* Read 8bit CRC */
  2702. tmpreg8 = *ptmpreg8;
  2703. /* To avoid GCC warning */
  2704. UNUSED(tmpreg8);
  2705. if (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
  2706. {
  2707. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
  2708. {
  2709. /* Error on the CRC reception */
  2710. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  2711. }
  2712. /* Read 8bit CRC again in case of 16bit CRC in 8bit Data mode */
  2713. tmpreg8 = *ptmpreg8;
  2714. /* To avoid GCC warning */
  2715. UNUSED(tmpreg8);
  2716. }
  2717. }
  2718. }
  2719. #endif /* USE_SPI_CRC */
  2720. /* Check if we are in Master RX 2 line mode */
  2721. if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
  2722. {
  2723. /* Disable Rx/Tx DMA Request (done by default to handle the case master rx direction 2 lines) */
  2724. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  2725. }
  2726. else
  2727. {
  2728. /* Normal case */
  2729. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
  2730. }
  2731. /* Check the end of the transaction */
  2732. if (SPI_EndRxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
  2733. {
  2734. hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
  2735. }
  2736. hspi->RxXferCount = 0U;
  2737. hspi->State = HAL_SPI_STATE_READY;
  2738. #if (USE_SPI_CRC != 0U)
  2739. /* Check if CRC error occurred */
  2740. if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR))
  2741. {
  2742. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  2743. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  2744. }
  2745. #endif /* USE_SPI_CRC */
  2746. if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  2747. {
  2748. /* Call user error callback */
  2749. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2750. hspi->ErrorCallback(hspi);
  2751. #else
  2752. HAL_SPI_ErrorCallback(hspi);
  2753. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2754. return;
  2755. }
  2756. }
  2757. /* Call user Rx complete callback */
  2758. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2759. hspi->RxCpltCallback(hspi);
  2760. #else
  2761. HAL_SPI_RxCpltCallback(hspi);
  2762. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2763. }
  2764. /**
  2765. * @brief DMA SPI transmit receive process complete callback.
  2766. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2767. * the configuration information for the specified DMA module.
  2768. * @retval None
  2769. */
  2770. static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma)
  2771. {
  2772. SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  2773. uint32_t tickstart;
  2774. #if (USE_SPI_CRC != 0U)
  2775. __IO uint32_t tmpreg = 0U;
  2776. __IO uint8_t * ptmpreg8;
  2777. __IO uint8_t tmpreg8 = 0;
  2778. #endif /* USE_SPI_CRC */
  2779. /* Init tickstart for timeout management*/
  2780. tickstart = HAL_GetTick();
  2781. /* DMA Normal Mode */
  2782. if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
  2783. {
  2784. /* Disable ERR interrupt */
  2785. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
  2786. #if (USE_SPI_CRC != 0U)
  2787. /* CRC handling */
  2788. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2789. {
  2790. if ((hspi->Init.DataSize == SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_8BIT))
  2791. {
  2792. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_QUARTER_FULL, SPI_DEFAULT_TIMEOUT,
  2793. tickstart) != HAL_OK)
  2794. {
  2795. /* Error on the CRC reception */
  2796. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  2797. }
  2798. /* Initialize the 8bit temporary pointer */
  2799. ptmpreg8 = (__IO uint8_t *)&hspi->Instance->DR;
  2800. /* Read 8bit CRC */
  2801. tmpreg8 = *ptmpreg8;
  2802. /* To avoid GCC warning */
  2803. UNUSED(tmpreg8);
  2804. }
  2805. else
  2806. {
  2807. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_HALF_FULL, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
  2808. {
  2809. /* Error on the CRC reception */
  2810. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  2811. }
  2812. /* Read CRC to Flush DR and RXNE flag */
  2813. tmpreg = READ_REG(hspi->Instance->DR);
  2814. /* To avoid GCC warning */
  2815. UNUSED(tmpreg);
  2816. }
  2817. }
  2818. #endif /* USE_SPI_CRC */
  2819. /* Check the end of the transaction */
  2820. if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
  2821. {
  2822. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
  2823. }
  2824. /* Disable Rx/Tx DMA Request */
  2825. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  2826. hspi->TxXferCount = 0U;
  2827. hspi->RxXferCount = 0U;
  2828. hspi->State = HAL_SPI_STATE_READY;
  2829. #if (USE_SPI_CRC != 0U)
  2830. /* Check if CRC error occurred */
  2831. if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR))
  2832. {
  2833. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  2834. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  2835. }
  2836. #endif /* USE_SPI_CRC */
  2837. if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  2838. {
  2839. /* Call user error callback */
  2840. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2841. hspi->ErrorCallback(hspi);
  2842. #else
  2843. HAL_SPI_ErrorCallback(hspi);
  2844. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2845. return;
  2846. }
  2847. }
  2848. /* Call user TxRx complete callback */
  2849. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2850. hspi->TxRxCpltCallback(hspi);
  2851. #else
  2852. HAL_SPI_TxRxCpltCallback(hspi);
  2853. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2854. }
  2855. /**
  2856. * @brief DMA SPI half transmit process complete callback.
  2857. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2858. * the configuration information for the specified DMA module.
  2859. * @retval None
  2860. */
  2861. static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma)
  2862. {
  2863. SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  2864. /* Call user Tx half complete callback */
  2865. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2866. hspi->TxHalfCpltCallback(hspi);
  2867. #else
  2868. HAL_SPI_TxHalfCpltCallback(hspi);
  2869. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2870. }
  2871. /**
  2872. * @brief DMA SPI half receive process complete callback
  2873. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2874. * the configuration information for the specified DMA module.
  2875. * @retval None
  2876. */
  2877. static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma)
  2878. {
  2879. SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  2880. /* Call user Rx half complete callback */
  2881. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2882. hspi->RxHalfCpltCallback(hspi);
  2883. #else
  2884. HAL_SPI_RxHalfCpltCallback(hspi);
  2885. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2886. }
  2887. /**
  2888. * @brief DMA SPI half transmit receive process complete callback.
  2889. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2890. * the configuration information for the specified DMA module.
  2891. * @retval None
  2892. */
  2893. static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma)
  2894. {
  2895. SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  2896. /* Call user TxRx half complete callback */
  2897. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2898. hspi->TxRxHalfCpltCallback(hspi);
  2899. #else
  2900. HAL_SPI_TxRxHalfCpltCallback(hspi);
  2901. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2902. }
  2903. /**
  2904. * @brief DMA SPI communication error callback.
  2905. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  2906. * the configuration information for the specified DMA module.
  2907. * @retval None
  2908. */
  2909. static void SPI_DMAError(DMA_HandleTypeDef *hdma)
  2910. {
  2911. SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  2912. /* Stop the disable DMA transfer on SPI side */
  2913. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  2914. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
  2915. hspi->State = HAL_SPI_STATE_READY;
  2916. /* Call user error callback */
  2917. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2918. hspi->ErrorCallback(hspi);
  2919. #else
  2920. HAL_SPI_ErrorCallback(hspi);
  2921. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2922. }
  2923. /**
  2924. * @brief DMA SPI communication abort callback, when initiated by HAL services on Error
  2925. * (To be called at end of DMA Abort procedure following error occurrence).
  2926. * @param hdma DMA handle.
  2927. * @retval None
  2928. */
  2929. static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  2930. {
  2931. SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  2932. hspi->RxXferCount = 0U;
  2933. hspi->TxXferCount = 0U;
  2934. /* Call user error callback */
  2935. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2936. hspi->ErrorCallback(hspi);
  2937. #else
  2938. HAL_SPI_ErrorCallback(hspi);
  2939. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2940. }
  2941. /**
  2942. * @brief DMA SPI Tx communication abort callback, when initiated by user
  2943. * (To be called at end of DMA Tx Abort procedure following user abort request).
  2944. * @note When this callback is executed, User Abort complete call back is called only if no
  2945. * Abort still ongoing for Rx DMA Handle.
  2946. * @param hdma DMA handle.
  2947. * @retval None
  2948. */
  2949. static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  2950. {
  2951. SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  2952. hspi->hdmatx->XferAbortCallback = NULL;
  2953. /* Disable Tx DMA Request */
  2954. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
  2955. if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  2956. {
  2957. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  2958. }
  2959. /* Disable SPI Peripheral */
  2960. __HAL_SPI_DISABLE(hspi);
  2961. /* Empty the FRLVL fifo */
  2962. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  2963. {
  2964. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  2965. }
  2966. /* Check if an Abort process is still ongoing */
  2967. if (hspi->hdmarx != NULL)
  2968. {
  2969. if (hspi->hdmarx->XferAbortCallback != NULL)
  2970. {
  2971. return;
  2972. }
  2973. }
  2974. /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */
  2975. hspi->RxXferCount = 0U;
  2976. hspi->TxXferCount = 0U;
  2977. /* Check no error during Abort procedure */
  2978. if (hspi->ErrorCode != HAL_SPI_ERROR_ABORT)
  2979. {
  2980. /* Reset errorCode */
  2981. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  2982. }
  2983. /* Clear the Error flags in the SR register */
  2984. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  2985. __HAL_SPI_CLEAR_FREFLAG(hspi);
  2986. /* Restore hspi->State to Ready */
  2987. hspi->State = HAL_SPI_STATE_READY;
  2988. /* Call user Abort complete callback */
  2989. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  2990. hspi->AbortCpltCallback(hspi);
  2991. #else
  2992. HAL_SPI_AbortCpltCallback(hspi);
  2993. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  2994. }
  2995. /**
  2996. * @brief DMA SPI Rx communication abort callback, when initiated by user
  2997. * (To be called at end of DMA Rx Abort procedure following user abort request).
  2998. * @note When this callback is executed, User Abort complete call back is called only if no
  2999. * Abort still ongoing for Tx DMA Handle.
  3000. * @param hdma DMA handle.
  3001. * @retval None
  3002. */
  3003. static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  3004. {
  3005. SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  3006. /* Disable SPI Peripheral */
  3007. __HAL_SPI_DISABLE(hspi);
  3008. hspi->hdmarx->XferAbortCallback = NULL;
  3009. /* Disable Rx DMA Request */
  3010. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
  3011. /* Control the BSY flag */
  3012. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  3013. {
  3014. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  3015. }
  3016. /* Empty the FRLVL fifo */
  3017. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  3018. {
  3019. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  3020. }
  3021. /* Check if an Abort process is still ongoing */
  3022. if (hspi->hdmatx != NULL)
  3023. {
  3024. if (hspi->hdmatx->XferAbortCallback != NULL)
  3025. {
  3026. return;
  3027. }
  3028. }
  3029. /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */
  3030. hspi->RxXferCount = 0U;
  3031. hspi->TxXferCount = 0U;
  3032. /* Check no error during Abort procedure */
  3033. if (hspi->ErrorCode != HAL_SPI_ERROR_ABORT)
  3034. {
  3035. /* Reset errorCode */
  3036. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  3037. }
  3038. /* Clear the Error flags in the SR register */
  3039. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  3040. __HAL_SPI_CLEAR_FREFLAG(hspi);
  3041. /* Restore hspi->State to Ready */
  3042. hspi->State = HAL_SPI_STATE_READY;
  3043. /* Call user Abort complete callback */
  3044. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  3045. hspi->AbortCpltCallback(hspi);
  3046. #else
  3047. HAL_SPI_AbortCpltCallback(hspi);
  3048. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  3049. }
  3050. /**
  3051. * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode.
  3052. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3053. * the configuration information for SPI module.
  3054. * @retval None
  3055. */
  3056. static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  3057. {
  3058. /* Receive data in packing mode */
  3059. if (hspi->RxXferCount > 1U)
  3060. {
  3061. *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR);
  3062. hspi->pRxBuffPtr += sizeof(uint16_t);
  3063. hspi->RxXferCount -= 2U;
  3064. if (hspi->RxXferCount == 1U)
  3065. {
  3066. /* Set RX Fifo threshold according the reception data length: 8bit */
  3067. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  3068. }
  3069. }
  3070. /* Receive data in 8 Bit mode */
  3071. else
  3072. {
  3073. *hspi->pRxBuffPtr = *((__IO uint8_t *)&hspi->Instance->DR);
  3074. hspi->pRxBuffPtr++;
  3075. hspi->RxXferCount--;
  3076. }
  3077. /* Check end of the reception */
  3078. if (hspi->RxXferCount == 0U)
  3079. {
  3080. #if (USE_SPI_CRC != 0U)
  3081. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  3082. {
  3083. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  3084. hspi->RxISR = SPI_2linesRxISR_8BITCRC;
  3085. return;
  3086. }
  3087. #endif /* USE_SPI_CRC */
  3088. /* Disable RXNE and ERR interrupt */
  3089. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  3090. if (hspi->TxXferCount == 0U)
  3091. {
  3092. SPI_CloseRxTx_ISR(hspi);
  3093. }
  3094. }
  3095. }
  3096. #if (USE_SPI_CRC != 0U)
  3097. /**
  3098. * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode.
  3099. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3100. * the configuration information for SPI module.
  3101. * @retval None
  3102. */
  3103. static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
  3104. {
  3105. __IO uint8_t * ptmpreg8;
  3106. __IO uint8_t tmpreg8 = 0;
  3107. /* Initialize the 8bit temporary pointer */
  3108. ptmpreg8 = (__IO uint8_t *)&hspi->Instance->DR;
  3109. /* Read 8bit CRC to flush Data Register */
  3110. tmpreg8 = *ptmpreg8;
  3111. /* To avoid GCC warning */
  3112. UNUSED(tmpreg8);
  3113. hspi->CRCSize--;
  3114. /* Check end of the reception */
  3115. if (hspi->CRCSize == 0U)
  3116. {
  3117. /* Disable RXNE and ERR interrupt */
  3118. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  3119. if (hspi->TxXferCount == 0U)
  3120. {
  3121. SPI_CloseRxTx_ISR(hspi);
  3122. }
  3123. }
  3124. }
  3125. #endif /* USE_SPI_CRC */
  3126. /**
  3127. * @brief Tx 8-bit handler for Transmit and Receive in Interrupt mode.
  3128. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3129. * the configuration information for SPI module.
  3130. * @retval None
  3131. */
  3132. static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  3133. {
  3134. /* Transmit data in packing Bit mode */
  3135. if (hspi->TxXferCount >= 2U)
  3136. {
  3137. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  3138. hspi->pTxBuffPtr += sizeof(uint16_t);
  3139. hspi->TxXferCount -= 2U;
  3140. }
  3141. /* Transmit data in 8 Bit mode */
  3142. else
  3143. {
  3144. *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
  3145. hspi->pTxBuffPtr++;
  3146. hspi->TxXferCount--;
  3147. }
  3148. /* Check the end of the transmission */
  3149. if (hspi->TxXferCount == 0U)
  3150. {
  3151. #if (USE_SPI_CRC != 0U)
  3152. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  3153. {
  3154. /* Set CRC Next Bit to send CRC */
  3155. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  3156. /* Disable TXE interrupt */
  3157. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
  3158. return;
  3159. }
  3160. #endif /* USE_SPI_CRC */
  3161. /* Disable TXE interrupt */
  3162. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
  3163. if (hspi->RxXferCount == 0U)
  3164. {
  3165. SPI_CloseRxTx_ISR(hspi);
  3166. }
  3167. }
  3168. }
  3169. /**
  3170. * @brief Rx 16-bit handler for Transmit and Receive in Interrupt mode.
  3171. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3172. * the configuration information for SPI module.
  3173. * @retval None
  3174. */
  3175. static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  3176. {
  3177. /* Receive data in 16 Bit mode */
  3178. *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR);
  3179. hspi->pRxBuffPtr += sizeof(uint16_t);
  3180. hspi->RxXferCount--;
  3181. if (hspi->RxXferCount == 0U)
  3182. {
  3183. #if (USE_SPI_CRC != 0U)
  3184. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  3185. {
  3186. hspi->RxISR = SPI_2linesRxISR_16BITCRC;
  3187. return;
  3188. }
  3189. #endif /* USE_SPI_CRC */
  3190. /* Disable RXNE interrupt */
  3191. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
  3192. if (hspi->TxXferCount == 0U)
  3193. {
  3194. SPI_CloseRxTx_ISR(hspi);
  3195. }
  3196. }
  3197. }
  3198. #if (USE_SPI_CRC != 0U)
  3199. /**
  3200. * @brief Manage the CRC 16-bit receive for Transmit and Receive in Interrupt mode.
  3201. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3202. * the configuration information for SPI module.
  3203. * @retval None
  3204. */
  3205. static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
  3206. {
  3207. __IO uint32_t tmpreg = 0U;
  3208. /* Read 16bit CRC to flush Data Register */
  3209. tmpreg = READ_REG(hspi->Instance->DR);
  3210. /* To avoid GCC warning */
  3211. UNUSED(tmpreg);
  3212. /* Disable RXNE interrupt */
  3213. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
  3214. SPI_CloseRxTx_ISR(hspi);
  3215. }
  3216. #endif /* USE_SPI_CRC */
  3217. /**
  3218. * @brief Tx 16-bit handler for Transmit and Receive in Interrupt mode.
  3219. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3220. * the configuration information for SPI module.
  3221. * @retval None
  3222. */
  3223. static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  3224. {
  3225. /* Transmit data in 16 Bit mode */
  3226. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  3227. hspi->pTxBuffPtr += sizeof(uint16_t);
  3228. hspi->TxXferCount--;
  3229. /* Enable CRC Transmission */
  3230. if (hspi->TxXferCount == 0U)
  3231. {
  3232. #if (USE_SPI_CRC != 0U)
  3233. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  3234. {
  3235. /* Set CRC Next Bit to send CRC */
  3236. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  3237. /* Disable TXE interrupt */
  3238. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
  3239. return;
  3240. }
  3241. #endif /* USE_SPI_CRC */
  3242. /* Disable TXE interrupt */
  3243. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
  3244. if (hspi->RxXferCount == 0U)
  3245. {
  3246. SPI_CloseRxTx_ISR(hspi);
  3247. }
  3248. }
  3249. }
  3250. #if (USE_SPI_CRC != 0U)
  3251. /**
  3252. * @brief Manage the CRC 8-bit receive in Interrupt context.
  3253. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3254. * the configuration information for SPI module.
  3255. * @retval None
  3256. */
  3257. static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
  3258. {
  3259. __IO uint8_t * ptmpreg8;
  3260. __IO uint8_t tmpreg8 = 0;
  3261. /* Initialize the 8bit temporary pointer */
  3262. ptmpreg8 = (__IO uint8_t *)&hspi->Instance->DR;
  3263. /* Read 8bit CRC to flush Data Register */
  3264. tmpreg8 = *ptmpreg8;
  3265. /* To avoid GCC warning */
  3266. UNUSED(tmpreg8);
  3267. hspi->CRCSize--;
  3268. if (hspi->CRCSize == 0U)
  3269. {
  3270. SPI_CloseRx_ISR(hspi);
  3271. }
  3272. }
  3273. #endif /* USE_SPI_CRC */
  3274. /**
  3275. * @brief Manage the receive 8-bit in Interrupt context.
  3276. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3277. * the configuration information for SPI module.
  3278. * @retval None
  3279. */
  3280. static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  3281. {
  3282. *hspi->pRxBuffPtr = (*(__IO uint8_t *)&hspi->Instance->DR);
  3283. hspi->pRxBuffPtr++;
  3284. hspi->RxXferCount--;
  3285. #if (USE_SPI_CRC != 0U)
  3286. /* Enable CRC Transmission */
  3287. if ((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  3288. {
  3289. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  3290. }
  3291. #endif /* USE_SPI_CRC */
  3292. if (hspi->RxXferCount == 0U)
  3293. {
  3294. #if (USE_SPI_CRC != 0U)
  3295. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  3296. {
  3297. hspi->RxISR = SPI_RxISR_8BITCRC;
  3298. return;
  3299. }
  3300. #endif /* USE_SPI_CRC */
  3301. SPI_CloseRx_ISR(hspi);
  3302. }
  3303. }
  3304. #if (USE_SPI_CRC != 0U)
  3305. /**
  3306. * @brief Manage the CRC 16-bit receive in Interrupt context.
  3307. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3308. * the configuration information for SPI module.
  3309. * @retval None
  3310. */
  3311. static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
  3312. {
  3313. __IO uint32_t tmpreg = 0U;
  3314. /* Read 16bit CRC to flush Data Register */
  3315. tmpreg = READ_REG(hspi->Instance->DR);
  3316. /* To avoid GCC warning */
  3317. UNUSED(tmpreg);
  3318. /* Disable RXNE and ERR interrupt */
  3319. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  3320. SPI_CloseRx_ISR(hspi);
  3321. }
  3322. #endif /* USE_SPI_CRC */
  3323. /**
  3324. * @brief Manage the 16-bit receive in Interrupt context.
  3325. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3326. * the configuration information for SPI module.
  3327. * @retval None
  3328. */
  3329. static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  3330. {
  3331. *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR);
  3332. hspi->pRxBuffPtr += sizeof(uint16_t);
  3333. hspi->RxXferCount--;
  3334. #if (USE_SPI_CRC != 0U)
  3335. /* Enable CRC Transmission */
  3336. if ((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  3337. {
  3338. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  3339. }
  3340. #endif /* USE_SPI_CRC */
  3341. if (hspi->RxXferCount == 0U)
  3342. {
  3343. #if (USE_SPI_CRC != 0U)
  3344. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  3345. {
  3346. hspi->RxISR = SPI_RxISR_16BITCRC;
  3347. return;
  3348. }
  3349. #endif /* USE_SPI_CRC */
  3350. SPI_CloseRx_ISR(hspi);
  3351. }
  3352. }
  3353. /**
  3354. * @brief Handle the data 8-bit transmit in Interrupt mode.
  3355. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3356. * the configuration information for SPI module.
  3357. * @retval None
  3358. */
  3359. static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  3360. {
  3361. *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
  3362. hspi->pTxBuffPtr++;
  3363. hspi->TxXferCount--;
  3364. if (hspi->TxXferCount == 0U)
  3365. {
  3366. #if (USE_SPI_CRC != 0U)
  3367. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  3368. {
  3369. /* Enable CRC Transmission */
  3370. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  3371. }
  3372. #endif /* USE_SPI_CRC */
  3373. SPI_CloseTx_ISR(hspi);
  3374. }
  3375. }
  3376. /**
  3377. * @brief Handle the data 16-bit transmit in Interrupt mode.
  3378. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3379. * the configuration information for SPI module.
  3380. * @retval None
  3381. */
  3382. static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  3383. {
  3384. /* Transmit data in 16 Bit mode */
  3385. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  3386. hspi->pTxBuffPtr += sizeof(uint16_t);
  3387. hspi->TxXferCount--;
  3388. if (hspi->TxXferCount == 0U)
  3389. {
  3390. #if (USE_SPI_CRC != 0U)
  3391. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  3392. {
  3393. /* Enable CRC Transmission */
  3394. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  3395. }
  3396. #endif /* USE_SPI_CRC */
  3397. SPI_CloseTx_ISR(hspi);
  3398. }
  3399. }
  3400. /**
  3401. * @brief Handle SPI Communication Timeout.
  3402. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3403. * the configuration information for SPI module.
  3404. * @param Flag SPI flag to check
  3405. * @param State flag state to check
  3406. * @param Timeout Timeout duration
  3407. * @param Tickstart tick start value
  3408. * @retval HAL status
  3409. */
  3410. static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State,
  3411. uint32_t Timeout, uint32_t Tickstart)
  3412. {
  3413. __IO uint32_t count;
  3414. uint32_t tmp_timeout;
  3415. uint32_t tmp_tickstart;
  3416. /* Adjust Timeout value in case of end of transfer */
  3417. tmp_timeout = Timeout - (HAL_GetTick() - Tickstart);
  3418. tmp_tickstart = HAL_GetTick();
  3419. /* Calculate Timeout based on a software loop to avoid blocking issue if Systick is disabled */
  3420. count = tmp_timeout * ((SystemCoreClock * 32U) >> 20U);
  3421. while ((__HAL_SPI_GET_FLAG(hspi, Flag) ? SET : RESET) != State)
  3422. {
  3423. if (Timeout != HAL_MAX_DELAY)
  3424. {
  3425. if (((HAL_GetTick() - tmp_tickstart) >= tmp_timeout) || (tmp_timeout == 0U))
  3426. {
  3427. /* Disable the SPI and reset the CRC: the CRC value should be cleared
  3428. on both master and slave sides in order to resynchronize the master
  3429. and slave for their respective CRC calculation */
  3430. /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
  3431. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  3432. if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
  3433. || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  3434. {
  3435. /* Disable SPI peripheral */
  3436. __HAL_SPI_DISABLE(hspi);
  3437. }
  3438. /* Reset CRC Calculation */
  3439. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  3440. {
  3441. SPI_RESET_CRC(hspi);
  3442. }
  3443. hspi->State = HAL_SPI_STATE_READY;
  3444. /* Process Unlocked */
  3445. __HAL_UNLOCK(hspi);
  3446. return HAL_TIMEOUT;
  3447. }
  3448. /* If Systick is disabled or not incremented, deactivate timeout to go in disable loop procedure */
  3449. if(count == 0U)
  3450. {
  3451. tmp_timeout = 0U;
  3452. }
  3453. count--;
  3454. }
  3455. }
  3456. return HAL_OK;
  3457. }
  3458. /**
  3459. * @brief Handle SPI FIFO Communication Timeout.
  3460. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3461. * the configuration information for SPI module.
  3462. * @param Fifo Fifo to check
  3463. * @param State Fifo state to check
  3464. * @param Timeout Timeout duration
  3465. * @param Tickstart tick start value
  3466. * @retval HAL status
  3467. */
  3468. static HAL_StatusTypeDef SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Fifo, uint32_t State,
  3469. uint32_t Timeout, uint32_t Tickstart)
  3470. {
  3471. __IO uint32_t count;
  3472. uint32_t tmp_timeout;
  3473. uint32_t tmp_tickstart;
  3474. __IO uint8_t * ptmpreg8;
  3475. __IO uint8_t tmpreg8 = 0;
  3476. /* Adjust Timeout value in case of end of transfer */
  3477. tmp_timeout = Timeout - (HAL_GetTick() - Tickstart);
  3478. tmp_tickstart = HAL_GetTick();
  3479. /* Initialize the 8bit temporary pointer */
  3480. ptmpreg8 = (__IO uint8_t *)&hspi->Instance->DR;
  3481. /* Calculate Timeout based on a software loop to avoid blocking issue if Systick is disabled */
  3482. count = tmp_timeout * ((SystemCoreClock * 35U) >> 20U);
  3483. while ((hspi->Instance->SR & Fifo) != State)
  3484. {
  3485. if ((Fifo == SPI_SR_FRLVL) && (State == SPI_FRLVL_EMPTY))
  3486. {
  3487. /* Flush Data Register by a blank read */
  3488. tmpreg8 = *ptmpreg8;
  3489. /* To avoid GCC warning */
  3490. UNUSED(tmpreg8);
  3491. }
  3492. if (Timeout != HAL_MAX_DELAY)
  3493. {
  3494. if (((HAL_GetTick() - tmp_tickstart) >= tmp_timeout) || (tmp_timeout == 0U))
  3495. {
  3496. /* Disable the SPI and reset the CRC: the CRC value should be cleared
  3497. on both master and slave sides in order to resynchronize the master
  3498. and slave for their respective CRC calculation */
  3499. /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
  3500. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  3501. if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
  3502. || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  3503. {
  3504. /* Disable SPI peripheral */
  3505. __HAL_SPI_DISABLE(hspi);
  3506. }
  3507. /* Reset CRC Calculation */
  3508. if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  3509. {
  3510. SPI_RESET_CRC(hspi);
  3511. }
  3512. hspi->State = HAL_SPI_STATE_READY;
  3513. /* Process Unlocked */
  3514. __HAL_UNLOCK(hspi);
  3515. return HAL_TIMEOUT;
  3516. }
  3517. /* If Systick is disabled or not incremented, deactivate timeout to go in disable loop procedure */
  3518. if(count == 0U)
  3519. {
  3520. tmp_timeout = 0U;
  3521. }
  3522. count--;
  3523. }
  3524. }
  3525. return HAL_OK;
  3526. }
  3527. /**
  3528. * @brief Handle the check of the RX transaction complete.
  3529. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3530. * the configuration information for SPI module.
  3531. * @param Timeout Timeout duration
  3532. * @param Tickstart tick start value
  3533. * @retval HAL status
  3534. */
  3535. static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
  3536. {
  3537. if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
  3538. || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  3539. {
  3540. /* Disable SPI peripheral */
  3541. __HAL_SPI_DISABLE(hspi);
  3542. }
  3543. /* Control the BSY flag */
  3544. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout, Tickstart) != HAL_OK)
  3545. {
  3546. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
  3547. return HAL_TIMEOUT;
  3548. }
  3549. if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
  3550. || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  3551. {
  3552. /* Empty the FRLVL fifo */
  3553. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, Timeout, Tickstart) != HAL_OK)
  3554. {
  3555. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
  3556. return HAL_TIMEOUT;
  3557. }
  3558. }
  3559. return HAL_OK;
  3560. }
  3561. /**
  3562. * @brief Handle the check of the RXTX or TX transaction complete.
  3563. * @param hspi SPI handle
  3564. * @param Timeout Timeout duration
  3565. * @param Tickstart tick start value
  3566. * @retval HAL status
  3567. */
  3568. static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
  3569. {
  3570. /* Control if the TX fifo is empty */
  3571. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FTLVL, SPI_FTLVL_EMPTY, Timeout, Tickstart) != HAL_OK)
  3572. {
  3573. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
  3574. return HAL_TIMEOUT;
  3575. }
  3576. /* Control the BSY flag */
  3577. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout, Tickstart) != HAL_OK)
  3578. {
  3579. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
  3580. return HAL_TIMEOUT;
  3581. }
  3582. /* Control if the RX fifo is empty */
  3583. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, Timeout, Tickstart) != HAL_OK)
  3584. {
  3585. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
  3586. return HAL_TIMEOUT;
  3587. }
  3588. return HAL_OK;
  3589. }
  3590. /**
  3591. * @brief Handle the end of the RXTX transaction.
  3592. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3593. * the configuration information for SPI module.
  3594. * @retval None
  3595. */
  3596. static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi)
  3597. {
  3598. uint32_t tickstart;
  3599. /* Init tickstart for timeout management */
  3600. tickstart = HAL_GetTick();
  3601. /* Disable ERR interrupt */
  3602. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
  3603. /* Check the end of the transaction */
  3604. if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
  3605. {
  3606. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
  3607. }
  3608. #if (USE_SPI_CRC != 0U)
  3609. /* Check if CRC error occurred */
  3610. if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  3611. {
  3612. hspi->State = HAL_SPI_STATE_READY;
  3613. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  3614. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  3615. /* Call user error callback */
  3616. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  3617. hspi->ErrorCallback(hspi);
  3618. #else
  3619. HAL_SPI_ErrorCallback(hspi);
  3620. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  3621. }
  3622. else
  3623. {
  3624. #endif /* USE_SPI_CRC */
  3625. if (hspi->ErrorCode == HAL_SPI_ERROR_NONE)
  3626. {
  3627. if (hspi->State == HAL_SPI_STATE_BUSY_RX)
  3628. {
  3629. hspi->State = HAL_SPI_STATE_READY;
  3630. /* Call user Rx complete callback */
  3631. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  3632. hspi->RxCpltCallback(hspi);
  3633. #else
  3634. HAL_SPI_RxCpltCallback(hspi);
  3635. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  3636. }
  3637. else
  3638. {
  3639. hspi->State = HAL_SPI_STATE_READY;
  3640. /* Call user TxRx complete callback */
  3641. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  3642. hspi->TxRxCpltCallback(hspi);
  3643. #else
  3644. HAL_SPI_TxRxCpltCallback(hspi);
  3645. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  3646. }
  3647. }
  3648. else
  3649. {
  3650. hspi->State = HAL_SPI_STATE_READY;
  3651. /* Call user error callback */
  3652. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  3653. hspi->ErrorCallback(hspi);
  3654. #else
  3655. HAL_SPI_ErrorCallback(hspi);
  3656. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  3657. }
  3658. #if (USE_SPI_CRC != 0U)
  3659. }
  3660. #endif /* USE_SPI_CRC */
  3661. }
  3662. /**
  3663. * @brief Handle the end of the RX transaction.
  3664. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3665. * the configuration information for SPI module.
  3666. * @retval None
  3667. */
  3668. static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi)
  3669. {
  3670. /* Disable RXNE and ERR interrupt */
  3671. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  3672. /* Check the end of the transaction */
  3673. if (SPI_EndRxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  3674. {
  3675. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
  3676. }
  3677. hspi->State = HAL_SPI_STATE_READY;
  3678. #if (USE_SPI_CRC != 0U)
  3679. /* Check if CRC error occurred */
  3680. if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  3681. {
  3682. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
  3683. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  3684. /* Call user error callback */
  3685. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  3686. hspi->ErrorCallback(hspi);
  3687. #else
  3688. HAL_SPI_ErrorCallback(hspi);
  3689. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  3690. }
  3691. else
  3692. {
  3693. #endif /* USE_SPI_CRC */
  3694. if (hspi->ErrorCode == HAL_SPI_ERROR_NONE)
  3695. {
  3696. /* Call user Rx complete callback */
  3697. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  3698. hspi->RxCpltCallback(hspi);
  3699. #else
  3700. HAL_SPI_RxCpltCallback(hspi);
  3701. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  3702. }
  3703. else
  3704. {
  3705. /* Call user error callback */
  3706. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  3707. hspi->ErrorCallback(hspi);
  3708. #else
  3709. HAL_SPI_ErrorCallback(hspi);
  3710. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  3711. }
  3712. #if (USE_SPI_CRC != 0U)
  3713. }
  3714. #endif /* USE_SPI_CRC */
  3715. }
  3716. /**
  3717. * @brief Handle the end of the TX transaction.
  3718. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3719. * the configuration information for SPI module.
  3720. * @retval None
  3721. */
  3722. static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi)
  3723. {
  3724. uint32_t tickstart;
  3725. /* Init tickstart for timeout management*/
  3726. tickstart = HAL_GetTick();
  3727. /* Disable TXE and ERR interrupt */
  3728. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
  3729. /* Check the end of the transaction */
  3730. if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
  3731. {
  3732. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
  3733. }
  3734. /* Clear overrun flag in 2 Lines communication mode because received is not read */
  3735. if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
  3736. {
  3737. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  3738. }
  3739. hspi->State = HAL_SPI_STATE_READY;
  3740. if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  3741. {
  3742. /* Call user error callback */
  3743. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  3744. hspi->ErrorCallback(hspi);
  3745. #else
  3746. HAL_SPI_ErrorCallback(hspi);
  3747. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  3748. }
  3749. else
  3750. {
  3751. /* Call user Rx complete callback */
  3752. #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
  3753. hspi->TxCpltCallback(hspi);
  3754. #else
  3755. HAL_SPI_TxCpltCallback(hspi);
  3756. #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
  3757. }
  3758. }
  3759. /**
  3760. * @brief Handle abort a Rx transaction.
  3761. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3762. * the configuration information for SPI module.
  3763. * @retval None
  3764. */
  3765. static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi)
  3766. {
  3767. __IO uint32_t count;
  3768. /* Disable SPI Peripheral */
  3769. __HAL_SPI_DISABLE(hspi);
  3770. count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
  3771. /* Disable RXNEIE interrupt */
  3772. CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_RXNEIE));
  3773. /* Check RXNEIE is disabled */
  3774. do
  3775. {
  3776. if (count == 0U)
  3777. {
  3778. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
  3779. break;
  3780. }
  3781. count--;
  3782. } while (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE));
  3783. /* Control the BSY flag */
  3784. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  3785. {
  3786. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  3787. }
  3788. /* Empty the FRLVL fifo */
  3789. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  3790. {
  3791. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  3792. }
  3793. hspi->State = HAL_SPI_STATE_ABORT;
  3794. }
  3795. /**
  3796. * @brief Handle abort a Tx or Rx/Tx transaction.
  3797. * @param hspi pointer to a SPI_HandleTypeDef structure that contains
  3798. * the configuration information for SPI module.
  3799. * @retval None
  3800. */
  3801. static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi)
  3802. {
  3803. __IO uint32_t count;
  3804. count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
  3805. /* Disable TXEIE interrupt */
  3806. CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXEIE));
  3807. /* Check TXEIE is disabled */
  3808. do
  3809. {
  3810. if (count == 0U)
  3811. {
  3812. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
  3813. break;
  3814. }
  3815. count--;
  3816. } while (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE));
  3817. if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  3818. {
  3819. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  3820. }
  3821. /* Disable SPI Peripheral */
  3822. __HAL_SPI_DISABLE(hspi);
  3823. /* Empty the FRLVL fifo */
  3824. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  3825. {
  3826. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  3827. }
  3828. /* Check case of Full-Duplex Mode and disable directly RXNEIE interrupt */
  3829. if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
  3830. {
  3831. /* Disable RXNEIE interrupt */
  3832. CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_RXNEIE));
  3833. /* Check RXNEIE is disabled */
  3834. do
  3835. {
  3836. if (count == 0U)
  3837. {
  3838. SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
  3839. break;
  3840. }
  3841. count--;
  3842. } while (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE));
  3843. /* Control the BSY flag */
  3844. if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  3845. {
  3846. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  3847. }
  3848. /* Empty the FRLVL fifo */
  3849. if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
  3850. {
  3851. hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
  3852. }
  3853. }
  3854. hspi->State = HAL_SPI_STATE_ABORT;
  3855. }
  3856. /**
  3857. * @}
  3858. */
  3859. #endif /* HAL_SPI_MODULE_ENABLED */
  3860. /**
  3861. * @}
  3862. */
  3863. /**
  3864. * @}
  3865. */
  3866. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/