stm32f1xx_hal_spi.c 125 KB

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