stm32f4xx_hal_sai.h 37 KB

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