Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

3896 lines
121 KiB

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