2 ******************************************************************************
3 * @file stm32h7xx_hal_i2s.c
4 * @author MCD Application Team
5 * @brief I2S HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Integrated Interchip Sound (I2S) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral State and Errors functions
12 ===============================================================================
13 ##### How to use this driver #####
14 ===============================================================================
16 The I2S HAL driver can be used as follow:
18 (#) Declare a I2S_HandleTypeDef handle structure.
19 (#) Initialize the I2S low level resources by implement the HAL_I2S_MspInit() API:
20 (##) Enable the SPIx interface clock.
21 (##) I2S pins configuration:
22 (+++) Enable the clock for the I2S GPIOs.
23 (+++) Configure these I2S pins as alternate function pull-up.
24 (##) NVIC configuration if you need to use interrupt process (HAL_I2S_Transmit_IT()
25 and HAL_I2S_Receive_IT() APIs).
26 (+++) Configure the I2Sx interrupt priority.
27 (+++) Enable the NVIC I2S IRQ handle.
28 (##) DMA Configuration if you need to use DMA process (HAL_I2S_Transmit_DMA()
29 and HAL_I2S_Receive_DMA() APIs:
30 (+++) Declare a DMA handle structure for the Tx/Rx Stream/Channel.
31 (+++) Enable the DMAx interface clock.
32 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
33 (+++) Configure the DMA Tx/Rx Stream/Channel.
34 (+++) Associate the initialized DMA handle to the I2S DMA Tx/Rx handle.
35 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the
36 DMA Tx/Rx Stream/Channel.
38 (#) Program the Mode, Standard, Data Format, MCLK Output, Audio frequency and Polarity
39 using HAL_I2S_Init() function.
41 -@- The specific I2S interrupts (Transmission complete interrupt,
42 RXNE interrupt and Error Interrupts) will be managed using the macros
43 __HAL_I2S_ENABLE_IT() and __HAL_I2S_DISABLE_IT() inside the transmit and receive process.
45 (+@) External clock source is configured after setting correctly
46 the define constant EXTERNAL_CLOCK_VALUE in the stm32h7xx_hal_conf.h file.
48 (#) Three mode of operations are available within this driver :
50 *** Polling mode IO operation ***
51 =================================
53 (+) Send an amount of data in blocking mode using HAL_I2S_Transmit()
54 (+) Receive an amount of data in blocking mode using HAL_I2S_Receive()
56 *** Interrupt mode IO operation ***
57 ===================================
59 (+) Send an amount of data in non blocking mode using HAL_I2S_Transmit_IT()
60 (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can
61 add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback
62 (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can
63 add his own code by customization of function pointer HAL_I2S_TxCpltCallback
64 (+) Receive an amount of data in non blocking mode using HAL_I2S_Receive_IT()
65 (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can
66 add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback
67 (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can
68 add his own code by customization of function pointer HAL_I2S_RxCpltCallback
69 (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
70 add his own code by customization of function pointer HAL_I2S_ErrorCallback
72 *** DMA mode IO operation ***
73 ==============================
75 (+) Send an amount of data in non blocking mode (DMA) using HAL_I2S_Transmit_DMA()
76 (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can
77 add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback
78 (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can
79 add his own code by customization of function pointer HAL_I2S_TxCpltCallback
80 (+) Receive an amount of data in non blocking mode (DMA) using HAL_I2S_Receive_DMA()
81 (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can
82 add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback
83 (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can
84 add his own code by customization of function pointer HAL_I2S_RxCpltCallback
85 (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
86 add his own code by customization of function pointer HAL_I2S_ErrorCallback
87 (+) Pause the DMA Transfer using HAL_I2S_DMAPause()
88 (+) Resume the DMA Transfer using HAL_I2S_DMAResume()
89 (+) Stop the DMA Transfer using HAL_I2S_DMAStop()
91 *** I2S HAL driver macros list ***
92 ===================================
94 Below the list of most used macros in I2S HAL driver.
96 (+) __HAL_I2S_ENABLE: Enable the specified SPI peripheral (in I2S mode)
97 (+) __HAL_I2S_DISABLE: Disable the specified SPI peripheral (in I2S mode)
98 (+) __HAL_I2S_ENABLE_IT : Enable the specified I2S interrupts
99 (+) __HAL_I2S_DISABLE_IT : Disable the specified I2S interrupts
100 (+) __HAL_I2S_GET_FLAG: Check whether the specified I2S flag is set or not
103 (@) You can refer to the I2S HAL driver header file for more useful macros
105 *** I2S HAL driver macros list ***
106 ===================================
108 Callback registration:
110 (#) The compilation flag USE_HAL_I2S_REGISTER_CALLBACKS when set to 1UL
111 allows the user to configure dynamically the driver callbacks.
112 Use Functions HAL_I2S_RegisterCallback() to register an interrupt callback.
114 Function HAL_I2S_RegisterCallback() allows to register following callbacks:
115 (+) TxCpltCallback : I2S Tx Completed callback
116 (+) RxCpltCallback : I2S Rx Completed callback
117 (+) TxRxCpltCallback : I2S TxRx Completed callback
118 (+) TxHalfCpltCallback : I2S Tx Half Completed callback
119 (+) RxHalfCpltCallback : I2S Rx Half Completed callback
120 (+) ErrorCallback : I2S Error callback
121 (+) MspInitCallback : I2S Msp Init callback
122 (+) MspDeInitCallback : I2S Msp DeInit callback
123 This function takes as parameters the HAL peripheral handle, the Callback ID
124 and a pointer to the user callback function.
127 (#) Use function HAL_I2S_UnRegisterCallback to reset a callback to the default
129 HAL_I2S_UnRegisterCallback takes as parameters the HAL peripheral handle,
131 This function allows to reset following callbacks:
132 (+) TxCpltCallback : I2S Tx Completed callback
133 (+) RxCpltCallback : I2S Rx Completed callback
134 (+) TxRxCpltCallback : I2S TxRx Completed callback
135 (+) TxHalfCpltCallback : I2S Tx Half Completed callback
136 (+) RxHalfCpltCallback : I2S Rx Half Completed callback
137 (+) ErrorCallback : I2S Error callback
138 (+) MspInitCallback : I2S Msp Init callback
139 (+) MspDeInitCallback : I2S Msp DeInit callback
141 By default, after the HAL_I2S_Init() and when the state is HAL_I2S_STATE_RESET
142 all callbacks are set to the corresponding weak functions:
143 examples HAL_I2S_MasterTxCpltCallback(), HAL_I2S_MasterRxCpltCallback().
144 Exception done for MspInit and MspDeInit functions that are
145 reset to the legacy weak functions in the HAL_I2S_Init()/ HAL_I2S_DeInit() only when
146 these callbacks are null (not registered beforehand).
147 If MspInit or MspDeInit are not null, the HAL_I2S_Init()/ HAL_I2S_DeInit()
148 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
150 Callbacks can be registered/unregistered in HAL_I2S_STATE_READY state only.
151 Exception done MspInit/MspDeInit functions that can be registered/unregistered
152 in HAL_I2S_STATE_READY or HAL_I2S_STATE_RESET state,
153 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
154 Then, the user first registers the MspInit/MspDeInit user callbacks
155 using HAL_I2S_RegisterCallback() before calling HAL_I2S_DeInit()
156 or HAL_I2S_Init() function.
158 When The compilation define USE_HAL_I2S_REGISTER_CALLBACKS is set to 0 or
159 not defined, the callback registering feature is not available
160 and weak (surcharged) callbacks are used.
164 ******************************************************************************
167 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
168 * All rights reserved.</center></h2>
170 * This software component is licensed by ST under BSD 3-Clause license,
171 * the "License"; You may not use this file except in compliance with the
172 * License. You may obtain a copy of the License at:
173 * opensource.org/licenses/BSD-3-Clause
175 ******************************************************************************
178 /* Includes ------------------------------------------------------------------*/
179 #include "stm32h7xx_hal.h"
181 #ifdef HAL_I2S_MODULE_ENABLED
183 /** @addtogroup STM32H7xx_HAL_Driver
187 /** @defgroup I2S I2S
188 * @brief I2S HAL module driver
192 /* Private typedef -----------------------------------------------------------*/
193 /* Private define ------------------------------------------------------------*/
194 #define I2S_TIMEOUT 0xFFFFUL
196 /* Private macro -------------------------------------------------------------*/
197 /* Private variables ---------------------------------------------------------*/
198 /* Private function prototypes -----------------------------------------------*/
199 /** @defgroup I2S_Private_Functions I2S Private Functions
202 static void I2S_DMATxCplt(DMA_HandleTypeDef
*hdma
);
203 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
);
204 static void I2S_DMARxCplt(DMA_HandleTypeDef
*hdma
);
205 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
);
206 static void I2SEx_DMATxRxCplt(DMA_HandleTypeDef
*hdma
);
207 static void I2SEx_DMATxRxHalfCplt(DMA_HandleTypeDef
*hdma
);
208 static void I2S_DMAError(DMA_HandleTypeDef
*hdma
);
209 static void I2S_Transmit_16Bit_IT(I2S_HandleTypeDef
*hi2s
);
210 static void I2S_Transmit_32Bit_IT(I2S_HandleTypeDef
*hi2s
);
211 static void I2S_Receive_16Bit_IT(I2S_HandleTypeDef
*hi2s
);
212 static void I2S_Receive_32Bit_IT(I2S_HandleTypeDef
*hi2s
);
213 static HAL_StatusTypeDef
I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef
*hi2s
, uint32_t Flag
, FlagStatus State
,
219 /* Exported functions ---------------------------------------------------------*/
221 /** @defgroup I2S_Exported_Functions I2S Exported Functions
225 /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions
226 * @brief Initialization and Configuration functions
229 ===============================================================================
230 ##### Initialization and de-initialization functions #####
231 ===============================================================================
232 [..] This subsection provides a set of functions allowing to initialize and
233 de-initialize the I2Sx peripheral in simplex mode:
235 (+) User must Implement HAL_I2S_MspInit() function in which he configures
236 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
238 (+) Call the function HAL_I2S_Init() to configure the selected device with
239 the selected configuration:
247 (+) Call the function HAL_I2S_DeInit() to restore the default configuration
248 of the selected I2Sx peripheral.
254 * @brief Initializes the I2S according to the specified parameters
255 * in the I2S_InitTypeDef and create the associated handle.
256 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
257 * the configuration information for I2S module
260 HAL_StatusTypeDef
HAL_I2S_Init(I2S_HandleTypeDef
*hi2s
)
264 uint32_t packetlength
;
269 /* Check the I2S handle allocation */
275 /* Check the I2S parameters */
276 assert_param(IS_I2S_ALL_INSTANCE(hi2s
->Instance
));
277 assert_param(IS_I2S_MODE(hi2s
->Init
.Mode
));
278 assert_param(IS_I2S_STANDARD(hi2s
->Init
.Standard
));
279 assert_param(IS_I2S_DATA_FORMAT(hi2s
->Init
.DataFormat
));
280 assert_param(IS_I2S_MCLK_OUTPUT(hi2s
->Init
.MCLKOutput
));
281 assert_param(IS_I2S_AUDIO_FREQ(hi2s
->Init
.AudioFreq
));
282 assert_param(IS_I2S_CPOL(hi2s
->Init
.CPOL
));
283 assert_param(IS_I2S_FIRST_BIT(hi2s
->Init
.FirstBit
));
284 assert_param(IS_I2S_WS_INVERSION(hi2s
->Init
.WSInversion
));
285 assert_param(IS_I2S_DATA_24BIT_ALIGNMENT(hi2s
->Init
.Data24BitAlignment
));
286 assert_param(IS_I2S_MASTER_KEEP_IO_STATE(hi2s
->Init
.MasterKeepIOState
));
288 if (hi2s
->State
== HAL_I2S_STATE_RESET
)
290 /* Allocate lock resource and initialize it */
291 hi2s
->Lock
= HAL_UNLOCKED
;
293 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
294 /* Init the I2S Callback settings */
295 hi2s
->TxCpltCallback
= HAL_I2S_TxCpltCallback
; /* Legacy weak TxCpltCallback */
296 hi2s
->RxCpltCallback
= HAL_I2S_RxCpltCallback
; /* Legacy weak RxCpltCallback */
297 hi2s
->TxRxCpltCallback
= HAL_I2SEx_TxRxCpltCallback
; /* Legacy weak TxRxCpltCallback */
298 hi2s
->TxHalfCpltCallback
= HAL_I2S_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
299 hi2s
->RxHalfCpltCallback
= HAL_I2S_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
300 hi2s
->TxRxHalfCpltCallback
= HAL_I2SEx_TxRxHalfCpltCallback
; /* Legacy weak TxRxHalfCpltCallback */
301 hi2s
->ErrorCallback
= HAL_I2S_ErrorCallback
; /* Legacy weak ErrorCallback */
303 if (hi2s
->MspInitCallback
== NULL
)
305 hi2s
->MspInitCallback
= HAL_I2S_MspInit
; /* Legacy weak MspInit */
308 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
309 hi2s
->MspInitCallback(hi2s
);
311 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
312 HAL_I2S_MspInit(hi2s
);
313 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
316 hi2s
->State
= HAL_I2S_STATE_BUSY
;
318 /* Disable the selected I2S peripheral */
319 if ((hi2s
->Instance
->CR1
& SPI_CR1_SPE
) == SPI_CR1_SPE
)
321 /* Disable I2S peripheral */
322 __HAL_I2S_DISABLE(hi2s
);
325 /* Clear I2S configuration register */
326 CLEAR_REG(hi2s
->Instance
->I2SCFGR
);
328 if (IS_I2S_MASTER(hi2s
->Init
.Mode
))
330 /*------------------------- I2SDIV and ODD Calculation ---------------------*/
331 /* If the requested audio frequency is not the default, compute the prescaler */
332 if (hi2s
->Init
.AudioFreq
!= I2S_AUDIOFREQ_DEFAULT
)
334 /* Check the frame length (For the Prescaler computing) ********************/
335 if (hi2s
->Init
.DataFormat
!= I2S_DATAFORMAT_16B
)
337 /* Channel length is 32 bits */
342 /* Channel length is 16 bits */
346 /* Check if PCM standard is used */
347 if ((hi2s
->Init
.Standard
== I2S_STANDARD_PCM_SHORT
) ||
348 (hi2s
->Init
.Standard
== I2S_STANDARD_PCM_LONG
))
357 /* Get the source clock value: based on System Clock value */
358 #if defined (SPI_SPI6I2S_SUPPORT)
359 if (hi2s
->Instance
== SPI6
)
361 /* SPI6 source clock */
362 i2sclk
= HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI6
);
366 /* SPI1,SPI2 and SPI3 share the same source clock */
367 i2sclk
= HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI123
);
370 /* SPI1,SPI2 and SPI3 share the same source clock */
371 i2sclk
= HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI123
);
374 /* Compute the Real divider depending on the MCLK output state, with a floating point */
375 if (hi2s
->Init
.MCLKOutput
== I2S_MCLKOUTPUT_ENABLE
)
377 /* MCLK output is enabled */
378 tmp
= (uint32_t)((((i2sclk
/ (256UL >> ispcm
)) * 10UL) / hi2s
->Init
.AudioFreq
) + 5UL);
382 /* MCLK output is disabled */
383 tmp
= (uint32_t)((((i2sclk
/ ((32UL >> ispcm
) * packetlength
)) * 10UL) / hi2s
->Init
.AudioFreq
) + 5UL);
386 /* Remove the flatting point */
389 /* Check the parity of the divider */
390 i2sodd
= (uint32_t)(tmp
& (uint32_t)1UL);
392 /* Compute the i2sdiv prescaler */
393 i2sdiv
= (uint32_t)((tmp
- i2sodd
) / 2UL);
397 /* Set the default values */
402 /* Test if the obtain values are forbidden or out of range */
403 if (((i2sodd
== 1UL) && (i2sdiv
== 1UL)) || (i2sdiv
> 0xFFUL
))
405 /* Set the error code */
406 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_PRESCALER
);
410 /* Force i2smod to 1 just to be sure that (2xi2sdiv + i2sodd) is always higher than 0 */
416 MODIFY_REG(hi2s
->Instance
->I2SCFGR
, (SPI_I2SCFGR_I2SDIV
| SPI_I2SCFGR_ODD
),
417 ((i2sdiv
<< SPI_I2SCFGR_I2SDIV_Pos
) | (i2sodd
<< SPI_I2SCFGR_ODD_Pos
)));
420 /*-------------------------- I2Sx I2SCFGR Configuration --------------------*/
421 /* Configure I2SMOD, I2SCFG, I2SSTD, PCMSYNC, DATLEN ,CHLEN ,CKPOL, WSINV, DATAFMT, I2SDIV, ODD and MCKOE bits bits */
422 /* And configure the I2S with the I2S_InitStruct values */
423 MODIFY_REG(hi2s
->Instance
->I2SCFGR
, (SPI_I2SCFGR_I2SMOD
| SPI_I2SCFGR_I2SCFG
| \
424 SPI_I2SCFGR_I2SSTD
| SPI_I2SCFGR_PCMSYNC
| \
425 SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
| \
426 SPI_I2SCFGR_CKPOL
| SPI_I2SCFGR_WSINV
| \
427 SPI_I2SCFGR_DATFMT
| SPI_I2SCFGR_MCKOE
),
428 (SPI_I2SCFGR_I2SMOD
| hi2s
->Init
.Mode
| \
429 hi2s
->Init
.Standard
| hi2s
->Init
.DataFormat
| \
430 hi2s
->Init
.CPOL
| hi2s
->Init
.WSInversion
| \
431 hi2s
->Init
.Data24BitAlignment
| hi2s
->Init
.MCLKOutput
));
432 /*Clear status register*/
433 WRITE_REG(hi2s
->Instance
->IFCR
, 0x0FF8);
435 /*---------------------------- I2Sx CFG2 Configuration ----------------------*/
437 /* Unlock the AF configuration to configure CFG2 register*/
438 CLEAR_BIT(hi2s
->Instance
->CR1
, SPI_CR1_IOLOCK
);
440 MODIFY_REG(hi2s
->Instance
->CFG2
, SPI_CFG2_LSBFRST
, hi2s
->Init
.FirstBit
);
442 /* Insure that AFCNTR is managed only by Master */
443 if (IS_I2S_MASTER(hi2s
->Init
.Mode
))
445 /* Alternate function GPIOs control */
446 MODIFY_REG(hi2s
->Instance
->CFG2
, SPI_CFG2_AFCNTR
, (hi2s
->Init
.MasterKeepIOState
));
449 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
450 hi2s
->State
= HAL_I2S_STATE_READY
;
456 * @brief DeInitializes the I2S peripheral
457 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
458 * the configuration information for I2S module
461 HAL_StatusTypeDef
HAL_I2S_DeInit(I2S_HandleTypeDef
*hi2s
)
463 /* Check the I2S handle allocation */
469 /* Check the parameters */
470 assert_param(IS_I2S_ALL_INSTANCE(hi2s
->Instance
));
472 hi2s
->State
= HAL_I2S_STATE_BUSY
;
474 /* Disable the I2S Peripheral Clock */
475 __HAL_I2S_DISABLE(hi2s
);
477 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
478 if (hi2s
->MspDeInitCallback
== NULL
)
480 hi2s
->MspDeInitCallback
= HAL_I2S_MspDeInit
; /* Legacy weak MspDeInit */
483 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
484 hi2s
->MspDeInitCallback(hi2s
);
486 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
487 HAL_I2S_MspDeInit(hi2s
);
488 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
490 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
491 hi2s
->State
= HAL_I2S_STATE_RESET
;
500 * @brief I2S MSP Init
501 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
502 * the configuration information for I2S module
505 __weak
void HAL_I2S_MspInit(I2S_HandleTypeDef
*hi2s
)
507 /* Prevent unused argument(s) compilation warning */
510 /* NOTE : This function Should not be modified, when the callback is needed,
511 the HAL_I2S_MspInit could be implemented in the user file
516 * @brief I2S MSP DeInit
517 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
518 * the configuration information for I2S module
521 __weak
void HAL_I2S_MspDeInit(I2S_HandleTypeDef
*hi2s
)
523 /* Prevent unused argument(s) compilation warning */
526 /* NOTE : This function Should not be modified, when the callback is needed,
527 the HAL_I2S_MspDeInit could be implemented in the user file
531 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
533 * @brief Register a User I2S Callback
534 * To be used instead of the weak predefined callback
535 * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains
536 * the configuration information for the specified I2S.
537 * @param CallbackID ID of the callback to be registered
538 * @param pCallback pointer to the Callback function
541 HAL_StatusTypeDef
HAL_I2S_RegisterCallback(I2S_HandleTypeDef
*hi2s
, HAL_I2S_CallbackIDTypeDef CallbackID
, pI2S_CallbackTypeDef pCallback
)
543 HAL_StatusTypeDef status
= HAL_OK
;
545 if (pCallback
== NULL
)
547 /* Update the error code */
548 hi2s
->ErrorCode
|= HAL_I2S_ERROR_INVALID_CALLBACK
;
555 if (HAL_I2S_STATE_READY
== hi2s
->State
)
559 case HAL_I2S_TX_COMPLETE_CB_ID
:
560 hi2s
->TxCpltCallback
= pCallback
;
563 case HAL_I2S_RX_COMPLETE_CB_ID
:
564 hi2s
->RxCpltCallback
= pCallback
;
567 case HAL_I2S_TX_RX_COMPLETE_CB_ID
:
568 hi2s
->TxRxCpltCallback
= pCallback
;
571 case HAL_I2S_TX_HALF_COMPLETE_CB_ID
:
572 hi2s
->TxHalfCpltCallback
= pCallback
;
575 case HAL_I2S_RX_HALF_COMPLETE_CB_ID
:
576 hi2s
->RxHalfCpltCallback
= pCallback
;
580 case HAL_I2S_TX_RX_HALF_COMPLETE_CB_ID
:
581 hi2s
->TxRxHalfCpltCallback
= pCallback
;
584 case HAL_I2S_ERROR_CB_ID
:
585 hi2s
->ErrorCallback
= pCallback
;
588 case HAL_I2S_MSPINIT_CB_ID
:
589 hi2s
->MspInitCallback
= pCallback
;
592 case HAL_I2S_MSPDEINIT_CB_ID
:
593 hi2s
->MspDeInitCallback
= pCallback
;
597 /* Update the error code */
598 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
600 /* Return error status */
605 else if (HAL_I2S_STATE_RESET
== hi2s
->State
)
609 case HAL_I2S_MSPINIT_CB_ID
:
610 hi2s
->MspInitCallback
= pCallback
;
613 case HAL_I2S_MSPDEINIT_CB_ID
:
614 hi2s
->MspDeInitCallback
= pCallback
;
618 /* Update the error code */
619 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
621 /* Return error status */
628 /* Update the error code */
629 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
631 /* Return error status */
641 * @brief Unregister an I2S Callback
642 * I2S callback is redirected to the weak predefined callback
643 * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains
644 * the configuration information for the specified I2S.
645 * @param CallbackID ID of the callback to be unregistered
648 HAL_StatusTypeDef
HAL_I2S_UnRegisterCallback(I2S_HandleTypeDef
*hi2s
, HAL_I2S_CallbackIDTypeDef CallbackID
)
650 HAL_StatusTypeDef status
= HAL_OK
;
655 if (HAL_I2S_STATE_READY
== hi2s
->State
)
659 case HAL_I2S_TX_COMPLETE_CB_ID
:
660 hi2s
->TxCpltCallback
= HAL_I2S_TxCpltCallback
; /* Legacy weak TxCpltCallback */
663 case HAL_I2S_RX_COMPLETE_CB_ID
:
664 hi2s
->RxCpltCallback
= HAL_I2S_RxCpltCallback
; /* Legacy weak RxCpltCallback */
667 case HAL_I2S_TX_RX_COMPLETE_CB_ID
:
668 hi2s
->TxRxCpltCallback
= HAL_I2SEx_TxRxCpltCallback
; /* Legacy weak TxRxCpltCallback */
671 case HAL_I2S_TX_HALF_COMPLETE_CB_ID
:
672 hi2s
->TxHalfCpltCallback
= HAL_I2S_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
675 case HAL_I2S_RX_HALF_COMPLETE_CB_ID
:
676 hi2s
->RxHalfCpltCallback
= HAL_I2S_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
679 case HAL_I2S_TX_RX_HALF_COMPLETE_CB_ID
:
680 hi2s
->TxRxHalfCpltCallback
= HAL_I2SEx_TxRxHalfCpltCallback
; /* Legacy weak TxRxHalfCpltCallback */
683 case HAL_I2S_ERROR_CB_ID
:
684 hi2s
->ErrorCallback
= HAL_I2S_ErrorCallback
; /* Legacy weak ErrorCallback */
687 case HAL_I2S_MSPINIT_CB_ID
:
688 hi2s
->MspInitCallback
= HAL_I2S_MspInit
; /* Legacy weak MspInit */
691 case HAL_I2S_MSPDEINIT_CB_ID
:
692 hi2s
->MspDeInitCallback
= HAL_I2S_MspDeInit
; /* Legacy weak MspDeInit */
696 /* Update the error code */
697 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
699 /* Return error status */
704 else if (HAL_I2S_STATE_RESET
== hi2s
->State
)
708 case HAL_I2S_MSPINIT_CB_ID
:
709 hi2s
->MspInitCallback
= HAL_I2S_MspInit
; /* Legacy weak MspInit */
712 case HAL_I2S_MSPDEINIT_CB_ID
:
713 hi2s
->MspDeInitCallback
= HAL_I2S_MspDeInit
; /* Legacy weak MspDeInit */
717 /* Update the error code */
718 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
720 /* Return error status */
727 /* Update the error code */
728 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
730 /* Return error status */
738 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
743 /** @defgroup I2S_Exported_Functions_Group2 IO operation functions
744 * @brief Data transfers functions
747 ===============================================================================
748 ##### IO operation functions #####
749 ===============================================================================
751 This subsection provides a set of functions allowing to manage the I2S data
754 (#) There are two modes of transfer:
755 (++) Blocking mode : The communication is performed in the polling mode.
756 The status of all data processing is returned by the same function
757 after finishing transfer.
758 (++) No-Blocking mode : The communication is performed using Interrupts
759 or DMA. These functions return the status of the transfer startup.
760 The end of the data processing will be indicated through the
761 dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
764 (#) Blocking mode functions are :
765 (++) HAL_I2S_Transmit()
766 (++) HAL_I2S_Receive()
767 (++) HAL_I2SEx_TransmitReceive()
769 (#) No-Blocking mode functions with Interrupt are :
770 (++) HAL_I2S_Transmit_IT()
771 (++) HAL_I2S_Receive_IT()
772 (++) HAL_I2SEx_TransmitReceive_IT()
774 (#) No-Blocking mode functions with DMA are :
775 (++) HAL_I2S_Transmit_DMA()
776 (++) HAL_I2S_Receive_DMA()
777 (++) HAL_I2SEx_TransmitReceive_DMA()
779 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
780 (++) HAL_I2S_TxCpltCallback()
781 (++) HAL_I2S_RxCpltCallback()
782 (++) HAL_I2SEx_TxRxCpltCallback()
783 (++) HAL_I2S_ErrorCallback()
790 * @brief Transmit an amount of data in blocking mode
791 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
792 * the configuration information for I2S module
793 * @param pData a 16-bit pointer to data buffer.
794 * @param Size number of data sample to be sent:
795 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
796 * configuration phase, the Size parameter means the number of 16-bit data length
797 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
798 * the Size parameter means the number of 16-bit data length.
799 * @param Timeout Timeout duration
800 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
801 * between Master and Slave(example: audio streaming).
804 HAL_StatusTypeDef
HAL_I2S_Transmit(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
, uint32_t Timeout
)
806 #if defined (__GNUC__)
807 __IO
uint16_t *ptxdr_16bits
= (__IO
uint16_t *)(&(hi2s
->Instance
->TXDR
));
808 #endif /* __GNUC__ */
810 if ((pData
== NULL
) || (Size
== 0UL))
818 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
824 /* Set state and reset error code */
825 hi2s
->State
= HAL_I2S_STATE_BUSY_TX
;
826 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
827 hi2s
->pTxBuffPtr
= pData
;
828 hi2s
->TxXferSize
= Size
;
829 hi2s
->TxXferCount
= Size
;
831 /* Initialize fields not used in handle to zero */
832 hi2s
->pRxBuffPtr
= NULL
;
833 hi2s
->RxXferSize
= (uint16_t) 0UL;
834 hi2s
->RxXferCount
= (uint16_t) 0UL;
836 /* Check if the I2S is already enabled */
837 if ((hi2s
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
839 /* Enable I2S peripheral */
840 __HAL_I2S_ENABLE(hi2s
);
843 /* Start the transfer */
844 SET_BIT(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
);
847 /* Wait until TXP flag is set */
848 if (I2S_WaitFlagStateUntilTimeout(hi2s
, I2S_FLAG_TXP
, SET
, Timeout
) != HAL_OK
)
850 /* Set the error code */
851 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_TIMEOUT
);
852 hi2s
->State
= HAL_I2S_STATE_READY
;
857 while (hi2s
->TxXferCount
> 0UL)
859 if ((hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_24B
) || (hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_32B
))
861 /* Transmit data in 32 Bit mode */
862 hi2s
->Instance
->TXDR
= *((uint32_t *)hi2s
->pTxBuffPtr
);
863 hi2s
->pTxBuffPtr
+= 2;
868 /* Transmit data in 16 Bit mode */
869 #if defined (__GNUC__)
870 *ptxdr_16bits
= *((uint16_t *)hi2s
->pTxBuffPtr
);
872 *((__IO
uint16_t *)&hi2s
->Instance
->TXDR
) = *((uint16_t *)hi2s
->pTxBuffPtr
);
873 #endif /* __GNUC__ */
879 /* Wait until TXP flag is set */
880 if (I2S_WaitFlagStateUntilTimeout(hi2s
, I2S_FLAG_TXP
, SET
, Timeout
) != HAL_OK
)
882 /* Set the error code */
883 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_TIMEOUT
);
884 hi2s
->State
= HAL_I2S_STATE_READY
;
889 /* Check if an underrun occurs */
890 if (__HAL_I2S_GET_FLAG(hi2s
, I2S_FLAG_UDR
) == SET
)
892 /* Clear underrun flag */
893 __HAL_I2S_CLEAR_UDRFLAG(hi2s
);
895 /* Set the error code */
896 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_UDR
);
900 hi2s
->State
= HAL_I2S_STATE_READY
;
906 * @brief Receive an amount of data in blocking mode
907 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
908 * the configuration information for I2S module
909 * @param pData a 16-bit pointer to data buffer.
910 * @param Size number of data sample to be sent:
911 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
912 * configuration phase, the Size parameter means the number of 16-bit data length
913 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
914 * the Size parameter means the number of 16-bit data length.
915 * @param Timeout Timeout duration
916 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
917 * between Master and Slave(example: audio streaming).
918 * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate
919 * in continuous way and as the I2S is not disabled at the end of the I2S transaction.
922 HAL_StatusTypeDef
HAL_I2S_Receive(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
, uint32_t Timeout
)
924 #if defined (__GNUC__)
925 __IO
uint16_t *prxdr_16bits
= (__IO
uint16_t *)(&(hi2s
->Instance
->RXDR
));
926 #endif /* __GNUC__ */
928 if ((pData
== NULL
) || (Size
== 0UL))
936 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
942 /* Set state and reset error code */
943 hi2s
->State
= HAL_I2S_STATE_BUSY_RX
;
944 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
945 hi2s
->pRxBuffPtr
= pData
;
946 hi2s
->RxXferSize
= Size
;
947 hi2s
->RxXferCount
= Size
;
949 /* Initialize fields not used in handle to zero */
950 hi2s
->pTxBuffPtr
= NULL
;
951 hi2s
->TxXferSize
= (uint16_t) 0UL;
952 hi2s
->TxXferCount
= (uint16_t) 0UL;
954 /* Check if the I2S is already enabled */
955 if ((hi2s
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
957 /* Enable I2S peripheral */
958 __HAL_I2S_ENABLE(hi2s
);
961 /* Start the transfer */
962 SET_BIT(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
);
965 while (hi2s
->RxXferCount
> 0UL)
967 /* Wait until RXP flag is set */
968 if (I2S_WaitFlagStateUntilTimeout(hi2s
, I2S_FLAG_RXP
, SET
, Timeout
) != HAL_OK
)
970 /* Set the error code */
971 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_TIMEOUT
);
972 hi2s
->State
= HAL_I2S_STATE_READY
;
977 if ((hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_24B
) || (hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_32B
))
979 /* Receive data in 32 Bit mode */
980 *((uint32_t *)hi2s
->pRxBuffPtr
) = hi2s
->Instance
->RXDR
;
981 hi2s
->pRxBuffPtr
+= 2;
986 /* Receive data in 16 Bit mode */
987 #if defined (__GNUC__)
988 *((uint16_t *)hi2s
->pRxBuffPtr
) = *prxdr_16bits
;
990 *((uint16_t *)hi2s
->pRxBuffPtr
) = *((__IO
uint16_t *)&hi2s
->Instance
->RXDR
);
991 #endif /* __GNUC__ */
996 /* Check if an overrun occurs */
997 if (__HAL_I2S_GET_FLAG(hi2s
, I2S_FLAG_OVR
) == SET
)
999 /* Clear overrun flag */
1000 __HAL_I2S_CLEAR_OVRFLAG(hi2s
);
1002 /* Set the error code */
1003 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_OVR
);
1007 hi2s
->State
= HAL_I2S_STATE_READY
;
1013 * @brief Full-Duplex Transmit/Receive data in blocking mode.
1014 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1015 * the configuration information for I2S module
1016 * @param pTxData a 16-bit pointer to the Transmit data buffer.
1017 * @param pRxData a 16-bit pointer to the Receive data buffer.
1018 * @param Size number of data sample to be sent:
1019 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
1020 * configuration phase, the Size parameter means the number of 16-bit data length
1021 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
1022 * the Size parameter means the number of 16-bit data length.
1023 * @param Timeout Timeout duration
1024 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
1025 * between Master and Slave(example: audio streaming).
1026 * @retval HAL status
1029 HAL_StatusTypeDef
HAL_I2SEx_TransmitReceive(I2S_HandleTypeDef
*hi2s
, uint16_t *pTxData
, uint16_t *pRxData
,
1030 uint16_t Size
, uint32_t Timeout
)
1032 uint32_t tmp_TxXferCount
;
1033 uint32_t tmp_RxXferCount
;
1036 #if defined (__GNUC__)
1037 __IO
uint16_t *ptxdr_16bits
= (__IO
uint16_t *)(&(hi2s
->Instance
->TXDR
));
1038 __IO
uint16_t *prxdr_16bits
= (__IO
uint16_t *)(&(hi2s
->Instance
->RXDR
));
1039 #endif /* __GNUC__ */
1041 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
1047 if ((pTxData
== NULL
) || (pRxData
== NULL
) || (Size
== 0U))
1052 /* Process Locked */
1055 /* Init tickstart for timeout management*/
1056 tickstart
= HAL_GetTick();
1058 hi2s
->TxXferSize
= Size
;
1059 hi2s
->TxXferCount
= Size
;
1060 hi2s
->pTxBuffPtr
= pTxData
;
1061 hi2s
->RxXferSize
= Size
;
1062 hi2s
->RxXferCount
= Size
;
1063 hi2s
->pRxBuffPtr
= pRxData
;
1065 tmp_TxXferCount
= hi2s
->TxXferCount
;
1066 tmp_RxXferCount
= hi2s
->RxXferCount
;
1068 /* Set state and reset error code */
1069 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
1070 hi2s
->State
= HAL_I2S_STATE_BUSY_TX_RX
;
1072 /* Check if the I2S is already enabled */
1073 if ((hi2s
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
1075 /* Enable I2S peripheral */
1076 __HAL_I2S_ENABLE(hi2s
);
1079 /* Start the transfer */
1080 SET_BIT(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
);
1082 while ((tmp_TxXferCount
> 0UL) || (tmp_RxXferCount
> 0UL))
1084 if ((__HAL_I2S_GET_FLAG(hi2s
, I2S_FLAG_TXP
) == SET
) && (tmp_TxXferCount
!= 0UL))
1086 if ((hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_24B
) || (hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_32B
))
1088 /* Transmit data in 32 Bit mode */
1089 hi2s
->Instance
->TXDR
= *((uint32_t *)hi2s
->pTxBuffPtr
);
1090 hi2s
->pTxBuffPtr
+= 2;
1095 /* Transmit data in 16 Bit mode */
1096 #if defined (__GNUC__)
1097 *ptxdr_16bits
= *((uint16_t *)hi2s
->pTxBuffPtr
);
1099 *((__IO
uint16_t *)&hi2s
->Instance
->TXDR
) = *((uint16_t *)hi2s
->pTxBuffPtr
);
1100 #endif /* __GNUC__ */
1106 /* Check if an underrun occurs */
1107 if (__HAL_I2S_GET_FLAG(hi2s
, I2S_FLAG_UDR
) == SET
)
1109 /* Clear underrun flag */
1110 __HAL_I2S_CLEAR_UDRFLAG(hi2s
);
1112 /* Set the error code */
1113 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_UDR
);
1117 if ((__HAL_I2S_GET_FLAG(hi2s
, I2S_FLAG_RXP
) == SET
) && (tmp_RxXferCount
!= 0UL))
1119 if ((hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_24B
) || (hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_32B
))
1121 /* Receive data in 32 Bit mode */
1122 *((uint32_t *)hi2s
->pRxBuffPtr
) = hi2s
->Instance
->RXDR
;
1123 hi2s
->pRxBuffPtr
+= 2;
1128 /* Receive data in 16 Bit mode */
1129 #if defined (__GNUC__)
1130 *((uint16_t *)hi2s
->pRxBuffPtr
) = *prxdr_16bits
;
1132 *((uint16_t *)hi2s
->pRxBuffPtr
) = *((__IO
uint16_t *)&hi2s
->Instance
->RXDR
);
1133 #endif /* __GNUC__ */
1138 /* Check if an overrun occurs */
1139 if (__HAL_I2S_GET_FLAG(hi2s
, I2S_FLAG_OVR
) == SET
)
1141 /* Clear overrun flag */
1142 __HAL_I2S_CLEAR_OVRFLAG(hi2s
);
1144 /* Set the error code */
1145 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_OVR
);
1149 /* Timeout management */
1150 if ((((HAL_GetTick() - tickstart
) >= Timeout
) && (Timeout
!= HAL_MAX_DELAY
)) || (Timeout
== 0U))
1152 /* Set the error code */
1153 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_TIMEOUT
);
1154 hi2s
->State
= HAL_I2S_STATE_READY
;
1160 hi2s
->State
= HAL_I2S_STATE_READY
;
1166 * @brief Transmit an amount of data in non-blocking mode with Interrupt
1167 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1168 * the configuration information for I2S module
1169 * @param pData a 16-bit pointer to data buffer.
1170 * @param Size number of data sample to be sent:
1171 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
1172 * configuration phase, the Size parameter means the number of 16-bit data length
1173 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
1174 * the Size parameter means the number of 16-bit data length.
1175 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
1176 * between Master and Slave(example: audio streaming).
1177 * @retval HAL status
1179 HAL_StatusTypeDef
HAL_I2S_Transmit_IT(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
1181 if ((pData
== NULL
) || (Size
== 0UL))
1186 /* Process Locked */
1189 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
1195 /* Set state and reset error code */
1196 hi2s
->State
= HAL_I2S_STATE_BUSY_TX
;
1197 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
1198 hi2s
->pTxBuffPtr
= (uint16_t *)pData
;
1199 hi2s
->TxXferSize
= Size
;
1200 hi2s
->TxXferCount
= Size
;
1202 /* Initialize fields not used in handle to zero */
1203 hi2s
->pRxBuffPtr
= NULL
;
1204 hi2s
->RxXferSize
= (uint16_t) 0UL;
1205 hi2s
->RxXferCount
= (uint16_t) 0UL;
1207 /* Set the function for IT treatment */
1208 if ((hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_24B
) || (hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_32B
))
1210 hi2s
->TxISR
= I2S_Transmit_32Bit_IT
;
1214 hi2s
->TxISR
= I2S_Transmit_16Bit_IT
;
1217 /* Check if the I2S is already enabled */
1218 if ((hi2s
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
1220 /* Enable I2S peripheral */
1221 __HAL_I2S_ENABLE(hi2s
);
1224 /* Enable TXP and UDR interrupt */
1225 __HAL_I2S_ENABLE_IT(hi2s
, (I2S_IT_TXP
| I2S_IT_UDR
));
1227 /* Enable TIFRE interrupt if the mode is Slave */
1228 if (hi2s
->Init
.Mode
== I2S_MODE_SLAVE_TX
)
1230 __HAL_I2S_ENABLE_IT(hi2s
, I2S_IT_FRE
);
1233 /* Start the transfer */
1234 SET_BIT(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
);
1241 * @brief Receive an amount of data in non-blocking mode with Interrupt
1242 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1243 * the configuration information for I2S module
1244 * @param pData a 16-bit pointer to the Receive data buffer.
1245 * @param Size number of data sample to be sent:
1246 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
1247 * configuration phase, the Size parameter means the number of 16-bit data length
1248 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
1249 * the Size parameter means the number of 16-bit data length.
1250 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
1251 * between Master and Slave(example: audio streaming).
1252 * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronization
1253 * between Master and Slave otherwise the I2S interrupt should be optimized.
1254 * @retval HAL status
1256 HAL_StatusTypeDef
HAL_I2S_Receive_IT(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
1258 if ((pData
== NULL
) || (Size
== 0UL))
1263 /* Process Locked */
1266 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
1272 /* Set state and reset error code */
1273 hi2s
->State
= HAL_I2S_STATE_BUSY_RX
;
1274 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
1275 hi2s
->pRxBuffPtr
= pData
;
1276 hi2s
->RxXferSize
= Size
;
1277 hi2s
->RxXferCount
= Size
;
1279 /* Initialize fields not used in handle to zero */
1280 hi2s
->pTxBuffPtr
= NULL
;
1281 hi2s
->TxXferSize
= (uint16_t) 0UL;
1282 hi2s
->TxXferCount
= (uint16_t) 0UL;
1284 /* Set the function for IT treatment */
1285 if ((hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_24B
) || (hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_32B
))
1287 hi2s
->RxISR
= I2S_Receive_32Bit_IT
;
1291 hi2s
->RxISR
= I2S_Receive_16Bit_IT
;
1294 /* Check if the I2S is already enabled */
1295 if ((hi2s
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
1297 /* Enable I2S peripheral */
1298 __HAL_I2S_ENABLE(hi2s
);
1300 /* Enable RXP and ERR interrupt */
1301 __HAL_I2S_ENABLE_IT(hi2s
, (I2S_IT_RXP
| I2S_IT_OVR
));
1303 /* Enable TIFRE interrupt if the mode is Slave */
1304 if (hi2s
->Init
.Mode
== I2S_MODE_SLAVE_RX
)
1306 __HAL_I2S_ENABLE_IT(hi2s
, I2S_IT_FRE
);
1309 /* Start the transfer */
1310 SET_BIT(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
);
1317 * @brief Full-Duplex Transmit/Receive data in non-blocking mode using Interrupt
1318 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1319 * the configuration information for I2S module
1320 * @param pTxData a 16-bit pointer to the Transmit data buffer.
1321 * @param pRxData a 16-bit pointer to the Receive data buffer.
1322 * @param Size number of data sample to be sent:
1323 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
1324 * configuration phase, the Size parameter means the number of 16-bit data length
1325 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
1326 * the Size parameter means the number of 16-bit data length.
1327 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
1328 * between Master and Slave(example: audio streaming).
1329 * @retval HAL status
1331 HAL_StatusTypeDef
HAL_I2SEx_TransmitReceive_IT(I2S_HandleTypeDef
*hi2s
, uint16_t *pTxData
, uint16_t *pRxData
,
1334 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
1340 if ((pTxData
== NULL
) || (pRxData
== NULL
) || (Size
== 0U))
1345 /* Process Locked */
1348 hi2s
->pTxBuffPtr
= pTxData
;
1349 hi2s
->pRxBuffPtr
= pRxData
;
1351 hi2s
->TxXferSize
= Size
;
1352 hi2s
->TxXferCount
= Size
;
1353 hi2s
->RxXferSize
= Size
;
1354 hi2s
->RxXferCount
= Size
;
1356 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
1357 hi2s
->State
= HAL_I2S_STATE_BUSY_TX_RX
;
1360 /* Set the function for IT treatment */
1361 if ((hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_24B
) || (hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_32B
))
1363 hi2s
->TxISR
= I2S_Transmit_32Bit_IT
;
1364 hi2s
->RxISR
= I2S_Receive_32Bit_IT
;
1368 hi2s
->TxISR
= I2S_Transmit_16Bit_IT
;
1369 hi2s
->RxISR
= I2S_Receive_16Bit_IT
;
1372 /* Check if the I2S is already enabled */
1373 if ((hi2s
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
1375 /* Enable I2S peripheral */
1376 __HAL_I2S_ENABLE(hi2s
);
1379 /* Enable TXP, RXP, DXP, UDR, OVR interrupts */
1380 __HAL_I2S_ENABLE_IT(hi2s
, (I2S_IT_TXP
| I2S_IT_RXP
| I2S_IT_DXP
| I2S_IT_UDR
| I2S_IT_OVR
));
1382 /* Enable TIFRE interrupt if the mode is Slave */
1383 if (hi2s
->Init
.Mode
== I2S_MODE_SLAVE_FULLDUPLEX
)
1385 __HAL_I2S_ENABLE_IT(hi2s
, I2S_IT_FRE
);
1388 /* Start the transfer */
1389 SET_BIT(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
);
1397 * @brief Transmit an amount of data in non-blocking mode with DMA
1398 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1399 * the configuration information for I2S module
1400 * @param pData a 16-bit pointer to the Transmit data buffer.
1401 * @param Size number of data sample to be sent:
1402 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
1403 * configuration phase, the Size parameter means the number of 16-bit data length
1404 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
1405 * the Size parameter means the number of 16-bit data length.
1406 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
1407 * between Master and Slave(example: audio streaming).
1408 * @retval HAL status
1410 HAL_StatusTypeDef
HAL_I2S_Transmit_DMA(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
1412 if ((pData
== NULL
) || (Size
== 0UL))
1417 /* Process Locked */
1420 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
1426 /* Set state and reset error code */
1427 hi2s
->State
= HAL_I2S_STATE_BUSY_TX
;
1428 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
1429 hi2s
->pTxBuffPtr
= pData
;
1430 hi2s
->TxXferSize
= Size
;
1431 hi2s
->TxXferCount
= Size
;
1433 /* Init field not used in handle to zero */
1434 hi2s
->pRxBuffPtr
= NULL
;
1435 hi2s
->RxXferSize
= (uint16_t)0UL;
1436 hi2s
->RxXferCount
= (uint16_t)0UL;
1438 /* Set the I2S Tx DMA Half transfer complete callback */
1439 hi2s
->hdmatx
->XferHalfCpltCallback
= I2S_DMATxHalfCplt
;
1441 /* Set the I2S Tx DMA transfer complete callback */
1442 hi2s
->hdmatx
->XferCpltCallback
= I2S_DMATxCplt
;
1444 /* Set the DMA error callback */
1445 hi2s
->hdmatx
->XferErrorCallback
= I2S_DMAError
;
1447 /* Enable the Tx DMA Stream/Channel */
1448 if (HAL_OK
!= HAL_DMA_Start_IT(hi2s
->hdmatx
, (uint32_t)hi2s
->pTxBuffPtr
, (uint32_t)&hi2s
->Instance
->TXDR
, hi2s
->TxXferSize
))
1450 /* Update I2S error code */
1451 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
1452 hi2s
->State
= HAL_I2S_STATE_READY
;
1458 /* Check if the I2S Tx request is already enabled */
1459 if (HAL_IS_BIT_CLR(hi2s
->Instance
->CFG1
, SPI_CFG1_TXDMAEN
))
1461 /* Enable Tx DMA Request */
1462 SET_BIT(hi2s
->Instance
->CFG1
, SPI_CFG1_TXDMAEN
);
1465 /* Check if the I2S is already enabled */
1466 if (HAL_IS_BIT_CLR(hi2s
->Instance
->CR1
, SPI_CR1_SPE
))
1468 /* Enable I2S peripheral */
1469 __HAL_I2S_ENABLE(hi2s
);
1472 /* Start the transfer */
1473 SET_BIT(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
);
1480 * @brief Receive an amount of data in non-blocking mode with DMA
1481 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1482 * the configuration information for I2S module
1483 * @param pData a 16-bit pointer to the Receive data buffer.
1484 * @param Size number of data sample to be sent:
1485 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
1486 * configuration phase, the Size parameter means the number of 16-bit data length
1487 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
1488 * the Size parameter means the number of 16-bit data length.
1489 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
1490 * between Master and Slave(example: audio streaming).
1491 * @retval HAL status
1493 HAL_StatusTypeDef
HAL_I2S_Receive_DMA(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
1495 if ((pData
== NULL
) || (Size
== 0UL))
1500 /* Process Locked */
1503 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
1509 /* Set state and reset error code */
1510 hi2s
->State
= HAL_I2S_STATE_BUSY_RX
;
1511 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
1512 hi2s
->pRxBuffPtr
= pData
;
1513 hi2s
->RxXferSize
= Size
;
1514 hi2s
->RxXferCount
= Size
;
1516 /* Init field not used in handle to zero */
1517 hi2s
->pTxBuffPtr
= NULL
;
1518 hi2s
->TxXferSize
= (uint16_t)0UL;
1519 hi2s
->TxXferCount
= (uint16_t)0UL;
1522 /* Set the I2S Rx DMA Half transfer complete callback */
1523 hi2s
->hdmarx
->XferHalfCpltCallback
= I2S_DMARxHalfCplt
;
1525 /* Set the I2S Rx DMA transfer complete callback */
1526 hi2s
->hdmarx
->XferCpltCallback
= I2S_DMARxCplt
;
1528 /* Set the DMA error callback */
1529 hi2s
->hdmarx
->XferErrorCallback
= I2S_DMAError
;
1531 /* Enable the Rx DMA Stream/Channel */
1532 if (HAL_OK
!= HAL_DMA_Start_IT(hi2s
->hdmarx
, (uint32_t)&hi2s
->Instance
->RXDR
, (uint32_t)hi2s
->pRxBuffPtr
, hi2s
->RxXferSize
))
1534 /* Update I2S error code */
1535 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
1536 hi2s
->State
= HAL_I2S_STATE_READY
;
1542 /* Check if the I2S Rx request is already enabled */
1543 if (HAL_IS_BIT_CLR(hi2s
->Instance
->CFG1
, SPI_CFG1_RXDMAEN
))
1545 /* Enable Rx DMA Request */
1546 SET_BIT(hi2s
->Instance
->CFG1
, SPI_CFG1_RXDMAEN
);
1549 /* Check if the I2S is already enabled */
1550 if (HAL_IS_BIT_CLR(hi2s
->Instance
->CR1
, SPI_CR1_SPE
))
1552 /* Enable I2S peripheral */
1553 __HAL_I2S_ENABLE(hi2s
);
1556 /* Start the transfer */
1557 SET_BIT(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
);
1564 * @brief Full-Duplex Transmit/Receive data in non-blocking mode using DMA
1565 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1566 * the configuration information for I2S module
1567 * @param pTxData a 16-bit pointer to the Transmit data buffer.
1568 * @param pRxData a 16-bit pointer to the Receive data buffer.
1569 * @param Size number of data sample to be sent:
1570 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
1571 * configuration phase, the Size parameter means the number of 16-bit data length
1572 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
1573 * the Size parameter means the number of 16-bit data length.
1574 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
1575 * between Master and Slave(example: audio streaming).
1576 * @retval HAL status
1578 HAL_StatusTypeDef
HAL_I2SEx_TransmitReceive_DMA(I2S_HandleTypeDef
*hi2s
, uint16_t *pTxData
, uint16_t *pRxData
,
1581 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
1587 if ((pTxData
== NULL
) || (pRxData
== NULL
) || (Size
== 0U))
1592 /* Process Locked */
1595 hi2s
->pTxBuffPtr
= pTxData
;
1596 hi2s
->pRxBuffPtr
= pRxData
;
1598 hi2s
->TxXferSize
= Size
;
1599 hi2s
->TxXferCount
= Size
;
1600 hi2s
->RxXferSize
= Size
;
1601 hi2s
->RxXferCount
= Size
;
1603 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
1604 hi2s
->State
= HAL_I2S_STATE_BUSY_TX_RX
;
1606 /* Reset the Tx/Rx DMA bits */
1607 CLEAR_BIT(hi2s
->Instance
->CFG1
, SPI_CFG1_TXDMAEN
| SPI_CFG1_RXDMAEN
);
1609 /* Set the I2S Rx DMA Half transfer complete callback */
1610 hi2s
->hdmarx
->XferHalfCpltCallback
= I2SEx_DMATxRxHalfCplt
;
1612 /* Set the I2S Rx DMA transfer complete callback */
1613 hi2s
->hdmarx
->XferCpltCallback
= I2SEx_DMATxRxCplt
;
1615 /* Set the I2S Rx DMA error callback */
1616 hi2s
->hdmarx
->XferErrorCallback
= I2S_DMAError
;
1618 /* Enable the Tx DMA Stream/Channel */
1619 if (HAL_OK
!= HAL_DMA_Start_IT(hi2s
->hdmatx
, (uint32_t)hi2s
->pTxBuffPtr
, (uint32_t)&hi2s
->Instance
->TXDR
, hi2s
->TxXferSize
))
1621 /* Update I2S error code */
1622 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
1623 hi2s
->State
= HAL_I2S_STATE_READY
;
1629 /* Check if the I2S Tx request is already enabled */
1630 if (HAL_IS_BIT_CLR(hi2s
->Instance
->CFG1
, SPI_CFG1_TXDMAEN
))
1632 /* Enable Tx DMA Request */
1633 SET_BIT(hi2s
->Instance
->CFG1
, SPI_CFG1_TXDMAEN
);
1636 /* Enable the Rx DMA Stream/Channel */
1637 if (HAL_OK
!= HAL_DMA_Start_IT(hi2s
->hdmarx
, (uint32_t)&hi2s
->Instance
->RXDR
, (uint32_t)hi2s
->pRxBuffPtr
, hi2s
->RxXferSize
))
1639 /* Update I2S error code */
1640 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
1641 hi2s
->State
= HAL_I2S_STATE_READY
;
1647 /* Check if the I2S Rx request is already enabled */
1648 if (HAL_IS_BIT_CLR(hi2s
->Instance
->CFG1
, SPI_CFG1_RXDMAEN
))
1650 /* Enable Rx DMA Request */
1651 SET_BIT(hi2s
->Instance
->CFG1
, SPI_CFG1_RXDMAEN
);
1654 /* Check if the I2S is already enabled */
1655 if (HAL_IS_BIT_CLR(hi2s
->Instance
->CR1
, SPI_CR1_SPE
))
1657 /* Enable I2S peripheral */
1658 __HAL_I2S_ENABLE(hi2s
);
1661 /* Start the transfer */
1662 SET_BIT(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
);
1669 * @brief Pauses the audio DMA Stream/Channel playing from the Media.
1670 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1671 * the configuration information for I2S module
1672 * @retval HAL status
1674 HAL_StatusTypeDef
HAL_I2S_DMAPause(I2S_HandleTypeDef
*hi2s
)
1676 /* Process Locked */
1682 tickstart
= HAL_GetTick();
1685 /* Check if the I2S peripheral is in master mode */
1686 if (IS_I2S_MASTER(hi2s
->Init
.Mode
))
1688 /* Check if there is a transfer on-going */
1689 if (HAL_IS_BIT_SET(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
) == 0UL)
1691 /* Set error code to no on going transfer */
1692 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_NO_OGT
);
1693 hi2s
->State
= HAL_I2S_STATE_READY
;
1699 SET_BIT(hi2s
->Instance
->CR1
, SPI_CR1_CSUSP
);
1701 while (HAL_IS_BIT_SET(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
) != 0UL)
1703 if ((((HAL_GetTick() - tickstart
) >= I2S_TIMEOUT
) && (I2S_TIMEOUT
!= HAL_MAX_DELAY
)) || (I2S_TIMEOUT
== 0U))
1705 /* Set the I2S State ready */
1706 hi2s
->State
= HAL_I2S_STATE_READY
;
1708 /* Process Unlocked */
1711 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_TIMEOUT
);
1712 hi2s
->State
= HAL_I2S_STATE_READY
;
1717 /* Disable I2S peripheral */
1718 __HAL_I2S_DISABLE(hi2s
);
1720 hi2s
->State
= HAL_I2S_STATE_READY
;
1722 /* Process Unlocked */
1729 /* Set error code to not supported */
1730 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_NOT_SUPPORTED
);
1731 hi2s
->State
= HAL_I2S_STATE_READY
;
1733 /* Process Unlocked */
1741 * @brief Resumes the audio DMA Stream/Channel playing from the Media.
1742 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1743 * the configuration information for I2S module
1744 * @retval HAL status
1746 HAL_StatusTypeDef
HAL_I2S_DMAResume(I2S_HandleTypeDef
*hi2s
)
1748 /* Process Locked */
1751 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
1753 hi2s
->State
= HAL_I2S_STATE_READY
;
1759 /* Set state and reset error code */
1760 hi2s
->State
= HAL_I2S_STATE_BUSY
;
1761 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
1763 /* Enable I2S peripheral */
1764 __HAL_I2S_ENABLE(hi2s
);
1766 /* Start the transfer */
1767 SET_BIT(hi2s
->Instance
->CR1
, SPI_CR1_CSTART
);
1769 /* Process Unlocked */
1776 * @brief Stops the audio DMA Stream/Channel playing from the Media.
1777 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1778 * the configuration information for I2S module
1779 * @retval HAL status
1781 HAL_StatusTypeDef
HAL_I2S_DMAStop(I2S_HandleTypeDef
*hi2s
)
1783 HAL_StatusTypeDef errorcode
= HAL_OK
;
1784 /* The Lock is not implemented on this API to allow the user application
1785 to call the HAL I2S API under callbacks HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback()
1786 when calling HAL_DMA_Abort() API the DMA TX or RX Transfer complete interrupt is generated
1787 and the correspond call back is executed HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback()
1790 /* Disable the I2S Tx/Rx DMA requests */
1791 CLEAR_BIT(hi2s
->Instance
->CFG1
, SPI_CFG1_TXDMAEN
);
1792 CLEAR_BIT(hi2s
->Instance
->CFG1
, SPI_CFG1_RXDMAEN
);
1794 /* Abort the I2S DMA tx Stream/Channel */
1795 if (hi2s
->hdmatx
!= NULL
)
1797 /* Disable the I2S DMA tx Stream/Channel */
1798 if (HAL_OK
!= HAL_DMA_Abort(hi2s
->hdmatx
))
1800 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
1801 errorcode
= HAL_ERROR
;
1805 /* Abort the I2S DMA rx Stream/Channel */
1806 if (hi2s
->hdmarx
!= NULL
)
1808 /* Disable the I2S DMA rx Stream/Channel */
1809 if (HAL_OK
!= HAL_DMA_Abort(hi2s
->hdmarx
))
1811 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
1812 errorcode
= HAL_ERROR
;
1816 /* Disable I2S peripheral */
1817 __HAL_I2S_DISABLE(hi2s
);
1819 hi2s
->State
= HAL_I2S_STATE_READY
;
1825 * @brief This function handles I2S interrupt request.
1826 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1827 * the configuration information for I2S module
1830 void HAL_I2S_IRQHandler(I2S_HandleTypeDef
*hi2s
)
1832 uint32_t i2sier
= hi2s
->Instance
->IER
;
1833 uint32_t i2ssr
= hi2s
->Instance
->SR
;
1834 uint32_t trigger
= i2sier
& i2ssr
;
1836 if (hi2s
->State
== HAL_I2S_STATE_BUSY_RX
)
1838 /* I2S in mode Receiver ------------------------------------------------*/
1839 if (HAL_IS_BIT_SET(trigger
, I2S_FLAG_RXP
) && HAL_IS_BIT_CLR(trigger
, I2S_FLAG_OVR
))
1844 /* I2S Overrun error interrupt occurred -------------------------------------*/
1845 if (HAL_IS_BIT_SET(trigger
, I2S_FLAG_OVR
))
1847 /* Disable RXP and ERR interrupt */
1848 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_RXP
| I2S_IT_ERR
));
1850 /* Clear Overrun flag */
1851 __HAL_I2S_CLEAR_OVRFLAG(hi2s
);
1853 /* Set the I2S State ready */
1854 hi2s
->State
= HAL_I2S_STATE_READY
;
1857 /* Set the error code and execute error callback*/
1858 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_OVR
);
1859 /* Call user error callback */
1860 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
1861 hi2s
->ErrorCallback(hi2s
);
1863 HAL_I2S_ErrorCallback(hi2s
);
1864 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1868 if (hi2s
->State
== HAL_I2S_STATE_BUSY_TX
)
1870 /* I2S in mode Transmitter -----------------------------------------------*/
1871 if (HAL_IS_BIT_SET(trigger
, I2S_FLAG_TXP
) && HAL_IS_BIT_CLR(trigger
, I2S_FLAG_UDR
))
1876 /* I2S Underrun error interrupt occurred --------------------------------*/
1877 if (HAL_IS_BIT_SET(trigger
, I2S_FLAG_UDR
))
1879 /* Disable TXP and ERR interrupt */
1880 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_TXP
| I2S_IT_ERR
));
1882 /* Clear Underrun flag */
1883 __HAL_I2S_CLEAR_UDRFLAG(hi2s
);
1885 /* Set the I2S State ready */
1886 hi2s
->State
= HAL_I2S_STATE_READY
;
1888 /* Set the error code and execute error callback*/
1889 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_UDR
);
1890 /* Call user error callback */
1891 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
1892 hi2s
->ErrorCallback(hi2s
);
1894 HAL_I2S_ErrorCallback(hi2s
);
1895 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1898 if (hi2s
->State
== HAL_I2S_STATE_BUSY_TX_RX
)
1900 /* I2S in mode Transmitter -----------------------------------------------*/
1901 if (HAL_IS_BIT_SET(trigger
, I2S_FLAG_DXP
))
1906 /* I2S in mode Receiver ------------------------------------------------*/
1907 if (HAL_IS_BIT_SET(trigger
, I2S_FLAG_RXP
) && HAL_IS_BIT_CLR(trigger
, I2S_FLAG_DXP
))
1911 /* I2S in mode Transmitter -----------------------------------------------*/
1912 if (HAL_IS_BIT_SET(trigger
, I2S_FLAG_TXP
) && HAL_IS_BIT_CLR(trigger
, I2S_FLAG_DXP
))
1917 /* I2S Underrun error interrupt occurred --------------------------------*/
1918 if (HAL_IS_BIT_SET(trigger
, I2S_FLAG_UDR
))
1920 /* Disable TXP and ERR interrupt */
1921 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_TXP
| I2S_IT_ERR
));
1923 /* Clear Underrun flag */
1924 __HAL_I2S_CLEAR_UDRFLAG(hi2s
);
1926 /* Set the I2S State ready */
1927 hi2s
->State
= HAL_I2S_STATE_READY
;
1929 /* Set the error code and execute error callback*/
1930 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_UDR
);
1931 /* Call user error callback */
1932 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
1933 hi2s
->ErrorCallback(hi2s
);
1935 HAL_I2S_ErrorCallback(hi2s
);
1936 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1939 /* I2S Overrun error interrupt occurred -------------------------------------*/
1940 if (HAL_IS_BIT_SET(trigger
, I2S_FLAG_OVR
))
1942 /* Disable RXP and ERR interrupt */
1943 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_RXP
| I2S_IT_ERR
));
1945 /* Clear Overrun flag */
1946 __HAL_I2S_CLEAR_OVRFLAG(hi2s
);
1948 /* Set the I2S State ready */
1949 hi2s
->State
= HAL_I2S_STATE_READY
;
1952 /* Set the error code and execute error callback*/
1953 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_OVR
);
1955 /* Call user error callback */
1956 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
1957 hi2s
->ErrorCallback(hi2s
);
1959 HAL_I2S_ErrorCallback(hi2s
);
1960 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1966 * @brief Tx Transfer Half completed callbacks
1967 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1968 * the configuration information for I2S module
1971 __weak
void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef
*hi2s
)
1973 /* Prevent unused argument(s) compilation warning */
1976 /* NOTE : This function Should not be modified, when the callback is needed,
1977 the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
1982 * @brief Tx Transfer completed callbacks
1983 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1984 * the configuration information for I2S module
1987 __weak
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef
*hi2s
)
1989 /* Prevent unused argument(s) compilation warning */
1992 /* NOTE : This function Should not be modified, when the callback is needed,
1993 the HAL_I2S_TxCpltCallback could be implemented in the user file
1998 * @brief Rx Transfer half completed callbacks
1999 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2000 * the configuration information for I2S module
2003 __weak
void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef
*hi2s
)
2005 /* Prevent unused argument(s) compilation warning */
2008 /* NOTE : This function Should not be modified, when the callback is needed,
2009 the HAL_I2S_RxHalfCpltCallback could be implemented in the user file
2014 * @brief Rx Transfer completed callbacks
2015 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2016 * the configuration information for I2S module
2019 __weak
void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef
*hi2s
)
2021 /* Prevent unused argument(s) compilation warning */
2024 /* NOTE : This function Should not be modified, when the callback is needed,
2025 the HAL_I2S_RxCpltCallback could be implemented in the user file
2030 * @brief Rx Transfer half completed callbacks
2031 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2032 * the configuration information for I2S module
2035 __weak
void HAL_I2SEx_TxRxHalfCpltCallback(I2S_HandleTypeDef
*hi2s
)
2037 /* Prevent unused argument(s) compilation warning */
2040 /* NOTE : This function Should not be modified, when the callback is needed,
2041 the HAL_I2S_RxHalfCpltCallback could be implemented in the user file
2046 * @brief Rx Transfer completed callbacks
2047 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2048 * the configuration information for I2S module
2051 __weak
void HAL_I2SEx_TxRxCpltCallback(I2S_HandleTypeDef
*hi2s
)
2053 /* Prevent unused argument(s) compilation warning */
2056 /* NOTE : This function Should not be modified, when the callback is needed,
2057 the HAL_I2S_RxCpltCallback could be implemented in the user file
2062 * @brief I2S error callbacks
2063 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2064 * the configuration information for I2S module
2067 __weak
void HAL_I2S_ErrorCallback(I2S_HandleTypeDef
*hi2s
)
2069 /* Prevent unused argument(s) compilation warning */
2072 /* NOTE : This function Should not be modified, when the callback is needed,
2073 the HAL_I2S_ErrorCallback could be implemented in the user file
2081 /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions
2082 * @brief Peripheral State functions
2085 ===============================================================================
2086 ##### Peripheral State and Errors functions #####
2087 ===============================================================================
2089 This subsection permits to get in run-time the status of the peripheral
2097 * @brief Return the I2S state
2098 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2099 * the configuration information for I2S module
2102 HAL_I2S_StateTypeDef
HAL_I2S_GetState(I2S_HandleTypeDef
*hi2s
)
2108 * @brief Return the I2S error code
2109 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2110 * the configuration information for I2S module
2111 * @retval I2S Error Code
2113 uint32_t HAL_I2S_GetError(I2S_HandleTypeDef
*hi2s
)
2115 return hi2s
->ErrorCode
;
2123 * @brief DMA I2S transmit process complete callback
2124 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
2125 * the configuration information for the specified DMA module.
2128 static void I2S_DMATxCplt(DMA_HandleTypeDef
*hdma
)
2130 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
; /* Derogation MISRAC2012-Rule-11.5 */
2132 /* if DMA is configured in DMA_NORMAL Mode */
2133 if (hdma
->Init
.Mode
== DMA_NORMAL
)
2135 /* Disable Tx DMA Request */
2136 CLEAR_BIT(hi2s
->Instance
->CFG1
, SPI_CFG1_TXDMAEN
);
2138 hi2s
->TxXferCount
= (uint16_t) 0UL;
2139 hi2s
->State
= HAL_I2S_STATE_READY
;
2141 /* Call user Tx complete callback */
2142 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
2143 hi2s
->TxCpltCallback(hi2s
);
2145 HAL_I2S_TxCpltCallback(hi2s
);
2146 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
2150 * @brief DMA I2S transmit process half complete callback
2151 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
2152 * the configuration information for the specified DMA module.
2155 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
)
2157 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
; /* Derogation MISRAC2012-Rule-11.5 */
2159 /* Call user Tx half complete callback */
2160 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
2161 hi2s
->TxHalfCpltCallback(hi2s
);
2163 HAL_I2S_TxHalfCpltCallback(hi2s
);
2164 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
2168 * @brief DMA I2S receive process complete callback
2169 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
2170 * the configuration information for the specified DMA module.
2173 static void I2S_DMARxCplt(DMA_HandleTypeDef
*hdma
)
2175 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
; /* Derogation MISRAC2012-Rule-11.5 */
2177 /* if DMA is configured in DMA_NORMAL Mode */
2178 if (hdma
->Init
.Mode
== DMA_NORMAL
)
2180 /* Disable Rx DMA Request */
2181 CLEAR_BIT(hi2s
->Instance
->CFG1
, SPI_CFG1_RXDMAEN
);
2182 hi2s
->RxXferCount
= (uint16_t)0UL;
2183 hi2s
->State
= HAL_I2S_STATE_READY
;
2185 /* Call user Rx complete callback */
2186 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
2187 hi2s
->RxCpltCallback(hi2s
);
2189 HAL_I2S_RxCpltCallback(hi2s
);
2190 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
2194 * @brief DMA I2S receive process half complete callback
2195 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
2196 * the configuration information for the specified DMA module.
2199 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
)
2201 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
; /* Derogation MISRAC2012-Rule-11.5 */
2203 /* Call user Rx half complete callback */
2204 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
2205 hi2s
->RxHalfCpltCallback(hi2s
);
2207 HAL_I2S_RxHalfCpltCallback(hi2s
);
2208 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
2212 * @brief DMA I2S transmit receive process complete callback
2213 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
2214 * the configuration information for the specified DMA module.
2217 static void I2SEx_DMATxRxCplt(DMA_HandleTypeDef
*hdma
)
2219 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
2221 /* Call user TxRx complete callback */
2222 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
2223 hi2s
->TxRxCpltCallback(hi2s
);
2225 HAL_I2SEx_TxRxCpltCallback(hi2s
);
2226 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
2230 * @brief DMA I2S transmit receive process half complete callback
2231 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
2232 * the configuration information for the specified DMA module.
2235 static void I2SEx_DMATxRxHalfCplt(DMA_HandleTypeDef
*hdma
)
2237 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
2239 /* Call user TxRx Half complete callback */
2240 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
2241 hi2s
->TxRxHalfCpltCallback(hi2s
);
2243 HAL_I2SEx_TxRxHalfCpltCallback(hi2s
);
2244 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
2248 * @brief DMA I2S communication error callback
2249 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
2250 * the configuration information for the specified DMA module.
2253 static void I2S_DMAError(DMA_HandleTypeDef
*hdma
)
2255 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
; /* Derogation MISRAC2012-Rule-11.5 */
2257 /* Disable Rx and Tx DMA Request */
2258 CLEAR_BIT(hi2s
->Instance
->CFG1
, (SPI_CFG1_RXDMAEN
| SPI_CFG1_TXDMAEN
));
2259 hi2s
->TxXferCount
= (uint16_t) 0UL;
2260 hi2s
->RxXferCount
= (uint16_t) 0UL;
2262 hi2s
->State
= HAL_I2S_STATE_READY
;
2264 /* Set the error code and execute error callback*/
2265 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
2266 /* Call user error callback */
2267 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
2268 hi2s
->ErrorCallback(hi2s
);
2270 HAL_I2S_ErrorCallback(hi2s
);
2271 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
2275 * @brief Manage the transmission 16-bit in Interrupt context
2276 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2277 * the configuration information for I2S module
2280 static void I2S_Transmit_16Bit_IT(I2S_HandleTypeDef
*hi2s
)
2283 #if defined (__GNUC__)
2284 __IO
uint16_t *ptxdr_16bits
= (__IO
uint16_t *)(&(hi2s
->Instance
->TXDR
));
2286 *ptxdr_16bits
= *((uint16_t *)hi2s
->pTxBuffPtr
);
2288 *((__IO
uint16_t *)&hi2s
->Instance
->TXDR
) = *((uint16_t *)hi2s
->pTxBuffPtr
);
2289 #endif /* __GNUC__ */
2291 hi2s
->TxXferCount
--;
2293 if (hi2s
->TxXferCount
== 0UL)
2295 /* Disable TXP and ERR interrupt */
2296 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_TXP
| I2S_IT_ERR
));
2298 if ((hi2s
->Init
.Mode
== I2S_MODE_SLAVE_TX
) ||(hi2s
->Init
.Mode
== I2S_MODE_MASTER_TX
))
2300 hi2s
->State
= HAL_I2S_STATE_READY
;
2302 /* Call user Tx complete callback */
2303 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
2304 hi2s
->TxCpltCallback(hi2s
);
2306 HAL_I2S_TxCpltCallback(hi2s
);
2307 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
2312 * @brief Manage the transmission 32-bit in Interrupt context
2313 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2314 * the configuration information for I2S module
2317 static void I2S_Transmit_32Bit_IT(I2S_HandleTypeDef
*hi2s
)
2320 hi2s
->Instance
->TXDR
= *((uint32_t *)hi2s
->pTxBuffPtr
);
2321 hi2s
->pTxBuffPtr
+= 2;
2322 hi2s
->TxXferCount
--;
2324 if (hi2s
->TxXferCount
== 0UL)
2326 /* Disable TXP and ERR interrupt */
2327 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_TXP
| I2S_IT_ERR
));
2329 if ((hi2s
->Init
.Mode
== I2S_MODE_SLAVE_TX
) ||(hi2s
->Init
.Mode
== I2S_MODE_MASTER_TX
))
2331 hi2s
->State
= HAL_I2S_STATE_READY
;
2333 /* Call user Tx complete callback */
2334 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
2335 hi2s
->TxCpltCallback(hi2s
);
2337 HAL_I2S_TxCpltCallback(hi2s
);
2338 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
2343 * @brief Manage the reception 16-bit in Interrupt context
2344 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2345 * the configuration information for I2S module
2348 static void I2S_Receive_16Bit_IT(I2S_HandleTypeDef
*hi2s
)
2351 #if defined (__GNUC__)
2352 __IO
uint16_t *prxdr_16bits
= (__IO
uint16_t *)(&(hi2s
->Instance
->RXDR
));
2354 *((uint16_t *)hi2s
->pRxBuffPtr
) = *prxdr_16bits
;
2356 *((uint16_t *)hi2s
->pRxBuffPtr
) = *((__IO
uint16_t *)&hi2s
->Instance
->RXDR
);
2357 #endif /* __GNUC__ */
2359 hi2s
->RxXferCount
--;
2361 if (hi2s
->RxXferCount
== 0UL)
2363 if (IS_I2S_FULLDUPLEX(hi2s
->Init
.Mode
))
2365 /* Disable TXP, RXP, DXP, ERR interrupts */
2366 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_TXP
| I2S_IT_RXP
| I2S_IT_DXP
| I2S_IT_ERR
));
2370 /* Disable RXP and ERR interrupt */
2371 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_RXP
| I2S_IT_ERR
));
2374 hi2s
->State
= HAL_I2S_STATE_READY
;
2375 /* Call user Rx complete callback */
2376 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
2377 if (IS_I2S_FULLDUPLEX(hi2s
->Init
.Mode
))
2379 hi2s
->TxRxCpltCallback(hi2s
);
2383 hi2s
->RxCpltCallback(hi2s
);
2386 if (IS_I2S_FULLDUPLEX(hi2s
->Init
.Mode
))
2388 HAL_I2SEx_TxRxCpltCallback(hi2s
);
2392 HAL_I2S_RxCpltCallback(hi2s
);
2394 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
2399 * @brief Manage the reception 32-bit in Interrupt context
2400 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2401 * the configuration information for I2S module
2404 static void I2S_Receive_32Bit_IT(I2S_HandleTypeDef
*hi2s
)
2407 *((uint32_t *)hi2s
->pRxBuffPtr
) = hi2s
->Instance
->RXDR
;
2408 hi2s
->pRxBuffPtr
+= 2;
2409 hi2s
->RxXferCount
--;
2411 if (hi2s
->RxXferCount
== 0UL)
2413 if (IS_I2S_FULLDUPLEX(hi2s
->Init
.Mode
))
2415 /* Disable TXP, RXP, DXP, ERR interrupts */
2416 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_TXP
| I2S_IT_RXP
| I2S_IT_DXP
| I2S_IT_ERR
));
2420 /* Disable RXP and ERR interrupt */
2421 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_RXP
| I2S_IT_ERR
));
2424 hi2s
->State
= HAL_I2S_STATE_READY
;
2425 /* Call user Rx complete callback */
2426 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1UL)
2427 if (IS_I2S_FULLDUPLEX(hi2s
->Init
.Mode
))
2429 hi2s
->TxRxCpltCallback(hi2s
);
2433 hi2s
->RxCpltCallback(hi2s
);
2436 if (IS_I2S_FULLDUPLEX(hi2s
->Init
.Mode
))
2438 HAL_I2SEx_TxRxCpltCallback(hi2s
);
2442 HAL_I2S_RxCpltCallback(hi2s
);
2444 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
2449 * @brief This function handles I2S Communication Timeout.
2450 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
2451 * the configuration information for I2S module
2452 * @param Flag Flag checked
2453 * @param State Value of the flag expected
2454 * @param Timeout Duration of the timeout
2455 * @retval HAL status
2457 static HAL_StatusTypeDef
I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef
*hi2s
, uint32_t Flag
, FlagStatus State
, uint32_t Timeout
)
2462 tickstart
= HAL_GetTick();
2464 /* Wait until flag is set to status*/
2465 while (((__HAL_I2S_GET_FLAG(hi2s
, Flag
)) ? SET
: RESET
) != State
)
2467 if (Timeout
!= HAL_MAX_DELAY
)
2469 if (((HAL_GetTick() - tickstart
) >= Timeout
) || (Timeout
== 0UL))
2471 /* Set the I2S State ready */
2472 hi2s
->State
= HAL_I2S_STATE_READY
;
2474 /* Process Unlocked */
2496 #endif /* HAL_I2S_MODULE_ENABLED */
2498 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/