您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

3946 行
122 KiB

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