You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_spi.c 118KB

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