STM32F439xx HAL User Manual
stm32f4xx_hal_sai.h
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_hal_sai.h
00004   * @author  MCD Application Team
00005   * @brief   Header file of SAI HAL module.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00010   *
00011   * Redistribution and use in source and binary forms, with or without modification,
00012   * are permitted provided that the following conditions are met:
00013   *   1. Redistributions of source code must retain the above copyright notice,
00014   *      this list of conditions and the following disclaimer.
00015   *   2. Redistributions in binary form must reproduce the above copyright notice,
00016   *      this list of conditions and the following disclaimer in the documentation
00017   *      and/or other materials provided with the distribution.
00018   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00019   *      may be used to endorse or promote products derived from this software
00020   *      without specific prior written permission.
00021   *
00022   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00025   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00026   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00030   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032   *
00033   ******************************************************************************
00034   */
00035 
00036 /* Define to prevent recursive inclusion -------------------------------------*/
00037 #ifndef __STM32F4xx_HAL_SAI_H
00038 #define __STM32F4xx_HAL_SAI_H
00039 
00040 #ifdef __cplusplus
00041  extern "C" {
00042 #endif
00043 
00044 /* Includes ------------------------------------------------------------------*/
00045 #include "stm32f4xx_hal_def.h"
00046 
00047 /** @addtogroup STM32F4xx_HAL_Driver
00048   * @{
00049   */
00050 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
00051     defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) || \
00052     defined(STM32F423xx)
00053 
00054 /** @addtogroup SAI
00055   * @{
00056   */
00057 
00058 /* Exported types ------------------------------------------------------------*/
00059 /** @defgroup SAI_Exported_Types SAI Exported Types
00060   * @{
00061   */
00062 
00063 /**
00064   * @brief  HAL State structures definition
00065   */
00066 typedef enum
00067 {
00068   HAL_SAI_STATE_RESET      = 0x00U,  /*!< SAI not yet initialized or disabled                */
00069   HAL_SAI_STATE_READY      = 0x01U,  /*!< SAI initialized and ready for use                  */
00070   HAL_SAI_STATE_BUSY       = 0x02U,  /*!< SAI internal process is ongoing                    */
00071   HAL_SAI_STATE_BUSY_TX    = 0x12U,  /*!< Data transmission process is ongoing               */
00072   HAL_SAI_STATE_BUSY_RX    = 0x22U,  /*!< Data reception process is ongoing                  */
00073   HAL_SAI_STATE_TIMEOUT    = 0x03U,  /*!< SAI timeout state                                  */
00074   HAL_SAI_STATE_ERROR      = 0x04U   /*!< SAI error state                                    */
00075 }HAL_SAI_StateTypeDef;
00076 
00077 /**
00078   * @brief  SAI Callback prototype
00079   */
00080 typedef void (*SAIcallback)(void);
00081 
00082 /** @defgroup SAI_Init_Structure_definition SAI Init Structure definition
00083   * @brief  SAI Init Structure definition
00084   * @{
00085   */
00086 typedef struct
00087 {
00088   uint32_t AudioMode;           /*!< Specifies the SAI Block audio Mode.
00089                                      This parameter can be a value of @ref SAI_Block_Mode                      */
00090 
00091   uint32_t Synchro;             /*!< Specifies SAI Block synchronization
00092                                      This parameter can be a value of @ref SAI_Block_Synchronization           */
00093  
00094   uint32_t SynchroExt;          /*!< Specifies SAI external output synchronization, this setup is common
00095                                      for BlockA and BlockB
00096                                      This parameter can be a value of @ref SAI_Block_SyncExt
00097                                      @note: If both audio blocks of same SAI are used, this parameter has
00098                                             to be set to the same value for each audio block                   */
00099 
00100   uint32_t OutputDrive;         /*!< Specifies when SAI Block outputs are driven.
00101                                      This parameter can be a value of @ref SAI_Block_Output_Drive
00102                                      @note this value has to be set before enabling the audio block
00103                                      but after the audio block configuration.                                  */
00104 
00105   uint32_t NoDivider;           /*!< Specifies whether master clock will be divided or not.
00106                                      This parameter can be a value of @ref SAI_Block_NoDivider
00107                                      @note  If bit NODIV in the SAI_xCR1 register is cleared, the frame length
00108                                             should be aligned to a number equal to a power of 2, from 8 to 256.
00109                                             If bit NODIV in the SAI_xCR1 register is set, the frame length can
00110                                             take any of the values without constraint since the input clock of
00111                                             the audio block should be equal to the bit clock.
00112                                              There is no MCLK_x clock which can be output.                     */
00113   
00114   uint32_t FIFOThreshold;       /*!< Specifies SAI Block FIFO threshold.
00115                                      This parameter can be a value of @ref SAI_Block_Fifo_Threshold            */
00116 
00117   uint32_t ClockSource;         /*!< Specifies the SAI Block x Clock source.
00118                                      This parameter is not used for STM32F446xx devices.                       */
00119 
00120   uint32_t AudioFrequency;      /*!< Specifies the audio frequency sampling.
00121                                      This parameter can be a value of @ref SAI_Audio_Frequency                 */
00122 
00123   uint32_t Mckdiv;              /*!< Specifies the master clock divider, the parameter will be used if for 
00124                                      AudioFrequency the user choice
00125                                      This parameter must be a number between Min_Data = 0 and Max_Data = 15    */
00126 
00127   uint32_t MonoStereoMode;      /*!< Specifies if the mono or stereo mode is selected.
00128                                      This parameter can be a value of @ref SAI_Mono_Stereo_Mode                */
00129 
00130   uint32_t CompandingMode;      /*!< Specifies the companding mode type.
00131                                      This parameter can be a value of @ref SAI_Block_Companding_Mode           */
00132 
00133   uint32_t TriState;            /*!< Specifies the companding mode type.
00134                                      This parameter can be a value of @ref SAI_TRIState_Management             */
00135 
00136   /* This part of the structure is automatically filled if your are using the high level intialisation
00137      function HAL_SAI_InitProtocol                                                                             */
00138 
00139   uint32_t Protocol;        /*!< Specifies the SAI Block protocol.
00140                                  This parameter can be a value of @ref SAI_Block_Protocol                      */
00141  
00142   uint32_t DataSize;        /*!< Specifies the SAI Block data size.
00143                                  This parameter can be a value of @ref SAI_Block_Data_Size                     */
00144 
00145   uint32_t FirstBit;        /*!< Specifies whether data transfers start from MSB or LSB bit.
00146                                  This parameter can be a value of @ref SAI_Block_MSB_LSB_transmission          */
00147 
00148   uint32_t ClockStrobing;   /*!< Specifies the SAI Block clock strobing edge sensitivity.
00149                                  This parameter can be a value of @ref SAI_Block_Clock_Strobing                */
00150 }SAI_InitTypeDef;
00151 /**
00152   * @}
00153   */
00154 
00155 /** @defgroup SAI_Frame_Structure_definition SAI Frame Structure definition
00156   * @brief  SAI Frame Init structure definition
00157   * @{
00158   */
00159 typedef struct
00160 {
00161   uint32_t FrameLength;         /*!< Specifies the Frame length, the number of SCK clocks for each audio frame.
00162                                      This parameter must be a number between Min_Data = 8 and Max_Data = 256.
00163                                      @note  If master clock MCLK_x pin is declared as an output, the frame length
00164                                             should be aligned to a number equal to power of 2 in order to keep 
00165                                             in an audio frame, an integer number of MCLK pulses by bit Clock. */
00166 
00167   uint32_t ActiveFrameLength;  /*!< Specifies the Frame synchronization active level length.
00168                                     This Parameter specifies the length in number of bit clock (SCK + 1)
00169                                     of the active level of FS signal in audio frame.
00170                                     This parameter must be a number between Min_Data = 1 and Max_Data = 128   */
00171 
00172   uint32_t FSDefinition;       /*!< Specifies the Frame synchronization definition.
00173                                     This parameter can be a value of @ref SAI_Block_FS_Definition             */
00174 
00175   uint32_t FSPolarity;         /*!< Specifies the Frame synchronization Polarity.
00176                                     This parameter can be a value of @ref SAI_Block_FS_Polarity               */
00177 
00178   uint32_t FSOffset;           /*!< Specifies the Frame synchronization Offset.
00179                                     This parameter can be a value of @ref SAI_Block_FS_Offset                 */
00180 }SAI_FrameInitTypeDef;
00181 /**
00182   * @}
00183   */
00184 
00185 /** @defgroup SAI_Slot_Structure_definition SAI Slot Structure definition
00186   * @brief   SAI Block Slot Init Structure definition
00187   * @{
00188   */
00189 typedef struct
00190 {
00191   uint32_t FirstBitOffset;  /*!< Specifies the position of first data transfer bit in the slot.
00192                                  This parameter must be a number between Min_Data = 0 and Max_Data = 24 */
00193 
00194   uint32_t SlotSize;        /*!< Specifies the Slot Size.
00195                                  This parameter can be a value of @ref SAI_Block_Slot_Size              */
00196 
00197   uint32_t SlotNumber;      /*!< Specifies the number of slot in the audio frame.
00198                                  This parameter must be a number between Min_Data = 1 and Max_Data = 16 */
00199 
00200   uint32_t SlotActive;      /*!< Specifies the slots in audio frame that will be activated.
00201                                  This parameter can be a value of @ref SAI_Block_Slot_Active            */
00202 }SAI_SlotInitTypeDef;
00203 
00204 /**
00205   * @}
00206   */
00207 
00208 /** @defgroup SAI_Handle_Structure_definition SAI Handle Structure definition
00209   * @brief  SAI handle Structure definition
00210   * @{
00211   */
00212 typedef struct __SAI_HandleTypeDef
00213 {
00214   SAI_Block_TypeDef         *Instance;  /*!< SAI Blockx registers base address        */
00215 
00216   SAI_InitTypeDef           Init;       /*!< SAI communication parameters             */
00217 
00218   SAI_FrameInitTypeDef      FrameInit;  /*!< SAI Frame configuration parameters       */
00219 
00220   SAI_SlotInitTypeDef       SlotInit;   /*!< SAI Slot configuration parameters        */
00221 
00222   uint8_t                  *pBuffPtr;  /*!< Pointer to SAI transfer Buffer            */
00223 
00224   uint16_t                  XferSize;  /*!< SAI transfer size                         */
00225 
00226   uint16_t                  XferCount; /*!< SAI transfer counter                      */
00227 
00228   DMA_HandleTypeDef         *hdmatx;     /*!< SAI Tx DMA handle parameters            */
00229 
00230   DMA_HandleTypeDef         *hdmarx;     /*!< SAI Rx DMA handle parameters            */
00231 
00232   SAIcallback               mutecallback;/*!< SAI mute callback                       */
00233 
00234   void (*InterruptServiceRoutine)(struct __SAI_HandleTypeDef *hsai); /* function pointer for IRQ handler   */
00235 
00236   HAL_LockTypeDef           Lock;        /*!< SAI locking object                      */
00237 
00238   __IO HAL_SAI_StateTypeDef State;       /*!< SAI communication state                 */
00239 
00240   __IO uint32_t             ErrorCode;   /*!< SAI Error code                          */
00241 }SAI_HandleTypeDef;
00242 /**
00243   * @}
00244   */
00245 
00246 /**
00247   * @}
00248   */
00249 
00250 /* Exported constants --------------------------------------------------------*/
00251 
00252 /** @defgroup SAI_Exported_Constants SAI Exported Constants
00253   * @{
00254   */
00255 
00256 /** @defgroup SAI_Error_Code SAI Error Code 
00257   * @{
00258   */
00259 #define HAL_SAI_ERROR_NONE    0x00000000U  /*!< No error                                    */
00260 #define HAL_SAI_ERROR_OVR     0x00000001U  /*!< Overrun Error                               */
00261 #define HAL_SAI_ERROR_UDR     0x00000002U  /*!< Underrun error                              */
00262 #define HAL_SAI_ERROR_AFSDET  0x00000004U  /*!< Anticipated Frame synchronisation detection */
00263 #define HAL_SAI_ERROR_LFSDET  0x00000008U  /*!< Late Frame synchronisation detection        */
00264 #define HAL_SAI_ERROR_CNREADY 0x00000010U  /*!< codec not ready                             */
00265 #define HAL_SAI_ERROR_WCKCFG  0x00000020U  /*!< Wrong clock configuration                   */
00266 #define HAL_SAI_ERROR_TIMEOUT 0x00000040U  /*!< Timeout error                               */
00267 #define HAL_SAI_ERROR_DMA     0x00000080U  /*!< DMA error                                   */
00268 /**
00269   * @}
00270   */
00271 
00272 /** @defgroup SAI_Block_SyncExt SAI External synchronisation
00273   * @{
00274   */
00275 #define SAI_SYNCEXT_DISABLE          0U
00276 #define SAI_SYNCEXT_OUTBLOCKA_ENABLE 1U
00277 #define SAI_SYNCEXT_OUTBLOCKB_ENABLE 2U
00278 /**
00279   * @}
00280   */
00281 
00282 /** @defgroup SAI_Protocol SAI Supported protocol
00283   * @{
00284   */
00285 #define SAI_I2S_STANDARD      0U
00286 #define SAI_I2S_MSBJUSTIFIED  1U
00287 #define SAI_I2S_LSBJUSTIFIED  2U
00288 #define SAI_PCM_LONG          3U
00289 #define SAI_PCM_SHORT         4U
00290 /**
00291   * @}
00292   */
00293 
00294 /** @defgroup SAI_Protocol_DataSize SAI protocol data size
00295   * @{
00296   */
00297 #define SAI_PROTOCOL_DATASIZE_16BIT         0U
00298 #define SAI_PROTOCOL_DATASIZE_16BITEXTENDED 1U
00299 #define SAI_PROTOCOL_DATASIZE_24BIT         2U
00300 #define SAI_PROTOCOL_DATASIZE_32BIT         3U
00301 /**
00302   * @}
00303   */
00304 
00305 /** @defgroup SAI_Audio_Frequency SAI Audio Frequency
00306   * @{
00307   */
00308 #define SAI_AUDIO_FREQUENCY_192K          192000U
00309 #define SAI_AUDIO_FREQUENCY_96K           96000U
00310 #define SAI_AUDIO_FREQUENCY_48K           48000U
00311 #define SAI_AUDIO_FREQUENCY_44K           44100U
00312 #define SAI_AUDIO_FREQUENCY_32K           32000U
00313 #define SAI_AUDIO_FREQUENCY_22K           22050U
00314 #define SAI_AUDIO_FREQUENCY_16K           16000U
00315 #define SAI_AUDIO_FREQUENCY_11K           11025U
00316 #define SAI_AUDIO_FREQUENCY_8K            8000U
00317 #define SAI_AUDIO_FREQUENCY_MCKDIV        0U
00318 /**
00319   * @}
00320   */
00321 
00322 /** @defgroup SAI_Block_Mode SAI Block Mode
00323   * @{
00324   */
00325 #define SAI_MODEMASTER_TX         0x00000000U
00326 #define SAI_MODEMASTER_RX         ((uint32_t)SAI_xCR1_MODE_0)
00327 #define SAI_MODESLAVE_TX          ((uint32_t)SAI_xCR1_MODE_1)
00328 #define SAI_MODESLAVE_RX          ((uint32_t)(SAI_xCR1_MODE_1 | SAI_xCR1_MODE_0))
00329 /**
00330   * @}
00331   */
00332 
00333 /** @defgroup SAI_Block_Protocol SAI Block Protocol
00334   * @{
00335   */
00336 #define SAI_FREE_PROTOCOL                 0x00000000U
00337 #define SAI_SPDIF_PROTOCOL                ((uint32_t)SAI_xCR1_PRTCFG_0)
00338 #define SAI_AC97_PROTOCOL                 ((uint32_t)SAI_xCR1_PRTCFG_1)
00339 /**
00340   * @}
00341   */
00342 
00343 /** @defgroup SAI_Block_Data_Size SAI Block Data Size
00344   * @{
00345   */
00346 #define SAI_DATASIZE_8                   ((uint32_t)SAI_xCR1_DS_1)
00347 #define SAI_DATASIZE_10                  ((uint32_t)(SAI_xCR1_DS_1 | SAI_xCR1_DS_0))
00348 #define SAI_DATASIZE_16                  ((uint32_t)SAI_xCR1_DS_2)
00349 #define SAI_DATASIZE_20                  ((uint32_t)(SAI_xCR1_DS_2 | SAI_xCR1_DS_0))
00350 #define SAI_DATASIZE_24                  ((uint32_t)(SAI_xCR1_DS_2 | SAI_xCR1_DS_1))
00351 #define SAI_DATASIZE_32                  ((uint32_t)(SAI_xCR1_DS_2 | SAI_xCR1_DS_1 | SAI_xCR1_DS_0))
00352 /**
00353   * @}
00354   */
00355 
00356 /** @defgroup SAI_Block_MSB_LSB_transmission SAI Block MSB LSB transmission
00357   * @{
00358   */
00359 #define SAI_FIRSTBIT_MSB                  0x00000000U
00360 #define SAI_FIRSTBIT_LSB                  ((uint32_t)SAI_xCR1_LSBFIRST)
00361 /**
00362   * @}
00363   */
00364 
00365 /** @defgroup SAI_Block_Clock_Strobing SAI Block Clock Strobing
00366   * @{
00367   */
00368 #define SAI_CLOCKSTROBING_FALLINGEDGE     0U
00369 #define SAI_CLOCKSTROBING_RISINGEDGE      1U
00370 /**
00371   * @}
00372   */
00373 
00374 /** @defgroup SAI_Block_Synchronization SAI Block Synchronization
00375   * @{
00376   */
00377 #define SAI_ASYNCHRONOUS                  0U /*!< Asynchronous                             */
00378 #define SAI_SYNCHRONOUS                   1U /*!< Synchronous with other block of same SAI */
00379 #define SAI_SYNCHRONOUS_EXT_SAI1          2U /*!< Synchronous with other SAI, SAI1         */
00380 #define SAI_SYNCHRONOUS_EXT_SAI2          3U /*!< Synchronous with other SAI, SAI2         */
00381 /**
00382   * @}
00383   */
00384 
00385 /** @defgroup SAI_Block_Output_Drive SAI Block Output Drive 
00386   * @{
00387   */
00388 #define SAI_OUTPUTDRIVE_DISABLE          0x00000000U
00389 #define SAI_OUTPUTDRIVE_ENABLE           ((uint32_t)SAI_xCR1_OUTDRIV)
00390 /**
00391   * @}
00392   */
00393 
00394 /** @defgroup SAI_Block_NoDivider SAI Block NoDivider
00395   * @{
00396   */
00397 #define SAI_MASTERDIVIDER_ENABLE         0x00000000U
00398 #define SAI_MASTERDIVIDER_DISABLE        ((uint32_t)SAI_xCR1_NODIV)
00399 /**
00400   * @}
00401   */
00402 
00403 /** @defgroup SAI_Block_FS_Definition SAI Block FS Definition
00404   * @{
00405   */
00406 #define SAI_FS_STARTFRAME                 0x00000000U
00407 #define SAI_FS_CHANNEL_IDENTIFICATION     ((uint32_t)SAI_xFRCR_FSDEF)
00408 /**
00409   * @}
00410   */
00411 
00412 /** @defgroup SAI_Block_FS_Polarity SAI Block FS Polarity 
00413   * @{
00414   */
00415 #define SAI_FS_ACTIVE_LOW                  0x00000000U
00416 #define SAI_FS_ACTIVE_HIGH                 ((uint32_t)SAI_xFRCR_FSPOL)
00417 /**
00418   * @}
00419   */
00420 
00421 /** @defgroup SAI_Block_FS_Offset SAI Block FS Offset 
00422   * @{
00423   */
00424 #define SAI_FS_FIRSTBIT                   0x00000000U
00425 #define SAI_FS_BEFOREFIRSTBIT             ((uint32_t)SAI_xFRCR_FSOFF)
00426 /**
00427   * @}
00428   */
00429 
00430 /** @defgroup SAI_Block_Slot_Size SAI Block Slot Size
00431   * @{
00432   */
00433 #define SAI_SLOTSIZE_DATASIZE             0x00000000U  
00434 #define SAI_SLOTSIZE_16B                  ((uint32_t)SAI_xSLOTR_SLOTSZ_0)
00435 #define SAI_SLOTSIZE_32B                  ((uint32_t)SAI_xSLOTR_SLOTSZ_1)
00436 /**
00437   * @}
00438   */
00439 
00440 /** @defgroup SAI_Block_Slot_Active SAI Block Slot Active
00441   * @{
00442   */
00443 #define SAI_SLOT_NOTACTIVE           0x00000000U
00444 #define SAI_SLOTACTIVE_0             0x00000001U
00445 #define SAI_SLOTACTIVE_1             0x00000002U
00446 #define SAI_SLOTACTIVE_2             0x00000004U
00447 #define SAI_SLOTACTIVE_3             0x00000008U
00448 #define SAI_SLOTACTIVE_4             0x00000010U
00449 #define SAI_SLOTACTIVE_5             0x00000020U
00450 #define SAI_SLOTACTIVE_6             0x00000040U
00451 #define SAI_SLOTACTIVE_7             0x00000080U
00452 #define SAI_SLOTACTIVE_8             0x00000100U
00453 #define SAI_SLOTACTIVE_9             0x00000200U
00454 #define SAI_SLOTACTIVE_10            0x00000400U
00455 #define SAI_SLOTACTIVE_11            0x00000800U
00456 #define SAI_SLOTACTIVE_12            0x00001000U
00457 #define SAI_SLOTACTIVE_13            0x00002000U
00458 #define SAI_SLOTACTIVE_14            0x00004000U
00459 #define SAI_SLOTACTIVE_15            0x00008000U
00460 #define SAI_SLOTACTIVE_ALL           0x0000FFFFU
00461 /**
00462   * @}
00463   */
00464 
00465 /** @defgroup SAI_Mono_Stereo_Mode SAI Mono Stereo Mode
00466   * @{
00467   */
00468 #define SAI_STEREOMODE                    0x00000000U
00469 #define SAI_MONOMODE                      ((uint32_t)SAI_xCR1_MONO)
00470 /**
00471   * @}
00472   */
00473 
00474 /** @defgroup SAI_TRIState_Management SAI TRIState Management
00475   * @{
00476   */
00477 #define SAI_OUTPUT_NOTRELEASED              0x00000000U
00478 #define SAI_OUTPUT_RELEASED                 ((uint32_t)SAI_xCR2_TRIS)
00479 /**
00480   * @}
00481   */
00482 
00483 /** @defgroup SAI_Block_Fifo_Threshold SAI Block Fifo Threshold
00484   * @{
00485   */
00486 #define SAI_FIFOTHRESHOLD_EMPTY  0x00000000U
00487 #define SAI_FIFOTHRESHOLD_1QF    ((uint32_t)(SAI_xCR2_FTH_0))
00488 #define SAI_FIFOTHRESHOLD_HF     ((uint32_t)(SAI_xCR2_FTH_1))
00489 #define SAI_FIFOTHRESHOLD_3QF    ((uint32_t)(SAI_xCR2_FTH_1 | SAI_xCR2_FTH_0))
00490 #define SAI_FIFOTHRESHOLD_FULL   ((uint32_t)(SAI_xCR2_FTH_2))
00491 /**
00492   * @}
00493   */
00494 
00495 /** @defgroup SAI_Block_Companding_Mode SAI Block Companding Mode
00496   * @{
00497   */
00498 #define SAI_NOCOMPANDING                 0x00000000U
00499 #define SAI_ULAW_1CPL_COMPANDING         ((uint32_t)(SAI_xCR2_COMP_1))
00500 #define SAI_ALAW_1CPL_COMPANDING         ((uint32_t)(SAI_xCR2_COMP_1 | SAI_xCR2_COMP_0))
00501 #define SAI_ULAW_2CPL_COMPANDING         ((uint32_t)(SAI_xCR2_COMP_1 | SAI_xCR2_CPL))
00502 #define SAI_ALAW_2CPL_COMPANDING         ((uint32_t)(SAI_xCR2_COMP_1 | SAI_xCR2_COMP_0 | SAI_xCR2_CPL))
00503 /**
00504   * @}
00505   */
00506 
00507 /** @defgroup SAI_Block_Mute_Value SAI Block Mute Value
00508   * @{
00509   */
00510 #define SAI_ZERO_VALUE                      0x00000000U
00511 #define SAI_LAST_SENT_VALUE                 ((uint32_t)SAI_xCR2_MUTEVAL)
00512 /**
00513   * @}
00514   */
00515 
00516 /** @defgroup SAI_Block_Interrupts_Definition SAI Block Interrupts Definition
00517   * @{
00518   */
00519 #define SAI_IT_OVRUDR                     ((uint32_t)SAI_xIMR_OVRUDRIE)
00520 #define SAI_IT_MUTEDET                    ((uint32_t)SAI_xIMR_MUTEDETIE)
00521 #define SAI_IT_WCKCFG                     ((uint32_t)SAI_xIMR_WCKCFGIE)
00522 #define SAI_IT_FREQ                       ((uint32_t)SAI_xIMR_FREQIE)
00523 #define SAI_IT_CNRDY                      ((uint32_t)SAI_xIMR_CNRDYIE)
00524 #define SAI_IT_AFSDET                     ((uint32_t)SAI_xIMR_AFSDETIE)
00525 #define SAI_IT_LFSDET                     ((uint32_t)SAI_xIMR_LFSDETIE)
00526 /**
00527   * @}
00528   */
00529 
00530 /** @defgroup SAI_Block_Flags_Definition  SAI Block Flags Definition
00531   * @{
00532   */
00533 #define SAI_FLAG_OVRUDR                   ((uint32_t)SAI_xSR_OVRUDR)
00534 #define SAI_FLAG_MUTEDET                  ((uint32_t)SAI_xSR_MUTEDET)
00535 #define SAI_FLAG_WCKCFG                   ((uint32_t)SAI_xSR_WCKCFG)
00536 #define SAI_FLAG_FREQ                     ((uint32_t)SAI_xSR_FREQ)
00537 #define SAI_FLAG_CNRDY                    ((uint32_t)SAI_xSR_CNRDY)
00538 #define SAI_FLAG_AFSDET                   ((uint32_t)SAI_xSR_AFSDET)
00539 #define SAI_FLAG_LFSDET                   ((uint32_t)SAI_xSR_LFSDET)
00540 /**
00541   * @}
00542   */
00543 
00544 /** @defgroup SAI_Block_Fifo_Status_Level   SAI Block Fifo Status Level
00545   * @{
00546   */
00547 #define SAI_FIFOSTATUS_EMPTY              0x00000000U
00548 #define SAI_FIFOSTATUS_LESS1QUARTERFULL   0x00010000U
00549 #define SAI_FIFOSTATUS_1QUARTERFULL       0x00020000U
00550 #define SAI_FIFOSTATUS_HALFFULL           0x00030000U
00551 #define SAI_FIFOSTATUS_3QUARTERFULL       0x00040000U
00552 #define SAI_FIFOSTATUS_FULL               0x00050000U
00553 /**
00554   * @}
00555   */
00556 
00557 /**
00558   * @}
00559   */
00560 
00561 /* Exported macro ------------------------------------------------------------*/
00562 
00563 /** @defgroup SAI_Exported_Macros SAI Exported Macros
00564  *  @brief macros to handle interrupts and specific configurations
00565  * @{
00566  */
00567 
00568 /** @brief Reset SAI handle state
00569   * @param  __HANDLE__ specifies the SAI Handle.
00570   * @retval NoneS
00571   */
00572 #define __HAL_SAI_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_SAI_STATE_RESET)
00573 
00574 /** @brief  Enable or disable the specified SAI interrupts.
00575   * @param  __HANDLE__ specifies the SAI Handle.
00576   * @param  __INTERRUPT__ specifies the interrupt source to enable or disable.
00577   *         This parameter can be one of the following values:
00578   *            @arg SAI_IT_OVRUDR: Overrun underrun interrupt enable
00579   *            @arg SAI_IT_MUTEDET: Mute detection interrupt enable
00580   *            @arg SAI_IT_WCKCFG: Wrong Clock Configuration interrupt enable
00581   *            @arg SAI_IT_FREQ: FIFO request interrupt enable
00582   *            @arg SAI_IT_CNRDY: Codec not ready interrupt enable
00583   *            @arg SAI_IT_AFSDET: Anticipated frame synchronization detection interrupt enable
00584   *            @arg SAI_IT_LFSDET: Late frame synchronization detection interrupt enable
00585   * @retval None
00586   */
00587 #define __HAL_SAI_ENABLE_IT(__HANDLE__, __INTERRUPT__)   ((__HANDLE__)->Instance->IMR |= (__INTERRUPT__))
00588 #define __HAL_SAI_DISABLE_IT(__HANDLE__, __INTERRUPT__)  ((__HANDLE__)->Instance->IMR &= (~(__INTERRUPT__)))
00589 
00590 /** @brief  Check if the specified SAI interrupt source is enabled or disabled.
00591   * @param  __HANDLE__ specifies the SAI Handle.
00592   *         This parameter can be SAI where x: 1, 2, or 3 to select the SAI peripheral.
00593   * @param  __INTERRUPT__ specifies the SAI interrupt source to check.
00594   *         This parameter can be one of the following values:
00595   *            @arg SAI_IT_OVRUDR: Overrun underrun interrupt enable
00596   *            @arg SAI_IT_MUTEDET: Mute detection interrupt enable
00597   *            @arg SAI_IT_WCKCFG: Wrong Clock Configuration interrupt enable
00598   *            @arg SAI_IT_FREQ: FIFO request interrupt enable
00599   *            @arg SAI_IT_CNRDY: Codec not ready interrupt enable
00600   *            @arg SAI_IT_AFSDET: Anticipated frame synchronization detection interrupt enable
00601   *            @arg SAI_IT_LFSDET: Late frame synchronization detection interrupt enable
00602   * @retval The new state of __INTERRUPT__ (TRUE or FALSE).
00603   */
00604 #define __HAL_SAI_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->IMR & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
00605 
00606 /** @brief  Check whether the specified SAI flag is set or not.
00607   * @param  __HANDLE__ specifies the SAI Handle.
00608   * @param  __FLAG__ specifies the flag to check.
00609   *         This parameter can be one of the following values:
00610   *            @arg SAI_FLAG_OVRUDR: Overrun underrun flag.
00611   *            @arg SAI_FLAG_MUTEDET: Mute detection flag.
00612   *            @arg SAI_FLAG_WCKCFG: Wrong Clock Configuration flag.
00613   *            @arg SAI_FLAG_FREQ: FIFO request flag.
00614   *            @arg SAI_FLAG_CNRDY: Codec not ready flag.
00615   *            @arg SAI_FLAG_AFSDET: Anticipated frame synchronization detection flag.
00616   *            @arg SAI_FLAG_LFSDET: Late frame synchronization detection flag.  
00617   * @retval The new state of __FLAG__ (TRUE or FALSE).
00618   */
00619 #define __HAL_SAI_GET_FLAG(__HANDLE__, __FLAG__) ((((__HANDLE__)->Instance->SR) & (__FLAG__)) == (__FLAG__))
00620 
00621 /** @brief  Clear the specified SAI pending flag.
00622   * @param  __HANDLE__ specifies the SAI Handle.
00623   * @param  __FLAG__ specifies the flag to check.
00624   *          This parameter can be any combination of the following values:
00625   *            @arg SAI_FLAG_OVRUDR: Clear Overrun underrun  
00626   *            @arg SAI_FLAG_MUTEDET: Clear Mute detection 
00627   *            @arg SAI_FLAG_WCKCFG: Clear Wrong Clock Configuration  
00628   *            @arg SAI_FLAG_FREQ: Clear FIFO request   
00629   *            @arg SAI_FLAG_CNRDY: Clear Codec not ready
00630   *            @arg SAI_FLAG_AFSDET: Clear Anticipated frame synchronization detection
00631   *            @arg SAI_FLAG_LFSDET: Clear Late frame synchronization detection
00632   * @retval None
00633   */
00634 #define __HAL_SAI_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->CLRFR = (__FLAG__))
00635 
00636 /** @brief  Enable SAI
00637   * @param  __HANDLE__ specifies the SAI Handle.
00638   * @retval None
00639   */    
00640 #define __HAL_SAI_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |=  SAI_xCR1_SAIEN)
00641 
00642 /** @brief  Disable SAI
00643   * @param  __HANDLE__ specifies the SAI Handle.
00644   * @retval None
00645   */
00646 #define __HAL_SAI_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &=  ~SAI_xCR1_SAIEN)
00647  
00648  /**
00649   * @}
00650   */
00651 
00652 /* Include RCC SAI Extension module */
00653 #include "stm32f4xx_hal_sai_ex.h"
00654 
00655 /* Exported functions --------------------------------------------------------*/
00656 
00657 /** @addtogroup SAI_Exported_Functions
00658   * @{
00659   */
00660 
00661 /* Initialization/de-initialization functions  **********************************/
00662 /** @addtogroup SAI_Exported_Functions_Group1
00663   * @{
00664   */
00665 HAL_StatusTypeDef HAL_SAI_InitProtocol(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot);
00666 HAL_StatusTypeDef HAL_SAI_Init(SAI_HandleTypeDef *hsai);
00667 HAL_StatusTypeDef HAL_SAI_DeInit (SAI_HandleTypeDef *hsai);
00668 void HAL_SAI_MspInit(SAI_HandleTypeDef *hsai);
00669 void HAL_SAI_MspDeInit(SAI_HandleTypeDef *hsai);
00670 
00671 /**
00672   * @}
00673   */
00674 
00675 /* I/O operation functions  *****************************************************/
00676 /** @addtogroup SAI_Exported_Functions_Group2
00677   * @{
00678   */
00679 /* Blocking mode: Polling */
00680 HAL_StatusTypeDef HAL_SAI_Transmit(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size, uint32_t Timeout);
00681 HAL_StatusTypeDef HAL_SAI_Receive(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size, uint32_t Timeout);
00682 
00683 /* Non-Blocking mode: Interrupt */
00684 HAL_StatusTypeDef HAL_SAI_Transmit_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size);
00685 HAL_StatusTypeDef HAL_SAI_Receive_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size);
00686 
00687 /* Non-Blocking mode: DMA */
00688 HAL_StatusTypeDef HAL_SAI_Transmit_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size);
00689 HAL_StatusTypeDef HAL_SAI_Receive_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size);
00690 HAL_StatusTypeDef HAL_SAI_DMAPause(SAI_HandleTypeDef *hsai);
00691 HAL_StatusTypeDef HAL_SAI_DMAResume(SAI_HandleTypeDef *hsai);
00692 HAL_StatusTypeDef HAL_SAI_DMAStop(SAI_HandleTypeDef *hsai);
00693 
00694 /* Abort function */
00695 HAL_StatusTypeDef HAL_SAI_Abort(SAI_HandleTypeDef *hsai);
00696 
00697 /* Mute management */
00698 HAL_StatusTypeDef HAL_SAI_EnableTxMuteMode(SAI_HandleTypeDef *hsai, uint16_t val);
00699 HAL_StatusTypeDef HAL_SAI_DisableTxMuteMode(SAI_HandleTypeDef *hsai);
00700 HAL_StatusTypeDef HAL_SAI_EnableRxMuteMode(SAI_HandleTypeDef *hsai, SAIcallback callback, uint16_t counter);
00701 HAL_StatusTypeDef HAL_SAI_DisableRxMuteMode(SAI_HandleTypeDef *hsai);
00702 
00703 /* SAI IRQHandler and Callbacks used in non blocking modes (Interrupt and DMA) */
00704 void HAL_SAI_IRQHandler(SAI_HandleTypeDef *hsai);
00705 void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai);
00706 void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai);
00707 void HAL_SAI_RxHalfCpltCallback(SAI_HandleTypeDef *hsai);
00708 void HAL_SAI_RxCpltCallback(SAI_HandleTypeDef *hsai);
00709 void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai);
00710 /**
00711   * @}
00712   */
00713 
00714 /** @addtogroup SAI_Exported_Functions_Group3
00715   * @{
00716   */
00717 /* Peripheral State functions  ************************************************/
00718 HAL_SAI_StateTypeDef HAL_SAI_GetState(SAI_HandleTypeDef *hsai);
00719 uint32_t HAL_SAI_GetError(SAI_HandleTypeDef *hsai);
00720 /**
00721   * @}
00722   */
00723 
00724 /**
00725   * @}
00726   */ 
00727 
00728 /* Private macros ------------------------------------------------------------*/
00729 /** @addtogroup SAI_Private_Macros
00730   * @{
00731   */
00732 #define IS_SAI_BLOCK_SYNCEXT(STATE)   (((STATE) == SAI_SYNCEXT_DISABLE)           ||\
00733                                        ((STATE) == SAI_SYNCEXT_OUTBLOCKA_ENABLE)  ||\
00734                                        ((STATE) == SAI_SYNCEXT_OUTBLOCKB_ENABLE))
00735 
00736 #define IS_SAI_SUPPORTED_PROTOCOL(PROTOCOL)   (((PROTOCOL) == SAI_I2S_STANDARD)     ||\
00737                                                ((PROTOCOL) == SAI_I2S_MSBJUSTIFIED) ||\
00738                                                ((PROTOCOL) == SAI_I2S_LSBJUSTIFIED) ||\
00739                                                ((PROTOCOL) == SAI_PCM_LONG)         ||\
00740                                                ((PROTOCOL) == SAI_PCM_SHORT))
00741 
00742 #define IS_SAI_PROTOCOL_DATASIZE(DATASIZE)   (((DATASIZE) == SAI_PROTOCOL_DATASIZE_16BIT)         ||\
00743                                               ((DATASIZE) == SAI_PROTOCOL_DATASIZE_16BITEXTENDED) ||\
00744                                               ((DATASIZE) == SAI_PROTOCOL_DATASIZE_24BIT)         ||\
00745                                               ((DATASIZE) == SAI_PROTOCOL_DATASIZE_32BIT))
00746 
00747 #define IS_SAI_AUDIO_FREQUENCY(AUDIO) (((AUDIO) == SAI_AUDIO_FREQUENCY_192K) || ((AUDIO) == SAI_AUDIO_FREQUENCY_96K) || \
00748                                        ((AUDIO) == SAI_AUDIO_FREQUENCY_48K)  || ((AUDIO) == SAI_AUDIO_FREQUENCY_44K) || \
00749                                        ((AUDIO) == SAI_AUDIO_FREQUENCY_32K)  || ((AUDIO) == SAI_AUDIO_FREQUENCY_22K) || \
00750                                        ((AUDIO) == SAI_AUDIO_FREQUENCY_16K)  || ((AUDIO) == SAI_AUDIO_FREQUENCY_11K) || \
00751                                        ((AUDIO) == SAI_AUDIO_FREQUENCY_8K)   || ((AUDIO) == SAI_AUDIO_FREQUENCY_MCKDIV))
00752 
00753 #define IS_SAI_BLOCK_MODE(MODE)    (((MODE) == SAI_MODEMASTER_TX) || \
00754                                     ((MODE) == SAI_MODEMASTER_RX) || \
00755                                     ((MODE) == SAI_MODESLAVE_TX)  || \
00756                                     ((MODE) == SAI_MODESLAVE_RX))
00757 
00758 #define IS_SAI_BLOCK_PROTOCOL(PROTOCOL) (((PROTOCOL) == SAI_FREE_PROTOCOL)  || \
00759                                          ((PROTOCOL) == SAI_AC97_PROTOCOL)  || \
00760                                          ((PROTOCOL) == SAI_SPDIF_PROTOCOL))
00761 
00762 #define IS_SAI_BLOCK_DATASIZE(DATASIZE) (((DATASIZE) == SAI_DATASIZE_8)  || \
00763                                          ((DATASIZE) == SAI_DATASIZE_10) || \
00764                                          ((DATASIZE) == SAI_DATASIZE_16) || \
00765                                          ((DATASIZE) == SAI_DATASIZE_20) || \
00766                                          ((DATASIZE) == SAI_DATASIZE_24) || \
00767                                          ((DATASIZE) == SAI_DATASIZE_32))
00768 
00769 #define IS_SAI_BLOCK_FIRST_BIT(BIT) (((BIT) == SAI_FIRSTBIT_MSB) || \
00770                                      ((BIT) == SAI_FIRSTBIT_LSB))
00771 
00772 #define IS_SAI_BLOCK_CLOCK_STROBING(CLOCK) (((CLOCK) == SAI_CLOCKSTROBING_FALLINGEDGE) || \
00773                                             ((CLOCK) == SAI_CLOCKSTROBING_RISINGEDGE))
00774 
00775 #define IS_SAI_BLOCK_SYNCHRO(SYNCHRO) (((SYNCHRO) == SAI_ASYNCHRONOUS)         || \
00776                                        ((SYNCHRO) == SAI_SYNCHRONOUS)          || \
00777                                        ((SYNCHRO) == SAI_SYNCHRONOUS_EXT_SAI1) ||\
00778                                        ((SYNCHRO) == SAI_SYNCHRONOUS_EXT_SAI2))
00779 
00780 #define IS_SAI_BLOCK_OUTPUT_DRIVE(DRIVE) (((DRIVE) == SAI_OUTPUTDRIVE_DISABLE) || \
00781                                           ((DRIVE) == SAI_OUTPUTDRIVE_ENABLE))
00782 
00783 #define IS_SAI_BLOCK_NODIVIDER(NODIVIDER) (((NODIVIDER) == SAI_MASTERDIVIDER_ENABLE) || \
00784                                            ((NODIVIDER) == SAI_MASTERDIVIDER_DISABLE))
00785 
00786 #define IS_SAI_BLOCK_MUTE_COUNTER(COUNTER) ((COUNTER) <= 63U)
00787 
00788 #define IS_SAI_BLOCK_MUTE_VALUE(VALUE)    (((VALUE) == SAI_ZERO_VALUE)     || \
00789                                            ((VALUE) == SAI_LAST_SENT_VALUE)) 
00790 
00791 #define IS_SAI_BLOCK_COMPANDING_MODE(MODE)    (((MODE) == SAI_NOCOMPANDING)         || \
00792                                                ((MODE) == SAI_ULAW_1CPL_COMPANDING) || \
00793                                                ((MODE) == SAI_ALAW_1CPL_COMPANDING) || \
00794                                                ((MODE) == SAI_ULAW_2CPL_COMPANDING) || \
00795                                                ((MODE) == SAI_ALAW_2CPL_COMPANDING)) 
00796 
00797 #define IS_SAI_BLOCK_FIFO_THRESHOLD(THRESHOLD) (((THRESHOLD) == SAI_FIFOTHRESHOLD_EMPTY)   || \
00798                                                 ((THRESHOLD) == SAI_FIFOTHRESHOLD_1QF)     || \
00799                                                 ((THRESHOLD) == SAI_FIFOTHRESHOLD_HF)      || \
00800                                                 ((THRESHOLD) == SAI_FIFOTHRESHOLD_3QF)     || \
00801                                                 ((THRESHOLD) == SAI_FIFOTHRESHOLD_FULL))  
00802 
00803 #define IS_SAI_BLOCK_TRISTATE_MANAGEMENT(STATE) (((STATE) == SAI_OUTPUT_NOTRELEASED) ||\
00804                                                  ((STATE) == SAI_OUTPUT_RELEASED)) 
00805 
00806 #define IS_SAI_MONO_STEREO_MODE(MODE) (((MODE) == SAI_MONOMODE) ||\
00807                                        ((MODE) == SAI_STEREOMODE)) 
00808 
00809 #define IS_SAI_SLOT_ACTIVE(ACTIVE)  ((ACTIVE) <= SAI_SLOTACTIVE_ALL)
00810 
00811 #define IS_SAI_BLOCK_SLOT_NUMBER(NUMBER) ((1U <= (NUMBER)) && ((NUMBER) <= 16U))
00812 
00813 #define IS_SAI_BLOCK_SLOT_SIZE(SIZE) (((SIZE) == SAI_SLOTSIZE_DATASIZE) || \
00814                                       ((SIZE) == SAI_SLOTSIZE_16B)      || \
00815                                       ((SIZE) == SAI_SLOTSIZE_32B))
00816 
00817 #define IS_SAI_BLOCK_FIRSTBIT_OFFSET(OFFSET) ((OFFSET) <= 24U) 
00818 
00819 #define IS_SAI_BLOCK_FS_OFFSET(OFFSET) (((OFFSET) == SAI_FS_FIRSTBIT) || \
00820                                         ((OFFSET) == SAI_FS_BEFOREFIRSTBIT))
00821 
00822 #define IS_SAI_BLOCK_FS_POLARITY(POLARITY) (((POLARITY) == SAI_FS_ACTIVE_LOW) || \
00823                                             ((POLARITY) == SAI_FS_ACTIVE_HIGH)) 
00824 
00825 #define IS_SAI_BLOCK_FS_DEFINITION(DEFINITION) (((DEFINITION) == SAI_FS_STARTFRAME) || \
00826                                                 ((DEFINITION) == SAI_FS_CHANNEL_IDENTIFICATION))
00827 
00828 #define IS_SAI_BLOCK_MASTER_DIVIDER(DIVIDER) ((DIVIDER) <= 15U)
00829 
00830 #define IS_SAI_BLOCK_FRAME_LENGTH(LENGTH) ((8U <= (LENGTH)) && ((LENGTH) <= 256U))
00831 
00832 #define IS_SAI_BLOCK_ACTIVE_FRAME(LENGTH) ((1U <= (LENGTH)) && ((LENGTH) <= 128U))
00833 
00834 
00835 /**
00836   * @}
00837   */
00838 
00839 /* Private functions ---------------------------------------------------------*/
00840 /** @defgroup SAI_Private_Functions SAI Private Functions
00841   * @{
00842   */
00843 
00844 /**
00845   * @}
00846   */
00847 
00848 /**
00849   * @}
00850   */
00851 
00852 #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx || STM32F413xx || STM32F423xx  */
00853 
00854 /**
00855   * @}
00856   */
00857   
00858 #ifdef __cplusplus
00859 }
00860 #endif
00861 
00862 #endif /* __STM32F4xx_HAL_SAI_H */
00863 
00864 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/