STM32F479xx HAL User Manual
stm32f4xx_hal_i2s_ex.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_hal_i2s_ex.c
00004   * @author  MCD Application Team
00005   * @brief   I2S HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of I2S extension peripheral:
00008   *           + Extension features Functions
00009   *
00010   @verbatim
00011   ==============================================================================
00012                     ##### I2S Extension features #####
00013   ==============================================================================
00014   [..]
00015      (#) In I2S full duplex mode, each SPI peripheral is able to manage sending and receiving
00016          data simultaneously using two data lines. Each SPI peripheral has an extended block
00017          called I2Sxext (i.e I2S2ext for SPI2 and I2S3ext for SPI3).
00018      (#) The extension block is not a full SPI IP, it is used only as I2S slave to
00019          implement full duplex mode. The extension block uses the same clock sources
00020          as its master.
00021 
00022      (#) Both I2Sx and I2Sx_ext can be configured as transmitters or receivers.
00023 
00024      [..]
00025        (@) Only I2Sx can deliver SCK and WS to I2Sx_ext in full duplex mode, where
00026          I2Sx can be I2S2 or I2S3.
00027 
00028                   ##### How to use this driver #####
00029  ===============================================================================
00030  [..]
00031    Three operation modes are available within this driver :
00032 
00033    *** Polling mode IO operation ***
00034    =================================
00035    [..]
00036      (+) Send and receive in the same time an amount of data in blocking mode using HAL_I2SEx_TransmitReceive()
00037 
00038    *** Interrupt mode IO operation ***
00039    ===================================
00040    [..]
00041      (+) Send and receive in the same time an amount of data in non blocking mode using HAL_I2SEx_TransmitReceive_IT()
00042      (+) At transmission/reception end of transfer HAL_I2SEx_TxRxCpltCallback is executed and user can
00043          add his own code by customization of function pointer HAL_I2SEx_TxRxCpltCallback
00044      (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
00045          add his own code by customization of function pointer HAL_I2S_ErrorCallback
00046 
00047    *** DMA mode IO operation ***
00048    ==============================
00049    [..]
00050      (+) Send and receive an amount of data in non blocking mode (DMA) using HAL_I2SEx_TransmitReceive_DMA()
00051      (+) At transmission/reception end of transfer HAL_I2SEx_TxRxCpltCallback is executed and user can
00052          add his own code by customization of function pointer HAL_I2S_TxRxCpltCallback
00053      (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
00054          add his own code by customization of function pointer HAL_I2S_ErrorCallback
00055      (+) __HAL_I2SEXT_FLUSH_RX_DR: In Full-Duplex Slave mode, if HAL_I2S_DMAStop is used to stop the
00056          communication, an error HAL_I2S_ERROR_BUSY_LINE_RX is raised as the master continue to transmit data.
00057          In this case __HAL_I2SEXT_FLUSH_RX_DR macro must be used to flush the remaining data
00058          inside I2Sx and I2Sx_ext DR registers and avoid using DeInit/Init process for the next transfer.
00059   @endverbatim
00060 
00061  Additional Figure: The Extended block uses the same clock sources as its master.
00062 
00063                 +-----------------------+
00064     I2Sx_SCK    |                       |
00065  ----------+-->|          I2Sx         |------------------->I2Sx_SD(in/out)
00066          +--|-->|                       |
00067         |   |   +-----------------------+
00068         |   |
00069  I2S_WS |   |
00070  ------>|   |
00071         |   |   +-----------------------+
00072         |   +-->|                       |
00073         |       |       I2Sx_ext        |------------------->I2Sx_extSD(in/out)
00074          +----->|                       |
00075                 +-----------------------+
00076   ******************************************************************************
00077   * @attention
00078   *
00079   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00080   * All rights reserved.</center></h2>
00081   *
00082   * This software component is licensed by ST under BSD 3-Clause license,
00083   * the "License"; You may not use this file except in compliance with the
00084   * License. You may obtain a copy of the License at:
00085   *                        opensource.org/licenses/BSD-3-Clause
00086   *
00087   ******************************************************************************
00088   */
00089 
00090 /* Includes ------------------------------------------------------------------*/
00091 #include "stm32f4xx_hal.h"
00092 
00093 /** @addtogroup STM32F4xx_HAL_Driver
00094   * @{
00095   */
00096 
00097 #ifdef HAL_I2S_MODULE_ENABLED
00098 
00099 /** @defgroup I2SEx I2SEx
00100   * @brief I2S Extended HAL module driver
00101   * @{
00102   */
00103 
00104 #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
00105 
00106 /* Private typedef -----------------------------------------------------------*/
00107 /** @defgroup I2SEx_Private_Typedef I2S Extended Private Typedef
00108   * @{
00109   */
00110 typedef enum
00111 {
00112   I2S_USE_I2S      = 0x00U,   /*!< I2Sx should be used      */
00113   I2S_USE_I2SEXT   = 0x01U,   /*!< I2Sx_ext should be used  */
00114 } I2S_UseTypeDef;
00115 /**
00116   * @}
00117   */
00118 /* Private define ------------------------------------------------------------*/
00119 /* Private macro -------------------------------------------------------------*/
00120 /* Private variables ---------------------------------------------------------*/
00121 /* Private function prototypes -----------------------------------------------*/
00122 /** @defgroup I2SEx_Private_Functions I2S Extended Private Functions
00123   * @{
00124   */
00125 static void I2SEx_TxRxDMAHalfCplt(DMA_HandleTypeDef *hdma);
00126 static void I2SEx_TxRxDMACplt(DMA_HandleTypeDef *hdma);
00127 static void I2SEx_TxRxDMAError(DMA_HandleTypeDef *hdma);
00128 static void I2SEx_RxISR_I2S(I2S_HandleTypeDef *hi2s);
00129 static void I2SEx_RxISR_I2SExt(I2S_HandleTypeDef *hi2s);
00130 static void I2SEx_TxISR_I2S(I2S_HandleTypeDef *hi2s);
00131 static void I2SEx_TxISR_I2SExt(I2S_HandleTypeDef *hi2s);
00132 static HAL_StatusTypeDef I2SEx_FullDuplexWaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s,
00133                                                                    uint32_t Flag,
00134                                                                    uint32_t State,
00135                                                                    uint32_t Timeout,
00136                                                                    I2S_UseTypeDef i2sUsed);
00137 /**
00138   * @}
00139   */
00140 
00141 /**
00142   * @}
00143   */
00144 
00145 /* Private functions ---------------------------------------------------------*/
00146 /* Exported functions --------------------------------------------------------*/
00147 
00148 /** @addtogroup I2SEx I2SEx
00149   * @{
00150   */
00151 
00152 /** @addtogroup I2SEx_Exported_Functions I2S Extended Exported Functions
00153   * @{
00154   */
00155 
00156 /** @defgroup I2SEx_Exported_Functions_Group1 I2S Extended IO operation functions
00157   *  @brief   I2SEx IO operation functions
00158   *
00159 @verbatim
00160  ===============================================================================
00161                        ##### IO operation functions#####
00162  ===============================================================================
00163     [..]
00164     This subsection provides a set of functions allowing to manage the I2S data
00165     transfers.
00166 
00167     (#) There are two modes of transfer:
00168        (++) Blocking mode : The communication is performed in the polling mode.
00169             The status of all data processing is returned by the same function
00170             after finishing transfer.
00171        (++) No-Blocking mode : The communication is performed using Interrupts
00172             or DMA. These functions return the status of the transfer startup.
00173             The end of the data processing will be indicated through the
00174             dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
00175             using DMA mode.
00176 
00177     (#) Blocking mode functions are :
00178         (++) HAL_I2SEx_TransmitReceive()
00179 
00180     (#) No-Blocking mode functions with Interrupt are :
00181         (++) HAL_I2SEx_TransmitReceive_IT()
00182         (++) HAL_I2SEx_FullDuplex_IRQHandler()
00183 
00184     (#) No-Blocking mode functions with DMA are :
00185         (++) HAL_I2SEx_TransmitReceive_DMA()
00186 
00187     (#) A set of Transfer Complete Callback are provided in non Blocking mode:
00188         (++) HAL_I2SEx_TxRxCpltCallback()
00189 @endverbatim
00190   * @{
00191   */
00192 /**
00193   * @brief  Full-Duplex Transmit/Receive data in blocking mode.
00194   * @param  hi2s pointer to a I2S_HandleTypeDef structure that contains
00195   *         the configuration information for I2S module
00196   * @param  pTxData a 16-bit pointer to the Transmit data buffer.
00197   * @param  pRxData a 16-bit pointer to the Receive data buffer.
00198   * @param  Size number of data sample to be sent:
00199   * @note   When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
00200   *         configuration phase, the Size parameter means the number of 16-bit data length
00201   *         in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
00202   *         the Size parameter means the number of 16-bit data length.
00203   * @param  Timeout Timeout duration
00204   * @note   The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
00205   *         between Master and Slave(example: audio streaming).
00206   * @retval HAL status
00207   */
00208 HAL_StatusTypeDef HAL_I2SEx_TransmitReceive(I2S_HandleTypeDef *hi2s,
00209                                             uint16_t *pTxData,
00210                                             uint16_t *pRxData,
00211                                             uint16_t Size,
00212                                             uint32_t Timeout)
00213 {
00214   uint32_t tmp1 = 0U;
00215   HAL_StatusTypeDef errorcode = HAL_OK;
00216 
00217   if (hi2s->State != HAL_I2S_STATE_READY)
00218   {
00219     errorcode = HAL_BUSY;
00220     goto error;
00221   }
00222 
00223   if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
00224   {
00225     return  HAL_ERROR;
00226   }
00227 
00228   /* Process Locked */
00229   __HAL_LOCK(hi2s);
00230 
00231   tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
00232   /* Check the Data format: When a 16-bit data frame or a 16-bit data frame extended
00233      is selected during the I2S configuration phase, the Size parameter means the number
00234      of 16-bit data length in the transaction and when a 24-bit data frame or a 32-bit data
00235      frame is selected the Size parameter means the number of 16-bit data length. */
00236   if ((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
00237   {
00238     hi2s->TxXferSize  = (Size << 1U);
00239     hi2s->TxXferCount = (Size << 1U);
00240     hi2s->RxXferSize  = (Size << 1U);
00241     hi2s->RxXferCount = (Size << 1U);
00242   }
00243   else
00244   {
00245     hi2s->TxXferSize  = Size;
00246     hi2s->TxXferCount = Size;
00247     hi2s->RxXferSize  = Size;
00248     hi2s->RxXferCount = Size;
00249   }
00250 
00251   /* Set state and reset error code */
00252   hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
00253   hi2s->State = HAL_I2S_STATE_BUSY_TX_RX;
00254 
00255   tmp1 = hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG;
00256   /* Check if the I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX Mode is selected */
00257   if ((tmp1 == I2S_MODE_MASTER_TX) || (tmp1 == I2S_MODE_SLAVE_TX))
00258   {
00259     /* Prepare the First Data before enabling the I2S */
00260     hi2s->Instance->DR = (*pTxData++);
00261     hi2s->TxXferCount--;
00262 
00263     /* Enable I2Sext(receiver) before enabling I2Sx peripheral */
00264     __HAL_I2SEXT_ENABLE(hi2s);
00265 
00266     /* Enable I2Sx peripheral */
00267     __HAL_I2S_ENABLE(hi2s);
00268 
00269     /* Check if Master Receiver mode is selected */
00270     if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_TX)
00271     {
00272       /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
00273       access to the SPI_SR register. */
00274       __HAL_I2SEXT_CLEAR_OVRFLAG(hi2s);
00275     }
00276 
00277     while ((hi2s->RxXferCount > 0U) || (hi2s->TxXferCount > 0U))
00278     {
00279       if (hi2s->TxXferCount > 0U)
00280       {
00281         /* Wait until TXE flag is set */
00282         if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout, I2S_USE_I2S) != HAL_OK)
00283         {
00284           /* Set the error code */
00285           SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
00286           errorcode = HAL_ERROR;
00287           goto error;
00288         }
00289         /* Write Data on DR register */
00290         hi2s->Instance->DR = (*pTxData++);
00291         hi2s->TxXferCount--;
00292 
00293         /* Check if an underrun occurs */
00294         if ((__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET) && (tmp1 == I2S_MODE_SLAVE_TX))
00295         {
00296           /* Clear Underrun flag */
00297           __HAL_I2S_CLEAR_UDRFLAG(hi2s);
00298 
00299           /* Set the error code */
00300           SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
00301         }
00302       }
00303       if (hi2s->RxXferCount > 0U)
00304       {
00305         /* Wait until RXNE flag is set */
00306         if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout, I2S_USE_I2SEXT) != HAL_OK)
00307         {
00308           /* Set the error code */
00309           SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
00310           errorcode = HAL_ERROR;
00311           goto error;
00312         }
00313         /* Read Data from DR register */
00314         (*pRxData++) = I2SxEXT(hi2s->Instance)->DR;
00315         hi2s->RxXferCount--;
00316 
00317         /* Check if an overrun occurs */
00318         if (__HAL_I2SEXT_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
00319         {
00320           /* Clear Overrun flag */
00321           __HAL_I2S_CLEAR_OVRFLAG(hi2s);
00322 
00323           /* Set the error code */
00324           SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
00325         }
00326       }
00327     }
00328   }
00329   /* The I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX Mode is selected */
00330   else
00331   {
00332     /* Prepare the First Data before enabling the I2S */
00333     I2SxEXT(hi2s->Instance)->DR = (*pTxData++);
00334     hi2s->TxXferCount--;
00335 
00336     /* Enable I2Sext(transmitter) after enabling I2Sx peripheral */
00337     __HAL_I2SEXT_ENABLE(hi2s);
00338 
00339     /* Enable I2S peripheral before the I2Sext*/
00340     __HAL_I2S_ENABLE(hi2s);
00341 
00342     /* Check if Master Receiver mode is selected */
00343     if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
00344     {
00345       /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
00346       access to the SPI_SR register. */
00347       __HAL_I2S_CLEAR_OVRFLAG(hi2s);
00348     }
00349 
00350     while ((hi2s->RxXferCount > 0U) || (hi2s->TxXferCount > 0U))
00351     {
00352       if (hi2s->TxXferCount > 0U)
00353       {
00354         /* Wait until TXE flag is set */
00355         if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout, I2S_USE_I2SEXT) != HAL_OK)
00356         {
00357           /* Set the error code */
00358           SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
00359           errorcode = HAL_ERROR;
00360           goto error;
00361         }
00362         /* Write Data on DR register */
00363         I2SxEXT(hi2s->Instance)->DR = (*pTxData++);
00364         hi2s->TxXferCount--;
00365 
00366         /* Check if an underrun occurs */
00367         if ((__HAL_I2SEXT_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET) && (tmp1 == I2S_MODE_SLAVE_RX))
00368         {
00369           /* Clear Underrun flag */
00370           __HAL_I2S_CLEAR_UDRFLAG(hi2s);
00371 
00372           /* Set the error code */
00373           SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
00374         }
00375       }
00376       if (hi2s->RxXferCount > 0U)
00377       {
00378         /* Wait until RXNE flag is set */
00379         if (I2SEx_FullDuplexWaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout, I2S_USE_I2S) != HAL_OK)
00380         {
00381           /* Set the error code */
00382           SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_TIMEOUT);
00383           errorcode = HAL_ERROR;
00384           goto error;
00385         }
00386         /* Read Data from DR register */
00387         (*pRxData++) = hi2s->Instance->DR;
00388         hi2s->RxXferCount--;
00389 
00390         /* Check if an overrun occurs */
00391         if (__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
00392         {
00393           /* Clear Overrun flag */
00394           __HAL_I2S_CLEAR_OVRFLAG(hi2s);
00395 
00396           /* Set the error code */
00397           SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
00398         }
00399       }
00400     }
00401   }
00402 
00403   if (hi2s->ErrorCode != HAL_I2S_ERROR_NONE)
00404   {
00405     errorcode = HAL_ERROR;
00406   }
00407 
00408 error :
00409   hi2s->State = HAL_I2S_STATE_READY;
00410   __HAL_UNLOCK(hi2s);
00411   return errorcode;
00412 }
00413 
00414 /**
00415   * @brief  Full-Duplex Transmit/Receive data in non-blocking mode using Interrupt
00416   * @param  hi2s pointer to a I2S_HandleTypeDef structure that contains
00417   *         the configuration information for I2S module
00418   * @param  pTxData a 16-bit pointer to the Transmit data buffer.
00419   * @param  pRxData a 16-bit pointer to the Receive data buffer.
00420   * @param  Size number of data sample to be sent:
00421   * @note   When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
00422   *         configuration phase, the Size parameter means the number of 16-bit data length
00423   *         in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
00424   *         the Size parameter means the number of 16-bit data length.
00425   * @note   The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
00426   *         between Master and Slave(example: audio streaming).
00427   * @retval HAL status
00428   */
00429 HAL_StatusTypeDef HAL_I2SEx_TransmitReceive_IT(I2S_HandleTypeDef *hi2s,
00430                                                uint16_t *pTxData,
00431                                                uint16_t *pRxData,
00432                                                uint16_t Size)
00433 {
00434   uint32_t tmp1 = 0U;
00435   HAL_StatusTypeDef errorcode = HAL_OK;
00436 
00437   if (hi2s->State != HAL_I2S_STATE_READY)
00438   {
00439     errorcode = HAL_BUSY;
00440     goto error;
00441   }
00442 
00443   if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
00444   {
00445     return  HAL_ERROR;
00446   }
00447 
00448   /* Process Locked */
00449   __HAL_LOCK(hi2s);
00450 
00451   hi2s->pTxBuffPtr = pTxData;
00452   hi2s->pRxBuffPtr = pRxData;
00453 
00454   tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
00455   /* Check the Data format: When a 16-bit data frame or a 16-bit data frame extended
00456   is selected during the I2S configuration phase, the Size parameter means the number
00457   of 16-bit data length in the transaction and when a 24-bit data frame or a 32-bit data
00458   frame is selected the Size parameter means the number of 16-bit data length. */
00459   if ((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
00460   {
00461     hi2s->TxXferSize  = (Size << 1U);
00462     hi2s->TxXferCount = (Size << 1U);
00463     hi2s->RxXferSize  = (Size << 1U);
00464     hi2s->RxXferCount = (Size << 1U);
00465   }
00466   else
00467   {
00468     hi2s->TxXferSize  = Size;
00469     hi2s->TxXferCount = Size;
00470     hi2s->RxXferSize  = Size;
00471     hi2s->RxXferCount = Size;
00472   }
00473 
00474   hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
00475   hi2s->State     = HAL_I2S_STATE_BUSY_TX_RX;
00476 
00477   /* Set the function for IT treatment */
00478   if ((hi2s->Init.Mode == I2S_MODE_MASTER_TX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_TX))
00479   {
00480     /* Enable I2Sext RXNE and ERR interrupts */
00481     __HAL_I2SEXT_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
00482 
00483     /* Enable I2Sx TXE and ERR interrupts */
00484     __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
00485 
00486     /* Transmit First data */
00487     hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
00488     hi2s->TxXferCount--;
00489 
00490     if (hi2s->TxXferCount == 0U)
00491     {
00492       /* Disable TXE and ERR interrupt */
00493       __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
00494     }
00495   }
00496   else  /* The I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX Mode is selected */
00497   {
00498     /* Enable I2Sext TXE and ERR interrupts */
00499     __HAL_I2SEXT_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
00500 
00501     /* Enable I2Sext RXNE and ERR interrupts */
00502     __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
00503 
00504     /* Transmit First data */
00505     I2SxEXT(hi2s->Instance)->DR = (*hi2s->pTxBuffPtr++);
00506     hi2s->TxXferCount--;
00507 
00508     if (hi2s->TxXferCount == 0U)
00509     {
00510       /* Disable I2Sext TXE and ERR interrupt */
00511       __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
00512     }
00513   }
00514 
00515   /* Enable I2Sext peripheral */
00516   __HAL_I2SEXT_ENABLE(hi2s);
00517 
00518   /* Enable I2S peripheral */
00519   __HAL_I2S_ENABLE(hi2s);
00520 
00521 error :
00522   __HAL_UNLOCK(hi2s);
00523   return errorcode;
00524 }
00525 
00526 /**
00527   * @brief  Full-Duplex Transmit/Receive data in non-blocking mode using DMA
00528   * @param  hi2s pointer to a I2S_HandleTypeDef structure that contains
00529   *         the configuration information for I2S module
00530   * @param  pTxData a 16-bit pointer to the Transmit data buffer.
00531   * @param  pRxData a 16-bit pointer to the Receive data buffer.
00532   * @param  Size number of data sample to be sent:
00533   * @note   When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
00534   *         configuration phase, the Size parameter means the number of 16-bit data length
00535   *         in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
00536   *         the Size parameter means the number of 16-bit data length.
00537   * @note   The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
00538   *         between Master and Slave(example: audio streaming).
00539   * @retval HAL status
00540   */
00541 HAL_StatusTypeDef HAL_I2SEx_TransmitReceive_DMA(I2S_HandleTypeDef *hi2s,
00542                                                 uint16_t *pTxData,
00543                                                 uint16_t *pRxData,
00544                                                 uint16_t Size)
00545 {
00546   uint32_t *tmp = NULL;
00547   uint32_t tmp1 = 0U;
00548   HAL_StatusTypeDef errorcode = HAL_OK;
00549 
00550   if (hi2s->State != HAL_I2S_STATE_READY)
00551   {
00552     errorcode = HAL_BUSY;
00553     goto error;
00554   }
00555 
00556   if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
00557   {
00558     return  HAL_ERROR;
00559   }
00560 
00561   /* Process Locked */
00562   __HAL_LOCK(hi2s);
00563 
00564   hi2s->pTxBuffPtr = pTxData;
00565   hi2s->pRxBuffPtr = pRxData;
00566 
00567   tmp1 = hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN);
00568   /* Check the Data format: When a 16-bit data frame or a 16-bit data frame extended
00569   is selected during the I2S configuration phase, the Size parameter means the number
00570   of 16-bit data length in the transaction and when a 24-bit data frame or a 32-bit data
00571   frame is selected the Size parameter means the number of 16-bit data length. */
00572   if ((tmp1 == I2S_DATAFORMAT_24B) || (tmp1 == I2S_DATAFORMAT_32B))
00573   {
00574     hi2s->TxXferSize  = (Size << 1U);
00575     hi2s->TxXferCount = (Size << 1U);
00576     hi2s->RxXferSize  = (Size << 1U);
00577     hi2s->RxXferCount = (Size << 1U);
00578   }
00579   else
00580   {
00581     hi2s->TxXferSize  = Size;
00582     hi2s->TxXferCount = Size;
00583     hi2s->RxXferSize  = Size;
00584     hi2s->RxXferCount = Size;
00585   }
00586 
00587   hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
00588   hi2s->State     = HAL_I2S_STATE_BUSY_TX_RX;
00589 
00590   /* Set the I2S Rx DMA Half transfer complete callback */
00591   hi2s->hdmarx->XferHalfCpltCallback = I2SEx_TxRxDMAHalfCplt;
00592 
00593   /* Set the I2S Rx DMA transfer complete callback */
00594   hi2s->hdmarx->XferCpltCallback  = I2SEx_TxRxDMACplt;
00595 
00596   /* Set the I2S Rx DMA error callback */
00597   hi2s->hdmarx->XferErrorCallback = I2SEx_TxRxDMAError;
00598 
00599   /* Set the I2S Tx DMA Half transfer complete callback as NULL */
00600   hi2s->hdmatx->XferHalfCpltCallback  = NULL;
00601 
00602   /* Set the I2S Tx DMA transfer complete callback as NULL */
00603   hi2s->hdmatx->XferCpltCallback  = NULL;
00604 
00605   /* Set the I2S Tx DMA error callback */
00606   hi2s->hdmatx->XferErrorCallback = I2SEx_TxRxDMAError;
00607 
00608   tmp1 = hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG;
00609   /* Check if the I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX Mode is selected */
00610   if ((tmp1 == I2S_MODE_MASTER_TX) || (tmp1 == I2S_MODE_SLAVE_TX))
00611   {
00612     /* Enable the Rx DMA Stream */
00613     tmp = (uint32_t *)&pRxData;
00614     HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&I2SxEXT(hi2s->Instance)->DR, *(uint32_t *)tmp, hi2s->RxXferSize);
00615 
00616     /* Enable Rx DMA Request */
00617     SET_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_RXDMAEN);
00618 
00619     /* Enable the Tx DMA Stream */
00620     tmp = (uint32_t *)&pTxData;
00621     HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t *)tmp, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize);
00622 
00623     /* Enable Tx DMA Request */
00624     SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
00625 
00626     /* Check if the I2S is already enabled */
00627     if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
00628     {
00629       /* Enable I2Sext(receiver) before enabling I2Sx peripheral */
00630       __HAL_I2SEXT_ENABLE(hi2s);
00631 
00632       /* Enable I2S peripheral after the I2Sext */
00633       __HAL_I2S_ENABLE(hi2s);
00634     }
00635   }
00636   else
00637   {
00638     /* Check if Master Receiver mode is selected */
00639     if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
00640     {
00641       /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
00642       access to the SPI_SR register. */
00643       __HAL_I2S_CLEAR_OVRFLAG(hi2s);
00644     }
00645     /* Enable the Tx DMA Stream */
00646     tmp = (uint32_t *)&pTxData;
00647     HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t *)tmp, (uint32_t)&I2SxEXT(hi2s->Instance)->DR, hi2s->TxXferSize);
00648 
00649     /* Enable Tx DMA Request */
00650     SET_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_TXDMAEN);
00651 
00652     /* Enable the Rx DMA Stream */
00653     tmp = (uint32_t *)&pRxData;
00654     HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, *(uint32_t *)tmp, hi2s->RxXferSize);
00655 
00656     /* Enable Rx DMA Request */
00657     SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
00658 
00659     /* Check if the I2S is already enabled */
00660     if ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
00661     {
00662       /* Enable I2Sext(transmitter) before enabling I2Sx peripheral */
00663       __HAL_I2SEXT_ENABLE(hi2s);
00664       /* Enable I2S peripheral before the I2Sext */
00665       __HAL_I2S_ENABLE(hi2s);
00666     }
00667   }
00668 
00669 error :
00670   __HAL_UNLOCK(hi2s);
00671   return errorcode;
00672 }
00673 
00674 /**
00675   * @brief  This function handles I2S/I2Sext interrupt requests in full-duplex mode.
00676   * @param  hi2s I2S handle
00677   * @retval HAL status
00678   */
00679 void HAL_I2SEx_FullDuplex_IRQHandler(I2S_HandleTypeDef *hi2s)
00680 {
00681   __IO uint32_t i2ssr     = hi2s->Instance->SR;
00682   __IO uint32_t i2sextsr  = I2SxEXT(hi2s->Instance)->SR;
00683   __IO uint32_t i2scr2    = hi2s->Instance->CR2;
00684   __IO uint32_t i2sextcr2 = I2SxEXT(hi2s->Instance)->CR2;
00685 
00686   /* Check if the I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX Mode is selected */
00687   if ((hi2s->Init.Mode == I2S_MODE_MASTER_TX) || (hi2s->Init.Mode == I2S_MODE_SLAVE_TX))
00688   {
00689     /* I2S in mode Transmitter -------------------------------------------------*/
00690     if (((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && ((i2scr2 & I2S_IT_TXE) != RESET))
00691     {
00692       /* When the I2S mode is configured as I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX,
00693       the I2S TXE interrupt will be generated to manage the full-duplex transmit phase. */
00694       I2SEx_TxISR_I2S(hi2s);
00695     }
00696 
00697     /* I2Sext in mode Receiver -----------------------------------------------*/
00698     if (((i2sextsr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && ((i2sextcr2 & I2S_IT_RXNE) != RESET))
00699     {
00700       /* When the I2S mode is configured as I2S_MODE_MASTER_TX or I2S_MODE_SLAVE_TX,
00701       the I2Sext RXNE interrupt will be generated to manage the full-duplex receive phase. */
00702       I2SEx_RxISR_I2SExt(hi2s);
00703     }
00704 
00705     /* I2Sext Overrun error interrupt occurred --------------------------------*/
00706     if (((i2sextsr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && ((i2sextcr2 & I2S_IT_ERR) != RESET))
00707     {
00708       /* Disable RXNE and ERR interrupt */
00709       __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
00710 
00711       /* Disable TXE and ERR interrupt */
00712       __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
00713 
00714       /* Clear Overrun flag */
00715       __HAL_I2S_CLEAR_OVRFLAG(hi2s);
00716 
00717       /* Set the I2S State ready */
00718       hi2s->State = HAL_I2S_STATE_READY;
00719 
00720       /* Set the error code and execute error callback*/
00721       SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
00722       /* Call user error callback */
00723 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
00724       hi2s->ErrorCallback(hi2s);
00725 #else
00726       HAL_I2S_ErrorCallback(hi2s);
00727 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
00728     }
00729 
00730     /* I2S Underrun error interrupt occurred ----------------------------------*/
00731     if (((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && ((i2scr2 & I2S_IT_ERR) != RESET))
00732     {
00733       /* Disable TXE and ERR interrupt */
00734       __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
00735 
00736       /* Disable RXNE and ERR interrupt */
00737       __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
00738 
00739       /* Clear underrun flag */
00740       __HAL_I2S_CLEAR_UDRFLAG(hi2s);
00741 
00742       /* Set the I2S State ready */
00743       hi2s->State = HAL_I2S_STATE_READY;
00744 
00745       /* Set the error code and execute error callback*/
00746       SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
00747       /* Call user error callback */
00748 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
00749       hi2s->ErrorCallback(hi2s);
00750 #else
00751       HAL_I2S_ErrorCallback(hi2s);
00752 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
00753     }
00754   }
00755   /* The I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX Mode is selected */
00756   else
00757   {
00758     /* I2Sext in mode Transmitter ----------------------------------------------*/
00759     if (((i2sextsr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && ((i2sextcr2 & I2S_IT_TXE) != RESET))
00760     {
00761       /* When the I2S mode is configured as I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX,
00762       the I2Sext TXE interrupt will be generated to manage the full-duplex transmit phase. */
00763       I2SEx_TxISR_I2SExt(hi2s);
00764     }
00765 
00766     /* I2S in mode Receiver --------------------------------------------------*/
00767     if (((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && ((i2scr2 & I2S_IT_RXNE) != RESET))
00768     {
00769       /* When the I2S mode is configured as I2S_MODE_MASTER_RX or I2S_MODE_SLAVE_RX,
00770       the I2S RXNE interrupt will be generated to manage the full-duplex receive phase. */
00771       I2SEx_RxISR_I2S(hi2s);
00772     }
00773 
00774     /* I2S Overrun error interrupt occurred -------------------------------------*/
00775     if (((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && ((i2scr2 & I2S_IT_ERR) != RESET))
00776     {
00777       /* Disable RXNE and ERR interrupt */
00778       __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
00779 
00780       /* Disable TXE and ERR interrupt */
00781       __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
00782 
00783       /* Set the I2S State ready */
00784       hi2s->State = HAL_I2S_STATE_READY;
00785 
00786       /* Set the error code and execute error callback*/
00787       SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_OVR);
00788       /* Call user error callback */
00789 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
00790       hi2s->ErrorCallback(hi2s);
00791 #else
00792       HAL_I2S_ErrorCallback(hi2s);
00793 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
00794     }
00795 
00796     /* I2Sext Underrun error interrupt occurred -------------------------------*/
00797     if (((i2sextsr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && ((i2sextcr2 & I2S_IT_ERR) != RESET))
00798     {
00799       /* Disable TXE and ERR interrupt */
00800       __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
00801 
00802       /* Disable RXNE and ERR interrupt */
00803       __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
00804 
00805       /* Set the I2S State ready */
00806       hi2s->State = HAL_I2S_STATE_READY;
00807 
00808       /* Set the error code and execute error callback*/
00809       SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_UDR);
00810       /* Call user error callback */
00811 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
00812       hi2s->ErrorCallback(hi2s);
00813 #else
00814       HAL_I2S_ErrorCallback(hi2s);
00815 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
00816     }
00817   }
00818 }
00819 
00820 /**
00821   * @brief  Tx and Rx Transfer half completed callback
00822   * @param  hi2s I2S handle
00823   * @retval None
00824   */
00825 __weak void HAL_I2SEx_TxRxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
00826 {
00827   /* Prevent unused argument(s) compilation warning */
00828   UNUSED(hi2s);
00829 
00830   /* NOTE : This function Should not be modified, when the callback is needed,
00831             the HAL_I2SEx_TxRxHalfCpltCallback could be implemented in the user file
00832    */
00833 }
00834 
00835 /**
00836   * @brief  Tx and Rx Transfer completed callback
00837   * @param  hi2s I2S handle
00838   * @retval None
00839   */
00840 __weak void HAL_I2SEx_TxRxCpltCallback(I2S_HandleTypeDef *hi2s)
00841 {
00842   /* Prevent unused argument(s) compilation warning */
00843   UNUSED(hi2s);
00844 
00845   /* NOTE : This function should not be modified, when the callback is needed,
00846             the HAL_I2SEx_TxRxCpltCallback could be implemented in the user file
00847    */
00848 }
00849 
00850 /**
00851   * @}
00852   */
00853 
00854 /**
00855   * @}
00856   */
00857 
00858 /** @addtogroup I2SEx_Private_Functions I2S Extended Private Functions
00859   * @{
00860   */
00861 
00862 /**
00863   * @brief  DMA I2S transmit receive process half complete callback
00864   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
00865   *               the configuration information for the specified DMA module.
00866   * @retval None
00867   */
00868 static void I2SEx_TxRxDMAHalfCplt(DMA_HandleTypeDef *hdma)
00869 {
00870   I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
00871 
00872   /* Call user TxRx Half complete callback */
00873 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
00874   hi2s->TxRxHalfCpltCallback(hi2s);
00875 #else
00876   HAL_I2SEx_TxRxHalfCpltCallback(hi2s);
00877 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
00878 }
00879 
00880 /**
00881   * @brief  DMA I2S transmit receive process complete callback
00882   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
00883   *               the configuration information for the specified DMA module.
00884   * @retval None
00885   */
00886 static void I2SEx_TxRxDMACplt(DMA_HandleTypeDef *hdma)
00887 {
00888   I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
00889 
00890   /* If DMA is configured in DMA_NORMAL mode */
00891   if (hdma->Init.Mode == DMA_NORMAL)
00892   {
00893     if (((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_TX) || \
00894         ((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_SLAVE_TX))
00895     /* Disable Tx & Rx DMA Requests */
00896     {
00897       CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_RXDMAEN);
00898       CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);
00899     }
00900     else
00901     {
00902       CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);
00903       CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, SPI_CR2_TXDMAEN);
00904     }
00905 
00906     hi2s->RxXferCount = 0U;
00907     hi2s->TxXferCount = 0U;
00908 
00909     hi2s->State = HAL_I2S_STATE_READY;
00910   }
00911 
00912   /* Call user TxRx complete callback */
00913 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
00914   hi2s->TxRxCpltCallback(hi2s);
00915 #else
00916   HAL_I2SEx_TxRxCpltCallback(hi2s);
00917 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
00918 }
00919 
00920 /**
00921   * @brief  DMA I2S communication error callback
00922   * @param  hdma DMA handle
00923   * @retval None
00924   */
00925 static void I2SEx_TxRxDMAError(DMA_HandleTypeDef *hdma)
00926 {
00927   I2S_HandleTypeDef *hi2s = (I2S_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
00928 
00929   /* Disable Rx and Tx DMA Request */
00930   CLEAR_BIT(hi2s->Instance->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
00931   CLEAR_BIT(I2SxEXT(hi2s->Instance)->CR2, (SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
00932 
00933   hi2s->TxXferCount = 0U;
00934   hi2s->RxXferCount = 0U;
00935 
00936   hi2s->State = HAL_I2S_STATE_READY;
00937 
00938   /* Set the error code and execute error callback*/
00939   SET_BIT(hi2s->ErrorCode, HAL_I2S_ERROR_DMA);
00940   /* Call user error callback */
00941 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
00942   hi2s->ErrorCallback(hi2s);
00943 #else
00944   HAL_I2S_ErrorCallback(hi2s);
00945 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
00946 }
00947 
00948 /**
00949   * @brief  I2S Full-Duplex IT handler transmit function
00950   * @param  hi2s I2S handle
00951   * @retval None
00952   */
00953 static void I2SEx_TxISR_I2S(I2S_HandleTypeDef *hi2s)
00954 {
00955   /* Write Data on DR register */
00956   hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
00957   hi2s->TxXferCount--;
00958 
00959   if (hi2s->TxXferCount == 0U)
00960   {
00961     /* Disable TXE and ERR interrupt */
00962     __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
00963 
00964     if (hi2s->RxXferCount == 0U)
00965     {
00966       hi2s->State = HAL_I2S_STATE_READY;
00967       /* Call user TxRx complete callback */
00968 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
00969       hi2s->TxRxCpltCallback(hi2s);
00970 #else
00971       HAL_I2SEx_TxRxCpltCallback(hi2s);
00972 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
00973     }
00974   }
00975 }
00976 
00977 /**
00978   * @brief  I2SExt Full-Duplex IT handler transmit function
00979   * @param  hi2s I2S handle
00980   * @retval None
00981   */
00982 static void I2SEx_TxISR_I2SExt(I2S_HandleTypeDef *hi2s)
00983 {
00984   /* Write Data on DR register */
00985   I2SxEXT(hi2s->Instance)->DR = (*hi2s->pTxBuffPtr++);
00986   hi2s->TxXferCount--;
00987 
00988   if (hi2s->TxXferCount == 0U)
00989   {
00990     /* Disable I2Sext TXE and ERR interrupt */
00991     __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
00992 
00993     if (hi2s->RxXferCount == 0U)
00994     {
00995       hi2s->State = HAL_I2S_STATE_READY;
00996       /* Call user TxRx complete callback */
00997 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
00998       hi2s->TxRxCpltCallback(hi2s);
00999 #else
01000       HAL_I2SEx_TxRxCpltCallback(hi2s);
01001 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
01002     }
01003   }
01004 }
01005 
01006 /**
01007   * @brief  I2S Full-Duplex IT handler receive function
01008   * @param  hi2s I2S handle
01009   * @retval None
01010   */
01011 static void I2SEx_RxISR_I2S(I2S_HandleTypeDef *hi2s)
01012 {
01013   /* Read Data from DR register */
01014   (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
01015   hi2s->RxXferCount--;
01016 
01017   if (hi2s->RxXferCount == 0U)
01018   {
01019     /* Disable RXNE and ERR interrupt */
01020     __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
01021 
01022     if (hi2s->TxXferCount == 0U)
01023     {
01024       hi2s->State = HAL_I2S_STATE_READY;
01025       /* Call user TxRx complete callback */
01026 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
01027       hi2s->TxRxCpltCallback(hi2s);
01028 #else
01029       HAL_I2SEx_TxRxCpltCallback(hi2s);
01030 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
01031     }
01032   }
01033 }
01034 
01035 /**
01036   * @brief  I2SExt Full-Duplex IT handler receive function
01037   * @param  hi2s I2S handle
01038   * @retval None
01039   */
01040 static void I2SEx_RxISR_I2SExt(I2S_HandleTypeDef *hi2s)
01041 {
01042   /* Read Data from DR register */
01043   (*hi2s->pRxBuffPtr++) = I2SxEXT(hi2s->Instance)->DR;
01044   hi2s->RxXferCount--;
01045 
01046   if (hi2s->RxXferCount == 0U)
01047   {
01048     /* Disable I2Sext RXNE and ERR interrupt */
01049     __HAL_I2SEXT_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
01050 
01051     if (hi2s->TxXferCount == 0U)
01052     {
01053       hi2s->State = HAL_I2S_STATE_READY;
01054       /* Call user TxRx complete callback */
01055 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
01056       hi2s->TxRxCpltCallback(hi2s);
01057 #else
01058       HAL_I2SEx_TxRxCpltCallback(hi2s);
01059 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
01060     }
01061   }
01062 }
01063 
01064 /**
01065   * @brief This function handles I2S Communication Timeout.
01066   * @param hi2s I2S handle
01067   * @param Flag Flag checked
01068   * @param State Value of the flag expected
01069   * @param Timeout Duration of the timeout
01070   * @param i2sUsed I2S instance reference
01071   * @retval HAL status
01072   */
01073 static HAL_StatusTypeDef I2SEx_FullDuplexWaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s,
01074                                                                    uint32_t Flag,
01075                                                                    uint32_t State,
01076                                                                    uint32_t Timeout,
01077                                                                    I2S_UseTypeDef i2sUsed)
01078 {
01079   uint32_t tickstart = HAL_GetTick();
01080 
01081   if (i2sUsed == I2S_USE_I2S)
01082   {
01083     /* Wait until flag is reset */
01084     while (((__HAL_I2S_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State)
01085     {
01086       if (Timeout != HAL_MAX_DELAY)
01087       {
01088         if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
01089         {
01090           /* Set the I2S State ready */
01091           hi2s->State = HAL_I2S_STATE_READY;
01092 
01093           /* Process Unlocked */
01094           __HAL_UNLOCK(hi2s);
01095 
01096           return HAL_TIMEOUT;
01097         }
01098       }
01099     }
01100   }
01101   else /* i2sUsed == I2S_USE_I2SEXT */
01102   {
01103     /* Wait until flag is reset */
01104     while (((__HAL_I2SEXT_GET_FLAG(hi2s, Flag)) ? SET : RESET) != State)
01105     {
01106       if (Timeout != HAL_MAX_DELAY)
01107       {
01108         if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
01109         {
01110           /* Set the I2S State ready */
01111           hi2s->State = HAL_I2S_STATE_READY;
01112 
01113           /* Process Unlocked */
01114           __HAL_UNLOCK(hi2s);
01115 
01116           return HAL_TIMEOUT;
01117         }
01118       }
01119     }
01120   }
01121   return HAL_OK;
01122 }
01123 
01124 /**
01125   * @}
01126   */
01127 #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
01128 
01129 /**
01130   * @}
01131   */
01132 #endif /* HAL_I2S_MODULE_ENABLED */
01133 
01134 /**
01135   * @}
01136   */
01137 
01138 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/