stm32f1xx_hal_spi.c 99 KB

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