stm32f4xx_sai.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_sai.c
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Serial Audio Interface (SAI):
  9. * + Initialization and Configuration
  10. * + Data transfers functions
  11. * + DMA transfers management
  12. * + Interrupts and flags management
  13. *
  14. @verbatim
  15. ===============================================================================
  16. ##### How to use this driver #####
  17. ===============================================================================
  18. [..]
  19. (#) Enable peripheral clock using the following functions
  20. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SAI1, ENABLE) for SAI1
  21. (#) For each SAI Block A/B enable SCK, SD, FS and MCLK GPIO clocks
  22. using RCC_AHB1PeriphClockCmd() function.
  23. (#) Peripherals alternate function:
  24. (++) Connect the pin to the desired peripherals' Alternate
  25. Function (AF) using GPIO_PinAFConfig() function.
  26. (++) Configure the desired pin in alternate function by:
  27. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  28. (++) Select the type, pull-up/pull-down and output speed via
  29. GPIO_PuPd, GPIO_OType and GPIO_Speed members
  30. (++) Call GPIO_Init() function
  31. -@@- If an external clock source is used then the I2S CKIN pin should be
  32. also configured in Alternate function Push-pull pull-up mode.
  33. (#) The SAI clock can be generated from different clock source :
  34. PLL I2S, PLL SAI or external clock source.
  35. (++) The PLL I2S is configured using the following functions RCC_PLLI2SConfig(),
  36. RCC_PLLI2SCmd(ENABLE), RCC_GetFlagStatus(RCC_FLAG_PLLI2SRDY) and
  37. RCC_SAIPLLI2SClkDivConfig() or;
  38. (++) The PLL SAI is configured using the following functions RCC_PLLSAIConfig(),
  39. RCC_PLLSAICmd(ENABLE), RCC_GetFlagStatus(RCC_FLAG_PLLSAIRDY) and
  40. RCC_SAIPLLSAIClkDivConfig()or;
  41. (++) External clock source is configured using the function
  42. RCC_I2SCLKConfig(RCC_I2S2CLKSource_Ext) and after setting correctly the
  43. define constant I2S_EXTERNAL_CLOCK_VAL in the stm32f4xx_conf.h file.
  44. (#) Each SAI Block A or B has its own clock generator to make these two blocks
  45. completely independent. The Clock generator is configured using RCC_SAIBlockACLKConfig() and
  46. RCC_SAIBlockBCLKConfig() functions.
  47. (#) Each SAI Block A or B can be configured separately :
  48. (++) Program the Master clock divider, Audio mode, Protocol, Data Length, Clock Strobing Edge,
  49. Synchronous mode, Output drive and FIFO Thresold using SAI_Init() function.
  50. In case of master mode, program the Master clock divider (MCKDIV) using
  51. the following formula :
  52. (+++) MCLK_x = SAI_CK_x / (MCKDIV * 2) with MCLK_x = 256 * FS
  53. (+++) FS = SAI_CK_x / (MCKDIV * 2) * 256
  54. (+++) MCKDIV = SAI_CK_x / FS * 512
  55. (++) Program the Frame Length, Frame active Length, FS Definition, FS Polarity,
  56. FS Offset using SAI_FrameInit() function.
  57. (++) Program the Slot First Bit Offset, Slot Size, Slot Number, Slot Active
  58. using SAI_SlotInit() function.
  59. (#) Enable the NVIC and the corresponding interrupt using the function
  60. SAI_ITConfig() if you need to use interrupt mode.
  61. (#) When using the DMA mode
  62. (++) Configure the DMA using DMA_Init() function
  63. (++) Active the needed channel Request using SAI_DMACmd() function
  64. (#) Enable the SAI using the SAI_Cmd() function.
  65. (#) Enable the DMA using the DMA_Cmd() function when using DMA mode.
  66. (#) The SAI has some specific functions which can be useful depending
  67. on the audio protocol selected.
  68. (++) Enable Mute mode when the audio block is a transmitter using SAI_MuteModeCmd()
  69. function and configure the value transmitted during mute using SAI_MuteValueConfig().
  70. (++) Detect the Mute mode when audio block is a receiver using SAI_MuteFrameCounterConfig().
  71. (++) Enable the MONO mode without any data preprocessing in memory when the number
  72. of slot is equal to 2 using SAI_MonoModeConfig() function.
  73. (++) Enable data companding algorithm (U law and A law) using SAI_CompandingModeConfig().
  74. (++) Choose the behavior of the SD line in output when an inactive slot is sent
  75. on the data line using SAI_TRIStateConfig() function.
  76. [..]
  77. (@) In master TX mode: enabling the audio block immediately generates the bit clock
  78. for the external slaves even if there is no data in the FIFO, However FS signal
  79. generation is conditioned by the presence of data in the FIFO.
  80. (@) In master RX mode: enabling the audio block immediately generates the bit clock
  81. and FS signal for the external slaves.
  82. (@) It is mandatory to respect the following conditions in order to avoid bad SAI behavior:
  83. (+@) First bit Offset <= (SLOT size - Data size)
  84. (+@) Data size <= SLOT size
  85. (+@) Number of SLOT x SLOT size = Frame length
  86. (+@) The number of slots should be even when bit FSDEF in the SAI_xFRCR is set.
  87. @endverbatim
  88. ******************************************************************************
  89. * @attention
  90. *
  91. * Copyright (c) 2016 STMicroelectronics.
  92. * All rights reserved.
  93. *
  94. * This software is licensed under terms that can be found in the LICENSE file
  95. * in the root directory of this software component.
  96. * If no LICENSE file comes with this software, it is provided AS-IS.
  97. *
  98. ******************************************************************************
  99. */
  100. /* Includes ------------------------------------------------------------------*/
  101. #include "stm32f4xx_sai.h"
  102. #include "stm32f4xx_rcc.h"
  103. /** @addtogroup STM32F4xx_StdPeriph_Driver
  104. * @{
  105. */
  106. /** @defgroup SAI
  107. * @brief SAI driver modules
  108. * @{
  109. */
  110. #if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx) || \
  111. defined (STM32F401xx) || defined (STM32F411xE) || defined (STM32F446xx) || defined (STM32F469_479xx) || \
  112. defined (STM32F413_423xx)
  113. /* Private typedef -----------------------------------------------------------*/
  114. /* Private define ------------------------------------------------------------*/
  115. /* *SAI registers Masks */
  116. #define CR1_CLEAR_MASK ((uint32_t)0xFF07C010)
  117. #define FRCR_CLEAR_MASK ((uint32_t)0xFFF88000)
  118. #define SLOTR_CLEAR_MASK ((uint32_t)0x0000F020)
  119. /* Private macro -------------------------------------------------------------*/
  120. /* Private variables ---------------------------------------------------------*/
  121. /* Private function prototypes -----------------------------------------------*/
  122. /* Private functions ---------------------------------------------------------*/
  123. /** @defgroup SAI_Private_Functions
  124. * @{
  125. */
  126. /** @defgroup SAI_Group1 Initialization and Configuration functions
  127. * @brief Initialization and Configuration functions
  128. *
  129. @verbatim
  130. ===============================================================================
  131. ##### Initialization and Configuration functions #####
  132. ===============================================================================
  133. [..]
  134. This section provides a set of functions allowing to initialize the SAI Audio
  135. Block Mode, Audio Protocol, Data size, Synchronization between audio block,
  136. Master clock Divider, Fifo threshold, Frame configuration, slot configuration,
  137. Tristate mode, Companding mode and Mute mode.
  138. [..]
  139. The SAI_Init(), SAI_FrameInit() and SAI_SlotInit() functions follows the SAI Block
  140. configuration procedures for Master mode and Slave mode (details for these procedures
  141. are available in reference manual(RM0090).
  142. @endverbatim
  143. * @{
  144. */
  145. /**
  146. * @brief Deinitialize the SAIx peripheral registers to their default reset values.
  147. * @param SAIx: To select the SAIx peripheral, where x can be the different instances
  148. *
  149. * @retval None
  150. */
  151. void SAI_DeInit(SAI_TypeDef* SAIx)
  152. {
  153. /* Check the parameters */
  154. assert_param(IS_SAI_PERIPH(SAIx));
  155. if(SAIx == SAI1)
  156. {
  157. /* Enable SAI1 reset state */
  158. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SAI1, ENABLE);
  159. /* Release SAI1 from reset state */
  160. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SAI1, DISABLE);
  161. }
  162. else
  163. {
  164. #if defined(STM32F446xx)
  165. if(SAIx == SAI2)
  166. {
  167. /* Enable SAI2 reset state */
  168. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SAI2, ENABLE);
  169. /* Release SAI2 from reset state */
  170. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SAI2, DISABLE);
  171. }
  172. #endif /* STM32F446xx */
  173. }
  174. }
  175. /**
  176. * @brief Initializes the SAI Block x peripheral according to the specified
  177. * parameters in the SAI_InitStruct.
  178. *
  179. * @note SAI clock is generated from a specific output of the PLLSAI or a specific
  180. * output of the PLLI2S or from an alternate function bypassing the PLL I2S.
  181. *
  182. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  183. * @param SAI_InitStruct: pointer to a SAI_InitTypeDef structure that
  184. * contains the configuration information for the specified SAI Block peripheral.
  185. * @retval None
  186. */
  187. void SAI_Init(SAI_Block_TypeDef* SAI_Block_x, SAI_InitTypeDef* SAI_InitStruct)
  188. {
  189. uint32_t tmpreg = 0;
  190. /* Check the parameters */
  191. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  192. /* Check the SAI Block parameters */
  193. assert_param(IS_SAI_BLOCK_MODE(SAI_InitStruct->SAI_AudioMode));
  194. assert_param(IS_SAI_BLOCK_PROTOCOL(SAI_InitStruct->SAI_Protocol));
  195. assert_param(IS_SAI_BLOCK_DATASIZE(SAI_InitStruct->SAI_DataSize));
  196. assert_param(IS_SAI_BLOCK_FIRST_BIT(SAI_InitStruct->SAI_FirstBit));
  197. assert_param(IS_SAI_BLOCK_CLOCK_STROBING(SAI_InitStruct->SAI_ClockStrobing));
  198. assert_param(IS_SAI_BLOCK_SYNCHRO(SAI_InitStruct->SAI_Synchro));
  199. assert_param(IS_SAI_BLOCK_SYNCEXT(SAI_InitStruct->SAI_SynchroExt));
  200. assert_param(IS_SAI_BLOCK_OUTPUT_DRIVE(SAI_InitStruct->SAI_OUTDRIV));
  201. assert_param(IS_SAI_BLOCK_NODIVIDER(SAI_InitStruct->SAI_NoDivider));
  202. assert_param(IS_SAI_BLOCK_MASTER_DIVIDER(SAI_InitStruct->SAI_MasterDivider));
  203. assert_param(IS_SAI_BLOCK_FIFO_THRESHOLD(SAI_InitStruct->SAI_FIFOThreshold));
  204. /* SAI Block_x CR1 Configuration */
  205. /* Get the SAI Block_x CR1 value */
  206. tmpreg = SAI_Block_x->CR1;
  207. /* Clear MODE, PRTCFG, DS, LSBFIRST, CKSTR, SYNCEN, OUTDRIV, NODIV, and MCKDIV bits */
  208. tmpreg &= CR1_CLEAR_MASK;
  209. /* Configure SAI_Block_x: Audio mode, Protocol, Data Size, first transmitted bit, Clock strobing
  210. edge, Synchronization mode, Output drive, Master Divider and FIFO level */
  211. /* Set MODE bits according to SAI_AudioMode value */
  212. /* Set PRTCFG bits according to SAI_Protocol value */
  213. /* Set DS bits according to SAI_DataSize value */
  214. /* Set LSBFIRST bit according to SAI_FirstBit value */
  215. /* Set CKSTR bit according to SAI_ClockStrobing value */
  216. /* Set SYNCEN bit according to SAI_Synchro value */
  217. /* Set OUTDRIV bit according to SAI_OUTDRIV value */
  218. /* Set NODIV bit according to SAI_NoDivider value */
  219. /* Set MCKDIV bits according to SAI_MasterDivider value */
  220. tmpreg |= (uint32_t)(SAI_InitStruct->SAI_AudioMode | SAI_InitStruct->SAI_Protocol |
  221. SAI_InitStruct->SAI_DataSize | SAI_InitStruct->SAI_FirstBit |
  222. SAI_InitStruct->SAI_ClockStrobing | SAI_InitStruct->SAI_Synchro |
  223. SAI_InitStruct->SAI_OUTDRIV | SAI_InitStruct->SAI_NoDivider |
  224. SAI_InitStruct->SAI_SynchroExt | (uint32_t)((SAI_InitStruct->SAI_MasterDivider) << 20));
  225. /* Write to SAI_Block_x CR1 */
  226. SAI_Block_x->CR1 = tmpreg;
  227. /* SAI Block_x CR2 Configuration */
  228. /* Get the SAIBlock_x CR2 value */
  229. tmpreg = SAI_Block_x->CR2;
  230. /* Clear FTH bits */
  231. tmpreg &= ~(SAI_xCR2_FTH);
  232. /* Configure the FIFO Level */
  233. /* Set FTH bits according to SAI_FIFOThreshold value */
  234. tmpreg |= (uint32_t)(SAI_InitStruct->SAI_FIFOThreshold);
  235. /* Write to SAI_Block_x CR2 */
  236. SAI_Block_x->CR2 = tmpreg;
  237. }
  238. /**
  239. * @brief Initializes the SAI Block Audio frame according to the specified
  240. * parameters in the SAI_FrameInitStruct.
  241. *
  242. * @note this function has no meaning if the AC'97 or SPDIF audio protocol
  243. * are selected.
  244. *
  245. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  246. * @param SAI_FrameInitStruct: pointer to an SAI_FrameInitTypeDef structure that
  247. * contains the configuration of audio frame for a specified SAI Block
  248. * @retval None
  249. */
  250. void SAI_FrameInit(SAI_Block_TypeDef* SAI_Block_x, SAI_FrameInitTypeDef* SAI_FrameInitStruct)
  251. {
  252. uint32_t tmpreg = 0;
  253. /* Check the parameters */
  254. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  255. /* Check the SAI Block frame parameters */
  256. assert_param(IS_SAI_BLOCK_FRAME_LENGTH(SAI_FrameInitStruct->SAI_FrameLength));
  257. assert_param(IS_SAI_BLOCK_ACTIVE_FRAME(SAI_FrameInitStruct->SAI_ActiveFrameLength));
  258. assert_param(IS_SAI_BLOCK_FS_DEFINITION(SAI_FrameInitStruct->SAI_FSDefinition));
  259. assert_param(IS_SAI_BLOCK_FS_POLARITY(SAI_FrameInitStruct->SAI_FSPolarity));
  260. assert_param(IS_SAI_BLOCK_FS_OFFSET(SAI_FrameInitStruct->SAI_FSOffset));
  261. /* SAI Block_x FRCR Configuration */
  262. /* Get the SAI Block_x FRCR value */
  263. tmpreg = SAI_Block_x->FRCR;
  264. /* Clear FRL, FSALL, FSDEF, FSPOL, FSOFF bits */
  265. tmpreg &= FRCR_CLEAR_MASK;
  266. /* Configure SAI_Block_x Frame: Frame Length, Active Frame Length, Frame Synchronization
  267. Definition, Frame Synchronization Polarity and Frame Synchronization Polarity */
  268. /* Set FRL bits according to SAI_FrameLength value */
  269. /* Set FSALL bits according to SAI_ActiveFrameLength value */
  270. /* Set FSDEF bit according to SAI_FSDefinition value */
  271. /* Set FSPOL bit according to SAI_FSPolarity value */
  272. /* Set FSOFF bit according to SAI_FSOffset value */
  273. tmpreg |= (uint32_t)((uint32_t)(SAI_FrameInitStruct->SAI_FrameLength - 1) |
  274. SAI_FrameInitStruct->SAI_FSOffset |
  275. SAI_FrameInitStruct->SAI_FSDefinition |
  276. SAI_FrameInitStruct->SAI_FSPolarity |
  277. (uint32_t)((SAI_FrameInitStruct->SAI_ActiveFrameLength - 1) << 8));
  278. /* Write to SAI_Block_x FRCR */
  279. SAI_Block_x->FRCR = tmpreg;
  280. }
  281. /**
  282. * @brief Initializes the SAI Block audio Slot according to the specified
  283. * parameters in the SAI_SlotInitStruct.
  284. *
  285. * @note this function has no meaning if the AC'97 or SPDIF audio protocol
  286. * are selected.
  287. *
  288. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  289. * @param SAI_SlotInitStruct: pointer to an SAI_SlotInitTypeDef structure that
  290. * contains the configuration of audio slot for a specified SAI Block
  291. * @retval None
  292. */
  293. void SAI_SlotInit(SAI_Block_TypeDef* SAI_Block_x, SAI_SlotInitTypeDef* SAI_SlotInitStruct)
  294. {
  295. uint32_t tmpreg = 0;
  296. /* Check the parameters */
  297. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  298. /* Check the SAI Block Slot parameters */
  299. assert_param(IS_SAI_BLOCK_FIRSTBIT_OFFSET(SAI_SlotInitStruct->SAI_FirstBitOffset));
  300. assert_param(IS_SAI_BLOCK_SLOT_SIZE(SAI_SlotInitStruct->SAI_SlotSize));
  301. assert_param(IS_SAI_BLOCK_SLOT_NUMBER(SAI_SlotInitStruct->SAI_SlotNumber));
  302. assert_param(IS_SAI_SLOT_ACTIVE(SAI_SlotInitStruct->SAI_SlotActive));
  303. /* SAI Block_x SLOTR Configuration */
  304. /* Get the SAI Block_x SLOTR value */
  305. tmpreg = SAI_Block_x->SLOTR;
  306. /* Clear FBOFF, SLOTSZ, NBSLOT, SLOTEN bits */
  307. tmpreg &= SLOTR_CLEAR_MASK;
  308. /* Configure SAI_Block_x Slot: First bit offset, Slot size, Number of Slot in
  309. audio frame and slots activated in audio frame */
  310. /* Set FBOFF bits according to SAI_FirstBitOffset value */
  311. /* Set SLOTSZ bits according to SAI_SlotSize value */
  312. /* Set NBSLOT bits according to SAI_SlotNumber value */
  313. /* Set SLOTEN bits according to SAI_SlotActive value */
  314. tmpreg |= (uint32_t)(SAI_SlotInitStruct->SAI_FirstBitOffset |
  315. SAI_SlotInitStruct->SAI_SlotSize |
  316. SAI_SlotInitStruct->SAI_SlotActive |
  317. (uint32_t)((SAI_SlotInitStruct->SAI_SlotNumber - 1) << 8));
  318. /* Write to SAI_Block_x SLOTR */
  319. SAI_Block_x->SLOTR = tmpreg;
  320. }
  321. /**
  322. * @brief Fills each SAI_InitStruct member with its default value.
  323. * @param SAI_InitStruct: pointer to a SAI_InitTypeDef structure which will
  324. * be initialized.
  325. * @retval None
  326. */
  327. void SAI_StructInit(SAI_InitTypeDef* SAI_InitStruct)
  328. {
  329. /* Reset SAI init structure parameters values */
  330. /* Initialize the SAI_AudioMode member */
  331. SAI_InitStruct->SAI_AudioMode = SAI_Mode_MasterTx;
  332. /* Initialize the SAI_Protocol member */
  333. SAI_InitStruct->SAI_Protocol = SAI_Free_Protocol;
  334. /* Initialize the SAI_DataSize member */
  335. SAI_InitStruct->SAI_DataSize = SAI_DataSize_8b;
  336. /* Initialize the SAI_FirstBit member */
  337. SAI_InitStruct->SAI_FirstBit = SAI_FirstBit_MSB;
  338. /* Initialize the SAI_ClockStrobing member */
  339. SAI_InitStruct->SAI_ClockStrobing = SAI_ClockStrobing_FallingEdge;
  340. /* Initialize the SAI_Synchro member */
  341. SAI_InitStruct->SAI_Synchro = SAI_Asynchronous;
  342. /* Initialize the SAI_SynchroExt member */
  343. SAI_InitStruct->SAI_SynchroExt = SAI_SyncExt_Disable;
  344. /* Initialize the SAI_OUTDRIV member */
  345. SAI_InitStruct->SAI_OUTDRIV = SAI_OutputDrive_Disabled;
  346. /* Initialize the SAI_NoDivider member */
  347. SAI_InitStruct->SAI_NoDivider = SAI_MasterDivider_Enabled;
  348. /* Initialize the SAI_MasterDivider member */
  349. SAI_InitStruct->SAI_MasterDivider = 0;
  350. /* Initialize the SAI_FIFOThreshold member */
  351. SAI_InitStruct->SAI_FIFOThreshold = SAI_Threshold_FIFOEmpty;
  352. }
  353. /**
  354. * @brief Fills each SAI_FrameInitStruct member with its default value.
  355. * @param SAI_FrameInitStruct: pointer to a SAI_FrameInitTypeDef structure
  356. * which will be initialized.
  357. * @retval None
  358. */
  359. void SAI_FrameStructInit(SAI_FrameInitTypeDef* SAI_FrameInitStruct)
  360. {
  361. /* Reset SAI Frame init structure parameters values */
  362. /* Initialize the SAI_FrameLength member */
  363. SAI_FrameInitStruct->SAI_FrameLength = 8;
  364. /* Initialize the SAI_ActiveFrameLength member */
  365. SAI_FrameInitStruct->SAI_ActiveFrameLength = 1;
  366. /* Initialize the SAI_FSDefinition member */
  367. SAI_FrameInitStruct->SAI_FSDefinition = SAI_FS_StartFrame;
  368. /* Initialize the SAI_FSPolarity member */
  369. SAI_FrameInitStruct->SAI_FSPolarity = SAI_FS_ActiveLow;
  370. /* Initialize the SAI_FSOffset member */
  371. SAI_FrameInitStruct->SAI_FSOffset = SAI_FS_FirstBit;
  372. }
  373. /**
  374. * @brief Fills each SAI_SlotInitStruct member with its default value.
  375. * @param SAI_SlotInitStruct: pointer to a SAI_SlotInitTypeDef structure
  376. * which will be initialized.
  377. * @retval None
  378. */
  379. void SAI_SlotStructInit(SAI_SlotInitTypeDef* SAI_SlotInitStruct)
  380. {
  381. /* Reset SAI Slot init structure parameters values */
  382. /* Initialize the SAI_FirstBitOffset member */
  383. SAI_SlotInitStruct->SAI_FirstBitOffset = 0;
  384. /* Initialize the SAI_SlotSize member */
  385. SAI_SlotInitStruct->SAI_SlotSize = SAI_SlotSize_DataSize;
  386. /* Initialize the SAI_SlotNumber member */
  387. SAI_SlotInitStruct->SAI_SlotNumber = 1;
  388. /* Initialize the SAI_SlotActive member */
  389. SAI_SlotInitStruct->SAI_SlotActive = SAI_Slot_NotActive;
  390. }
  391. /**
  392. * @brief Enables or disables the specified SAI Block peripheral.
  393. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  394. * @param NewState: new state of the SAI_Block_x peripheral.
  395. * This parameter can be: ENABLE or DISABLE.
  396. * @retval None
  397. */
  398. void SAI_Cmd(SAI_Block_TypeDef* SAI_Block_x, FunctionalState NewState)
  399. {
  400. /* Check the parameters */
  401. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  402. assert_param(IS_FUNCTIONAL_STATE(NewState));
  403. if (NewState != DISABLE)
  404. {
  405. /* Enable the selected SAI peripheral */
  406. SAI_Block_x->CR1 |= SAI_xCR1_SAIEN;
  407. }
  408. else
  409. {
  410. /* Disable the selected SAI peripheral */
  411. SAI_Block_x->CR1 &= ~(SAI_xCR1_SAIEN);
  412. }
  413. }
  414. /**
  415. * @brief Configures the mono mode for the selected SAI block.
  416. *
  417. * @note This function has a meaning only when the number of slot is equal to 2.
  418. *
  419. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  420. * @param SAI_MonoMode: specifies the SAI block mono mode.
  421. * This parameter can be one of the following values:
  422. * @arg SAI_MonoMode : Set mono audio mode
  423. * @arg SAI_StreoMode : Set streo audio mode
  424. * @retval None
  425. */
  426. void SAI_MonoModeConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_Mono_StreoMode)
  427. {
  428. /* Check the parameters */
  429. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  430. assert_param(IS_SAI_BLOCK_MONO_STREO_MODE(SAI_MonoMode));
  431. /* Clear MONO bit */
  432. SAI_Block_x->CR1 &= ~(SAI_xCR1_MONO);
  433. /* Set new Mono Mode value */
  434. SAI_Block_x->CR1 |= SAI_MonoMode;
  435. }
  436. /**
  437. * @brief Configures the TRIState management on data line for the selected SAI block.
  438. *
  439. * @note This function has a meaning only when the SAI block is configured in transmitter
  440. *
  441. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  442. * @param SAI_TRIState: specifies the SAI block TRIState management.
  443. * This parameter can be one of the following values:
  444. * @arg SAI_Output_NotReleased : SD output line is still driven by the SAI.
  445. * @arg SAI_Output_Released : SD output line is released (HI-Z)
  446. * @retval None
  447. */
  448. void SAI_TRIStateConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_TRIState)
  449. {
  450. /* Check the parameters */
  451. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  452. assert_param(IS_SAI_BLOCK_TRISTATE_MANAGEMENT(SAI_TRIState));
  453. /* Clear MONO bit */
  454. SAI_Block_x->CR1 &= ~(SAI_xCR1_MONO);
  455. /* Set new Mono Mode value */
  456. SAI_Block_x->CR1 |= SAI_MonoMode;
  457. }
  458. /**
  459. * @brief Configures the companding mode for the selected SAI block.
  460. *
  461. * @note The data expansion or data compression are determined by the state of
  462. * SAI block selected (transmitter or receiver).
  463. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  464. * @param SAI_CompandingMode: specifies the SAI block companding mode.
  465. * This parameter can be one of the following values:
  466. * @arg SAI_NoCompanding : no companding algorithm set
  467. * @arg SAI_ULaw_1CPL_Companding : Set U law (algorithm 1's complement representation)
  468. * @arg SAI_ALaw_1CPL_Companding : Set A law (algorithm 1's complement representation)
  469. * @arg SAI_ULaw_2CPL_Companding : Set U law (algorithm 2's complement representation)
  470. * @arg SAI_ALaw_2CPL_Companding : Set A law (algorithm 2's complement representation)
  471. * @retval None
  472. */
  473. void SAI_CompandingModeConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_CompandingMode)
  474. {
  475. /* Check the parameters */
  476. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  477. assert_param(IS_SAI_BLOCK_COMPANDING_MODE(SAI_CompandingMode));
  478. /* Clear Companding Mode bits */
  479. SAI_Block_x->CR2 &= ~(SAI_xCR2_COMP);
  480. /* Set new Companding Mode value */
  481. SAI_Block_x->CR2 |= SAI_CompandingMode;
  482. }
  483. /**
  484. * @brief Enables or disables the Mute mode for the selected SAI block.
  485. *
  486. * @note This function has a meaning only when the audio block is transmitter
  487. * @note Mute mode is applied for an entire frame for all the valid slot
  488. * It becomes active at the end of an audio frame when set somewhere in a frame.
  489. * Mute mode exit occurs at the end of the frame in which the bit MUTE has been set.
  490. *
  491. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  492. * @param NewState: new state of the SAIx block.
  493. * This parameter can be: ENABLE or DISABLE.
  494. * @retval None
  495. */
  496. void SAI_MuteModeCmd(SAI_Block_TypeDef* SAI_Block_x, FunctionalState NewState)
  497. {
  498. /* Check the parameters */
  499. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  500. assert_param(IS_FUNCTIONAL_STATE(NewState));
  501. if (NewState != DISABLE)
  502. {
  503. /* Enable the selected SAI block mute mode */
  504. SAI_Block_x->CR2 |= SAI_xCR2_MUTE;
  505. }
  506. else
  507. {
  508. /* Disable the selected SAI SS output */
  509. SAI_Block_x->CR2 &= ~(SAI_xCR2_MUTE);
  510. }
  511. }
  512. /**
  513. * @brief Configure the mute value for the selected SAI block.
  514. *
  515. * @note This function has a meaning only when the audio block is transmitter
  516. * @note the configuration last value sent during mute mode has only a meaning
  517. * when the number of slot is lower or equal to 2 and if the MUTE bit is set.
  518. *
  519. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  520. * @param SAI_MuteValue: specifies the SAI block mute value.
  521. * This parameter can be one of the following values:
  522. * @arg SAI_ZeroValue : bit value 0 is sent during Mute Mode
  523. * @arg SAI_LastSentValue : Last value is sent during Mute Mode
  524. * @retval None
  525. */
  526. void SAI_MuteValueConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_MuteValue)
  527. {
  528. /* Check the parameters */
  529. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  530. assert_param(IS_SAI_BLOCK_MUTE_VALUE(SAI_MuteValue));
  531. /* Clear Mute value bits */
  532. SAI_Block_x->CR2 &= ~(SAI_xCR2_MUTEVAL);
  533. /* Set new Mute value */
  534. SAI_Block_x->CR2 |= SAI_MuteValue;
  535. }
  536. /**
  537. * @brief Enables or disables the Mute mode for the selected SAI block.
  538. *
  539. * @note This function has a meaning only when the audio block is Receiver
  540. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  541. * @param SAI_MuteCounter: specifies the SAI block mute value.
  542. * This parameter can be a number between 0 and 63.
  543. * @retval None
  544. */
  545. void SAI_MuteFrameCounterConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_MuteCounter)
  546. {
  547. /* Check the parameters */
  548. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  549. assert_param(IS_SAI_BLOCK_MUTE_COUNTER(SAI_MuteCounter));
  550. /* Clear Mute value bits */
  551. SAI_Block_x->CR2 &= ~(SAI_xCR2_MUTECNT);
  552. /* Set new Mute value */
  553. SAI_Block_x->CR2 |= (SAI_MuteCounter << 7);
  554. }
  555. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
  556. defined(STM32F469_479xx) || defined(STM32F413_423xx) || defined(STM32F446xx)
  557. /**
  558. * @brief Configure SAI Block synchronization mode
  559. * @param SAI_InitStruct: pointer to a SAI_InitTypeDef structure that
  560. * contains the configuration information for the specified SAI Block peripheral.
  561. * @param SAIx: To select the SAIx peripheral, where x can be the different instances
  562. * @retval None
  563. */
  564. void SAI_BlockSynchroConfig(SAI_InitTypeDef* SAI_InitStruct, SAI_TypeDef* SAIx)
  565. {
  566. uint32_t tmpregisterGCR = 0U;
  567. #if defined(STM32F446xx)
  568. /* This setting must be done with both audio block (A & B) disabled */
  569. switch(SAI_InitStruct->SAI_SynchroExt)
  570. {
  571. case SAI_SyncExt_Disable :
  572. tmpregisterGCR = 0U;
  573. break;
  574. case SAI_SyncExt_OutBlockA_Enable :
  575. tmpregisterGCR = SAI_GCR_SYNCOUT_0;
  576. break;
  577. case SAI_SyncExt_OutBlockB_Enable :
  578. tmpregisterGCR = SAI_GCR_SYNCOUT_1;
  579. break;
  580. default:
  581. break;
  582. }
  583. if(((SAI_InitStruct->SAI_Synchro) == SAI_Synchronous_Ext) && (SAIx == SAI1))
  584. {
  585. tmpregisterGCR |= SAI_GCR_SYNCIN_0;
  586. }
  587. if(SAIx == SAI1)
  588. {
  589. SAI1->GCR = tmpregisterGCR;
  590. }
  591. else
  592. {
  593. SAI2->GCR = tmpregisterGCR;
  594. }
  595. #endif /* STM32F446xx */
  596. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
  597. defined(STM32F469_479xx) || defined(STM32F413_423xx)
  598. /* This setting must be done with both audio block (A & B) disabled */
  599. switch(SAI_InitStruct->SAI_SynchroExt)
  600. {
  601. case SAI_SyncExt_Disable :
  602. tmpregisterGCR = 0U;
  603. break;
  604. case SAI_SyncExt_OutBlockA_Enable :
  605. tmpregisterGCR = SAI_GCR_SYNCOUT_0;
  606. break;
  607. case SAI_SyncExt_OutBlockB_Enable :
  608. tmpregisterGCR = SAI_GCR_SYNCOUT_1;
  609. break;
  610. default:
  611. break;
  612. }
  613. SAI1->GCR = tmpregisterGCR;
  614. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469_479xx || STM32F413_423xx */
  615. }
  616. #endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469_479xx || STM32F413_423xx || STM32F446xx */
  617. /**
  618. * @brief Reinitialize the FIFO pointer
  619. *
  620. * @note The FIFO pointers can be reinitialized at anytime The data present
  621. * into the FIFO, if it is not empty, will be lost.
  622. *
  623. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  624. * @param NewState: new state of the selected SAI TI communication mode.
  625. * This parameter can be: ENABLE or DISABLE.
  626. * @retval None
  627. */
  628. void SAI_FlushFIFO(SAI_Block_TypeDef* SAI_Block_x)
  629. {
  630. /* Check the parameters */
  631. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  632. /* FIFO flush */
  633. SAI_Block_x->CR2 |= SAI_xCR2_FFLUSH;
  634. }
  635. /**
  636. * @}
  637. */
  638. /** @defgroup SAI_Group2 Data transfers functions
  639. * @brief Data transfers functions
  640. *
  641. @verbatim
  642. ===============================================================================
  643. ##### Data transfers functions #####
  644. ===============================================================================
  645. [..]
  646. This section provides a set of functions allowing to manage the SAI data transfers.
  647. [..]
  648. In reception, data are received and then stored into an internal FIFO while
  649. In transmission, data are first stored into an internal FIFO before being
  650. transmitted.
  651. [..]
  652. The read access of the SAI_xDR register can be done using the SAI_ReceiveData()
  653. function and returns the Rx buffered value. Whereas a write access to the SAI_DR
  654. can be done using SAI_SendData() function and stores the written data into
  655. Tx buffer.
  656. @endverbatim
  657. * @{
  658. */
  659. /**
  660. * @brief Returns the most recent received data by the SAI block x peripheral.
  661. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  662. *
  663. * @retval The value of the received data.
  664. */
  665. uint32_t SAI_ReceiveData(SAI_Block_TypeDef* SAI_Block_x)
  666. {
  667. /* Check the parameters */
  668. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  669. /* Return the data in the DR register */
  670. return SAI_Block_x->DR;
  671. }
  672. /**
  673. * @brief Transmits a Data through the SAI block x peripheral.
  674. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  675. *
  676. * @param Data: Data to be transmitted.
  677. * @retval None
  678. */
  679. void SAI_SendData(SAI_Block_TypeDef* SAI_Block_x, uint32_t Data)
  680. {
  681. /* Check the parameters */
  682. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  683. /* Write in the DR register the data to be sent */
  684. SAI_Block_x->DR = Data;
  685. }
  686. /**
  687. * @}
  688. */
  689. /** @defgroup SAI_Group3 DMA transfers management functions
  690. * @brief DMA transfers management functions
  691. *
  692. @verbatim
  693. ===============================================================================
  694. ##### DMA transfers management functions #####
  695. ===============================================================================
  696. @endverbatim
  697. * @{
  698. */
  699. /**
  700. * @brief Enables or disables the SAI Block x DMA interface.
  701. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  702. * @param NewState: new state of the selected SAI block DMA transfer request.
  703. * This parameter can be: ENABLE or DISABLE.
  704. * @retval None
  705. */
  706. void SAI_DMACmd(SAI_Block_TypeDef* SAI_Block_x, FunctionalState NewState)
  707. {
  708. /* Check the parameters */
  709. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  710. assert_param(IS_FUNCTIONAL_STATE(NewState));
  711. if (NewState != DISABLE)
  712. {
  713. /* Enable the selected SAI block mute mode */
  714. SAI_Block_x->CR1 |= SAI_xCR1_DMAEN;
  715. }
  716. else
  717. {
  718. /* Disable the selected SAI SS output */
  719. SAI_Block_x->CR1 &= ~(SAI_xCR1_DMAEN);
  720. }
  721. }
  722. /**
  723. * @}
  724. */
  725. /** @defgroup SAI_Group4 Interrupts and flags management functions
  726. * @brief Interrupts and flags management functions
  727. *
  728. @verbatim
  729. ===============================================================================
  730. ##### Interrupts and flags management functions #####
  731. ===============================================================================
  732. [..]
  733. This section provides a set of functions allowing to configure the SAI Interrupts
  734. sources and check or clear the flags or pending bits status.
  735. The user should identify which mode will be used in his application to manage
  736. the communication: Polling mode, Interrupt mode or DMA mode.
  737. *** Polling Mode ***
  738. ====================
  739. [..]
  740. In Polling Mode, the SAI communication can be managed by 7 flags:
  741. (#) SAI_FLAG_FREQ : to indicate if there is a FIFO Request to write or to read.
  742. (#) SAI_FLAG_MUTEDET : to indicate if a MUTE frame detected
  743. (#) SAI_FLAG_OVRUDR : to indicate if an Overrun or Underrun error occur
  744. (#) SAI_FLAG_AFSDET : to indicate if there is the detection of a audio frame
  745. synchronisation (FS) earlier than expected
  746. (#) SAI_FLAG_LFSDET : to indicate if there is the detection of a audio frame
  747. synchronisation (FS) later than expected
  748. (#) SAI_FLAG_CNRDY : to indicate if the codec is not ready to communicate during
  749. the reception of the TAG 0 (slot0) of the AC97 audio frame
  750. (#) SAI_FLAG_WCKCFG: to indicate if wrong clock configuration in master mode
  751. error occurs.
  752. [..]
  753. In this Mode it is advised to use the following functions:
  754. (+) FlagStatus SAI_GetFlagStatus(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_FLAG);
  755. (+) void SAI_ClearFlag(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_FLAG);
  756. *** Interrupt Mode ***
  757. ======================
  758. [..]
  759. In Interrupt Mode, the SAI communication can be managed by 7 interrupt sources
  760. and 7 pending bits:
  761. (+) Pending Bits:
  762. (##) SAI_IT_FREQ : to indicate if there is a FIFO Request to write or to read.
  763. (##) SAI_IT_MUTEDET : to indicate if a MUTE frame detected.
  764. (##) SAI_IT_OVRUDR : to indicate if an Overrun or Underrun error occur.
  765. (##) SAI_IT_AFSDET : to indicate if there is the detection of a audio frame
  766. synchronisation (FS) earlier than expected.
  767. (##) SAI_IT_LFSDET : to indicate if there is the detection of a audio frame
  768. synchronisation (FS) later than expected.
  769. (##) SAI_IT_CNRDY : to indicate if the codec is not ready to communicate during
  770. the reception of the TAG 0 (slot0) of the AC97 audio frame.
  771. (##) SAI_IT_WCKCFG: to indicate if wrong clock configuration in master mode
  772. error occurs.
  773. (+) Interrupt Source:
  774. (##) SAI_IT_FREQ : specifies the interrupt source for FIFO Request.
  775. (##) SAI_IT_MUTEDET : specifies the interrupt source for MUTE frame detected.
  776. (##) SAI_IT_OVRUDR : specifies the interrupt source for overrun or underrun error.
  777. (##) SAI_IT_AFSDET : specifies the interrupt source for anticipated frame synchronization
  778. detection interrupt.
  779. (##) SAI_IT_LFSDET : specifies the interrupt source for late frame synchronization
  780. detection interrupt.
  781. (##) SAI_IT_CNRDY : specifies the interrupt source for codec not ready interrupt
  782. (##) SAI_IT_WCKCFG: specifies the interrupt source for wrong clock configuration
  783. interrupt.
  784. [..]
  785. In this Mode it is advised to use the following functions:
  786. (+) void SAI_ITConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_IT, FunctionalState NewState);
  787. (+) ITStatus SAI_GetITStatus(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_IT);
  788. (+) void SAI_ClearITPendingBit(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_IT);
  789. *** DMA Mode ***
  790. ================
  791. [..]
  792. In DMA Mode, each SAI audio block has an independent DMA interface in order to
  793. read or to write into the SAI_xDR register (to hit the internal FIFO).
  794. There is one DMA channel by audio block following basic DMA request/acknowledge
  795. protocol.
  796. [..]
  797. In this Mode it is advised to use the following function:
  798. (+) void SAI_DMACmd(SAI_Block_TypeDef* SAI_Block_x, FunctionalState NewState);
  799. [..]
  800. This section provides also functions allowing to
  801. (+) Check the SAI Block enable status
  802. (+)Check the FIFO status
  803. *** SAI Block Enable status ***
  804. ===============================
  805. [..]
  806. After disabling a SAI Block, it is recommended to check (or wait until) the SAI Block
  807. is effectively disabled. If a Block is disabled while an audio frame transfer is ongoing
  808. the current frame will be transferred and the block will be effectively disabled only at
  809. the end of audio frame.
  810. To monitor this state it is possible to use the following function:
  811. (+) FunctionalState SAI_GetCmdStatus(SAI_Block_TypeDef* SAI_Block_x);
  812. *** SAI Block FIFO status ***
  813. =============================
  814. [..]
  815. It is possible to monitor the FIFO status when a transfer is ongoing using the following
  816. function:
  817. (+) uint32_t SAI_GetFIFOStatus(SAI_Block_TypeDef* SAI_Block_x);
  818. @endverbatim
  819. * @{
  820. */
  821. /**
  822. * @brief Enables or disables the specified SAI Block interrupts.
  823. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  824. * @param SAI_IT: specifies the SAI interrupt source to be enabled or disabled.
  825. * This parameter can be one of the following values:
  826. * @arg SAI_IT_FREQ: FIFO Request interrupt mask
  827. * @arg SAI_IT_MUTEDET: MUTE detection interrupt mask
  828. * @arg SAI_IT_OVRUDR: overrun/underrun interrupt mask
  829. * @arg SAI_IT_AFSDET: anticipated frame synchronization detection
  830. * interrupt mask
  831. * @arg SAI_IT_LFSDET: late frame synchronization detection interrupt
  832. * mask
  833. * @arg SAI_IT_CNRDY: codec not ready interrupt mask
  834. * @arg SAI_IT_WCKCFG: wrong clock configuration interrupt mask
  835. * @param NewState: new state of the specified SAI interrupt.
  836. * This parameter can be: ENABLE or DISABLE.
  837. * @retval None
  838. */
  839. void SAI_ITConfig(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_IT, FunctionalState NewState)
  840. {
  841. /* Check the parameters */
  842. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  843. assert_param(IS_FUNCTIONAL_STATE(NewState));
  844. assert_param(IS_SAI_BLOCK_CONFIG_IT(SAI_IT));
  845. if (NewState != DISABLE)
  846. {
  847. /* Enable the selected SAI Block interrupt */
  848. SAI_Block_x->IMR |= SAI_IT;
  849. }
  850. else
  851. {
  852. /* Disable the selected SAI Block interrupt */
  853. SAI_Block_x->IMR &= ~(SAI_IT);
  854. }
  855. }
  856. /**
  857. * @brief Checks whether the specified SAI block x flag is set or not.
  858. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  859. * @param SAI_FLAG: specifies the SAI block flag to check.
  860. * This parameter can be one of the following values:
  861. * @arg SAI_FLAG_FREQ: FIFO Request flag.
  862. * @arg SAI_FLAG_MUTEDET: MUTE detection flag.
  863. * @arg SAI_FLAG_OVRUDR: overrun/underrun flag.
  864. * @arg SAI_FLAG_WCKCFG: wrong clock configuration flag.
  865. * @arg SAI_FLAG_CNRDY: codec not ready flag.
  866. * @arg SAI_FLAG_AFSDET: anticipated frame synchronization detection flag.
  867. * @arg SAI_FLAG_LFSDET: late frame synchronization detection flag.
  868. * @retval The new state of SAI_FLAG (SET or RESET).
  869. */
  870. FlagStatus SAI_GetFlagStatus(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_FLAG)
  871. {
  872. FlagStatus bitstatus = RESET;
  873. /* Check the parameters */
  874. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  875. assert_param(IS_SAI_BLOCK_GET_FLAG(SAI_FLAG));
  876. /* Check the status of the specified SAI flag */
  877. if ((SAI_Block_x->SR & SAI_FLAG) != (uint32_t)RESET)
  878. {
  879. /* SAI_FLAG is set */
  880. bitstatus = SET;
  881. }
  882. else
  883. {
  884. /* SAI_FLAG is reset */
  885. bitstatus = RESET;
  886. }
  887. /* Return the SAI_FLAG status */
  888. return bitstatus;
  889. }
  890. /**
  891. * @brief Clears the specified SAI Block x flag.
  892. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  893. * @param SAI_FLAG: specifies the SAI block flag to check.
  894. * This parameter can be one of the following values:
  895. * @arg SAI_FLAG_MUTEDET: MUTE detection flag.
  896. * @arg SAI_FLAG_OVRUDR: overrun/underrun flag.
  897. * @arg SAI_FLAG_WCKCFG: wrong clock configuration flag.
  898. * @arg SAI_FLAG_CNRDY: codec not ready flag.
  899. * @arg SAI_FLAG_AFSDET: anticipated frame synchronization detection flag.
  900. * @arg SAI_FLAG_LFSDET: late frame synchronization detection flag.
  901. *
  902. * @note FREQ (FIFO Request) flag is cleared :
  903. * - When the audio block is transmitter and the FIFO is full or the FIFO
  904. * has one data (one buffer mode) depending the bit FTH in the
  905. * SAI_xCR2 register.
  906. * - When the audio block is receiver and the FIFO is not empty
  907. *
  908. * @retval None
  909. */
  910. void SAI_ClearFlag(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_FLAG)
  911. {
  912. /* Check the parameters */
  913. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  914. assert_param(IS_SAI_BLOCK_CLEAR_FLAG(SAI_FLAG));
  915. /* Clear the selected SAI Block flag */
  916. SAI_Block_x->CLRFR |= SAI_FLAG;
  917. }
  918. /**
  919. * @brief Checks whether the specified SAI Block x interrupt has occurred or not.
  920. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  921. * @param SAI_IT: specifies the SAI interrupt source to be enabled or disabled.
  922. * This parameter can be one of the following values:
  923. * @arg SAI_IT_FREQ: FIFO Request interrupt
  924. * @arg SAI_IT_MUTEDET: MUTE detection interrupt
  925. * @arg SAI_IT_OVRUDR: overrun/underrun interrupt
  926. * @arg SAI_IT_AFSDET: anticipated frame synchronization detection interrupt
  927. * @arg SAI_IT_LFSDET: late frame synchronization detection interrupt
  928. * @arg SAI_IT_CNRDY: codec not ready interrupt
  929. * @arg SAI_IT_WCKCFG: wrong clock configuration interrupt
  930. *
  931. * @retval The new state of SAI_IT (SET or RESET).
  932. */
  933. ITStatus SAI_GetITStatus(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_IT)
  934. {
  935. ITStatus bitstatus = RESET;
  936. uint32_t enablestatus = 0;
  937. /* Check the parameters */
  938. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  939. assert_param(IS_SAI_BLOCK_CONFIG_IT(SAI_IT));
  940. /* Get the SAI_IT enable bit status */
  941. enablestatus = (SAI_Block_x->IMR & SAI_IT) ;
  942. /* Check the status of the specified SAI interrupt */
  943. if (((SAI_Block_x->SR & SAI_IT) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
  944. {
  945. /* SAI_IT is set */
  946. bitstatus = SET;
  947. }
  948. else
  949. {
  950. /* SAI_IT is reset */
  951. bitstatus = RESET;
  952. }
  953. /* Return the SAI_IT status */
  954. return bitstatus;
  955. }
  956. /**
  957. * @brief Clears the SAI Block x interrupt pending bit.
  958. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  959. * @param SAI_IT: specifies the SAI Block interrupt pending bit to clear.
  960. * This parameter can be one of the following values:
  961. * @arg SAI_IT_MUTEDET: MUTE detection interrupt.
  962. * @arg SAI_IT_OVRUDR: overrun/underrun interrupt.
  963. * @arg SAI_IT_WCKCFG: wrong clock configuration interrupt.
  964. * @arg SAI_IT_CNRDY: codec not ready interrupt.
  965. * @arg SAI_IT_AFSDET: anticipated frame synchronization detection interrupt.
  966. * @arg SAI_IT_LFSDET: late frame synchronization detection interrupt.
  967. *
  968. * @note FREQ (FIFO Request) flag is cleared :
  969. * - When the audio block is transmitter and the FIFO is full or the FIFO
  970. * has one data (one buffer mode) depending the bit FTH in the
  971. * SAI_xCR2 register.
  972. * - When the audio block is receiver and the FIFO is not empty
  973. *
  974. * @retval None
  975. */
  976. void SAI_ClearITPendingBit(SAI_Block_TypeDef* SAI_Block_x, uint32_t SAI_IT)
  977. {
  978. /* Check the parameters */
  979. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  980. assert_param(IS_SAI_BLOCK_CONFIG_IT(SAI_IT));
  981. /* Clear the selected SAI Block x interrupt pending bit */
  982. SAI_Block_x->CLRFR |= SAI_IT;
  983. }
  984. /**
  985. * @brief Returns the status of EN bit for the specified SAI Block x.
  986. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  987. *
  988. * @note After disabling a SAI Block, it is recommended to check (or wait until)
  989. * the SAI Block is effectively disabled. If a Block is disabled while
  990. * an audio frame transfer is ongoing, the current frame will be
  991. * transferred and the block will be effectively disabled only at
  992. * the end of audio frame.
  993. *
  994. * @retval Current state of the DMAy Streamx (ENABLE or DISABLE).
  995. */
  996. FunctionalState SAI_GetCmdStatus(SAI_Block_TypeDef* SAI_Block_x)
  997. {
  998. FunctionalState state = DISABLE;
  999. /* Check the parameters */
  1000. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  1001. if ((SAI_Block_x->CR1 & (uint32_t)SAI_xCR1_SAIEN) != 0)
  1002. {
  1003. /* The selected SAI Block x EN bit is set (audio frame transfer is ongoing) */
  1004. state = ENABLE;
  1005. }
  1006. else
  1007. {
  1008. /* The selected SAI Block x EN bit is cleared (SAI Block is disabled and
  1009. all transfers are complete) */
  1010. state = DISABLE;
  1011. }
  1012. return state;
  1013. }
  1014. /**
  1015. * @brief Returns the current SAI Block x FIFO filled level.
  1016. * @param SAI_Block_x: where x can be A or B to select the SAI Block peripheral.
  1017. *
  1018. * @retval The FIFO filling state.
  1019. * - SAI_FIFOStatus_Empty: when FIFO is empty
  1020. * - SAI_FIFOStatus_Less1QuarterFull: when FIFO is less than 1 quarter-full
  1021. * and not empty.
  1022. * - SAI_FIFOStatus_1QuarterFull: if more than 1 quarter-full.
  1023. * - SAI_FIFOStatus_HalfFull: if more than 1 half-full.
  1024. * - SAI_FIFOStatus_3QuartersFull: if more than 3 quarters-full.
  1025. * - SAI_FIFOStatus_Full: when FIFO is full
  1026. */
  1027. uint32_t SAI_GetFIFOStatus(SAI_Block_TypeDef* SAI_Block_x)
  1028. {
  1029. uint32_t tmpreg = 0;
  1030. /* Check the parameters */
  1031. assert_param(IS_SAI_BLOCK_PERIPH(SAI_Block_x));
  1032. /* Get the FIFO level bits */
  1033. tmpreg = (uint32_t)((SAI_Block_x->SR & SAI_xSR_FLVL));
  1034. return tmpreg;
  1035. }
  1036. /**
  1037. * @}
  1038. */
  1039. /**
  1040. * @}
  1041. */
  1042. #endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx || STM32F411xE || STM32F446xx || STM32F469_479xx */
  1043. /**
  1044. * @}
  1045. */
  1046. /**
  1047. * @}
  1048. */