1 /**
2   ******************************************************************************
3   * @file    stm32f7xx_hal_sai.h
4   * @author  MCD Application Team
5   * @version V1.0.1
6   * @date    25-June-2015
7   * @brief   Header file of SAI HAL module.
8   ******************************************************************************
9   * @attention
10   *
11   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
12   *
13   * Redistribution and use in source and binary forms, with or without modification,
14   * are permitted provided that the following conditions are met:
15   *   1. Redistributions of source code must retain the above copyright notice,
16   *      this list of conditions and the following disclaimer.
17   *   2. Redistributions in binary form must reproduce the above copyright notice,
18   *      this list of conditions and the following disclaimer in the documentation
19   *      and/or other materials provided with the distribution.
20   *   3. Neither the name of STMicroelectronics nor the names of its contributors
21   *      may be used to endorse or promote products derived from this software
22   *      without specific prior written permission.
23   *
24   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34   *
35   ******************************************************************************
36   */
37 
38 /* Define to prevent recursive inclusion -------------------------------------*/
39 #ifndef __STM32F7xx_HAL_SAI_H
40 #define __STM32F7xx_HAL_SAI_H
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 
47 /* Includes ------------------------------------------------------------------*/
48 #include "stm32f7xx_hal_def.h"
49 
50 /** @addtogroup STM32F7xx_HAL_Driver
51   * @{
52   */
53 
54 /** @addtogroup SAI
55   * @{
56   */
57 
58 /* Exported types ------------------------------------------------------------*/
59 /** @defgroup SAI_Exported_Types SAI Exported Types
60   * @{
61   */
62 
63 /**
64   * @brief  HAL State structures definition
65   */
66 typedef enum {
67     HAL_SAI_STATE_RESET      = 0x00,  /*!< SAI not yet initialized or disabled                */
68     HAL_SAI_STATE_READY      = 0x01,  /*!< SAI initialized and ready for use                  */
69     HAL_SAI_STATE_BUSY       = 0x02,  /*!< SAI internal process is ongoing                    */
70     HAL_SAI_STATE_BUSY_TX    = 0x12,  /*!< Data transmission process is ongoing               */
71     HAL_SAI_STATE_BUSY_RX    = 0x22,  /*!< Data reception process is ongoing                  */
72     HAL_SAI_STATE_TIMEOUT    = 0x03,  /*!< SAI timeout state                                  */
73     HAL_SAI_STATE_ERROR      = 0x04   /*!< SAI error state                                    */
74 } HAL_SAI_StateTypeDef;
75 
76 /**
77   * @brief  SAI Callback prototype
78   */
79 typedef void (*SAIcallback)(void);
80 
81 /**
82   * @brief  SAI Init Structure definition
83   */
84 typedef struct {
85     uint32_t AudioMode;           /*!< Specifies the SAI Block audio Mode.
86                                      This parameter can be a value of @ref SAI_Block_Mode                 */
87 
88     uint32_t Synchro;             /*!< Specifies SAI Block synchronization
89                                      This parameter can be a value of @ref SAI_Block_Synchronization           */
90 
91     uint32_t SynchroExt;          /*!< Specifies SAI Block synchronization, this setup is common
92                                      for BLOCKA and BLOCKB
93                                      This parameter can be a value of @ref SAI_Block_SyncExt                   */
94 
95     uint32_t OutputDrive;         /*!< Specifies when SAI Block outputs are driven.
96                                      This parameter can be a value of @ref SAI_Block_Output_Drive
97                                      @note this value has to be set before enabling the audio block
98                                      but after the audio block configuration.                                  */
99 
100     uint32_t NoDivider;           /*!< Specifies whether master clock will be divided or not.
101                                      This parameter can be a value of @ref SAI_Block_NoDivider
102                                      @note  If bit NODIV in the SAI_xCR1 register is cleared, the frame length
103                                             should be aligned to a number equal to a power of 2, from 8 to 256.
104                                             If bit NODIV in the SAI_xCR1 register is set, the frame length can
105                                             take any of the values without constraint since the input clock of
106                                             the audio block should be equal to the bit clock.
107                                              There is no MCLK_x clock which can be output.                     */
108 
109     uint32_t FIFOThreshold;       /*!< Specifies SAI Block FIFO threshold.
110                                      This parameter can be a value of @ref SAI_Block_Fifo_Threshold            */
111 
112     uint32_t AudioFrequency;      /*!< Specifies the audio frequency sampling.
113                                      This parameter can be a value of @ref SAI_Audio_Frequency                 */
114 
115     uint32_t Mckdiv;              /*!< Specifies the master clock divider, the parameter will be used if for
116                                      AudioFrequency the user choice
117                                      This parameter must be a number between Min_Data = 0 and Max_Data = 15    */
118 
119     uint32_t MonoStereoMode;      /*!< Specifies if the mono or stereo mode is selected.
120                                      This parameter can be a value of @ref SAI_Mono_Stereo_Mode                */
121 
122     uint32_t CompandingMode;      /*!< Specifies the companding mode type.
123                                      This parameter can be a value of @ref SAI_Block_Companding_Mode           */
124 
125     uint32_t TriState;            /*!< Specifies the companding mode type.
126                                      This parameter can be a value of @ref SAI_TRIState_Management             */
127 
128     /* This part of the structure is automatically filled if your are using the high level initialisation
129        function HAL_SAI_InitProtocol                                                                             */
130 
131     uint32_t Protocol;        /*!< Specifies the SAI Block protocol.
132                                  This parameter can be a value of @ref SAI_Block_Protocol                      */
133 
134     uint32_t DataSize;        /*!< Specifies the SAI Block data size.
135                                  This parameter can be a value of @ref SAI_Block_Data_Size                     */
136 
137     uint32_t FirstBit;        /*!< Specifies whether data transfers start from MSB or LSB bit.
138                                  This parameter can be a value of @ref SAI_Block_MSB_LSB_transmission          */
139 
140     uint32_t ClockStrobing;   /*!< Specifies the SAI Block clock strobing edge sensitivity.
141                                  This parameter can be a value of @ref SAI_Block_Clock_Strobing                */
142 } SAI_InitTypeDef;
143 
144 /**
145   * @brief  SAI Block Frame Init structure definition
146   */
147 
148 typedef struct {
149 
150     uint32_t FrameLength;         /*!< Specifies the Frame length, the number of SCK clocks for each audio frame.
151                                      This parameter must be a number between Min_Data = 8 and Max_Data = 256.
152                                      @note  If master clock MCLK_x pin is declared as an output, the frame length
153                                             should be aligned to a number equal to power of 2 in order to keep
154                                             in an audio frame, an integer number of MCLK pulses by bit Clock. */
155 
156     uint32_t ActiveFrameLength;  /*!< Specifies the Frame synchronization active level length.
157                                     This Parameter specifies the length in number of bit clock (SCK + 1)
158                                     of the active level of FS signal in audio frame.
159                                     This parameter must be a number between Min_Data = 1 and Max_Data = 128   */
160 
161     uint32_t FSDefinition;       /*!< Specifies the Frame synchronization definition.
162                                     This parameter can be a value of @ref SAI_Block_FS_Definition             */
163 
164     uint32_t FSPolarity;         /*!< Specifies the Frame synchronization Polarity.
165                                     This parameter can be a value of @ref SAI_Block_FS_Polarity               */
166 
167     uint32_t FSOffset;           /*!< Specifies the Frame synchronization Offset.
168                                     This parameter can be a value of @ref SAI_Block_FS_Offset                 */
169 
170 } SAI_FrameInitTypeDef;
171 
172 /**
173   * @brief   SAI Block Slot Init Structure definition
174   */
175 
176 typedef struct {
177     uint32_t FirstBitOffset;  /*!< Specifies the position of first data transfer bit in the slot.
178                                  This parameter must be a number between Min_Data = 0 and Max_Data = 24 */
179 
180     uint32_t SlotSize;        /*!< Specifies the Slot Size.
181                                  This parameter can be a value of @ref SAI_Block_Slot_Size              */
182 
183     uint32_t SlotNumber;      /*!< Specifies the number of slot in the audio frame.
184                                  This parameter must be a number between Min_Data = 1 and Max_Data = 16 */
185 
186     uint32_t SlotActive;      /*!< Specifies the slots in audio frame that will be activated.
187                                  This parameter can be a value of @ref SAI_Block_Slot_Active            */
188 } SAI_SlotInitTypeDef;
189 
190 /**
191   * @brief  SAI handle Structure definition
192   */
193 typedef struct __SAI_HandleTypeDef {
194     SAI_Block_TypeDef         *Instance;  /*!< SAI Blockx registers base address        */
195 
196     SAI_InitTypeDef           Init;       /*!< SAI communication parameters             */
197 
198     SAI_FrameInitTypeDef      FrameInit;  /*!< SAI Frame configuration parameters       */
199 
200     SAI_SlotInitTypeDef       SlotInit;   /*!< SAI Slot configuration parameters        */
201 
202     uint8_t                  *pBuffPtr;  /*!< Pointer to SAI transfer Buffer            */
203 
204     uint16_t                  XferSize;  /*!< SAI transfer size                         */
205 
206     uint16_t                  XferCount; /*!< SAI transfer counter                      */
207 
208     DMA_HandleTypeDef         *hdmatx;     /*!< SAI Tx DMA handle parameters            */
209 
210     DMA_HandleTypeDef         *hdmarx;     /*!< SAI Rx DMA handle parameters            */
211 
212     SAIcallback               mutecallback;/*!< SAI mute callback                */
213 
214     void (*InterruptServiceRoutine)(struct __SAI_HandleTypeDef *hsai); /* function pointer for IRQ handler   */
215 
216     HAL_LockTypeDef           Lock;        /*!< SAI locking object                      */
217 
218     __IO HAL_SAI_StateTypeDef State;       /*!< SAI communication state                 */
219 
220     __IO uint32_t             ErrorCode;   /*!< SAI Error code                          */
221 } SAI_HandleTypeDef;
222 
223 /**
224   * @}
225   */
226 
227 /* Exported constants --------------------------------------------------------*/
228 
229 /** @defgroup SAI_Exported_Constants SAI Exported Constants
230   * @{
231   */
232 
233 /** @defgroup SAI_Error_Code SAI Error Code
234   * @{
235   */
236 #define HAL_SAI_ERROR_NONE    ((uint32_t)0x00000000)  /*!< No error                                    */
237 #define HAL_SAI_ERROR_OVR     ((uint32_t)0x00000001)  /*!< Overrun Error                               */
238 #define HAL_SAI_ERROR_UDR     ((uint32_t)0x00000002)  /*!< Underrun error                              */
239 #define HAL_SAI_ERROR_AFSDET  ((uint32_t)0x00000004)  /*!< Anticipated Frame synchronisation detection */
240 #define HAL_SAI_ERROR_LFSDET  ((uint32_t)0x00000008)  /*!< Late Frame synchronisation detection        */
241 #define HAL_SAI_ERROR_CNREADY ((uint32_t)0x00000010)  /*!< codec not ready                             */
242 #define HAL_SAI_ERROR_WCKCFG  ((uint32_t)0x00000020)  /*!< Wrong clock configuration                   */
243 #define HAL_SAI_ERROR_TIMEOUT ((uint32_t)0x00000040)  /*!< Timeout error                               */
244 /**
245   * @}
246   */
247 
248 /** @defgroup SAI_Block_SyncExt SAI External synchronisation
249   * @{
250   */
251 #define SAI_SYNCEXT_DISABLE           ((uint32_t)0x00000000)
252 #define SAI_SYNCEXT_IN_ENABLE         ((uint32_t)0x00000001)
253 #define SAI_SYNCEXT_OUTBLOCKA_ENABLE  ((uint32_t)0x00000002)
254 #define SAI_SYNCEXT_OUTBLOCKB_ENABLE  ((uint32_t)0x00000004)
255 /**
256   * @}
257   */
258 
259 /** @defgroup SAI_Protocol SAI Supported protocol
260   * @{
261   */
262 #define SAI_I2S_STANDARD      ((uint32_t)0x00000000)
263 #define SAI_I2S_MSBJUSTIFIED  ((uint32_t)0x00000001)
264 #define SAI_I2S_LSBJUSTIFIED  ((uint32_t)0x00000002)
265 #define SAI_PCM_LONG          ((uint32_t)0x00000004)
266 #define SAI_PCM_SHORT         ((uint32_t)0x00000008)
267 /**
268   * @}
269   */
270 
271 /** @defgroup SAI_Protocol_DataSize SAI protocol data size
272   * @{
273   */
274 #define SAI_PROTOCOL_DATASIZE_16BIT          ((uint32_t)0x00000000)
275 #define SAI_PROTOCOL_DATASIZE_16BITEXTENDED  ((uint32_t)0x00000001)
276 #define SAI_PROTOCOL_DATASIZE_24BIT          ((uint32_t)0x00000002)
277 #define SAI_PROTOCOL_DATASIZE_32BIT          ((uint32_t)0x00000004)
278 /**
279   * @}
280   */
281 
282 /** @defgroup SAI_Clock_Source  SAI Clock Source
283   * @{
284   */
285 #define SAI_CLKSOURCE_PLLSAI             ((uint32_t)0x00000000)
286 #define SAI_CLKSOURCE_PLLI2S             ((uint32_t)0x00100000)
287 #define SAI_CLKSOURCE_EXT                ((uint32_t)0x00200000)
288 #define SAI_CLKSOURCE_NA                 ((uint32_t)0x00400000)
289 /**
290   * @}
291   */
292 
293 /** @defgroup SAI_Audio_Frequency SAI Audio Frequency
294   * @{
295   */
296 #define SAI_AUDIO_FREQUENCY_192K          ((uint32_t)192000)
297 #define SAI_AUDIO_FREQUENCY_96K           ((uint32_t)96000)
298 #define SAI_AUDIO_FREQUENCY_48K           ((uint32_t)48000)
299 #define SAI_AUDIO_FREQUENCY_44K           ((uint32_t)44100)
300 #define SAI_AUDIO_FREQUENCY_32K           ((uint32_t)32000)
301 #define SAI_AUDIO_FREQUENCY_22K           ((uint32_t)22050)
302 #define SAI_AUDIO_FREQUENCY_16K           ((uint32_t)16000)
303 #define SAI_AUDIO_FREQUENCY_11K           ((uint32_t)11025)
304 #define SAI_AUDIO_FREQUENCY_8K            ((uint32_t)8000)
305 #define SAI_AUDIO_FREQUENCY_MCKDIV        ((uint32_t)0)
306 
307 /**
308   * @}
309   */
310 
311 /** @defgroup SAI_Block_Mode SAI Block Mode
312   * @{
313   */
314 #define SAI_MODEMASTER_TX         ((uint32_t)0x00000000)
315 #define SAI_MODEMASTER_RX         ((uint32_t)SAI_xCR1_MODE_0)
316 #define SAI_MODESLAVE_TX          ((uint32_t)SAI_xCR1_MODE_1)
317 #define SAI_MODESLAVE_RX          ((uint32_t)(SAI_xCR1_MODE_1 | SAI_xCR1_MODE_0))
318 
319 /**
320   * @}
321   */
322 
323 /** @defgroup SAI_Block_Protocol SAI Block Protocol
324   * @{
325   */
326 #define SAI_FREE_PROTOCOL                 ((uint32_t)0x00000000)
327 #define SAI_SPDIF_PROTOCOL                ((uint32_t)SAI_xCR1_PRTCFG_0)
328 #define SAI_AC97_PROTOCOL                 ((uint32_t)SAI_xCR1_PRTCFG_1)
329 
330 /**
331   * @}
332   */
333 
334 /** @defgroup SAI_Block_Data_Size SAI Block Data Size
335   * @{
336   */
337 #define SAI_DATASIZE_8                   ((uint32_t)SAI_xCR1_DS_1)
338 #define SAI_DATASIZE_10                  ((uint32_t)(SAI_xCR1_DS_1 | SAI_xCR1_DS_0))
339 #define SAI_DATASIZE_16                  ((uint32_t)SAI_xCR1_DS_2)
340 #define SAI_DATASIZE_20                  ((uint32_t)(SAI_xCR1_DS_2 | SAI_xCR1_DS_0))
341 #define SAI_DATASIZE_24                  ((uint32_t)(SAI_xCR1_DS_2 | SAI_xCR1_DS_1))
342 #define SAI_DATASIZE_32                  ((uint32_t)(SAI_xCR1_DS_2 | SAI_xCR1_DS_1 | SAI_xCR1_DS_0))
343 
344 /**
345   * @}
346   */
347 
348 /** @defgroup SAI_Block_MSB_LSB_transmission SAI Block MSB LSB transmission
349   * @{
350   */
351 #define SAI_FIRSTBIT_MSB                  ((uint32_t)0x00000000)
352 #define SAI_FIRSTBIT_LSB                  ((uint32_t)SAI_xCR1_LSBFIRST)
353 
354 /**
355   * @}
356   */
357 
358 /** @defgroup SAI_Block_Clock_Strobing SAI Block Clock Strobing
359   * @{
360   */
361 #define SAI_CLOCKSTROBING_FALLINGEDGE     ((uint32_t)0x00000000)
362 #define SAI_CLOCKSTROBING_RISINGEDGE      ((uint32_t)SAI_xCR1_CKSTR)
363 
364 /**
365   * @}
366   */
367 
368 /** @defgroup SAI_Block_Synchronization SAI Block Synchronization
369   * @{
370   */
371 #define SAI_ASYNCHRONOUS                  ((uint32_t)0x00000000)
372 #define SAI_SYNCHRONOUS                   ((uint32_t)SAI_xCR1_SYNCEN_0)
373 #define SAI_SYNCHRONOUS_EXT               ((uint32_t)SAI_xCR1_SYNCEN_1)
374 
375 /**
376   * @}
377   */
378 
379 /** @defgroup SAI_Block_Output_Drive SAI Block Output Drive
380   * @{
381   */
382 #define SAI_OUTPUTDRIVE_DISABLE          ((uint32_t)0x00000000)
383 #define SAI_OUTPUTDRIVE_ENABLE           ((uint32_t)SAI_xCR1_OUTDRIV)
384 
385 /**
386   * @}
387   */
388 
389 /** @defgroup SAI_Block_NoDivider SAI Block NoDivider
390   * @{
391   */
392 #define SAI_MASTERDIVIDER_ENABLE         ((uint32_t)0x00000000)
393 #define SAI_MASTERDIVIDER_DISABLE        ((uint32_t)SAI_xCR1_NODIV)
394 
395 /**
396   * @}
397   */
398 
399 
400 /** @defgroup SAI_Block_FS_Definition SAI Block FS Definition
401   * @{
402   */
403 #define SAI_FS_STARTFRAME                 ((uint32_t)0x00000000)
404 #define SAI_FS_CHANNEL_IDENTIFICATION     ((uint32_t)SAI_xFRCR_FSDEF)
405 
406 /**
407   * @}
408   */
409 
410 /** @defgroup SAI_Block_FS_Polarity SAI Block FS Polarity
411   * @{
412   */
413 #define SAI_FS_ACTIVE_LOW                  ((uint32_t)0x00000000)
414 #define SAI_FS_ACTIVE_HIGH                 ((uint32_t)SAI_xFRCR_FSPO)
415 
416 /**
417   * @}
418   */
419 
420 /** @defgroup SAI_Block_FS_Offset SAI Block FS Offset
421   * @{
422   */
423 #define SAI_FS_FIRSTBIT                   ((uint32_t)0x00000000)
424 #define SAI_FS_BEFOREFIRSTBIT             ((uint32_t)SAI_xFRCR_FSOFF)
425 
426 /**
427   * @}
428   */
429 
430 
431 /** @defgroup SAI_Block_Slot_Size SAI Block Slot Size
432 * @{
433 */
434 #define SAI_SLOTSIZE_DATASIZE             ((uint32_t)0x00000000)
435 #define SAI_SLOTSIZE_16B                  ((uint32_t)SAI_xSLOTR_SLOTSZ_0)
436 #define SAI_SLOTSIZE_32B                  ((uint32_t)SAI_xSLOTR_SLOTSZ_1)
437 /**
438   * @}
439   */
440 
441 /** @defgroup SAI_Block_Slot_Active SAI Block Slot Active
442   * @{
443   */
444 #define SAI_SLOT_NOTACTIVE           ((uint32_t)0x00000000)
445 #define SAI_SLOTACTIVE_0             ((uint32_t)0x00010000)
446 #define SAI_SLOTACTIVE_1             ((uint32_t)0x00020000)
447 #define SAI_SLOTACTIVE_2             ((uint32_t)0x00040000)
448 #define SAI_SLOTACTIVE_3             ((uint32_t)0x00080000)
449 #define SAI_SLOTACTIVE_4             ((uint32_t)0x00100000)
450 #define SAI_SLOTACTIVE_5             ((uint32_t)0x00200000)
451 #define SAI_SLOTACTIVE_6             ((uint32_t)0x00400000)
452 #define SAI_SLOTACTIVE_7             ((uint32_t)0x00800000)
453 #define SAI_SLOTACTIVE_8             ((uint32_t)0x01000000)
454 #define SAI_SLOTACTIVE_9             ((uint32_t)0x02000000)
455 #define SAI_SLOTACTIVE_10            ((uint32_t)0x04000000)
456 #define SAI_SLOTACTIVE_11            ((uint32_t)0x08000000)
457 #define SAI_SLOTACTIVE_12            ((uint32_t)0x10000000)
458 #define SAI_SLOTACTIVE_13            ((uint32_t)0x20000000)
459 #define SAI_SLOTACTIVE_14            ((uint32_t)0x40000000)
460 #define SAI_SLOTACTIVE_15            ((uint32_t)0x80000000)
461 #define SAI_SLOTACTIVE_ALL           ((uint32_t)0xFFFF0000)
462 
463 /**
464   * @}
465   */
466 
467 /** @defgroup SAI_Mono_Stereo_Mode SAI Mono Stereo Mode
468   * @{
469   */
470 #define SAI_STEREOMODE                    ((uint32_t)0x00000000)
471 #define SAI_MONOMODE                      ((uint32_t)SAI_xCR1_MONO)
472 
473 /**
474   * @}
475   */
476 
477 /** @defgroup SAI_TRIState_Management SAI TRIState Management
478   * @{
479   */
480 #define SAI_OUTPUT_NOTRELEASED              ((uint32_t)0x00000000)
481 #define SAI_OUTPUT_RELEASED                 ((uint32_t)SAI_xCR2_TRIS)
482 
483 /**
484   * @}
485   */
486 
487 /** @defgroup SAI_Block_Fifo_Threshold SAI Block Fifo Threshold
488   * @{
489   */
490 #define SAI_FIFOTHRESHOLD_EMPTY  ((uint32_t)0x00000000)
491 #define SAI_FIFOTHRESHOLD_1QF    ((uint32_t)SAI_xCR2_FTH_0)
492 #define SAI_FIFOTHRESHOLD_HF     ((uint32_t)SAI_xCR2_FTH_1)
493 #define SAI_FIFOTHRESHOLD_3QF    ((uint32_t)(SAI_xCR2_FTH_1 | SAI_xCR2_FTH_0))
494 #define SAI_FIFOTHRESHOLD_FULL   ((uint32_t)SAI_xCR2_FTH_2)
495 
496 /**
497   * @}
498   */
499 
500 /** @defgroup SAI_Block_Companding_Mode SAI Block Companding Mode
501   * @{
502   */
503 #define SAI_NOCOMPANDING                  ((uint32_t)0x00000000)
504 #define SAI_ULAW_1CPL_COMPANDING          ((uint32_t)SAI_xCR2_COMP_1)
505 #define SAI_ALAW_1CPL_COMPANDING          ((uint32_t)(SAI_xCR2_COMP_1 | SAI_xCR2_COMP_0))
506 #define SAI_ULAW_2CPL_COMPANDING          ((uint32_t)(SAI_xCR2_COMP_1 | SAI_xCR2_CPL))
507 #define SAI_ALAW_2CPL_COMPANDING          ((uint32_t)(SAI_xCR2_COMP_1 | SAI_xCR2_COMP_0 | SAI_xCR2_CPL))
508 
509 /**
510   * @}
511   */
512 
513 /** @defgroup SAI_Block_Mute_Value SAI Block Mute Value
514   * @{
515   */
516 #define SAI_ZERO_VALUE                     ((uint32_t)0x00000000)
517 #define SAI_LAST_SENT_VALUE                 ((uint32_t)SAI_xCR2_MUTEVAL)
518 
519 /**
520   * @}
521   */
522 
523 
524 /** @defgroup SAI_Block_Interrupts_Definition SAI Block Interrupts Definition
525   * @{
526   */
527 #define SAI_IT_OVRUDR                     ((uint32_t)SAI_xIMR_OVRUDRIE)
528 #define SAI_IT_MUTEDET                    ((uint32_t)SAI_xIMR_MUTEDETIE)
529 #define SAI_IT_WCKCFG                     ((uint32_t)SAI_xIMR_WCKCFGIE)
530 #define SAI_IT_FREQ                       ((uint32_t)SAI_xIMR_FREQIE)
531 #define SAI_IT_CNRDY                      ((uint32_t)SAI_xIMR_CNRDYIE)
532 #define SAI_IT_AFSDET                     ((uint32_t)SAI_xIMR_AFSDETIE)
533 #define SAI_IT_LFSDET                     ((uint32_t)SAI_xIMR_LFSDETIE)
534 
535 /**
536   * @}
537   */
538 
539 /** @defgroup SAI_Block_Flags_Definition  SAI Block Flags Definition
540   * @{
541   */
542 #define SAI_FLAG_OVRUDR                   ((uint32_t)SAI_xSR_OVRUDR)
543 #define SAI_FLAG_MUTEDET                  ((uint32_t)SAI_xSR_MUTEDET)
544 #define SAI_FLAG_WCKCFG                   ((uint32_t)SAI_xSR_WCKCFG)
545 #define SAI_FLAG_FREQ                     ((uint32_t)SAI_xSR_FREQ)
546 #define SAI_FLAG_CNRDY                    ((uint32_t)SAI_xSR_CNRDY)
547 #define SAI_FLAG_AFSDET                   ((uint32_t)SAI_xSR_AFSDET)
548 #define SAI_FLAG_LFSDET                   ((uint32_t)SAI_xSR_LFSDET)
549 
550 /**
551   * @}
552   */
553 
554 /** @defgroup SAI_Block_Fifo_Status_Level   SAI Block Fifo Status Level
555   * @{
556   */
557 #define SAI_FIFOSTATUS_EMPTY              ((uint32_t)0x00000000)
558 #define SAI_FIFOSTATUS_LESS1QUARTERFULL   ((uint32_t)0x00010000)
559 #define SAI_FIFOSTATUS_1QUARTERFULL       ((uint32_t)0x00020000)
560 #define SAI_FIFOSTATUS_HALFFULL           ((uint32_t)0x00030000)
561 #define SAI_FIFOSTATUS_3QUARTERFULL       ((uint32_t)0x00040000)
562 #define SAI_FIFOSTATUS_FULL               ((uint32_t)0x00050000)
563 
564 /**
565   * @}
566   */
567 
568 /**
569   * @}
570   */
571 
572 /* Exported macro ------------------------------------------------------------*/
573 
574 /** @defgroup SAI_Exported_Macros SAI Exported Macros
575  *  @brief macros to handle interrupts and specific configurations
576  * @{
577  */
578 
579 /** @brief Reset SAI handle state
580   * @param  __HANDLE__: specifies the SAI Handle.
581   * @retval None
582   */
583 #define __HAL_SAI_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_SAI_STATE_RESET)
584 
585 /** @brief  Enable or disable the specified SAI interrupts.
586   * @param  __HANDLE__: specifies the SAI Handle.
587   * @param  __INTERRUPT__: specifies the interrupt source to enable or disable.
588   *         This parameter can be one of the following values:
589   *            @arg SAI_IT_OVRUDR: Overrun underrun interrupt enable
590   *            @arg SAI_IT_MUTEDET: Mute detection interrupt enable
591   *            @arg SAI_IT_WCKCFG: Wrong Clock Configuration interrupt enable
592   *            @arg SAI_IT_FREQ: FIFO request interrupt enable
593   *            @arg SAI_IT_CNRDY: Codec not ready interrupt enable
594   *            @arg SAI_IT_AFSDET: Anticipated frame synchronization detection interrupt enable
595   *            @arg SAI_IT_LFSDET: Late frame synchronization detection interrupt enabl
596   * @retval None
597   */
598 
599 #define __HAL_SAI_ENABLE_IT(__HANDLE__, __INTERRUPT__)   ((__HANDLE__)->Instance->IMR |= (__INTERRUPT__))
600 #define __HAL_SAI_DISABLE_IT(__HANDLE__, __INTERRUPT__)  ((__HANDLE__)->Instance->IMR &= (~(__INTERRUPT__)))
601 
602 /** @brief  Check if the specified SAI interrupt source is enabled or disabled.
603   * @param  __HANDLE__: specifies the SAI Handle.
604   *         This parameter can be SAI where x: 1, 2, or 3 to select the SAI peripheral.
605   * @param  __INTERRUPT__: specifies the SAI interrupt source to check.
606   *         This parameter can be one of the following values:
607   *            @arg SAI_IT_TXE: Tx buffer empty interrupt enable.
608   *            @arg SAI_IT_RXNE: Rx buffer not empty interrupt enable.
609   *            @arg SAI_IT_ERR: Error interrupt enable.
610   * @retval The new state of __INTERRUPT__ (TRUE or FALSE).
611   */
612 #define __HAL_SAI_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->IMR & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
613 
614 /** @brief  Check whether the specified SAI flag is set or not.
615   * @param  __HANDLE__: specifies the SAI Handle.
616   * @param  __FLAG__: specifies the flag to check.
617   *         This parameter can be one of the following values:
618   *            @arg SAI_FLAG_OVRUDR: Overrun underrun flag.
619   *            @arg SAI_FLAG_MUTEDET: Mute detection flag.
620   *            @arg SAI_FLAG_WCKCFG: Wrong Clock Configuration flag.
621   *            @arg SAI_FLAG_FREQ: FIFO request flag.
622   *            @arg SAI_FLAG_CNRDY: Codec not ready flag.
623   *            @arg SAI_FLAG_AFSDET: Anticipated frame synchronization detection flag.
624   *            @arg SAI_FLAG_LFSDET: Late frame synchronization detection flag.
625   * @retval The new state of __FLAG__ (TRUE or FALSE).
626   */
627 #define __HAL_SAI_GET_FLAG(__HANDLE__, __FLAG__) ((((__HANDLE__)->Instance->SR) & (__FLAG__)) == (__FLAG__))
628 
629 /** @brief  Clears the specified SAI pending flag.
630   * @param  __HANDLE__: specifies the SAI Handle.
631   * @param  __FLAG__: specifies the flag to check.
632   *          This parameter can be any combination of the following values:
633   *            @arg SAI_FLAG_OVRUDR: Clear Overrun underrun
634   *            @arg SAI_FLAG_MUTEDET: Clear Mute detection
635   *            @arg SAI_FLAG_WCKCFG: Clear Wrong Clock Configuration
636   *            @arg SAI_FLAG_FREQ: Clear FIFO request
637   *            @arg SAI_FLAG_CNRDY: Clear Codec not ready
638   *            @arg SAI_FLAG_AFSDET: Clear Anticipated frame synchronization detection
639   *            @arg SAI_FLAG_LFSDET: Clear Late frame synchronization detection
640   *
641   * @retval None
642   */
643 #define __HAL_SAI_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->CLRFR = (__FLAG__))
644 
645 #define __HAL_SAI_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |=  SAI_xCR1_SAIEN)
646 #define __HAL_SAI_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &=  ~SAI_xCR1_SAIEN)
647 
648 /**
649  * @}
650  */
651 
652 /* Include RCC SAI Extension module */
653 #include "stm32f7xx_hal_sai_ex.h"
654 
655 /* Exported functions --------------------------------------------------------*/
656 
657 /** @addtogroup SAI_Exported_Functions
658   * @{
659   */
660 
661 /* Initialization/de-initialization functions  **********************************/
662 /** @addtogroup SAI_Exported_Functions_Group1
663   * @{
664   */
665 HAL_StatusTypeDef HAL_SAI_InitProtocol(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot);
666 HAL_StatusTypeDef HAL_SAI_Init(SAI_HandleTypeDef *hsai);
667 HAL_StatusTypeDef HAL_SAI_DeInit (SAI_HandleTypeDef *hsai);
668 void HAL_SAI_MspInit(SAI_HandleTypeDef *hsai);
669 void HAL_SAI_MspDeInit(SAI_HandleTypeDef *hsai);
670 
671 /**
672   * @}
673   */
674 
675 /* I/O operation functions  *****************************************************/
676 /** @addtogroup SAI_Exported_Functions_Group2
677   * @{
678   */
679 /* Blocking mode: Polling */
680 HAL_StatusTypeDef HAL_SAI_Transmit(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size, uint32_t Timeout);
681 HAL_StatusTypeDef HAL_SAI_Receive(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size, uint32_t Timeout);
682 
683 /* Non-Blocking mode: Interrupt */
684 HAL_StatusTypeDef HAL_SAI_Transmit_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size);
685 HAL_StatusTypeDef HAL_SAI_Receive_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size);
686 
687 /* Non-Blocking mode: DMA */
688 HAL_StatusTypeDef HAL_SAI_Transmit_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size);
689 HAL_StatusTypeDef HAL_SAI_Receive_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size);
690 HAL_StatusTypeDef HAL_SAI_DMAPause(SAI_HandleTypeDef *hsai);
691 HAL_StatusTypeDef HAL_SAI_DMAResume(SAI_HandleTypeDef *hsai);
692 HAL_StatusTypeDef HAL_SAI_DMAStop(SAI_HandleTypeDef *hsai);
693 
694 /* Abort function */
695 HAL_StatusTypeDef HAL_SAI_Abort(SAI_HandleTypeDef *hsai);
696 
697 /* Mute management */
698 HAL_StatusTypeDef HAL_SAI_EnableTxMuteMode(SAI_HandleTypeDef *hsai, uint16_t val);
699 HAL_StatusTypeDef HAL_SAI_DisableTxMuteMode(SAI_HandleTypeDef *hsai);
700 HAL_StatusTypeDef HAL_SAI_EnableRxMuteMode(SAI_HandleTypeDef *hsai, SAIcallback callback, uint16_t counter);
701 HAL_StatusTypeDef HAL_SAI_DisableRxMuteMode(SAI_HandleTypeDef *hsai);
702 
703 /* SAI IRQHandler and Callbacks used in non blocking modes (Interrupt and DMA) */
704 void HAL_SAI_IRQHandler(SAI_HandleTypeDef *hsai);
705 void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai);
706 void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai);
707 void HAL_SAI_RxHalfCpltCallback(SAI_HandleTypeDef *hsai);
708 void HAL_SAI_RxCpltCallback(SAI_HandleTypeDef *hsai);
709 void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai);
710 /**
711   * @}
712   */
713 
714 /** @addtogroup SAI_Exported_Functions_Group3
715   * @{
716   */
717 /* Peripheral State functions  **************************************************/
718 HAL_SAI_StateTypeDef HAL_SAI_GetState(SAI_HandleTypeDef *hsai);
719 uint32_t HAL_SAI_GetError(SAI_HandleTypeDef *hsai);
720 /**
721   * @}
722   */
723 
724 /**
725   * @}
726   */
727 
728 /* Private types -------------------------------------------------------------*/
729 /** @defgroup SAI_Private_Types SAI Private Types
730   * @{
731   */
732 
733 /**
734   * @}
735   */
736 
737 /* Private variables ---------------------------------------------------------*/
738 /** @defgroup SAI_Private_Variables SAI Private Variables
739   * @{
740   */
741 
742 /**
743   * @}
744   */
745 
746 /* Private constants ---------------------------------------------------------*/
747 /** @defgroup SAI_Private_Constants SAI Private Constants
748   * @{
749   */
750 
751 /**
752   * @}
753   */
754 
755 /* Private macros ------------------------------------------------------------*/
756 /** @addtogroup SAI_Private_Macros
757   * @{
758   */
759 #define IS_SAI_BLOCK_SYNCEXT(STATE)   (((STATE) == SAI_SYNCEXT_DISABLE)           ||\
760                                        ((STATE) == SAI_SYNCEXT_IN_ENABLE)         ||\
761                                        ((STATE) == SAI_SYNCEXT_OUTBLOCKA_ENABLE)  ||\
762                                        ((STATE) == SAI_SYNCEXT_OUTBLOCKB_ENABLE))
763 
764 #define IS_SAI_SUPPORTED_PROTOCOL(PROTOCOL)   (((PROTOCOL) == SAI_I2S_STANDARD)     ||\
765                                                ((PROTOCOL) == SAI_I2S_MSBJUSTIFIED) ||\
766                                                ((PROTOCOL) == SAI_I2S_LSBJUSTIFIED) ||\
767                                                ((PROTOCOL) == SAI_PCM_LONG)         ||\
768                                                ((PROTOCOL) == SAI_PCM_SHORT))
769 
770 #define IS_SAI_PROTOCOL_DATASIZE(DATASIZE)   (((DATASIZE) == SAI_PROTOCOL_DATASIZE_16BIT)         ||\
771                                               ((DATASIZE) == SAI_PROTOCOL_DATASIZE_16BITEXTENDED) ||\
772                                               ((DATASIZE) == SAI_PROTOCOL_DATASIZE_24BIT)         ||\
773                                               ((DATASIZE) == SAI_PROTOCOL_DATASIZE_32BIT))
774 
775 #define IS_SAI_CLK_SOURCE(SOURCE) (((SOURCE) == SAI_CLKSOURCE_PLLSAI) ||\
776                                    ((SOURCE) == SAI_CLKSOURCE_PLLI2S) ||\
777                                    ((SOURCE) == SAI_CLKSOURCE_EXT))
778 
779 #define IS_SAI_AUDIO_FREQUENCY(AUDIO) (((AUDIO) == SAI_AUDIO_FREQUENCY_192K) || ((AUDIO) == SAI_AUDIO_FREQUENCY_96K) || \
780                                        ((AUDIO) == SAI_AUDIO_FREQUENCY_48K)  || ((AUDIO) == SAI_AUDIO_FREQUENCY_44K) || \
781                                        ((AUDIO) == SAI_AUDIO_FREQUENCY_32K)  || ((AUDIO) == SAI_AUDIO_FREQUENCY_22K) || \
782                                        ((AUDIO) == SAI_AUDIO_FREQUENCY_16K)  || ((AUDIO) == SAI_AUDIO_FREQUENCY_11K) || \
783                                        ((AUDIO) == SAI_AUDIO_FREQUENCY_8K)   || ((AUDIO) == SAI_AUDIO_FREQUENCY_MCKDIV))
784 
785 #define IS_SAI_BLOCK_MODE(MODE)    (((MODE) == SAI_MODEMASTER_TX) || \
786                                     ((MODE) == SAI_MODEMASTER_RX) || \
787                                     ((MODE) == SAI_MODESLAVE_TX)  || \
788                                     ((MODE) == SAI_MODESLAVE_RX))
789 
790 #define IS_SAI_BLOCK_PROTOCOL(PROTOCOL) (((PROTOCOL) == SAI_FREE_PROTOCOL)  || \
791                                          ((PROTOCOL) == SAI_AC97_PROTOCOL)  || \
792                                          ((PROTOCOL) == SAI_SPDIF_PROTOCOL))
793 
794 #define IS_SAI_BLOCK_DATASIZE(DATASIZE) (((DATASIZE) == SAI_DATASIZE_8)  || \
795                                          ((DATASIZE) == SAI_DATASIZE_10) || \
796                                          ((DATASIZE) == SAI_DATASIZE_16) || \
797                                          ((DATASIZE) == SAI_DATASIZE_20) || \
798                                          ((DATASIZE) == SAI_DATASIZE_24) || \
799                                          ((DATASIZE) == SAI_DATASIZE_32))
800 
801 #define IS_SAI_BLOCK_FIRST_BIT(BIT) (((BIT) == SAI_FIRSTBIT_MSB) || \
802                                      ((BIT) == SAI_FIRSTBIT_LSB))
803 
804 #define IS_SAI_BLOCK_CLOCK_STROBING(CLOCK) (((CLOCK) == SAI_CLOCKSTROBING_FALLINGEDGE) || \
805                                             ((CLOCK) == SAI_CLOCKSTROBING_RISINGEDGE))
806 
807 #define IS_SAI_BLOCK_SYNCHRO(SYNCHRO) (((SYNCHRO) == SAI_ASYNCHRONOUS) || \
808                                        ((SYNCHRO) == SAI_SYNCHRONOUS)  || \
809                                        ((SYNCHRO) == SAI_SYNCHRONOUS_EXT))
810 
811 #define IS_SAI_BLOCK_OUTPUT_DRIVE(DRIVE) (((DRIVE) == SAI_OUTPUTDRIVE_DISABLE) || \
812                                           ((DRIVE) == SAI_OUTPUTDRIVE_ENABLE))
813 
814 #define IS_SAI_BLOCK_NODIVIDER(NODIVIDER) (((NODIVIDER) == SAI_MASTERDIVIDER_ENABLE) || \
815                                            ((NODIVIDER) == SAI_MASTERDIVIDER_DISABLE))
816 
817 #define IS_SAI_BLOCK_FIFO_STATUS(STATUS) (((STATUS) == SAI_FIFOSTATUS_LESS1QUARTERFULL ) || \
818                                           ((STATUS) == SAI_FIFOSTATUS_HALFFULL)          || \
819                                           ((STATUS) == SAI_FIFOSTATUS_1QUARTERFULL)      || \
820                                           ((STATUS) == SAI_FIFOSTATUS_3QUARTERFULL)     || \
821                                           ((STATUS) == SAI_FIFOSTATUS_FULL)              || \
822                                           ((STATUS) == SAI_FIFOSTATUS_EMPTY))
823 
824 #define IS_SAI_BLOCK_MUTE_COUNTER(COUNTER) ((COUNTER) <= 63)
825 
826 #define IS_SAI_BLOCK_MUTE_VALUE(VALUE)    (((VALUE) == SAI_ZERO_VALUE)     || \
827                                            ((VALUE) == SAI_LAST_SENT_VALUE))
828 
829 #define IS_SAI_BLOCK_COMPANDING_MODE(MODE)    (((MODE) == SAI_NOCOMPANDING)         || \
830                                                ((MODE) == SAI_ULAW_1CPL_COMPANDING) || \
831                                                ((MODE) == SAI_ALAW_1CPL_COMPANDING) || \
832                                                ((MODE) == SAI_ULAW_2CPL_COMPANDING) || \
833                                                ((MODE) == SAI_ALAW_2CPL_COMPANDING))
834 
835 #define IS_SAI_BLOCK_FIFO_THRESHOLD(THRESHOLD) (((THRESHOLD) == SAI_FIFOTHRESHOLD_EMPTY)   || \
836                                                 ((THRESHOLD) == SAI_FIFOTHRESHOLD_1QF)     || \
837                                                 ((THRESHOLD) == SAI_FIFOTHRESHOLD_HF)      || \
838                                                 ((THRESHOLD) == SAI_FIFOTHRESHOLD_3QF)     || \
839                                                 ((THRESHOLD) == SAI_FIFOTHRESHOLD_FULL))
840 
841 #define IS_SAI_BLOCK_TRISTATE_MANAGEMENT(STATE) (((STATE) == SAI_OUTPUT_NOTRELEASED) ||\
842                                                  ((STATE) == SAI_OUTPUT_RELEASED))
843 
844 #define IS_SAI_MONO_STEREO_MODE(MODE) (((MODE) == SAI_MONOMODE) ||\
845                                        ((MODE) == SAI_STEREOMODE))
846 
847 #define IS_SAI_SLOT_ACTIVE(ACTIVE)  ((((ACTIVE) >> 16 )  > 0) && (((ACTIVE) >> 16 )  <= (SAI_SLOTACTIVE_ALL >> 16)))
848 
849 #define IS_SAI_BLOCK_SLOT_NUMBER(NUMBER) ((1 <= (NUMBER)) && ((NUMBER) <= 16))
850 
851 #define IS_SAI_BLOCK_SLOT_SIZE(SIZE) (((SIZE) == SAI_SLOTSIZE_DATASIZE) || \
852                                       ((SIZE) == SAI_SLOTSIZE_16B)      || \
853                                       ((SIZE) == SAI_SLOTSIZE_32B))
854 
855 #define IS_SAI_BLOCK_FIRSTBIT_OFFSET(OFFSET) ((OFFSET) <= 24)
856 
857 #define IS_SAI_BLOCK_FS_OFFSET(OFFSET) (((OFFSET) == SAI_FS_FIRSTBIT) || \
858                                         ((OFFSET) == SAI_FS_BEFOREFIRSTBIT))
859 
860 #define IS_SAI_BLOCK_FS_POLARITY(POLARITY) (((POLARITY) == SAI_FS_ACTIVE_LOW) || \
861                                             ((POLARITY) == SAI_FS_ACTIVE_HIGH))
862 
863 #define IS_SAI_BLOCK_FS_DEFINITION(DEFINITION) (((DEFINITION) == SAI_FS_STARTFRAME) || \
864                                                 ((DEFINITION) == SAI_FS_CHANNEL_IDENTIFICATION))
865 
866 #define IS_SAI_BLOCK_MASTER_DIVIDER(DIVIDER) ((DIVIDER) <= 15)
867 
868 #define IS_SAI_BLOCK_FRAME_LENGTH(LENGTH) ((8 <= (LENGTH)) && ((LENGTH) <= 256))
869 
870 #define IS_SAI_BLOCK_ACTIVE_FRAME(LENGTH) ((1 <= (LENGTH)) && ((LENGTH) <= 128))
871 
872 /**
873   * @}
874   */
875 
876 /* Private functions ---------------------------------------------------------*/
877 /** @defgroup SAI_Private_Functions SAI Private Functions
878   * @{
879   */
880 
881 /**
882   * @}
883   */
884 
885 /**
886   * @}
887   */
888 
889 /**
890   * @}
891   */
892 
893 #ifdef __cplusplus
894 }
895 #endif
896 
897 #endif /* __STM32F7xx_HAL_SAI_H */
898 
899 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
900