]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL/ST_Library/stm32f7xx_hal_i2s.c
Update version number ready for V8.2.1 release.
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL / ST_Library / stm32f7xx_hal_i2s.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_i2s.c\r
4   * @author  MCD Application Team\r
5   * @version V0.3.0\r
6   * @date    06-March-2015\r
7   * @brief   I2S HAL module driver.\r
8   *          This file provides firmware functions to manage the following \r
9   *          functionalities of the Integrated Interchip Sound (I2S) peripheral:\r
10   *           + Initialization and de-initialization functions\r
11   *           + IO operation functions\r
12   *           + Peripheral State and Errors functions\r
13   @verbatim\r
14  ===============================================================================\r
15                   ##### How to use this driver #####\r
16  ===============================================================================\r
17  [..]\r
18     The I2S HAL driver can be used as follows:\r
19     \r
20     (#) Declare a I2S_HandleTypeDef handle structure.\r
21     (#) Initialize the I2S low level resources by implement the HAL_I2S_MspInit() API:\r
22         (##) Enable the SPIx interface clock.                      \r
23         (##) I2S pins configuration:\r
24             (+++) Enable the clock for the I2S GPIOs.\r
25             (+++) Configure these I2S pins as alternate function pull-up.\r
26         (##) NVIC configuration if you need to use interrupt process (HAL_I2S_Transmit_IT()\r
27              and HAL_I2S_Receive_IT() APIs).\r
28             (+++) Configure the I2Sx interrupt priority.\r
29             (+++) Enable the NVIC I2S IRQ handle.\r
30         (##) DMA Configuration if you need to use DMA process (HAL_I2S_Transmit_DMA()\r
31              and HAL_I2S_Receive_DMA() APIs:\r
32             (+++) Declare a DMA handle structure for the Tx/Rx channel.\r
33             (+++) Enable the DMAx interface clock.\r
34             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.                \r
35             (+++) Configure the DMA Tx/Rx Channel.\r
36             (+++) Associate the initialized DMA handle to the I2S DMA Tx/Rx handle.\r
37             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the \r
38                 DMA Tx/Rx Channel.\r
39   \r
40    (#) Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity\r
41        using HAL_I2S_Init() function.\r
42 \r
43    -@- The specific I2S interrupts (Transmission complete interrupt, \r
44        RXNE interrupt and Error Interrupts) will be managed using the macros\r
45        __HAL_I2S_ENABLE_IT() and __HAL_I2S_DISABLE_IT() inside the transmit and receive process.\r
46    -@- Make sure that either:\r
47        (+@) I2S clock is configured based on SYSCLK or \r
48        (+@) External clock source is configured after setting correctly \r
49             the define constant EXTERNAL_CLOCK_VALUE in the stm32f3xx_hal_conf.h file. \r
50 \r
51    (#) Three mode of operations are available within this driver :     \r
52   \r
53    *** Polling mode IO operation ***\r
54    =================================\r
55    [..]    \r
56      (+) Send an amount of data in blocking mode using HAL_I2S_Transmit() \r
57      (+) Receive an amount of data in blocking mode using HAL_I2S_Receive()\r
58    \r
59    *** Interrupt mode IO operation ***    \r
60    ===================================\r
61    [..]    \r
62      (+) Send an amount of data in non blocking mode using HAL_I2S_Transmit_IT() \r
63      (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can \r
64          add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback \r
65      (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can \r
66          add his own code by customization of function pointer HAL_I2S_TxCpltCallback\r
67      (+) Receive an amount of data in non blocking mode using HAL_I2S_Receive_IT() \r
68      (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can \r
69          add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback \r
70      (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can \r
71          add his own code by customization of function pointer HAL_I2S_RxCpltCallback                                      \r
72      (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can \r
73          add his own code by customization of function pointer HAL_I2S_ErrorCallback\r
74 \r
75    *** DMA mode IO operation ***    \r
76    ==============================\r
77    [..] \r
78      (+) Send an amount of data in non blocking mode (DMA) using HAL_I2S_Transmit_DMA() \r
79      (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can \r
80          add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback \r
81      (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can \r
82          add his own code by customization of function pointer HAL_I2S_TxCpltCallback\r
83      (+) Receive an amount of data in non blocking mode (DMA) using HAL_I2S_Receive_DMA() \r
84      (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can \r
85          add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback \r
86      (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can \r
87          add his own code by customization of function pointer HAL_I2S_RxCpltCallback                                     \r
88      (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can \r
89          add his own code by customization of function pointer HAL_I2S_ErrorCallback\r
90      (+) Pause the DMA Transfer using HAL_I2S_DMAPause()      \r
91      (+) Resume the DMA Transfer using HAL_I2S_DMAResume()  \r
92      (+) Stop the DMA Transfer using HAL_I2S_DMAStop()      \r
93    \r
94    *** I2S HAL driver macros list ***\r
95    ============================================= \r
96    [..]\r
97      Below the list of most used macros in I2S HAL driver.\r
98        \r
99       (+) __HAL_I2S_ENABLE: Enable the specified SPI peripheral (in I2S mode) \r
100       (+) __HAL_I2S_DISABLE: Disable the specified SPI peripheral (in I2S mode)    \r
101       (+) __HAL_I2S_ENABLE_IT : Enable the specified I2S interrupts\r
102       (+) __HAL_I2S_DISABLE_IT : Disable the specified I2S interrupts\r
103       (+) __HAL_I2S_GET_FLAG: Check whether the specified I2S flag is set or not\r
104       \r
105     [..]  \r
106       (@) You can refer to the I2S HAL driver header file for more useful macros\r
107 \r
108   @endverbatim\r
109   ******************************************************************************\r
110   * @attention\r
111   *\r
112   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
113   *\r
114   * Redistribution and use in source and binary forms, with or without modification,\r
115   * are permitted provided that the following conditions are met:\r
116   *   1. Redistributions of source code must retain the above copyright notice,\r
117   *      this list of conditions and the following disclaimer.\r
118   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
119   *      this list of conditions and the following disclaimer in the documentation\r
120   *      and/or other materials provided with the distribution.\r
121   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
122   *      may be used to endorse or promote products derived from this software\r
123   *      without specific prior written permission.\r
124   *\r
125   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
126   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
127   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
128   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
129   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
130   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
131   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
132   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
133   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
134   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
135   *\r
136   ******************************************************************************\r
137   */ \r
138 \r
139 /* Includes ------------------------------------------------------------------*/\r
140 #include "stm32f7xx_hal.h"\r
141 \r
142 /** @addtogroup STM32F3xx_HAL_Driver\r
143   * @{\r
144   */\r
145 \r
146 /** @defgroup I2S I2S HAL module driver\r
147   * @brief I2S HAL module driver\r
148   * @{\r
149   */\r
150 \r
151 #ifdef HAL_I2S_MODULE_ENABLED\r
152 \r
153 /* Private typedef -----------------------------------------------------------*/\r
154 /* Private define ------------------------------------------------------------*/\r
155 /* Private macro -------------------------------------------------------------*/\r
156 /* Private variables ---------------------------------------------------------*/\r
157 /* Private function prototypes -----------------------------------------------*/\r
158 /** @defgroup I2S_Private_Functions I2S Private Functions\r
159   * @{\r
160   */\r
161 static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma);\r
162 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma);\r
163 static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma);\r
164 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma);\r
165 static void I2S_DMAError(DMA_HandleTypeDef *hdma);\r
166 static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s);\r
167 static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s);\r
168 static uint32_t I2S_GetClockFreq(I2S_HandleTypeDef *hi2s);\r
169 static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, uint32_t State, uint32_t Timeout);\r
170 /**\r
171   * @}\r
172   */\r
173 \r
174 /* Exported functions ---------------------------------------------------------*/\r
175 \r
176 /** @defgroup I2S_Exported_Functions I2S Exported Functions\r
177   * @{\r
178   */\r
179 \r
180 /** @defgroup  I2S_Exported_Functions_Group1 Initialization and de-initialization functions \r
181   *  @brief    Initialization and Configuration functions \r
182   *\r
183 @verbatim    \r
184  ===============================================================================\r
185               ##### Initialization and de-initialization functions #####\r
186  ===============================================================================\r
187     [..]  This subsection provides a set of functions allowing to initialize and \r
188           de-initialize the I2Sx peripheral in simplex mode:\r
189 \r
190       (+) User must Implement HAL_I2S_MspInit() function in which he configures \r
191           all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).\r
192 \r
193       (+) Call the function HAL_I2S_Init() to configure the selected device with \r
194           the selected configuration:\r
195         (++) Mode\r
196         (++) Standard \r
197         (++) Data Format\r
198         (++) MCLK Output\r
199         (++) Audio frequency\r
200         (++) Polarity\r
201         (++) Full duplex mode\r
202 \r
203       (+) Call the function HAL_I2S_DeInit() to restore the default configuration \r
204           of the selected I2Sx peripheral. \r
205 @endverbatim\r
206   * @{\r
207   */\r
208 \r
209 /**\r
210   * @brief Initializes the I2S according to the specified parameters \r
211   *         in the I2S_InitTypeDef and create the associated handle.\r
212   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
213   *         the configuration information for I2S module\r
214   * @retval HAL status\r
215   */\r
216 HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s)\r
217 {\r
218   uint16_t tmpreg = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;\r
219   uint32_t tmp = 0, i2sclk = 0;\r
220  \r
221   /* Check the I2S handle allocation */\r
222   if(hi2s == NULL)\r
223   {\r
224     return HAL_ERROR;\r
225   }\r
226   \r
227   /* Check the parameters */\r
228   assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));\r
229   assert_param(IS_I2S_MODE(hi2s->Init.Mode));\r
230   assert_param(IS_I2S_STANDARD(hi2s->Init.Standard));\r
231   assert_param(IS_I2S_DATA_FORMAT(hi2s->Init.DataFormat));\r
232   assert_param(IS_I2S_MCLK_OUTPUT(hi2s->Init.MCLKOutput));\r
233   assert_param(IS_I2S_AUDIO_FREQ(hi2s->Init.AudioFreq));\r
234   assert_param(IS_I2S_CPOL(hi2s->Init.CPOL));  \r
235   assert_param(IS_I2S_CLOCKSOURCE(hi2s->Init.ClockSource));\r
236   \r
237   hi2s->State = HAL_I2S_STATE_BUSY;\r
238   \r
239   /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */\r
240   HAL_I2S_MspInit(hi2s);\r
241   \r
242   /*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/\r
243   /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */\r
244   hi2s->Instance->I2SCFGR &= ~(SPI_I2SCFGR_CHLEN | SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CKPOL | \\r
245                                SPI_I2SCFGR_I2SSTD | SPI_I2SCFGR_PCMSYNC | SPI_I2SCFGR_I2SCFG | \\r
246                                SPI_I2SCFGR_I2SE | SPI_I2SCFGR_I2SMOD); \r
247   hi2s->Instance->I2SPR = 0x0002;\r
248   \r
249   /* Get the I2SCFGR register value */\r
250   tmpreg = hi2s->Instance->I2SCFGR;\r
251   \r
252   /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/\r
253   if(hi2s->Init.AudioFreq == I2S_AUDIOFREQ_DEFAULT)\r
254   {\r
255     i2sodd = (uint16_t)0;\r
256     i2sdiv = (uint16_t)2;   \r
257   }\r
258   /* If the requested audio frequency is not the default, compute the prescaler */\r
259   else\r
260   {\r
261     /* Check the frame length (For the Prescaler computing) *******************/\r
262     if(hi2s->Init.DataFormat == I2S_DATAFORMAT_16B)\r
263     {\r
264       /* Packet length is 16 bits */\r
265       packetlength = 1;\r
266     }\r
267     else\r
268     {\r
269       /* Packet length is 32 bits */\r
270       packetlength = 2;\r
271     }\r
272     \r
273     /* Get I2S source Clock frequency  ****************************************/\r
274 \r
275     /* If an external I2S clock has to be used, the specific define should be set  \r
276     in the project configuration or in the stm32f3xx_conf.h file */\r
277     if(hi2s->Init.ClockSource == I2S_CLOCK_EXTERNAL)\r
278     {    \r
279       /* Set the I2S clock to the external clock  value */\r
280       i2sclk = EXTERNAL_CLOCK_VALUE;\r
281     }\r
282     else\r
283     {\r
284       /* Get the I2S source clock value */\r
285                         i2sclk = I2S_GetClockFreq(hi2s);\r
286     }\r
287     \r
288     /* Compute the Real divider depending on the MCLK output state, with a floating point */\r
289     if(hi2s->Init.MCLKOutput == I2S_MCLKOUTPUT_ENABLE)\r
290     {\r
291       /* MCLK output is enabled */\r
292       tmp = (uint16_t)(((((i2sclk / 256) * 10) / hi2s->Init.AudioFreq)) + 5);\r
293     }\r
294     else\r
295     {\r
296       /* MCLK output is disabled */\r
297       tmp = (uint16_t)(((((i2sclk / (32 * packetlength)) *10 ) / hi2s->Init.AudioFreq)) + 5);\r
298     }\r
299     \r
300     /* Remove the flatting point */\r
301     tmp = tmp / 10;  \r
302     \r
303     /* Check the parity of the divider */\r
304     i2sodd = (uint16_t)(tmp & (uint16_t)0x0001);\r
305     \r
306     /* Compute the i2sdiv prescaler */\r
307     i2sdiv = (uint16_t)((tmp - i2sodd) / 2);\r
308     \r
309     /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */\r
310     i2sodd = (uint16_t) (i2sodd << 8);\r
311   }\r
312   \r
313   /* Test if the divider is 1 or 0 or greater than 0xFF */\r
314   if((i2sdiv < 2) || (i2sdiv > 0xFF))\r
315   {\r
316     /* Set the default values */\r
317     i2sdiv = 2;\r
318     i2sodd = 0;\r
319   }\r
320   \r
321   /* Write to SPIx I2SPR register the computed value */\r
322   hi2s->Instance->I2SPR = (uint16_t)((uint16_t)i2sdiv | (uint16_t)(i2sodd | (uint16_t)hi2s->Init.MCLKOutput));\r
323   \r
324   /* Configure the I2S with the I2S_InitStruct values */\r
325   tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(hi2s->Init.Mode | \\r
326                        (uint16_t)(hi2s->Init.Standard | (uint16_t)(hi2s->Init.DataFormat | \\r
327                        (uint16_t)hi2s->Init.CPOL))));\r
328   \r
329   /* Write to SPIx I2SCFGR */  \r
330   hi2s->Instance->I2SCFGR = tmpreg;\r
331   \r
332   hi2s->ErrorCode = HAL_I2S_ERROR_NONE;\r
333   hi2s->State= HAL_I2S_STATE_READY;\r
334   \r
335   return HAL_OK;\r
336 }\r
337            \r
338 /**\r
339   * @brief DeInitializes the I2S peripheral \r
340   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
341   *         the configuration information for I2S module\r
342   * @retval HAL status\r
343   */\r
344 HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)\r
345 {\r
346   /* Check the I2S handle allocation */\r
347   if(hi2s == NULL)\r
348   {\r
349     return HAL_ERROR;\r
350   }\r
351   \r
352   /* Check the parameters */\r
353   assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));\r
354 \r
355   hi2s->State = HAL_I2S_STATE_BUSY;\r
356   \r
357   /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */\r
358   HAL_I2S_MspDeInit(hi2s);\r
359   \r
360   hi2s->ErrorCode = HAL_I2S_ERROR_NONE;\r
361   hi2s->State = HAL_I2S_STATE_RESET;\r
362   \r
363   /* Release Lock */\r
364   __HAL_UNLOCK(hi2s);\r
365 \r
366   return HAL_OK;\r
367 }\r
368 \r
369 /**\r
370   * @brief I2S MSP Init\r
371   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
372   *         the configuration information for I2S module\r
373   * @retval None\r
374   */\r
375  __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)\r
376 {\r
377   /* NOTE : This function Should not be modified, when the callback is needed,\r
378             the HAL_I2S_MspInit could be implemented in the user file\r
379    */ \r
380 }\r
381 \r
382 /**\r
383   * @brief I2S MSP DeInit\r
384   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
385   *         the configuration information for I2S module\r
386   * @retval None\r
387   */\r
388  __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)\r
389 {\r
390   /* NOTE : This function Should not be modified, when the callback is needed,\r
391             the HAL_I2S_MspDeInit could be implemented in the user file\r
392    */ \r
393 }\r
394 \r
395 /**\r
396   * @}\r
397   */\r
398 \r
399 /** @defgroup I2S_Exported_Functions_Group2 Input and Output operation functions \r
400   *  @brief Data transfers functions \r
401   *\r
402 @verbatim   \r
403  ===============================================================================\r
404                       ##### IO operation functions #####\r
405  ===============================================================================  \r
406     [..]\r
407     This subsection provides a set of functions allowing to manage the I2S data \r
408     transfers.\r
409 \r
410     (#) There are two modes of transfer:\r
411        (++) Blocking mode : The communication is performed in the polling mode. \r
412             The status of all data processing is returned by the same function \r
413             after finishing transfer.  \r
414        (++) No-Blocking mode : The communication is performed using Interrupts \r
415             or DMA. These functions return the status of the transfer startup.\r
416             The end of the data processing will be indicated through the \r
417             dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when \r
418             using DMA mode.\r
419 \r
420     (#) Blocking mode functions are :\r
421         (++) HAL_I2S_Transmit()\r
422         (++) HAL_I2S_Receive()\r
423         \r
424     (#) No-Blocking mode functions with Interrupt are :\r
425         (++) HAL_I2S_Transmit_IT()\r
426         (++) HAL_I2S_Receive_IT()\r
427 \r
428     (#) No-Blocking mode functions with DMA are :\r
429         (++) HAL_I2S_Transmit_DMA()\r
430         (++) HAL_I2S_Receive_DMA()\r
431 \r
432     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:\r
433         (++) HAL_I2S_TxCpltCallback()\r
434         (++) HAL_I2S_RxCpltCallback()\r
435         (++) HAL_I2S_ErrorCallback()\r
436 \r
437 @endverbatim\r
438   * @{\r
439   */\r
440 \r
441 /**\r
442   * @brief Transmit an amount of data in blocking mode\r
443   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
444   *         the configuration information for I2S module\r
445   * @param pData: a 16-bit pointer to data buffer.\r
446   * @param Size: number of data sample to be sent:\r
447   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S\r
448   *       configuration phase, the Size parameter means the number of 16-bit data length \r
449   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected \r
450   *       the Size parameter means the number of 16-bit data length. \r
451   * @param  Timeout: Timeout duration\r
452   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization \r
453   *       between Master and Slave(example: audio streaming).\r
454   * @retval HAL status\r
455   */\r
456 HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)\r
457 {\r
458   if((pData == NULL ) || (Size == 0)) \r
459   {\r
460     return  HAL_ERROR;                                    \r
461   }\r
462   \r
463   if(hi2s->State == HAL_I2S_STATE_READY)\r
464   { \r
465     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\\r
466        ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))\r
467     {\r
468       hi2s->TxXferSize = (Size << 1);\r
469       hi2s->TxXferCount = (Size << 1);\r
470     }\r
471     else\r
472     {\r
473       hi2s->TxXferSize = Size;\r
474       hi2s->TxXferCount = Size;\r
475     }\r
476     \r
477     /* Process Locked */\r
478     __HAL_LOCK(hi2s);\r
479     \r
480     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;\r
481     hi2s->State = HAL_I2S_STATE_BUSY_TX;\r
482    \r
483     /* Check if the I2S is already enabled */ \r
484     if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)\r
485     {\r
486       /* Enable I2S peripheral */    \r
487       __HAL_I2S_ENABLE(hi2s);\r
488     }\r
489     \r
490     while(hi2s->TxXferCount > 0)\r
491     {\r
492       hi2s->Instance->DR = (*pData++);\r
493       hi2s->TxXferCount--;   \r
494       /* Wait until TXE flag is set */\r
495       if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK)\r
496       {\r
497         /* Set the error code and execute error callback*/\r
498         hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;\r
499         HAL_I2S_ErrorCallback(hi2s);\r
500         return HAL_TIMEOUT;\r
501       }\r
502 \r
503       /* Check if an underrun occurs */\r
504       if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET) \r
505       {\r
506         /* Set the I2S State ready */\r
507         hi2s->State = HAL_I2S_STATE_READY; \r
508 \r
509         /* Process Unlocked */\r
510         __HAL_UNLOCK(hi2s);\r
511 \r
512         /* Set the error code and execute error callback*/\r
513         hi2s->ErrorCode |= HAL_I2S_ERROR_UDR;\r
514         HAL_I2S_ErrorCallback(hi2s);\r
515 \r
516         return HAL_ERROR;\r
517       }\r
518     }      \r
519     \r
520     /* Wait until Busy flag is reset */\r
521     if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, RESET, Timeout) != HAL_OK) \r
522     {\r
523       /* Set the error code and execute error callback*/\r
524       hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;\r
525       HAL_I2S_ErrorCallback(hi2s);\r
526       return HAL_TIMEOUT;\r
527     }\r
528     \r
529     hi2s->State = HAL_I2S_STATE_READY; \r
530     \r
531     /* Process Unlocked */\r
532     __HAL_UNLOCK(hi2s);\r
533     \r
534     return HAL_OK;\r
535   }\r
536   else\r
537   {\r
538     return HAL_BUSY;\r
539   }\r
540 }\r
541 \r
542 /**\r
543   * @brief Receive an amount of data in blocking mode \r
544   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
545   *         the configuration information for I2S module\r
546   * @param pData: a 16-bit pointer to data buffer.\r
547   * @param Size: number of data sample to be sent:\r
548   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S\r
549   *       configuration phase, the Size parameter means the number of 16-bit data length \r
550   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected \r
551   *       the Size parameter means the number of 16-bit data length. \r
552   * @param Timeout: Timeout duration\r
553   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization \r
554   *       between Master and Slave(example: audio streaming).\r
555   * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate\r
556   *       in continuous way and as the I2S is not disabled at the end of the I2S transaction.\r
557   * @retval HAL status\r
558   */\r
559 HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)\r
560 {\r
561   if((pData == NULL ) || (Size == 0)) \r
562   {\r
563     return  HAL_ERROR;                                    \r
564   }\r
565   \r
566   if(hi2s->State == HAL_I2S_STATE_READY)\r
567   { \r
568     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\\r
569        ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))\r
570     {\r
571       hi2s->RxXferSize = (Size << 1);\r
572       hi2s->RxXferCount = (Size << 1);\r
573     }\r
574     else\r
575     {\r
576       hi2s->RxXferSize = Size;\r
577       hi2s->RxXferCount = Size;\r
578     }\r
579     /* Process Locked */\r
580     __HAL_LOCK(hi2s);\r
581     \r
582     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;\r
583     hi2s->State = HAL_I2S_STATE_BUSY_RX;\r
584         \r
585     /* Check if the I2S is already enabled */ \r
586     if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)\r
587     {\r
588       /* Enable I2S peripheral */    \r
589       __HAL_I2S_ENABLE(hi2s);\r
590     }\r
591     \r
592     /* Check if Master Receiver mode is selected */\r
593     if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)\r
594     {\r
595       /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read\r
596       access to the SPI_SR register. */ \r
597       __HAL_I2S_CLEAR_OVRFLAG(hi2s);        \r
598     }\r
599     \r
600     /* Receive data */\r
601     while(hi2s->RxXferCount > 0)\r
602     {\r
603       /* Wait until RXNE flag is set */\r
604       if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK) \r
605       {\r
606         /* Set the error code and execute error callback*/\r
607         hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;\r
608         HAL_I2S_ErrorCallback(hi2s);\r
609         return HAL_TIMEOUT;\r
610       }\r
611       \r
612       /* Check if an overrun occurs */\r
613       if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET) \r
614       {\r
615         /* Set the I2S State ready */\r
616         hi2s->State = HAL_I2S_STATE_READY; \r
617 \r
618         /* Process Unlocked */\r
619         __HAL_UNLOCK(hi2s);\r
620 \r
621         /* Set the error code and execute error callback*/\r
622         hi2s->ErrorCode |= HAL_I2S_ERROR_OVR;\r
623         HAL_I2S_ErrorCallback(hi2s);\r
624 \r
625         return HAL_ERROR;\r
626       }\r
627 \r
628       (*pData++) = hi2s->Instance->DR;\r
629       hi2s->RxXferCount--;\r
630     }      \r
631 \r
632     hi2s->State = HAL_I2S_STATE_READY; \r
633     \r
634     /* Process Unlocked */\r
635     __HAL_UNLOCK(hi2s);\r
636     \r
637     return HAL_OK;\r
638   }\r
639   else\r
640   {\r
641     return HAL_BUSY;\r
642   }\r
643 }\r
644 \r
645 /**\r
646   * @brief Transmit an amount of data in non-blocking mode with Interrupt\r
647   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
648   *         the configuration information for I2S module\r
649   * @param pData: a 16-bit pointer to data buffer.\r
650   * @param Size: number of data sample to be sent:\r
651   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S\r
652   *       configuration phase, the Size parameter means the number of 16-bit data length \r
653   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected \r
654   *       the Size parameter means the number of 16-bit data length. \r
655   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization \r
656   *       between Master and Slave(example: audio streaming).\r
657   * @retval HAL status\r
658   */\r
659 HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)\r
660 {\r
661   if(hi2s->State == HAL_I2S_STATE_READY)\r
662   {\r
663     if((pData == NULL) || (Size == 0)) \r
664     {\r
665       return  HAL_ERROR;                                    \r
666     }\r
667     \r
668     hi2s->pTxBuffPtr = pData;\r
669     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\\r
670       ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))\r
671     {\r
672       hi2s->TxXferSize = (Size << 1);\r
673       hi2s->TxXferCount = (Size << 1);\r
674     }  \r
675     else\r
676     {\r
677       hi2s->TxXferSize = Size;\r
678       hi2s->TxXferCount = Size;\r
679     }\r
680     \r
681     /* Process Locked */\r
682     __HAL_LOCK(hi2s);\r
683     \r
684     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;\r
685     hi2s->State = HAL_I2S_STATE_BUSY_TX;\r
686 \r
687     /* Enable TXE and ERR interrupt */\r
688     __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));\r
689     \r
690     /* Check if the I2S is already enabled */ \r
691     if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)\r
692     {\r
693       /* Enable I2S peripheral */    \r
694       __HAL_I2S_ENABLE(hi2s);\r
695     }\r
696     \r
697     /* Process Unlocked */\r
698     __HAL_UNLOCK(hi2s);\r
699     \r
700     return HAL_OK;\r
701   }\r
702   else\r
703   {\r
704     return HAL_BUSY;\r
705   }\r
706 }\r
707 \r
708 /**\r
709   * @brief Receive an amount of data in non-blocking mode with Interrupt\r
710   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
711   *         the configuration information for I2S module\r
712   * @param pData: a 16-bit pointer to the Receive data buffer.\r
713   * @param Size: number of data sample to be sent:\r
714   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S\r
715   *       configuration phase, the Size parameter means the number of 16-bit data length \r
716   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected \r
717   *       the Size parameter means the number of 16-bit data length. \r
718   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization \r
719   *       between Master and Slave(example: audio streaming).\r
720   * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronisation \r
721   * between Master and Slave otherwise the I2S interrupt should be optimized. \r
722   * @retval HAL status\r
723   */\r
724 HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)\r
725 {\r
726   if(hi2s->State == HAL_I2S_STATE_READY)\r
727   {\r
728     if((pData == NULL) || (Size == 0)) \r
729     {\r
730       return  HAL_ERROR;                                    \r
731     }\r
732     \r
733     hi2s->pRxBuffPtr = pData;\r
734     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\\r
735       ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))\r
736     {\r
737       hi2s->RxXferSize = (Size << 1);\r
738       hi2s->RxXferCount = (Size << 1);\r
739     }  \r
740     else\r
741     {\r
742       hi2s->RxXferSize = Size;\r
743       hi2s->RxXferCount = Size;\r
744     }\r
745     /* Process Locked */\r
746     __HAL_LOCK(hi2s);\r
747     \r
748     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;\r
749     hi2s->State = HAL_I2S_STATE_BUSY_RX;\r
750 \r
751     /* Enable TXE and ERR interrupt */\r
752     __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));\r
753     \r
754     /* Check if the I2S is already enabled */ \r
755     if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)\r
756     {\r
757       /* Enable I2S peripheral */    \r
758       __HAL_I2S_ENABLE(hi2s);\r
759     }\r
760     \r
761     /* Process Unlocked */\r
762     __HAL_UNLOCK(hi2s);\r
763     \r
764     return HAL_OK;\r
765   }\r
766   else\r
767   {\r
768     return HAL_BUSY; \r
769   } \r
770 }\r
771 \r
772 /**\r
773   * @brief Transmit an amount of data in non-blocking mode with DMA\r
774   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
775   *         the configuration information for I2S module\r
776   * @param pData: a 16-bit pointer to the Transmit data buffer.\r
777   * @param Size: number of data sample to be sent:\r
778   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S\r
779   *       configuration phase, the Size parameter means the number of 16-bit data length \r
780   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected \r
781   *       the Size parameter means the number of 16-bit data length. \r
782   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization \r
783   *       between Master and Slave(example: audio streaming).\r
784   * @retval HAL status\r
785   */\r
786 HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)\r
787 {\r
788   uint32_t *tmp;\r
789   \r
790   if((pData == NULL) || (Size == 0)) \r
791   {\r
792     return  HAL_ERROR;                                    \r
793   }\r
794   \r
795   if(hi2s->State == HAL_I2S_STATE_READY)\r
796   {  \r
797     hi2s->pTxBuffPtr = pData;\r
798     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\\r
799       ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))\r
800     {\r
801       hi2s->TxXferSize = (Size << 1);\r
802       hi2s->TxXferCount = (Size << 1);\r
803     }  \r
804     else\r
805     {\r
806       hi2s->TxXferSize = Size;\r
807       hi2s->TxXferCount = Size;\r
808     }  \r
809     \r
810     /* Process Locked */\r
811     __HAL_LOCK(hi2s);\r
812     \r
813     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;\r
814     hi2s->State = HAL_I2S_STATE_BUSY_TX;\r
815 \r
816     /* Set the I2S Tx DMA Half transfer complete callback */\r
817     hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt;\r
818 \r
819     /* Set the I2S TxDMA transfer complete callback */\r
820     hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt;\r
821     \r
822     /* Set the DMA error callback */\r
823     hi2s->hdmatx->XferErrorCallback = I2S_DMAError;\r
824     \r
825     /* Enable the Tx DMA Channel */\r
826     tmp = (uint32_t*)&pData;\r
827     HAL_DMA_Start_IT(hi2s->hdmatx, *(uint32_t*)tmp, (uint32_t)&hi2s->Instance->DR, hi2s->TxXferSize);\r
828     \r
829     /* Check if the I2S is already enabled */ \r
830     if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)\r
831     {\r
832       /* Enable I2S peripheral */    \r
833       __HAL_I2S_ENABLE(hi2s);\r
834     }\r
835     \r
836     /* Enable Tx DMA Request */  \r
837     hi2s->Instance->CR2 |= SPI_CR2_TXDMAEN;\r
838 \r
839     /* Process Unlocked */\r
840     __HAL_UNLOCK(hi2s);\r
841     \r
842     return HAL_OK;\r
843   }\r
844   else\r
845   {\r
846     return HAL_BUSY;\r
847   }\r
848 }\r
849 \r
850 /**\r
851   * @brief Receive an amount of data in non-blocking mode with DMA \r
852   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
853   *         the configuration information for I2S module\r
854   * @param pData: a 16-bit pointer to the Receive data buffer.\r
855   * @param Size: number of data sample to be sent:\r
856   * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S\r
857   *       configuration phase, the Size parameter means the number of 16-bit data length \r
858   *       in the transaction and when a 24-bit data frame or a 32-bit data frame is selected \r
859   *       the Size parameter means the number of 16-bit data length. \r
860   * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization \r
861   *       between Master and Slave(example: audio streaming).\r
862   * @retval HAL status\r
863   */\r
864 HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)\r
865 {\r
866   uint32_t *tmp;\r
867   \r
868   if((pData == NULL) || (Size == 0)) \r
869   {\r
870     return  HAL_ERROR;                                    \r
871   } \r
872     \r
873   if(hi2s->State == HAL_I2S_STATE_READY)\r
874   {    \r
875     hi2s->pRxBuffPtr = pData;\r
876     if(((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_24B)||\\r
877       ((hi2s->Instance->I2SCFGR & (SPI_I2SCFGR_DATLEN | SPI_I2SCFGR_CHLEN)) == I2S_DATAFORMAT_32B))\r
878     {\r
879       hi2s->RxXferSize = (Size << 1);\r
880       hi2s->RxXferCount = (Size << 1);\r
881     }  \r
882     else\r
883     {\r
884       hi2s->RxXferSize = Size;\r
885       hi2s->RxXferCount = Size;\r
886     }\r
887     /* Process Locked */\r
888     __HAL_LOCK(hi2s);\r
889     \r
890     hi2s->ErrorCode = HAL_I2S_ERROR_NONE;\r
891     hi2s->State = HAL_I2S_STATE_BUSY_RX;\r
892    \r
893     /* Set the I2S Rx DMA Half transfer complete callback */\r
894     hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt;\r
895 \r
896     /* Set the I2S Rx DMA transfer complete callback */\r
897     hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt;\r
898     \r
899     /* Set the DMA error callback */\r
900     hi2s->hdmarx->XferErrorCallback = I2S_DMAError;\r
901     \r
902     /* Check if Master Receiver mode is selected */\r
903     if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)\r
904     {\r
905       /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read\r
906       access to the SPI_SR register. */ \r
907       __HAL_I2S_CLEAR_OVRFLAG(hi2s);        \r
908     }\r
909     \r
910     /* Enable the Rx DMA Channel */\r
911     tmp = (uint32_t*)&pData;        \r
912     HAL_DMA_Start_IT(hi2s->hdmarx, (uint32_t)&hi2s->Instance->DR, *(uint32_t*)tmp, hi2s->RxXferSize);\r
913     \r
914     /* Check if the I2S is already enabled */ \r
915     if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)\r
916     {\r
917       /* Enable I2S peripheral */    \r
918       __HAL_I2S_ENABLE(hi2s);\r
919     }\r
920     \r
921     /* Enable Rx DMA Request */  \r
922     hi2s->Instance->CR2 |= SPI_CR2_RXDMAEN;\r
923     \r
924     /* Process Unlocked */\r
925     __HAL_UNLOCK(hi2s);\r
926 \r
927     return HAL_OK;\r
928   }\r
929   else\r
930   {\r
931     return HAL_BUSY;\r
932   }\r
933 }\r
934 \r
935 /**\r
936   * @brief Pauses the audio stream playing from the Media.\r
937   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
938   *         the configuration information for I2S module\r
939   * @retval HAL status\r
940   */\r
941 HAL_StatusTypeDef HAL_I2S_DMAPause(I2S_HandleTypeDef *hi2s)\r
942 {\r
943   /* Process Locked */\r
944   __HAL_LOCK(hi2s);\r
945 \r
946   if(hi2s->State == HAL_I2S_STATE_BUSY_TX)\r
947   {\r
948     /* Disable the I2S DMA Tx request */\r
949     hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);\r
950   }\r
951   else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)\r
952   {\r
953     /* Disable the I2S DMA Rx request */\r
954     hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);\r
955   }\r
956   else if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)\r
957   {\r
958     if((hi2s->Init.Mode == I2S_MODE_SLAVE_TX)||(hi2s->Init.Mode == I2S_MODE_MASTER_TX))\r
959     {\r
960       /* Disable the I2S DMA Tx request */\r
961       hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);\r
962     }\r
963     else\r
964     {\r
965       /* Disable the I2S DMA Rx request */\r
966       hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);\r
967     }\r
968   }\r
969 \r
970   /* Process Unlocked */\r
971   __HAL_UNLOCK(hi2s);\r
972   \r
973   return HAL_OK; \r
974 }\r
975 \r
976 /**\r
977   * @brief Resumes the audio stream playing from the Media.\r
978   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
979   *         the configuration information for I2S module\r
980   * @retval HAL status\r
981   */\r
982 HAL_StatusTypeDef HAL_I2S_DMAResume(I2S_HandleTypeDef *hi2s)\r
983 {\r
984   /* Process Locked */\r
985   __HAL_LOCK(hi2s);\r
986   \r
987   if(hi2s->State == HAL_I2S_STATE_BUSY_TX)\r
988   {\r
989     /* Enable the I2S DMA Tx request */\r
990     SET_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);\r
991   }\r
992   else if(hi2s->State == HAL_I2S_STATE_BUSY_RX)\r
993   {\r
994     /* Enable the I2S DMA Rx request */\r
995     SET_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);\r
996   }\r
997   \r
998   /* If the I2S peripheral is still not enabled, enable it */\r
999   if(HAL_IS_BIT_CLR(hi2s->Instance->I2SCFGR, SPI_I2SCFGR_I2SE))\r
1000   {\r
1001     /* Enable I2S peripheral */    \r
1002     __HAL_I2S_ENABLE(hi2s);\r
1003   }\r
1004   \r
1005   /* Process Unlocked */\r
1006   __HAL_UNLOCK(hi2s);\r
1007   \r
1008   return HAL_OK;\r
1009 }\r
1010 \r
1011 /**\r
1012   * @brief Stops the audio stream playing from the Media.\r
1013   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
1014   *         the configuration information for I2S module\r
1015   * @retval HAL status\r
1016   */\r
1017 HAL_StatusTypeDef HAL_I2S_DMAStop(I2S_HandleTypeDef *hi2s)\r
1018 {\r
1019   /* Process Locked */\r
1020   __HAL_LOCK(hi2s);\r
1021   \r
1022   /* Disable the I2S Tx/Rx DMA requests */\r
1023   CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_TXDMAEN);\r
1024   CLEAR_BIT(hi2s->Instance->CR2, SPI_CR2_RXDMAEN);\r
1025   \r
1026   /* Abort the I2S DMA Channel tx */\r
1027   if(hi2s->hdmatx != NULL)\r
1028   {\r
1029     /* Disable the I2S DMA channel */\r
1030     __HAL_DMA_DISABLE(hi2s->hdmatx);\r
1031     HAL_DMA_Abort(hi2s->hdmatx);\r
1032   }\r
1033   /* Abort the I2S DMA Channel rx */\r
1034   if(hi2s->hdmarx != NULL)\r
1035   {\r
1036     /* Disable the I2S DMA channel */\r
1037     __HAL_DMA_DISABLE(hi2s->hdmarx);\r
1038     HAL_DMA_Abort(hi2s->hdmarx);\r
1039   }\r
1040 \r
1041   /* Disable I2S peripheral */\r
1042   __HAL_I2S_DISABLE(hi2s);\r
1043   \r
1044   hi2s->State = HAL_I2S_STATE_READY;\r
1045   \r
1046   /* Process Unlocked */\r
1047   __HAL_UNLOCK(hi2s);\r
1048   \r
1049   return HAL_OK;\r
1050 }\r
1051 \r
1052 /**\r
1053   * @brief  This function handles I2S interrupt request.\r
1054   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
1055   *         the configuration information for I2S module\r
1056   * @retval HAL status\r
1057   */\r
1058 void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)\r
1059 {  \r
1060   __IO uint32_t i2ssr = hi2s->Instance->SR;\r
1061 \r
1062   if(hi2s->State == HAL_I2S_STATE_BUSY_RX)\r
1063   {  \r
1064     /* I2S in mode Receiver ----------------------------------------------------*/\r
1065     if(((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_RXNE) != RESET))\r
1066     {\r
1067       I2S_Receive_IT(hi2s);\r
1068     }\r
1069 \r
1070     /* I2S Overrun error interrupt occurred -------------------------------------*/\r
1071     if(((i2ssr & I2S_FLAG_OVR) == I2S_FLAG_OVR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))\r
1072     {\r
1073       /* Disable RXNE and ERR interrupt */\r
1074       __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));\r
1075 \r
1076       /* Set the I2S State ready */\r
1077       hi2s->State = HAL_I2S_STATE_READY; \r
1078 \r
1079       /* Set the error code and execute error callback*/\r
1080       hi2s->ErrorCode |= HAL_I2S_ERROR_OVR;\r
1081       HAL_I2S_ErrorCallback(hi2s);\r
1082     }  \r
1083   }\r
1084   else if(hi2s->State == HAL_I2S_STATE_BUSY_TX)\r
1085   {  \r
1086     /* I2S in mode Transmitter ---------------------------------------------------*/\r
1087     if(((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_TXE) != RESET))\r
1088     {     \r
1089       I2S_Transmit_IT(hi2s);\r
1090     } \r
1091     \r
1092     /* I2S Underrun error interrupt occurred ------------------------------------*/\r
1093     if(((i2ssr & I2S_FLAG_UDR) == I2S_FLAG_UDR) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_ERR) != RESET))\r
1094     {\r
1095       /* Disable TXE and ERR interrupt */\r
1096       __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));\r
1097 \r
1098       /* Set the I2S State ready */\r
1099       hi2s->State = HAL_I2S_STATE_READY; \r
1100 \r
1101       /* Set the error code and execute error callback*/\r
1102       hi2s->ErrorCode |= HAL_I2S_ERROR_UDR;\r
1103       HAL_I2S_ErrorCallback(hi2s);\r
1104     }\r
1105   }\r
1106 }\r
1107 \r
1108 /**\r
1109   * @}\r
1110   */\r
1111 \r
1112 /**\r
1113   * @}\r
1114   */\r
1115 \r
1116 /** @addtogroup I2S_Private_Functions I2S Private Functions\r
1117   * @{\r
1118   */\r
1119 /**\r
1120   * @brief This function handles I2S Communication Timeout.\r
1121   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
1122   *         the configuration information for I2S module\r
1123   * @param Flag: Flag checked\r
1124   * @param State: Value of the flag expected\r
1125   * @param Timeout: Duration of the timeout\r
1126   * @retval HAL status\r
1127   */\r
1128 static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag, \r
1129                                                        uint32_t State, uint32_t Timeout)\r
1130 {\r
1131   uint32_t tickstart = 0;\r
1132   \r
1133   /* Get tick */\r
1134   tickstart = HAL_GetTick();\r
1135   \r
1136   /* Wait until flag is set */\r
1137   if(State == RESET)\r
1138   {\r
1139     while(__HAL_I2S_GET_FLAG(hi2s, Flag) == RESET)\r
1140     {\r
1141       if(Timeout != HAL_MAX_DELAY)\r
1142       {\r
1143         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
1144         {\r
1145           /* Set the I2S State ready */\r
1146           hi2s->State= HAL_I2S_STATE_READY;\r
1147 \r
1148           /* Process Unlocked */\r
1149           __HAL_UNLOCK(hi2s);\r
1150 \r
1151           return HAL_TIMEOUT;\r
1152         }\r
1153       }\r
1154     }\r
1155   }\r
1156   else\r
1157   {\r
1158     while(__HAL_I2S_GET_FLAG(hi2s, Flag) != RESET)\r
1159     {\r
1160       if(Timeout != HAL_MAX_DELAY)\r
1161       {\r
1162         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
1163         {\r
1164           /* Set the I2S State ready */\r
1165           hi2s->State= HAL_I2S_STATE_READY;\r
1166 \r
1167           /* Process Unlocked */\r
1168           __HAL_UNLOCK(hi2s);\r
1169 \r
1170           return HAL_TIMEOUT;\r
1171         }\r
1172       }\r
1173     }\r
1174   }\r
1175   return HAL_OK;    \r
1176 }\r
1177 /**\r
1178   * @}\r
1179   */\r
1180 \r
1181 /** @addtogroup I2S_Exported_Functions I2S Exported Functions\r
1182   * @{\r
1183   */\r
1184 \r
1185 /** @addtogroup  I2S_Exported_Functions_Group2 Input and Output operation functions \r
1186   * @{\r
1187   */\r
1188 /**\r
1189   * @brief Tx Transfer Half completed callbacks\r
1190   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
1191   *         the configuration information for I2S module\r
1192   * @retval None\r
1193   */\r
1194  __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)\r
1195 {\r
1196   /* NOTE : This function Should not be modified, when the callback is needed,\r
1197             the HAL_I2S_TxHalfCpltCallback could be implemented in the user file\r
1198    */ \r
1199 }\r
1200 \r
1201 /**\r
1202   * @brief Tx Transfer completed callbacks\r
1203   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
1204   *         the configuration information for I2S module\r
1205   * @retval None\r
1206   */\r
1207  __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)\r
1208 {\r
1209   /* NOTE : This function Should not be modified, when the callback is needed,\r
1210             the HAL_I2S_TxCpltCallback could be implemented in the user file\r
1211    */ \r
1212 }\r
1213 \r
1214 /**\r
1215   * @brief Rx Transfer half completed callbacks\r
1216   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
1217   *         the configuration information for I2S module\r
1218   * @retval None\r
1219   */\r
1220 __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)\r
1221 {\r
1222   /* NOTE : This function Should not be modified, when the callback is needed,\r
1223             the HAL_I2S_RxCpltCallback could be implemented in the user file\r
1224    */\r
1225 }\r
1226 \r
1227 /**\r
1228   * @brief Rx Transfer completed callbacks\r
1229   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
1230   *         the configuration information for I2S module\r
1231   * @retval None\r
1232   */\r
1233 __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)\r
1234 {\r
1235   /* NOTE : This function Should not be modified, when the callback is needed,\r
1236             the HAL_I2S_RxCpltCallback could be implemented in the user file\r
1237    */\r
1238 }\r
1239 \r
1240 /**\r
1241   * @brief I2S error callbacks\r
1242   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
1243   *         the configuration information for I2S module\r
1244   * @retval None\r
1245   */\r
1246  __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)\r
1247 {\r
1248   /* NOTE : This function Should not be modified, when the callback is needed,\r
1249             the HAL_I2S_ErrorCallback could be implemented in the user file\r
1250    */ \r
1251 }\r
1252 \r
1253 /**\r
1254   * @}\r
1255   */\r
1256 \r
1257 /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions \r
1258   *  @brief   Peripheral State functions \r
1259   *\r
1260 @verbatim   \r
1261  ===============================================================================\r
1262                       ##### Peripheral State and Errors functions #####\r
1263  ===============================================================================  \r
1264     [..]\r
1265     This subsection permits to get in run-time the status of the peripheral \r
1266     and the data flow.\r
1267 \r
1268 @endverbatim\r
1269   * @{\r
1270   */\r
1271 \r
1272 /**\r
1273   * @brief  Return the I2S state\r
1274   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
1275   *         the configuration information for I2S module\r
1276   * @retval HAL state\r
1277   */\r
1278 HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s)\r
1279 {\r
1280   return hi2s->State;\r
1281 }\r
1282 \r
1283 /**\r
1284   * @brief  Return the I2S error code\r
1285   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
1286   *         the configuration information for I2S module\r
1287   * @retval I2S Error Code\r
1288   */\r
1289 uint32_t HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)\r
1290 {\r
1291   return hi2s->ErrorCode;\r
1292 }\r
1293 /**\r
1294   * @}\r
1295   */  \r
1296 \r
1297 /**\r
1298   * @}\r
1299   */\r
1300 \r
1301   /**\r
1302   * @brief  Get I2S Input Clock based on I2S source clock selection\r
1303   * @param  hsai: pointer to a I2S_HandleTypeDef structure that contains\r
1304   *               the configuration information for I2S module.   \r
1305   * @retval I2S Clock Input \r
1306   */\r
1307 static uint32_t I2S_GetClockFreq(I2S_HandleTypeDef *hi2s)   \r
1308 {\r
1309   uint32_t tmpreg = 0;\r
1310   /* This variable used to store the VCO Input (value in Hz) */\r
1311   uint32_t vcoinput = 0;\r
1312   /* This variable used to store the I2S_CK_x (value in Hz) */\r
1313   uint32_t i2sclocksource = 0;\r
1314 \r
1315   /* Configure I2S Clock based on I2S source clock selection */ \r
1316   \r
1317   /* I2S_CLK_x : I2S Block Clock configuration for different clock sources selected */\r
1318   switch(hi2s->Init.ClockSource)\r
1319   {\r
1320     case I2S_CLOCK_SYSCLK :\r
1321     {\r
1322       /* Configure the PLLI2S division factor */\r
1323       /* PLLI2S_VCO Input  = PLL_SOURCE/PLLI2SM */ \r
1324       if((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLSOURCE_HSI)\r
1325       {\r
1326         /* In Case the PLL Source is HSI (Internal Clock) */\r
1327         vcoinput = (HSI_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM));\r
1328       }\r
1329       else\r
1330       {\r
1331         /* In Case the PLL Source is HSE (External Clock) */\r
1332         vcoinput = ((HSE_VALUE / (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM)));\r
1333       }\r
1334 \r
1335       /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */\r
1336       /* I2S_CLK(first level) = PLLI2S_VCO Output/PLLI2SR */\r
1337       tmpreg = (RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> 28;\r
1338       i2sclocksource = (vcoinput * ((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> 6))/(tmpreg);\r
1339     \r
1340       break;\r
1341     }\r
1342     case I2S_CLOCK_EXTERNAL :\r
1343     {\r
1344       i2sclocksource = EXTERNAL_CLOCK_VALUE;\r
1345       break;\r
1346     }\r
1347     default :\r
1348     {\r
1349       break;\r
1350     }\r
1351   }\r
1352 \r
1353   /* the return result is the value of I2S clock */\r
1354   return i2sclocksource; \r
1355 }\r
1356 \r
1357 /** @addtogroup I2S_Private_Functions I2S Private Functions\r
1358   * @{\r
1359   */\r
1360 /**\r
1361   * @brief DMA I2S transmit process complete callback \r
1362   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
1363   *                the configuration information for the specified DMA module.\r
1364   * @retval None\r
1365   */\r
1366 static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma)   \r
1367 {\r
1368   I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;\r
1369   \r
1370   if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)\r
1371   {\r
1372     hi2s->TxXferCount = 0;\r
1373 \r
1374     /* Disable Tx DMA Request */\r
1375     hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);\r
1376     \r
1377     if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)\r
1378     {\r
1379       if(hi2s->RxXferCount == 0)\r
1380       {\r
1381         hi2s->State = HAL_I2S_STATE_READY;\r
1382       }\r
1383     }\r
1384     else\r
1385     {\r
1386       hi2s->State = HAL_I2S_STATE_READY; \r
1387     }\r
1388   }\r
1389   HAL_I2S_TxCpltCallback(hi2s);\r
1390 }\r
1391 \r
1392 /**\r
1393   * @brief DMA I2S transmit process half complete callback \r
1394   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
1395   *                the configuration information for the specified DMA module.\r
1396   * @retval None\r
1397   */\r
1398 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma)\r
1399 {\r
1400   I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;\r
1401 \r
1402   HAL_I2S_TxHalfCpltCallback(hi2s);\r
1403 }\r
1404 \r
1405 /**\r
1406   * @brief DMA I2S receive process complete callback \r
1407   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
1408   *                the configuration information for the specified DMA module.\r
1409   * @retval None\r
1410   */\r
1411 static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma)   \r
1412 {\r
1413   I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;\r
1414 \r
1415   if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)\r
1416   {\r
1417     /* Disable Rx DMA Request */\r
1418     hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);\r
1419 \r
1420     hi2s->RxXferCount = 0;\r
1421     if(hi2s->State == HAL_I2S_STATE_BUSY_TX_RX)\r
1422     {\r
1423       if(hi2s->TxXferCount == 0)\r
1424       {\r
1425         hi2s->State = HAL_I2S_STATE_READY;\r
1426       }\r
1427     }\r
1428     else\r
1429     {\r
1430       hi2s->State = HAL_I2S_STATE_READY; \r
1431     }\r
1432   }\r
1433   HAL_I2S_RxCpltCallback(hi2s); \r
1434 }\r
1435       \r
1436 /**\r
1437   * @brief DMA I2S receive process half complete callback \r
1438   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
1439   *                the configuration information for the specified DMA module.\r
1440   * @retval None\r
1441   */\r
1442 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma)\r
1443 {\r
1444   I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;\r
1445 \r
1446   HAL_I2S_RxHalfCpltCallback(hi2s); \r
1447 }\r
1448 \r
1449 /**\r
1450   * @brief DMA I2S communication error callback \r
1451   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
1452   *                the configuration information for the specified DMA module.\r
1453   * @retval None\r
1454   */\r
1455 static void I2S_DMAError(DMA_HandleTypeDef *hdma)   \r
1456 {\r
1457   I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
1458   \r
1459   /* Disable Rx and Tx DMA Request */\r
1460   hi2s->Instance->CR2 &= (uint32_t)(~(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN));\r
1461   hi2s->TxXferCount = 0;\r
1462   hi2s->RxXferCount = 0;\r
1463   \r
1464   hi2s->State= HAL_I2S_STATE_READY;\r
1465 \r
1466   /* Set the error code and execute error callback*/\r
1467   hi2s->ErrorCode |= HAL_I2S_ERROR_DMA;\r
1468   HAL_I2S_ErrorCallback(hi2s);\r
1469 }\r
1470 \r
1471 /**\r
1472   * @brief Transmit an amount of data in non-blocking mode with Interrupt\r
1473   * @param  hi2s: pointer to a I2S_HandleTypeDef structure that contains\r
1474   *         the configuration information for I2S module\r
1475   * @retval None\r
1476   */\r
1477 static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s)\r
1478 {\r
1479   /* Transmit data */\r
1480   hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);\r
1481   hi2s->TxXferCount--;  \r
1482 \r
1483   if(hi2s->TxXferCount == 0)\r
1484   {\r
1485     /* Disable TXE and ERR interrupt */\r
1486     __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));\r
1487 \r
1488     hi2s->State = HAL_I2S_STATE_READY;\r
1489     HAL_I2S_TxCpltCallback(hi2s);\r
1490   }\r
1491 }\r
1492 \r
1493 /**\r
1494   * @brief Receive an amount of data in non-blocking mode with Interrupt\r
1495   * @param hi2s: I2S handle\r
1496   * @retval None\r
1497   */\r
1498 static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s)\r
1499 {\r
1500   /* Receive data */    \r
1501   (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;\r
1502   hi2s->RxXferCount--;\r
1503 \r
1504   if(hi2s->RxXferCount == 0)\r
1505   {    \r
1506     /* Disable RXNE and ERR interrupt */\r
1507     __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));\r
1508 \r
1509     hi2s->State = HAL_I2S_STATE_READY;     \r
1510     HAL_I2S_RxCpltCallback(hi2s); \r
1511   }\r
1512 }\r
1513 /**\r
1514   * @}\r
1515   */\r
1516   \r
1517 #endif /* HAL_I2S_MODULE_ENABLED */\r
1518 /**\r
1519   * @}\r
1520   */\r
1521 \r
1522 /**\r
1523   * @}\r
1524   */\r
1525 \r
1526 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r