py32f0xx_hal_spi.c 118 KB

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