stm32g0xx_hal_spi.c 142 KB

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