stm32l4xx_hal_spi.c 139 KB

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