stm32f1xx_hal_spi.c 99 KB

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