stm32l4xx_hal_spi.c 117 KB

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