You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

3837 lines
118 KiB

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