STM32L4xx_HAL_Driver  1.14.0
IO operation functions

Data transfers functions. More...

Functions

HAL_StatusTypeDef HAL_SPI_Transmit (SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
 Transmit an amount of data in blocking mode. More...
 
HAL_StatusTypeDef HAL_SPI_Receive (SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
 Receive an amount of data in blocking mode. More...
 
HAL_StatusTypeDef HAL_SPI_TransmitReceive (SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
 Transmit and Receive an amount of data in blocking mode. More...
 
HAL_StatusTypeDef HAL_SPI_Transmit_IT (SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
 Transmit an amount of data in non-blocking mode with Interrupt. More...
 
HAL_StatusTypeDef HAL_SPI_Receive_IT (SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
 Receive an amount of data in non-blocking mode with Interrupt. More...
 
HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT (SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
 Transmit and Receive an amount of data in non-blocking mode with Interrupt. More...
 
HAL_StatusTypeDef HAL_SPI_Transmit_DMA (SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
 Transmit an amount of data in non-blocking mode with DMA. More...
 
HAL_StatusTypeDef HAL_SPI_Receive_DMA (SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
 Receive an amount of data in non-blocking mode with DMA. More...
 
HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA (SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
 Transmit and Receive an amount of data in non-blocking mode with DMA. More...
 
HAL_StatusTypeDef HAL_SPI_DMAPause (SPI_HandleTypeDef *hspi)
 Pause the DMA Transfer. More...
 
HAL_StatusTypeDef HAL_SPI_DMAResume (SPI_HandleTypeDef *hspi)
 Resume the DMA Transfer. More...
 
HAL_StatusTypeDef HAL_SPI_DMAStop (SPI_HandleTypeDef *hspi)
 Stop the DMA Transfer. More...
 
HAL_StatusTypeDef HAL_SPI_Abort (SPI_HandleTypeDef *hspi)
 Abort ongoing transfer (blocking mode). More...
 
HAL_StatusTypeDef HAL_SPI_Abort_IT (SPI_HandleTypeDef *hspi)
 Abort ongoing transfer (Interrupt mode). More...
 
void HAL_SPI_IRQHandler (SPI_HandleTypeDef *hspi)
 Handle SPI interrupt request. More...
 
void HAL_SPI_TxCpltCallback (SPI_HandleTypeDef *hspi)
 Tx Transfer completed callback. More...
 
void HAL_SPI_RxCpltCallback (SPI_HandleTypeDef *hspi)
 Rx Transfer completed callback. More...
 
void HAL_SPI_TxRxCpltCallback (SPI_HandleTypeDef *hspi)
 Tx and Rx Transfer completed callback. More...
 
void HAL_SPI_TxHalfCpltCallback (SPI_HandleTypeDef *hspi)
 Tx Half Transfer completed callback. More...
 
void HAL_SPI_RxHalfCpltCallback (SPI_HandleTypeDef *hspi)
 Rx Half Transfer completed callback. More...
 
void HAL_SPI_TxRxHalfCpltCallback (SPI_HandleTypeDef *hspi)
 Tx and Rx Half Transfer callback. More...
 
void HAL_SPI_ErrorCallback (SPI_HandleTypeDef *hspi)
 SPI error callback. More...
 
void HAL_SPI_AbortCpltCallback (SPI_HandleTypeDef *hspi)
 SPI Abort Complete callback. More...
 

Detailed Description

Data transfers functions.

  ==============================================================================
                      ##### IO operation functions #####
 ===============================================================================
 [..]
    This subsection provides a set of functions allowing to manage the SPI
    data transfers.

    [..] The SPI supports master and slave mode :

    (#) There are two modes of transfer:
       (++) Blocking mode: The communication is performed in polling mode.
            The HAL status of all data processing is returned by the same function
            after finishing transfer.
       (++) No-Blocking mode: The communication is performed using Interrupts
            or DMA, These APIs return the HAL status.
            The end of the data processing will be indicated through the
            dedicated SPI IRQ when using Interrupt mode or the DMA IRQ when
            using DMA mode.
            The HAL_SPI_TxCpltCallback(), HAL_SPI_RxCpltCallback() and HAL_SPI_TxRxCpltCallback() user callbacks
            will be executed respectively at the end of the transmit or Receive process
            The HAL_SPI_ErrorCallback()user callback will be executed when a communication error is detected

    (#) APIs provided for these 2 transfer modes (Blocking mode or Non blocking mode using either Interrupt or DMA)
        exist for 1Line (simplex) and 2Lines (full duplex) modes.

Function Documentation

◆ HAL_SPI_Abort()

HAL_StatusTypeDef HAL_SPI_Abort ( SPI_HandleTypeDef hspi)

Abort ongoing transfer (blocking mode).

Parameters
hspiSPI handle.
Note
This procedure could be used for aborting any ongoing transfer (Tx and Rx), started in Interrupt or DMA mode. This procedure performs following operations :
  • Disable SPI Interrupts (depending of transfer direction)
  • Disable the DMA transfer in the peripheral register (if enabled)
  • Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  • Set handle State to READY
This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
Return values
HALstatus

Definition at line 2246 of file stm32l4xx_hal_spi.c.

2247 {
2248  HAL_StatusTypeDef errorcode;
2249  __IO uint32_t count, resetcount;
2250 
2251  /* Initialized local variable */
2252  errorcode = HAL_OK;
2253  resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
2254  count = resetcount;
2255 
2256  /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
2257  CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE);
2258 
2259  /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */
2260  if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE))
2261  {
2262  hspi->TxISR = SPI_AbortTx_ISR;
2263  /* Wait HAL_SPI_STATE_ABORT state */
2264  do
2265  {
2266  if (count == 0U)
2267  {
2268  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2269  break;
2270  }
2271  count--;
2272  }
2273  while (hspi->State != HAL_SPI_STATE_ABORT);
2274  /* Reset Timeout Counter */
2275  count = resetcount;
2276  }
2277 
2278  if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
2279  {
2280  hspi->RxISR = SPI_AbortRx_ISR;
2281  /* Wait HAL_SPI_STATE_ABORT state */
2282  do
2283  {
2284  if (count == 0U)
2285  {
2286  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2287  break;
2288  }
2289  count--;
2290  }
2291  while (hspi->State != HAL_SPI_STATE_ABORT);
2292  /* Reset Timeout Counter */
2293  count = resetcount;
2294  }
2295 
2296  /* Disable the SPI DMA Tx request if enabled */
2297  if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
2298  {
2299  /* Abort the SPI DMA Tx Stream/Channel : use blocking DMA Abort API (no callback) */
2300  if (hspi->hdmatx != NULL)
2301  {
2302  /* Set the SPI DMA Abort callback :
2303  will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
2304  hspi->hdmatx->XferAbortCallback = NULL;
2305 
2306  /* Abort DMA Tx Handle linked to SPI Peripheral */
2307  if (HAL_DMA_Abort(hspi->hdmatx) != HAL_OK)
2308  {
2309  hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
2310  }
2311 
2312  /* Disable Tx DMA Request */
2313  CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN));
2314 
2315  if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
2316  {
2317  hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
2318  }
2319 
2320  /* Disable SPI Peripheral */
2321  __HAL_SPI_DISABLE(hspi);
2322 
2323  /* Empty the FRLVL fifo */
2324  if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
2325  {
2326  hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
2327  }
2328  }
2329  }
2330 
2331  /* Disable the SPI DMA Rx request if enabled */
2332  if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
2333  {
2334  /* Abort the SPI DMA Rx Stream/Channel : use blocking DMA Abort API (no callback) */
2335  if (hspi->hdmarx != NULL)
2336  {
2337  /* Set the SPI DMA Abort callback :
2338  will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
2339  hspi->hdmarx->XferAbortCallback = NULL;
2340 
2341  /* Abort DMA Rx Handle linked to SPI Peripheral */
2342  if (HAL_DMA_Abort(hspi->hdmarx) != HAL_OK)
2343  {
2344  hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
2345  }
2346 
2347  /* Disable peripheral */
2348  __HAL_SPI_DISABLE(hspi);
2349 
2350  /* Control the BSY flag */
2351  if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
2352  {
2353  hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
2354  }
2355 
2356  /* Empty the FRLVL fifo */
2357  if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
2358  {
2359  hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
2360  }
2361 
2362  /* Disable Rx DMA Request */
2363  CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_RXDMAEN));
2364  }
2365  }
2366  /* Reset Tx and Rx transfer counters */
2367  hspi->RxXferCount = 0U;
2368  hspi->TxXferCount = 0U;
2369 
2370  /* Check error during Abort procedure */
2371  if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT)
2372  {
2373  /* return HAL_Error in case of error during Abort procedure */
2374  errorcode = HAL_ERROR;
2375  }
2376  else
2377  {
2378  /* Reset errorCode */
2379  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
2380  }
2381 
2382  /* Clear the Error flags in the SR register */
2383  __HAL_SPI_CLEAR_OVRFLAG(hspi);
2384  __HAL_SPI_CLEAR_FREFLAG(hspi);
2385 
2386  /* Restore hspi->state to ready */
2387  hspi->State = HAL_SPI_STATE_READY;
2388 
2389  return errorcode;
2390 }
static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State, uint32_t Timeout, uint32_t Tickstart)
Handle SPI Communication Timeout.
void(* XferAbortCallback)(struct __DMA_HandleTypeDef *hdma)
static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi)
Handle abort a Rx transaction.
static HAL_StatusTypeDef SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Fifo, uint32_t State, uint32_t Timeout, uint32_t Tickstart)
Handle SPI FIFO Communication Timeout.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
DMA_HandleTypeDef * hdmatx
static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
Handle the check of the RXTX or TX transaction complete.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
return HAL_OK
static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi)
Handle abort a Tx or Rx/Tx transaction.
DMA_HandleTypeDef * hdmarx
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
Abort the DMA Transfer.
__IO uint16_t RxXferCount
__IO HAL_SPI_StateTypeDef State
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
__IO uint16_t TxXferCount
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)

◆ HAL_SPI_Abort_IT()

HAL_StatusTypeDef HAL_SPI_Abort_IT ( SPI_HandleTypeDef hspi)

Abort ongoing transfer (Interrupt mode).

Parameters
hspiSPI handle.
Note
This procedure could be used for aborting any ongoing transfer (Tx and Rx), started in Interrupt or DMA mode. This procedure performs following operations :
  • Disable SPI Interrupts (depending of transfer direction)
  • Disable the DMA transfer in the peripheral register (if enabled)
  • Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  • Set handle State to READY
  • At abort completion, call user abort complete callback
This procedure is executed in Interrupt mode, meaning that abort procedure could be considered as completed only when user abort complete callback is executed (not when exiting function).
Return values
HALstatus

Definition at line 2407 of file stm32l4xx_hal_spi.c.

2408 {
2409  HAL_StatusTypeDef errorcode;
2410  uint32_t abortcplt ;
2411  __IO uint32_t count, resetcount;
2412 
2413  /* Initialized local variable */
2414  errorcode = HAL_OK;
2415  abortcplt = 1U;
2416  resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
2417  count = resetcount;
2418 
2419  /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
2420  CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE);
2421 
2422  /* Change Rx and Tx Irq Handler to Disable TXEIE, RXNEIE and ERRIE interrupts */
2423  if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE))
2424  {
2425  hspi->TxISR = SPI_AbortTx_ISR;
2426  /* Wait HAL_SPI_STATE_ABORT state */
2427  do
2428  {
2429  if (count == 0U)
2430  {
2431  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2432  break;
2433  }
2434  count--;
2435  }
2436  while (hspi->State != HAL_SPI_STATE_ABORT);
2437  /* Reset Timeout Counter */
2438  count = resetcount;
2439  }
2440 
2441  if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
2442  {
2443  hspi->RxISR = SPI_AbortRx_ISR;
2444  /* Wait HAL_SPI_STATE_ABORT state */
2445  do
2446  {
2447  if (count == 0U)
2448  {
2449  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2450  break;
2451  }
2452  count--;
2453  }
2454  while (hspi->State != HAL_SPI_STATE_ABORT);
2455  /* Reset Timeout Counter */
2456  count = resetcount;
2457  }
2458 
2459  /* If DMA Tx and/or DMA Rx Handles are associated to SPI Handle, DMA Abort complete callbacks should be initialised
2460  before any call to DMA Abort functions */
2461  /* DMA Tx Handle is valid */
2462  if (hspi->hdmatx != NULL)
2463  {
2464  /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
2465  Otherwise, set it to NULL */
2466  if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
2467  {
2469  }
2470  else
2471  {
2472  hspi->hdmatx->XferAbortCallback = NULL;
2473  }
2474  }
2475  /* DMA Rx Handle is valid */
2476  if (hspi->hdmarx != NULL)
2477  {
2478  /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
2479  Otherwise, set it to NULL */
2480  if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
2481  {
2483  }
2484  else
2485  {
2486  hspi->hdmarx->XferAbortCallback = NULL;
2487  }
2488  }
2489 
2490  /* Disable the SPI DMA Tx request if enabled */
2491  if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
2492  {
2493  /* Abort the SPI DMA Tx Stream/Channel */
2494  if (hspi->hdmatx != NULL)
2495  {
2496  /* Abort DMA Tx Handle linked to SPI Peripheral */
2497  if (HAL_DMA_Abort_IT(hspi->hdmatx) != HAL_OK)
2498  {
2499  hspi->hdmatx->XferAbortCallback = NULL;
2500  hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
2501  }
2502  else
2503  {
2504  abortcplt = 0U;
2505  }
2506  }
2507  }
2508  /* Disable the SPI DMA Rx request if enabled */
2509  if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
2510  {
2511  /* Abort the SPI DMA Rx Stream/Channel */
2512  if (hspi->hdmarx != NULL)
2513  {
2514  /* Abort DMA Rx Handle linked to SPI Peripheral */
2515  if (HAL_DMA_Abort_IT(hspi->hdmarx) != HAL_OK)
2516  {
2517  hspi->hdmarx->XferAbortCallback = NULL;
2518  hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
2519  }
2520  else
2521  {
2522  abortcplt = 0U;
2523  }
2524  }
2525  }
2526 
2527  if (abortcplt == 1U)
2528  {
2529  /* Reset Tx and Rx transfer counters */
2530  hspi->RxXferCount = 0U;
2531  hspi->TxXferCount = 0U;
2532 
2533  /* Check error during Abort procedure */
2534  if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT)
2535  {
2536  /* return HAL_Error in case of error during Abort procedure */
2537  errorcode = HAL_ERROR;
2538  }
2539  else
2540  {
2541  /* Reset errorCode */
2542  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
2543  }
2544 
2545  /* Clear the Error flags in the SR register */
2546  __HAL_SPI_CLEAR_OVRFLAG(hspi);
2547  __HAL_SPI_CLEAR_FREFLAG(hspi);
2548 
2549  /* Restore hspi->State to Ready */
2550  hspi->State = HAL_SPI_STATE_READY;
2551 
2552  /* As no DMA to be aborted, call directly user Abort complete callback */
2553 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2554  hspi->AbortCpltCallback(hspi);
2555 #else
2557 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2558  }
2559 
2560  return errorcode;
2561 }
void(* XferAbortCallback)(struct __DMA_HandleTypeDef *hdma)
void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef *hspi)
SPI Abort Complete callback.
static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi)
Handle abort a Rx transaction.
DMA_HandleTypeDef * hdmatx
HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
Aborts the DMA Transfer in Interrupt mode.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
void(* AbortCpltCallback)(struct __SPI_HandleTypeDef *hspi)
return HAL_OK
static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi)
Handle abort a Tx or Rx/Tx transaction.
DMA_HandleTypeDef * hdmarx
__IO uint16_t RxXferCount
__IO HAL_SPI_StateTypeDef State
static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
DMA SPI Rx communication abort callback, when initiated by user (To be called at end of DMA Rx Abort ...
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
__IO uint16_t TxXferCount
static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
DMA SPI Tx communication abort callback, when initiated by user (To be called at end of DMA Tx Abort ...
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)

◆ HAL_SPI_AbortCpltCallback()

__weak void HAL_SPI_AbortCpltCallback ( SPI_HandleTypeDef hspi)

SPI Abort Complete callback.

Parameters
hspiSPI handle.
Return values
None

Definition at line 2869 of file stm32l4xx_hal_spi.c.

2870 {
2871  /* Prevent unused argument(s) compilation warning */
2872  UNUSED(hspi);
2873 
2874  /* NOTE : This function should not be modified, when the callback is needed,
2875  the HAL_SPI_AbortCpltCallback can be implemented in the user file.
2876  */
2877 }

◆ HAL_SPI_DMAPause()

HAL_StatusTypeDef HAL_SPI_DMAPause ( SPI_HandleTypeDef hspi)

Pause the DMA Transfer.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for the specified SPI module.
Return values
HALstatus

Definition at line 2569 of file stm32l4xx_hal_spi.c.

2570 {
2571  /* Process Locked */
2572  __HAL_LOCK(hspi);
2573 
2574  /* Disable the SPI DMA Tx & Rx requests */
2575  CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
2576 
2577  /* Process Unlocked */
2578  __HAL_UNLOCK(hspi);
2579 
2580  return HAL_OK;
2581 }
__HAL_UNLOCK(hrtc)
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
return HAL_OK

◆ HAL_SPI_DMAResume()

HAL_StatusTypeDef HAL_SPI_DMAResume ( SPI_HandleTypeDef hspi)

Resume the DMA Transfer.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for the specified SPI module.
Return values
HALstatus

Definition at line 2589 of file stm32l4xx_hal_spi.c.

2590 {
2591  /* Process Locked */
2592  __HAL_LOCK(hspi);
2593 
2594  /* Enable the SPI DMA Tx & Rx requests */
2595  SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
2596 
2597  /* Process Unlocked */
2598  __HAL_UNLOCK(hspi);
2599 
2600  return HAL_OK;
2601 }
__HAL_UNLOCK(hrtc)
__HAL_LOCK(hrtc)
return HAL_OK

◆ HAL_SPI_DMAStop()

HAL_StatusTypeDef HAL_SPI_DMAStop ( SPI_HandleTypeDef hspi)

Stop the DMA Transfer.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for the specified SPI module.
Return values
HALstatus

Definition at line 2609 of file stm32l4xx_hal_spi.c.

2610 {
2611  HAL_StatusTypeDef errorcode = HAL_OK;
2612  /* The Lock is not implemented on this API to allow the user application
2613  to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback():
2614  when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
2615  and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback()
2616  */
2617 
2618  /* Abort the SPI DMA tx Stream/Channel */
2619  if (hspi->hdmatx != NULL)
2620  {
2621  if (HAL_OK != HAL_DMA_Abort(hspi->hdmatx))
2622  {
2623  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2624  errorcode = HAL_ERROR;
2625  }
2626  }
2627  /* Abort the SPI DMA rx Stream/Channel */
2628  if (hspi->hdmarx != NULL)
2629  {
2630  if (HAL_OK != HAL_DMA_Abort(hspi->hdmarx))
2631  {
2632  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2633  errorcode = HAL_ERROR;
2634  }
2635  }
2636 
2637  /* Disable the SPI DMA Tx & Rx requests */
2638  CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
2639  hspi->State = HAL_SPI_STATE_READY;
2640  return errorcode;
2641 }
DMA_HandleTypeDef * hdmatx
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
return HAL_OK
DMA_HandleTypeDef * hdmarx
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
Abort the DMA Transfer.
__IO HAL_SPI_StateTypeDef State

◆ HAL_SPI_ErrorCallback()

__weak void HAL_SPI_ErrorCallback ( SPI_HandleTypeDef hspi)

SPI error callback.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
Return values
None

Definition at line 2851 of file stm32l4xx_hal_spi.c.

2852 {
2853  /* Prevent unused argument(s) compilation warning */
2854  UNUSED(hspi);
2855 
2856  /* NOTE : This function should not be modified, when the callback is needed,
2857  the HAL_SPI_ErrorCallback should be implemented in the user file
2858  */
2859  /* NOTE : The ErrorCode parameter in the hspi handle is updated by the SPI processes
2860  and user can use HAL_SPI_GetError() API to check the latest error occurred
2861  */
2862 }

◆ HAL_SPI_IRQHandler()

void HAL_SPI_IRQHandler ( SPI_HandleTypeDef hspi)

Handle SPI interrupt request.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for the specified SPI module.
Return values
None

Definition at line 2649 of file stm32l4xx_hal_spi.c.

2650 {
2651  uint32_t itsource = hspi->Instance->CR2;
2652  uint32_t itflag = hspi->Instance->SR;
2653 
2654  /* SPI in mode Receiver ----------------------------------------------------*/
2655  if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) == RESET) &&
2656  (SPI_CHECK_FLAG(itflag, SPI_FLAG_RXNE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_RXNE) != RESET))
2657  {
2658  hspi->RxISR(hspi);
2659  return;
2660  }
2661 
2662  /* SPI in mode Transmitter -------------------------------------------------*/
2663  if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_TXE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_TXE) != RESET))
2664  {
2665  hspi->TxISR(hspi);
2666  return;
2667  }
2668 
2669  /* SPI in Error Treatment --------------------------------------------------*/
2670  if (((SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET) || (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET) || (SPI_CHECK_FLAG(itflag, SPI_FLAG_FRE) != RESET)) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_ERR) != RESET))
2671  {
2672  /* SPI Overrun error interrupt occurred ----------------------------------*/
2673  if (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET)
2674  {
2675  if (hspi->State != HAL_SPI_STATE_BUSY_TX)
2676  {
2677  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_OVR);
2678  __HAL_SPI_CLEAR_OVRFLAG(hspi);
2679  }
2680  else
2681  {
2682  __HAL_SPI_CLEAR_OVRFLAG(hspi);
2683  return;
2684  }
2685  }
2686 
2687  /* SPI Mode Fault error interrupt occurred -------------------------------*/
2688  if (SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET)
2689  {
2690  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_MODF);
2691  __HAL_SPI_CLEAR_MODFFLAG(hspi);
2692  }
2693 
2694  /* SPI Frame error interrupt occurred ------------------------------------*/
2695  if (SPI_CHECK_FLAG(itflag, SPI_FLAG_FRE) != RESET)
2696  {
2697  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FRE);
2698  __HAL_SPI_CLEAR_FREFLAG(hspi);
2699  }
2700 
2701  if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
2702  {
2703  /* Disable all interrupts */
2704  __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR);
2705 
2706  hspi->State = HAL_SPI_STATE_READY;
2707  /* Disable the SPI DMA requests if enabled */
2708  if ((HAL_IS_BIT_SET(itsource, SPI_CR2_TXDMAEN)) || (HAL_IS_BIT_SET(itsource, SPI_CR2_RXDMAEN)))
2709  {
2710  CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN));
2711 
2712  /* Abort the SPI DMA Rx channel */
2713  if (hspi->hdmarx != NULL)
2714  {
2715  /* Set the SPI DMA Abort callback :
2716  will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
2718  if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmarx))
2719  {
2720  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2721  }
2722  }
2723  /* Abort the SPI DMA Tx channel */
2724  if (hspi->hdmatx != NULL)
2725  {
2726  /* Set the SPI DMA Abort callback :
2727  will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
2729  if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmatx))
2730  {
2731  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2732  }
2733  }
2734  }
2735  else
2736  {
2737  /* Call user error callback */
2738 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2739  hspi->ErrorCallback(hspi);
2740 #else
2741  HAL_SPI_ErrorCallback(hspi);
2742 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2743  }
2744  }
2745  return;
2746  }
2747 }
void(* ErrorCallback)(struct __SPI_HandleTypeDef *hspi)
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
SPI error callback.
void(* XferAbortCallback)(struct __DMA_HandleTypeDef *hdma)
DMA_HandleTypeDef * hdmatx
HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
Aborts the DMA Transfer in Interrupt mode.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
return HAL_OK
DMA_HandleTypeDef * hdmarx
__IO HAL_SPI_StateTypeDef State
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma)
DMA SPI communication abort callback, when initiated by HAL services on Error (To be called at end of...
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)

◆ HAL_SPI_Receive()

HAL_StatusTypeDef HAL_SPI_Receive ( SPI_HandleTypeDef hspi,
uint8_t *  pData,
uint16_t  Size,
uint32_t  Timeout 
)

Receive an amount of data in blocking mode.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
pDatapointer to data buffer
Sizeamount of data to be received
TimeoutTimeout duration
Return values
HALstatus

Definition at line 972 of file stm32l4xx_hal_spi.c.

973 {
974  uint32_t tickstart;
975  HAL_StatusTypeDef errorcode = HAL_OK;
976 
977  if ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
978  {
980  /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
981  return HAL_SPI_TransmitReceive(hspi, pData, pData, Size, Timeout);
982  }
983 
984  /* Process Locked */
985  __HAL_LOCK(hspi);
986 
987  /* Init tickstart for timeout management*/
988  tickstart = HAL_GetTick();
989 
990  if (hspi->State != HAL_SPI_STATE_READY)
991  {
992  errorcode = HAL_BUSY;
993  goto error;
994  }
995 
996  if ((pData == NULL) || (Size == 0U))
997  {
998  errorcode = HAL_ERROR;
999  goto error;
1000  }
1001 
1002  /* Set the transaction information */
1003  hspi->State = HAL_SPI_STATE_BUSY_RX;
1004  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
1005  hspi->pRxBuffPtr = (uint8_t *)pData;
1006  hspi->RxXferSize = Size;
1007  hspi->RxXferCount = Size;
1008 
1009  /*Init field not used in handle to zero */
1010  hspi->pTxBuffPtr = (uint8_t *)NULL;
1011  hspi->TxXferSize = 0U;
1012  hspi->TxXferCount = 0U;
1013  hspi->RxISR = NULL;
1014  hspi->TxISR = NULL;
1015 
1016 #if (USE_SPI_CRC != 0U)
1017  /* Reset CRC Calculation */
1018  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
1019  {
1020  SPI_RESET_CRC(hspi);
1021  /* this is done to handle the CRCNEXT before the latest data */
1022  hspi->RxXferCount--;
1023  }
1024 #endif /* USE_SPI_CRC */
1025 
1026  /* Set the Rx Fifo threshold */
1027  if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1028  {
1029  /* Set RX Fifo threshold according the reception data length: 16bit */
1030  CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1031  }
1032  else
1033  {
1034  /* Set RX Fifo threshold according the reception data length: 8bit */
1035  SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1036  }
1037 
1038  /* Configure communication direction: 1Line */
1039  if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1040  {
1041  SPI_1LINE_RX(hspi);
1042  }
1043 
1044  /* Check if the SPI is already enabled */
1045  if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1046  {
1047  /* Enable SPI peripheral */
1048  __HAL_SPI_ENABLE(hspi);
1049  }
1050 
1051  /* Receive data in 8 Bit mode */
1052  if (hspi->Init.DataSize <= SPI_DATASIZE_8BIT)
1053  {
1054  /* Transfer loop */
1055  while (hspi->RxXferCount > 0U)
1056  {
1057  /* Check the RXNE flag */
1058  if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))
1059  {
1060  /* read the received data */
1061  (* (uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
1062  hspi->pRxBuffPtr += sizeof(uint8_t);
1063  hspi->RxXferCount--;
1064  }
1065  else
1066  {
1067  /* Timeout management */
1068  if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
1069  {
1070  errorcode = HAL_TIMEOUT;
1071  goto error;
1072  }
1073  }
1074  }
1075  }
1076  else
1077  {
1078  /* Transfer loop */
1079  while (hspi->RxXferCount > 0U)
1080  {
1081  /* Check the RXNE flag */
1082  if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))
1083  {
1084  *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
1085  hspi->pRxBuffPtr += sizeof(uint16_t);
1086  hspi->RxXferCount--;
1087  }
1088  else
1089  {
1090  /* Timeout management */
1091  if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
1092  {
1093  errorcode = HAL_TIMEOUT;
1094  goto error;
1095  }
1096  }
1097  }
1098  }
1099 
1100 #if (USE_SPI_CRC != 0U)
1101  /* Handle the CRC Transmission */
1102  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
1103  {
1104  /* freeze the CRC before the latest data */
1105  SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
1106 
1107  /* Read the latest data */
1108  if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
1109  {
1110  /* the latest data has not been received */
1111  errorcode = HAL_TIMEOUT;
1112  goto error;
1113  }
1114 
1115  /* Receive last data in 16 Bit mode */
1116  if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1117  {
1118  *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
1119  }
1120  /* Receive last data in 8 Bit mode */
1121  else
1122  {
1123  (*(uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
1124  }
1125 
1126  /* Wait the CRC data */
1127  if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
1128  {
1129  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1130  errorcode = HAL_TIMEOUT;
1131  goto error;
1132  }
1133 
1134  /* Read CRC to Flush DR and RXNE flag */
1135  if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
1136  {
1137  /* Read 16bit CRC */
1138  READ_REG(hspi->Instance->DR);
1139  }
1140  else
1141  {
1142  /* Read 8bit CRC */
1143  READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
1144 
1145  if ((hspi->Init.DataSize == SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
1146  {
1147  if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
1148  {
1149  /* Error on the CRC reception */
1150  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1151  errorcode = HAL_TIMEOUT;
1152  goto error;
1153  }
1154  /* Read 8bit CRC again in case of 16bit CRC in 8bit Data mode */
1155  READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
1156  }
1157  }
1158  }
1159 #endif /* USE_SPI_CRC */
1160 
1161  /* Check the end of the transaction */
1162  if (SPI_EndRxTransaction(hspi, Timeout, tickstart) != HAL_OK)
1163  {
1164  hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
1165  }
1166 
1167 #if (USE_SPI_CRC != 0U)
1168  /* Check if CRC error occurred */
1169  if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR))
1170  {
1171  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1172  __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
1173  }
1174 #endif /* USE_SPI_CRC */
1175 
1176  if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
1177  {
1178  errorcode = HAL_ERROR;
1179  }
1180 
1181 error :
1182  hspi->State = HAL_SPI_STATE_READY;
1183  __HAL_UNLOCK(hspi);
1184  return errorcode;
1185 }
static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State, uint32_t Timeout, uint32_t Tickstart)
Handle SPI Communication Timeout.
SPI_InitTypeDef Init
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
__HAL_UNLOCK(hrtc)
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
return HAL_OK
__IO uint16_t RxXferCount
__IO HAL_SPI_StateTypeDef State
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
__IO uint16_t TxXferCount
static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
Handle the check of the RX transaction complete.
HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
Transmit and Receive an amount of data in blocking mode.
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)

◆ HAL_SPI_Receive_DMA()

HAL_StatusTypeDef HAL_SPI_Receive_DMA ( SPI_HandleTypeDef hspi,
uint8_t *  pData,
uint16_t  Size 
)

Receive an amount of data in non-blocking mode with DMA.

Note
In case of MASTER mode and SPI_DIRECTION_2LINES direction, hdmatx shall be defined.
Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
pDatapointer to data buffer
Note
When the CRC feature is enabled the pData Length must be Size + 1.
Parameters
Sizeamount of data to be sent
Return values
HALstatus

Definition at line 1914 of file stm32l4xx_hal_spi.c.

1915 {
1916  HAL_StatusTypeDef errorcode = HAL_OK;
1917 
1918  /* Check rx dma handle */
1919  assert_param(IS_SPI_DMA_HANDLE(hspi->hdmarx));
1920 
1921  if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
1922  {
1923  hspi->State = HAL_SPI_STATE_BUSY_RX;
1924 
1925  /* Check tx dma handle */
1926  assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
1927 
1928  /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
1929  return HAL_SPI_TransmitReceive_DMA(hspi, pData, pData, Size);
1930  }
1931 
1932  /* Process Locked */
1933  __HAL_LOCK(hspi);
1934 
1935  if (hspi->State != HAL_SPI_STATE_READY)
1936  {
1937  errorcode = HAL_BUSY;
1938  goto error;
1939  }
1940 
1941  if ((pData == NULL) || (Size == 0U))
1942  {
1943  errorcode = HAL_ERROR;
1944  goto error;
1945  }
1946 
1947  /* Set the transaction information */
1948  hspi->State = HAL_SPI_STATE_BUSY_RX;
1949  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
1950  hspi->pRxBuffPtr = (uint8_t *)pData;
1951  hspi->RxXferSize = Size;
1952  hspi->RxXferCount = Size;
1953 
1954  /*Init field not used in handle to zero */
1955  hspi->RxISR = NULL;
1956  hspi->TxISR = NULL;
1957  hspi->TxXferSize = 0U;
1958  hspi->TxXferCount = 0U;
1959 
1960  /* Configure communication direction : 1Line */
1961  if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1962  {
1963  SPI_1LINE_RX(hspi);
1964  }
1965 
1966 #if (USE_SPI_CRC != 0U)
1967  /* Reset CRC Calculation */
1968  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
1969  {
1970  SPI_RESET_CRC(hspi);
1971  }
1972 #endif /* USE_SPI_CRC */
1973 
1974 
1975  CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
1976  if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1977  {
1978  /* Set RX Fifo threshold according the reception data length: 16bit */
1979  CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1980  }
1981  else
1982  {
1983  /* Set RX Fifo threshold according the reception data length: 8bit */
1984  SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1985 
1986  if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
1987  {
1988  /* Set RX Fifo threshold according the reception data length: 16bit */
1989  CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1990 
1991  if ((hspi->RxXferCount & 0x1U) == 0x0U)
1992  {
1993  CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
1994  hspi->RxXferCount = hspi->RxXferCount >> 1U;
1995  }
1996  else
1997  {
1998  SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
1999  hspi->RxXferCount = (hspi->RxXferCount >> 1U) + 1U;
2000  }
2001  }
2002  }
2003 
2004  /* Set the SPI RxDMA Half transfer complete callback */
2006 
2007  /* Set the SPI Rx DMA transfer complete callback */
2009 
2010  /* Set the DMA error callback */
2012 
2013  /* Set the DMA AbortCpltCallback */
2014  hspi->hdmarx->XferAbortCallback = NULL;
2015 
2016  /* Enable the Rx DMA Stream/Channel */
2017  if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount))
2018  {
2019  /* Update SPI error code */
2020  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2021  errorcode = HAL_ERROR;
2022 
2023  hspi->State = HAL_SPI_STATE_READY;
2024  goto error;
2025  }
2026 
2027  /* Check if the SPI is already enabled */
2028  if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
2029  {
2030  /* Enable SPI peripheral */
2031  __HAL_SPI_ENABLE(hspi);
2032  }
2033 
2034  /* Enable the SPI Error Interrupt Bit */
2035  __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
2036 
2037  /* Enable Rx DMA Request */
2038  SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
2039 
2040 error:
2041  /* Process Unlocked */
2042  __HAL_UNLOCK(hspi);
2043  return errorcode;
2044 }
void(* XferAbortCallback)(struct __DMA_HandleTypeDef *hdma)
static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
DMA SPI receive process complete callback.
void(* XferCpltCallback)(struct __DMA_HandleTypeDef *hdma)
SPI_InitTypeDef Init
DMA_HandleTypeDef * hdmatx
void(* XferErrorCallback)(struct __DMA_HandleTypeDef *hdma)
__HAL_UNLOCK(hrtc)
void(* XferHalfCpltCallback)(struct __DMA_HandleTypeDef *hdma)
DMA_InitTypeDef Init
static void SPI_DMAError(DMA_HandleTypeDef *hdma)
DMA SPI communication error callback.
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
Start the DMA Transfer with interrupt enabled.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma)
DMA SPI half receive process complete callback.
return HAL_OK
DMA_HandleTypeDef * hdmarx
HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
Transmit and Receive an amount of data in non-blocking mode with DMA.
__IO uint16_t RxXferCount
__IO HAL_SPI_StateTypeDef State
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
__IO uint16_t TxXferCount
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_SPI_Receive_IT()

HAL_StatusTypeDef HAL_SPI_Receive_IT ( SPI_HandleTypeDef hspi,
uint8_t *  pData,
uint16_t  Size 
)

Receive an amount of data in non-blocking mode with Interrupt.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
pDatapointer to data buffer
Sizeamount of data to be sent
Return values
HALstatus

Definition at line 1578 of file stm32l4xx_hal_spi.c.

1579 {
1580  HAL_StatusTypeDef errorcode = HAL_OK;
1581 
1582  if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
1583  {
1584  hspi->State = HAL_SPI_STATE_BUSY_RX;
1585  /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
1586  return HAL_SPI_TransmitReceive_IT(hspi, pData, pData, Size);
1587  }
1588 
1589  /* Process Locked */
1590  __HAL_LOCK(hspi);
1591 
1592  if (hspi->State != HAL_SPI_STATE_READY)
1593  {
1594  errorcode = HAL_BUSY;
1595  goto error;
1596  }
1597 
1598  if ((pData == NULL) || (Size == 0U))
1599  {
1600  errorcode = HAL_ERROR;
1601  goto error;
1602  }
1603 
1604  /* Set the transaction information */
1605  hspi->State = HAL_SPI_STATE_BUSY_RX;
1606  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
1607  hspi->pRxBuffPtr = (uint8_t *)pData;
1608  hspi->RxXferSize = Size;
1609  hspi->RxXferCount = Size;
1610 
1611  /* Init field not used in handle to zero */
1612  hspi->pTxBuffPtr = (uint8_t *)NULL;
1613  hspi->TxXferSize = 0U;
1614  hspi->TxXferCount = 0U;
1615  hspi->TxISR = NULL;
1616 
1617  /* Check the data size to adapt Rx threshold and the set the function for IT treatment */
1618  if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1619  {
1620  /* Set RX Fifo threshold according the reception data length: 16 bit */
1621  CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1622  hspi->RxISR = SPI_RxISR_16BIT;
1623  }
1624  else
1625  {
1626  /* Set RX Fifo threshold according the reception data length: 8 bit */
1627  SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1628  hspi->RxISR = SPI_RxISR_8BIT;
1629  }
1630 
1631  /* Configure communication direction : 1Line */
1632  if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1633  {
1634  SPI_1LINE_RX(hspi);
1635  }
1636 
1637 #if (USE_SPI_CRC != 0U)
1638  /* Reset CRC Calculation */
1639  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
1640  {
1641  hspi->CRCSize = 1U;
1642  if ((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
1643  {
1644  hspi->CRCSize = 2U;
1645  }
1646  SPI_RESET_CRC(hspi);
1647  }
1648  else
1649  {
1650  hspi->CRCSize = 0U;
1651  }
1652 #endif /* USE_SPI_CRC */
1653 
1654  /* Enable TXE and ERR interrupt */
1655  __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
1656 
1657  /* Note : The SPI must be enabled after unlocking current process
1658  to avoid the risk of SPI interrupt handle execution before current
1659  process unlock */
1660 
1661  /* Check if the SPI is already enabled */
1662  if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1663  {
1664  /* Enable SPI peripheral */
1665  __HAL_SPI_ENABLE(hspi);
1666  }
1667 
1668 error :
1669  /* Process Unlocked */
1670  __HAL_UNLOCK(hspi);
1671  return errorcode;
1672 }
static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
Manage the 16-bit receive in Interrupt context.
static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
Manage the receive 8-bit in Interrupt context.
SPI_InitTypeDef Init
__HAL_UNLOCK(hrtc)
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
return HAL_OK
__IO uint16_t RxXferCount
__IO HAL_SPI_StateTypeDef State
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
Transmit and Receive an amount of data in non-blocking mode with Interrupt.
__IO uint16_t TxXferCount
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)

◆ HAL_SPI_RxCpltCallback()

__weak void HAL_SPI_RxCpltCallback ( SPI_HandleTypeDef hspi)

Rx Transfer completed callback.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
Return values
None

Definition at line 2771 of file stm32l4xx_hal_spi.c.

2772 {
2773  /* Prevent unused argument(s) compilation warning */
2774  UNUSED(hspi);
2775 
2776  /* NOTE : This function should not be modified, when the callback is needed,
2777  the HAL_SPI_RxCpltCallback should be implemented in the user file
2778  */
2779 }

◆ HAL_SPI_RxHalfCpltCallback()

__weak void HAL_SPI_RxHalfCpltCallback ( SPI_HandleTypeDef hspi)

Rx Half Transfer completed callback.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
Return values
None

Definition at line 2819 of file stm32l4xx_hal_spi.c.

2820 {
2821  /* Prevent unused argument(s) compilation warning */
2822  UNUSED(hspi);
2823 
2824  /* NOTE : This function should not be modified, when the callback is needed,
2825  the HAL_SPI_RxHalfCpltCallback() should be implemented in the user file
2826  */
2827 }

◆ HAL_SPI_Transmit()

HAL_StatusTypeDef HAL_SPI_Transmit ( SPI_HandleTypeDef hspi,
uint8_t *  pData,
uint16_t  Size,
uint32_t  Timeout 
)

Transmit an amount of data in blocking mode.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
pDatapointer to data buffer
Sizeamount of data to be sent
TimeoutTimeout duration
Return values
HALstatus

Definition at line 789 of file stm32l4xx_hal_spi.c.

790 {
791  uint32_t tickstart;
792  HAL_StatusTypeDef errorcode = HAL_OK;
793  uint16_t initial_TxXferCount;
794 
795  /* Check Direction parameter */
796  assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
797 
798  /* Process Locked */
799  __HAL_LOCK(hspi);
800 
801  /* Init tickstart for timeout management*/
802  tickstart = HAL_GetTick();
803  initial_TxXferCount = Size;
804 
805  if (hspi->State != HAL_SPI_STATE_READY)
806  {
807  errorcode = HAL_BUSY;
808  goto error;
809  }
810 
811  if ((pData == NULL) || (Size == 0U))
812  {
813  errorcode = HAL_ERROR;
814  goto error;
815  }
816 
817  /* Set the transaction information */
819  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
820  hspi->pTxBuffPtr = (uint8_t *)pData;
821  hspi->TxXferSize = Size;
822  hspi->TxXferCount = Size;
823 
824  /*Init field not used in handle to zero */
825  hspi->pRxBuffPtr = (uint8_t *)NULL;
826  hspi->RxXferSize = 0U;
827  hspi->RxXferCount = 0U;
828  hspi->TxISR = NULL;
829  hspi->RxISR = NULL;
830 
831  /* Configure communication direction : 1Line */
832  if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
833  {
834  SPI_1LINE_TX(hspi);
835  }
836 
837 #if (USE_SPI_CRC != 0U)
838  /* Reset CRC Calculation */
839  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
840  {
841  SPI_RESET_CRC(hspi);
842  }
843 #endif /* USE_SPI_CRC */
844 
845  /* Check if the SPI is already enabled */
846  if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
847  {
848  /* Enable SPI peripheral */
849  __HAL_SPI_ENABLE(hspi);
850  }
851 
852  /* Transmit data in 16 Bit mode */
853  if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
854  {
855  if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
856  {
857  hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
858  hspi->pTxBuffPtr += sizeof(uint16_t);
859  hspi->TxXferCount--;
860  }
861  /* Transmit data in 16 Bit mode */
862  while (hspi->TxXferCount > 0U)
863  {
864  /* Wait until TXE flag is set to send data */
865  if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE))
866  {
867  hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
868  hspi->pTxBuffPtr += sizeof(uint16_t);
869  hspi->TxXferCount--;
870  }
871  else
872  {
873  /* Timeout management */
874  if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
875  {
876  errorcode = HAL_TIMEOUT;
877  goto error;
878  }
879  }
880  }
881  }
882  /* Transmit data in 8 Bit mode */
883  else
884  {
885  if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
886  {
887  if (hspi->TxXferCount > 1U)
888  {
889  /* write on the data register in packing mode */
890  hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
891  hspi->pTxBuffPtr += sizeof(uint16_t);
892  hspi->TxXferCount -= 2U;
893  }
894  else
895  {
896  *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr);
897  hspi->pTxBuffPtr ++;
898  hspi->TxXferCount--;
899  }
900  }
901  while (hspi->TxXferCount > 0U)
902  {
903  /* Wait until TXE flag is set to send data */
904  if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE))
905  {
906  if (hspi->TxXferCount > 1U)
907  {
908  /* write on the data register in packing mode */
909  hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
910  hspi->pTxBuffPtr += sizeof(uint16_t);
911  hspi->TxXferCount -= 2U;
912  }
913  else
914  {
915  *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr);
916  hspi->pTxBuffPtr++;
917  hspi->TxXferCount--;
918  }
919  }
920  else
921  {
922  /* Timeout management */
923  if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
924  {
925  errorcode = HAL_TIMEOUT;
926  goto error;
927  }
928  }
929  }
930  }
931 #if (USE_SPI_CRC != 0U)
932  /* Enable CRC Transmission */
933  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
934  {
935  SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
936  }
937 #endif /* USE_SPI_CRC */
938 
939  /* Check the end of the transaction */
940  if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK)
941  {
942  hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
943  }
944 
945  /* Clear overrun flag in 2 Lines communication mode because received is not read */
946  if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
947  {
948  __HAL_SPI_CLEAR_OVRFLAG(hspi);
949  }
950 
951  if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
952  {
953  errorcode = HAL_ERROR;
954  }
955 
956 error:
957  hspi->State = HAL_SPI_STATE_READY;
958  /* Process Unlocked */
959  __HAL_UNLOCK(hspi);
960  return errorcode;
961 }
SPI_InitTypeDef Init
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
__HAL_UNLOCK(hrtc)
static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
Handle the check of the RXTX or TX transaction complete.
__HAL_LOCK(hrtc)
return HAL_OK
__IO uint16_t RxXferCount
__IO HAL_SPI_StateTypeDef State
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
__IO uint16_t TxXferCount
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_SPI_Transmit_DMA()

HAL_StatusTypeDef HAL_SPI_Transmit_DMA ( SPI_HandleTypeDef hspi,
uint8_t *  pData,
uint16_t  Size 
)

Transmit an amount of data in non-blocking mode with DMA.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
pDatapointer to data buffer
Sizeamount of data to be sent
Return values
HALstatus

Definition at line 1792 of file stm32l4xx_hal_spi.c.

1793 {
1794  HAL_StatusTypeDef errorcode = HAL_OK;
1795 
1796  /* Check tx dma handle */
1797  assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
1798 
1799  /* Check Direction parameter */
1800  assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
1801 
1802  /* Process Locked */
1803  __HAL_LOCK(hspi);
1804 
1805  if (hspi->State != HAL_SPI_STATE_READY)
1806  {
1807  errorcode = HAL_BUSY;
1808  goto error;
1809  }
1810 
1811  if ((pData == NULL) || (Size == 0U))
1812  {
1813  errorcode = HAL_ERROR;
1814  goto error;
1815  }
1816 
1817  /* Set the transaction information */
1818  hspi->State = HAL_SPI_STATE_BUSY_TX;
1819  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
1820  hspi->pTxBuffPtr = (uint8_t *)pData;
1821  hspi->TxXferSize = Size;
1822  hspi->TxXferCount = Size;
1823 
1824  /* Init field not used in handle to zero */
1825  hspi->pRxBuffPtr = (uint8_t *)NULL;
1826  hspi->TxISR = NULL;
1827  hspi->RxISR = NULL;
1828  hspi->RxXferSize = 0U;
1829  hspi->RxXferCount = 0U;
1830 
1831  /* Configure communication direction : 1Line */
1832  if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1833  {
1834  SPI_1LINE_TX(hspi);
1835  }
1836 
1837 #if (USE_SPI_CRC != 0U)
1838  /* Reset CRC Calculation */
1839  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
1840  {
1841  SPI_RESET_CRC(hspi);
1842  }
1843 #endif /* USE_SPI_CRC */
1844 
1845  /* Set the SPI TxDMA Half transfer complete callback */
1847 
1848  /* Set the SPI TxDMA transfer complete callback */
1850 
1851  /* Set the DMA error callback */
1853 
1854  /* Set the DMA AbortCpltCallback */
1855  hspi->hdmatx->XferAbortCallback = NULL;
1856 
1857  CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
1858  /* Packing mode is enabled only if the DMA setting is HALWORD */
1859  if ((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD))
1860  {
1861  /* Check the even/odd of the data size + crc if enabled */
1862  if ((hspi->TxXferCount & 0x1U) == 0U)
1863  {
1864  CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
1865  hspi->TxXferCount = (hspi->TxXferCount >> 1U);
1866  }
1867  else
1868  {
1869  SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
1870  hspi->TxXferCount = (hspi->TxXferCount >> 1U) + 1U;
1871  }
1872  }
1873 
1874  /* Enable the Tx DMA Stream/Channel */
1875  if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount))
1876  {
1877  /* Update SPI error code */
1878  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
1879  errorcode = HAL_ERROR;
1880 
1881  hspi->State = HAL_SPI_STATE_READY;
1882  goto error;
1883  }
1884 
1885  /* Check if the SPI is already enabled */
1886  if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1887  {
1888  /* Enable SPI peripheral */
1889  __HAL_SPI_ENABLE(hspi);
1890  }
1891 
1892  /* Enable the SPI Error Interrupt Bit */
1893  __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
1894 
1895  /* Enable Tx DMA Request */
1896  SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
1897 
1898 error :
1899  /* Process Unlocked */
1900  __HAL_UNLOCK(hspi);
1901  return errorcode;
1902 }
void(* XferAbortCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferCpltCallback)(struct __DMA_HandleTypeDef *hdma)
SPI_InitTypeDef Init
DMA_HandleTypeDef * hdmatx
void(* XferErrorCallback)(struct __DMA_HandleTypeDef *hdma)
__HAL_UNLOCK(hrtc)
void(* XferHalfCpltCallback)(struct __DMA_HandleTypeDef *hdma)
DMA_InitTypeDef Init
static void SPI_DMAError(DMA_HandleTypeDef *hdma)
DMA SPI communication error callback.
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
Start the DMA Transfer with interrupt enabled.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
return HAL_OK
__IO uint16_t RxXferCount
__IO HAL_SPI_StateTypeDef State
static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma)
DMA SPI half transmit process complete callback.
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma)
DMA SPI transmit process complete callback.
__IO uint16_t TxXferCount
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_SPI_Transmit_IT()

HAL_StatusTypeDef HAL_SPI_Transmit_IT ( SPI_HandleTypeDef hspi,
uint8_t *  pData,
uint16_t  Size 
)

Transmit an amount of data in non-blocking mode with Interrupt.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
pDatapointer to data buffer
Sizeamount of data to be sent
Return values
HALstatus

Definition at line 1495 of file stm32l4xx_hal_spi.c.

1496 {
1497  HAL_StatusTypeDef errorcode = HAL_OK;
1498 
1499  /* Check Direction parameter */
1500  assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
1501 
1502  /* Process Locked */
1503  __HAL_LOCK(hspi);
1504 
1505  if ((pData == NULL) || (Size == 0U))
1506  {
1507  errorcode = HAL_ERROR;
1508  goto error;
1509  }
1510 
1511  if (hspi->State != HAL_SPI_STATE_READY)
1512  {
1513  errorcode = HAL_BUSY;
1514  goto error;
1515  }
1516 
1517  /* Set the transaction information */
1518  hspi->State = HAL_SPI_STATE_BUSY_TX;
1519  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
1520  hspi->pTxBuffPtr = (uint8_t *)pData;
1521  hspi->TxXferSize = Size;
1522  hspi->TxXferCount = Size;
1523 
1524  /* Init field not used in handle to zero */
1525  hspi->pRxBuffPtr = (uint8_t *)NULL;
1526  hspi->RxXferSize = 0U;
1527  hspi->RxXferCount = 0U;
1528  hspi->RxISR = NULL;
1529 
1530  /* Set the function for IT treatment */
1531  if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1532  {
1533  hspi->TxISR = SPI_TxISR_16BIT;
1534  }
1535  else
1536  {
1537  hspi->TxISR = SPI_TxISR_8BIT;
1538  }
1539 
1540  /* Configure communication direction : 1Line */
1541  if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1542  {
1543  SPI_1LINE_TX(hspi);
1544  }
1545 
1546 #if (USE_SPI_CRC != 0U)
1547  /* Reset CRC Calculation */
1548  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
1549  {
1550  SPI_RESET_CRC(hspi);
1551  }
1552 #endif /* USE_SPI_CRC */
1553 
1554  /* Enable TXE and ERR interrupt */
1555  __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
1556 
1557 
1558  /* Check if the SPI is already enabled */
1559  if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1560  {
1561  /* Enable SPI peripheral */
1562  __HAL_SPI_ENABLE(hspi);
1563  }
1564 
1565 error :
1566  __HAL_UNLOCK(hspi);
1567  return errorcode;
1568 }
SPI_InitTypeDef Init
__HAL_UNLOCK(hrtc)
static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
Handle the data 16-bit transmit in Interrupt mode.
__HAL_LOCK(hrtc)
return HAL_OK
__IO uint16_t RxXferCount
__IO HAL_SPI_StateTypeDef State
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
__IO uint16_t TxXferCount
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)
static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
Handle the data 8-bit transmit in Interrupt mode.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_SPI_TransmitReceive()

HAL_StatusTypeDef HAL_SPI_TransmitReceive ( SPI_HandleTypeDef hspi,
uint8_t *  pTxData,
uint8_t *  pRxData,
uint16_t  Size,
uint32_t  Timeout 
)

Transmit and Receive an amount of data in blocking mode.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
pTxDatapointer to transmission data buffer
pRxDatapointer to reception data buffer
Sizeamount of data to be sent and received
TimeoutTimeout duration
Return values
HALstatus

Definition at line 1197 of file stm32l4xx_hal_spi.c.

1199 {
1200  uint16_t initial_TxXferCount;
1201  uint16_t initial_RxXferCount;
1202  uint32_t tmp_mode;
1203  HAL_SPI_StateTypeDef tmp_state;
1204  uint32_t tickstart;
1205 #if (USE_SPI_CRC != 0U)
1206  uint32_t spi_cr1;
1207  uint32_t spi_cr2;
1208 #endif /* USE_SPI_CRC */
1209 
1210  /* Variable used to alternate Rx and Tx during transfer */
1211  uint32_t txallowed = 1U;
1212  HAL_StatusTypeDef errorcode = HAL_OK;
1213 
1214  /* Check Direction parameter */
1215  assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
1216 
1217  /* Process Locked */
1218  __HAL_LOCK(hspi);
1219 
1220  /* Init tickstart for timeout management*/
1221  tickstart = HAL_GetTick();
1222 
1223  /* Init temporary variables */
1224  tmp_state = hspi->State;
1225  tmp_mode = hspi->Init.Mode;
1226  initial_TxXferCount = Size;
1227  initial_RxXferCount = Size;
1228 #if (USE_SPI_CRC != 0U)
1229  spi_cr1 = READ_REG(hspi->Instance->CR1);
1230  spi_cr2 = READ_REG(hspi->Instance->CR2);
1231 #endif /* USE_SPI_CRC */
1232 
1233  if (!((tmp_state == HAL_SPI_STATE_READY) || \
1234  ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
1235  {
1236  errorcode = HAL_BUSY;
1237  goto error;
1238  }
1239 
1240  if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
1241  {
1242  errorcode = HAL_ERROR;
1243  goto error;
1244  }
1245 
1246  /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
1247  if (hspi->State != HAL_SPI_STATE_BUSY_RX)
1248  {
1250  }
1251 
1252  /* Set the transaction information */
1253  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
1254  hspi->pRxBuffPtr = (uint8_t *)pRxData;
1255  hspi->RxXferCount = Size;
1256  hspi->RxXferSize = Size;
1257  hspi->pTxBuffPtr = (uint8_t *)pTxData;
1258  hspi->TxXferCount = Size;
1259  hspi->TxXferSize = Size;
1260 
1261  /*Init field not used in handle to zero */
1262  hspi->RxISR = NULL;
1263  hspi->TxISR = NULL;
1264 
1265 #if (USE_SPI_CRC != 0U)
1266  /* Reset CRC Calculation */
1267  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
1268  {
1269  SPI_RESET_CRC(hspi);
1270  }
1271 #endif /* USE_SPI_CRC */
1272 
1273  /* Set the Rx Fifo threshold */
1274  if ((hspi->Init.DataSize > SPI_DATASIZE_8BIT) || (initial_RxXferCount > 1U))
1275  {
1276  /* Set fiforxthreshold according the reception data length: 16bit */
1277  CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1278  }
1279  else
1280  {
1281  /* Set fiforxthreshold according the reception data length: 8bit */
1282  SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1283  }
1284 
1285  /* Check if the SPI is already enabled */
1286  if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1287  {
1288  /* Enable SPI peripheral */
1289  __HAL_SPI_ENABLE(hspi);
1290  }
1291 
1292  /* Transmit and Receive data in 16 Bit mode */
1293  if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1294  {
1295  if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
1296  {
1297  hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
1298  hspi->pTxBuffPtr += sizeof(uint16_t);
1299  hspi->TxXferCount--;
1300  }
1301  while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))
1302  {
1303  /* Check TXE flag */
1304  if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U))
1305  {
1306  hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
1307  hspi->pTxBuffPtr += sizeof(uint16_t);
1308  hspi->TxXferCount--;
1309  /* Next Data is a reception (Rx). Tx not allowed */
1310  txallowed = 0U;
1311 
1312 #if (USE_SPI_CRC != 0U)
1313  /* Enable CRC Transmission */
1314  if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
1315  {
1316  /* Set NSS Soft to received correctly the CRC on slave mode with NSS pulse activated */
1317  if ((READ_BIT(spi_cr1, SPI_CR1_MSTR) == 0U) && (READ_BIT(spi_cr2, SPI_CR2_NSSP) == SPI_CR2_NSSP))
1318  {
1319  SET_BIT(hspi->Instance->CR1, SPI_CR1_SSM);
1320  }
1321  SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
1322  }
1323 #endif /* USE_SPI_CRC */
1324  }
1325 
1326  /* Check RXNE flag */
1327  if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U))
1328  {
1329  *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
1330  hspi->pRxBuffPtr += sizeof(uint16_t);
1331  hspi->RxXferCount--;
1332  /* Next Data is a Transmission (Tx). Tx is allowed */
1333  txallowed = 1U;
1334  }
1335  if (((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY))
1336  {
1337  errorcode = HAL_TIMEOUT;
1338  goto error;
1339  }
1340  }
1341  }
1342  /* Transmit and Receive data in 8 Bit mode */
1343  else
1344  {
1345  if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
1346  {
1347  if (hspi->TxXferCount > 1U)
1348  {
1349  hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
1350  hspi->pTxBuffPtr += sizeof(uint16_t);
1351  hspi->TxXferCount -= 2U;
1352  }
1353  else
1354  {
1355  *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
1356  hspi->pTxBuffPtr++;
1357  hspi->TxXferCount--;
1358  }
1359  }
1360  while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))
1361  {
1362  /* Check TXE flag */
1363  if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U))
1364  {
1365  if (hspi->TxXferCount > 1U)
1366  {
1367  hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
1368  hspi->pTxBuffPtr += sizeof(uint16_t);
1369  hspi->TxXferCount -= 2U;
1370  }
1371  else
1372  {
1373  *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
1374  hspi->pTxBuffPtr++;
1375  hspi->TxXferCount--;
1376  }
1377  /* Next Data is a reception (Rx). Tx not allowed */
1378  txallowed = 0U;
1379 
1380 #if (USE_SPI_CRC != 0U)
1381  /* Enable CRC Transmission */
1382  if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
1383  {
1384  /* Set NSS Soft to received correctly the CRC on slave mode with NSS pulse activated */
1385  if ((READ_BIT(spi_cr1, SPI_CR1_MSTR) == 0U) && (READ_BIT(spi_cr2, SPI_CR2_NSSP) == SPI_CR2_NSSP))
1386  {
1387  SET_BIT(hspi->Instance->CR1, SPI_CR1_SSM);
1388  }
1389  SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
1390  }
1391 #endif /* USE_SPI_CRC */
1392  }
1393 
1394  /* Wait until RXNE flag is reset */
1395  if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U))
1396  {
1397  if (hspi->RxXferCount > 1U)
1398  {
1399  *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
1400  hspi->pRxBuffPtr += sizeof(uint16_t);
1401  hspi->RxXferCount -= 2U;
1402  if (hspi->RxXferCount <= 1U)
1403  {
1404  /* Set RX Fifo threshold before to switch on 8 bit data size */
1405  SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1406  }
1407  }
1408  else
1409  {
1410  (*(uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
1411  hspi->pRxBuffPtr++;
1412  hspi->RxXferCount--;
1413  }
1414  /* Next Data is a Transmission (Tx). Tx is allowed */
1415  txallowed = 1U;
1416  }
1417  if ((((HAL_GetTick() - tickstart) >= Timeout) && ((Timeout != HAL_MAX_DELAY))) || (Timeout == 0U))
1418  {
1419  errorcode = HAL_TIMEOUT;
1420  goto error;
1421  }
1422  }
1423  }
1424 
1425 #if (USE_SPI_CRC != 0U)
1426  /* Read CRC from DR to close CRC calculation process */
1427  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
1428  {
1429  /* Wait until TXE flag */
1430  if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
1431  {
1432  /* Error on the CRC reception */
1433  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1434  errorcode = HAL_TIMEOUT;
1435  goto error;
1436  }
1437  /* Read CRC */
1438  if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
1439  {
1440  /* Read 16bit CRC */
1441  READ_REG(hspi->Instance->DR);
1442  }
1443  else
1444  {
1445  /* Read 8bit CRC */
1446  READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
1447 
1448  if (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
1449  {
1450  if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
1451  {
1452  /* Error on the CRC reception */
1453  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1454  errorcode = HAL_TIMEOUT;
1455  goto error;
1456  }
1457  /* Read 8bit CRC again in case of 16bit CRC in 8bit Data mode */
1458  READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
1459  }
1460  }
1461  }
1462 
1463  /* Check if CRC error occurred */
1464  if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR))
1465  {
1466  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1467  /* Clear CRC Flag */
1468  __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
1469 
1470  errorcode = HAL_ERROR;
1471  }
1472 #endif /* USE_SPI_CRC */
1473 
1474  /* Check the end of the transaction */
1475  if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK)
1476  {
1477  errorcode = HAL_ERROR;
1478  hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
1479  }
1480 
1481 error :
1482  hspi->State = HAL_SPI_STATE_READY;
1483  __HAL_UNLOCK(hspi);
1484  return errorcode;
1485 }
HAL_SPI_StateTypeDef
HAL SPI State structure definition.
static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State, uint32_t Timeout, uint32_t Tickstart)
Handle SPI Communication Timeout.
SPI_InitTypeDef Init
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
__HAL_UNLOCK(hrtc)
static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
Handle the check of the RXTX or TX transaction complete.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
return HAL_OK
__IO uint16_t RxXferCount
__IO HAL_SPI_StateTypeDef State
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
__IO uint16_t TxXferCount
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_SPI_TransmitReceive_DMA()

HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA ( SPI_HandleTypeDef hspi,
uint8_t *  pTxData,
uint8_t *  pRxData,
uint16_t  Size 
)

Transmit and Receive an amount of data in non-blocking mode with DMA.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
pTxDatapointer to transmission data buffer
pRxDatapointer to reception data buffer
Note
When the CRC feature is enabled the pRxData Length must be Size + 1
Parameters
Sizeamount of data to be sent
Return values
HALstatus

Definition at line 2056 of file stm32l4xx_hal_spi.c.

2058 {
2059  uint32_t tmp_mode;
2060  HAL_SPI_StateTypeDef tmp_state;
2061  HAL_StatusTypeDef errorcode = HAL_OK;
2062 
2063  /* Check rx & tx dma handles */
2064  assert_param(IS_SPI_DMA_HANDLE(hspi->hdmarx));
2065  assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
2066 
2067  /* Check Direction parameter */
2068  assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
2069 
2070  /* Process locked */
2071  __HAL_LOCK(hspi);
2072 
2073  /* Init temporary variables */
2074  tmp_state = hspi->State;
2075  tmp_mode = hspi->Init.Mode;
2076 
2077  if (!((tmp_state == HAL_SPI_STATE_READY) ||
2078  ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
2079  {
2080  errorcode = HAL_BUSY;
2081  goto error;
2082  }
2083 
2084  if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
2085  {
2086  errorcode = HAL_ERROR;
2087  goto error;
2088  }
2089 
2090  /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
2091  if (hspi->State != HAL_SPI_STATE_BUSY_RX)
2092  {
2094  }
2095 
2096  /* Set the transaction information */
2097  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
2098  hspi->pTxBuffPtr = (uint8_t *)pTxData;
2099  hspi->TxXferSize = Size;
2100  hspi->TxXferCount = Size;
2101  hspi->pRxBuffPtr = (uint8_t *)pRxData;
2102  hspi->RxXferSize = Size;
2103  hspi->RxXferCount = Size;
2104 
2105  /* Init field not used in handle to zero */
2106  hspi->RxISR = NULL;
2107  hspi->TxISR = NULL;
2108 
2109 #if (USE_SPI_CRC != 0U)
2110  /* Reset CRC Calculation */
2111  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
2112  {
2113  SPI_RESET_CRC(hspi);
2114  }
2115 #endif /* USE_SPI_CRC */
2116 
2117  /* Reset the threshold bit */
2118  CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX | SPI_CR2_LDMARX);
2119 
2120  /* The packing mode management is enabled by the DMA settings according the spi data size */
2121  if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
2122  {
2123  /* Set fiforxthreshold according the reception data length: 16bit */
2124  CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
2125  }
2126  else
2127  {
2128  /* Set RX Fifo threshold according the reception data length: 8bit */
2129  SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
2130 
2131  if (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
2132  {
2133  if ((hspi->TxXferSize & 0x1U) == 0x0U)
2134  {
2135  CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
2136  hspi->TxXferCount = hspi->TxXferCount >> 1U;
2137  }
2138  else
2139  {
2140  SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
2141  hspi->TxXferCount = (hspi->TxXferCount >> 1U) + 1U;
2142  }
2143  }
2144 
2145  if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
2146  {
2147  /* Set RX Fifo threshold according the reception data length: 16bit */
2148  CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
2149 
2150  if ((hspi->RxXferCount & 0x1U) == 0x0U)
2151  {
2152  CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
2153  hspi->RxXferCount = hspi->RxXferCount >> 1U;
2154  }
2155  else
2156  {
2157  SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
2158  hspi->RxXferCount = (hspi->RxXferCount >> 1U) + 1U;
2159  }
2160  }
2161  }
2162 
2163  /* Check if we are in Rx only or in Rx/Tx Mode and configure the DMA transfer complete callback */
2164  if (hspi->State == HAL_SPI_STATE_BUSY_RX)
2165  {
2166  /* Set the SPI Rx DMA Half transfer complete callback */
2169  }
2170  else
2171  {
2172  /* Set the SPI Tx/Rx DMA Half transfer complete callback */
2175  }
2176 
2177  /* Set the DMA error callback */
2179 
2180  /* Set the DMA AbortCpltCallback */
2181  hspi->hdmarx->XferAbortCallback = NULL;
2182 
2183  /* Enable the Rx DMA Stream/Channel */
2184  if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount))
2185  {
2186  /* Update SPI error code */
2187  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2188  errorcode = HAL_ERROR;
2189 
2190  hspi->State = HAL_SPI_STATE_READY;
2191  goto error;
2192  }
2193 
2194  /* Enable Rx DMA Request */
2195  SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
2196 
2197  /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing
2198  is performed in DMA reception complete callback */
2199  hspi->hdmatx->XferHalfCpltCallback = NULL;
2200  hspi->hdmatx->XferCpltCallback = NULL;
2201  hspi->hdmatx->XferErrorCallback = NULL;
2202  hspi->hdmatx->XferAbortCallback = NULL;
2203 
2204  /* Enable the Tx DMA Stream/Channel */
2205  if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount))
2206  {
2207  /* Update SPI error code */
2208  SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2209  errorcode = HAL_ERROR;
2210 
2211  hspi->State = HAL_SPI_STATE_READY;
2212  goto error;
2213  }
2214 
2215  /* Check if the SPI is already enabled */
2216  if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
2217  {
2218  /* Enable SPI peripheral */
2219  __HAL_SPI_ENABLE(hspi);
2220  }
2221  /* Enable the SPI Error Interrupt Bit */
2222  __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
2223 
2224  /* Enable Tx DMA Request */
2225  SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
2226 
2227 error :
2228  /* Process Unlocked */
2229  __HAL_UNLOCK(hspi);
2230  return errorcode;
2231 }
HAL_SPI_StateTypeDef
HAL SPI State structure definition.
void(* XferAbortCallback)(struct __DMA_HandleTypeDef *hdma)
static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma)
DMA SPI transmit receive process complete callback.
static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
DMA SPI receive process complete callback.
void(* XferCpltCallback)(struct __DMA_HandleTypeDef *hdma)
SPI_InitTypeDef Init
DMA_HandleTypeDef * hdmatx
void(* XferErrorCallback)(struct __DMA_HandleTypeDef *hdma)
__HAL_UNLOCK(hrtc)
void(* XferHalfCpltCallback)(struct __DMA_HandleTypeDef *hdma)
DMA_InitTypeDef Init
static void SPI_DMAError(DMA_HandleTypeDef *hdma)
DMA SPI communication error callback.
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
Start the DMA Transfer with interrupt enabled.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma)
DMA SPI half receive process complete callback.
return HAL_OK
DMA_HandleTypeDef * hdmarx
__IO uint16_t RxXferCount
__IO HAL_SPI_StateTypeDef State
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
__IO uint16_t TxXferCount
static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma)
DMA SPI half transmit receive process complete callback.
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_SPI_TransmitReceive_IT()

HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT ( SPI_HandleTypeDef hspi,
uint8_t *  pTxData,
uint8_t *  pRxData,
uint16_t  Size 
)

Transmit and Receive an amount of data in non-blocking mode with Interrupt.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
pTxDatapointer to transmission data buffer
pRxDatapointer to reception data buffer
Sizeamount of data to be sent and received
Return values
HALstatus

Definition at line 1683 of file stm32l4xx_hal_spi.c.

1684 {
1685  uint32_t tmp_mode;
1686  HAL_SPI_StateTypeDef tmp_state;
1687  HAL_StatusTypeDef errorcode = HAL_OK;
1688 
1689  /* Check Direction parameter */
1690  assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
1691 
1692  /* Process locked */
1693  __HAL_LOCK(hspi);
1694 
1695  /* Init temporary variables */
1696  tmp_state = hspi->State;
1697  tmp_mode = hspi->Init.Mode;
1698 
1699  if (!((tmp_state == HAL_SPI_STATE_READY) || \
1700  ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
1701  {
1702  errorcode = HAL_BUSY;
1703  goto error;
1704  }
1705 
1706  if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
1707  {
1708  errorcode = HAL_ERROR;
1709  goto error;
1710  }
1711 
1712  /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
1713  if (hspi->State != HAL_SPI_STATE_BUSY_RX)
1714  {
1716  }
1717 
1718  /* Set the transaction information */
1719  hspi->ErrorCode = HAL_SPI_ERROR_NONE;
1720  hspi->pTxBuffPtr = (uint8_t *)pTxData;
1721  hspi->TxXferSize = Size;
1722  hspi->TxXferCount = Size;
1723  hspi->pRxBuffPtr = (uint8_t *)pRxData;
1724  hspi->RxXferSize = Size;
1725  hspi->RxXferCount = Size;
1726 
1727  /* Set the function for IT treatment */
1728  if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1729  {
1730  hspi->RxISR = SPI_2linesRxISR_16BIT;
1731  hspi->TxISR = SPI_2linesTxISR_16BIT;
1732  }
1733  else
1734  {
1735  hspi->RxISR = SPI_2linesRxISR_8BIT;
1736  hspi->TxISR = SPI_2linesTxISR_8BIT;
1737  }
1738 
1739 #if (USE_SPI_CRC != 0U)
1740  /* Reset CRC Calculation */
1741  if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
1742  {
1743  hspi->CRCSize = 1U;
1744  if ((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
1745  {
1746  hspi->CRCSize = 2U;
1747  }
1748  SPI_RESET_CRC(hspi);
1749  }
1750  else
1751  {
1752  hspi->CRCSize = 0U;
1753  }
1754 #endif /* USE_SPI_CRC */
1755 
1756  /* Check if packing mode is enabled and if there is more than 2 data to receive */
1757  if ((hspi->Init.DataSize > SPI_DATASIZE_8BIT) || (Size >= 2U))
1758  {
1759  /* Set RX Fifo threshold according the reception data length: 16 bit */
1760  CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1761  }
1762  else
1763  {
1764  /* Set RX Fifo threshold according the reception data length: 8 bit */
1765  SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
1766  }
1767 
1768  /* Enable TXE, RXNE and ERR interrupt */
1769  __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
1770 
1771  /* Check if the SPI is already enabled */
1772  if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
1773  {
1774  /* Enable SPI peripheral */
1775  __HAL_SPI_ENABLE(hspi);
1776  }
1777 
1778 error :
1779  /* Process Unlocked */
1780  __HAL_UNLOCK(hspi);
1781  return errorcode;
1782 }
HAL_SPI_StateTypeDef
HAL SPI State structure definition.
SPI_InitTypeDef Init
static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
Tx 16-bit handler for Transmit and Receive in Interrupt mode.
__HAL_UNLOCK(hrtc)
static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
Rx 8-bit handler for Transmit and Receive in Interrupt mode.
static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
Rx 16-bit handler for Transmit and Receive in Interrupt mode.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
Tx 8-bit handler for Transmit and Receive in Interrupt mode.
return HAL_OK
__IO uint16_t RxXferCount
__IO HAL_SPI_StateTypeDef State
void(* TxISR)(struct __SPI_HandleTypeDef *hspi)
__IO uint16_t TxXferCount
void(* RxISR)(struct __SPI_HandleTypeDef *hspi)
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_SPI_TxCpltCallback()

__weak void HAL_SPI_TxCpltCallback ( SPI_HandleTypeDef hspi)

Tx Transfer completed callback.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
Return values
None

Definition at line 2755 of file stm32l4xx_hal_spi.c.

2756 {
2757  /* Prevent unused argument(s) compilation warning */
2758  UNUSED(hspi);
2759 
2760  /* NOTE : This function should not be modified, when the callback is needed,
2761  the HAL_SPI_TxCpltCallback should be implemented in the user file
2762  */
2763 }

◆ HAL_SPI_TxHalfCpltCallback()

__weak void HAL_SPI_TxHalfCpltCallback ( SPI_HandleTypeDef hspi)

Tx Half Transfer completed callback.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
Return values
None

Definition at line 2803 of file stm32l4xx_hal_spi.c.

2804 {
2805  /* Prevent unused argument(s) compilation warning */
2806  UNUSED(hspi);
2807 
2808  /* NOTE : This function should not be modified, when the callback is needed,
2809  the HAL_SPI_TxHalfCpltCallback should be implemented in the user file
2810  */
2811 }

◆ HAL_SPI_TxRxCpltCallback()

__weak void HAL_SPI_TxRxCpltCallback ( SPI_HandleTypeDef hspi)

Tx and Rx Transfer completed callback.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
Return values
None

Definition at line 2787 of file stm32l4xx_hal_spi.c.

2788 {
2789  /* Prevent unused argument(s) compilation warning */
2790  UNUSED(hspi);
2791 
2792  /* NOTE : This function should not be modified, when the callback is needed,
2793  the HAL_SPI_TxRxCpltCallback should be implemented in the user file
2794  */
2795 }

◆ HAL_SPI_TxRxHalfCpltCallback()

__weak void HAL_SPI_TxRxHalfCpltCallback ( SPI_HandleTypeDef hspi)

Tx and Rx Half Transfer callback.

Parameters
hspipointer to a SPI_HandleTypeDef structure that contains the configuration information for SPI module.
Return values
None

Definition at line 2835 of file stm32l4xx_hal_spi.c.

2836 {
2837  /* Prevent unused argument(s) compilation warning */
2838  UNUSED(hspi);
2839 
2840  /* NOTE : This function should not be modified, when the callback is needed,
2841  the HAL_SPI_TxRxHalfCpltCallback() should be implemented in the user file
2842  */
2843 }