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.
 
 
 

3911 lines
120 KiB

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