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.
 
 
 

3740 lines
114 KiB

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