]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3/stm32f3xx_hal_i2s.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3 / stm32f3xx_hal_i2s.c
1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_hal_i2s.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    12-Sept-2014
7   * @brief   I2S HAL module driver.
8   *          This file provides firmware functions to manage the following 
9   *          functionalities of the Integrated Interchip Sound (I2S) peripheral:
10   *           + Initialization and de-initialization functions
11   *           + IO operation functions
12   *           + Peripheral State and Errors functions
13   @verbatim
14  ===============================================================================
15                   ##### How to use this driver #####
16  ===============================================================================
17  [..]
18     The I2S HAL driver can be used as follows:
19     
20     (#) Declare a I2S_HandleTypeDef handle structure.
21     (#) Initialize the I2S low level resources by implement the HAL_I2S_MspInit() API:
22         (##) Enable the SPIx interface clock.                      
23         (##) I2S pins configuration:
24             (+++) Enable the clock for the I2S GPIOs.
25             (+++) Configure these I2S pins as alternate function pull-up.
26         (##) NVIC configuration if you need to use interrupt process (HAL_I2S_Transmit_IT()
27              and HAL_I2S_Receive_IT() APIs).
28             (+++) Configure the I2Sx interrupt priority.
29             (+++) Enable the NVIC I2S IRQ handle.
30         (##) DMA Configuration if you need to use DMA process (HAL_I2S_Transmit_DMA()
31              and HAL_I2S_Receive_DMA() APIs:
32             (+++) Declare a DMA handle structure for the Tx/Rx channel.
33             (+++) Enable the DMAx interface clock.
34             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.                
35             (+++) Configure the DMA Tx/Rx Channel.
36             (+++) Associate the initilalized DMA handle to the I2S DMA Tx/Rx handle.
37             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the 
38                 DMA Tx/Rx Channel.
39   
40    (#) Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity
41        using HAL_I2S_Init() function.
42
43    -@- The specific I2S interrupts (Transmission complete interrupt, 
44        RXNE interrupt and Error Interrupts) will be managed using the macros
45        __HAL_I2S_ENABLE_IT() and __HAL_I2S_DISABLE_IT() inside the transmit and receive process.
46    -@- Make sure that either:
47        (+@) I2S clock is configured based on SYSCLK or 
48        (+@) External clock source is configured after setting correctly 
49             the define constant EXTERNAL_CLOCK_VALUE in the stm32f3xx_hal_conf.h file. 
50
51    (#) Three mode of operations are available within this driver :     
52   
53    *** Polling mode IO operation ***
54    =================================
55    [..]    
56      (+) Send an amount of data in blocking mode using HAL_I2S_Transmit() 
57      (+) Receive an amount of data in blocking mode using HAL_I2S_Receive()
58    
59    *** Interrupt mode IO operation ***    
60    ===================================
61    [..]    
62      (+) Send an amount of data in non blocking mode using HAL_I2S_Transmit_IT() 
63      (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can 
64          add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback 
65      (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can 
66          add his own code by customization of function pointer HAL_I2S_TxCpltCallback
67      (+) Receive an amount of data in non blocking mode using HAL_I2S_Receive_IT() 
68      (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can 
69          add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback 
70      (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can 
71          add his own code by customization of function pointer HAL_I2S_RxCpltCallback                                      
72      (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can 
73          add his own code by customization of function pointer HAL_I2S_ErrorCallback
74
75    *** DMA mode IO operation ***    
76    ==============================
77    [..] 
78      (+) Send an amount of data in non blocking mode (DMA) using HAL_I2S_Transmit_DMA() 
79      (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can 
80          add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback 
81      (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can 
82          add his own code by customization of function pointer HAL_I2S_TxCpltCallback
83      (+) Receive an amount of data in non blocking mode (DMA) using HAL_I2S_Receive_DMA() 
84      (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can 
85          add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback 
86      (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can 
87          add his own code by customization of function pointer HAL_I2S_RxCpltCallback                                     
88      (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can 
89          add his own code by customization of function pointer HAL_I2S_ErrorCallback
90      (+) Pause the DMA Transfer using HAL_I2S_DMAPause()      
91      (+) Resume the DMA Transfer using HAL_I2S_DMAResume()  
92      (+) Stop the DMA Transfer using HAL_I2S_DMAStop()      
93    
94    *** I2S HAL driver macros list ***
95    ============================================= 
96    [..]
97      Below the list of most used macros in I2S HAL driver.
98        
99       (+) __HAL_I2S_ENABLE: Enable the specified SPI peripheral (in I2S mode) 
100       (+) __HAL_I2S_DISABLE: Disable the specified SPI peripheral (in I2S mode)    
101       (+) __HAL_I2S_ENABLE_IT : Enable the specified I2S interrupts
102       (+) __HAL_I2S_DISABLE_IT : Disable the specified I2S interrupts
103       (+) __HAL_I2S_GET_FLAG: Check whether the specified I2S flag is set or not
104       
105     [..]  
106       (@) You can refer to the I2S HAL driver header file for more useful macros
107
108   @endverbatim
109   ******************************************************************************
110   * @attention
111   *
112   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
113   *
114   * Redistribution and use in source and binary forms, with or without modification,
115   * are permitted provided that the following conditions are met:
116   *   1. Redistributions of source code must retain the above copyright notice,
117   *      this list of conditions and the following disclaimer.
118   *   2. Redistributions in binary form must reproduce the above copyright notice,
119   *      this list of conditions and the following disclaimer in the documentation
120   *      and/or other materials provided with the distribution.
121   *   3. Neither the name of STMicroelectronics nor the names of its contributors
122   *      may be used to endorse or promote products derived from this software
123   *      without specific prior written permission.
124   *
125   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
126   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
127   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
128   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
129   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
130   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
131   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
132   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
133   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
134   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135   *
136   ******************************************************************************
137   */ 
138
139 /* Includes ------------------------------------------------------------------*/
140 #include "stm32f3xx_hal.h"
141
142 /** @addtogroup STM32F3xx_HAL_Driver
143   * @{
144   */
145
146 /** @defgroup I2S I2S HAL module driver
147   * @brief I2S HAL module driver
148   * @{
149   */
150
151 #ifdef HAL_I2S_MODULE_ENABLED
152
153 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
154     defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
155     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || \
156     defined(STM32F373xC) || defined(STM32F378xx)
157
158 /* Private typedef -----------------------------------------------------------*/
159 /* Private define ------------------------------------------------------------*/
160 /* Private macro -------------------------------------------------------------*/
161 /* Private variables ---------------------------------------------------------*/
162 /* Private function prototypes -----------------------------------------------*/
163 /** @defgroup I2S_Private_Functions I2S Private Functions
164   * @{
165   */
166 static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma);
167 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
168 static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma);
169 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
170 static void I2S_DMAError(DMA_HandleTypeDef *hdma);
171 static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s);
172 static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s);
173 static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t State, uint32_t Timeout);
174 /**
175   * @}
176   */
177
178 /* Exported functions ---------------------------------------------------------*/
179
180 /** @defgroup I2S_Exported_Functions I2S Exported Functions
181   * @{
182   */
183
184 /** @defgroup  I2S_Exported_Functions_Group1 Initialization and de-initialization functions 
185   *  @brief    Initialization and Configuration functions 
186   *
187 @verbatim    
188  ===============================================================================
189               ##### Initialization and de-initialization functions #####
190  ===============================================================================
191     [..]  This subsection provides a set of functions allowing to initialize and 
192           de-initialiaze the I2Sx peripheral in simplex mode:
193
194       (+) User must Implement HAL_I2S_MspInit() function in which he configures 
195           all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
196
197       (+) Call the function HAL_I2S_Init() to configure the selected device with 
198           the selected configuration:
199         (++) Mode
200         (++) Standard 
201         (++) Data Format
202         (++) MCLK Output
203         (++) Audio frequency
204         (++) Polarity
205         (++) Full duplex mode
206
207       (+) Call the function HAL_I2S_DeInit() to restore the default configuration 
208           of the selected I2Sx periperal. 
209 @endverbatim
210   * @{
211   */
212
213 /**
214   * @brief Initializes the I2S according to the specified parameters 
215   *         in the I2S_InitTypeDef and create the associated handle.
216   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
217   *         the configuration information for I2S module
218   * @retval HAL status
219   */
220 __weak HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s)
221 {
222   /* Note : This function is defined into this file for library reference.  */
223   /*        Function content is located into file stm32f3xx_hal_i2s_ex.c to */
224   /*        handle the possible I2S interfaces defined in STM32F3xx devices */
225   
226   /* Return error status as not implemented here */
227   return HAL_ERROR;
228 }
229            
230 /**
231   * @brief DeInitializes the I2S peripheral 
232   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
233   *         the configuration information for I2S module
234   * @retval HAL status
235   */
236 HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)
237 {
238   /* Check the I2S handle allocation */
239   if(hi2s == HAL_NULL)
240   {
241     return HAL_ERROR;
242   }
243   
244   /* Check the parameters */
245   assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));
246
247   hi2s->State = HAL_I2S_STATE_BUSY;
248   
249   /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
250   HAL_I2S_MspDeInit(hi2s);
251   
252   hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
253   hi2s->State = HAL_I2S_STATE_RESET;
254   
255   /* Release Lock */
256   __HAL_UNLOCK(hi2s);
257
258   return HAL_OK;
259 }
260
261 /**
262   * @brief I2S MSP Init
263   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
264   *         the configuration information for I2S module
265   * @retval None
266   */
267  __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
268 {
269   /* NOTE : This function Should not be modified, when the callback is needed,
270             the HAL_I2S_MspInit could be implemented in the user file
271    */ 
272 }
273
274 /**
275   * @brief I2S MSP DeInit
276   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
277   *         the configuration information for I2S module
278   * @retval None
279   */
280  __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
281 {
282   /* NOTE : This function Should not be modified, when the callback is needed,
283             the HAL_I2S_MspDeInit could be implemented in the user file
284    */ 
285 }
286
287 /**
288   * @}
289   */
290
291 /** @defgroup I2S_Exported_Functions_Group2 Input and Output operation functions 
292   *  @brief Data transfers functions 
293   *
294 @verbatim   
295  ===============================================================================
296                       ##### IO operation functions #####
297  ===============================================================================  
298     [..]
299     This subsection provides a set of functions allowing to manage the I2S data 
300     transfers.
301
302     (#) There are two modes of transfer:
303        (++) Blocking mode : The communication is performed in the polling mode. 
304             The status of all data processing is returned by the same function 
305             after finishing transfer.  
306        (++) No-Blocking mode : The communication is performed using Interrupts 
307             or DMA. These functions return the status of the transfer startup.
308             The end of the data processing will be indicated through the 
309             dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when 
310             using DMA mode.
311
312     (#) Blocking mode functions are :
313         (++) HAL_I2S_Transmit()
314         (++) HAL_I2S_Receive()
315         
316     (#) No-Blocking mode functions with Interrupt are :
317         (++) HAL_I2S_Transmit_IT()
318         (++) HAL_I2S_Receive_IT()
319
320     (#) No-Blocking mode functions with DMA are :
321         (++) HAL_I2S_Transmit_DMA()
322         (++) HAL_I2S_Receive_DMA()
323
324     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
325         (++) HAL_I2S_TxCpltCallback()
326         (++) HAL_I2S_RxCpltCallback()
327         (++) HAL_I2S_ErrorCallback()
328
329 @endverbatim
330   * @{
331   */
332
333 /**
334   * @brief Transmit an amount of data in blocking mode
335   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
336   *         the configuration information for I2S module
337   * @param pData: a 16-bit pointer to data buffer.
338   * @param Size: number of data sample to be sent:
339   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
340   *       configuration phase, the Size parameter means the number of 16-bit data length 
341   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 
342   *       the Size parameter means the number of 16-bit data length. 
343   * @param  Timeout: Timeout duration
344   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 
345   *       between Master and Slave(example: audio streaming).
346   * @retval HAL status
347   */
348 HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
349 {
350   if((pData == HAL_NULL ) || (Size == 0)) 
351   {
352     return  HAL_ERROR;                                    
353   }
354   
355   if(hi2s->State == HAL_I2S_STATE_READY)
356   { 
357     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
358        ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
359     {
360       hi2s->TxXferSize = (Size << 1);
361       hi2s->TxXferCount = (Size << 1);
362     }
363     else
364     {
365       hi2s->TxXferSize = Size;
366       hi2s->TxXferCount = Size;
367     }
368     
369     /* Process Locked */
370     __HAL_LOCK(hi2s);
371     
372     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
373     hi2s->State = HAL_I2S_STATE_BUSY_TX;
374    
375     /* Check if the I2S is already enabled */ 
376     if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
377     {
378       /* Enable I2S peripheral */    
379       __HAL_I2S_ENABLE(hi2s);
380     }
381     
382     while(hi2s->TxXferCount > 0)
383     {
384       hi2s->Instance->DR = (*pData++);
385       hi2s->TxXferCount--;   
386       /* Wait until TXE flag is set */
387       if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK)
388       {
389         /* Set the error code and execute error callback*/
390         hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;
391         HAL_I2S_ErrorCallback(hi2s);
392         return HAL_TIMEOUT;
393       }
394
395       /* Check if an underrun occurs */
396       if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET) 
397       {
398         /* Set the I2S State ready */
399         hi2s->State = HAL_I2S_STATE_READY; 
400
401         /* Process Unlocked */
402         __HAL_UNLOCK(hi2s);
403
404         /* Set the error code and execute error callback*/
405         hi2s->ErrorCode |= HAL_I2S_ERROR_UDR;
406         HAL_I2S_ErrorCallback(hi2s);
407
408         return HAL_ERROR;
409       }
410     }      
411     
412     /* Wait until Busy flag is reset */
413     if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, RESET, Timeout) != HAL_OK) 
414     {
415       /* Set the error code and execute error callback*/
416       hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;
417       HAL_I2S_ErrorCallback(hi2s);
418       return HAL_TIMEOUT;
419     }
420     
421     hi2s->State = HAL_I2S_STATE_READY; 
422     
423     /* Process Unlocked */
424     __HAL_UNLOCK(hi2s);
425     
426     return HAL_OK;
427   }
428   else
429   {
430     return HAL_BUSY;
431   }
432 }
433
434 /**
435   * @brief Receive an amount of data in blocking mode 
436   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
437   *         the configuration information for I2S module
438   * @param pData: a 16-bit pointer to data buffer.
439   * @param Size: number of data sample to be sent:
440   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
441   *       configuration phase, the Size parameter means the number of 16-bit data length 
442   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 
443   *       the Size parameter means the number of 16-bit data length. 
444   * @param Timeout: Timeout duration
445   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 
446   *       between Master and Slave(example: audio streaming).
447   * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate
448   *       in continouse way and as the I2S is not disabled at the end of the I2S transaction.
449   * @retval HAL status
450   */
451 HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
452 {
453   if((pData == HAL_NULL ) || (Size == 0)) 
454   {
455     return  HAL_ERROR;                                    
456   }
457   
458   if(hi2s->State == HAL_I2S_STATE_READY)
459   { 
460     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
461        ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
462     {
463       hi2s->RxXferSize = (Size << 1);
464       hi2s->RxXferCount = (Size << 1);
465     }
466     else
467     {
468       hi2s->RxXferSize = Size;
469       hi2s->RxXferCount = Size;
470     }
471     /* Process Locked */
472     __HAL_LOCK(hi2s);
473     
474     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
475     hi2s->State = HAL_I2S_STATE_BUSY_RX;
476         
477     /* Check if the I2S is already enabled */ 
478     if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
479     {
480       /* Enable I2S peripheral */    
481       __HAL_I2S_ENABLE(hi2s);
482     }
483     
484     /* Check if Master Receiver mode is selected */
485     if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
486     {
487       /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
488       access to the SPI_SR register. */ 
489       __HAL_I2S_CLEAR_OVRFLAG(hi2s);        
490     }
491     
492     /* Receive data */
493     while(hi2s->RxXferCount > 0)
494     {
495       /* Wait until RXNE flag is set */
496       if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK) 
497       {
498         /* Set the error code and execute error callback*/
499         hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;
500         HAL_I2S_ErrorCallback(hi2s);
501         return HAL_TIMEOUT;
502       }
503       
504       /* Check if an overrun occurs */
505       if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET) 
506       {
507         /* Set the I2S State ready */
508         hi2s->State = HAL_I2S_STATE_READY; 
509
510         /* Process Unlocked */
511         __HAL_UNLOCK(hi2s);
512
513         /* Set the error code and execute error callback*/
514         hi2s->ErrorCode |= HAL_I2S_ERROR_OVR;
515         HAL_I2S_ErrorCallback(hi2s);
516
517         return HAL_ERROR;
518       }
519
520       (*pData++) = hi2s->Instance->DR;
521       hi2s->RxXferCount--;
522     }      
523
524     hi2s->State = HAL_I2S_STATE_READY; 
525     
526     /* Process Unlocked */
527     __HAL_UNLOCK(hi2s);
528     
529     return HAL_OK;
530   }
531   else
532   {
533     return HAL_BUSY;
534   }
535 }
536
537 /**
538   * @brief Transmit an amount of data in non-blocking mode with Interrupt
539   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
540   *         the configuration information for I2S module
541   * @param pData: a 16-bit pointer to data buffer.
542   * @param Size: number of data sample to be sent:
543   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
544   *       configuration phase, the Size parameter means the number of 16-bit data length 
545   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 
546   *       the Size parameter means the number of 16-bit data length. 
547   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 
548   *       between Master and Slave(example: audio streaming).
549   * @retval HAL status
550   */
551 HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
552 {
553   if(hi2s->State == HAL_I2S_STATE_READY)
554   {
555     if((pData == HAL_NULL) || (Size == 0)) 
556     {
557       return  HAL_ERROR;                                    
558     }
559     
560     hi2s->pTxBuffPtr = pData;
561     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
562       ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
563     {
564       hi2s->TxXferSize = (Size << 1);
565       hi2s->TxXferCount = (Size << 1);
566     }  
567     else
568     {
569       hi2s->TxXferSize = Size;
570       hi2s->TxXferCount = Size;
571     }
572     
573     /* Process Locked */
574     __HAL_LOCK(hi2s);
575     
576     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
577     hi2s->State = HAL_I2S_STATE_BUSY_TX;
578
579     /* Enable TXE and ERR interrupt */
580     __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
581     
582     /* Check if the I2S is already enabled */ 
583     if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
584     {
585       /* Enable I2S peripheral */    
586       __HAL_I2S_ENABLE(hi2s);
587     }
588     
589     /* Process Unlocked */
590     __HAL_UNLOCK(hi2s);
591     
592     return HAL_OK;
593   }
594   else
595   {
596     return HAL_BUSY;
597   }
598 }
599
600 /**
601   * @brief Receive an amount of data in non-blocking mode with Interrupt
602   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
603   *         the configuration information for I2S module
604   * @param pData: a 16-bit pointer to the Receive data buffer.
605   * @param Size: number of data sample to be sent:
606   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
607   *       configuration phase, the Size parameter means the number of 16-bit data length 
608   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 
609   *       the Size parameter means the number of 16-bit data length. 
610   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 
611   *       between Master and Slave(example: audio streaming).
612   * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronisation 
613   * between Master and Slave otherwise the I2S interrupt should be optimized. 
614   * @retval HAL status
615   */
616 HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
617 {
618   if(hi2s->State == HAL_I2S_STATE_READY)
619   {
620     if((pData == HAL_NULL) || (Size == 0)) 
621     {
622       return  HAL_ERROR;                                    
623     }
624     
625     hi2s->pRxBuffPtr = pData;
626     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
627       ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
628     {
629       hi2s->RxXferSize = (Size << 1);
630       hi2s->RxXferCount = (Size << 1);
631     }  
632     else
633     {
634       hi2s->RxXferSize = Size;
635       hi2s->RxXferCount = Size;
636     }
637     /* Process Locked */
638     __HAL_LOCK(hi2s);
639     
640     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
641     hi2s->State = HAL_I2S_STATE_BUSY_RX;
642
643     /* Enable TXE and ERR interrupt */
644     __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
645     
646     /* Check if the I2S is already enabled */ 
647     if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
648     {
649       /* Enable I2S peripheral */    
650       __HAL_I2S_ENABLE(hi2s);
651     }
652     
653     /* Process Unlocked */
654     __HAL_UNLOCK(hi2s);
655     
656     return HAL_OK;
657   }
658   else
659   {
660     return HAL_BUSY; 
661   } 
662 }
663
664 /**
665   * @brief Transmit an amount of data in non-blocking mode with DMA
666   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
667   *         the configuration information for I2S module
668   * @param pData: a 16-bit pointer to the Transmit data buffer.
669   * @param Size: number of data sample to be sent:
670   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
671   *       configuration phase, the Size parameter means the number of 16-bit data length 
672   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 
673   *       the Size parameter means the number of 16-bit data length. 
674   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 
675   *       between Master and Slave(example: audio streaming).
676   * @retval HAL status
677   */
678 HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
679 {
680   uint32_t *tmp;
681   
682   if((pData == HAL_NULL) || (Size == 0)) 
683   {
684     return  HAL_ERROR;                                    
685   }
686   
687   if(hi2s->State == HAL_I2S_STATE_READY)
688   {  
689     hi2s->pTxBuffPtr = pData;
690     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
691       ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
692     {
693       hi2s->TxXferSize = (Size << 1);
694       hi2s->TxXferCount = (Size << 1);
695     }  
696     else
697     {
698       hi2s->TxXferSize = Size;
699       hi2s->TxXferCount = Size;
700     }  
701     
702     /* Process Locked */
703     __HAL_LOCK(hi2s);
704     
705     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
706     hi2s->State = HAL_I2S_STATE_BUSY_TX;
707
708     /* Set the I2S Tx DMA Half transfer complete callback */
709     hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt;
710
711     /* Set the I2S TxDMA transfer complete callback */
712     hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt;
713     
714     /* Set the DMA error callback */
715     hi2s->hdmatx->XferErrorCallback = I2S_DMAError;
716     
717     /* Enable the Tx DMA Channel */
718     tmp = (uint32_t*)&pData;
719     HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t*)tmp, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize);
720     
721     /* Check if the I2S is already enabled */ 
722     if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
723     {
724       /* Enable I2S peripheral */    
725       __HAL_I2S_ENABLE(hi2s);
726     }
727     
728     /* Enable Tx DMA Request */  
729     hi2s->Instance->CR2 |= SPI_CR2_TXDMAEN;
730
731     /* Process Unlocked */
732     __HAL_UNLOCK(hi2s);
733     
734     return HAL_OK;
735   }
736   else
737   {
738     return HAL_BUSY;
739   }
740 }
741
742 /**
743   * @brief Receive an amount of data in non-blocking mode with DMA 
744   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
745   *         the configuration information for I2S module
746   * @param pData: a 16-bit pointer to the Receive data buffer.
747   * @param Size: number of data sample to be sent:
748   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
749   *       configuration phase, the Size parameter means the number of 16-bit data length 
750   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected 
751   *       the Size parameter means the number of 16-bit data length. 
752   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization 
753   *       between Master and Slave(example: audio streaming).
754   * @retval HAL status
755   */
756 HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
757 {
758   uint32_t *tmp;
759   
760   if((pData == HAL_NULL) || (Size == 0)) 
761   {
762     return  HAL_ERROR;                                    
763   } 
764     
765   if(hi2s->State == HAL_I2S_STATE_READY)
766   {    
767     hi2s->pRxBuffPtr = pData;
768     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\
769       ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))
770     {
771       hi2s->RxXferSize = (Size << 1);
772       hi2s->RxXferCount = (Size << 1);
773     }  
774     else
775     {
776       hi2s->RxXferSize = Size;
777       hi2s->RxXferCount = Size;
778     }
779     /* Process Locked */
780     __HAL_LOCK(hi2s);
781     
782     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
783     hi2s->State = HAL_I2S_STATE_BUSY_RX;
784    
785     /* Set the I2S Rx DMA Half transfer complete callback */
786     hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt;
787
788     /* Set the I2S Rx DMA transfer complete callback */
789     hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt;
790     
791     /* Set the DMA error callback */
792     hi2s->hdmarx->XferErrorCallback = I2S_DMAError;
793     
794     /* Check if Master Receiver mode is selected */
795     if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
796     {
797       /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read
798       access to the SPI_SR register. */ 
799       __HAL_I2S_CLEAR_OVRFLAG(hi2s);        
800     }
801     
802     /* Enable the Rx DMA Channel */
803     tmp = (uint32_t*)&pData;        
804     HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, *(uint32_t*)tmp, hi2s->RxXferSize);
805     
806     /* Check if the I2S is already enabled */ 
807     if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
808     {
809       /* Enable I2S peripheral */    
810       __HAL_I2S_ENABLE(hi2s);
811     }
812     
813     /* Enable Rx DMA Request */  
814     hi2s->Instance->CR2 |= SPI_CR2_RXDMAEN;
815     
816     /* Process Unlocked */
817     __HAL_UNLOCK(hi2s);
818
819     return HAL_OK;
820   }
821   else
822   {
823     return HAL_BUSY;
824   }
825 }
826
827 /**
828   * @brief  This function handles I2S interrupt request.
829   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
830   *         the configuration information for I2S module
831   * @retval HAL status
832   */
833 void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
834 {  
835   __IO uint32_t i2ssr = hi2s->Instance->SR;
836
837   if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
838   {  
839     /* I2S in mode Receiver ----------------------------------------------------*/
840     if(((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_RXNE) != RESET))
841     {
842       I2S_Receive_IT(hi2s);
843     }
844
845     /* I2S Overrun error interrupt occured -------------------------------------*/
846     if(((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
847     {
848       /* Disable RXNE and ERR interrupt */
849       __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
850
851       /* Set the I2S State ready */
852       hi2s->State = HAL_I2S_STATE_READY; 
853
854       /* Set the error code and execute error callback*/
855       hi2s->ErrorCode |= HAL_I2S_ERROR_OVR;
856       HAL_I2S_ErrorCallback(hi2s);
857     }  
858   }
859   else if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
860   {  
861     /* I2S in mode Tramitter ---------------------------------------------------*/
862     if(((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_TXE) != RESET))
863     {     
864       I2S_Transmit_IT(hi2s);
865     } 
866     
867     /* I2S Underrun error interrupt occured ------------------------------------*/
868     if(((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))
869     {
870       /* Disable TXE and ERR interrupt */
871       __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
872
873       /* Set the I2S State ready */
874       hi2s->State = HAL_I2S_STATE_READY; 
875
876       /* Set the error code and execute error callback*/
877       hi2s->ErrorCode |= HAL_I2S_ERROR_UDR;
878       HAL_I2S_ErrorCallback(hi2s);
879     }
880   }
881 }
882
883 /**
884   * @}
885   */
886
887 /**
888   * @}
889   */
890
891 /** @addtogroup I2S_Private_Functions I2S Private Functions
892   * @{
893   */
894 /**
895   * @brief This function handles I2S Communication Timeout.
896   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
897   *         the configuration information for I2S module
898   * @param Flag: Flag checked
899   * @param State: Value of the flag expected
900   * @param Timeout: Duration of the timeout
901   * @retval HAL status
902   */
903 static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, 
904                                                        uint32_t State, uint32_t Timeout)
905 {
906   uint32_t tickstart = HAL_GetTick();
907      
908   while((__HAL_I2S_GET_FLAG(hi2s, Flag)) != State)
909   {
910     if(Timeout != HAL_MAX_DELAY)
911     {
912         if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
913       {
914         /* Set the I2S State ready */
915         hi2s->State= HAL_I2S_STATE_READY;
916       
917         /* Process Unlocked */
918         __HAL_UNLOCK(hi2s);
919       
920         return HAL_TIMEOUT;
921       }
922     }
923   }
924   
925   return HAL_OK;      
926 }
927 /**
928   * @}
929   */
930
931 /** @addtogroup I2S_Exported_Functions I2S Exported Functions
932   * @{
933   */
934
935 /** @addtogroup  I2S_Exported_Functions_Group2 Input and Output operation functions 
936   * @{
937   */
938 /**
939   * @brief Tx Transfer Half completed callbacks
940   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
941   *         the configuration information for I2S module
942   * @retval None
943   */
944  __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
945 {
946   /* NOTE : This function Should not be modified, when the callback is needed,
947             the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
948    */ 
949 }
950
951 /**
952   * @brief Tx Transfer completed callbacks
953   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
954   *         the configuration information for I2S module
955   * @retval None
956   */
957  __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
958 {
959   /* NOTE : This function Should not be modified, when the callback is needed,
960             the HAL_I2S_TxCpltCallback could be implemented in the user file
961    */ 
962 }
963
964 /**
965   * @brief Rx Transfer half completed callbacks
966   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
967   *         the configuration information for I2S module
968   * @retval None
969   */
970 __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
971 {
972   /* NOTE : This function Should not be modified, when the callback is needed,
973             the HAL_I2S_RxCpltCallback could be implemented in the user file
974    */
975 }
976
977 /**
978   * @brief Rx Transfer completed callbacks
979   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
980   *         the configuration information for I2S module
981   * @retval None
982   */
983 __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
984 {
985   /* NOTE : This function Should not be modified, when the callback is needed,
986             the HAL_I2S_RxCpltCallback could be implemented in the user file
987    */
988 }
989
990 /**
991   * @brief I2S error callbacks
992   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
993   *         the configuration information for I2S module
994   * @retval None
995   */
996  __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
997 {
998   /* NOTE : This function Should not be modified, when the callback is needed,
999             the HAL_I2S_ErrorCallback could be implemented in the user file
1000    */ 
1001 }
1002
1003 /**
1004   * @}
1005   */
1006
1007 /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions 
1008   *  @brief   Peripheral State functions 
1009   *
1010 @verbatim   
1011  ===============================================================================
1012                       ##### Peripheral State and Errors functions #####
1013  ===============================================================================  
1014     [..]
1015     This subsection permits to get in run-time the status of the peripheral 
1016     and the data flow.
1017
1018 @endverbatim
1019   * @{
1020   */
1021
1022 /**
1023   * @brief  Return the I2S state
1024   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
1025   *         the configuration information for I2S module
1026   * @retval HAL state
1027   */
1028 HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s)
1029 {
1030   return hi2s->State;
1031 }
1032
1033 /**
1034   * @brief  Return the I2S error code
1035   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
1036   *         the configuration information for I2S module
1037   * @retval I2S Error Code
1038   */
1039 HAL_I2S_ErrorTypeDef HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)
1040 {
1041   return hi2s->ErrorCode;
1042 }
1043 /**
1044   * @}
1045   */  
1046
1047 /**
1048   * @}
1049   */
1050
1051 /** @addtogroup I2S_Private_Functions I2S Private Functions
1052   * @{
1053   */
1054 /**
1055   * @brief DMA I2S transmit process complete callback 
1056   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
1057   *                the configuration information for the specified DMA module.
1058   * @retval None
1059   */
1060 static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma)   
1061 {
1062   I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1063   
1064   /* Disable Tx DMA Request */
1065   hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
1066   
1067   hi2s->TxXferCount = 0;
1068   hi2s->State = HAL_I2S_STATE_READY;
1069   
1070   HAL_I2S_TxCpltCallback(hi2s);
1071 }
1072
1073 /**
1074   * @brief DMA I2S transmit process half complete callback 
1075   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
1076   *                the configuration information for the specified DMA module.
1077   * @retval None
1078   */
1079 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
1080 {
1081   I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1082
1083   HAL_I2S_TxHalfCpltCallback(hi2s);
1084 }
1085
1086 /**
1087   * @brief DMA I2S receive process complete callback 
1088   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
1089   *                the configuration information for the specified DMA module.
1090   * @retval None
1091   */
1092 static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma)   
1093 {
1094   I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1095   
1096   /* Disable Rx DMA Request */
1097   hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
1098   hi2s->RxXferCount = 0;
1099   
1100   hi2s->State = HAL_I2S_STATE_READY; 
1101   HAL_I2S_RxCpltCallback(hi2s); 
1102 }
1103       
1104 /**
1105   * @brief DMA I2S receive process half complete callback 
1106   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
1107   *                the configuration information for the specified DMA module.
1108   * @retval None
1109   */
1110 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
1111 {
1112   I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1113
1114   HAL_I2S_RxHalfCpltCallback(hi2s); 
1115 }
1116
1117 /**
1118   * @brief DMA I2S communication error callback 
1119   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
1120   *                the configuration information for the specified DMA module.
1121   * @retval None
1122   */
1123 static void I2S_DMAError(DMA_HandleTypeDef *hdma)   
1124 {
1125   I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1126   
1127   /* Disable Rx and Tx DMA Request */
1128   hi2s->Instance->CR2 &= (uint32_t)(~(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));
1129   hi2s->TxXferCount = 0;
1130   hi2s->RxXferCount = 0;
1131   
1132   hi2s->State= HAL_I2S_STATE_READY;
1133
1134   /* Set the error code and execute error callback*/
1135   hi2s->ErrorCode |= HAL_I2S_ERROR_DMA;
1136   HAL_I2S_ErrorCallback(hi2s);
1137 }
1138
1139 /**
1140   * @brief Transmit an amount of data in non-blocking mode with Interrupt
1141   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains
1142   *         the configuration information for I2S module
1143   * @retval None
1144   */
1145 static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s)
1146 {
1147   /* Transmit data */
1148   hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
1149   hi2s->TxXferCount--;  
1150
1151   if(hi2s->TxXferCount == 0)
1152   {
1153     /* Disable TXE and ERR interrupt */
1154     __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
1155
1156     hi2s->State = HAL_I2S_STATE_READY;
1157     HAL_I2S_TxCpltCallback(hi2s);
1158   }
1159 }
1160
1161 /**
1162   * @brief Receive an amount of data in non-blocking mode with Interrupt
1163   * @param hi2s: I2S handle
1164   * @retval None
1165   */
1166 static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s)
1167 {
1168   /* Receive data */    
1169   (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
1170   hi2s->RxXferCount--;
1171
1172   if(hi2s->RxXferCount == 0)
1173   {    
1174     /* Disable RXNE and ERR interrupt */
1175     __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
1176
1177     hi2s->State = HAL_I2S_STATE_READY;     
1178     HAL_I2S_RxCpltCallback(hi2s); 
1179   }
1180 }
1181 /**
1182   * @}
1183   */
1184
1185 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
1186        /* STM32F302xC || STM32F303xC || STM32F358xx || */
1187        /* STM32F301x8 || STM32F302x8 || STM32F318xx || */
1188        /* STM32F373xC || STM32F378xx                   */
1189
1190 #endif /* HAL_I2S_MODULE_ENABLED */
1191 /**
1192   * @}
1193   */
1194
1195 /**
1196   * @}
1197   */
1198
1199 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/