hc32f4a0_i2s.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. *******************************************************************************
  3. * @file hc32f4a0_i2s.h
  4. * @brief This file contains all the functions prototypes of the I2S driver
  5. * library.
  6. @verbatim
  7. Change Logs:
  8. Date Author Notes
  9. 2020-06-12 Yangjp First version
  10. @endverbatim
  11. *******************************************************************************
  12. * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
  13. *
  14. * This software component is licensed by HDSC under BSD 3-Clause license
  15. * (the "License"); You may not use this file except in compliance with the
  16. * License. You may obtain a copy of the License at:
  17. * opensource.org/licenses/BSD-3-Clause
  18. *
  19. *******************************************************************************
  20. */
  21. #ifndef __HC32F4A0_I2S_H__
  22. #define __HC32F4A0_I2S_H__
  23. /* C binding of definitions if building with C++ compiler */
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. /*******************************************************************************
  29. * Include files
  30. ******************************************************************************/
  31. #include "hc32_common.h"
  32. #include "ddl_config.h"
  33. /**
  34. * @addtogroup HC32F4A0_DDL_Driver
  35. * @{
  36. */
  37. /**
  38. * @addtogroup DDL_I2S
  39. * @{
  40. */
  41. #if (DDL_I2S_ENABLE == DDL_ON)
  42. /*******************************************************************************
  43. * Global type definitions ('typedef')
  44. ******************************************************************************/
  45. /**
  46. * @defgroup I2S_Global_Types I2S Global Types
  47. * @{
  48. */
  49. /**
  50. * @brief I2S Init structure definition
  51. */
  52. typedef struct
  53. {
  54. uint32_t u32ClockSrc; /*!< Specifies the clock source of I2S.
  55. This parameter can be a value of @ref I2S_Clock_Source */
  56. uint32_t u32Mode; /*!< Specifies the master/slave mode of I2S.
  57. This parameter can be a value of @ref I2S_Mode */
  58. uint32_t u32ComProtocol; /*!< Specifies the communication protocol of I2S.
  59. This parameter can be a value of @ref I2S_Com_Protocol */
  60. uint32_t u32TransMode; /*!< Specifies the transmission mode for the I2S communication.
  61. This parameter can be a value of @ref I2S_Trans_Mode */
  62. uint32_t u32AudioFreq; /*!< Specifies the frequency selected for the I2S communication.
  63. This parameter can be a value of @ref I2S_Audio_Frequency */
  64. uint32_t u32ChLen; /*!< Specifies the channel length for the I2S communication.
  65. This parameter can be a value of @ref I2S_Channel_Length */
  66. uint32_t u32DataLen; /*!< Specifies the data length for the I2S communication.
  67. This parameter can be a value of @ref I2S_Data_Length */
  68. uint32_t u32MCKOutput; /*!< Specifies the validity of the MCK output for I2S.
  69. This parameter can be a value of @ref I2S_MCK_Output */
  70. uint32_t u32TransFIFOLevel; /*!< Specifies the level of transfer FIFO.
  71. This parameter can be a value of @ref I2S_Trans_Level */
  72. uint32_t u32ReceiveFIFOLevel; /*!< Specifies the level of receive FIFO.
  73. This parameter can be a value of @ref I2S_Receive_Level */
  74. } stc_i2s_init_t;
  75. /**
  76. * @}
  77. */
  78. /*******************************************************************************
  79. * Global pre-processor symbols/macros ('#define')
  80. ******************************************************************************/
  81. /**
  82. * @defgroup I2S_Global_Macros I2S Global Macros
  83. * @{
  84. */
  85. /**
  86. * @defgroup I2S_Clock_Source I2S Clock Source
  87. * @{
  88. */
  89. #define I2S_CLK_SRC_PLL (I2S_CTRL_I2SPLLSEL) /*!< Internal PLL Clock */
  90. #define I2S_CLK_SRC_EXT (I2S_CTRL_CLKSEL) /*!< External Clock */
  91. /**
  92. * @}
  93. */
  94. /**
  95. * @defgroup I2S_Mode I2S Mode
  96. * @{
  97. */
  98. #define I2S_MD_MASTER (0UL) /*!< Master mode */
  99. #define I2S_MD_SLAVE (I2S_CTRL_WMS) /*!< Slave mode */
  100. /**
  101. * @}
  102. */
  103. /**
  104. * @defgroup I2S_Com_Protocol I2S Communication Protocol
  105. * @{
  106. */
  107. #define I2S_COM_PROTOCOL_PHILLIPS (0UL) /*!< Phillips protocol */
  108. #define I2S_COM_PROTOCOL_MSB (I2S_CFGR_I2SSTD_0) /*!< MSB justified protocol */
  109. #define I2S_COM_PROTOCOL_LSB (I2S_CFGR_I2SSTD_1) /*!< LSB justified protocol */
  110. #define I2S_COM_PROTOCOL_PCM_SHORT (I2S_CFGR_I2SSTD) /*!< PCM short-frame protocol */
  111. #define I2S_COM_PROTOCOL_PCM_LONG (I2S_CFGR_I2SSTD | I2S_CFGR_PCMSYNC) /*!< PCM long-frame protocol */
  112. /**
  113. * @}
  114. */
  115. /**
  116. * @defgroup I2S_Trans_Mode I2S Transfer Mode
  117. * @{
  118. */
  119. #define I2S_TRANS_MD_HALF_DUPLEX_RX (0UL) /*!< Receive only and half duplex mode */
  120. #define I2S_TRANS_MD_HALF_DUPLEX_TX (I2S_CTRL_SDOE) /*!< Send only and half duplex mode */
  121. #define I2S_TRANS_MD_FULL_DUPLEX (I2S_CTRL_DUPLEX | I2S_CTRL_SDOE) /*!< Full duplex mode */
  122. /**
  123. * @}
  124. */
  125. /**
  126. * @defgroup I2S_Audio_Frequency I2S Audio Frequency
  127. * @{
  128. */
  129. #define I2S_AUDIO_FREQ_192K (192000UL) /*!< FS = 192000Hz */
  130. #define I2S_AUDIO_FREQ_96K (96000UL) /*!< FS = 96000Hz */
  131. #define I2S_AUDIO_FREQ_48K (48000UL) /*!< FS = 48000Hz */
  132. #define I2S_AUDIO_FREQ_44K (44100UL) /*!< FS = 44100Hz */
  133. #define I2S_AUDIO_FREQ_32K (32000UL) /*!< FS = 32000Hz */
  134. #define I2S_AUDIO_FREQ_22K (22050UL) /*!< FS = 22050Hz */
  135. #define I2S_AUDIO_FREQ_16K (16000UL) /*!< FS = 16000Hz */
  136. #define I2S_AUDIO_FREQ_8K (8000UL) /*!< FS = 8000Hz */
  137. #define I2S_AUDIO_FREQ_DEFAULT (2UL)
  138. /**
  139. * @}
  140. */
  141. /**
  142. * @defgroup I2S_Channel_Length I2S Channel Length
  143. * @{
  144. */
  145. #define I2S_CH_LEN_16BIT (0UL) /*!< Channel length is 16bits */
  146. #define I2S_CH_LEN_32BIT (I2S_CFGR_CHLEN) /*!< Channel length is 32bits */
  147. /**
  148. * @}
  149. */
  150. /**
  151. * @defgroup I2S_Data_Length I2S Data Length
  152. * @{
  153. */
  154. #define I2S_DATA_LEN_16BIT (0UL) /*!< Transfer data length is 16bits */
  155. #define I2S_DATA_LEN_24BIT (I2S_CFGR_DATLEN_0) /*!< Transfer data length is 24bits */
  156. #define I2S_DATA_LEN_32BIT (I2S_CFGR_DATLEN_1) /*!< Transfer data length is 32bits */
  157. /**
  158. * @}
  159. */
  160. /**
  161. * @defgroup I2S_MCK_Output I2S MCK Output
  162. * @{
  163. */
  164. #define I2S_MCK_OUTPUT_DISABLE (0UL) /*!< Disable the drive clock(MCK) output */
  165. #define I2S_MCK_OUTPUT_ENABLE (I2S_CTRL_MCKOE) /*!< Enable the drive clock(MCK) output */
  166. /**
  167. * @}
  168. */
  169. /**
  170. * @defgroup I2S_Trans_Level I2S Transfer Level
  171. * @{
  172. */
  173. #define I2S_TRANS_LVL0 (0x00UL << I2S_CTRL_TXBIRQWL_POS) /*!< Transfer FIFO level is 0 */
  174. #define I2S_TRANS_LVL1 (0x01UL << I2S_CTRL_TXBIRQWL_POS) /*!< Transfer FIFO level is 1 */
  175. #define I2S_TRANS_LVL2 (0x02UL << I2S_CTRL_TXBIRQWL_POS) /*!< Transfer FIFO level is 2 */
  176. #define I2S_TRANS_LVL3 (0x03UL << I2S_CTRL_TXBIRQWL_POS) /*!< Transfer FIFO level is 3 */
  177. #define I2S_TRANS_LVL4 (0x04UL << I2S_CTRL_TXBIRQWL_POS) /*!< Transfer FIFO level is 4 */
  178. /**
  179. * @}
  180. */
  181. /**
  182. * @defgroup I2S_Receive_Level I2S Receive Level
  183. * @{
  184. */
  185. #define I2S_RECEIVE_LVL0 (0x00UL << I2S_CTRL_RXBIRQWL_POS) /*!< Receive FIFO level is 0 */
  186. #define I2S_RECEIVE_LVL1 (0x01UL << I2S_CTRL_RXBIRQWL_POS) /*!< Receive FIFO level is 1 */
  187. #define I2S_RECEIVE_LVL2 (0x02UL << I2S_CTRL_RXBIRQWL_POS) /*!< Receive FIFO level is 2 */
  188. #define I2S_RECEIVE_LVL3 (0x03UL << I2S_CTRL_RXBIRQWL_POS) /*!< Receive FIFO level is 3 */
  189. #define I2S_RECEIVE_LVL4 (0x04UL << I2S_CTRL_RXBIRQWL_POS) /*!< Receive FIFO level is 4 */
  190. /**
  191. * @}
  192. */
  193. /**
  194. * @defgroup I2S_Com_Func I2S Communication Function
  195. * @{
  196. */
  197. #define I2S_FUNC_TXE (I2S_CTRL_TXE) /*!< Transfer function */
  198. #define I2S_FUNC_RXE (I2S_CTRL_RXE) /*!< Receive function */
  199. #define I2S_FUNC_ALL (I2S_FUNC_TXE | I2S_FUNC_RXE)
  200. /**
  201. * @}
  202. */
  203. /**
  204. * @defgroup I2S_Reset_Type I2S Reset Type
  205. * @{
  206. */
  207. #define I2S_RST_TYPE_SW (I2S_CTRL_SRST) /*!< I2S software reset */
  208. #define I2S_RST_TYPE_CODEC (I2S_CTRL_CODECRC) /*!< Reset codec of I2S */
  209. #define I2S_RST_TYPE_FIFO (I2S_CTRL_FIFOR) /*!< Reset FIFO of I2S */
  210. #define I2S_RST_TYPE_ALL (I2S_RST_TYPE_SW | I2S_RST_TYPE_CODEC | I2S_RST_TYPE_FIFO)
  211. /**
  212. * @}
  213. */
  214. /**
  215. * @defgroup I2S_Interrupt I2S Interrupt
  216. * @{
  217. */
  218. #define I2S_INT_TX (I2S_CTRL_TXIE) /*!< Transfer interrupt */
  219. #define I2S_INT_RX (I2S_CTRL_RXIE) /*!< Receive interrupt */
  220. #define I2S_INT_ERR (I2S_CTRL_EIE) /*!< Communication error interrupt */
  221. #define I2S_INT_ALL (I2S_INT_TX | I2S_INT_RX | I2S_INT_ERR)
  222. /**
  223. * @}
  224. */
  225. /**
  226. * @defgroup I2S_Flag I2S Flag
  227. * @{
  228. */
  229. #define I2S_FLAG_TX_ALARM (I2S_SR_TXBA) /*!< Transfer buffer alarm flag */
  230. #define I2S_FLAG_RX_ALARM (I2S_SR_RXBA) /*!< Receive buffer alarm flag */
  231. #define I2S_FLAG_TX_EMPTY (I2S_SR_TXBE) /*!< Transfer buffer empty flag */
  232. #define I2S_FLAG_TX_FULL (I2S_SR_TXBF) /*!< Transfer buffer full flag */
  233. #define I2S_FLAG_RX_EMPTY (I2S_SR_RXBE) /*!< Receive buffer empty flag */
  234. #define I2S_FLAG_RX_FULL (I2S_SR_RXBF) /*!< Receive buffer full flag */
  235. #define I2S_FLAG_TX_ERR (I2S_ER_TXERR << 16U) /*!< Transfer overflow or underflow flag */
  236. #define I2S_FLAG_RX_ERR (I2S_ER_RXERR << 16U) /*!< Receive overflow flag */
  237. #define I2S_FLAG_ALL (I2S_FLAG_TX_ALARM | I2S_FLAG_RX_ALARM | I2S_FLAG_TX_EMPTY | \
  238. I2S_FLAG_TX_FULL | I2S_FLAG_RX_EMPTY | I2S_FLAG_RX_FULL | \
  239. I2S_FLAG_TX_ERR | I2S_FLAG_RX_ERR)
  240. #define I2S_CLR_FLAG_ALL (I2S_FLAG_TX_ERR | I2S_FLAG_RX_ERR)
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. /*******************************************************************************
  248. * Global variable definitions ('extern')
  249. ******************************************************************************/
  250. /*******************************************************************************
  251. Global function prototypes (definition in C source)
  252. ******************************************************************************/
  253. /**
  254. * @addtogroup I2S_Global_Functions
  255. * @{
  256. */
  257. /* Initialization and configuration functions */
  258. void I2S_DeInit(M4_I2S_TypeDef *I2Sx);
  259. en_result_t I2S_Init(M4_I2S_TypeDef *I2Sx, const stc_i2s_init_t *pstcI2sInit);
  260. en_result_t I2S_StructInit(stc_i2s_init_t *pstcI2sInit);
  261. void I2S_SetSWReset(M4_I2S_TypeDef *I2Sx, uint32_t u32ResetType);
  262. void I2S_SetTransMode(M4_I2S_TypeDef *I2Sx, uint32_t u32Mode);
  263. void I2S_SetTransFIFOLevel(M4_I2S_TypeDef *I2Sx, uint32_t u32Level);
  264. void I2S_SetReceiveFIFOLevel(M4_I2S_TypeDef *I2Sx, uint32_t u32Level);
  265. void I2S_SetComProtocol(M4_I2S_TypeDef *I2Sx, uint32_t u32Protocol);
  266. en_result_t I2S_SetAudioFreq(M4_I2S_TypeDef *I2Sx, uint32_t u32Freq);
  267. void I2S_MCKOutputCmd(M4_I2S_TypeDef *I2Sx, en_functional_state_t enNewState);
  268. void I2S_FuncCmd(M4_I2S_TypeDef* I2Sx, uint32_t u32Func, en_functional_state_t enNewState);
  269. /* Transfer and receive data functions */
  270. void I2S_WriteData(M4_I2S_TypeDef *I2Sx, uint32_t u32Data);
  271. uint32_t I2S_ReadData(const M4_I2S_TypeDef *I2Sx);
  272. en_result_t I2S_Trans(M4_I2S_TypeDef *I2Sx, const void *pvTxBuf, uint32_t u32Len, uint32_t u32Timeout);
  273. en_result_t I2S_Receive(const M4_I2S_TypeDef *I2Sx, void *pvRxBuf, uint32_t u32Len, uint32_t u32Timeout);
  274. en_result_t I2S_TransReceive(M4_I2S_TypeDef *I2Sx, const void *pvTxBuf, void *pvRxBuf, uint32_t u32Len, uint32_t u32Timeout);
  275. /* Interrupt and flag management functions */
  276. void I2S_IntCmd(M4_I2S_TypeDef *I2Sx, uint32_t u32IntType, en_functional_state_t enNewState);
  277. en_flag_status_t I2S_GetStatus(const M4_I2S_TypeDef *I2Sx, uint32_t u32Flag);
  278. void I2S_ClearStatus(M4_I2S_TypeDef *I2Sx, uint32_t u32Flag);
  279. /**
  280. * @}
  281. */
  282. #endif /* DDL_I2S_ENABLE */
  283. /**
  284. * @}
  285. */
  286. /**
  287. * @}
  288. */
  289. #ifdef __cplusplus
  290. }
  291. #endif
  292. #endif /* __HC32F4A0_I2S_H__ */
  293. /*******************************************************************************
  294. * EOF (not truncated)
  295. ******************************************************************************/