2 ******************************************************************************
3 * @file stm32f7xx_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.
44 -@- Make sure that either:
45 (+@) I2S clock is configured based on SYSCLK or
46 (+@) External clock source is configured after setting correctly
47 the define constant EXTERNAL_CLOCK_VALUE in the stm32f7xx_hal_conf.h file.
49 (#) Three mode of operations are available within this driver :
51 *** Polling mode IO operation ***
52 =================================
54 (+) Send an amount of data in blocking mode using HAL_I2S_Transmit()
55 (+) Receive an amount of data in blocking mode using HAL_I2S_Receive()
57 *** Interrupt mode IO operation ***
58 ===================================
60 (+) Send an amount of data in non blocking mode using HAL_I2S_Transmit_IT()
61 (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can
62 add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback
63 (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can
64 add his own code by customization of function pointer HAL_I2S_TxCpltCallback
65 (+) Receive an amount of data in non blocking mode using HAL_I2S_Receive_IT()
66 (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can
67 add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback
68 (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can
69 add his own code by customization of function pointer HAL_I2S_RxCpltCallback
70 (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
71 add his own code by customization of function pointer HAL_I2S_ErrorCallback
73 *** DMA mode IO operation ***
74 ==============================
76 (+) Send an amount of data in non blocking mode (DMA) using HAL_I2S_Transmit_DMA()
77 (+) At transmission end of half transfer HAL_I2S_TxHalfCpltCallback is executed and user can
78 add his own code by customization of function pointer HAL_I2S_TxHalfCpltCallback
79 (+) At transmission end of transfer HAL_I2S_TxCpltCallback is executed and user can
80 add his own code by customization of function pointer HAL_I2S_TxCpltCallback
81 (+) Receive an amount of data in non blocking mode (DMA) using HAL_I2S_Receive_DMA()
82 (+) At reception end of half transfer HAL_I2S_RxHalfCpltCallback is executed and user can
83 add his own code by customization of function pointer HAL_I2S_RxHalfCpltCallback
84 (+) At reception end of transfer HAL_I2S_RxCpltCallback is executed and user can
85 add his own code by customization of function pointer HAL_I2S_RxCpltCallback
86 (+) In case of transfer Error, HAL_I2S_ErrorCallback() function is executed and user can
87 add his own code by customization of function pointer HAL_I2S_ErrorCallback
88 (+) Pause the DMA Transfer using HAL_I2S_DMAPause()
89 (+) Resume the DMA Transfer using HAL_I2S_DMAResume()
90 (+) Stop the DMA Transfer using HAL_I2S_DMAStop()
92 *** I2S HAL driver macros list ***
93 ===================================
95 Below the list of most used macros in I2S HAL driver.
97 (+) __HAL_I2S_ENABLE: Enable the specified SPI peripheral (in I2S mode)
98 (+) __HAL_I2S_DISABLE: Disable the specified SPI peripheral (in I2S mode)
99 (+) __HAL_I2S_ENABLE_IT : Enable the specified I2S interrupts
100 (+) __HAL_I2S_DISABLE_IT : Disable the specified I2S interrupts
101 (+) __HAL_I2S_GET_FLAG: Check whether the specified I2S flag is set or not
104 (@) You can refer to the I2S HAL driver header file for more useful macros
106 *** I2S HAL driver macros list ***
107 ===================================
109 Callback registration:
111 (#) The compilation flag USE_HAL_I2S_REGISTER_CALLBACKS when set to 1U
112 allows the user to configure dynamically the driver callbacks.
113 Use Functions HAL_I2S_RegisterCallback() to register an interrupt callback.
115 Function HAL_I2S_RegisterCallback() allows to register following callbacks:
116 (+) TxCpltCallback : I2S Tx Completed callback
117 (+) RxCpltCallback : I2S Rx 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 (+) TxHalfCpltCallback : I2S Tx Half Completed callback
135 (+) RxHalfCpltCallback : I2S Rx Half Completed callback
136 (+) ErrorCallback : I2S Error callback
137 (+) MspInitCallback : I2S Msp Init callback
138 (+) MspDeInitCallback : I2S Msp DeInit callback
140 By default, after the HAL_I2S_Init() and when the state is HAL_I2S_STATE_RESET
141 all callbacks are set to the corresponding weak functions:
142 examples HAL_I2S_MasterTxCpltCallback(), HAL_I2S_MasterRxCpltCallback().
143 Exception done for MspInit and MspDeInit functions that are
144 reset to the legacy weak functions in the HAL_I2S_Init()/ HAL_I2S_DeInit() only when
145 these callbacks are null (not registered beforehand).
146 If MspInit or MspDeInit are not null, the HAL_I2S_Init()/ HAL_I2S_DeInit()
147 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
149 Callbacks can be registered/unregistered in HAL_I2S_STATE_READY state only.
150 Exception done MspInit/MspDeInit functions that can be registered/unregistered
151 in HAL_I2S_STATE_READY or HAL_I2S_STATE_RESET state,
152 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
153 Then, the user first registers the MspInit/MspDeInit user callbacks
154 using HAL_I2S_RegisterCallback() before calling HAL_I2S_DeInit()
155 or HAL_I2S_Init() function.
157 When The compilation define USE_HAL_I2S_REGISTER_CALLBACKS is set to 0 or
158 not defined, the callback registering feature is not available
159 and weak (surcharged) callbacks are used.
162 ******************************************************************************
165 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
166 * All rights reserved.</center></h2>
168 * This software component is licensed by ST under BSD 3-Clause license,
169 * the "License"; You may not use this file except in compliance with the
170 * License. You may obtain a copy of the License at:
171 * opensource.org/licenses/BSD-3-Clause
173 ******************************************************************************
176 /* Includes ------------------------------------------------------------------*/
177 #include "stm32f7xx_hal.h"
179 #ifdef HAL_I2S_MODULE_ENABLED
181 /** @addtogroup STM32F7xx_HAL_Driver
185 /** @defgroup I2S I2S
186 * @brief I2S HAL module driver
190 /* Private typedef -----------------------------------------------------------*/
191 /* Private define ------------------------------------------------------------*/
192 /* Private macro -------------------------------------------------------------*/
193 /* Private variables ---------------------------------------------------------*/
194 /* Private function prototypes -----------------------------------------------*/
195 /** @defgroup I2S_Private_Functions I2S Private Functions
198 static void I2S_DMATxCplt(DMA_HandleTypeDef
*hdma
);
199 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
);
200 static void I2S_DMARxCplt(DMA_HandleTypeDef
*hdma
);
201 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
);
202 static void I2S_DMAError(DMA_HandleTypeDef
*hdma
);
203 static void I2S_Transmit_IT(I2S_HandleTypeDef
*hi2s
);
204 static void I2S_Receive_IT(I2S_HandleTypeDef
*hi2s
);
205 static uint32_t I2S_GetClockFreq(I2S_HandleTypeDef
*hi2s
);
206 static HAL_StatusTypeDef
I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef
*hi2s
, uint32_t Flag
, FlagStatus State
,
212 /* Exported functions ---------------------------------------------------------*/
214 /** @defgroup I2S_Exported_Functions I2S Exported Functions
218 /** @defgroup I2S_Exported_Functions_Group1 Initialization and de-initialization functions
219 * @brief Initialization and Configuration functions
222 ===============================================================================
223 ##### Initialization and de-initialization functions #####
224 ===============================================================================
225 [..] This subsection provides a set of functions allowing to initialize and
226 de-initialize the I2Sx peripheral in simplex mode:
228 (+) User must Implement HAL_I2S_MspInit() function in which he configures
229 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
231 (+) Call the function HAL_I2S_Init() to configure the selected device with
232 the selected configuration:
240 (+) Call the function HAL_I2S_DeInit() to restore the default configuration
241 of the selected I2Sx peripheral.
247 * @brief Initializes the I2S according to the specified parameters
248 * in the I2S_InitTypeDef and create the associated handle.
249 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
250 * the configuration information for I2S module
253 HAL_StatusTypeDef
HAL_I2S_Init(I2S_HandleTypeDef
*hi2s
)
257 uint32_t packetlength
;
261 /* Check the I2S handle allocation */
267 /* Check the I2S parameters */
268 assert_param(IS_I2S_ALL_INSTANCE(hi2s
->Instance
));
269 assert_param(IS_I2S_MODE(hi2s
->Init
.Mode
));
270 assert_param(IS_I2S_STANDARD(hi2s
->Init
.Standard
));
271 assert_param(IS_I2S_DATA_FORMAT(hi2s
->Init
.DataFormat
));
272 assert_param(IS_I2S_MCLK_OUTPUT(hi2s
->Init
.MCLKOutput
));
273 assert_param(IS_I2S_AUDIO_FREQ(hi2s
->Init
.AudioFreq
));
274 assert_param(IS_I2S_CPOL(hi2s
->Init
.CPOL
));
275 assert_param(IS_I2S_CLOCKSOURCE(hi2s
->Init
.ClockSource
));
277 if (hi2s
->State
== HAL_I2S_STATE_RESET
)
279 /* Allocate lock resource and initialize it */
280 hi2s
->Lock
= HAL_UNLOCKED
;
282 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
283 /* Init the I2S Callback settings */
284 hi2s
->TxCpltCallback
= HAL_I2S_TxCpltCallback
; /* Legacy weak TxCpltCallback */
285 hi2s
->RxCpltCallback
= HAL_I2S_RxCpltCallback
; /* Legacy weak RxCpltCallback */
286 hi2s
->TxHalfCpltCallback
= HAL_I2S_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
287 hi2s
->RxHalfCpltCallback
= HAL_I2S_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
288 hi2s
->ErrorCallback
= HAL_I2S_ErrorCallback
; /* Legacy weak ErrorCallback */
290 if (hi2s
->MspInitCallback
== NULL
)
292 hi2s
->MspInitCallback
= HAL_I2S_MspInit
; /* Legacy weak MspInit */
295 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
296 hi2s
->MspInitCallback(hi2s
);
298 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
299 HAL_I2S_MspInit(hi2s
);
300 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
303 hi2s
->State
= HAL_I2S_STATE_BUSY
;
305 /*----------------------- SPIx I2SCFGR & I2SPR Configuration ----------------*/
306 /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
307 CLEAR_BIT(hi2s
->Instance
->I2SCFGR
, (SPI_I2SCFGR_CHLEN
| SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CKPOL
| \
308 SPI_I2SCFGR_I2SSTD
| SPI_I2SCFGR_PCMSYNC
| SPI_I2SCFGR_I2SCFG
| \
309 SPI_I2SCFGR_I2SE
| SPI_I2SCFGR_I2SMOD
));
310 hi2s
->Instance
->I2SPR
= 0x0002U
;
312 /*----------------------- I2SPR: I2SDIV and ODD Calculation -----------------*/
313 /* If the requested audio frequency is not the default, compute the prescaler */
314 if (hi2s
->Init
.AudioFreq
!= I2S_AUDIOFREQ_DEFAULT
)
316 /* Check the frame length (For the Prescaler computing) ********************/
317 if (hi2s
->Init
.DataFormat
== I2S_DATAFORMAT_16B
)
319 /* Packet length is 16 bits */
324 /* Packet length is 32 bits */
329 if (hi2s
->Init
.Standard
<= I2S_STANDARD_LSB
)
331 /* In I2S standard packet lenght is multiplied by 2 */
332 packetlength
= packetlength
* 2U;
335 /* If an external I2S clock has to be used, the specific define should be set
336 in the project configuration or in the stm32f3xx_conf.h file */
337 if (hi2s
->Init
.ClockSource
== I2S_CLOCK_EXTERNAL
)
339 /* Set the I2S clock to the external clock value */
340 i2sclk
= EXTERNAL_CLOCK_VALUE
;
344 /* Get the I2S source clock value */
345 i2sclk
= I2S_GetClockFreq(hi2s
);
348 /* Compute the Real divider depending on the MCLK output state, with a floating point */
349 if (hi2s
->Init
.MCLKOutput
== I2S_MCLKOUTPUT_ENABLE
)
351 /* MCLK output is enabled */
352 if (hi2s
->Init
.DataFormat
!= I2S_DATAFORMAT_16B
)
354 tmp
= (uint32_t)(((((i2sclk
/ (packetlength
* 4U)) * 10U) / hi2s
->Init
.AudioFreq
)) + 5U);
358 tmp
= (uint32_t)(((((i2sclk
/ (packetlength
* 8U)) * 10U) / hi2s
->Init
.AudioFreq
)) + 5U);
363 /* MCLK output is disabled */
364 tmp
= (uint32_t)(((((i2sclk
/ packetlength
) * 10U) / hi2s
->Init
.AudioFreq
)) + 5U);
367 /* Remove the flatting point */
370 /* Check the parity of the divider */
371 i2sodd
= (uint32_t)(tmp
& (uint32_t)1U);
373 /* Compute the i2sdiv prescaler */
374 i2sdiv
= (uint32_t)((tmp
- i2sodd
) / 2U);
376 /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
377 i2sodd
= (uint32_t)(i2sodd
<< 8U);
381 /* Set the default values */
386 /* Test if the divider is 1 or 0 or greater than 0xFF */
387 if ((i2sdiv
< 2U) || (i2sdiv
> 0xFFU
))
389 /* Set the error code and execute error callback*/
390 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_PRESCALER
);
394 /*----------------------- SPIx I2SCFGR & I2SPR Configuration ----------------*/
396 /* Write to SPIx I2SPR register the computed value */
397 hi2s
->Instance
->I2SPR
= (uint32_t)((uint32_t)i2sdiv
| (uint32_t)(i2sodd
| (uint32_t)hi2s
->Init
.MCLKOutput
));
399 /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
400 /* And configure the I2S with the I2S_InitStruct values */
401 MODIFY_REG(hi2s
->Instance
->I2SCFGR
, (SPI_I2SCFGR_CHLEN
| SPI_I2SCFGR_DATLEN
| \
402 SPI_I2SCFGR_CKPOL
| SPI_I2SCFGR_I2SSTD
| \
403 SPI_I2SCFGR_PCMSYNC
| SPI_I2SCFGR_I2SCFG
| \
404 SPI_I2SCFGR_I2SE
| SPI_I2SCFGR_I2SMOD
), \
405 (SPI_I2SCFGR_I2SMOD
| hi2s
->Init
.Mode
| \
406 hi2s
->Init
.Standard
| hi2s
->Init
.DataFormat
| \
409 #if defined(SPI_I2SCFGR_ASTRTEN)
410 if ((hi2s
->Init
.Standard
== I2S_STANDARD_PCM_SHORT
) || ((hi2s
->Init
.Standard
== I2S_STANDARD_PCM_LONG
)))
412 /* Write to SPIx I2SCFGR */
413 SET_BIT(hi2s
->Instance
->I2SCFGR
, SPI_I2SCFGR_ASTRTEN
);
417 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
418 hi2s
->State
= HAL_I2S_STATE_READY
;
424 * @brief DeInitializes the I2S peripheral
425 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
426 * the configuration information for I2S module
429 HAL_StatusTypeDef
HAL_I2S_DeInit(I2S_HandleTypeDef
*hi2s
)
431 /* Check the I2S handle allocation */
437 /* Check the parameters */
438 assert_param(IS_I2S_ALL_INSTANCE(hi2s
->Instance
));
440 hi2s
->State
= HAL_I2S_STATE_BUSY
;
442 /* Disable the I2S Peripheral Clock */
443 __HAL_I2S_DISABLE(hi2s
);
445 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
446 if (hi2s
->MspDeInitCallback
== NULL
)
448 hi2s
->MspDeInitCallback
= HAL_I2S_MspDeInit
; /* Legacy weak MspDeInit */
451 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
452 hi2s
->MspDeInitCallback(hi2s
);
454 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
455 HAL_I2S_MspDeInit(hi2s
);
456 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
458 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
459 hi2s
->State
= HAL_I2S_STATE_RESET
;
468 * @brief I2S MSP Init
469 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
470 * the configuration information for I2S module
473 __weak
void HAL_I2S_MspInit(I2S_HandleTypeDef
*hi2s
)
475 /* Prevent unused argument(s) compilation warning */
478 /* NOTE : This function Should not be modified, when the callback is needed,
479 the HAL_I2S_MspInit could be implemented in the user file
484 * @brief I2S MSP DeInit
485 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
486 * the configuration information for I2S module
489 __weak
void HAL_I2S_MspDeInit(I2S_HandleTypeDef
*hi2s
)
491 /* Prevent unused argument(s) compilation warning */
494 /* NOTE : This function Should not be modified, when the callback is needed,
495 the HAL_I2S_MspDeInit could be implemented in the user file
499 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
501 * @brief Register a User I2S Callback
502 * To be used instead of the weak predefined callback
503 * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains
504 * the configuration information for the specified I2S.
505 * @param CallbackID ID of the callback to be registered
506 * @param pCallback pointer to the Callback function
509 HAL_StatusTypeDef
HAL_I2S_RegisterCallback(I2S_HandleTypeDef
*hi2s
, HAL_I2S_CallbackIDTypeDef CallbackID
, pI2S_CallbackTypeDef pCallback
)
511 HAL_StatusTypeDef status
= HAL_OK
;
513 if (pCallback
== NULL
)
515 /* Update the error code */
516 hi2s
->ErrorCode
|= HAL_I2S_ERROR_INVALID_CALLBACK
;
523 if (HAL_I2S_STATE_READY
== hi2s
->State
)
527 case HAL_I2S_TX_COMPLETE_CB_ID
:
528 hi2s
->TxCpltCallback
= pCallback
;
531 case HAL_I2S_RX_COMPLETE_CB_ID
:
532 hi2s
->RxCpltCallback
= pCallback
;
535 case HAL_I2S_TX_HALF_COMPLETE_CB_ID
:
536 hi2s
->TxHalfCpltCallback
= pCallback
;
539 case HAL_I2S_RX_HALF_COMPLETE_CB_ID
:
540 hi2s
->RxHalfCpltCallback
= pCallback
;
543 case HAL_I2S_ERROR_CB_ID
:
544 hi2s
->ErrorCallback
= pCallback
;
547 case HAL_I2S_MSPINIT_CB_ID
:
548 hi2s
->MspInitCallback
= pCallback
;
551 case HAL_I2S_MSPDEINIT_CB_ID
:
552 hi2s
->MspDeInitCallback
= pCallback
;
556 /* Update the error code */
557 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
559 /* Return error status */
564 else if (HAL_I2S_STATE_RESET
== hi2s
->State
)
568 case HAL_I2S_MSPINIT_CB_ID
:
569 hi2s
->MspInitCallback
= pCallback
;
572 case HAL_I2S_MSPDEINIT_CB_ID
:
573 hi2s
->MspDeInitCallback
= pCallback
;
577 /* Update the error code */
578 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
580 /* Return error status */
587 /* Update the error code */
588 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
590 /* Return error status */
600 * @brief Unregister an I2S Callback
601 * I2S callback is redirected to the weak predefined callback
602 * @param hi2s Pointer to a I2S_HandleTypeDef structure that contains
603 * the configuration information for the specified I2S.
604 * @param CallbackID ID of the callback to be unregistered
607 HAL_StatusTypeDef
HAL_I2S_UnRegisterCallback(I2S_HandleTypeDef
*hi2s
, HAL_I2S_CallbackIDTypeDef CallbackID
)
609 HAL_StatusTypeDef status
= HAL_OK
;
614 if (HAL_I2S_STATE_READY
== hi2s
->State
)
618 case HAL_I2S_TX_COMPLETE_CB_ID
:
619 hi2s
->TxCpltCallback
= HAL_I2S_TxCpltCallback
; /* Legacy weak TxCpltCallback */
622 case HAL_I2S_RX_COMPLETE_CB_ID
:
623 hi2s
->RxCpltCallback
= HAL_I2S_RxCpltCallback
; /* Legacy weak RxCpltCallback */
626 case HAL_I2S_TX_HALF_COMPLETE_CB_ID
:
627 hi2s
->TxHalfCpltCallback
= HAL_I2S_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
630 case HAL_I2S_RX_HALF_COMPLETE_CB_ID
:
631 hi2s
->RxHalfCpltCallback
= HAL_I2S_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
634 case HAL_I2S_ERROR_CB_ID
:
635 hi2s
->ErrorCallback
= HAL_I2S_ErrorCallback
; /* Legacy weak ErrorCallback */
638 case HAL_I2S_MSPINIT_CB_ID
:
639 hi2s
->MspInitCallback
= HAL_I2S_MspInit
; /* Legacy weak MspInit */
642 case HAL_I2S_MSPDEINIT_CB_ID
:
643 hi2s
->MspDeInitCallback
= HAL_I2S_MspDeInit
; /* Legacy weak MspDeInit */
647 /* Update the error code */
648 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
650 /* Return error status */
655 else if (HAL_I2S_STATE_RESET
== hi2s
->State
)
659 case HAL_I2S_MSPINIT_CB_ID
:
660 hi2s
->MspInitCallback
= HAL_I2S_MspInit
; /* Legacy weak MspInit */
663 case HAL_I2S_MSPDEINIT_CB_ID
:
664 hi2s
->MspDeInitCallback
= HAL_I2S_MspDeInit
; /* Legacy weak MspDeInit */
668 /* Update the error code */
669 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
671 /* Return error status */
678 /* Update the error code */
679 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_INVALID_CALLBACK
);
681 /* Return error status */
689 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
694 /** @defgroup I2S_Exported_Functions_Group2 IO operation functions
695 * @brief Data transfers functions
698 ===============================================================================
699 ##### IO operation functions #####
700 ===============================================================================
702 This subsection provides a set of functions allowing to manage the I2S data
705 (#) There are two modes of transfer:
706 (++) Blocking mode : The communication is performed in the polling mode.
707 The status of all data processing is returned by the same function
708 after finishing transfer.
709 (++) No-Blocking mode : The communication is performed using Interrupts
710 or DMA. These functions return the status of the transfer startup.
711 The end of the data processing will be indicated through the
712 dedicated I2S IRQ when using Interrupt mode or the DMA IRQ when
715 (#) Blocking mode functions are :
716 (++) HAL_I2S_Transmit()
717 (++) HAL_I2S_Receive()
719 (#) No-Blocking mode functions with Interrupt are :
720 (++) HAL_I2S_Transmit_IT()
721 (++) HAL_I2S_Receive_IT()
723 (#) No-Blocking mode functions with DMA are :
724 (++) HAL_I2S_Transmit_DMA()
725 (++) HAL_I2S_Receive_DMA()
727 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
728 (++) HAL_I2S_TxCpltCallback()
729 (++) HAL_I2S_RxCpltCallback()
730 (++) HAL_I2S_ErrorCallback()
737 * @brief Transmit an amount of data in blocking mode
738 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
739 * the configuration information for I2S module
740 * @param pData a 16-bit pointer to data buffer.
741 * @param Size number of data sample to be sent:
742 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
743 * configuration phase, the Size parameter means the number of 16-bit data length
744 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
745 * the Size parameter means the number of 16-bit data length.
746 * @param Timeout Timeout duration
747 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
748 * between Master and Slave(example: audio streaming).
751 HAL_StatusTypeDef
HAL_I2S_Transmit(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
, uint32_t Timeout
)
753 uint32_t tmpreg_cfgr
;
755 if ((pData
== NULL
) || (Size
== 0U))
763 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
769 /* Set state and reset error code */
770 hi2s
->State
= HAL_I2S_STATE_BUSY_TX
;
771 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
772 hi2s
->pTxBuffPtr
= pData
;
774 tmpreg_cfgr
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
776 if ((tmpreg_cfgr
== I2S_DATAFORMAT_24B
) || (tmpreg_cfgr
== I2S_DATAFORMAT_32B
))
778 hi2s
->TxXferSize
= (Size
<< 1U);
779 hi2s
->TxXferCount
= (Size
<< 1U);
783 hi2s
->TxXferSize
= Size
;
784 hi2s
->TxXferCount
= Size
;
787 tmpreg_cfgr
= hi2s
->Instance
->I2SCFGR
;
789 /* Check if the I2S is already enabled */
790 if ((hi2s
->Instance
->I2SCFGR
& SPI_I2SCFGR_I2SE
) != SPI_I2SCFGR_I2SE
)
792 /* Enable I2S peripheral */
793 __HAL_I2S_ENABLE(hi2s
);
796 /* Wait until TXE flag is set */
797 if (I2S_WaitFlagStateUntilTimeout(hi2s
, I2S_FLAG_TXE
, SET
, Timeout
) != HAL_OK
)
799 /* Set the error code */
800 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_TIMEOUT
);
801 hi2s
->State
= HAL_I2S_STATE_READY
;
806 while (hi2s
->TxXferCount
> 0U)
808 hi2s
->Instance
->DR
= (*hi2s
->pTxBuffPtr
);
812 /* Wait until TXE flag is set */
813 if (I2S_WaitFlagStateUntilTimeout(hi2s
, I2S_FLAG_TXE
, SET
, Timeout
) != HAL_OK
)
815 /* Set the error code */
816 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_TIMEOUT
);
817 hi2s
->State
= HAL_I2S_STATE_READY
;
822 /* Check if an underrun occurs */
823 if (__HAL_I2S_GET_FLAG(hi2s
, I2S_FLAG_UDR
) == SET
)
825 /* Clear underrun flag */
826 __HAL_I2S_CLEAR_UDRFLAG(hi2s
);
828 /* Set the error code */
829 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_UDR
);
833 /* Check if Slave mode is selected */
834 if (((tmpreg_cfgr
& SPI_I2SCFGR_I2SCFG
) == I2S_MODE_SLAVE_TX
) || ((tmpreg_cfgr
& SPI_I2SCFGR_I2SCFG
) == I2S_MODE_SLAVE_RX
))
836 /* Wait until Busy flag is reset */
837 if (I2S_WaitFlagStateUntilTimeout(hi2s
, I2S_FLAG_BSY
, RESET
, Timeout
) != HAL_OK
)
839 /* Set the error code */
840 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_TIMEOUT
);
841 hi2s
->State
= HAL_I2S_STATE_READY
;
847 hi2s
->State
= HAL_I2S_STATE_READY
;
853 * @brief Receive an amount of data in blocking mode
854 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
855 * the configuration information for I2S module
856 * @param pData a 16-bit pointer to data buffer.
857 * @param Size number of data sample to be sent:
858 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
859 * configuration phase, the Size parameter means the number of 16-bit data length
860 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
861 * the Size parameter means the number of 16-bit data length.
862 * @param Timeout Timeout duration
863 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
864 * between Master and Slave(example: audio streaming).
865 * @note In I2S Master Receiver mode, just after enabling the peripheral the clock will be generate
866 * in continuous way and as the I2S is not disabled at the end of the I2S transaction.
869 HAL_StatusTypeDef
HAL_I2S_Receive(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
, uint32_t Timeout
)
871 uint32_t tmpreg_cfgr
;
873 if ((pData
== NULL
) || (Size
== 0U))
881 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
887 /* Set state and reset error code */
888 hi2s
->State
= HAL_I2S_STATE_BUSY_RX
;
889 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
890 hi2s
->pRxBuffPtr
= pData
;
892 tmpreg_cfgr
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
894 if ((tmpreg_cfgr
== I2S_DATAFORMAT_24B
) || (tmpreg_cfgr
== I2S_DATAFORMAT_32B
))
896 hi2s
->RxXferSize
= (Size
<< 1U);
897 hi2s
->RxXferCount
= (Size
<< 1U);
901 hi2s
->RxXferSize
= Size
;
902 hi2s
->RxXferCount
= Size
;
905 /* Check if the I2S is already enabled */
906 if ((hi2s
->Instance
->I2SCFGR
& SPI_I2SCFGR_I2SE
) != SPI_I2SCFGR_I2SE
)
908 /* Enable I2S peripheral */
909 __HAL_I2S_ENABLE(hi2s
);
912 /* Check if Master Receiver mode is selected */
913 if ((hi2s
->Instance
->I2SCFGR
& SPI_I2SCFGR_I2SCFG
) == I2S_MODE_MASTER_RX
)
915 /* Clear the Overrun Flag by a read operation on the SPI_DR register followed by a read
916 access to the SPI_SR register. */
917 __HAL_I2S_CLEAR_OVRFLAG(hi2s
);
921 while (hi2s
->RxXferCount
> 0U)
923 /* Wait until RXNE flag is set */
924 if (I2S_WaitFlagStateUntilTimeout(hi2s
, I2S_FLAG_RXNE
, SET
, Timeout
) != HAL_OK
)
926 /* Set the error code */
927 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_TIMEOUT
);
928 hi2s
->State
= HAL_I2S_STATE_READY
;
933 (*hi2s
->pRxBuffPtr
) = (uint16_t)hi2s
->Instance
->DR
;
937 /* Check if an overrun occurs */
938 if (__HAL_I2S_GET_FLAG(hi2s
, I2S_FLAG_OVR
) == SET
)
940 /* Clear overrun flag */
941 __HAL_I2S_CLEAR_OVRFLAG(hi2s
);
943 /* Set the error code */
944 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_OVR
);
948 hi2s
->State
= HAL_I2S_STATE_READY
;
954 * @brief Transmit an amount of data in non-blocking mode with Interrupt
955 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
956 * the configuration information for I2S module
957 * @param pData a 16-bit pointer to data buffer.
958 * @param Size number of data sample to be sent:
959 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
960 * configuration phase, the Size parameter means the number of 16-bit data length
961 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
962 * the Size parameter means the number of 16-bit data length.
963 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
964 * between Master and Slave(example: audio streaming).
967 HAL_StatusTypeDef
HAL_I2S_Transmit_IT(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
969 uint32_t tmpreg_cfgr
;
971 if ((pData
== NULL
) || (Size
== 0U))
979 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
985 /* Set state and reset error code */
986 hi2s
->State
= HAL_I2S_STATE_BUSY_TX
;
987 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
988 hi2s
->pTxBuffPtr
= pData
;
990 tmpreg_cfgr
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
992 if ((tmpreg_cfgr
== I2S_DATAFORMAT_24B
) || (tmpreg_cfgr
== I2S_DATAFORMAT_32B
))
994 hi2s
->TxXferSize
= (Size
<< 1U);
995 hi2s
->TxXferCount
= (Size
<< 1U);
999 hi2s
->TxXferSize
= Size
;
1000 hi2s
->TxXferCount
= Size
;
1003 /* Enable TXE and ERR interrupt */
1004 __HAL_I2S_ENABLE_IT(hi2s
, (I2S_IT_TXE
| I2S_IT_ERR
));
1006 /* Check if the I2S is already enabled */
1007 if ((hi2s
->Instance
->I2SCFGR
& SPI_I2SCFGR_I2SE
) != SPI_I2SCFGR_I2SE
)
1009 /* Enable I2S peripheral */
1010 __HAL_I2S_ENABLE(hi2s
);
1018 * @brief Receive an amount of data in non-blocking mode with Interrupt
1019 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1020 * the configuration information for I2S module
1021 * @param pData a 16-bit pointer to the Receive data buffer.
1022 * @param Size number of data sample to be sent:
1023 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
1024 * configuration phase, the Size parameter means the number of 16-bit data length
1025 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
1026 * the Size parameter means the number of 16-bit data length.
1027 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
1028 * between Master and Slave(example: audio streaming).
1029 * @note It is recommended to use DMA for the I2S receiver to avoid de-synchronization
1030 * between Master and Slave otherwise the I2S interrupt should be optimized.
1031 * @retval HAL status
1033 HAL_StatusTypeDef
HAL_I2S_Receive_IT(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
1035 uint32_t tmpreg_cfgr
;
1037 if ((pData
== NULL
) || (Size
== 0U))
1042 /* Process Locked */
1045 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
1051 /* Set state and reset error code */
1052 hi2s
->State
= HAL_I2S_STATE_BUSY_RX
;
1053 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
1054 hi2s
->pRxBuffPtr
= pData
;
1056 tmpreg_cfgr
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
1058 if ((tmpreg_cfgr
== I2S_DATAFORMAT_24B
) || (tmpreg_cfgr
== I2S_DATAFORMAT_32B
))
1060 hi2s
->RxXferSize
= (Size
<< 1U);
1061 hi2s
->RxXferCount
= (Size
<< 1U);
1065 hi2s
->RxXferSize
= Size
;
1066 hi2s
->RxXferCount
= Size
;
1069 /* Enable RXNE and ERR interrupt */
1070 __HAL_I2S_ENABLE_IT(hi2s
, (I2S_IT_RXNE
| I2S_IT_ERR
));
1072 /* Check if the I2S is already enabled */
1073 if ((hi2s
->Instance
->I2SCFGR
& SPI_I2SCFGR_I2SE
) != SPI_I2SCFGR_I2SE
)
1075 /* Enable I2S peripheral */
1076 __HAL_I2S_ENABLE(hi2s
);
1084 * @brief Transmit an amount of data in non-blocking mode with DMA
1085 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1086 * the configuration information for I2S module
1087 * @param pData a 16-bit pointer to the Transmit data buffer.
1088 * @param Size number of data sample to be sent:
1089 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
1090 * configuration phase, the Size parameter means the number of 16-bit data length
1091 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
1092 * the Size parameter means the number of 16-bit data length.
1093 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
1094 * between Master and Slave(example: audio streaming).
1095 * @retval HAL status
1097 HAL_StatusTypeDef
HAL_I2S_Transmit_DMA(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
1099 uint32_t tmpreg_cfgr
;
1101 if ((pData
== NULL
) || (Size
== 0U))
1106 /* Process Locked */
1109 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
1115 /* Set state and reset error code */
1116 hi2s
->State
= HAL_I2S_STATE_BUSY_TX
;
1117 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
1118 hi2s
->pTxBuffPtr
= pData
;
1120 tmpreg_cfgr
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
1122 if ((tmpreg_cfgr
== I2S_DATAFORMAT_24B
) || (tmpreg_cfgr
== I2S_DATAFORMAT_32B
))
1124 hi2s
->TxXferSize
= (Size
<< 1U);
1125 hi2s
->TxXferCount
= (Size
<< 1U);
1129 hi2s
->TxXferSize
= Size
;
1130 hi2s
->TxXferCount
= Size
;
1133 /* Set the I2S Tx DMA Half transfer complete callback */
1134 hi2s
->hdmatx
->XferHalfCpltCallback
= I2S_DMATxHalfCplt
;
1136 /* Set the I2S Tx DMA transfer complete callback */
1137 hi2s
->hdmatx
->XferCpltCallback
= I2S_DMATxCplt
;
1139 /* Set the DMA error callback */
1140 hi2s
->hdmatx
->XferErrorCallback
= I2S_DMAError
;
1142 /* Enable the Tx DMA Stream/Channel */
1143 if (HAL_OK
!= HAL_DMA_Start_IT(hi2s
->hdmatx
, (uint32_t)hi2s
->pTxBuffPtr
, (uint32_t)&hi2s
->Instance
->DR
, hi2s
->TxXferSize
))
1145 /* Update SPI error code */
1146 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
1147 hi2s
->State
= HAL_I2S_STATE_READY
;
1153 /* Check if the I2S is already enabled */
1154 if (HAL_IS_BIT_CLR(hi2s
->Instance
->I2SCFGR
, SPI_I2SCFGR_I2SE
))
1156 /* Enable I2S peripheral */
1157 __HAL_I2S_ENABLE(hi2s
);
1160 /* Check if the I2S Tx request is already enabled */
1161 if (HAL_IS_BIT_CLR(hi2s
->Instance
->CR2
, SPI_CR2_TXDMAEN
))
1163 /* Enable Tx DMA Request */
1164 SET_BIT(hi2s
->Instance
->CR2
, SPI_CR2_TXDMAEN
);
1172 * @brief Receive an amount of data in non-blocking mode with DMA
1173 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1174 * the configuration information for I2S module
1175 * @param pData a 16-bit pointer to the Receive data buffer.
1176 * @param Size number of data sample to be sent:
1177 * @note When a 16-bit data frame or a 16-bit data frame extended is selected during the I2S
1178 * configuration phase, the Size parameter means the number of 16-bit data length
1179 * in the transaction and when a 24-bit data frame or a 32-bit data frame is selected
1180 * the Size parameter means the number of 16-bit data length.
1181 * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
1182 * between Master and Slave(example: audio streaming).
1183 * @retval HAL status
1185 HAL_StatusTypeDef
HAL_I2S_Receive_DMA(I2S_HandleTypeDef
*hi2s
, uint16_t *pData
, uint16_t Size
)
1187 uint32_t tmpreg_cfgr
;
1189 if ((pData
== NULL
) || (Size
== 0U))
1194 /* Process Locked */
1197 if (hi2s
->State
!= HAL_I2S_STATE_READY
)
1203 /* Set state and reset error code */
1204 hi2s
->State
= HAL_I2S_STATE_BUSY_RX
;
1205 hi2s
->ErrorCode
= HAL_I2S_ERROR_NONE
;
1206 hi2s
->pRxBuffPtr
= pData
;
1208 tmpreg_cfgr
= hi2s
->Instance
->I2SCFGR
& (SPI_I2SCFGR_DATLEN
| SPI_I2SCFGR_CHLEN
);
1210 if ((tmpreg_cfgr
== I2S_DATAFORMAT_24B
) || (tmpreg_cfgr
== I2S_DATAFORMAT_32B
))
1212 hi2s
->RxXferSize
= (Size
<< 1U);
1213 hi2s
->RxXferCount
= (Size
<< 1U);
1217 hi2s
->RxXferSize
= Size
;
1218 hi2s
->RxXferCount
= Size
;
1221 /* Set the I2S Rx DMA Half transfer complete callback */
1222 hi2s
->hdmarx
->XferHalfCpltCallback
= I2S_DMARxHalfCplt
;
1224 /* Set the I2S Rx DMA transfer complete callback */
1225 hi2s
->hdmarx
->XferCpltCallback
= I2S_DMARxCplt
;
1227 /* Set the DMA error callback */
1228 hi2s
->hdmarx
->XferErrorCallback
= I2S_DMAError
;
1230 /* Check if Master Receiver mode is selected */
1231 if ((hi2s
->Instance
->I2SCFGR
& SPI_I2SCFGR_I2SCFG
) == I2S_MODE_MASTER_RX
)
1233 /* Clear the Overrun Flag by a read operation to the SPI_DR register followed by a read
1234 access to the SPI_SR register. */
1235 __HAL_I2S_CLEAR_OVRFLAG(hi2s
);
1238 /* Enable the Rx DMA Stream/Channel */
1239 if (HAL_OK
!= HAL_DMA_Start_IT(hi2s
->hdmarx
, (uint32_t)&hi2s
->Instance
->DR
, (uint32_t)hi2s
->pRxBuffPtr
, hi2s
->RxXferSize
))
1241 /* Update SPI error code */
1242 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
1243 hi2s
->State
= HAL_I2S_STATE_READY
;
1249 /* Check if the I2S is already enabled */
1250 if (HAL_IS_BIT_CLR(hi2s
->Instance
->I2SCFGR
, SPI_I2SCFGR_I2SE
))
1252 /* Enable I2S peripheral */
1253 __HAL_I2S_ENABLE(hi2s
);
1256 /* Check if the I2S Rx request is already enabled */
1257 if (HAL_IS_BIT_CLR(hi2s
->Instance
->CR2
, SPI_CR2_RXDMAEN
))
1259 /* Enable Rx DMA Request */
1260 SET_BIT(hi2s
->Instance
->CR2
, SPI_CR2_RXDMAEN
);
1268 * @brief Pauses the audio DMA Stream/Channel playing from the Media.
1269 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1270 * the configuration information for I2S module
1271 * @retval HAL status
1273 HAL_StatusTypeDef
HAL_I2S_DMAPause(I2S_HandleTypeDef
*hi2s
)
1275 /* Process Locked */
1278 if (hi2s
->State
== HAL_I2S_STATE_BUSY_TX
)
1280 /* Disable the I2S DMA Tx request */
1281 CLEAR_BIT(hi2s
->Instance
->CR2
, SPI_CR2_TXDMAEN
);
1283 else if (hi2s
->State
== HAL_I2S_STATE_BUSY_RX
)
1285 /* Disable the I2S DMA Rx request */
1286 CLEAR_BIT(hi2s
->Instance
->CR2
, SPI_CR2_RXDMAEN
);
1293 /* Process Unlocked */
1300 * @brief Resumes the audio DMA Stream/Channel playing from the Media.
1301 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1302 * the configuration information for I2S module
1303 * @retval HAL status
1305 HAL_StatusTypeDef
HAL_I2S_DMAResume(I2S_HandleTypeDef
*hi2s
)
1307 /* Process Locked */
1310 if (hi2s
->State
== HAL_I2S_STATE_BUSY_TX
)
1312 /* Enable the I2S DMA Tx request */
1313 SET_BIT(hi2s
->Instance
->CR2
, SPI_CR2_TXDMAEN
);
1315 else if (hi2s
->State
== HAL_I2S_STATE_BUSY_RX
)
1317 /* Enable the I2S DMA Rx request */
1318 SET_BIT(hi2s
->Instance
->CR2
, SPI_CR2_RXDMAEN
);
1325 /* If the I2S peripheral is still not enabled, enable it */
1326 if (HAL_IS_BIT_CLR(hi2s
->Instance
->I2SCFGR
, SPI_I2SCFGR_I2SE
))
1328 /* Enable I2S peripheral */
1329 __HAL_I2S_ENABLE(hi2s
);
1332 /* Process Unlocked */
1339 * @brief Stops the audio DMA Stream/Channel playing from the Media.
1340 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1341 * the configuration information for I2S module
1342 * @retval HAL status
1344 HAL_StatusTypeDef
HAL_I2S_DMAStop(I2S_HandleTypeDef
*hi2s
)
1346 HAL_StatusTypeDef errorcode
= HAL_OK
;
1347 /* The Lock is not implemented on this API to allow the user application
1348 to call the HAL SPI API under callbacks HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback()
1349 when calling HAL_DMA_Abort() API the DMA TX or RX Transfer complete interrupt is generated
1350 and the correspond call back is executed HAL_I2S_TxCpltCallback() or HAL_I2S_RxCpltCallback()
1353 /* Disable the I2S Tx/Rx DMA requests */
1354 CLEAR_BIT(hi2s
->Instance
->CR2
, SPI_CR2_TXDMAEN
);
1355 CLEAR_BIT(hi2s
->Instance
->CR2
, SPI_CR2_RXDMAEN
);
1357 /* Abort the I2S DMA tx Stream/Channel */
1358 if (hi2s
->hdmatx
!= NULL
)
1360 /* Disable the I2S DMA tx Stream/Channel */
1361 if (HAL_OK
!= HAL_DMA_Abort(hi2s
->hdmatx
))
1363 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
1364 errorcode
= HAL_ERROR
;
1368 /* Abort the I2S DMA rx Stream/Channel */
1369 if (hi2s
->hdmarx
!= NULL
)
1371 /* Disable the I2S DMA rx Stream/Channel */
1372 if (HAL_OK
!= HAL_DMA_Abort(hi2s
->hdmarx
))
1374 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
1375 errorcode
= HAL_ERROR
;
1379 /* Disable I2S peripheral */
1380 __HAL_I2S_DISABLE(hi2s
);
1382 hi2s
->State
= HAL_I2S_STATE_READY
;
1388 * @brief This function handles I2S interrupt request.
1389 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1390 * the configuration information for I2S module
1393 void HAL_I2S_IRQHandler(I2S_HandleTypeDef
*hi2s
)
1395 uint32_t itsource
= hi2s
->Instance
->CR2
;
1396 uint32_t itflag
= hi2s
->Instance
->SR
;
1398 /* I2S in mode Receiver ------------------------------------------------*/
1399 if ((I2S_CHECK_FLAG(itflag
, I2S_FLAG_OVR
) == RESET
) &&
1400 (I2S_CHECK_FLAG(itflag
, I2S_FLAG_RXNE
) != RESET
) && (I2S_CHECK_IT_SOURCE(itsource
, I2S_IT_RXNE
) != RESET
))
1402 I2S_Receive_IT(hi2s
);
1406 /* I2S in mode Tramitter -----------------------------------------------*/
1407 if ((I2S_CHECK_FLAG(itflag
, I2S_FLAG_TXE
) != RESET
) && (I2S_CHECK_IT_SOURCE(itsource
, I2S_IT_TXE
) != RESET
))
1409 I2S_Transmit_IT(hi2s
);
1413 /* I2S interrupt error -------------------------------------------------*/
1414 if (I2S_CHECK_IT_SOURCE(itsource
, I2S_IT_ERR
) != RESET
)
1416 /* I2S Overrun error interrupt occurred ---------------------------------*/
1417 if (I2S_CHECK_FLAG(itflag
, I2S_FLAG_OVR
) != RESET
)
1419 /* Disable RXNE and ERR interrupt */
1420 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_RXNE
| I2S_IT_ERR
));
1422 /* Set the error code and execute error callback*/
1423 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_OVR
);
1426 /* I2S Underrun error interrupt occurred --------------------------------*/
1427 if (I2S_CHECK_FLAG(itflag
, I2S_FLAG_UDR
) != RESET
)
1429 /* Disable TXE and ERR interrupt */
1430 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_TXE
| I2S_IT_ERR
));
1432 /* Set the error code and execute error callback*/
1433 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_UDR
);
1436 /* Set the I2S State ready */
1437 hi2s
->State
= HAL_I2S_STATE_READY
;
1439 /* Call user error callback */
1440 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
1441 hi2s
->ErrorCallback(hi2s
);
1443 HAL_I2S_ErrorCallback(hi2s
);
1444 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1449 * @brief Tx Transfer Half completed callbacks
1450 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1451 * the configuration information for I2S module
1454 __weak
void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef
*hi2s
)
1456 /* Prevent unused argument(s) compilation warning */
1459 /* NOTE : This function Should not be modified, when the callback is needed,
1460 the HAL_I2S_TxHalfCpltCallback could be implemented in the user file
1465 * @brief Tx Transfer completed callbacks
1466 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1467 * the configuration information for I2S module
1470 __weak
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef
*hi2s
)
1472 /* Prevent unused argument(s) compilation warning */
1475 /* NOTE : This function Should not be modified, when the callback is needed,
1476 the HAL_I2S_TxCpltCallback could be implemented in the user file
1481 * @brief Rx Transfer half completed callbacks
1482 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1483 * the configuration information for I2S module
1486 __weak
void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef
*hi2s
)
1488 /* Prevent unused argument(s) compilation warning */
1491 /* NOTE : This function Should not be modified, when the callback is needed,
1492 the HAL_I2S_RxHalfCpltCallback could be implemented in the user file
1497 * @brief Rx Transfer completed callbacks
1498 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1499 * the configuration information for I2S module
1502 __weak
void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef
*hi2s
)
1504 /* Prevent unused argument(s) compilation warning */
1507 /* NOTE : This function Should not be modified, when the callback is needed,
1508 the HAL_I2S_RxCpltCallback could be implemented in the user file
1513 * @brief I2S error callbacks
1514 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1515 * the configuration information for I2S module
1518 __weak
void HAL_I2S_ErrorCallback(I2S_HandleTypeDef
*hi2s
)
1520 /* Prevent unused argument(s) compilation warning */
1523 /* NOTE : This function Should not be modified, when the callback is needed,
1524 the HAL_I2S_ErrorCallback could be implemented in the user file
1532 /** @defgroup I2S_Exported_Functions_Group3 Peripheral State and Errors functions
1533 * @brief Peripheral State functions
1536 ===============================================================================
1537 ##### Peripheral State and Errors functions #####
1538 ===============================================================================
1540 This subsection permits to get in run-time the status of the peripheral
1548 * @brief Return the I2S state
1549 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1550 * the configuration information for I2S module
1553 HAL_I2S_StateTypeDef
HAL_I2S_GetState(I2S_HandleTypeDef
*hi2s
)
1559 * @brief Return the I2S error code
1560 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1561 * the configuration information for I2S module
1562 * @retval I2S Error Code
1564 uint32_t HAL_I2S_GetError(I2S_HandleTypeDef
*hi2s
)
1566 return hi2s
->ErrorCode
;
1576 /** @addtogroup I2S_Private_Functions I2S Private Functions
1580 * @brief Get I2S Input Clock based on I2S source clock selection
1581 * @param hi2s: pointer to a I2S_HandleTypeDef structure that contains
1582 * the configuration information for I2S module.
1583 * @retval I2S Clock Input
1585 static uint32_t I2S_GetClockFreq(I2S_HandleTypeDef
*hi2s
)
1588 /* This variable used to store the VCO Input (value in Hz) */
1590 /* This variable used to store the I2S_CK_x (value in Hz) */
1591 uint32_t i2sclocksource
;
1593 /* Configure I2S Clock based on I2S source clock selection */
1595 /* I2S_CLK_x : I2S Block Clock configuration for different clock sources selected */
1596 switch (hi2s
->Init
.ClockSource
)
1598 case I2S_CLOCK_PLL
:
1600 /* Configure the PLLI2S division factor */
1601 /* PLLI2S_VCO Input = PLL_SOURCE/PLLI2SM */
1602 if ((RCC
->PLLCFGR
& RCC_PLLCFGR_PLLSRC
) == RCC_PLLSOURCE_HSI
)
1604 /* In Case the PLL Source is HSI (Internal Clock) */
1605 vcoinput
= (HSI_VALUE
/ (uint32_t)(RCC
->PLLCFGR
& RCC_PLLCFGR_PLLM
));
1609 /* In Case the PLL Source is HSE (External Clock) */
1610 vcoinput
= ((HSE_VALUE
/ (uint32_t)(RCC
->PLLCFGR
& RCC_PLLCFGR_PLLM
)));
1613 /* PLLI2S_VCO Output = PLLI2S_VCO Input * PLLI2SN */
1614 /* I2S_CLK(first level) = PLLI2S_VCO Output/PLLI2SR */
1615 tmpreg
= (RCC
->PLLI2SCFGR
& RCC_PLLI2SCFGR_PLLI2SR
) >> 28U;
1616 i2sclocksource
= (vcoinput
* ((RCC
->PLLI2SCFGR
& RCC_PLLI2SCFGR_PLLI2SN
) >> 6U)) / (tmpreg
);
1620 case I2S_CLOCK_EXTERNAL
:
1622 i2sclocksource
= EXTERNAL_CLOCK_VALUE
;
1627 i2sclocksource
= 0U;
1632 /* the return result is the value of I2S clock */
1633 return i2sclocksource
;
1637 * @brief DMA I2S transmit process complete callback
1638 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1639 * the configuration information for the specified DMA module.
1642 static void I2S_DMATxCplt(DMA_HandleTypeDef
*hdma
)
1644 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
; /* Derogation MISRAC2012-Rule-11.5 */
1646 /* if DMA is configured in DMA_NORMAL Mode */
1647 if (hdma
->Init
.Mode
== DMA_NORMAL
)
1649 /* Disable Tx DMA Request */
1650 CLEAR_BIT(hi2s
->Instance
->CR2
, SPI_CR2_TXDMAEN
);
1652 hi2s
->TxXferCount
= 0U;
1653 hi2s
->State
= HAL_I2S_STATE_READY
;
1655 /* Call user Tx complete callback */
1656 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
1657 hi2s
->TxCpltCallback(hi2s
);
1659 HAL_I2S_TxCpltCallback(hi2s
);
1660 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1664 * @brief DMA I2S transmit process half complete callback
1665 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1666 * the configuration information for the specified DMA module.
1669 static void I2S_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
)
1671 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
; /* Derogation MISRAC2012-Rule-11.5 */
1673 /* Call user Tx half complete callback */
1674 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
1675 hi2s
->TxHalfCpltCallback(hi2s
);
1677 HAL_I2S_TxHalfCpltCallback(hi2s
);
1678 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1682 * @brief DMA I2S receive process complete callback
1683 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1684 * the configuration information for the specified DMA module.
1687 static void I2S_DMARxCplt(DMA_HandleTypeDef
*hdma
)
1689 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
; /* Derogation MISRAC2012-Rule-11.5 */
1691 /* if DMA is configured in DMA_NORMAL Mode */
1692 if (hdma
->Init
.Mode
== DMA_NORMAL
)
1694 /* Disable Rx DMA Request */
1695 CLEAR_BIT(hi2s
->Instance
->CR2
, SPI_CR2_RXDMAEN
);
1696 hi2s
->RxXferCount
= 0U;
1697 hi2s
->State
= HAL_I2S_STATE_READY
;
1699 /* Call user Rx complete callback */
1700 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
1701 hi2s
->RxCpltCallback(hi2s
);
1703 HAL_I2S_RxCpltCallback(hi2s
);
1704 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1708 * @brief DMA I2S receive process half complete callback
1709 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1710 * the configuration information for the specified DMA module.
1713 static void I2S_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
)
1715 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
; /* Derogation MISRAC2012-Rule-11.5 */
1717 /* Call user Rx half complete callback */
1718 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
1719 hi2s
->RxHalfCpltCallback(hi2s
);
1721 HAL_I2S_RxHalfCpltCallback(hi2s
);
1722 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1726 * @brief DMA I2S communication error callback
1727 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1728 * the configuration information for the specified DMA module.
1731 static void I2S_DMAError(DMA_HandleTypeDef
*hdma
)
1733 I2S_HandleTypeDef
*hi2s
= (I2S_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
; /* Derogation MISRAC2012-Rule-11.5 */
1735 /* Disable Rx and Tx DMA Request */
1736 CLEAR_BIT(hi2s
->Instance
->CR2
, (SPI_CR2_RXDMAEN
| SPI_CR2_TXDMAEN
));
1737 hi2s
->TxXferCount
= 0U;
1738 hi2s
->RxXferCount
= 0U;
1740 hi2s
->State
= HAL_I2S_STATE_READY
;
1742 /* Set the error code and execute error callback*/
1743 SET_BIT(hi2s
->ErrorCode
, HAL_I2S_ERROR_DMA
);
1744 /* Call user error callback */
1745 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
1746 hi2s
->ErrorCallback(hi2s
);
1748 HAL_I2S_ErrorCallback(hi2s
);
1749 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1753 * @brief Transmit an amount of data in non-blocking mode with Interrupt
1754 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1755 * the configuration information for I2S module
1758 static void I2S_Transmit_IT(I2S_HandleTypeDef
*hi2s
)
1761 hi2s
->Instance
->DR
= (*hi2s
->pTxBuffPtr
);
1763 hi2s
->TxXferCount
--;
1765 if (hi2s
->TxXferCount
== 0U)
1767 /* Disable TXE and ERR interrupt */
1768 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_TXE
| I2S_IT_ERR
));
1770 hi2s
->State
= HAL_I2S_STATE_READY
;
1771 /* Call user Tx complete callback */
1772 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
1773 hi2s
->TxCpltCallback(hi2s
);
1775 HAL_I2S_TxCpltCallback(hi2s
);
1776 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1781 * @brief Receive an amount of data in non-blocking mode with Interrupt
1782 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1783 * the configuration information for I2S module
1786 static void I2S_Receive_IT(I2S_HandleTypeDef
*hi2s
)
1789 (*hi2s
->pRxBuffPtr
) = (uint16_t)hi2s
->Instance
->DR
;
1791 hi2s
->RxXferCount
--;
1793 if (hi2s
->RxXferCount
== 0U)
1795 /* Disable RXNE and ERR interrupt */
1796 __HAL_I2S_DISABLE_IT(hi2s
, (I2S_IT_RXNE
| I2S_IT_ERR
));
1798 hi2s
->State
= HAL_I2S_STATE_READY
;
1799 /* Call user Rx complete callback */
1800 #if (USE_HAL_I2S_REGISTER_CALLBACKS == 1U)
1801 hi2s
->RxCpltCallback(hi2s
);
1803 HAL_I2S_RxCpltCallback(hi2s
);
1804 #endif /* USE_HAL_I2S_REGISTER_CALLBACKS */
1809 * @brief This function handles I2S Communication Timeout.
1810 * @param hi2s pointer to a I2S_HandleTypeDef structure that contains
1811 * the configuration information for I2S module
1812 * @param Flag Flag checked
1813 * @param State Value of the flag expected
1814 * @param Timeout Duration of the timeout
1815 * @retval HAL status
1817 static HAL_StatusTypeDef
I2S_WaitFlagStateUntilTimeout(I2S_HandleTypeDef
*hi2s
, uint32_t Flag
, FlagStatus State
, uint32_t Timeout
)
1822 tickstart
= HAL_GetTick();
1824 /* Wait until flag is set to status*/
1825 while (((__HAL_I2S_GET_FLAG(hi2s
, Flag
)) ? SET
: RESET
) != State
)
1827 if (Timeout
!= HAL_MAX_DELAY
)
1829 if (((HAL_GetTick() - tickstart
) >= Timeout
) || (Timeout
== 0U))
1831 /* Set the I2S State ready */
1832 hi2s
->State
= HAL_I2S_STATE_READY
;
1834 /* Process Unlocked */
1856 #endif /* HAL_I2S_MODULE_ENABLED */
1858 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/