2 ******************************************************************************
3 * @file stm32f4xx_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 follow:
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 stream.
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 Stream.
36 (+++) Associate the initialized 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 PLL is configured or
48 (+@) External clock source is configured after setting correctly
49 the define constant EXTERNAL_CLOCK_VALUE in the stm32f4xx_hal_conf.h file.
51 (#) Three operation modes 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) 2017 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 "stm32f4xx_hal.h"
142 /** @addtogroup STM32F4xx_HAL_Driver
146 #ifdef HAL_I2S_MODULE_ENABLED
148 /** @defgroup I2S I2S
149 * @brief I2S HAL module driver
153 /* Private typedef -----------------------------------------------------------*/
154 /* Private define ------------------------------------------------------------*/
155 /* Private macro -------------------------------------------------------------*/
156 /* Private variables ---------------------------------------------------------*/
157 /* Private function prototypes -----------------------------------------------*/
159 /** @addtogroup I2S_Private_Functions
162 static void I2S_DMATxCplt(DMA_HandleTypeDef
*hdma
);
163 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
);
164 static void I2S_DMARxCplt(DMA_HandleTypeDef
*hdma
);
165 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
);
166 static void I2S_DMAError(DMA_HandleTypeDef
*hdma
);
167 static void I2S_Transmit_IT(I2S_HandleTypeDef
*hi2s
);
168 static void I2S_Receive_IT(I2S_HandleTypeDef
*hi2s
);
169 static void I2S_IRQHandler(I2S_HandleTypeDef
*hi2s
);
170 static HAL_StatusTypeDef
I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef
*hi2s
, uint32_t Flag
, uint32_t State
,
176 /* Exported functions --------------------------------------------------------*/
177 /** @addtogroup I2S_Exported_Functions I2S Exported Functions
181 /** @addtogroup I2S_Exported_Functions_Group1
182 * @brief Initialization and Configuration functions
185 ===============================================================================
186 ##### Initialization and de-initialization functions #####
187 ===============================================================================
188 [..] This subsection provides a set of functions allowing to initialize and
189 de-initialize the I2Sx peripheral in simplex mode:
191 (+) User must Implement HAL_I2S_MspInit() function in which he configures
192 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
194 (+) Call the function HAL_I2S_Init() to configure the selected device with
195 the selected configuration:
202 (++) Full duplex mode
204 (+) Call the function HAL_I2S_DeInit() to restore the default configuration
205 of the selected I2Sx peripheral.
211 * @brief Initializes the I2S according to the specified parameters
212 * in the I2S_InitTypeDef and create the associated handle.
213 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
214 * the configuration information for I2S module
217 HAL_StatusTypeDef
HAL_I2S_Init(I2S_HandleTypeDef
*hi2s
)
219 uint32_t tmpreg
= 0U, i2sdiv
= 2U, i2sodd
= 0U, packetlength
= 16U;
220 uint32_t tmp
= 0U, i2sclk
= 0U;
222 /* Check the I2S handle allocation */
228 /* Check the I2S parameters */
229 assert_param(IS_I2S_ALL_INSTANCE(hi2s
->Instance
));
230 assert_param(IS_I2S_MODE(hi2s
->Init
.Mode
));
231 assert_param(IS_I2S_STANDARD(hi2s
->Init
.Standard
));
232 assert_param(IS_I2S_DATA_FORMAT(hi2s
->Init
.DataFormat
));
233 assert_param(IS_I2S_MCLK_OUTPUT(hi2s
->Init
.MCLKOutput
));
234 assert_param(IS_I2S_AUDIO_FREQ(hi2s
->Init
.AudioFreq
));
235 assert_param(IS_I2S_CPOL(hi2s
->Init
.CPOL
));
236 assert_param(IS_I2S_CLOCKSOURCE(hi2s
->Init
.ClockSource
));
238 hi2s
->State
= HAL_I2S_STATE_BUSY
;
240 /* Initialize Default I2S IrqHandler ISR */
241 hi2s
->IrqHandlerISR
= I2S_IRQHandler
;
243 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
244 HAL_I2S_MspInit(hi2s
);
246 /*----------------------- SPIx I2SCFGR & I2SPR Configuration ---------------*/
247 /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
248 CLEAR_BIT(hi2s
->Instance
->I2SCFGR
,(SPI_I2SCFGR_CHLEN
| SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CKPOL
| \
249 SPI_I2SCFGR_I2SSTD
| SPI_I2SCFGR_PCMSYNC
| SPI_I2SCFGR_I2SCFG
| \
250 SPI_I2SCFGR_I2SE
| SPI_I2SCFGR_I2SMOD
));
251 hi2s
->Instance
->I2SPR
= 0x0002U
;
253 /* Get the I2SCFGR register value */
254 tmpreg
= hi2s
->Instance
->I2SCFGR
;
256 /* If the default frequency value has to be written, reinitialize i2sdiv and i2sodd */
257 /* If the requested audio frequency is not the default, compute the prescaler */
258 if(hi2s
->Init
.AudioFreq
!= I2S_AUDIOFREQ_DEFAULT
)
260 /* Check the frame length (For the Prescaler computing) *******************/
261 /* Set I2S Packet Length value*/
262 if(hi2s
->Init
.DataFormat
!= I2S_DATAFORMAT_16B
)
264 /* Packet length is 32 bits */
269 /* Packet length is 16 bits */
274 if(hi2s
->Init
.Standard
<= I2S_STANDARD_LSB
)
276 /* In I2S standard packet lenght is multiplied by 2 */
277 packetlength
= packetlength
* 2U;
280 /* Get I2S source Clock frequency from RCC ********************************/
281 #if defined(I2S_APB1_APB2_FEATURE)
282 if(IS_I2S_APB1_INSTANCE(hi2s
->Instance
))
284 i2sclk
= HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S_APB1
);
288 i2sclk
= HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S_APB2
);
291 i2sclk
= HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2S
);
294 /* Compute the Real divider depending on the MCLK output state, with a floating point */
295 if(hi2s
->Init
.MCLKOutput
== I2S_MCLKOUTPUT_ENABLE
)
297 /* MCLK output is enabled */
298 if (hi2s
->Init
.DataFormat
!= I2S_DATAFORMAT_16B
)
300 tmp
= (uint32_t)(((((i2sclk
/ (packetlength
*4)) * 10) / hi2s
->Init
.AudioFreq
)) + 5);
304 tmp
= (uint32_t)(((((i2sclk
/ (packetlength
*8)) * 10) / hi2s
->Init
.AudioFreq
)) + 5);
309 /* MCLK output is disabled */
310 tmp
= (uint32_t)(((((i2sclk
/ packetlength
) *10 ) / hi2s
->Init
.AudioFreq
)) + 5);
313 /* Remove the flatting point */
316 /* Check the parity of the divider */
317 i2sodd
= (uint16_t)(tmp
& (uint16_t)1U);
319 /* Compute the i2sdiv prescaler */
320 i2sdiv
= (uint16_t)((tmp
- i2sodd
) / 2U);
322 /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
323 i2sodd
= (uint32_t) (i2sodd
<< 8U);
326 /* Test if the divider is 1 or 0 or greater than 0xFF */
327 if((i2sdiv
< 2U) || (i2sdiv
> 0xFFU
))
329 /* Set the default values */
333 /* Set the error code and execute error callback*/
334 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_PRESCALER
);
335 HAL_I2S_ErrorCallback(hi2s
);
339 /* Write to SPIx I2SPR register the computed value */
340 hi2s
->Instance
->I2SPR
= (uint32_t)((uint32_t)i2sdiv
| (uint32_t)(i2sodd
| (uint32_t)hi2s
->Init
.MCLKOutput
));
342 /* Configure the I2S with the I2S_InitStruct values */
343 tmpreg
|= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD
| (uint16_t)(hi2s
->Init
.Mode
| \
344 (uint16_t)(hi2s
->Init
.Standard
| (uint16_t)(hi2s
->Init
.DataFormat
| \
345 (uint16_t)hi2s
->Init
.CPOL
))));
347 #if defined(SPI_I2SCFGR_ASTRTEN)
348 if ((hi2s
->Init
.Standard
== I2S_STANDARD_PCM_SHORT
) ||(hi2s
->Init
.Standard
== I2S_STANDARD_PCM_LONG
))
350 /* Write to SPIx I2SCFGR */
351 WRITE_REG(hi2s
->Instance
->I2SCFGR
,(tmpreg
| SPI_I2SCFGR_ASTRTEN
));
355 /* Write to SPIx I2SCFGR */
356 WRITE_REG(hi2s
->Instance
->I2SCFGR
,tmpreg
);
359 /* Write to SPIx I2SCFGR */
360 WRITE_REG(hi2s
->Instance
->I2SCFGR
, tmpreg
);
363 #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
365 /* Configure the I2S extended if the full duplex mode is enabled */
366 assert_param(IS_I2S_FULLDUPLEX_MODE(hi2s
->Init
.FullDuplexMode
));
368 if(hi2s
->Init
.FullDuplexMode
== I2S_FULLDUPLEXMODE_ENABLE
)
370 /* Set FullDuplex I2S IrqHandler ISR if FULLDUPLEXMODE is enabled */
371 hi2s
->IrqHandlerISR
= HAL_I2SEx_FullDuplex_IRQHandler
;
373 /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
374 CLEAR_BIT(I2SxEXT(hi2s
->Instance
)->I2SCFGR
,(SPI_I2SCFGR_CHLEN
| SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CKPOL
| \
375 SPI_I2SCFGR_I2SSTD
| SPI_I2SCFGR_PCMSYNC
| SPI_I2SCFGR_I2SCFG
| \
376 SPI_I2SCFGR_I2SE
| SPI_I2SCFGR_I2SMOD
));
377 I2SxEXT(hi2s
->Instance
)->I2SPR
= 2U;
379 /* Get the I2SCFGR register value */
380 tmpreg
= I2SxEXT(hi2s
->Instance
)->I2SCFGR
;
382 /* Get the mode to be configured for the extended I2S */
383 if((hi2s
->Init
.Mode
== I2S_MODE_MASTER_TX
) || (hi2s
->Init
.Mode
== I2S_MODE_SLAVE_TX
))
385 tmp
= I2S_MODE_SLAVE_RX
;
387 else /* I2S_MODE_MASTER_RX || I2S_MODE_SLAVE_RX */
389 tmp
= I2S_MODE_SLAVE_TX
;
392 /* Configure the I2S Slave with the I2S Master parameter values */
393 tmpreg
|= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD
| (uint16_t)(tmp
| \
394 (uint16_t)(hi2s
->Init
.Standard
| (uint16_t)(hi2s
->Init
.DataFormat
| \
395 (uint16_t)hi2s
->Init
.CPOL
))));
397 /* Write to SPIx I2SCFGR */
398 WRITE_REG(I2SxEXT(hi2s
->Instance
)->I2SCFGR
,tmpreg
);
400 #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
402 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
403 hi2s
->State
= HAL_I2S_STATE_READY
;
409 * @brief DeInitializes the I2S peripheral
410 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
411 * the configuration information for I2S module
414 HAL_StatusTypeDef
HAL_I2S_DeInit(I2S_HandleTypeDef
*hi2s
)
416 /* Check the I2S handle allocation */
422 hi2s
->State
= HAL_I2S_STATE_BUSY
;
424 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
425 HAL_I2S_MspDeInit(hi2s
);
427 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
428 hi2s
->State
= HAL_I2S_STATE_RESET
;
437 * @brief I2S MSP Init
438 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
439 * the configuration information for I2S module
442 __weak
void HAL_I2S_MspInit(I2S_HandleTypeDef
*hi2s
)
444 /* Prevent unused argument(s) compilation warning */
446 /* NOTE : This function Should not be modified, when the callback is needed,
447 the HAL_I2S_MspInit could be implemented in the user file
452 * @brief I2S MSP DeInit
453 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
454 * the configuration information for I2S module
457 __weak
void HAL_I2S_MspDeInit(I2S_HandleTypeDef
*hi2s
)
459 /* Prevent unused argument(s) compilation warning */
461 /* NOTE : This function Should not be modified, when the callback is needed,
462 the HAL_I2S_MspDeInit could be implemented in the user file
469 /** @addtogroup I2S_Exported_Functions_Group2
470 * @brief Data transfers functions
473 ===============================================================================
474 ##### IO operation functions #####
475 ===============================================================================
477 This subsection provides a set of functions allowing to manage the I2S data
480 (#) There are two modes of transfer:
481 (++) Blocking mode : The communication is performed in the polling mode.
482 The status of all data processing is returned by the same function
483 after finishing transfer.
484 (++) No-Blocking mode : The communication is performed using Interrupts
485 or DMA. These functions return the status of the transfer startup.
486 The end of the data processing will be indicated through the
487 dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
490 (#) Blocking mode functions are :
491 (++) HAL_I2S_Transmit()
492 (++) HAL_I2S_Receive()
494 (#) No-Blocking mode functions with Interrupt are :
495 (++) HAL_I2S_Transmit_IT()
496 (++) HAL_I2S_Receive_IT()
498 (#) No-Blocking mode functions with DMA are :
499 (++) HAL_I2S_Transmit_DMA()
500 (++) HAL_I2S_Receive_DMA()
502 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
503 (++) HAL_I2S_TxCpltCallback()
504 (++) HAL_I2S_RxCpltCallback()
505 (++) HAL_I2S_ErrorCallback()
512 * @brief Transmit an amount of data in blocking mode
513 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
514 * the configuration information for I2S module
515 * @param pData: a 16-bit pointer to data buffer.
516 * @param Size: number of data sample to be sent:
517 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
518 * configuration phase, the Size parameter means the number of 16-bit data length
519 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
520 * the Size parameter means the number of 16-bit data length.
521 * @param Timeout: Timeout duration
522 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
523 * between Master and Slave(example: audio streaming).
526 HAL_StatusTypeDef
HAL_I2S_Transmit(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
, uint32_t Timeout
)
530 if((pData
== NULL
) || (Size
== 0U))
535 if(hi2s
->State
== HAL_I2S_STATE_READY
)
537 tmp1
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
539 if((tmp1
== I2S_DATAFORMAT_24B
) || (tmp1
== I2S_DATAFORMAT_32B
))
541 hi2s
->TxXferSize
= (Size
<< 1U);
542 hi2s
->TxXferCount
= (Size
<< 1U);
546 hi2s
->TxXferSize
= Size
;
547 hi2s
->TxXferCount
= Size
;
553 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
554 hi2s
->State
= HAL_I2S_STATE_BUSY_TX
;
556 /* Check if the I2S is already enabled */
557 if((hi2s
->Instance
->I2SCFGR
&SPI_I2SCFGR_I2SE
) != SPI_I2SCFGR_I2SE
)
559 /* Enable I2S peripheral */
560 __HAL_I2S_ENABLE(hi2s
);
563 while(hi2s
->TxXferCount
> 0U)
565 hi2s
->Instance
->DR
= (*pData
++);
568 /* Wait until TXE flag is set */
569 if (I2S_WaitFlagStateUntilTimeout(hi2s
, I2S_FLAG_TXE
, SET
, Timeout
) != HAL_OK
)
571 /* Set the error code and execute error callback*/
572 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_TIMEOUT
);
573 HAL_I2S_ErrorCallback(hi2s
);
577 /* Check if an underrun occurs */
578 if(__HAL_I2S_GET_FLAG(hi2s
, I2S_FLAG_UDR
) == SET
)
580 /* Clear underrun flag */
581 __HAL_I2S_CLEAR_UDRFLAG(hi2s
);
582 /* Set the I2S State ready */
583 hi2s
->State
= HAL_I2S_STATE_READY
;
585 /* Process Unlocked */
588 /* Set the error code and execute error callback*/
589 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_UDR
);
590 HAL_I2S_ErrorCallback(hi2s
);
595 hi2s
->State
= HAL_I2S_STATE_READY
;
597 /* Process Unlocked */
609 * @brief Receive an amount of data in blocking mode
610 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
611 * the configuration information for I2S module
612 * @param pData: a 16-bit pointer to data buffer
613 * @param Size: number of data sample to be sent:
614 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
615 * configuration phase, the Size parameter means the number of 16-bit data length
616 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
617 * the Size parameter means the number of 16-bit data length.
618 * @param Timeout: Timeout duration
619 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
620 * between Master and Slave(example: audio streaming)
621 * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate
622 * in continuous way and as the I2S is not disabled at the end of the I2S transaction
625 HAL_StatusTypeDef
HAL_I2S_Receive(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
, uint32_t Timeout
)
629 if((pData
== NULL
) || (Size
== 0U))
634 if(hi2s
->State
== HAL_I2S_STATE_READY
)
636 tmp1
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
637 if((tmp1
== I2S_DATAFORMAT_24B
) || (tmp1
== I2S_DATAFORMAT_32B
))
639 hi2s
->RxXferSize
= (Size
<< 1U);
640 hi2s
->RxXferCount
= (Size
<< 1U);
644 hi2s
->RxXferSize
= Size
;
645 hi2s
->RxXferCount
= Size
;
650 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
651 hi2s
->State
= HAL_I2S_STATE_BUSY_RX
;
653 /* Check if the I2S is already enabled */
654 if((hi2s
->Instance
->I2SCFGR
& SPI_I2SCFGR_I2SE
) != SPI_I2SCFGR_I2SE
)
656 /* Enable I2S peripheral */
657 __HAL_I2S_ENABLE(hi2s
);
660 /* Check if Master Receiver mode is selected */
661 if((hi2s
->Instance
->I2SCFGR
& SPI_I2SCFGR_I2SCFG
) == I2S_MODE_MASTER_RX
)
663 /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
664 access to the SPI_SR register. */
665 __HAL_I2S_CLEAR_OVRFLAG(hi2s
);
669 while(hi2s
->RxXferCount
> 0U)
671 /* Wait until RXNE flag is set */
672 if (I2S_WaitFlagStateUntilTimeout(hi2s
, I2S_FLAG_RXNE
, SET
, Timeout
) != HAL_OK
)
674 /* Set the error code and execute error callback*/
675 SET_BIT(hi2s
->ErrorCode
,HAL_I2S_ERROR_TIMEOUT
);
676 HAL_I2S_ErrorCallback(hi2s
);
680 /* Check if an overrun occurs */
681 if(__HAL_I2S_GET_FLAG(hi2s
, I2S_FLAG_OVR
) == SET
)
683 /* Clear overrun flag */
684 __HAL_I2S_CLEAR_OVRFLAG(hi2s
);
686 /* Set the I2S State ready */
687 hi2s
->State
= HAL_I2S_STATE_READY
;
689 /* Process Unlocked */
692 /* Set the error code and execute error callback*/
693 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_OVR
);
694 HAL_I2S_ErrorCallback(hi2s
);
699 (*pData
++) = hi2s
->Instance
->DR
;
703 hi2s
->State
= HAL_I2S_STATE_READY
;
705 /* Process Unlocked */
717 * @brief Transmit an amount of data in non-blocking mode with Interrupt
718 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
719 * the configuration information for I2S module
720 * @param pData: a 16-bit pointer to data buffer.
721 * @param Size: number of data sample to be sent:
722 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
723 * configuration phase, the Size parameter means the number of 16-bit data length
724 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
725 * the Size parameter means the number of 16-bit data length.
726 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
727 * between Master and Slave(example: audio streaming).
730 HAL_StatusTypeDef
HAL_I2S_Transmit_IT(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
734 if(hi2s
->State
== HAL_I2S_STATE_READY
)
736 if((pData
== NULL
) || (Size
== 0U))
741 hi2s
->pTxBuffPtr
= pData
;
742 tmp1
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
743 if((tmp1
== I2S_DATAFORMAT_24B
) || (tmp1
== I2S_DATAFORMAT_32B
))
745 hi2s
->TxXferSize
= (Size
<< 1U);
746 hi2s
->TxXferCount
= (Size
<< 1U);
750 hi2s
->TxXferSize
= Size
;
751 hi2s
->TxXferCount
= Size
;
757 hi2s
->State
= HAL_I2S_STATE_BUSY_TX
;
758 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
760 /* Enable TXE and ERR interrupt */
761 __HAL_I2S_ENABLE_IT(hi2s
, (I2S_IT_TXE
| I2S_IT_ERR
));
763 /* Check if the I2S is already enabled */
764 if((hi2s
->Instance
->I2SCFGR
&SPI_I2SCFGR_I2SE
) != SPI_I2SCFGR_I2SE
)
766 /* Enable I2S peripheral */
767 __HAL_I2S_ENABLE(hi2s
);
770 /* Process Unlocked */
782 * @brief Receive an amount of data in non-blocking mode with Interrupt
783 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
784 * the configuration information for I2S module
785 * @param pData: a 16-bit pointer to the Receive data buffer.
786 * @param Size: number of data sample to be sent:
787 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
788 * configuration phase, the Size parameter means the number of 16-bit data length
789 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
790 * the Size parameter means the number of 16-bit data length.
791 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
792 * between Master and Slave(example: audio streaming).
793 * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronisation
794 * between Master and Slave otherwise the I2S interrupt should be optimized.
797 HAL_StatusTypeDef
HAL_I2S_Receive_IT(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
801 if(hi2s
->State
== HAL_I2S_STATE_READY
)
803 if((pData
== NULL
) || (Size
== 0U))
808 hi2s
->pRxBuffPtr
= pData
;
809 tmp1
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
810 if((tmp1
== I2S_DATAFORMAT_24B
) || (tmp1
== I2S_DATAFORMAT_32B
))
812 hi2s
->RxXferSize
= (Size
<< 1U);
813 hi2s
->RxXferCount
= (Size
<< 1U);
817 hi2s
->RxXferSize
= Size
;
818 hi2s
->RxXferCount
= Size
;
823 hi2s
->State
= HAL_I2S_STATE_BUSY_RX
;
824 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
826 /* Enable TXE and ERR interrupt */
827 __HAL_I2S_ENABLE_IT(hi2s
, (I2S_IT_RXNE
| I2S_IT_ERR
));
829 /* Check if the I2S is already enabled */
830 if((hi2s
->Instance
->I2SCFGR
&SPI_I2SCFGR_I2SE
) != SPI_I2SCFGR_I2SE
)
832 /* Enable I2S peripheral */
833 __HAL_I2S_ENABLE(hi2s
);
836 /* Process Unlocked */
849 * @brief Transmit an amount of data in non-blocking mode with DMA
850 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
851 * the configuration information for I2S module
852 * @param pData: a 16-bit pointer to the Transmit data buffer.
853 * @param Size: number of data sample to be sent:
854 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
855 * configuration phase, the Size parameter means the number of 16-bit data length
856 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
857 * the Size parameter means the number of 16-bit data length.
858 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
859 * between Master and Slave(example: audio streaming).
862 HAL_StatusTypeDef
HAL_I2S_Transmit_DMA(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
864 uint32_t *tmp
= NULL
;
867 if((pData
== NULL
) || (Size
== 0U))
872 if(hi2s
->State
== HAL_I2S_STATE_READY
)
874 hi2s
->pTxBuffPtr
= pData
;
875 tmp1
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
876 if((tmp1
== I2S_DATAFORMAT_24B
) || (tmp1
== I2S_DATAFORMAT_32B
))
878 hi2s
->TxXferSize
= (Size
<< 1U);
879 hi2s
->TxXferCount
= (Size
<< 1U);
883 hi2s
->TxXferSize
= Size
;
884 hi2s
->TxXferCount
= Size
;
890 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
891 hi2s
->State
= HAL_I2S_STATE_BUSY_TX
;
893 /* Set the I2S Tx DMA Half transfer complete callback */
894 hi2s
->hdmatx
->XferHalfCpltCallback
= I2S_DMATxHalfCplt
;
896 /* Set the I2S Tx DMA transfer complete callback */
897 hi2s
->hdmatx
->XferCpltCallback
= I2S_DMATxCplt
;
899 /* Set the DMA error callback */
900 hi2s
->hdmatx
->XferErrorCallback
= I2S_DMAError
;
902 /* Enable the Tx DMA Stream */
903 tmp
= (uint32_t*)&pData
;
904 HAL_DMA_Start_IT(hi2s
->hdmatx
, *(uint32_t*)tmp
, (uint32_t)&hi2s
->Instance
->DR
, hi2s
->TxXferSize
);
906 /* Check if the I2S is already enabled */
907 if((hi2s
->Instance
->I2SCFGR
&SPI_I2SCFGR_I2SE
) != SPI_I2SCFGR_I2SE
)
909 /* Enable I2S peripheral */
910 __HAL_I2S_ENABLE(hi2s
);
913 /* Check if the I2S Tx request is already enabled */
914 if((hi2s
->Instance
->CR2
& SPI_CR2_TXDMAEN
) != SPI_CR2_TXDMAEN
)
916 /* Enable Tx DMA Request */
917 SET_BIT(hi2s
->Instance
->CR2
, SPI_CR2_TXDMAEN
);
920 /* Process Unlocked */
932 * @brief Receive an amount of data in non-blocking mode with DMA
933 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
934 * the configuration information for I2S module
935 * @param pData: a 16-bit pointer to the Receive data buffer.
936 * @param Size: number of data sample to be sent:
937 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
938 * configuration phase, the Size parameter means the number of 16-bit data length
939 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
940 * the Size parameter means the number of 16-bit data length.
941 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
942 * between Master and Slave(example: audio streaming).
945 HAL_StatusTypeDef
HAL_I2S_Receive_DMA(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
947 uint32_t *tmp
= NULL
;
950 if((pData
== NULL
) || (Size
== 0U))
955 if(hi2s
->State
== HAL_I2S_STATE_READY
)
957 hi2s
->pRxBuffPtr
= pData
;
958 tmp1
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
959 if((tmp1
== I2S_DATAFORMAT_24B
) || (tmp1
== I2S_DATAFORMAT_32B
))
961 hi2s
->RxXferSize
= (Size
<< 1U);
962 hi2s
->RxXferCount
= (Size
<< 1U);
966 hi2s
->RxXferSize
= Size
;
967 hi2s
->RxXferCount
= Size
;
972 hi2s
->State
= HAL_I2S_STATE_BUSY_RX
;
973 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
975 /* Set the I2S Rx DMA Half transfer complete callback */
976 hi2s
->hdmarx
->XferHalfCpltCallback
= I2S_DMARxHalfCplt
;
978 /* Set the I2S Rx DMA transfer complete callback */
979 hi2s
->hdmarx
->XferCpltCallback
= I2S_DMARxCplt
;
981 /* Set the DMA error callback */
982 hi2s
->hdmarx
->XferErrorCallback
= I2S_DMAError
;
984 /* Check if Master Receiver mode is selected */
985 if((hi2s
->Instance
->I2SCFGR
& SPI_I2SCFGR_I2SCFG
) == I2S_MODE_MASTER_RX
)
987 /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read
988 access to the SPI_SR register. */
989 __HAL_I2S_CLEAR_OVRFLAG(hi2s
);
992 /* Enable the Rx DMA Stream */
993 tmp
= (uint32_t*)&pData
;
994 HAL_DMA_Start_IT(hi2s
->hdmarx
, (uint32_t)&hi2s
->Instance
->DR
, *(uint32_t*)tmp
, hi2s
->RxXferSize
);
996 /* Check if the I2S is already enabled */
997 if((hi2s
->Instance
->I2SCFGR
&SPI_I2SCFGR_I2SE
) != SPI_I2SCFGR_I2SE
)
999 /* Enable I2S peripheral */
1000 __HAL_I2S_ENABLE(hi2s
);
1003 /* Check if the I2S Rx request is already enabled */
1004 if((hi2s
->Instance
->CR2
&SPI_CR2_RXDMAEN
) != SPI_CR2_RXDMAEN
)
1006 /* Enable Rx DMA Request */
1007 SET_BIT(hi2s
->Instance
->CR2
,SPI_CR2_RXDMAEN
);
1010 /* Process Unlocked */
1022 * @brief Pauses the audio stream playing from the Media.
1023 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1024 * the configuration information for I2S module
1025 * @retval HAL status
1027 HAL_StatusTypeDef
HAL_I2S_DMAPause(I2S_HandleTypeDef
*hi2s
)
1029 /* Process Locked */
1032 if(hi2s
->State
== HAL_I2S_STATE_BUSY_TX
)
1034 /* Disable the I2S DMA Tx request */
1035 CLEAR_BIT(hi2s
->Instance
->CR2
,SPI_CR2_TXDMAEN
);
1037 else if(hi2s
->State
== HAL_I2S_STATE_BUSY_RX
)
1039 /* Disable the I2S DMA Rx request */
1040 CLEAR_BIT(hi2s
->Instance
->CR2
,SPI_CR2_RXDMAEN
);
1042 #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
1043 else if(hi2s
->State
== HAL_I2S_STATE_BUSY_TX_RX
)
1045 /* Pause the audio file playing by disabling the I2S DMA request */
1046 CLEAR_BIT(hi2s
->Instance
->CR2
,(SPI_CR2_TXDMAEN
|SPI_CR2_RXDMAEN
));
1047 CLEAR_BIT(I2SxEXT(hi2s
->Instance
)->CR2
,(SPI_CR2_TXDMAEN
|SPI_CR2_RXDMAEN
));
1049 #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
1051 /* Process Unlocked */
1058 * @brief Resumes the audio stream playing from the Media.
1059 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1060 * the configuration information for I2S module
1061 * @retval HAL status
1063 HAL_StatusTypeDef
HAL_I2S_DMAResume(I2S_HandleTypeDef
*hi2s
)
1065 /* Process Locked */
1068 if(hi2s
->State
== HAL_I2S_STATE_BUSY_TX
)
1070 /* Enable the I2S DMA Tx request */
1071 SET_BIT(hi2s
->Instance
->CR2
,SPI_CR2_TXDMAEN
);
1073 else if(hi2s
->State
== HAL_I2S_STATE_BUSY_RX
)
1075 /* Enable the I2S DMA Rx request */
1076 SET_BIT(hi2s
->Instance
->CR2
,SPI_CR2_RXDMAEN
);
1078 #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
1079 else if(hi2s
->State
== HAL_I2S_STATE_BUSY_TX_RX
)
1081 /* Pause the audio file playing by disabling the I2S DMA request */
1082 SET_BIT(hi2s
->Instance
->CR2
,(SPI_CR2_RXDMAEN
| SPI_CR2_TXDMAEN
));
1083 SET_BIT(I2SxEXT(hi2s
->Instance
)->CR2
,(SPI_CR2_RXDMAEN
| SPI_CR2_TXDMAEN
));
1085 /* If the I2Sext peripheral is still not enabled, enable it */
1086 if ((I2SxEXT(hi2s
->Instance
)->I2SCFGR
& SPI_I2SCFGR_I2SE
) == 0U)
1088 /* Enable I2Sext peripheral */
1089 __HAL_I2SEXT_ENABLE(hi2s
);
1092 #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
1094 /* If the I2S peripheral is still not enabled, enable it */
1095 if ((hi2s
->Instance
->I2SCFGR
& SPI_I2SCFGR_I2SE
) == 0U)
1097 /* Enable I2S peripheral */
1098 __HAL_I2S_ENABLE(hi2s
);
1101 /* Process Unlocked */
1108 * @brief Resumes the audio stream playing from the Media.
1109 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1110 * the configuration information for I2S module
1111 * @retval HAL status
1113 HAL_StatusTypeDef
HAL_I2S_DMAStop(I2S_HandleTypeDef
*hi2s
)
1115 /* Process Locked */
1118 if(hi2s
->State
== HAL_I2S_STATE_BUSY_TX
)
1120 /* Disable the I2S DMA requests */
1121 CLEAR_BIT(hi2s
->Instance
->CR2
,SPI_CR2_TXDMAEN
);
1123 /* Disable the I2S DMA Channel */
1124 HAL_DMA_Abort(hi2s
->hdmatx
);
1126 else if(hi2s
->State
== HAL_I2S_STATE_BUSY_RX
)
1128 /* Disable the I2S DMA requests */
1129 CLEAR_BIT(hi2s
->Instance
->CR2
,SPI_CR2_RXDMAEN
);
1131 /* Disable the I2S DMA Channel */
1132 HAL_DMA_Abort(hi2s
->hdmarx
);
1134 #if defined (SPI_I2S_FULLDUPLEX_SUPPORT)
1135 else if(hi2s
->State
== HAL_I2S_STATE_BUSY_TX_RX
)
1137 /* Disable the I2S DMA requests */
1138 CLEAR_BIT(hi2s
->Instance
->CR2
,(SPI_CR2_RXDMAEN
| SPI_CR2_TXDMAEN
));
1139 CLEAR_BIT(I2SxEXT(hi2s
->Instance
)->CR2
,(SPI_CR2_RXDMAEN
| SPI_CR2_TXDMAEN
));
1141 /* Disable the I2S DMA Channels */
1142 HAL_DMA_Abort(hi2s
->hdmatx
);
1143 HAL_DMA_Abort(hi2s
->hdmarx
);
1145 /* Disable I2Sext peripheral */
1146 __HAL_I2SEXT_DISABLE(hi2s
);
1148 #endif /* SPI_I2S_FULLDUPLEX_SUPPORT */
1150 /* Disable I2S peripheral */
1151 __HAL_I2S_DISABLE(hi2s
);
1153 hi2s
->State
= HAL_I2S_STATE_READY
;
1155 /* Process Unlocked */
1162 * @brief This function handles I2S interrupt request.
1163 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1164 * the configuration information for I2S module
1167 void HAL_I2S_IRQHandler(I2S_HandleTypeDef
*hi2s
)
1169 /* Call the IrqHandler ISR set during HAL_I2S_INIT */
1170 hi2s
->IrqHandlerISR(hi2s
);
1174 * @brief Tx Transfer Half completed callbacks
1175 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1176 * the configuration information for I2S module
1179 __weak
void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef
*hi2s
)
1181 /* Prevent unused argument(s) compilation warning */
1183 /* NOTE : This function Should not be modified, when the callback is needed,
1184 the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
1189 * @brief Tx Transfer completed callbacks
1190 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1191 * the configuration information for I2S module
1194 __weak
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef
*hi2s
)
1196 /* Prevent unused argument(s) compilation warning */
1198 /* NOTE : This function Should not be modified, when the callback is needed,
1199 the HAL_I2S_TxCpltCallback could be implemented in the user file
1204 * @brief Rx Transfer half completed callbacks
1205 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1206 * the configuration information for I2S module
1209 __weak
void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef
*hi2s
)
1211 /* Prevent unused argument(s) compilation warning */
1213 /* NOTE : This function Should not be modified, when the callback is needed,
1214 the HAL_I2S_RxCpltCallback could be implemented in the user file
1219 * @brief Rx Transfer completed callbacks
1220 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1221 * the configuration information for I2S module
1224 __weak
void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef
*hi2s
)
1226 /* Prevent unused argument(s) compilation warning */
1228 /* NOTE : This function Should not be modified, when the callback is needed,
1229 the HAL_I2S_RxCpltCallback could be implemented in the user file
1234 * @brief I2S error callbacks
1235 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1236 * the configuration information for I2S module
1239 __weak
void HAL_I2S_ErrorCallback(I2S_HandleTypeDef
*hi2s
)
1241 /* Prevent unused argument(s) compilation warning */
1243 /* NOTE : This function Should not be modified, when the callback is needed,
1244 the HAL_I2S_ErrorCallback could be implemented in the user file
1252 /** @addtogroup I2S_Exported_Functions_Group3
1253 * @brief Peripheral State functions
1256 ===============================================================================
1257 ##### Peripheral State and Errors functions #####
1258 ===============================================================================
1260 This subsection permits to get in run-time the status of the peripheral
1268 * @brief Return the I2S state
1269 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1270 * the configuration information for I2S module
1273 HAL_I2S_StateTypeDef
HAL_I2S_GetState(I2S_HandleTypeDef
*hi2s
)
1279 * @brief Return the I2S error code
1280 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1281 * the configuration information for I2S module
1282 * @retval I2S Error Code
1284 uint32_t HAL_I2S_GetError(I2S_HandleTypeDef
*hi2s
)
1286 return hi2s
->ErrorCode
;
1296 /** @addtogroup I2S_Private_Functions I2S Private Functions
1300 * @brief DMA I2S transmit process complete callback
1301 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1302 * the configuration information for the specified DMA module.
1305 static void I2S_DMATxCplt(DMA_HandleTypeDef
*hdma
)
1307 I2S_HandleTypeDef
* hi2s
= ( I2S_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1309 if((hdma
->Instance
->CR
& DMA_SxCR_CIRC
) == 0U)
1311 /* Disable Tx DMA Request */
1312 CLEAR_BIT(hi2s
->Instance
->CR2
,SPI_CR2_TXDMAEN
);
1314 hi2s
->TxXferCount
= 0U;
1315 hi2s
->State
= HAL_I2S_STATE_READY
;
1317 HAL_I2S_TxCpltCallback(hi2s
);
1320 * @brief DMA I2S transmit process half complete callback
1321 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1322 * the configuration information for the specified DMA module.
1325 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
)
1327 I2S_HandleTypeDef
* hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
1329 HAL_I2S_TxHalfCpltCallback(hi2s
);
1333 * @brief DMA I2S receive process complete callback
1334 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1335 * the configuration information for the specified DMA module.
1338 static void I2S_DMARxCplt(DMA_HandleTypeDef
*hdma
)
1340 I2S_HandleTypeDef
* hi2s
= ( I2S_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1342 if((hdma
->Instance
->CR
& DMA_SxCR_CIRC
) == 0U)
1344 /* Disable Rx DMA Request */
1345 CLEAR_BIT(hi2s
->Instance
->CR2
,SPI_CR2_RXDMAEN
);
1346 hi2s
->RxXferCount
= 0U;
1347 hi2s
->State
= HAL_I2S_STATE_READY
;
1349 HAL_I2S_RxCpltCallback(hi2s
);
1353 * @brief DMA I2S receive process half complete callback
1354 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1355 * the configuration information for the specified DMA module.
1358 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
)
1360 I2S_HandleTypeDef
* hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
1362 HAL_I2S_RxHalfCpltCallback(hi2s
);
1366 * @brief DMA I2S communication error callback
1367 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1368 * the configuration information for the specified DMA module.
1371 static void I2S_DMAError(DMA_HandleTypeDef
*hdma
)
1373 I2S_HandleTypeDef
* hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
1375 /* Disable Rx and Tx DMA Request */
1376 CLEAR_BIT(hi2s
->Instance
->CR2
,(SPI_CR2_RXDMAEN
| SPI_CR2_TXDMAEN
));
1377 hi2s
->TxXferCount
= 0U;
1378 hi2s
->RxXferCount
= 0U;
1380 hi2s
->State
= HAL_I2S_STATE_READY
;
1382 SET_BIT(hi2s
->ErrorCode
,HAL_I2S_ERROR_DMA
);
1383 HAL_I2S_ErrorCallback(hi2s
);
1387 * @brief Transmit an amount of data in non-blocking mode with Interrupt
1388 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1389 * the configuration information for I2S module
1390 * @retval HAL status
1392 static void I2S_Transmit_IT(I2S_HandleTypeDef
*hi2s
)
1395 hi2s
->Instance
->DR
= (*hi2s
->pTxBuffPtr
++);
1396 hi2s
->TxXferCount
--;
1398 if(hi2s
->TxXferCount
== 0U)
1400 /* Disable TXE and ERR interrupt */
1401 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_TXE
| I2S_IT_ERR
));
1403 hi2s
->State
= HAL_I2S_STATE_READY
;
1404 HAL_I2S_TxCpltCallback(hi2s
);
1409 * @brief Receive an amount of data in non-blocking mode with Interrupt
1410 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1411 * the configuration information for I2S module
1412 * @retval HAL status
1414 static void I2S_Receive_IT(I2S_HandleTypeDef
*hi2s
)
1417 (*hi2s
->pRxBuffPtr
++) = hi2s
->Instance
->DR
;
1418 hi2s
->RxXferCount
--;
1420 if(hi2s
->RxXferCount
== 0U)
1422 /* Disable RXNE and ERR interrupt */
1423 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_RXNE
| I2S_IT_ERR
));
1425 hi2s
->State
= HAL_I2S_STATE_READY
;
1426 HAL_I2S_RxCpltCallback(hi2s
);
1431 * @brief This function handles I2S interrupt request.
1432 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1433 * the configuration information for I2S module
1436 static void I2S_IRQHandler(I2S_HandleTypeDef
*hi2s
)
1438 __IO
uint32_t i2ssr
= hi2s
->Instance
->SR
;
1440 if(hi2s
->State
== HAL_I2S_STATE_BUSY_RX
)
1442 /* I2S in mode Receiver ------------------------------------------------*/
1443 if(((i2ssr
& I2S_FLAG_RXNE
) == I2S_FLAG_RXNE
) && (__HAL_I2S_GET_IT_SOURCE(hi2s
, I2S_IT_RXNE
) != RESET
))
1445 I2S_Receive_IT(hi2s
);
1448 /* I2S Overrun error interrupt occured -------------------------------------*/
1449 if(((i2ssr
& I2S_FLAG_OVR
) == I2S_FLAG_OVR
) && (__HAL_I2S_GET_IT_SOURCE(hi2s
, I2S_IT_ERR
) != RESET
))
1451 /* Disable RXNE and ERR interrupt */
1452 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_RXNE
| I2S_IT_ERR
));
1454 /* Clear Overrun flag */
1455 __HAL_I2S_CLEAR_OVRFLAG(hi2s
);
1457 /* Set the I2S State ready */
1458 hi2s
->State
= HAL_I2S_STATE_READY
;
1461 /* Set the error code and execute error callback*/
1462 SET_BIT(hi2s
->ErrorCode
,HAL_I2S_ERROR_OVR
);
1463 HAL_I2S_ErrorCallback(hi2s
);
1467 if(hi2s
->State
== HAL_I2S_STATE_BUSY_TX
)
1469 /* I2S in mode Transmitter -----------------------------------------------*/
1470 if(((i2ssr
& I2S_FLAG_TXE
) == I2S_FLAG_TXE
) && (__HAL_I2S_GET_IT_SOURCE(hi2s
, I2S_IT_TXE
) != RESET
))
1472 I2S_Transmit_IT(hi2s
);
1475 /* I2S Underrun error interrupt occurred --------------------------------*/
1476 if(((i2ssr
& I2S_FLAG_UDR
) == I2S_FLAG_UDR
) && (__HAL_I2S_GET_IT_SOURCE(hi2s
, I2S_IT_ERR
) != RESET
))
1478 /* Disable TXE and ERR interrupt */
1479 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_TXE
| I2S_IT_ERR
));
1481 /* Clear Underrun flag */
1482 __HAL_I2S_CLEAR_UDRFLAG(hi2s
);
1484 /* Set the I2S State ready */
1485 hi2s
->State
= HAL_I2S_STATE_READY
;
1487 /* Set the error code and execute error callback*/
1488 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_UDR
);
1489 HAL_I2S_ErrorCallback(hi2s
);
1495 * @brief This function handles I2S Communication Timeout.
1496 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1497 * the configuration information for I2S module
1498 * @param Flag: Flag checked
1499 * @param State: Value of the flag expected
1500 * @param Timeout: Duration of the timeout
1501 * @retval HAL status
1503 static HAL_StatusTypeDef
I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef
*hi2s
, uint32_t Flag
, uint32_t State
,
1506 uint32_t tickstart
= HAL_GetTick();
1508 /* Wait until flag is set to status*/
1509 while(((__HAL_I2S_GET_FLAG(hi2s
, Flag
)) ? SET
: RESET
) != State
)
1511 if(Timeout
!= HAL_MAX_DELAY
)
1513 if((Timeout
== 0U)||((HAL_GetTick() - tickstart
) > Timeout
))
1515 /* Set the I2S State ready */
1516 hi2s
->State
= HAL_I2S_STATE_READY
;
1518 /* Process Unlocked */
1536 #endif /* HAL_I2S_MODULE_ENABLED */
1541 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/