stm32f1xx_hal_spi.c 125 KB

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