stm32f4xx_hal_spi.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_spi.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-2015
  7. * @brief SPI HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the Serial Peripheral Interface (SPI) peripheral:
  11. * + Initialization and de-initialization functions
  12. * + IO operation functions
  13. * + Peripheral Control functions
  14. * + Peripheral State functions
  15. @verbatim
  16. ==============================================================================
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..]
  20. The SPI HAL driver can be used as follows:
  21. (#) Declare a SPI_HandleTypeDef handle structure, for example:
  22. SPI_HandleTypeDef hspi;
  23. (#)Initialize the SPI low level resources by implementing the HAL_SPI_MspInit ()API:
  24. (##) Enable the SPIx interface clock
  25. (##) SPI pins configuration
  26. (+++) Enable the clock for the SPI GPIOs
  27. (+++) Configure these SPI pins as alternate function push-pull
  28. (##) NVIC configuration if you need to use interrupt process
  29. (+++) Configure the SPIx interrupt priority
  30. (+++) Enable the NVIC SPI IRQ handle
  31. (##) DMA Configuration if you need to use DMA process
  32. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive stream
  33. (+++) Enable the DMAx interface clock using
  34. (+++) Configure the DMA handle parameters
  35. (+++) Configure the DMA Tx or Rx Stream
  36. (+++) Associate the initialized hdma_tx handle to the hspi DMA Tx or Rx handle
  37. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx Stream
  38. (#) Program the Mode, Direction , Data size, Baudrate Prescaler, NSS
  39. management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure.
  40. (#) Initialize the SPI registers by calling the HAL_SPI_Init() API:
  41. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  42. by calling the customized HAL_SPI_MspInit() API.
  43. [..]
  44. Circular mode restriction:
  45. (#) The DMA circular mode cannot be used when the SPI is configured in these modes:
  46. (##) Master 2Lines RxOnly
  47. (##) Master 1Line Rx
  48. (#) The CRC feature is not managed when the DMA circular mode is enabled
  49. (#) When the SPI DMA Pause/Stop features are used, we must use the following APIs
  50. the HAL_SPI_DMAPause()/ HAL_SPI_DMAStop() only under the SPI callbacks
  51. @endverbatim
  52. ******************************************************************************
  53. * @attention
  54. *
  55. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  56. *
  57. * Redistribution and use in source and binary forms, with or without modification,
  58. * are permitted provided that the following conditions are met:
  59. * 1. Redistributions of source code must retain the above copyright notice,
  60. * this list of conditions and the following disclaimer.
  61. * 2. Redistributions in binary form must reproduce the above copyright notice,
  62. * this list of conditions and the following disclaimer in the documentation
  63. * and/or other materials provided with the distribution.
  64. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  65. * may be used to endorse or promote products derived from this software
  66. * without specific prior written permission.
  67. *
  68. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  69. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  70. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  71. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  72. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  73. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  75. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  76. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  77. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  78. *
  79. ******************************************************************************
  80. */
  81. /* Includes ------------------------------------------------------------------*/
  82. #include "stm32f4xx_hal.h"
  83. /** @addtogroup STM32F4xx_HAL_Driver
  84. * @{
  85. */
  86. /** @defgroup SPI SPI
  87. * @brief SPI HAL module driver
  88. * @{
  89. */
  90. #ifdef HAL_SPI_MODULE_ENABLED
  91. /* Private typedef -----------------------------------------------------------*/
  92. /* Private define ------------------------------------------------------------*/
  93. #define SPI_TIMEOUT_VALUE 10
  94. /* Private macro -------------------------------------------------------------*/
  95. /* Private variables ---------------------------------------------------------*/
  96. /* Private function prototypes -----------------------------------------------*/
  97. /** @addtogroup SPI_Private_Functions
  98. * @{
  99. */
  100. static void SPI_TxCloseIRQHandler(SPI_HandleTypeDef *hspi);
  101. static void SPI_TxISR(SPI_HandleTypeDef *hspi);
  102. static void SPI_RxCloseIRQHandler(SPI_HandleTypeDef *hspi);
  103. static void SPI_2LinesRxISR(SPI_HandleTypeDef *hspi);
  104. static void SPI_RxISR(SPI_HandleTypeDef *hspi);
  105. static void SPI_DMAEndTransmitReceive(SPI_HandleTypeDef *hspi);
  106. static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  107. static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  108. static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma);
  109. static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma);
  110. static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma);
  111. static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma);
  112. static void SPI_DMAError(DMA_HandleTypeDef *hdma);
  113. static HAL_StatusTypeDef SPI_WaitOnFlagUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  114. /**
  115. * @}
  116. */
  117. /* Exported functions --------------------------------------------------------*/
  118. /** @defgroup SPI_Exported_Functions SPI Exported Functions
  119. * @{
  120. */
  121. /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions
  122. * @brief Initialization and Configuration functions
  123. *
  124. @verbatim
  125. ===============================================================================
  126. ##### Initialization and de-initialization functions #####
  127. ===============================================================================
  128. [..] This subsection provides a set of functions allowing to initialize and
  129. de-initialize the SPIx peripheral:
  130. (+) User must implement HAL_SPI_MspInit() function in which he configures
  131. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  132. (+) Call the function HAL_SPI_Init() to configure the selected device with
  133. the selected configuration:
  134. (++) Mode
  135. (++) Direction
  136. (++) Data Size
  137. (++) Clock Polarity and Phase
  138. (++) NSS Management
  139. (++) BaudRate Prescaler
  140. (++) FirstBit
  141. (++) TIMode
  142. (++) CRC Calculation
  143. (++) CRC Polynomial if CRC enabled
  144. (+) Call the function HAL_SPI_DeInit() to restore the default configuration
  145. of the selected SPIx peripheral.
  146. @endverbatim
  147. * @{
  148. */
  149. /**
  150. * @brief Initializes the SPI according to the specified parameters
  151. * in the SPI_InitTypeDef and create the associated handle.
  152. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  153. * the configuration information for SPI module.
  154. * @retval HAL status
  155. */
  156. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
  157. {
  158. /* Check the SPI handle allocation */
  159. if(hspi == NULL)
  160. {
  161. return HAL_ERROR;
  162. }
  163. /* Check the parameters */
  164. assert_param(IS_SPI_MODE(hspi->Init.Mode));
  165. assert_param(IS_SPI_DIRECTION_MODE(hspi->Init.Direction));
  166. assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
  167. assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
  168. assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
  169. assert_param(IS_SPI_NSS(hspi->Init.NSS));
  170. assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
  171. assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
  172. assert_param(IS_SPI_TIMODE(hspi->Init.TIMode));
  173. assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
  174. assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
  175. if(hspi->State == HAL_SPI_STATE_RESET)
  176. {
  177. /* Allocate lock resource and initialize it */
  178. hspi->Lock = HAL_UNLOCKED;
  179. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  180. HAL_SPI_MspInit(hspi);
  181. }
  182. hspi->State = HAL_SPI_STATE_BUSY;
  183. /* Disable the selected SPI peripheral */
  184. __HAL_SPI_DISABLE(hspi);
  185. /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/
  186. /* Configure : SPI Mode, Communication Mode, Data size, Clock polarity and phase, NSS management,
  187. Communication speed, First bit and CRC calculation state */
  188. hspi->Instance->CR1 = (hspi->Init.Mode | hspi->Init.Direction | hspi->Init.DataSize |
  189. hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) |
  190. hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit | hspi->Init.CRCCalculation);
  191. /* Configure : NSS management */
  192. hspi->Instance->CR2 = (((hspi->Init.NSS >> 16) & SPI_CR2_SSOE) | hspi->Init.TIMode);
  193. /*---------------------------- SPIx CRCPOLY Configuration ------------------*/
  194. /* Configure : CRC Polynomial */
  195. hspi->Instance->CRCPR = hspi->Init.CRCPolynomial;
  196. /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */
  197. hspi->Instance->I2SCFGR &= (uint32_t)(~SPI_I2SCFGR_I2SMOD);
  198. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  199. hspi->State = HAL_SPI_STATE_READY;
  200. return HAL_OK;
  201. }
  202. /**
  203. * @brief DeInitializes the SPI peripheral
  204. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  205. * the configuration information for SPI module.
  206. * @retval HAL status
  207. */
  208. HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
  209. {
  210. /* Check the SPI handle allocation */
  211. if(hspi == NULL)
  212. {
  213. return HAL_ERROR;
  214. }
  215. /* Disable the SPI Peripheral Clock */
  216. __HAL_SPI_DISABLE(hspi);
  217. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  218. HAL_SPI_MspDeInit(hspi);
  219. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  220. hspi->State = HAL_SPI_STATE_RESET;
  221. /* Release Lock */
  222. __HAL_UNLOCK(hspi);
  223. return HAL_OK;
  224. }
  225. /**
  226. * @brief SPI MSP Init
  227. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  228. * the configuration information for SPI module.
  229. * @retval None
  230. */
  231. __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
  232. {
  233. /* Prevent unused argument(s) compilation warning */
  234. UNUSED(hspi);
  235. /* NOTE : This function Should not be modified, when the callback is needed,
  236. the HAL_SPI_MspInit could be implemented in the user file
  237. */
  238. }
  239. /**
  240. * @brief SPI MSP DeInit
  241. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  242. * the configuration information for SPI module.
  243. * @retval None
  244. */
  245. __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
  246. {
  247. /* Prevent unused argument(s) compilation warning */
  248. UNUSED(hspi);
  249. /* NOTE : This function Should not be modified, when the callback is needed,
  250. the HAL_SPI_MspDeInit could be implemented in the user file
  251. */
  252. }
  253. /**
  254. * @}
  255. */
  256. /** @defgroup SPI_Exported_Functions_Group2 IO operation functions
  257. * @brief Data transfers functions
  258. *
  259. @verbatim
  260. ==============================================================================
  261. ##### IO operation functions #####
  262. ===============================================================================
  263. This subsection provides a set of functions allowing to manage the SPI
  264. data transfers.
  265. [..] The SPI supports master and slave mode :
  266. (#) There are two modes of transfer:
  267. (++) Blocking mode: The communication is performed in polling mode.
  268. The HAL status of all data processing is returned by the same function
  269. after finishing transfer.
  270. (++) No-Blocking mode: The communication is performed using Interrupts
  271. or DMA, These APIs return the HAL status.
  272. The end of the data processing will be indicated through the
  273. dedicated SPI IRQ when using Interrupt mode or the DMA IRQ when
  274. using DMA mode.
  275. The HAL_SPI_TxCpltCallback(), HAL_SPI_RxCpltCallback() and HAL_SPI_TxRxCpltCallback() user callbacks
  276. will be executed respectively at the end of the transmit or Receive process
  277. The HAL_SPI_ErrorCallback()user callback will be executed when a communication error is detected
  278. (#) APIs provided for these 2 transfer modes (Blocking mode or Non blocking mode using either Interrupt or DMA)
  279. exist for 1Line (simplex) and 2Lines (full duplex) modes.
  280. @endverbatim
  281. * @{
  282. */
  283. /**
  284. * @brief Transmit an amount of data in blocking mode
  285. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  286. * the configuration information for SPI module.
  287. * @param pData: pointer to data buffer
  288. * @param Size: amount of data to be sent
  289. * @param Timeout: Timeout duration
  290. * @retval HAL status
  291. */
  292. HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  293. {
  294. if(hspi->State == HAL_SPI_STATE_READY)
  295. {
  296. if((pData == NULL ) || (Size == 0))
  297. {
  298. return HAL_ERROR;
  299. }
  300. /* Check the parameters */
  301. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  302. /* Process Locked */
  303. __HAL_LOCK(hspi);
  304. /* Configure communication */
  305. hspi->State = HAL_SPI_STATE_BUSY_TX;
  306. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  307. hspi->pTxBuffPtr = pData;
  308. hspi->TxXferSize = Size;
  309. hspi->TxXferCount = Size;
  310. /*Init field not used in handle to zero */
  311. hspi->TxISR = 0;
  312. hspi->RxISR = 0;
  313. hspi->RxXferSize = 0;
  314. hspi->RxXferCount = 0;
  315. /* Reset CRC Calculation */
  316. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  317. {
  318. SPI_RESET_CRC(hspi);
  319. }
  320. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  321. {
  322. /* Configure communication direction : 1Line */
  323. SPI_1LINE_TX(hspi);
  324. }
  325. /* Check if the SPI is already enabled */
  326. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  327. {
  328. /* Enable SPI peripheral */
  329. __HAL_SPI_ENABLE(hspi);
  330. }
  331. /* Transmit data in 8 Bit mode */
  332. if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
  333. {
  334. if((hspi->Init.Mode == SPI_MODE_SLAVE)|| (hspi->TxXferCount == 0x01))
  335. {
  336. hspi->Instance->DR = (*hspi->pTxBuffPtr++);
  337. hspi->TxXferCount--;
  338. }
  339. while(hspi->TxXferCount > 0)
  340. {
  341. /* Wait until TXE flag is set to send data */
  342. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, Timeout) != HAL_OK)
  343. {
  344. return HAL_TIMEOUT;
  345. }
  346. hspi->Instance->DR = (*hspi->pTxBuffPtr++);
  347. hspi->TxXferCount--;
  348. }
  349. /* Enable CRC Transmission */
  350. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  351. {
  352. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  353. }
  354. }
  355. /* Transmit data in 16 Bit mode */
  356. else
  357. {
  358. if((hspi->Init.Mode == SPI_MODE_SLAVE) || (hspi->TxXferCount == 0x01))
  359. {
  360. hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
  361. hspi->pTxBuffPtr+=2;
  362. hspi->TxXferCount--;
  363. }
  364. while(hspi->TxXferCount > 0)
  365. {
  366. /* Wait until TXE flag is set to send data */
  367. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, Timeout) != HAL_OK)
  368. {
  369. return HAL_TIMEOUT;
  370. }
  371. hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
  372. hspi->pTxBuffPtr+=2;
  373. hspi->TxXferCount--;
  374. }
  375. /* Enable CRC Transmission */
  376. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  377. {
  378. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  379. }
  380. }
  381. /* Wait until TXE flag is set to send data */
  382. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, Timeout) != HAL_OK)
  383. {
  384. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  385. return HAL_TIMEOUT;
  386. }
  387. /* Wait until Busy flag is reset before disabling SPI */
  388. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_BSY, SET, Timeout) != HAL_OK)
  389. {
  390. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  391. return HAL_TIMEOUT;
  392. }
  393. /* Clear OVERRUN flag in 2 Lines communication mode because received is not read */
  394. if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
  395. {
  396. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  397. }
  398. hspi->State = HAL_SPI_STATE_READY;
  399. /* Process Unlocked */
  400. __HAL_UNLOCK(hspi);
  401. return HAL_OK;
  402. }
  403. else
  404. {
  405. return HAL_BUSY;
  406. }
  407. }
  408. /**
  409. * @brief Receive an amount of data in blocking mode
  410. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  411. * the configuration information for SPI module.
  412. * @param pData: pointer to data buffer
  413. * @param Size: amount of data to be sent
  414. * @param Timeout: Timeout duration
  415. * @retval HAL status
  416. */
  417. HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  418. {
  419. __IO uint16_t tmpreg;
  420. uint32_t tmp = 0;
  421. if(hspi->State == HAL_SPI_STATE_READY)
  422. {
  423. if((pData == NULL ) || (Size == 0))
  424. {
  425. return HAL_ERROR;
  426. }
  427. /* Process Locked */
  428. __HAL_LOCK(hspi);
  429. /* Configure communication */
  430. hspi->State = HAL_SPI_STATE_BUSY_RX;
  431. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  432. hspi->pRxBuffPtr = pData;
  433. hspi->RxXferSize = Size;
  434. hspi->RxXferCount = Size;
  435. /*Init field not used in handle to zero */
  436. hspi->RxISR = 0;
  437. hspi->TxISR = 0;
  438. hspi->TxXferSize = 0;
  439. hspi->TxXferCount = 0;
  440. /* Configure communication direction : 1Line */
  441. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  442. {
  443. SPI_1LINE_RX(hspi);
  444. }
  445. /* Reset CRC Calculation */
  446. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  447. {
  448. SPI_RESET_CRC(hspi);
  449. }
  450. if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
  451. {
  452. /* Process Unlocked */
  453. __HAL_UNLOCK(hspi);
  454. /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
  455. return HAL_SPI_TransmitReceive(hspi, pData, pData, Size, Timeout);
  456. }
  457. /* Check if the SPI is already enabled */
  458. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  459. {
  460. /* Enable SPI peripheral */
  461. __HAL_SPI_ENABLE(hspi);
  462. }
  463. /* Receive data in 8 Bit mode */
  464. if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
  465. {
  466. while(hspi->RxXferCount > 1)
  467. {
  468. /* Wait until RXNE flag is set */
  469. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  470. {
  471. return HAL_TIMEOUT;
  472. }
  473. (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
  474. hspi->RxXferCount--;
  475. }
  476. /* Enable CRC Transmission */
  477. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  478. {
  479. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  480. }
  481. }
  482. /* Receive data in 16 Bit mode */
  483. else
  484. {
  485. while(hspi->RxXferCount > 1)
  486. {
  487. /* Wait until RXNE flag is set to read data */
  488. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  489. {
  490. return HAL_TIMEOUT;
  491. }
  492. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  493. hspi->pRxBuffPtr+=2;
  494. hspi->RxXferCount--;
  495. }
  496. /* Enable CRC Transmission */
  497. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  498. {
  499. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  500. }
  501. }
  502. /* Wait until RXNE flag is set */
  503. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  504. {
  505. return HAL_TIMEOUT;
  506. }
  507. /* Receive last data in 8 Bit mode */
  508. if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
  509. {
  510. (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
  511. }
  512. /* Receive last data in 16 Bit mode */
  513. else
  514. {
  515. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  516. hspi->pRxBuffPtr+=2;
  517. }
  518. hspi->RxXferCount--;
  519. /* Wait until RXNE flag is set: CRC Received */
  520. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  521. {
  522. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  523. {
  524. hspi->ErrorCode |= HAL_SPI_ERROR_CRC;
  525. return HAL_TIMEOUT;
  526. }
  527. /* Read CRC to Flush RXNE flag */
  528. tmpreg = hspi->Instance->DR;
  529. UNUSED(tmpreg);
  530. }
  531. if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  532. {
  533. /* Disable SPI peripheral */
  534. __HAL_SPI_DISABLE(hspi);
  535. }
  536. hspi->State = HAL_SPI_STATE_READY;
  537. tmp = __HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR);
  538. /* Check if CRC error occurred */
  539. if((hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) && (tmp != RESET))
  540. {
  541. hspi->ErrorCode |= HAL_SPI_ERROR_CRC;
  542. /* Reset CRC Calculation */
  543. SPI_RESET_CRC(hspi);
  544. /* Process Unlocked */
  545. __HAL_UNLOCK(hspi);
  546. return HAL_ERROR;
  547. }
  548. /* Process Unlocked */
  549. __HAL_UNLOCK(hspi);
  550. return HAL_OK;
  551. }
  552. else
  553. {
  554. return HAL_BUSY;
  555. }
  556. }
  557. /**
  558. * @brief Transmit and Receive an amount of data in blocking mode
  559. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  560. * the configuration information for SPI module.
  561. * @param pTxData: pointer to transmission data buffer
  562. * @param pRxData: pointer to reception data buffer to be
  563. * @param Size: amount of data to be sent
  564. * @param Timeout: Timeout duration
  565. * @retval HAL status
  566. */
  567. HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  568. {
  569. __IO uint16_t tmpreg;
  570. uint32_t tmpstate = 0, tmp = 0;
  571. tmpstate = hspi->State;
  572. if((tmpstate == HAL_SPI_STATE_READY) || (tmpstate == HAL_SPI_STATE_BUSY_RX))
  573. {
  574. if((pTxData == NULL ) || (pRxData == NULL ) || (Size == 0))
  575. {
  576. return HAL_ERROR;
  577. }
  578. /* Check the parameters */
  579. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  580. /* Process Locked */
  581. __HAL_LOCK(hspi);
  582. /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
  583. if(hspi->State == HAL_SPI_STATE_READY)
  584. {
  585. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  586. }
  587. /* Configure communication */
  588. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  589. hspi->pRxBuffPtr = pRxData;
  590. hspi->RxXferSize = Size;
  591. hspi->RxXferCount = Size;
  592. hspi->pTxBuffPtr = pTxData;
  593. hspi->TxXferSize = Size;
  594. hspi->TxXferCount = Size;
  595. /*Init field not used in handle to zero */
  596. hspi->RxISR = 0;
  597. hspi->TxISR = 0;
  598. /* Reset CRC Calculation */
  599. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  600. {
  601. SPI_RESET_CRC(hspi);
  602. }
  603. /* Check if the SPI is already enabled */
  604. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  605. {
  606. /* Enable SPI peripheral */
  607. __HAL_SPI_ENABLE(hspi);
  608. }
  609. /* Transmit and Receive data in 16 Bit mode */
  610. if(hspi->Init.DataSize == SPI_DATASIZE_16BIT)
  611. {
  612. if((hspi->Init.Mode == SPI_MODE_SLAVE) || ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->TxXferCount == 0x01)))
  613. {
  614. hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
  615. hspi->pTxBuffPtr+=2;
  616. hspi->TxXferCount--;
  617. }
  618. if(hspi->TxXferCount == 0)
  619. {
  620. /* Enable CRC Transmission */
  621. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  622. {
  623. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  624. }
  625. /* Wait until RXNE flag is set */
  626. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  627. {
  628. return HAL_TIMEOUT;
  629. }
  630. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  631. hspi->pRxBuffPtr+=2;
  632. hspi->RxXferCount--;
  633. }
  634. else
  635. {
  636. while(hspi->TxXferCount > 0)
  637. {
  638. /* Wait until TXE flag is set to send data */
  639. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, Timeout) != HAL_OK)
  640. {
  641. return HAL_TIMEOUT;
  642. }
  643. hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
  644. hspi->pTxBuffPtr+=2;
  645. hspi->TxXferCount--;
  646. /* Enable CRC Transmission */
  647. if((hspi->TxXferCount == 0) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  648. {
  649. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  650. }
  651. /* Wait until RXNE flag is set */
  652. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  653. {
  654. return HAL_TIMEOUT;
  655. }
  656. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  657. hspi->pRxBuffPtr+=2;
  658. hspi->RxXferCount--;
  659. }
  660. /* Receive the last byte */
  661. if(hspi->Init.Mode == SPI_MODE_SLAVE)
  662. {
  663. /* Wait until RXNE flag is set */
  664. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  665. {
  666. return HAL_TIMEOUT;
  667. }
  668. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  669. hspi->pRxBuffPtr+=2;
  670. hspi->RxXferCount--;
  671. }
  672. }
  673. }
  674. /* Transmit and Receive data in 8 Bit mode */
  675. else
  676. {
  677. if((hspi->Init.Mode == SPI_MODE_SLAVE) || ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->TxXferCount == 0x01)))
  678. {
  679. hspi->Instance->DR = (*hspi->pTxBuffPtr++);
  680. hspi->TxXferCount--;
  681. }
  682. if(hspi->TxXferCount == 0)
  683. {
  684. /* Enable CRC Transmission */
  685. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  686. {
  687. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  688. }
  689. /* Wait until RXNE flag is set */
  690. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  691. {
  692. return HAL_TIMEOUT;
  693. }
  694. (*hspi->pRxBuffPtr) = hspi->Instance->DR;
  695. hspi->RxXferCount--;
  696. }
  697. else
  698. {
  699. while(hspi->TxXferCount > 0)
  700. {
  701. /* Wait until TXE flag is set to send data */
  702. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, Timeout) != HAL_OK)
  703. {
  704. return HAL_TIMEOUT;
  705. }
  706. hspi->Instance->DR = (*hspi->pTxBuffPtr++);
  707. hspi->TxXferCount--;
  708. /* Enable CRC Transmission */
  709. if((hspi->TxXferCount == 0) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  710. {
  711. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  712. }
  713. /* Wait until RXNE flag is set */
  714. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  715. {
  716. return HAL_TIMEOUT;
  717. }
  718. (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
  719. hspi->RxXferCount--;
  720. }
  721. if(hspi->Init.Mode == SPI_MODE_SLAVE)
  722. {
  723. /* Wait until RXNE flag is set */
  724. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  725. {
  726. return HAL_TIMEOUT;
  727. }
  728. (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
  729. hspi->RxXferCount--;
  730. }
  731. }
  732. }
  733. /* Read CRC from DR to close CRC calculation process */
  734. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  735. {
  736. /* Wait until RXNE flag is set */
  737. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  738. {
  739. hspi->ErrorCode |= HAL_SPI_ERROR_CRC;
  740. return HAL_TIMEOUT;
  741. }
  742. /* Read CRC */
  743. tmpreg = hspi->Instance->DR;
  744. UNUSED(tmpreg);
  745. }
  746. /* Wait until Busy flag is reset before disabling SPI */
  747. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_BSY, SET, Timeout) != HAL_OK)
  748. {
  749. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  750. return HAL_TIMEOUT;
  751. }
  752. hspi->State = HAL_SPI_STATE_READY;
  753. tmp = __HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR);
  754. /* Check if CRC error occurred */
  755. if((hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE) && (tmp != RESET))
  756. {
  757. hspi->ErrorCode |= HAL_SPI_ERROR_CRC;
  758. /* Reset CRC Calculation */
  759. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  760. {
  761. SPI_RESET_CRC(hspi);
  762. }
  763. /* Process Unlocked */
  764. __HAL_UNLOCK(hspi);
  765. return HAL_ERROR;
  766. }
  767. /* Process Unlocked */
  768. __HAL_UNLOCK(hspi);
  769. return HAL_OK;
  770. }
  771. else
  772. {
  773. return HAL_BUSY;
  774. }
  775. }
  776. /**
  777. * @brief Transmit an amount of data in no-blocking mode with Interrupt
  778. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  779. * the configuration information for SPI module.
  780. * @param pData: pointer to data buffer
  781. * @param Size: amount of data to be sent
  782. * @retval HAL status
  783. */
  784. HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  785. {
  786. if(hspi->State == HAL_SPI_STATE_READY)
  787. {
  788. if((pData == NULL) || (Size == 0))
  789. {
  790. return HAL_ERROR;
  791. }
  792. /* Check the parameters */
  793. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  794. /* Process Locked */
  795. __HAL_LOCK(hspi);
  796. /* Configure communication */
  797. hspi->State = HAL_SPI_STATE_BUSY_TX;
  798. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  799. hspi->TxISR = &SPI_TxISR;
  800. hspi->pTxBuffPtr = pData;
  801. hspi->TxXferSize = Size;
  802. hspi->TxXferCount = Size;
  803. /*Init field not used in handle to zero */
  804. hspi->RxISR = 0;
  805. hspi->RxXferSize = 0;
  806. hspi->RxXferCount = 0;
  807. /* Configure communication direction : 1Line */
  808. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  809. {
  810. SPI_1LINE_TX(hspi);
  811. }
  812. /* Reset CRC Calculation */
  813. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  814. {
  815. SPI_RESET_CRC(hspi);
  816. }
  817. if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
  818. {
  819. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE));
  820. }else
  821. {
  822. /* Enable TXE and ERR interrupt */
  823. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
  824. }
  825. /* Process Unlocked */
  826. __HAL_UNLOCK(hspi);
  827. /* Check if the SPI is already enabled */
  828. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  829. {
  830. /* Enable SPI peripheral */
  831. __HAL_SPI_ENABLE(hspi);
  832. }
  833. return HAL_OK;
  834. }
  835. else
  836. {
  837. return HAL_BUSY;
  838. }
  839. }
  840. /**
  841. * @brief Receive an amount of data in no-blocking mode with Interrupt
  842. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  843. * the configuration information for SPI module.
  844. * @param pData: pointer to data buffer
  845. * @param Size: amount of data to be sent
  846. * @retval HAL status
  847. */
  848. HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  849. {
  850. if(hspi->State == HAL_SPI_STATE_READY)
  851. {
  852. if((pData == NULL) || (Size == 0))
  853. {
  854. return HAL_ERROR;
  855. }
  856. /* Process Locked */
  857. __HAL_LOCK(hspi);
  858. /* Configure communication */
  859. hspi->State = HAL_SPI_STATE_BUSY_RX;
  860. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  861. hspi->RxISR = &SPI_RxISR;
  862. hspi->pRxBuffPtr = pData;
  863. hspi->RxXferSize = Size;
  864. hspi->RxXferCount = Size ;
  865. /*Init field not used in handle to zero */
  866. hspi->TxISR = 0;
  867. hspi->TxXferSize = 0;
  868. hspi->TxXferCount = 0;
  869. /* Configure communication direction : 1Line */
  870. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  871. {
  872. SPI_1LINE_RX(hspi);
  873. }
  874. else if((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
  875. {
  876. /* Process Unlocked */
  877. __HAL_UNLOCK(hspi);
  878. /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
  879. return HAL_SPI_TransmitReceive_IT(hspi, pData, pData, Size);
  880. }
  881. /* Reset CRC Calculation */
  882. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  883. {
  884. SPI_RESET_CRC(hspi);
  885. }
  886. /* Enable TXE and ERR interrupt */
  887. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  888. /* Process Unlocked */
  889. __HAL_UNLOCK(hspi);
  890. /* Note : The SPI must be enabled after unlocking current process
  891. to avoid the risk of SPI interrupt handle execution before current
  892. process unlock */
  893. /* Check if the SPI is already enabled */
  894. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  895. {
  896. /* Enable SPI peripheral */
  897. __HAL_SPI_ENABLE(hspi);
  898. }
  899. return HAL_OK;
  900. }
  901. else
  902. {
  903. return HAL_BUSY;
  904. }
  905. }
  906. /**
  907. * @brief Transmit and Receive an amount of data in no-blocking mode with Interrupt
  908. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  909. * the configuration information for SPI module.
  910. * @param pTxData: pointer to transmission data buffer
  911. * @param pRxData: pointer to reception data buffer to be
  912. * @param Size: amount of data to be sent
  913. * @retval HAL status
  914. */
  915. HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  916. {
  917. uint32_t tmpstate = 0;
  918. tmpstate = hspi->State;
  919. if((tmpstate == HAL_SPI_STATE_READY) || \
  920. ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmpstate == HAL_SPI_STATE_BUSY_RX)))
  921. {
  922. if((pTxData == NULL ) || (pRxData == NULL ) || (Size == 0))
  923. {
  924. return HAL_ERROR;
  925. }
  926. /* Check the parameters */
  927. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  928. /* Process locked */
  929. __HAL_LOCK(hspi);
  930. /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
  931. if(hspi->State != HAL_SPI_STATE_BUSY_RX)
  932. {
  933. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  934. }
  935. /* Configure communication */
  936. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  937. hspi->TxISR = &SPI_TxISR;
  938. hspi->pTxBuffPtr = pTxData;
  939. hspi->TxXferSize = Size;
  940. hspi->TxXferCount = Size;
  941. hspi->RxISR = &SPI_2LinesRxISR;
  942. hspi->pRxBuffPtr = pRxData;
  943. hspi->RxXferSize = Size;
  944. hspi->RxXferCount = Size;
  945. /* Reset CRC Calculation */
  946. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  947. {
  948. SPI_RESET_CRC(hspi);
  949. }
  950. /* Enable TXE, RXNE and ERR interrupt */
  951. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  952. /* Process Unlocked */
  953. __HAL_UNLOCK(hspi);
  954. /* Check if the SPI is already enabled */
  955. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  956. {
  957. /* Enable SPI peripheral */
  958. __HAL_SPI_ENABLE(hspi);
  959. }
  960. return HAL_OK;
  961. }
  962. else
  963. {
  964. return HAL_BUSY;
  965. }
  966. }
  967. /**
  968. * @brief Transmit an amount of data in no-blocking mode with DMA
  969. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  970. * the configuration information for SPI module.
  971. * @param pData: pointer to data buffer
  972. * @param Size: amount of data to be sent
  973. * @retval HAL status
  974. */
  975. HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  976. {
  977. if(hspi->State == HAL_SPI_STATE_READY)
  978. {
  979. if((pData == NULL) || (Size == 0))
  980. {
  981. return HAL_ERROR;
  982. }
  983. /* Check the parameters */
  984. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  985. /* Process Locked */
  986. __HAL_LOCK(hspi);
  987. /* Configure communication */
  988. hspi->State = HAL_SPI_STATE_BUSY_TX;
  989. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  990. hspi->pTxBuffPtr = pData;
  991. hspi->TxXferSize = Size;
  992. hspi->TxXferCount = Size;
  993. /*Init field not used in handle to zero */
  994. hspi->TxISR = 0;
  995. hspi->RxISR = 0;
  996. hspi->RxXferSize = 0;
  997. hspi->RxXferCount = 0;
  998. /* Configure communication direction : 1Line */
  999. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  1000. {
  1001. SPI_1LINE_TX(hspi);
  1002. }
  1003. /* Reset CRC Calculation */
  1004. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1005. {
  1006. SPI_RESET_CRC(hspi);
  1007. }
  1008. /* Set the SPI TxDMA Half transfer complete callback */
  1009. hspi->hdmatx->XferHalfCpltCallback = SPI_DMAHalfTransmitCplt;
  1010. /* Set the SPI TxDMA transfer complete callback */
  1011. hspi->hdmatx->XferCpltCallback = SPI_DMATransmitCplt;
  1012. /* Set the DMA error callback */
  1013. hspi->hdmatx->XferErrorCallback = SPI_DMAError;
  1014. /* Enable the Tx DMA Stream */
  1015. HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount);
  1016. /* Process Unlocked */
  1017. __HAL_UNLOCK(hspi);
  1018. /* Check if the SPI is already enabled */
  1019. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  1020. {
  1021. /* Enable SPI peripheral */
  1022. __HAL_SPI_ENABLE(hspi);
  1023. }
  1024. /* Enable Tx DMA Request */
  1025. hspi->Instance->CR2 |= SPI_CR2_TXDMAEN;
  1026. return HAL_OK;
  1027. }
  1028. else
  1029. {
  1030. return HAL_BUSY;
  1031. }
  1032. }
  1033. /**
  1034. * @brief Receive an amount of data in no-blocking mode with DMA
  1035. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1036. * the configuration information for SPI module.
  1037. * @param pData: pointer to data buffer
  1038. * @note When the CRC feature is enabled the pData Length must be Size + 1.
  1039. * @param Size: amount of data to be sent
  1040. * @retval HAL status
  1041. */
  1042. HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  1043. {
  1044. if(hspi->State == HAL_SPI_STATE_READY)
  1045. {
  1046. if((pData == NULL) || (Size == 0))
  1047. {
  1048. return HAL_ERROR;
  1049. }
  1050. /* Process Locked */
  1051. __HAL_LOCK(hspi);
  1052. /* Configure communication */
  1053. hspi->State = HAL_SPI_STATE_BUSY_RX;
  1054. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1055. hspi->pRxBuffPtr = pData;
  1056. hspi->RxXferSize = Size;
  1057. hspi->RxXferCount = Size;
  1058. /*Init field not used in handle to zero */
  1059. hspi->RxISR = 0;
  1060. hspi->TxISR = 0;
  1061. hspi->TxXferSize = 0;
  1062. hspi->TxXferCount = 0;
  1063. /* Configure communication direction : 1Line */
  1064. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  1065. {
  1066. SPI_1LINE_RX(hspi);
  1067. }
  1068. else if((hspi->Init.Direction == SPI_DIRECTION_2LINES)&&(hspi->Init.Mode == SPI_MODE_MASTER))
  1069. {
  1070. /* Process Unlocked */
  1071. __HAL_UNLOCK(hspi);
  1072. /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
  1073. return HAL_SPI_TransmitReceive_DMA(hspi, pData, pData, Size);
  1074. }
  1075. /* Reset CRC Calculation */
  1076. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1077. {
  1078. SPI_RESET_CRC(hspi);
  1079. }
  1080. /* Set the SPI RxDMA Half transfer complete callback */
  1081. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
  1082. /* Set the SPI Rx DMA transfer complete callback */
  1083. hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
  1084. /* Set the DMA error callback */
  1085. hspi->hdmarx->XferErrorCallback = SPI_DMAError;
  1086. /* Enable the Rx DMA Stream */
  1087. HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount);
  1088. /* Process Unlocked */
  1089. __HAL_UNLOCK(hspi);
  1090. /* Check if the SPI is already enabled */
  1091. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  1092. {
  1093. /* Enable SPI peripheral */
  1094. __HAL_SPI_ENABLE(hspi);
  1095. }
  1096. /* Enable Rx DMA Request */
  1097. hspi->Instance->CR2 |= SPI_CR2_RXDMAEN;
  1098. return HAL_OK;
  1099. }
  1100. else
  1101. {
  1102. return HAL_BUSY;
  1103. }
  1104. }
  1105. /**
  1106. * @brief Transmit and Receive an amount of data in no-blocking mode with DMA
  1107. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1108. * the configuration information for SPI module.
  1109. * @param pTxData: pointer to transmission data buffer
  1110. * @param pRxData: pointer to reception data buffer
  1111. * @note When the CRC feature is enabled the pRxData Length must be Size + 1
  1112. * @param Size: amount of data to be sent
  1113. * @retval HAL status
  1114. */
  1115. HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  1116. {
  1117. uint32_t tmpstate = 0;
  1118. tmpstate = hspi->State;
  1119. if((tmpstate == HAL_SPI_STATE_READY) || ((hspi->Init.Mode == SPI_MODE_MASTER) && \
  1120. (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmpstate == HAL_SPI_STATE_BUSY_RX)))
  1121. {
  1122. if((pTxData == NULL ) || (pRxData == NULL ) || (Size == 0))
  1123. {
  1124. return HAL_ERROR;
  1125. }
  1126. /* Check the parameters */
  1127. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  1128. /* Process locked */
  1129. __HAL_LOCK(hspi);
  1130. /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
  1131. if(hspi->State != HAL_SPI_STATE_BUSY_RX)
  1132. {
  1133. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  1134. }
  1135. /* Configure communication */
  1136. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1137. hspi->pTxBuffPtr = (uint8_t*)pTxData;
  1138. hspi->TxXferSize = Size;
  1139. hspi->TxXferCount = Size;
  1140. hspi->pRxBuffPtr = (uint8_t*)pRxData;
  1141. hspi->RxXferSize = Size;
  1142. hspi->RxXferCount = Size;
  1143. /*Init field not used in handle to zero */
  1144. hspi->RxISR = 0;
  1145. hspi->TxISR = 0;
  1146. /* Reset CRC Calculation */
  1147. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1148. {
  1149. SPI_RESET_CRC(hspi);
  1150. }
  1151. /* Check if we are in Rx only or in Rx/Tx Mode and configure the DMA transfer complete callback */
  1152. if(hspi->State == HAL_SPI_STATE_BUSY_RX)
  1153. {
  1154. /* Set the SPI Rx DMA Half transfer complete callback */
  1155. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
  1156. hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
  1157. }
  1158. else
  1159. {
  1160. /* Set the SPI Tx/Rx DMA Half transfer complete callback */
  1161. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfTransmitReceiveCplt;
  1162. hspi->hdmarx->XferCpltCallback = SPI_DMATransmitReceiveCplt;
  1163. }
  1164. /* Set the DMA error callback */
  1165. hspi->hdmarx->XferErrorCallback = SPI_DMAError;
  1166. /* Enable the Rx DMA Stream */
  1167. HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount);
  1168. /* Enable Rx DMA Request */
  1169. hspi->Instance->CR2 |= SPI_CR2_RXDMAEN;
  1170. /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing
  1171. is performed in DMA reception complete callback */
  1172. hspi->hdmatx->XferCpltCallback = NULL;
  1173. if(hspi->State == HAL_SPI_STATE_BUSY_TX_RX)
  1174. {
  1175. /* Set the DMA error callback */
  1176. hspi->hdmatx->XferErrorCallback = SPI_DMAError;
  1177. }
  1178. else
  1179. {
  1180. hspi->hdmatx->XferErrorCallback = NULL;
  1181. }
  1182. /* Enable the Tx DMA Stream */
  1183. HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount);
  1184. /* Process Unlocked */
  1185. __HAL_UNLOCK(hspi);
  1186. /* Check if the SPI is already enabled */
  1187. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  1188. {
  1189. /* Enable SPI peripheral */
  1190. __HAL_SPI_ENABLE(hspi);
  1191. }
  1192. /* Enable Tx DMA Request */
  1193. hspi->Instance->CR2 |= SPI_CR2_TXDMAEN;
  1194. return HAL_OK;
  1195. }
  1196. else
  1197. {
  1198. return HAL_BUSY;
  1199. }
  1200. }
  1201. /**
  1202. * @brief Pauses the DMA Transfer.
  1203. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1204. * the configuration information for the specified SPI module.
  1205. * @retval HAL status
  1206. */
  1207. HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi)
  1208. {
  1209. /* Process Locked */
  1210. __HAL_LOCK(hspi);
  1211. /* Disable the SPI DMA Tx & Rx requests */
  1212. hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
  1213. hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
  1214. /* Process Unlocked */
  1215. __HAL_UNLOCK(hspi);
  1216. return HAL_OK;
  1217. }
  1218. /**
  1219. * @brief Resumes the DMA Transfer.
  1220. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1221. * the configuration information for the specified SPI module.
  1222. * @retval HAL status
  1223. */
  1224. HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi)
  1225. {
  1226. /* Process Locked */
  1227. __HAL_LOCK(hspi);
  1228. /* Enable the SPI DMA Tx & Rx requests */
  1229. hspi->Instance->CR2 |= SPI_CR2_TXDMAEN;
  1230. hspi->Instance->CR2 |= SPI_CR2_RXDMAEN;
  1231. /* Process Unlocked */
  1232. __HAL_UNLOCK(hspi);
  1233. return HAL_OK;
  1234. }
  1235. /**
  1236. * @brief Stops the DMA Transfer.
  1237. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1238. * the configuration information for the specified SPI module.
  1239. * @retval HAL status
  1240. */
  1241. HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi)
  1242. {
  1243. /* The Lock is not implemented on this API to allow the user application
  1244. to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback():
  1245. when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  1246. and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback()
  1247. */
  1248. /* Abort the SPI DMA tx Stream */
  1249. if(hspi->hdmatx != NULL)
  1250. {
  1251. HAL_DMA_Abort(hspi->hdmatx);
  1252. }
  1253. /* Abort the SPI DMA rx Stream */
  1254. if(hspi->hdmarx != NULL)
  1255. {
  1256. HAL_DMA_Abort(hspi->hdmarx);
  1257. }
  1258. /* Disable the SPI DMA Tx & Rx requests */
  1259. hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
  1260. hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
  1261. hspi->State = HAL_SPI_STATE_READY;
  1262. return HAL_OK;
  1263. }
  1264. /**
  1265. * @brief This function handles SPI interrupt request.
  1266. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1267. * the configuration information for SPI module.
  1268. * @retval HAL status
  1269. */
  1270. void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
  1271. {
  1272. uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0;
  1273. tmp1 = __HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE);
  1274. tmp2 = __HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_RXNE);
  1275. tmp3 = __HAL_SPI_GET_FLAG(hspi, SPI_FLAG_OVR);
  1276. /* SPI in mode Receiver and Overrun not occurred ---------------------------*/
  1277. if((tmp1 != RESET) && (tmp2 != RESET) && (tmp3 == RESET))
  1278. {
  1279. hspi->RxISR(hspi);
  1280. return;
  1281. }
  1282. tmp1 = __HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE);
  1283. tmp2 = __HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_TXE);
  1284. /* SPI in mode Transmitter ---------------------------------------------------*/
  1285. if((tmp1 != RESET) && (tmp2 != RESET))
  1286. {
  1287. hspi->TxISR(hspi);
  1288. return;
  1289. }
  1290. if(__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_ERR) != RESET)
  1291. {
  1292. /* SPI CRC error interrupt occurred ---------------------------------------*/
  1293. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  1294. {
  1295. hspi->ErrorCode |= HAL_SPI_ERROR_CRC;
  1296. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  1297. }
  1298. /* SPI Mode Fault error interrupt occurred --------------------------------*/
  1299. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_MODF) != RESET)
  1300. {
  1301. hspi->ErrorCode |= HAL_SPI_ERROR_MODF;
  1302. __HAL_SPI_CLEAR_MODFFLAG(hspi);
  1303. }
  1304. /* SPI Overrun error interrupt occurred -----------------------------------*/
  1305. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_OVR) != RESET)
  1306. {
  1307. if(hspi->State != HAL_SPI_STATE_BUSY_TX)
  1308. {
  1309. hspi->ErrorCode |= HAL_SPI_ERROR_OVR;
  1310. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  1311. }
  1312. }
  1313. /* SPI Frame error interrupt occurred -------------------------------------*/
  1314. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_FRE) != RESET)
  1315. {
  1316. hspi->ErrorCode |= HAL_SPI_ERROR_FRE;
  1317. __HAL_SPI_CLEAR_FREFLAG(hspi);
  1318. }
  1319. /* Call the Error call Back in case of Errors */
  1320. if(hspi->ErrorCode!=HAL_SPI_ERROR_NONE)
  1321. {
  1322. hspi->State = HAL_SPI_STATE_READY;
  1323. HAL_SPI_ErrorCallback(hspi);
  1324. }
  1325. }
  1326. }
  1327. /**
  1328. * @brief Tx Transfer completed callbacks
  1329. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1330. * the configuration information for SPI module.
  1331. * @retval None
  1332. */
  1333. __weak void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
  1334. {
  1335. /* Prevent unused argument(s) compilation warning */
  1336. UNUSED(hspi);
  1337. /* NOTE : This function Should not be modified, when the callback is needed,
  1338. the HAL_SPI_TxCpltCallback could be implemented in the user file
  1339. */
  1340. }
  1341. /**
  1342. * @brief Rx Transfer completed callbacks
  1343. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1344. * the configuration information for SPI module.
  1345. * @retval None
  1346. */
  1347. __weak void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
  1348. {
  1349. /* Prevent unused argument(s) compilation warning */
  1350. UNUSED(hspi);
  1351. /* NOTE : This function Should not be modified, when the callback is needed,
  1352. the HAL_SPI_RxCpltCallback() could be implemented in the user file
  1353. */
  1354. }
  1355. /**
  1356. * @brief Tx and Rx Transfer completed callbacks
  1357. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1358. * the configuration information for SPI module.
  1359. * @retval None
  1360. */
  1361. __weak void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
  1362. {
  1363. /* Prevent unused argument(s) compilation warning */
  1364. UNUSED(hspi);
  1365. /* NOTE : This function Should not be modified, when the callback is needed,
  1366. the HAL_SPI_TxRxCpltCallback() could be implemented in the user file
  1367. */
  1368. }
  1369. /**
  1370. * @brief Tx Half Transfer completed callbacks
  1371. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1372. * the configuration information for SPI module.
  1373. * @retval None
  1374. */
  1375. __weak void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  1376. {
  1377. /* Prevent unused argument(s) compilation warning */
  1378. UNUSED(hspi);
  1379. /* NOTE : This function Should not be modified, when the callback is needed,
  1380. the HAL_SPI_TxHalfCpltCallback could be implemented in the user file
  1381. */
  1382. }
  1383. /**
  1384. * @brief Rx Half Transfer completed callbacks
  1385. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1386. * the configuration information for SPI module.
  1387. * @retval None
  1388. */
  1389. __weak void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  1390. {
  1391. /* Prevent unused argument(s) compilation warning */
  1392. UNUSED(hspi);
  1393. /* NOTE : This function Should not be modified, when the callback is needed,
  1394. the HAL_SPI_RxHalfCpltCallback() could be implemented in the user file
  1395. */
  1396. }
  1397. /**
  1398. * @brief Tx and Rx Transfer completed callbacks
  1399. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1400. * the configuration information for SPI module.
  1401. * @retval None
  1402. */
  1403. __weak void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  1404. {
  1405. /* Prevent unused argument(s) compilation warning */
  1406. UNUSED(hspi);
  1407. /* NOTE : This function Should not be modified, when the callback is needed,
  1408. the HAL_SPI_TxRxHalfCpltCallback() could be implemented in the user file
  1409. */
  1410. }
  1411. /**
  1412. * @brief SPI error callbacks
  1413. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1414. * the configuration information for SPI module.
  1415. * @retval None
  1416. */
  1417. __weak void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
  1418. {
  1419. /* Prevent unused argument(s) compilation warning */
  1420. UNUSED(hspi);
  1421. /* NOTE : - This function Should not be modified, when the callback is needed,
  1422. the HAL_SPI_ErrorCallback() could be implemented in the user file.
  1423. - The ErrorCode parameter in the hspi handle is updated by the SPI processes
  1424. and user can use HAL_SPI_GetError() API to check the latest error occurred.
  1425. */
  1426. }
  1427. /**
  1428. * @}
  1429. */
  1430. /** @defgroup SPI_Exported_Functions_Group3 Peripheral State and Errors functions
  1431. * @brief SPI control functions
  1432. *
  1433. @verbatim
  1434. ===============================================================================
  1435. ##### Peripheral State and Errors functions #####
  1436. ===============================================================================
  1437. [..]
  1438. This subsection provides a set of functions allowing to control the SPI.
  1439. (+) HAL_SPI_GetState() API can be helpful to check in run-time the state of the SPI peripheral
  1440. (+) HAL_SPI_GetError() check in run-time Errors occurring during communication
  1441. @endverbatim
  1442. * @{
  1443. */
  1444. /**
  1445. * @brief Return the SPI state
  1446. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1447. * the configuration information for SPI module.
  1448. * @retval HAL state
  1449. */
  1450. HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi)
  1451. {
  1452. return hspi->State;
  1453. }
  1454. /**
  1455. * @brief Return the SPI error code
  1456. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1457. * the configuration information for SPI module.
  1458. * @retval SPI Error Code
  1459. */
  1460. uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi)
  1461. {
  1462. return hspi->ErrorCode;
  1463. }
  1464. /**
  1465. * @}
  1466. */
  1467. /**
  1468. * @brief Interrupt Handler to close Tx transfer
  1469. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1470. * the configuration information for SPI module.
  1471. * @retval void
  1472. */
  1473. static void SPI_TxCloseIRQHandler(SPI_HandleTypeDef *hspi)
  1474. {
  1475. /* Wait until TXE flag is set to send data */
  1476. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
  1477. {
  1478. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  1479. }
  1480. /* Disable TXE interrupt */
  1481. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE ));
  1482. /* Disable ERR interrupt if Receive process is finished */
  1483. if(__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_RXNE) == RESET)
  1484. {
  1485. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_ERR));
  1486. /* Wait until Busy flag is reset before disabling SPI */
  1487. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_BSY, SET, SPI_TIMEOUT_VALUE) != HAL_OK)
  1488. {
  1489. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  1490. }
  1491. /* Clear OVERRUN flag in 2 Lines communication mode because received is not read */
  1492. if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
  1493. {
  1494. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  1495. }
  1496. /* Check if Errors has been detected during transfer */
  1497. if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
  1498. {
  1499. /* Check if we are in Tx or in Rx/Tx Mode */
  1500. if(hspi->State == HAL_SPI_STATE_BUSY_TX_RX)
  1501. {
  1502. /* Set state to READY before run the Callback Complete */
  1503. hspi->State = HAL_SPI_STATE_READY;
  1504. HAL_SPI_TxRxCpltCallback(hspi);
  1505. }
  1506. else
  1507. {
  1508. /* Set state to READY before run the Callback Complete */
  1509. hspi->State = HAL_SPI_STATE_READY;
  1510. HAL_SPI_TxCpltCallback(hspi);
  1511. }
  1512. }
  1513. else
  1514. {
  1515. /* Set state to READY before run the Callback Complete */
  1516. hspi->State = HAL_SPI_STATE_READY;
  1517. /* Call Error call back in case of Error */
  1518. HAL_SPI_ErrorCallback(hspi);
  1519. }
  1520. }
  1521. }
  1522. /**
  1523. * @brief Interrupt Handler to transmit amount of data in no-blocking mode
  1524. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1525. * the configuration information for SPI module.
  1526. * @retval void
  1527. */
  1528. static void SPI_TxISR(SPI_HandleTypeDef *hspi)
  1529. {
  1530. /* Transmit data in 8 Bit mode */
  1531. if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
  1532. {
  1533. hspi->Instance->DR = (*hspi->pTxBuffPtr++);
  1534. }
  1535. /* Transmit data in 16 Bit mode */
  1536. else
  1537. {
  1538. hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
  1539. hspi->pTxBuffPtr+=2;
  1540. }
  1541. hspi->TxXferCount--;
  1542. if(hspi->TxXferCount == 0)
  1543. {
  1544. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1545. {
  1546. /* calculate and transfer CRC on Tx line */
  1547. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  1548. }
  1549. SPI_TxCloseIRQHandler(hspi);
  1550. }
  1551. }
  1552. /**
  1553. * @brief Interrupt Handler to close Rx transfer
  1554. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1555. * the configuration information for SPI module.
  1556. * @retval void
  1557. */
  1558. static void SPI_RxCloseIRQHandler(SPI_HandleTypeDef *hspi)
  1559. {
  1560. __IO uint16_t tmpreg;
  1561. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1562. {
  1563. /* Wait until RXNE flag is set to send data */
  1564. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
  1565. {
  1566. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  1567. }
  1568. /* Read CRC to reset RXNE flag */
  1569. tmpreg = hspi->Instance->DR;
  1570. UNUSED(tmpreg);
  1571. /* Wait until RXNE flag is set to send data */
  1572. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_TIMEOUT_VALUE) != HAL_OK)
  1573. {
  1574. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  1575. }
  1576. /* Check if CRC error occurred */
  1577. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  1578. {
  1579. hspi->ErrorCode |= HAL_SPI_ERROR_CRC;
  1580. /* Reset CRC Calculation */
  1581. SPI_RESET_CRC(hspi);
  1582. }
  1583. }
  1584. /* Disable RXNE and ERR interrupt */
  1585. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE));
  1586. /* if Transmit process is finished */
  1587. if(__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_TXE) == RESET)
  1588. {
  1589. /* Disable ERR interrupt */
  1590. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_ERR));
  1591. if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  1592. {
  1593. /* Disable SPI peripheral */
  1594. __HAL_SPI_DISABLE(hspi);
  1595. }
  1596. /* Check if Errors has been detected during transfer */
  1597. if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
  1598. {
  1599. /* Check if we are in Rx or in Rx/Tx Mode */
  1600. if(hspi->State == HAL_SPI_STATE_BUSY_TX_RX)
  1601. {
  1602. /* Set state to READY before run the Callback Complete */
  1603. hspi->State = HAL_SPI_STATE_READY;
  1604. HAL_SPI_TxRxCpltCallback(hspi);
  1605. }
  1606. else
  1607. {
  1608. /* Set state to READY before run the Callback Complete */
  1609. hspi->State = HAL_SPI_STATE_READY;
  1610. HAL_SPI_RxCpltCallback(hspi);
  1611. }
  1612. }
  1613. else
  1614. {
  1615. /* Set state to READY before run the Callback Complete */
  1616. hspi->State = HAL_SPI_STATE_READY;
  1617. /* Call Error call back in case of Error */
  1618. HAL_SPI_ErrorCallback(hspi);
  1619. }
  1620. }
  1621. }
  1622. /**
  1623. * @brief Interrupt Handler to receive amount of data in 2Lines mode
  1624. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1625. * the configuration information for SPI module.
  1626. * @retval void
  1627. */
  1628. static void SPI_2LinesRxISR(SPI_HandleTypeDef *hspi)
  1629. {
  1630. /* Receive data in 8 Bit mode */
  1631. if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
  1632. {
  1633. (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
  1634. }
  1635. /* Receive data in 16 Bit mode */
  1636. else
  1637. {
  1638. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  1639. hspi->pRxBuffPtr+=2;
  1640. }
  1641. hspi->RxXferCount--;
  1642. if(hspi->RxXferCount==0)
  1643. {
  1644. SPI_RxCloseIRQHandler(hspi);
  1645. }
  1646. }
  1647. /**
  1648. * @brief Interrupt Handler to receive amount of data in no-blocking mode
  1649. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1650. * the configuration information for SPI module.
  1651. * @retval void
  1652. */
  1653. static void SPI_RxISR(SPI_HandleTypeDef *hspi)
  1654. {
  1655. /* Receive data in 8 Bit mode */
  1656. if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
  1657. {
  1658. (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
  1659. }
  1660. /* Receive data in 16 Bit mode */
  1661. else
  1662. {
  1663. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  1664. hspi->pRxBuffPtr+=2;
  1665. }
  1666. hspi->RxXferCount--;
  1667. /* Enable CRC Transmission */
  1668. if((hspi->RxXferCount == 1) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  1669. {
  1670. /* Set CRC Next to calculate CRC on Rx side */
  1671. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  1672. }
  1673. if(hspi->RxXferCount == 0)
  1674. {
  1675. SPI_RxCloseIRQHandler(hspi);
  1676. }
  1677. }
  1678. /**
  1679. * @brief DMA SPI transmit process complete callback
  1680. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1681. * the configuration information for the specified DMA module.
  1682. * @retval None
  1683. */
  1684. static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1685. {
  1686. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1687. /* DMA Normal Mode */
  1688. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1689. {
  1690. /* Wait until TXE flag is set to send data */
  1691. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
  1692. {
  1693. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  1694. }
  1695. /* Disable Tx DMA Request */
  1696. hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
  1697. /* Wait until Busy flag is reset before disabling SPI */
  1698. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_BSY, SET, SPI_TIMEOUT_VALUE) != HAL_OK)
  1699. {
  1700. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  1701. }
  1702. hspi->TxXferCount = 0;
  1703. hspi->State = HAL_SPI_STATE_READY;
  1704. }
  1705. /* Clear OVERRUN flag in 2 Lines communication mode because received is not read */
  1706. if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
  1707. {
  1708. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  1709. }
  1710. /* Check if Errors has been detected during transfer */
  1711. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  1712. {
  1713. HAL_SPI_ErrorCallback(hspi);
  1714. }
  1715. else
  1716. {
  1717. HAL_SPI_TxCpltCallback(hspi);
  1718. }
  1719. }
  1720. /**
  1721. * @brief DMA SPI receive process complete callback
  1722. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1723. * the configuration information for the specified DMA module.
  1724. * @retval None
  1725. */
  1726. static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1727. {
  1728. __IO uint16_t tmpreg;
  1729. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1730. /* DMA Normal mode */
  1731. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1732. {
  1733. if((hspi->Init.Direction == SPI_DIRECTION_2LINES)&&(hspi->Init.Mode == SPI_MODE_MASTER))
  1734. {
  1735. SPI_DMAEndTransmitReceive(hspi);
  1736. }
  1737. /* SPI_DIRECTION_1LINE or SPI_DIRECTION_2LINES_RXONLY */
  1738. else
  1739. {
  1740. if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  1741. {
  1742. /* Disable SPI peripheral */
  1743. __HAL_SPI_DISABLE(hspi);
  1744. }
  1745. /* Disable Rx DMA Request */
  1746. hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
  1747. hspi->RxXferCount = 0;
  1748. /* Reset CRC Calculation */
  1749. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1750. {
  1751. /* Wait until RXNE flag is set to send data */
  1752. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
  1753. {
  1754. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  1755. }
  1756. /* Read CRC */
  1757. tmpreg = hspi->Instance->DR;
  1758. UNUSED(tmpreg);
  1759. /* Wait until RXNE flag is set */
  1760. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_TIMEOUT_VALUE) != HAL_OK)
  1761. {
  1762. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  1763. }
  1764. /* Check if CRC error occurred */
  1765. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  1766. {
  1767. hspi->ErrorCode |= HAL_SPI_ERROR_CRC;
  1768. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  1769. }
  1770. }
  1771. }
  1772. hspi->State = HAL_SPI_STATE_READY;
  1773. /* Check if Errors has been detected during transfer */
  1774. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  1775. {
  1776. HAL_SPI_ErrorCallback(hspi);
  1777. }
  1778. else
  1779. {
  1780. HAL_SPI_RxCpltCallback(hspi);
  1781. }
  1782. }
  1783. else
  1784. {
  1785. HAL_SPI_RxCpltCallback(hspi);
  1786. }
  1787. }
  1788. /**
  1789. * @brief End DMA SPI transmit receive process
  1790. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1791. * the configuration information for SPI module.
  1792. * @retval None
  1793. */
  1794. static void SPI_DMAEndTransmitReceive(SPI_HandleTypeDef *hspi)
  1795. {
  1796. __IO uint16_t tmpreg;
  1797. /* Reset CRC Calculation */
  1798. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1799. {
  1800. /* Check if CRC is done on going (RXNE flag set) */
  1801. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_TIMEOUT_VALUE) == HAL_OK)
  1802. {
  1803. /* Wait until RXNE flag is set to send data */
  1804. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
  1805. {
  1806. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  1807. }
  1808. }
  1809. /* Read CRC */
  1810. tmpreg = hspi->Instance->DR;
  1811. UNUSED(tmpreg);
  1812. /* Check if CRC error occurred */
  1813. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  1814. {
  1815. hspi->ErrorCode |= HAL_SPI_ERROR_CRC;
  1816. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  1817. }
  1818. }
  1819. /* Wait until TXE flag is set to send data */
  1820. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
  1821. {
  1822. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  1823. }
  1824. /* Disable Tx DMA Request */
  1825. hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
  1826. /* Wait until Busy flag is reset before disabling SPI */
  1827. if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_BSY, SET, SPI_TIMEOUT_VALUE) != HAL_OK)
  1828. {
  1829. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  1830. }
  1831. /* Disable Rx DMA Request */
  1832. hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
  1833. hspi->TxXferCount = 0;
  1834. hspi->RxXferCount = 0;
  1835. }
  1836. /**
  1837. * @brief DMA SPI transmit receive process complete callback
  1838. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1839. * the configuration information for the specified DMA module.
  1840. * @retval None
  1841. */
  1842. static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma)
  1843. {
  1844. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1845. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1846. { /**/
  1847. SPI_DMAEndTransmitReceive(hspi);
  1848. hspi->State = HAL_SPI_STATE_READY;
  1849. /* Check if Errors has been detected during transfer */
  1850. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  1851. {
  1852. HAL_SPI_ErrorCallback(hspi);
  1853. }
  1854. else
  1855. {
  1856. HAL_SPI_TxRxCpltCallback(hspi);
  1857. }
  1858. }
  1859. else
  1860. {
  1861. HAL_SPI_TxRxCpltCallback(hspi);
  1862. }
  1863. }
  1864. /**
  1865. * @brief DMA SPI half transmit process complete callback
  1866. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1867. * the configuration information for the specified DMA module.
  1868. * @retval None
  1869. */
  1870. static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma)
  1871. {
  1872. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1873. HAL_SPI_TxHalfCpltCallback(hspi);
  1874. }
  1875. /**
  1876. * @brief DMA SPI half receive process complete callback
  1877. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1878. * the configuration information for the specified DMA module.
  1879. * @retval None
  1880. */
  1881. static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma)
  1882. {
  1883. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1884. HAL_SPI_RxHalfCpltCallback(hspi);
  1885. }
  1886. /**
  1887. * @brief DMA SPI Half transmit receive process complete callback
  1888. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1889. * the configuration information for the specified DMA module.
  1890. * @retval None
  1891. */
  1892. static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma)
  1893. {
  1894. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1895. HAL_SPI_TxRxHalfCpltCallback(hspi);
  1896. }
  1897. /**
  1898. * @brief DMA SPI communication error callback
  1899. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1900. * the configuration information for the specified DMA module.
  1901. * @retval None
  1902. */
  1903. static void SPI_DMAError(DMA_HandleTypeDef *hdma)
  1904. {
  1905. SPI_HandleTypeDef* hspi = (SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1906. hspi->TxXferCount = 0;
  1907. hspi->RxXferCount = 0;
  1908. hspi->State= HAL_SPI_STATE_READY;
  1909. hspi->ErrorCode |= HAL_SPI_ERROR_DMA;
  1910. HAL_SPI_ErrorCallback(hspi);
  1911. }
  1912. /**
  1913. * @brief This function handles SPI Communication Timeout.
  1914. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1915. * the configuration information for SPI module.
  1916. * @param Flag: SPI flag to check
  1917. * @param Status: Flag status to check: RESET or set
  1918. * @param Timeout: Timeout duration
  1919. * @retval HAL status
  1920. */
  1921. static HAL_StatusTypeDef SPI_WaitOnFlagUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1922. {
  1923. uint32_t tickstart = 0;
  1924. /* Get tick */
  1925. tickstart = HAL_GetTick();
  1926. /* Wait until flag is set */
  1927. if(Status == RESET)
  1928. {
  1929. while(__HAL_SPI_GET_FLAG(hspi, Flag) == RESET)
  1930. {
  1931. if(Timeout != HAL_MAX_DELAY)
  1932. {
  1933. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1934. {
  1935. /* Disable the SPI and reset the CRC: the CRC value should be cleared
  1936. on both master and slave sides in order to resynchronize the master
  1937. and slave for their respective CRC calculation */
  1938. /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
  1939. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  1940. /* Disable SPI peripheral */
  1941. __HAL_SPI_DISABLE(hspi);
  1942. /* Reset CRC Calculation */
  1943. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1944. {
  1945. SPI_RESET_CRC(hspi);
  1946. }
  1947. hspi->State= HAL_SPI_STATE_READY;
  1948. /* Process Unlocked */
  1949. __HAL_UNLOCK(hspi);
  1950. return HAL_TIMEOUT;
  1951. }
  1952. }
  1953. }
  1954. }
  1955. else
  1956. {
  1957. while(__HAL_SPI_GET_FLAG(hspi, Flag) != RESET)
  1958. {
  1959. if(Timeout != HAL_MAX_DELAY)
  1960. {
  1961. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1962. {
  1963. /* Disable the SPI and reset the CRC: the CRC value should be cleared
  1964. on both master and slave sides in order to resynchronize the master
  1965. and slave for their respective CRC calculation */
  1966. /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
  1967. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  1968. /* Disable SPI peripheral */
  1969. __HAL_SPI_DISABLE(hspi);
  1970. /* Reset CRC Calculation */
  1971. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1972. {
  1973. SPI_RESET_CRC(hspi);
  1974. }
  1975. hspi->State= HAL_SPI_STATE_READY;
  1976. /* Process Unlocked */
  1977. __HAL_UNLOCK(hspi);
  1978. return HAL_TIMEOUT;
  1979. }
  1980. }
  1981. }
  1982. }
  1983. return HAL_OK;
  1984. }
  1985. /**
  1986. * @}
  1987. */
  1988. #endif /* HAL_SPI_MODULE_ENABLED */
  1989. /**
  1990. * @}
  1991. */
  1992. /**
  1993. * @}
  1994. */
  1995. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/