Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / lib / main / STM32F4 / Drivers / STM32F4xx_HAL_Driver / Src / stm32f4xx_hal_sai.c
blob8f1eced632467836bd94c5a3c2ce3fd69ff730c1
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_hal_sai.c
4 * @author MCD Application Team
5 * @version V1.7.1
6 * @date 14-April-2017
7 * @brief SAI HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Serial Audio Interface (SAI) peripheral:
10 * + Initialization/de-initialization functions
11 * + I/O operation functions
12 * + Peripheral Control functions
13 * + Peripheral State functions
15 @verbatim
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 [..]
21 The SAI HAL driver can be used as follows:
23 (#) Declare a SAI_HandleTypeDef handle structure (eg. SAI_HandleTypeDef hsai).
24 (#) Initialize the SAI low level resources by implementing the HAL_SAI_MspInit() API:
25 (##) Enable the SAI interface clock.
26 (##) SAI pins configuration:
27 (+++) Enable the clock for the SAI GPIOs.
28 (+++) Configure these SAI pins as alternate function pull-up.
29 (##) NVIC configuration if you need to use interrupt process (HAL_SAI_Transmit_IT()
30 and HAL_SAI_Receive_IT() APIs):
31 (+++) Configure the SAI interrupt priority.
32 (+++) Enable the NVIC SAI IRQ handle.
34 (##) DMA Configuration if you need to use DMA process (HAL_SAI_Transmit_DMA()
35 and HAL_SAI_Receive_DMA() APIs):
36 (+++) Declare a DMA handle structure for the Tx/Rx stream.
37 (+++) Enable the DMAx interface clock.
38 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
39 (+++) Configure the DMA Tx/Rx Stream.
40 (+++) Associate the initialized DMA handle to the SAI DMA Tx/Rx handle.
41 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the
42 DMA Tx/Rx Stream.
44 (#) The initialization can be done by two ways
45 (##) Expert mode : Initialize the structures Init, FrameInit and SlotInit and call HAL_SAI_Init().
46 (##) Simplified mode : Initialize the high part of Init Structure and call HAL_SAI_InitProtocol().
48 [..]
49 (@) The specific SAI interrupts (FIFO request and Overrun underrun interrupt)
50 will be managed using the macros __HAL_SAI_ENABLE_IT() and __HAL_SAI_DISABLE_IT()
51 inside the transmit and receive process.
53 [..]
54 (@) SAI Clock Source configuration is managed differently depending on the selected
55 STM32F4 devices :
56 (+@) For STM32F446xx devices, the configuration is managed through RCCEx_PeriphCLKConfig()
57 function in the HAL RCC drivers
58 (+@) For STM32F439xx/STM32F437xx/STM32F429xx/STM32F427xx devices, the configuration
59 is managed within HAL SAI drivers through HAL_SAI_Init() function using
60 ClockSource field of SAI_InitTypeDef structure.
61 [..]
62 (@) Make sure that either:
63 (+@) I2S PLL is configured or
64 (+@) SAI PLL is configured or
65 (+@) External clock source is configured after setting correctly
66 the define constant EXTERNAL_CLOCK_VALUE in the stm32f4xx_hal_conf.h file.
67 [..]
68 (@) In master Tx mode: enabling the audio block immediately generates the bit clock
69 for the external slaves even if there is no data in the FIFO, However FS signal
70 generation is conditioned by the presence of data in the FIFO.
72 [..]
73 (@) In master Rx mode: enabling the audio block immediately generates the bit clock
74 and FS signal for the external slaves.
76 [..]
77 (@) It is mandatory to respect the following conditions in order to avoid bad SAI behavior:
78 (+@) First bit Offset <= (SLOT size - Data size)
79 (+@) Data size <= SLOT size
80 (+@) Number of SLOT x SLOT size = Frame length
81 (+@) The number of slots should be even when SAI_FS_CHANNEL_IDENTIFICATION is selected.
83 [..]
84 Three operation modes are available within this driver :
86 *** Polling mode IO operation ***
87 =================================
88 [..]
89 (+) Send an amount of data in blocking mode using HAL_SAI_Transmit()
90 (+) Receive an amount of data in blocking mode using HAL_SAI_Receive()
92 *** Interrupt mode IO operation ***
93 ===================================
94 [..]
95 (+) Send an amount of data in non-blocking mode using HAL_SAI_Transmit_IT()
96 (+) At transmission end of transfer HAL_SAI_TxCpltCallback() is executed and user can
97 add his own code by customization of function pointer HAL_SAI_TxCpltCallback()
98 (+) Receive an amount of data in non-blocking mode using HAL_SAI_Receive_IT()
99 (+) At reception end of transfer HAL_SAI_RxCpltCallback() is executed and user can
100 add his own code by customization of function pointer HAL_SAI_RxCpltCallback()
101 (+) In case of flag error, HAL_SAI_ErrorCallback() function is executed and user can
102 add his own code by customization of function pointer HAL_SAI_ErrorCallback()
104 *** DMA mode IO operation ***
105 =============================
106 [..]
107 (+) Send an amount of data in non-blocking mode (DMA) using HAL_SAI_Transmit_DMA()
108 (+) At transmission end of transfer HAL_SAI_TxCpltCallback() is executed and user can
109 add his own code by customization of function pointer HAL_SAI_TxCpltCallback()
110 (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SAI_Receive_DMA()
111 (+) At reception end of transfer HAL_SAI_RxCpltCallback() is executed and user can
112 add his own code by customization of function pointer HAL_SAI_RxCpltCallback()
113 (+) In case of flag error, HAL_SAI_ErrorCallback() function is executed and user can
114 add his own code by customization of function pointer HAL_SAI_ErrorCallback()
115 (+) Pause the DMA Transfer using HAL_SAI_DMAPause()
116 (+) Resume the DMA Transfer using HAL_SAI_DMAResume()
117 (+) Stop the DMA Transfer using HAL_SAI_DMAStop()
119 *** SAI HAL driver additional function list ***
120 ===============================================
121 [..]
122 Below the list the others API available SAI HAL driver :
124 (+) HAL_SAI_EnableTxMuteMode(): Enable the mute in tx mode
125 (+) HAL_SAI_DisableTxMuteMode(): Disable the mute in tx mode
126 (+) HAL_SAI_EnableRxMuteMode(): Enable the mute in Rx mode
127 (+) HAL_SAI_DisableRxMuteMode(): Disable the mute in Rx mode
128 (+) HAL_SAI_FlushRxFifo(): Flush the rx fifo.
129 (+) HAL_SAI_Abort(): Abort the current transfer
131 *** SAI HAL driver macros list ***
132 ==================================
133 [..]
134 Below the list of most used macros in SAI HAL driver :
136 (+) __HAL_SAI_ENABLE(): Enable the SAI peripheral
137 (+) __HAL_SAI_DISABLE(): Disable the SAI peripheral
138 (+) __HAL_SAI_ENABLE_IT(): Enable the specified SAI interrupts
139 (+) __HAL_SAI_DISABLE_IT(): Disable the specified SAI interrupts
140 (+) __HAL_SAI_GET_IT_SOURCE(): Check if the specified SAI interrupt source is
141 enabled or disabled
142 (+) __HAL_SAI_GET_FLAG(): Check whether the specified SAI flag is set or not
144 @endverbatim
145 ******************************************************************************
146 * @attention
148 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
150 * Redistribution and use in source and binary forms, with or without modification,
151 * are permitted provided that the following conditions are met:
152 * 1. Redistributions of source code must retain the above copyright notice,
153 * this list of conditions and the following disclaimer.
154 * 2. Redistributions in binary form must reproduce the above copyright notice,
155 * this list of conditions and the following disclaimer in the documentation
156 * and/or other materials provided with the distribution.
157 * 3. Neither the name of STMicroelectronics nor the names of its contributors
158 * may be used to endorse or promote products derived from this software
159 * without specific prior written permission.
161 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
162 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
163 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
164 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
165 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
166 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
167 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
168 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
169 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
170 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
172 ******************************************************************************
175 /* Includes ------------------------------------------------------------------*/
176 #include "stm32f4xx_hal.h"
178 /** @addtogroup STM32F4xx_HAL_Driver
179 * @{
182 /** @defgroup SAI SAI
183 * @brief SAI HAL module driver
184 * @{
187 #ifdef HAL_SAI_MODULE_ENABLED
189 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
190 defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) ||\
191 defined(STM32F423xx)
193 /** @defgroup SAI_Private_Typedefs SAI Private Typedefs
194 * @{
196 typedef enum {
197 SAI_MODE_DMA,
198 SAI_MODE_IT
199 }SAI_ModeTypedef;
201 * @}
204 /* Private define ------------------------------------------------------------*/
206 /** @defgroup SAI_Private_Constants SAI Private Constants
207 * @{
209 #define SAI_FIFO_SIZE 8U
210 #define SAI_DEFAULT_TIMEOUT 4U /* 4ms */
211 #define SAI_xCR2_MUTECNT_OFFSET POSITION_VAL(SAI_xCR2_MUTECNT)
213 * @}
216 /* Private macro -------------------------------------------------------------*/
217 /* Private variables ---------------------------------------------------------*/
218 /* Private function prototypes -----------------------------------------------*/
220 /** @defgroup SAI_Private_Functions SAI Private Functions
221 * @{
223 static void SAI_FillFifo(SAI_HandleTypeDef *hsai);
224 static uint32_t SAI_InterruptFlag(SAI_HandleTypeDef *hsai, uint32_t mode);
225 static HAL_StatusTypeDef SAI_InitI2S(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot);
226 static HAL_StatusTypeDef SAI_InitPCM(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot);
228 static HAL_StatusTypeDef SAI_Disable(SAI_HandleTypeDef *hsai);
229 static void SAI_Transmit_IT8Bit(SAI_HandleTypeDef *hsai);
230 static void SAI_Transmit_IT16Bit(SAI_HandleTypeDef *hsai);
231 static void SAI_Transmit_IT32Bit(SAI_HandleTypeDef *hsai);
232 static void SAI_Receive_IT8Bit(SAI_HandleTypeDef *hsai);
233 static void SAI_Receive_IT16Bit(SAI_HandleTypeDef *hsai);
234 static void SAI_Receive_IT32Bit(SAI_HandleTypeDef *hsai);
236 static void SAI_DMATxCplt(DMA_HandleTypeDef *hdma);
237 static void SAI_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
238 static void SAI_DMARxCplt(DMA_HandleTypeDef *hdma);
239 static void SAI_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
240 static void SAI_DMAError(DMA_HandleTypeDef *hdma);
241 static void SAI_DMAAbort(DMA_HandleTypeDef *hdma);
243 * @}
246 /* Exported functions ---------------------------------------------------------*/
248 /** @defgroup SAI_Exported_Functions SAI Exported Functions
249 * @{
252 /** @defgroup SAI_Exported_Functions_Group1 Initialization and de-initialization functions
253 * @brief Initialization and Configuration functions
255 @verbatim
256 ===============================================================================
257 ##### Initialization and de-initialization functions #####
258 ===============================================================================
259 [..] This subsection provides a set of functions allowing to initialize and
260 de-initialize the SAIx peripheral:
262 (+) User must implement HAL_SAI_MspInit() function in which he configures
263 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
265 (+) Call the function HAL_SAI_Init() to configure the selected device with
266 the selected configuration:
267 (++) Mode (Master/slave TX/RX)
268 (++) Protocol
269 (++) Data Size
270 (++) MCLK Output
271 (++) Audio frequency
272 (++) FIFO Threshold
273 (++) Frame Config
274 (++) Slot Config
276 (+) Call the function HAL_SAI_DeInit() to restore the default configuration
277 of the selected SAI peripheral.
279 @endverbatim
280 * @{
284 * @brief Initialize the structure FrameInit, SlotInit and the low part of
285 * Init according to the specified parameters and call the function
286 * HAL_SAI_Init to initialize the SAI block.
287 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
288 * the configuration information for SAI module.
289 * @param protocol: one of the supported protocol @ref SAI_Protocol
290 * @param datasize: one of the supported datasize @ref SAI_Protocol_DataSize
291 * the configuration information for SAI module.
292 * @param nbslot: Number of slot.
293 * @retval HAL status
295 HAL_StatusTypeDef HAL_SAI_InitProtocol(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
297 HAL_StatusTypeDef status = HAL_OK;
299 /* Check the parameters */
300 assert_param(IS_SAI_SUPPORTED_PROTOCOL(protocol));
301 assert_param(IS_SAI_PROTOCOL_DATASIZE(datasize));
303 switch(protocol)
305 case SAI_I2S_STANDARD :
306 case SAI_I2S_MSBJUSTIFIED :
307 case SAI_I2S_LSBJUSTIFIED :
308 status = SAI_InitI2S(hsai, protocol, datasize, nbslot);
309 break;
310 case SAI_PCM_LONG :
311 case SAI_PCM_SHORT :
312 status = SAI_InitPCM(hsai, protocol, datasize, nbslot);
313 break;
314 default :
315 status = HAL_ERROR;
316 break;
319 if(status == HAL_OK)
321 status = HAL_SAI_Init(hsai);
324 return status;
328 * @brief Initialize the SAI according to the specified parameters.
329 * in the SAI_InitTypeDef structure and initialize the associated handle.
330 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
331 * the configuration information for SAI module.
332 * @retval HAL status
334 HAL_StatusTypeDef HAL_SAI_Init(SAI_HandleTypeDef *hsai)
336 uint32_t tmpregisterGCR = 0U;
338 /* This variable used to store the SAI_CK_x (value in Hz) */
339 uint32_t freq = 0U;
341 /* This variable is used to compute CKSTR bits of SAI CR1 according to
342 ClockStrobing and AudioMode fields */
343 uint32_t ckstr_bits = 0U;
344 uint32_t syncen_bits = 0U;
346 /* Check the SAI handle allocation */
347 if(hsai == NULL)
349 return HAL_ERROR;
352 /* check the instance */
353 assert_param(IS_SAI_ALL_INSTANCE(hsai->Instance));
355 /* Check the SAI Block parameters */
356 assert_param(IS_SAI_AUDIO_FREQUENCY(hsai->Init.AudioFrequency));
357 assert_param(IS_SAI_BLOCK_PROTOCOL(hsai->Init.Protocol));
358 assert_param(IS_SAI_BLOCK_MODE(hsai->Init.AudioMode));
359 assert_param(IS_SAI_BLOCK_SYNCEXT(hsai->Init.SynchroExt));
360 assert_param(IS_SAI_BLOCK_DATASIZE(hsai->Init.DataSize));
361 assert_param(IS_SAI_BLOCK_FIRST_BIT(hsai->Init.FirstBit));
362 assert_param(IS_SAI_BLOCK_CLOCK_STROBING(hsai->Init.ClockStrobing));
363 assert_param(IS_SAI_BLOCK_SYNCHRO(hsai->Init.Synchro));
364 assert_param(IS_SAI_BLOCK_OUTPUT_DRIVE(hsai->Init.OutputDrive));
365 assert_param(IS_SAI_BLOCK_NODIVIDER(hsai->Init.NoDivider));
366 assert_param(IS_SAI_BLOCK_FIFO_THRESHOLD(hsai->Init.FIFOThreshold));
367 assert_param(IS_SAI_MONO_STEREO_MODE(hsai->Init.MonoStereoMode));
368 assert_param(IS_SAI_BLOCK_COMPANDING_MODE(hsai->Init.CompandingMode));
369 assert_param(IS_SAI_BLOCK_TRISTATE_MANAGEMENT(hsai->Init.TriState));
371 /* Check the SAI Block Frame parameters */
372 assert_param(IS_SAI_BLOCK_FRAME_LENGTH(hsai->FrameInit.FrameLength));
373 assert_param(IS_SAI_BLOCK_ACTIVE_FRAME(hsai->FrameInit.ActiveFrameLength));
374 assert_param(IS_SAI_BLOCK_FS_DEFINITION(hsai->FrameInit.FSDefinition));
375 assert_param(IS_SAI_BLOCK_FS_POLARITY(hsai->FrameInit.FSPolarity));
376 assert_param(IS_SAI_BLOCK_FS_OFFSET(hsai->FrameInit.FSOffset));
378 /* Check the SAI Block Slot parameters */
379 assert_param(IS_SAI_BLOCK_FIRSTBIT_OFFSET(hsai->SlotInit.FirstBitOffset));
380 assert_param(IS_SAI_BLOCK_SLOT_SIZE(hsai->SlotInit.SlotSize));
381 assert_param(IS_SAI_BLOCK_SLOT_NUMBER(hsai->SlotInit.SlotNumber));
382 assert_param(IS_SAI_SLOT_ACTIVE(hsai->SlotInit.SlotActive));
384 if(hsai->State == HAL_SAI_STATE_RESET)
386 /* Allocate lock resource and initialize it */
387 hsai->Lock = HAL_UNLOCKED;
389 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
390 HAL_SAI_MspInit(hsai);
393 hsai->State = HAL_SAI_STATE_BUSY;
395 /* Disable the selected SAI peripheral */
396 SAI_Disable(hsai);
398 /* SAI Block Synchro Configuration -----------------------------------------*/
399 SAI_BlockSynchroConfig(hsai);
401 /* Configure Master Clock using the following formula :
402 MCLK_x = SAI_CK_x / (MCKDIV[3:0] * 2) with MCLK_x = 256 * FS
403 FS = SAI_CK_x / (MCKDIV[3:0] * 2) * 256
404 MCKDIV[3:0] = SAI_CK_x / FS * 512 */
405 if(hsai->Init.AudioFrequency != SAI_AUDIO_FREQUENCY_MCKDIV)
407 /* Get SAI clock source based on Source clock selection from RCC */
408 freq = SAI_GetInputClock(hsai);
410 /* (saiclocksource x 10) to keep Significant digits */
411 tmpregisterGCR = (((freq * 10U) / ((hsai->Init.AudioFrequency) * 512U)));
413 hsai->Init.Mckdiv = tmpregisterGCR / 10U;
415 /* Round result to the nearest integer */
416 if((tmpregisterGCR % 10U) > 8U)
418 hsai->Init.Mckdiv+= 1U;
422 /* Compute CKSTR bits of SAI CR1 according to ClockStrobing and AudioMode */
423 if((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
425 ckstr_bits = (hsai->Init.ClockStrobing == SAI_CLOCKSTROBING_RISINGEDGE) ? 0U: SAI_xCR1_CKSTR;
427 else
429 ckstr_bits = (hsai->Init.ClockStrobing == SAI_CLOCKSTROBING_RISINGEDGE) ? SAI_xCR1_CKSTR: 0U;
432 /* SAI Block Configuration -------------------------------------------------*/
433 switch(hsai->Init.Synchro)
435 case SAI_ASYNCHRONOUS :
437 syncen_bits = 0U;
439 break;
440 case SAI_SYNCHRONOUS :
442 syncen_bits = SAI_xCR1_SYNCEN_0;
444 break;
445 case SAI_SYNCHRONOUS_EXT_SAI1 :
446 case SAI_SYNCHRONOUS_EXT_SAI2 :
448 syncen_bits = SAI_xCR1_SYNCEN_1;
450 break;
451 default:
452 break;
454 /* SAI CR1 Configuration */
455 hsai->Instance->CR1 &= ~(SAI_xCR1_MODE | SAI_xCR1_PRTCFG | SAI_xCR1_DS | \
456 SAI_xCR1_LSBFIRST | SAI_xCR1_CKSTR | SAI_xCR1_SYNCEN |\
457 SAI_xCR1_MONO | SAI_xCR1_OUTDRIV | SAI_xCR1_DMAEN | \
458 SAI_xCR1_NODIV | SAI_xCR1_MCKDIV);
460 hsai->Instance->CR1 |= (hsai->Init.AudioMode | hsai->Init.Protocol | \
461 hsai->Init.DataSize | hsai->Init.FirstBit | \
462 ckstr_bits | syncen_bits | \
463 hsai->Init.MonoStereoMode | hsai->Init.OutputDrive | \
464 hsai->Init.NoDivider | (hsai->Init.Mckdiv << 20U));
466 /* SAI CR2 Configuration */
467 hsai->Instance->CR2 &= ~(SAI_xCR2_FTH | SAI_xCR2_FFLUSH | SAI_xCR2_COMP | SAI_xCR2_CPL);
468 hsai->Instance->CR2 |= (hsai->Init.FIFOThreshold | hsai->Init.CompandingMode | hsai->Init.TriState);
470 /* SAI Frame Configuration -----------------------------------------*/
471 hsai->Instance->FRCR&=(~(SAI_xFRCR_FRL | SAI_xFRCR_FSALL | SAI_xFRCR_FSDEF | \
472 SAI_xFRCR_FSPOL | SAI_xFRCR_FSOFF));
473 hsai->Instance->FRCR|=((hsai->FrameInit.FrameLength - 1U) |
474 hsai->FrameInit.FSOffset |
475 hsai->FrameInit.FSDefinition |
476 hsai->FrameInit.FSPolarity |
477 ((hsai->FrameInit.ActiveFrameLength - 1U) << 8U));
479 /* SAI Block_x SLOT Configuration ------------------------------------------*/
480 /* This register has no meaning in AC 97 and SPDIF audio protocol */
481 hsai->Instance->SLOTR &= ~(SAI_xSLOTR_FBOFF | SAI_xSLOTR_SLOTSZ | \
482 SAI_xSLOTR_NBSLOT | SAI_xSLOTR_SLOTEN );
484 hsai->Instance->SLOTR |= hsai->SlotInit.FirstBitOffset | hsai->SlotInit.SlotSize | \
485 (hsai->SlotInit.SlotActive << 16U) | ((hsai->SlotInit.SlotNumber - 1U) << 8U);
487 /* Initialize the error code */
488 hsai->ErrorCode = HAL_SAI_ERROR_NONE;
490 /* Initialize the SAI state */
491 hsai->State= HAL_SAI_STATE_READY;
493 /* Release Lock */
494 __HAL_UNLOCK(hsai);
496 return HAL_OK;
500 * @brief DeInitialize the SAI peripheral.
501 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
502 * the configuration information for SAI module.
503 * @retval HAL status
505 HAL_StatusTypeDef HAL_SAI_DeInit(SAI_HandleTypeDef *hsai)
507 /* Check the SAI handle allocation */
508 if(hsai == NULL)
510 return HAL_ERROR;
513 hsai->State = HAL_SAI_STATE_BUSY;
515 /* Disabled All interrupt and clear all the flag */
516 hsai->Instance->IMR = 0U;
517 hsai->Instance->CLRFR = 0xFFFFFFFFU;
519 /* Disable the SAI */
520 SAI_Disable(hsai);
522 /* Flush the fifo */
523 SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
525 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
526 HAL_SAI_MspDeInit(hsai);
528 /* Initialize the error code */
529 hsai->ErrorCode = HAL_SAI_ERROR_NONE;
531 /* Initialize the SAI state */
532 hsai->State = HAL_SAI_STATE_RESET;
534 /* Release Lock */
535 __HAL_UNLOCK(hsai);
537 return HAL_OK;
541 * @brief Initialize the SAI MSP.
542 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
543 * the configuration information for SAI module.
544 * @retval None
546 __weak void HAL_SAI_MspInit(SAI_HandleTypeDef *hsai)
548 /* Prevent unused argument(s) compilation warning */
549 UNUSED(hsai);
551 /* NOTE : This function should not be modified, when the callback is needed,
552 the HAL_SAI_MspInit could be implemented in the user file
557 * @brief DeInitialize the SAI MSP.
558 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
559 * the configuration information for SAI module.
560 * @retval None
562 __weak void HAL_SAI_MspDeInit(SAI_HandleTypeDef *hsai)
564 /* Prevent unused argument(s) compilation warning */
565 UNUSED(hsai);
567 /* NOTE : This function should not be modified, when the callback is needed,
568 the HAL_SAI_MspDeInit could be implemented in the user file
573 * @}
576 /** @defgroup SAI_Exported_Functions_Group2 IO operation functions
577 * @brief Data transfers functions
579 @verbatim
580 ==============================================================================
581 ##### IO operation functions #####
582 ==============================================================================
583 [..]
584 This subsection provides a set of functions allowing to manage the SAI data
585 transfers.
587 (+) There are two modes of transfer:
588 (++) Blocking mode : The communication is performed in the polling mode.
589 The status of all data processing is returned by the same function
590 after finishing transfer.
591 (++) No-Blocking mode : The communication is performed using Interrupts
592 or DMA. These functions return the status of the transfer startup.
593 The end of the data processing will be indicated through the
594 dedicated SAI IRQ when using Interrupt mode or the DMA IRQ when
595 using DMA mode.
597 (+) Blocking mode functions are :
598 (++) HAL_SAI_Transmit()
599 (++) HAL_SAI_Receive()
600 (++) HAL_SAI_TransmitReceive()
602 (+) Non Blocking mode functions with Interrupt are :
603 (++) HAL_SAI_Transmit_IT()
604 (++) HAL_SAI_Receive_IT()
605 (++) HAL_SAI_TransmitReceive_IT()
607 (+) Non Blocking mode functions with DMA are :
608 (++) HAL_SAI_Transmit_DMA()
609 (++) HAL_SAI_Receive_DMA()
610 (++) HAL_SAI_TransmitReceive_DMA()
612 (+) A set of Transfer Complete Callbacks are provided in non Blocking mode:
613 (++) HAL_SAI_TxCpltCallback()
614 (++) HAL_SAI_RxCpltCallback()
615 (++) HAL_SAI_ErrorCallback()
617 @endverbatim
618 * @{
622 * @brief Transmit an amount of data in blocking mode.
623 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
624 * the configuration information for SAI module.
625 * @param pData: Pointer to data buffer
626 * @param Size: Amount of data to be sent
627 * @param Timeout: Timeout duration
628 * @retval HAL status
630 HAL_StatusTypeDef HAL_SAI_Transmit(SAI_HandleTypeDef *hsai, uint8_t* pData, uint16_t Size, uint32_t Timeout)
632 uint32_t tickstart = HAL_GetTick();
634 if((pData == NULL ) || (Size == 0))
636 return HAL_ERROR;
639 if(hsai->State == HAL_SAI_STATE_READY)
641 /* Process Locked */
642 __HAL_LOCK(hsai);
644 hsai->XferSize = Size;
645 hsai->XferCount = Size;
646 hsai->pBuffPtr = pData;
647 hsai->State = HAL_SAI_STATE_BUSY_TX;
648 hsai->ErrorCode = HAL_SAI_ERROR_NONE;
650 /* Check if the SAI is already enabled */
651 if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
653 /* fill the fifo with data before to enabled the SAI */
654 SAI_FillFifo(hsai);
655 /* Enable SAI peripheral */
656 __HAL_SAI_ENABLE(hsai);
659 while(hsai->XferCount > 0U)
661 /* Write data if the FIFO is not full */
662 if((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_FULL)
664 if((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
666 hsai->Instance->DR = (*hsai->pBuffPtr++);
668 else if(hsai->Init.DataSize <= SAI_DATASIZE_16)
670 hsai->Instance->DR = *((uint16_t *)hsai->pBuffPtr);
671 hsai->pBuffPtr+= 2U;
673 else
675 hsai->Instance->DR = *((uint32_t *)hsai->pBuffPtr);
676 hsai->pBuffPtr+= 4U;
678 hsai->XferCount--;
680 else
682 /* Check for the Timeout */
683 if((Timeout != HAL_MAX_DELAY) && ((Timeout == 0U)||((HAL_GetTick() - tickstart) > Timeout)))
685 /* Update error code */
686 hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
688 /* Clear all the flags */
689 hsai->Instance->CLRFR = 0xFFFFFFFFU;
691 /* Disable SAI peripheral */
692 SAI_Disable(hsai);
694 /* Flush the fifo */
695 SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
697 /* Change the SAI state */
698 hsai->State = HAL_SAI_STATE_READY;
700 /* Process Unlocked */
701 __HAL_UNLOCK(hsai);
703 return HAL_ERROR;
708 hsai->State = HAL_SAI_STATE_READY;
710 /* Process Unlocked */
711 __HAL_UNLOCK(hsai);
713 return HAL_OK;
715 else
717 return HAL_BUSY;
722 * @brief Receive an amount of data in blocking mode.
723 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
724 * the configuration information for SAI module.
725 * @param pData: Pointer to data buffer
726 * @param Size: Amount of data to be received
727 * @param Timeout: Timeout duration
728 * @retval HAL status
730 HAL_StatusTypeDef HAL_SAI_Receive(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size, uint32_t Timeout)
732 uint32_t tickstart = HAL_GetTick();
734 if((pData == NULL ) || (Size == 0))
736 return HAL_ERROR;
739 if(hsai->State == HAL_SAI_STATE_READY)
741 /* Process Locked */
742 __HAL_LOCK(hsai);
744 hsai->pBuffPtr = pData;
745 hsai->XferSize = Size;
746 hsai->XferCount = Size;
747 hsai->State = HAL_SAI_STATE_BUSY_RX;
748 hsai->ErrorCode = HAL_SAI_ERROR_NONE;
750 /* Check if the SAI is already enabled */
751 if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
753 /* Enable SAI peripheral */
754 __HAL_SAI_ENABLE(hsai);
757 /* Receive data */
758 while(hsai->XferCount > 0U)
760 if((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_EMPTY)
762 if((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
764 (*hsai->pBuffPtr++) = hsai->Instance->DR;
766 else if(hsai->Init.DataSize <= SAI_DATASIZE_16)
768 *((uint16_t*)hsai->pBuffPtr) = hsai->Instance->DR;
769 hsai->pBuffPtr+= 2U;
771 else
773 *((uint32_t*)hsai->pBuffPtr) = hsai->Instance->DR;
774 hsai->pBuffPtr+= 4U;
776 hsai->XferCount--;
778 else
780 /* Check for the Timeout */
781 if((Timeout != HAL_MAX_DELAY) && ((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)))
783 /* Update error code */
784 hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
786 /* Clear all the flags */
787 hsai->Instance->CLRFR = 0xFFFFFFFFU;
789 /* Disable SAI peripheral */
790 SAI_Disable(hsai);
792 /* Flush the fifo */
793 SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
795 /* Change the SAI state */
796 hsai->State = HAL_SAI_STATE_READY;
798 /* Process Unlocked */
799 __HAL_UNLOCK(hsai);
801 return HAL_ERROR;
806 hsai->State = HAL_SAI_STATE_READY;
808 /* Process Unlocked */
809 __HAL_UNLOCK(hsai);
811 return HAL_OK;
813 else
815 return HAL_BUSY;
820 * @brief Transmit an amount of data in non-blocking mode with Interrupt.
821 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
822 * the configuration information for SAI module.
823 * @param pData: Pointer to data buffer
824 * @param Size: Amount of data to be sent
825 * @retval HAL status
827 HAL_StatusTypeDef HAL_SAI_Transmit_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
829 if((pData == NULL) || (Size == 0))
831 return HAL_ERROR;
834 if(hsai->State == HAL_SAI_STATE_READY)
836 /* Process Locked */
837 __HAL_LOCK(hsai);
839 hsai->pBuffPtr = pData;
840 hsai->XferSize = Size;
841 hsai->XferCount = Size;
842 hsai->ErrorCode = HAL_SAI_ERROR_NONE;
843 hsai->State = HAL_SAI_STATE_BUSY_TX;
845 if((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
847 hsai->InterruptServiceRoutine = SAI_Transmit_IT8Bit;
849 else if(hsai->Init.DataSize <= SAI_DATASIZE_16)
851 hsai->InterruptServiceRoutine = SAI_Transmit_IT16Bit;
853 else
855 hsai->InterruptServiceRoutine = SAI_Transmit_IT32Bit;
858 /* Fill the fifo before starting the communication */
859 SAI_FillFifo(hsai);
861 /* Enable FRQ and OVRUDR interrupts */
862 __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
864 /* Check if the SAI is already enabled */
865 if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
867 /* Enable SAI peripheral */
868 __HAL_SAI_ENABLE(hsai);
870 /* Process Unlocked */
871 __HAL_UNLOCK(hsai);
873 return HAL_OK;
875 else
877 return HAL_BUSY;
882 * @brief Receive an amount of data in non-blocking mode with Interrupt.
883 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
884 * the configuration information for SAI module.
885 * @param pData: Pointer to data buffer
886 * @param Size: Amount of data to be received
887 * @retval HAL status
889 HAL_StatusTypeDef HAL_SAI_Receive_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
891 if((pData == NULL) || (Size == 0))
893 return HAL_ERROR;
896 if(hsai->State == HAL_SAI_STATE_READY)
898 /* Process Locked */
899 __HAL_LOCK(hsai);
901 hsai->pBuffPtr = pData;
902 hsai->XferSize = Size;
903 hsai->XferCount = Size;
904 hsai->ErrorCode = HAL_SAI_ERROR_NONE;
905 hsai->State = HAL_SAI_STATE_BUSY_RX;
907 if((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
909 hsai->InterruptServiceRoutine = SAI_Receive_IT8Bit;
911 else if(hsai->Init.DataSize <= SAI_DATASIZE_16)
913 hsai->InterruptServiceRoutine = SAI_Receive_IT16Bit;
915 else
917 hsai->InterruptServiceRoutine = SAI_Receive_IT32Bit;
920 /* Enable TXE and OVRUDR interrupts */
921 __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
923 /* Check if the SAI is already enabled */
924 if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
926 /* Enable SAI peripheral */
927 __HAL_SAI_ENABLE(hsai);
930 /* Process Unlocked */
931 __HAL_UNLOCK(hsai);
933 return HAL_OK;
935 else
937 return HAL_BUSY;
942 * @brief Pause the audio stream playing from the Media.
943 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
944 * the configuration information for SAI module.
945 * @retval HAL status
947 HAL_StatusTypeDef HAL_SAI_DMAPause(SAI_HandleTypeDef *hsai)
949 /* Process Locked */
950 __HAL_LOCK(hsai);
952 /* Pause the audio file playing by disabling the SAI DMA requests */
953 hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
955 /* Process Unlocked */
956 __HAL_UNLOCK(hsai);
958 return HAL_OK;
962 * @brief Resume the audio stream playing from the Media.
963 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
964 * the configuration information for SAI module.
965 * @retval HAL status
967 HAL_StatusTypeDef HAL_SAI_DMAResume(SAI_HandleTypeDef *hsai)
969 /* Process Locked */
970 __HAL_LOCK(hsai);
972 /* Enable the SAI DMA requests */
973 hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
975 /* If the SAI peripheral is still not enabled, enable it */
976 if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
978 /* Enable SAI peripheral */
979 __HAL_SAI_ENABLE(hsai);
982 /* Process Unlocked */
983 __HAL_UNLOCK(hsai);
985 return HAL_OK;
989 * @brief Stop the audio stream playing from the Media.
990 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
991 * the configuration information for SAI module.
992 * @retval HAL status
994 HAL_StatusTypeDef HAL_SAI_DMAStop(SAI_HandleTypeDef *hsai)
996 /* Process Locked */
997 __HAL_LOCK(hsai);
999 /* Disable the SAI DMA request */
1000 hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
1002 /* Abort the SAI DMA Streams */
1003 if(hsai->hdmatx != NULL)
1005 if(HAL_DMA_Abort(hsai->hdmatx) != HAL_OK)
1007 return HAL_ERROR;
1011 if(hsai->hdmarx != NULL)
1013 if(HAL_DMA_Abort(hsai->hdmarx) != HAL_OK)
1015 return HAL_ERROR;
1019 /* Disable SAI peripheral */
1020 SAI_Disable(hsai);
1022 hsai->State = HAL_SAI_STATE_READY;
1024 /* Process Unlocked */
1025 __HAL_UNLOCK(hsai);
1027 return HAL_OK;
1031 * @brief Abort the current transfer and disable the SAI.
1032 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1033 * the configuration information for SAI module.
1034 * @retval HAL status
1036 HAL_StatusTypeDef HAL_SAI_Abort(SAI_HandleTypeDef *hsai)
1038 /* Process Locked */
1039 __HAL_LOCK(hsai);
1041 /* Check SAI DMA is enabled or not */
1042 if((hsai->Instance->CR1 & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
1044 /* Disable the SAI DMA request */
1045 hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
1047 /* Abort the SAI DMA Streams */
1048 if(hsai->hdmatx != NULL)
1050 if(HAL_DMA_Abort(hsai->hdmatx) != HAL_OK)
1052 return HAL_ERROR;
1056 if(hsai->hdmarx != NULL)
1058 if(HAL_DMA_Abort(hsai->hdmarx) != HAL_OK)
1060 return HAL_ERROR;
1065 /* Disabled All interrupt and clear all the flag */
1066 hsai->Instance->IMR = 0U;
1067 hsai->Instance->CLRFR = 0xFFFFFFFFU;
1069 /* Disable SAI peripheral */
1070 SAI_Disable(hsai);
1072 /* Flush the fifo */
1073 SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
1075 hsai->State = HAL_SAI_STATE_READY;
1077 /* Process Unlocked */
1078 __HAL_UNLOCK(hsai);
1080 return HAL_OK;
1084 * @brief Transmit an amount of data in non-blocking mode with DMA.
1085 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1086 * the configuration information for SAI module.
1087 * @param pData: Pointer to data buffer
1088 * @param Size: Amount of data to be sent
1089 * @retval HAL status
1091 HAL_StatusTypeDef HAL_SAI_Transmit_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
1093 if((pData == NULL) || (Size == 0))
1095 return HAL_ERROR;
1098 if(hsai->State == HAL_SAI_STATE_READY)
1100 /* Process Locked */
1101 __HAL_LOCK(hsai);
1103 hsai->pBuffPtr = pData;
1104 hsai->XferSize = Size;
1105 hsai->XferCount = Size;
1106 hsai->ErrorCode = HAL_SAI_ERROR_NONE;
1107 hsai->State = HAL_SAI_STATE_BUSY_TX;
1109 /* Set the SAI Tx DMA Half transfer complete callback */
1110 hsai->hdmatx->XferHalfCpltCallback = SAI_DMATxHalfCplt;
1112 /* Set the SAI TxDMA transfer complete callback */
1113 hsai->hdmatx->XferCpltCallback = SAI_DMATxCplt;
1115 /* Set the DMA error callback */
1116 hsai->hdmatx->XferErrorCallback = SAI_DMAError;
1118 /* Set the DMA Tx abort callback */
1119 hsai->hdmatx->XferAbortCallback = NULL;
1121 /* Enable the Tx DMA Stream */
1122 if(HAL_DMA_Start_IT(hsai->hdmatx, (uint32_t)hsai->pBuffPtr, (uint32_t)&hsai->Instance->DR, hsai->XferSize) != HAL_OK)
1124 __HAL_UNLOCK(hsai);
1125 return HAL_ERROR;
1128 /* Check if the SAI is already enabled */
1129 if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
1131 /* Enable SAI peripheral */
1132 __HAL_SAI_ENABLE(hsai);
1135 /* Enable the interrupts for error handling */
1136 __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
1138 /* Enable SAI Tx DMA Request */
1139 hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
1141 /* Process Unlocked */
1142 __HAL_UNLOCK(hsai);
1144 return HAL_OK;
1146 else
1148 return HAL_BUSY;
1153 * @brief Receive an amount of data in non-blocking mode with DMA.
1154 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1155 * the configuration information for SAI module.
1156 * @param pData: Pointer to data buffer
1157 * @param Size: Amount of data to be received
1158 * @retval HAL status
1160 HAL_StatusTypeDef HAL_SAI_Receive_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
1162 if((pData == NULL) || (Size == 0))
1164 return HAL_ERROR;
1167 if(hsai->State == HAL_SAI_STATE_READY)
1169 /* Process Locked */
1170 __HAL_LOCK(hsai);
1172 hsai->pBuffPtr = pData;
1173 hsai->XferSize = Size;
1174 hsai->XferCount = Size;
1175 hsai->ErrorCode = HAL_SAI_ERROR_NONE;
1176 hsai->State = HAL_SAI_STATE_BUSY_RX;
1178 /* Set the SAI Rx DMA Half transfer complete callback */
1179 hsai->hdmarx->XferHalfCpltCallback = SAI_DMARxHalfCplt;
1181 /* Set the SAI Rx DMA transfer complete callback */
1182 hsai->hdmarx->XferCpltCallback = SAI_DMARxCplt;
1184 /* Set the DMA error callback */
1185 hsai->hdmarx->XferErrorCallback = SAI_DMAError;
1187 /* Set the DMA Rx abort callback */
1188 hsai->hdmarx->XferAbortCallback = NULL;
1190 /* Enable the Rx DMA Stream */
1191 if(HAL_DMA_Start_IT(hsai->hdmarx, (uint32_t)&hsai->Instance->DR, (uint32_t)hsai->pBuffPtr, hsai->XferSize) != HAL_OK)
1193 __HAL_UNLOCK(hsai);
1194 return HAL_ERROR;
1197 /* Check if the SAI is already enabled */
1198 if((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
1200 /* Enable SAI peripheral */
1201 __HAL_SAI_ENABLE(hsai);
1204 /* Enable the interrupts for error handling */
1205 __HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
1207 /* Enable SAI Rx DMA Request */
1208 hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
1210 /* Process Unlocked */
1211 __HAL_UNLOCK(hsai);
1213 return HAL_OK;
1215 else
1217 return HAL_BUSY;
1222 * @brief Enable the Tx mute mode.
1223 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1224 * the configuration information for SAI module.
1225 * @param val: value sent during the mute @ref SAI_Block_Mute_Value
1226 * @retval HAL status
1228 HAL_StatusTypeDef HAL_SAI_EnableTxMuteMode(SAI_HandleTypeDef *hsai, uint16_t val)
1230 assert_param(IS_SAI_BLOCK_MUTE_VALUE(val));
1232 if(hsai->State != HAL_SAI_STATE_RESET)
1234 CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTEVAL | SAI_xCR2_MUTE);
1235 SET_BIT(hsai->Instance->CR2, SAI_xCR2_MUTE | val);
1236 return HAL_OK;
1238 return HAL_ERROR;
1242 * @brief Disable the Tx mute mode.
1243 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1244 * the configuration information for SAI module.
1245 * @retval HAL status
1247 HAL_StatusTypeDef HAL_SAI_DisableTxMuteMode(SAI_HandleTypeDef *hsai)
1249 if(hsai->State != HAL_SAI_STATE_RESET)
1251 CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTEVAL | SAI_xCR2_MUTE);
1252 return HAL_OK;
1254 return HAL_ERROR;
1258 * @brief Enable the Rx mute detection.
1259 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1260 * the configuration information for SAI module.
1261 * @param callback: function called when the mute is detected.
1262 * @param counter: number a data before mute detection max 63.
1263 * @retval HAL status
1265 HAL_StatusTypeDef HAL_SAI_EnableRxMuteMode(SAI_HandleTypeDef *hsai, SAIcallback callback, uint16_t counter)
1267 assert_param(IS_SAI_BLOCK_MUTE_COUNTER(counter));
1269 if(hsai->State != HAL_SAI_STATE_RESET)
1271 /* set the mute counter */
1272 CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTECNT);
1273 SET_BIT(hsai->Instance->CR2, (uint32_t)((uint32_t)counter << SAI_xCR2_MUTECNT_OFFSET));
1274 hsai->mutecallback = callback;
1275 /* enable the IT interrupt */
1276 __HAL_SAI_ENABLE_IT(hsai, SAI_IT_MUTEDET);
1277 return HAL_OK;
1279 return HAL_ERROR;
1283 * @brief Disable the Rx mute detection.
1284 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1285 * the configuration information for SAI module.
1286 * @retval HAL status
1288 HAL_StatusTypeDef HAL_SAI_DisableRxMuteMode(SAI_HandleTypeDef *hsai)
1290 if(hsai->State != HAL_SAI_STATE_RESET)
1292 /* set the mutecallback to NULL */
1293 hsai->mutecallback = (SAIcallback)NULL;
1294 /* enable the IT interrupt */
1295 __HAL_SAI_DISABLE_IT(hsai, SAI_IT_MUTEDET);
1296 return HAL_OK;
1298 return HAL_ERROR;
1302 * @brief Handle SAI interrupt request.
1303 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1304 * the configuration information for SAI module.
1305 * @retval None
1307 void HAL_SAI_IRQHandler(SAI_HandleTypeDef *hsai)
1309 if(hsai->State != HAL_SAI_STATE_RESET)
1311 uint32_t itflags = hsai->Instance->SR;
1312 uint32_t itsources = hsai->Instance->IMR;
1313 uint32_t cr1config = hsai->Instance->CR1;
1314 uint32_t tmperror;
1316 /* SAI Fifo request interrupt occured ------------------------------------*/
1317 if(((itflags & SAI_xSR_FREQ) == SAI_xSR_FREQ) && ((itsources & SAI_IT_FREQ) == SAI_IT_FREQ))
1319 hsai->InterruptServiceRoutine(hsai);
1321 /* SAI Overrun error interrupt occurred ----------------------------------*/
1322 else if(((itflags & SAI_FLAG_OVRUDR) == SAI_FLAG_OVRUDR) && ((itsources & SAI_IT_OVRUDR) == SAI_IT_OVRUDR))
1324 /* Clear the SAI Overrun flag */
1325 __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
1327 /* Get the SAI error code */
1328 tmperror = ((hsai->State == HAL_SAI_STATE_BUSY_RX) ? HAL_SAI_ERROR_OVR : HAL_SAI_ERROR_UDR);
1330 /* Change the SAI error code */
1331 hsai->ErrorCode |= tmperror;
1333 /* the transfer is not stopped, we will forward the information to the user and we let the user decide what needs to be done */
1334 HAL_SAI_ErrorCallback(hsai);
1336 /* SAI mutedet interrupt occurred ----------------------------------*/
1337 else if(((itflags & SAI_FLAG_MUTEDET) == SAI_FLAG_MUTEDET) && ((itsources & SAI_IT_MUTEDET) == SAI_IT_MUTEDET))
1339 /* Clear the SAI mutedet flag */
1340 __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_MUTEDET);
1342 /* call the call back function */
1343 if(hsai->mutecallback != (SAIcallback)NULL)
1345 /* inform the user that an RX mute event has been detected */
1346 hsai->mutecallback();
1349 /* SAI AFSDET interrupt occurred ----------------------------------*/
1350 else if(((itflags & SAI_FLAG_AFSDET) == SAI_FLAG_AFSDET) && ((itsources & SAI_IT_AFSDET) == SAI_IT_AFSDET))
1352 /* Change the SAI error code */
1353 hsai->ErrorCode |= HAL_SAI_ERROR_AFSDET;
1355 /* Check SAI DMA is enabled or not */
1356 if((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
1358 /* Abort the SAI DMA Streams */
1359 if(hsai->hdmatx != NULL)
1361 /* Set the DMA Tx abort callback */
1362 hsai->hdmatx->XferAbortCallback = SAI_DMAAbort;
1364 /* Abort DMA in IT mode */
1365 HAL_DMA_Abort_IT(hsai->hdmatx);
1367 else if(hsai->hdmarx != NULL)
1369 /* Set the DMA Rx abort callback */
1370 hsai->hdmarx->XferAbortCallback = SAI_DMAAbort;
1372 /* Abort DMA in IT mode */
1373 HAL_DMA_Abort_IT(hsai->hdmarx);
1376 else
1378 /* Abort SAI */
1379 HAL_SAI_Abort(hsai);
1381 /* Set error callback */
1382 HAL_SAI_ErrorCallback(hsai);
1385 /* SAI LFSDET interrupt occurred ----------------------------------*/
1386 else if(((itflags & SAI_FLAG_LFSDET) == SAI_FLAG_LFSDET) && ((itsources & SAI_IT_LFSDET) == SAI_IT_LFSDET))
1388 /* Change the SAI error code */
1389 hsai->ErrorCode |= HAL_SAI_ERROR_LFSDET;
1391 /* Check SAI DMA is enabled or not */
1392 if((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
1394 /* Abort the SAI DMA Streams */
1395 if(hsai->hdmatx != NULL)
1397 /* Set the DMA Tx abort callback */
1398 hsai->hdmatx->XferAbortCallback = SAI_DMAAbort;
1400 /* Abort DMA in IT mode */
1401 HAL_DMA_Abort_IT(hsai->hdmatx);
1403 else if(hsai->hdmarx != NULL)
1405 /* Set the DMA Rx abort callback */
1406 hsai->hdmarx->XferAbortCallback = SAI_DMAAbort;
1408 /* Abort DMA in IT mode */
1409 HAL_DMA_Abort_IT(hsai->hdmarx);
1412 else
1414 /* Abort SAI */
1415 HAL_SAI_Abort(hsai);
1417 /* Set error callback */
1418 HAL_SAI_ErrorCallback(hsai);
1421 /* SAI WCKCFG interrupt occurred ----------------------------------*/
1422 else if(((itflags & SAI_FLAG_WCKCFG) == SAI_FLAG_WCKCFG) && ((itsources & SAI_IT_WCKCFG) == SAI_IT_WCKCFG))
1424 /* Change the SAI error code */
1425 hsai->ErrorCode |= HAL_SAI_ERROR_WCKCFG;
1427 /* Check SAI DMA is enabled or not */
1428 if((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
1430 /* Abort the SAI DMA Streams */
1431 if(hsai->hdmatx != NULL)
1433 /* Set the DMA Tx abort callback */
1434 hsai->hdmatx->XferAbortCallback = SAI_DMAAbort;
1436 /* Abort DMA in IT mode */
1437 HAL_DMA_Abort_IT(hsai->hdmatx);
1439 else if(hsai->hdmarx != NULL)
1441 /* Set the DMA Rx abort callback */
1442 hsai->hdmarx->XferAbortCallback = SAI_DMAAbort;
1444 /* Abort DMA in IT mode */
1445 HAL_DMA_Abort_IT(hsai->hdmarx);
1448 else
1450 /* If WCKCFG occurs, SAI audio block is automatically disabled */
1451 /* Disable all interrupts and clear all flags */
1452 hsai->Instance->IMR = 0U;
1453 hsai->Instance->CLRFR = 0xFFFFFFFFU;
1455 /* Set the SAI state to ready to be able to start again the process */
1456 hsai->State = HAL_SAI_STATE_READY;
1458 /* Initialize XferCount */
1459 hsai->XferCount = 0U;
1461 /* SAI error Callback */
1462 HAL_SAI_ErrorCallback(hsai);
1465 /* SAI CNRDY interrupt occurred ----------------------------------*/
1466 else if(((itflags & SAI_FLAG_CNRDY) == SAI_FLAG_CNRDY) && ((itsources & SAI_IT_CNRDY) == SAI_IT_CNRDY))
1468 /* Clear the SAI CNRDY flag */
1469 __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_CNRDY);
1471 /* Change the SAI error code */
1472 hsai->ErrorCode |= HAL_SAI_ERROR_CNREADY;
1474 /* the transfer is not stopped, we will forward the information to the user and we let the user decide what needs to be done */
1475 HAL_SAI_ErrorCallback(hsai);
1477 else
1479 /* Nothing to do */
1485 * @brief Tx Transfer completed callback.
1486 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1487 * the configuration information for SAI module.
1488 * @retval None
1490 __weak void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai)
1492 /* Prevent unused argument(s) compilation warning */
1493 UNUSED(hsai);
1495 /* NOTE : This function should not be modified, when the callback is needed,
1496 the HAL_SAI_TxCpltCallback could be implemented in the user file
1501 * @brief Tx Transfer Half completed callback.
1502 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1503 * the configuration information for SAI module.
1504 * @retval None
1506 __weak void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai)
1508 /* Prevent unused argument(s) compilation warning */
1509 UNUSED(hsai);
1511 /* NOTE : This function should not be modified, when the callback is needed,
1512 the HAL_SAI_TxHalfCpltCallback could be implemented in the user file
1517 * @brief Rx Transfer completed callback.
1518 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1519 * the configuration information for SAI module.
1520 * @retval None
1522 __weak void HAL_SAI_RxCpltCallback(SAI_HandleTypeDef *hsai)
1524 /* Prevent unused argument(s) compilation warning */
1525 UNUSED(hsai);
1527 /* NOTE : This function should not be modified, when the callback is needed,
1528 the HAL_SAI_RxCpltCallback could be implemented in the user file
1533 * @brief Rx Transfer half completed callback.
1534 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1535 * the configuration information for SAI module.
1536 * @retval None
1538 __weak void HAL_SAI_RxHalfCpltCallback(SAI_HandleTypeDef *hsai)
1540 /* Prevent unused argument(s) compilation warning */
1541 UNUSED(hsai);
1543 /* NOTE : This function should not be modified, when the callback is needed,
1544 the HAL_SAI_RxHalfCpltCallback could be implemented in the user file
1549 * @brief SAI error callback.
1550 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1551 * the configuration information for SAI module.
1552 * @retval None
1554 __weak void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai)
1556 /* Prevent unused argument(s) compilation warning */
1557 UNUSED(hsai);
1559 /* NOTE : This function should not be modified, when the callback is needed,
1560 the HAL_SAI_ErrorCallback could be implemented in the user file
1565 * @}
1569 /** @defgroup SAI_Exported_Functions_Group3 Peripheral State functions
1570 * @brief Peripheral State functions
1572 @verbatim
1573 ===============================================================================
1574 ##### Peripheral State and Errors functions #####
1575 ===============================================================================
1576 [..]
1577 This subsection permits to get in run-time the status of the peripheral
1578 and the data flow.
1580 @endverbatim
1581 * @{
1585 * @brief Return the SAI handle state.
1586 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1587 * the configuration information for SAI module.
1588 * @retval HAL state
1590 HAL_SAI_StateTypeDef HAL_SAI_GetState(SAI_HandleTypeDef *hsai)
1592 return hsai->State;
1596 * @brief Return the SAI error code.
1597 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1598 * the configuration information for the specified SAI Block.
1599 * @retval SAI Error Code
1601 uint32_t HAL_SAI_GetError(SAI_HandleTypeDef *hsai)
1603 return hsai->ErrorCode;
1606 * @}
1610 * @}
1613 /** @addtogroup SAI_Private_Functions
1614 * @brief Private functions
1615 * @{
1619 * @brief Initialize the SAI I2S protocol according to the specified parameters
1620 * in the SAI_InitTypeDef and create the associated handle.
1621 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1622 * the configuration information for SAI module.
1623 * @param protocol: one of the supported protocol.
1624 * @param datasize: one of the supported datasize @ref SAI_Protocol_DataSize
1625 * the configuration information for SAI module.
1626 * @param nbslot: number of slot minimum value is 2 and max is 16.
1627 * the value must be a multiple of 2.
1628 * @retval HAL status
1630 static HAL_StatusTypeDef SAI_InitI2S(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
1632 hsai->Init.Protocol = SAI_FREE_PROTOCOL;
1633 hsai->Init.FirstBit = SAI_FIRSTBIT_MSB;
1634 /* Compute ClockStrobing according AudioMode */
1635 if((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
1636 { /* Transmit */
1637 hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
1639 else
1640 { /* Receive */
1641 hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE;
1643 hsai->FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION;
1644 hsai->SlotInit.SlotActive = SAI_SLOTACTIVE_ALL;
1645 hsai->SlotInit.FirstBitOffset = 0U;
1646 hsai->SlotInit.SlotNumber = nbslot;
1648 /* in IS2 the number of slot must be even */
1649 if((nbslot & 0x1U) != 0U)
1651 return HAL_ERROR;
1654 switch(protocol)
1656 case SAI_I2S_STANDARD :
1657 hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
1658 hsai->FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
1659 break;
1660 case SAI_I2S_MSBJUSTIFIED :
1661 case SAI_I2S_LSBJUSTIFIED :
1662 hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_HIGH;
1663 hsai->FrameInit.FSOffset = SAI_FS_FIRSTBIT;
1664 break;
1665 default :
1666 return HAL_ERROR;
1669 /* Frame definition */
1670 switch(datasize)
1672 case SAI_PROTOCOL_DATASIZE_16BIT:
1673 hsai->Init.DataSize = SAI_DATASIZE_16;
1674 hsai->FrameInit.FrameLength = 32U*(nbslot/2U);
1675 hsai->FrameInit.ActiveFrameLength = 16U*(nbslot/2U);
1676 hsai->SlotInit.SlotSize = SAI_SLOTSIZE_16B;
1677 break;
1678 case SAI_PROTOCOL_DATASIZE_16BITEXTENDED :
1679 hsai->Init.DataSize = SAI_DATASIZE_16;
1680 hsai->FrameInit.FrameLength = 64U*(nbslot/2U);
1681 hsai->FrameInit.ActiveFrameLength = 32U*(nbslot/2U);
1682 hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
1683 break;
1684 case SAI_PROTOCOL_DATASIZE_24BIT:
1685 hsai->Init.DataSize = SAI_DATASIZE_24;
1686 hsai->FrameInit.FrameLength = 64U*(nbslot/2U);
1687 hsai->FrameInit.ActiveFrameLength = 32U*(nbslot/2U);
1688 hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
1689 break;
1690 case SAI_PROTOCOL_DATASIZE_32BIT:
1691 hsai->Init.DataSize = SAI_DATASIZE_32;
1692 hsai->FrameInit.FrameLength = 64U*(nbslot/2U);
1693 hsai->FrameInit.ActiveFrameLength = 32U*(nbslot/2U);
1694 hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
1695 break;
1696 default :
1697 return HAL_ERROR;
1699 if(protocol == SAI_I2S_LSBJUSTIFIED)
1701 if (datasize == SAI_PROTOCOL_DATASIZE_16BITEXTENDED)
1703 hsai->SlotInit.FirstBitOffset = 16U;
1705 if (datasize == SAI_PROTOCOL_DATASIZE_24BIT)
1707 hsai->SlotInit.FirstBitOffset = 8U;
1710 return HAL_OK;
1714 * @brief Initialize the SAI PCM protocol according to the specified parameters
1715 * in the SAI_InitTypeDef and create the associated handle.
1716 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1717 * the configuration information for SAI module.
1718 * @param protocol: one of the supported protocol
1719 * @param datasize: one of the supported datasize @ref SAI_Protocol_DataSize
1720 * @param nbslot: number of slot minimum value is 1 and the max is 16.
1721 * @retval HAL status
1723 static HAL_StatusTypeDef SAI_InitPCM(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
1725 hsai->Init.Protocol = SAI_FREE_PROTOCOL;
1726 hsai->Init.FirstBit = SAI_FIRSTBIT_MSB;
1727 /* Compute ClockStrobing according AudioMode */
1728 if((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
1729 { /* Transmit */
1730 hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE;
1732 else
1733 { /* Receive */
1734 hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
1736 hsai->FrameInit.FSDefinition = SAI_FS_STARTFRAME;
1737 hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_HIGH;
1738 hsai->FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
1739 hsai->SlotInit.FirstBitOffset = 0U;
1740 hsai->SlotInit.SlotNumber = nbslot;
1741 hsai->SlotInit.SlotActive = SAI_SLOTACTIVE_ALL;
1743 switch(protocol)
1745 case SAI_PCM_SHORT :
1746 hsai->FrameInit.ActiveFrameLength = 1U;
1747 break;
1748 case SAI_PCM_LONG :
1749 hsai->FrameInit.ActiveFrameLength = 13U;
1750 break;
1751 default :
1752 return HAL_ERROR;
1755 switch(datasize)
1757 case SAI_PROTOCOL_DATASIZE_16BIT:
1758 hsai->Init.DataSize = SAI_DATASIZE_16;
1759 hsai->FrameInit.FrameLength = 16U * nbslot;
1760 hsai->SlotInit.SlotSize = SAI_SLOTSIZE_16B;
1761 break;
1762 case SAI_PROTOCOL_DATASIZE_16BITEXTENDED :
1763 hsai->Init.DataSize = SAI_DATASIZE_16;
1764 hsai->FrameInit.FrameLength = 32U * nbslot;
1765 hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
1766 break;
1767 case SAI_PROTOCOL_DATASIZE_24BIT :
1768 hsai->Init.DataSize = SAI_DATASIZE_24;
1769 hsai->FrameInit.FrameLength = 32U * nbslot;
1770 hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
1771 break;
1772 case SAI_PROTOCOL_DATASIZE_32BIT:
1773 hsai->Init.DataSize = SAI_DATASIZE_32;
1774 hsai->FrameInit.FrameLength = 32U * nbslot;
1775 hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
1776 break;
1777 default :
1778 return HAL_ERROR;
1781 return HAL_OK;
1785 * @brief Fill the fifo.
1786 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1787 * the configuration information for SAI module.
1788 * @retval None
1790 static void SAI_FillFifo(SAI_HandleTypeDef *hsai)
1792 /* fill the fifo with data before to enabled the SAI */
1793 while(((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_FULL) && (hsai->XferCount > 0U))
1795 if((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
1797 hsai->Instance->DR = (*hsai->pBuffPtr++);
1799 else if(hsai->Init.DataSize <= SAI_DATASIZE_16)
1801 hsai->Instance->DR = *((uint32_t *)hsai->pBuffPtr);
1802 hsai->pBuffPtr+= 2U;
1804 else
1806 hsai->Instance->DR = *((uint32_t *)hsai->pBuffPtr);
1807 hsai->pBuffPtr+= 4U;
1809 hsai->XferCount--;
1814 * @brief Return the interrupt flag to set according the SAI setup.
1815 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1816 * the configuration information for SAI module.
1817 * @param mode: SAI_MODE_DMA or SAI_MODE_IT
1818 * @retval the list of the IT flag to enable
1820 static uint32_t SAI_InterruptFlag(SAI_HandleTypeDef *hsai, uint32_t mode)
1822 uint32_t tmpIT = SAI_IT_OVRUDR;
1824 if(mode == SAI_MODE_IT)
1826 tmpIT|= SAI_IT_FREQ;
1829 if((hsai->Init.Protocol == SAI_AC97_PROTOCOL) &&
1830 ((hsai->Init.AudioMode == SAI_MODESLAVE_RX) || (hsai->Init.AudioMode == SAI_MODEMASTER_RX)))
1832 tmpIT|= SAI_IT_CNRDY;
1835 if((hsai->Init.AudioMode == SAI_MODESLAVE_RX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
1837 tmpIT|= SAI_IT_AFSDET | SAI_IT_LFSDET;
1839 else
1841 /* hsai has been configured in master mode */
1842 tmpIT|= SAI_IT_WCKCFG;
1844 return tmpIT;
1848 * @brief Disable the SAI and wait for the disabling.
1849 * @param hsai : pointer to a SAI_HandleTypeDef structure that contains
1850 * the configuration information for SAI module.
1851 * @retval None
1853 static HAL_StatusTypeDef SAI_Disable(SAI_HandleTypeDef *hsai)
1855 register uint32_t count = SAI_DEFAULT_TIMEOUT * (SystemCoreClock /7U/1000U);
1856 HAL_StatusTypeDef status = HAL_OK;
1858 /* Disable the SAI instance */
1859 __HAL_SAI_DISABLE(hsai);
1863 /* Check for the Timeout */
1864 if (count-- == 0U)
1866 /* Update error code */
1867 hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
1868 status = HAL_TIMEOUT;
1869 break;
1871 } while((hsai->Instance->CR1 & SAI_xCR1_SAIEN) != RESET);
1873 return status;
1877 * @brief Tx Handler for Transmit in Interrupt mode 8-Bit transfer.
1878 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1879 * the configuration information for SAI module.
1880 * @retval None
1882 static void SAI_Transmit_IT8Bit(SAI_HandleTypeDef *hsai)
1884 if(hsai->XferCount == 0U)
1886 /* Handle the end of the transmission */
1887 /* Disable FREQ and OVRUDR interrupts */
1888 __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
1889 hsai->State = HAL_SAI_STATE_READY;
1890 HAL_SAI_TxCpltCallback(hsai);
1892 else
1894 /* Write data on DR register */
1895 hsai->Instance->DR = (*hsai->pBuffPtr++);
1896 hsai->XferCount--;
1901 * @brief Tx Handler for Transmit in Interrupt mode for 16-Bit transfer.
1902 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1903 * the configuration information for SAI module.
1904 * @retval None
1906 static void SAI_Transmit_IT16Bit(SAI_HandleTypeDef *hsai)
1908 if(hsai->XferCount == 0U)
1910 /* Handle the end of the transmission */
1911 /* Disable FREQ and OVRUDR interrupts */
1912 __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
1913 hsai->State = HAL_SAI_STATE_READY;
1914 HAL_SAI_TxCpltCallback(hsai);
1916 else
1918 /* Write data on DR register */
1919 hsai->Instance->DR = *(uint16_t *)hsai->pBuffPtr;
1920 hsai->pBuffPtr+=2U;
1921 hsai->XferCount--;
1926 * @brief Tx Handler for Transmit in Interrupt mode for 32-Bit transfer.
1927 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1928 * the configuration information for SAI module.
1929 * @retval None
1931 static void SAI_Transmit_IT32Bit(SAI_HandleTypeDef *hsai)
1933 if(hsai->XferCount == 0U)
1935 /* Handle the end of the transmission */
1936 /* Disable FREQ and OVRUDR interrupts */
1937 __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
1938 hsai->State = HAL_SAI_STATE_READY;
1939 HAL_SAI_TxCpltCallback(hsai);
1941 else
1943 /* Write data on DR register */
1944 hsai->Instance->DR = *(uint32_t *)hsai->pBuffPtr;
1945 hsai->pBuffPtr+=4U;
1946 hsai->XferCount--;
1951 * @brief Rx Handler for Receive in Interrupt mode 8-Bit transfer.
1952 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1953 * the configuration information for SAI module.
1954 * @retval None
1956 static void SAI_Receive_IT8Bit(SAI_HandleTypeDef *hsai)
1958 /* Receive data */
1959 (*hsai->pBuffPtr++) = hsai->Instance->DR;
1960 hsai->XferCount--;
1962 /* Check end of the transfer */
1963 if(hsai->XferCount == 0U)
1965 /* Disable TXE and OVRUDR interrupts */
1966 __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
1968 /* Clear the SAI Overrun flag */
1969 __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
1971 hsai->State = HAL_SAI_STATE_READY;
1972 HAL_SAI_RxCpltCallback(hsai);
1977 * @brief Rx Handler for Receive in Interrupt mode for 16-Bit transfer.
1978 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
1979 * the configuration information for SAI module.
1980 * @retval None
1982 static void SAI_Receive_IT16Bit(SAI_HandleTypeDef *hsai)
1984 /* Receive data */
1985 *(uint16_t*)hsai->pBuffPtr = hsai->Instance->DR;
1986 hsai->pBuffPtr+=2U;
1987 hsai->XferCount--;
1989 /* Check end of the transfer */
1990 if(hsai->XferCount == 0U)
1992 /* Disable TXE and OVRUDR interrupts */
1993 __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
1995 /* Clear the SAI Overrun flag */
1996 __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
1998 hsai->State = HAL_SAI_STATE_READY;
1999 HAL_SAI_RxCpltCallback(hsai);
2004 * @brief Rx Handler for Receive in Interrupt mode for 32-Bit transfer.
2005 * @param hsai: pointer to a SAI_HandleTypeDef structure that contains
2006 * the configuration information for SAI module.
2007 * @retval None
2009 static void SAI_Receive_IT32Bit(SAI_HandleTypeDef *hsai)
2011 /* Receive data */
2012 *(uint32_t*)hsai->pBuffPtr = hsai->Instance->DR;
2013 hsai->pBuffPtr+=4U;
2014 hsai->XferCount--;
2016 /* Check end of the transfer */
2017 if(hsai->XferCount == 0U)
2019 /* Disable TXE and OVRUDR interrupts */
2020 __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
2022 /* Clear the SAI Overrun flag */
2023 __HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
2025 hsai->State = HAL_SAI_STATE_READY;
2026 HAL_SAI_RxCpltCallback(hsai);
2031 * @brief DMA SAI transmit process complete callback.
2032 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2033 * the configuration information for the specified DMA module.
2034 * @retval None
2036 static void SAI_DMATxCplt(DMA_HandleTypeDef *hdma)
2038 SAI_HandleTypeDef* hsai = (SAI_HandleTypeDef*)((DMA_HandleTypeDef* )hdma)->Parent;
2040 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
2042 hsai->XferCount = 0U;
2044 /* Disable SAI Tx DMA Request */
2045 hsai->Instance->CR1 &= (uint32_t)(~SAI_xCR1_DMAEN);
2047 /* Stop the interrupts error handling */
2048 __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
2050 hsai->State= HAL_SAI_STATE_READY;
2052 HAL_SAI_TxCpltCallback(hsai);
2056 * @brief DMA SAI transmit process half complete callback.
2057 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2058 * the configuration information for the specified DMA module.
2059 * @retval None
2061 static void SAI_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
2063 SAI_HandleTypeDef* hsai = (SAI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2065 HAL_SAI_TxHalfCpltCallback(hsai);
2069 * @brief DMA SAI receive process complete callback.
2070 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2071 * the configuration information for the specified DMA module.
2072 * @retval None
2074 static void SAI_DMARxCplt(DMA_HandleTypeDef *hdma)
2076 SAI_HandleTypeDef* hsai = ( SAI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2077 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
2079 /* Disable Rx DMA Request */
2080 hsai->Instance->CR1 &= (uint32_t)(~SAI_xCR1_DMAEN);
2081 hsai->XferCount = 0U;
2083 /* Stop the interrupts error handling */
2084 __HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
2086 hsai->State = HAL_SAI_STATE_READY;
2088 HAL_SAI_RxCpltCallback(hsai);
2092 * @brief DMA SAI receive process half complete callback
2093 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2094 * the configuration information for the specified DMA module.
2095 * @retval None
2097 static void SAI_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
2099 SAI_HandleTypeDef* hsai = (SAI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2101 HAL_SAI_RxHalfCpltCallback(hsai);
2105 * @brief DMA SAI communication error callback.
2106 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2107 * the configuration information for the specified DMA module.
2108 * @retval None
2110 static void SAI_DMAError(DMA_HandleTypeDef *hdma)
2112 SAI_HandleTypeDef* hsai = ( SAI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2114 /* Set SAI error code */
2115 hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
2117 if((hsai->hdmatx->ErrorCode == HAL_DMA_ERROR_TE) || (hsai->hdmarx->ErrorCode == HAL_DMA_ERROR_TE))
2119 /* Disable the SAI DMA request */
2120 hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
2122 /* Disable SAI peripheral */
2123 SAI_Disable(hsai);
2125 /* Set the SAI state ready to be able to start again the process */
2126 hsai->State = HAL_SAI_STATE_READY;
2128 /* Initialize XferCount */
2129 hsai->XferCount = 0U;
2131 /* SAI error Callback */
2132 HAL_SAI_ErrorCallback(hsai);
2136 * @brief DMA SAI Abort callback.
2137 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2138 * the configuration information for the specified DMA module.
2139 * @retval None
2141 static void SAI_DMAAbort(DMA_HandleTypeDef *hdma)
2143 SAI_HandleTypeDef* hsai = ( SAI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2145 /* Disable DMA request */
2146 hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
2148 /* Disable all interrupts and clear all flags */
2149 hsai->Instance->IMR = 0U;
2150 hsai->Instance->CLRFR = 0xFFFFFFFFU;
2152 if(hsai->ErrorCode != HAL_SAI_ERROR_WCKCFG)
2154 /* Disable SAI peripheral */
2155 SAI_Disable(hsai);
2157 /* Flush the fifo */
2158 SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
2160 /* Set the SAI state to ready to be able to start again the process */
2161 hsai->State = HAL_SAI_STATE_READY;
2163 /* Initialize XferCount */
2164 hsai->XferCount = 0U;
2166 /* SAI error Callback */
2167 HAL_SAI_ErrorCallback(hsai);
2171 * @}
2174 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx || STM32F413xx || STM32F423xx */
2175 #endif /* HAL_SAI_MODULE_ENABLED */
2177 * @}
2181 * @}
2184 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/