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.
 
 
 

3207 lines
98 KiB

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