stm32h7xx_hal_spi.c 117 KB

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