2 ******************************************************************************
3 * @file stm32f3xx_hal_i2s.c
4 * @author MCD Application Team
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
14 ===============================================================================
15 ##### How to use this driver #####
16 ===============================================================================
18 The I2S HAL driver can be used as follows:
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
40 (#) Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity
41 using HAL_I2S_Init() function.
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.
51 (#) Three mode of operations are available within this driver :
53 *** Polling mode IO operation ***
54 =================================
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()
59 *** Interrupt mode IO operation ***
60 ===================================
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
75 *** DMA mode IO operation ***
76 ==============================
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()
94 *** I2S HAL driver macros list ***
95 =============================================
97 Below the list of most used macros in I2S HAL driver.
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
106 (@) You can refer to the I2S HAL driver header file for more useful macros
109 ******************************************************************************
112 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
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.
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.
136 ******************************************************************************
139 /* Includes ------------------------------------------------------------------*/
140 #include "stm32f3xx_hal.h"
142 /** @addtogroup STM32F3xx_HAL_Driver
146 /** @defgroup I2S I2S HAL module driver
147 * @brief I2S HAL module driver
151 #ifdef HAL_I2S_MODULE_ENABLED
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)
158 /* Private typedef -----------------------------------------------------------*/
159 /* Private define ------------------------------------------------------------*/
160 /* Private macro -------------------------------------------------------------*/
161 /* Private variables ---------------------------------------------------------*/
162 /* Private function prototypes -----------------------------------------------*/
163 /** @defgroup I2S_Private_Functions I2S Private Functions
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);
178 /* Exported functions ---------------------------------------------------------*/
180 /** @defgroup I2S_Exported_Functions I2S Exported Functions
184 /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions
185 * @brief Initialization and Configuration functions
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:
194 (+) User must Implement HAL_I2S_MspInit() function in which he configures
195 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
197 (+) Call the function HAL_I2S_Init() to configure the selected device with
198 the selected configuration:
205 (++) Full duplex mode
207 (+) Call the function HAL_I2S_DeInit() to restore the default configuration
208 of the selected I2Sx periperal.
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
220 __weak HAL_StatusTypeDef HAL_I2S_Init(I2S_HandleTypeDef *hi2s)
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 */
226 /* Return error status as not implemented here */
231 * @brief DeInitializes the I2S peripheral
232 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
233 * the configuration information for I2S module
236 HAL_StatusTypeDef HAL_I2S_DeInit(I2S_HandleTypeDef *hi2s)
238 /* Check the I2S handle allocation */
244 /* Check the parameters */
245 assert_param(IS_I2S_ALL_INSTANCE(hi2s->Instance));
247 hi2s->State = HAL_I2S_STATE_BUSY;
249 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
250 HAL_I2S_MspDeInit(hi2s);
252 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
253 hi2s->State = HAL_I2S_STATE_RESET;
262 * @brief I2S MSP Init
263 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
264 * the configuration information for I2S module
267 __weak void HAL_I2S_MspInit(I2S_HandleTypeDef *hi2s)
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
275 * @brief I2S MSP DeInit
276 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
277 * the configuration information for I2S module
280 __weak void HAL_I2S_MspDeInit(I2S_HandleTypeDef *hi2s)
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
291 /** @defgroup I2S_Exported_Functions_Group2 Input and Output operation functions
292 * @brief Data transfers functions
295 ===============================================================================
296 ##### IO operation functions #####
297 ===============================================================================
299 This subsection provides a set of functions allowing to manage the I2S data
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
312 (#) Blocking mode functions are :
313 (++) HAL_I2S_Transmit()
314 (++) HAL_I2S_Receive()
316 (#) No-Blocking mode functions with Interrupt are :
317 (++) HAL_I2S_Transmit_IT()
318 (++) HAL_I2S_Receive_IT()
320 (#) No-Blocking mode functions with DMA are :
321 (++) HAL_I2S_Transmit_DMA()
322 (++) HAL_I2S_Receive_DMA()
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()
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).
348 HAL_StatusTypeDef HAL_I2S_Transmit(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
350 if((pData == HAL_NULL ) || (Size == 0))
355 if(hi2s->State == HAL_I2S_STATE_READY)
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))
360 hi2s->TxXferSize = (Size << 1);
361 hi2s->TxXferCount = (Size << 1);
365 hi2s->TxXferSize = Size;
366 hi2s->TxXferCount = Size;
372 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
373 hi2s->State = HAL_I2S_STATE_BUSY_TX;
375 /* Check if the I2S is already enabled */
376 if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
378 /* Enable I2S peripheral */
379 __HAL_I2S_ENABLE(hi2s);
382 while(hi2s->TxXferCount > 0)
384 hi2s->Instance->DR = (*pData++);
386 /* Wait until TXE flag is set */
387 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_TXE, SET, Timeout) != HAL_OK)
389 /* Set the error code and execute error callback*/
390 hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;
391 HAL_I2S_ErrorCallback(hi2s);
395 /* Check if an underrun occurs */
396 if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_UDR) == SET)
398 /* Set the I2S State ready */
399 hi2s->State = HAL_I2S_STATE_READY;
401 /* Process Unlocked */
404 /* Set the error code and execute error callback*/
405 hi2s->ErrorCode |= HAL_I2S_ERROR_UDR;
406 HAL_I2S_ErrorCallback(hi2s);
412 /* Wait until Busy flag is reset */
413 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_BSY, RESET, Timeout) != HAL_OK)
415 /* Set the error code and execute error callback*/
416 hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;
417 HAL_I2S_ErrorCallback(hi2s);
421 hi2s->State = HAL_I2S_STATE_READY;
423 /* Process Unlocked */
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.
451 HAL_StatusTypeDef HAL_I2S_Receive(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size, uint32_t Timeout)
453 if((pData == HAL_NULL ) || (Size == 0))
458 if(hi2s->State == HAL_I2S_STATE_READY)
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))
463 hi2s->RxXferSize = (Size << 1);
464 hi2s->RxXferCount = (Size << 1);
468 hi2s->RxXferSize = Size;
469 hi2s->RxXferCount = Size;
474 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
475 hi2s->State = HAL_I2S_STATE_BUSY_RX;
477 /* Check if the I2S is already enabled */
478 if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
480 /* Enable I2S peripheral */
481 __HAL_I2S_ENABLE(hi2s);
484 /* Check if Master Receiver mode is selected */
485 if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
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);
493 while(hi2s->RxXferCount > 0)
495 /* Wait until RXNE flag is set */
496 if (I2S_WaitFlagStateUntilTimeout(hi2s, I2S_FLAG_RXNE, SET, Timeout) != HAL_OK)
498 /* Set the error code and execute error callback*/
499 hi2s->ErrorCode |= HAL_I2S_ERROR_TIMEOUT;
500 HAL_I2S_ErrorCallback(hi2s);
504 /* Check if an overrun occurs */
505 if(__HAL_I2S_GET_FLAG(hi2s, I2S_FLAG_OVR) == SET)
507 /* Set the I2S State ready */
508 hi2s->State = HAL_I2S_STATE_READY;
510 /* Process Unlocked */
513 /* Set the error code and execute error callback*/
514 hi2s->ErrorCode |= HAL_I2S_ERROR_OVR;
515 HAL_I2S_ErrorCallback(hi2s);
520 (*pData++) = hi2s->Instance->DR;
524 hi2s->State = HAL_I2S_STATE_READY;
526 /* Process Unlocked */
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).
551 HAL_StatusTypeDef HAL_I2S_Transmit_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
553 if(hi2s->State == HAL_I2S_STATE_READY)
555 if((pData == HAL_NULL) || (Size == 0))
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))
564 hi2s->TxXferSize = (Size << 1);
565 hi2s->TxXferCount = (Size << 1);
569 hi2s->TxXferSize = Size;
570 hi2s->TxXferCount = Size;
576 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
577 hi2s->State = HAL_I2S_STATE_BUSY_TX;
579 /* Enable TXE and ERR interrupt */
580 __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
582 /* Check if the I2S is already enabled */
583 if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
585 /* Enable I2S peripheral */
586 __HAL_I2S_ENABLE(hi2s);
589 /* Process Unlocked */
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.
616 HAL_StatusTypeDef HAL_I2S_Receive_IT(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
618 if(hi2s->State == HAL_I2S_STATE_READY)
620 if((pData == HAL_NULL) || (Size == 0))
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))
629 hi2s->RxXferSize = (Size << 1);
630 hi2s->RxXferCount = (Size << 1);
634 hi2s->RxXferSize = Size;
635 hi2s->RxXferCount = Size;
640 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
641 hi2s->State = HAL_I2S_STATE_BUSY_RX;
643 /* Enable TXE and ERR interrupt */
644 __HAL_I2S_ENABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
646 /* Check if the I2S is already enabled */
647 if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
649 /* Enable I2S peripheral */
650 __HAL_I2S_ENABLE(hi2s);
653 /* Process Unlocked */
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).
678 HAL_StatusTypeDef HAL_I2S_Transmit_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
682 if((pData == HAL_NULL) || (Size == 0))
687 if(hi2s->State == HAL_I2S_STATE_READY)
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))
693 hi2s->TxXferSize = (Size << 1);
694 hi2s->TxXferCount = (Size << 1);
698 hi2s->TxXferSize = Size;
699 hi2s->TxXferCount = Size;
705 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
706 hi2s->State = HAL_I2S_STATE_BUSY_TX;
708 /* Set the I2S Tx DMA Half transfer complete callback */
709 hi2s->hdmatx->XferHalfCpltCallback = I2S_DMATxHalfCplt;
711 /* Set the I2S TxDMA transfer complete callback */
712 hi2s->hdmatx->XferCpltCallback = I2S_DMATxCplt;
714 /* Set the DMA error callback */
715 hi2s->hdmatx->XferErrorCallback = I2S_DMAError;
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);
721 /* Check if the I2S is already enabled */
722 if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
724 /* Enable I2S peripheral */
725 __HAL_I2S_ENABLE(hi2s);
728 /* Enable Tx DMA Request */
729 hi2s->Instance->CR2 |= SPI_CR2_TXDMAEN;
731 /* Process Unlocked */
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).
756 HAL_StatusTypeDef HAL_I2S_Receive_DMA(I2S_HandleTypeDef *hi2s, uint16_t *pData, uint16_t Size)
760 if((pData == HAL_NULL) || (Size == 0))
765 if(hi2s->State == HAL_I2S_STATE_READY)
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))
771 hi2s->RxXferSize = (Size << 1);
772 hi2s->RxXferCount = (Size << 1);
776 hi2s->RxXferSize = Size;
777 hi2s->RxXferCount = Size;
782 hi2s->ErrorCode = HAL_I2S_ERROR_NONE;
783 hi2s->State = HAL_I2S_STATE_BUSY_RX;
785 /* Set the I2S Rx DMA Half transfer complete callback */
786 hi2s->hdmarx->XferHalfCpltCallback = I2S_DMARxHalfCplt;
788 /* Set the I2S Rx DMA transfer complete callback */
789 hi2s->hdmarx->XferCpltCallback = I2S_DMARxCplt;
791 /* Set the DMA error callback */
792 hi2s->hdmarx->XferErrorCallback = I2S_DMAError;
794 /* Check if Master Receiver mode is selected */
795 if((hi2s->Instance->I2SCFGR & SPI_I2SCFGR_I2SCFG) == I2S_MODE_MASTER_RX)
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);
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);
806 /* Check if the I2S is already enabled */
807 if((hi2s->Instance->I2SCFGR &SPI_I2SCFGR_I2SE) != SPI_I2SCFGR_I2SE)
809 /* Enable I2S peripheral */
810 __HAL_I2S_ENABLE(hi2s);
813 /* Enable Rx DMA Request */
814 hi2s->Instance->CR2 |= SPI_CR2_RXDMAEN;
816 /* Process Unlocked */
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
833 void HAL_I2S_IRQHandler(I2S_HandleTypeDef *hi2s)
835 __IO uint32_t i2ssr = hi2s->Instance->SR;
837 if(hi2s->State == HAL_I2S_STATE_BUSY_RX)
839 /* I2S in mode Receiver ----------------------------------------------------*/
840 if(((i2ssr & I2S_FLAG_RXNE) == I2S_FLAG_RXNE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_RXNE) != RESET))
842 I2S_Receive_IT(hi2s);
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))
848 /* Disable RXNE and ERR interrupt */
849 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
851 /* Set the I2S State ready */
852 hi2s->State = HAL_I2S_STATE_READY;
854 /* Set the error code and execute error callback*/
855 hi2s->ErrorCode |= HAL_I2S_ERROR_OVR;
856 HAL_I2S_ErrorCallback(hi2s);
859 else if(hi2s->State == HAL_I2S_STATE_BUSY_TX)
861 /* I2S in mode Tramitter ---------------------------------------------------*/
862 if(((i2ssr & I2S_FLAG_TXE) == I2S_FLAG_TXE) && (__HAL_I2S_GET_IT_SOURCE(hi2s, I2S_IT_TXE) != RESET))
864 I2S_Transmit_IT(hi2s);
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))
870 /* Disable TXE and ERR interrupt */
871 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
873 /* Set the I2S State ready */
874 hi2s->State = HAL_I2S_STATE_READY;
876 /* Set the error code and execute error callback*/
877 hi2s->ErrorCode |= HAL_I2S_ERROR_UDR;
878 HAL_I2S_ErrorCallback(hi2s);
891 /** @addtogroup I2S_Private_Functions I2S Private Functions
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
903 static HAL_StatusTypeDef I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef *hi2s, uint32_t Flag,
904 uint32_t State, uint32_t Timeout)
906 uint32_t tickstart = HAL_GetTick();
908 while((__HAL_I2S_GET_FLAG(hi2s, Flag)) != State)
910 if(Timeout != HAL_MAX_DELAY)
912 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
914 /* Set the I2S State ready */
915 hi2s->State= HAL_I2S_STATE_READY;
917 /* Process Unlocked */
931 /** @addtogroup I2S_Exported_Functions I2S Exported Functions
935 /** @addtogroup I2S_Exported_Functions_Group2 Input and Output operation functions
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
944 __weak void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
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
952 * @brief Tx Transfer completed callbacks
953 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
954 * the configuration information for I2S module
957 __weak void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
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
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
970 __weak void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
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
978 * @brief Rx Transfer completed callbacks
979 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
980 * the configuration information for I2S module
983 __weak void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
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
991 * @brief I2S error callbacks
992 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
993 * the configuration information for I2S module
996 __weak void HAL_I2S_ErrorCallback(I2S_HandleTypeDef *hi2s)
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
1007 /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions
1008 * @brief Peripheral State functions
1011 ===============================================================================
1012 ##### Peripheral State and Errors functions #####
1013 ===============================================================================
1015 This subsection permits to get in run-time the status of the peripheral
1023 * @brief Return the I2S state
1024 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1025 * the configuration information for I2S module
1028 HAL_I2S_StateTypeDef HAL_I2S_GetState(I2S_HandleTypeDef *hi2s)
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
1039 HAL_I2S_ErrorTypeDef HAL_I2S_GetError(I2S_HandleTypeDef *hi2s)
1041 return hi2s->ErrorCode;
1051 /** @addtogroup I2S_Private_Functions I2S Private Functions
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.
1060 static void I2S_DMATxCplt(DMA_HandleTypeDef *hdma)
1062 I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1064 /* Disable Tx DMA Request */
1065 hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
1067 hi2s->TxXferCount = 0;
1068 hi2s->State = HAL_I2S_STATE_READY;
1070 HAL_I2S_TxCpltCallback(hi2s);
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.
1079 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
1081 I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1083 HAL_I2S_TxHalfCpltCallback(hi2s);
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.
1092 static void I2S_DMARxCplt(DMA_HandleTypeDef *hdma)
1094 I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1096 /* Disable Rx DMA Request */
1097 hi2s->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
1098 hi2s->RxXferCount = 0;
1100 hi2s->State = HAL_I2S_STATE_READY;
1101 HAL_I2S_RxCpltCallback(hi2s);
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.
1110 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
1112 I2S_HandleTypeDef* hi2s = (I2S_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1114 HAL_I2S_RxHalfCpltCallback(hi2s);
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.
1123 static void I2S_DMAError(DMA_HandleTypeDef *hdma)
1125 I2S_HandleTypeDef* hi2s = ( I2S_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
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;
1132 hi2s->State= HAL_I2S_STATE_READY;
1134 /* Set the error code and execute error callback*/
1135 hi2s->ErrorCode |= HAL_I2S_ERROR_DMA;
1136 HAL_I2S_ErrorCallback(hi2s);
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
1145 static void I2S_Transmit_IT(I2S_HandleTypeDef *hi2s)
1148 hi2s->Instance->DR = (*hi2s->pTxBuffPtr++);
1149 hi2s->TxXferCount--;
1151 if(hi2s->TxXferCount == 0)
1153 /* Disable TXE and ERR interrupt */
1154 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_TXE | I2S_IT_ERR));
1156 hi2s->State = HAL_I2S_STATE_READY;
1157 HAL_I2S_TxCpltCallback(hi2s);
1162 * @brief Receive an amount of data in non-blocking mode with Interrupt
1163 * @param hi2s: I2S handle
1166 static void I2S_Receive_IT(I2S_HandleTypeDef *hi2s)
1169 (*hi2s->pRxBuffPtr++) = hi2s->Instance->DR;
1170 hi2s->RxXferCount--;
1172 if(hi2s->RxXferCount == 0)
1174 /* Disable RXNE and ERR interrupt */
1175 __HAL_I2S_DISABLE_IT(hi2s, (I2S_IT_RXNE | I2S_IT_ERR));
1177 hi2s->State = HAL_I2S_STATE_READY;
1178 HAL_I2S_RxCpltCallback(hi2s);
1185 #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
1186 /* STM32F302xC || STM32F303xC || STM32F358xx || */
1187 /* STM32F301x8 || STM32F302x8 || STM32F318xx || */
1188 /* STM32F373xC || STM32F378xx */
1190 #endif /* HAL_I2S_MODULE_ENABLED */
1199 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/