stm32l0xx_hal_spi.c 124 KB

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