i2s.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /******************************************************************************
  2. * @file i2s.h
  3. * @version V0.10
  4. * $Revision: 12 $
  5. * $Date: 15/09/22 6:48p $
  6. * @brief NUC472/NUC442 I2S driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __I2S_H__
  12. #define __I2S_H__
  13. #include "NUC472_442.h"
  14. #ifdef __cplusplus
  15. extern "C"
  16. {
  17. #endif
  18. /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver
  19. @{
  20. */
  21. /** @addtogroup NUC472_442_I2S_Driver I2S Driver
  22. @{
  23. */
  24. /** @addtogroup NUC472_442_I2S_EXPORTED_CONSTANTS I2S Exported Constants
  25. @{
  26. */
  27. #define I2S_DATABIT_8 (0 << I2S_CTL_WDWIDTH_Pos) /*!< I2S data width is 8-bit \hideinitializer */
  28. #define I2S_DATABIT_16 (1 << I2S_CTL_WDWIDTH_Pos) /*!< I2S data width is 16-bit \hideinitializer */
  29. #define I2S_DATABIT_24 (2 << I2S_CTL_WDWIDTH_Pos) /*!< I2S data width is 24-bit \hideinitializer */
  30. #define I2S_DATABIT_32 (3 << I2S_CTL_WDWIDTH_Pos) /*!< I2S data width is 32-bit \hideinitializer */
  31. /* Audio Format */
  32. #define I2S_MONO I2S_CTL_MONO_Msk /*!< Mono channel \hideinitializer */
  33. #define I2S_STEREO 0 /*!< Stereo channel \hideinitializer */
  34. /* I2S Data Format */
  35. #define I2S_FORMAT_MSB I2S_CTL_FORMAT_Msk /*!< MSB data format \hideinitializer */
  36. #define I2S_FORMAT_I2S 0 /*!< I2S data format \hideinitializer */
  37. #define I2S_FORMAT_PCMB I2S_CTL_FORMAT_Msk /*!< PCMB data format \hideinitializer */
  38. #define I2S_FORMAT_PCMA 0 /*!< PCMA data format \hideinitializer */
  39. /* I2S Interface */
  40. #define I2S_PCM I2S_CTL_PCMEN_Msk /*!< PCM interface is selected \hideinitializer */
  41. #define I2S_I2S 0 /*!< I2S interface is selected \hideinitializer */
  42. /* I2S Operation mode */
  43. #define I2S_MODE_SLAVE I2S_CTL_SLAVE_Msk /*!< As slave mode \hideinitializer */
  44. #define I2S_MODE_MASTER 0 /*!< As master mode \hideinitializer */
  45. /* I2S FIFO Threshold */
  46. #define I2S_FIFO_TX_LEVEL_WORD_0 0 /*!< TX threshold is 0 word \hideinitializer */
  47. #define I2S_FIFO_TX_LEVEL_WORD_1 (1 << I2S_CTL_TXTH_Pos) /*!< TX threshold is 1 word \hideinitializer */
  48. #define I2S_FIFO_TX_LEVEL_WORD_2 (2 << I2S_CTL_TXTH_Pos) /*!< TX threshold is 2 words \hideinitializer */
  49. #define I2S_FIFO_TX_LEVEL_WORD_3 (3 << I2S_CTL_TXTH_Pos) /*!< TX threshold is 3 words \hideinitializer */
  50. #define I2S_FIFO_TX_LEVEL_WORD_4 (4 << I2S_CTL_TXTH_Pos) /*!< TX threshold is 4 words \hideinitializer */
  51. #define I2S_FIFO_TX_LEVEL_WORD_5 (5 << I2S_CTL_TXTH_Pos) /*!< TX threshold is 5 words \hideinitializer */
  52. #define I2S_FIFO_TX_LEVEL_WORD_6 (6 << I2S_CTL_TXTH_Pos) /*!< TX threshold is 6 words \hideinitializer */
  53. #define I2S_FIFO_TX_LEVEL_WORD_7 (7 << I2S_CTL_TXTH_Pos) /*!< TX threshold is 7 words \hideinitializer */
  54. #define I2S_FIFO_RX_LEVEL_WORD_1 0 /*!< RX threshold is 1 word \hideinitializer */
  55. #define I2S_FIFO_RX_LEVEL_WORD_2 (1 << I2S_CTL_RXTH_Pos) /*!< RX threshold is 2 words \hideinitializer */
  56. #define I2S_FIFO_RX_LEVEL_WORD_3 (2 << I2S_CTL_RXTH_Pos) /*!< RX threshold is 3 words \hideinitializer */
  57. #define I2S_FIFO_RX_LEVEL_WORD_4 (3 << I2S_CTL_RXTH_Pos) /*!< RX threshold is 4 words \hideinitializer */
  58. #define I2S_FIFO_RX_LEVEL_WORD_5 (4 << I2S_CTL_RXTH_Pos) /*!< RX threshold is 5 words \hideinitializer */
  59. #define I2S_FIFO_RX_LEVEL_WORD_6 (5 << I2S_CTL_RXTH_Pos) /*!< RX threshold is 6 words \hideinitializer */
  60. #define I2S_FIFO_RX_LEVEL_WORD_7 (6 << I2S_CTL_RXTH_Pos) /*!< RX threshold is 7 words \hideinitializer */
  61. #define I2S_FIFO_RX_LEVEL_WORD_8 (7 << I2S_CTL_RXTH_Pos) /*!< RX threshold is 8 words \hideinitializer */
  62. /* I2S Record Channel */
  63. #define I2S_MONO_RIGHT 0 /*!< Record mono right channel \hideinitializer */
  64. #define I2S_MONO_LEFT I2S_CTL_RXLCH_Msk /*!< Record mono left channel \hideinitializer */
  65. /* I2S Channel */
  66. #define I2S_RIGHT 0 /*!< Select right channel \hideinitializer */
  67. #define I2S_LEFT 1 /*!< Select left channel \hideinitializer */
  68. /*@}*/ /* end of group NUC472_442_I2S_EXPORTED_CONSTANTS */
  69. /** @addtogroup NUC472_442_I2S_EXPORTED_FUNCTIONS I2S Exported Functions
  70. @{
  71. */
  72. /*---------------------------------------------------------------------------------------------------------*/
  73. /* inline functions */
  74. /*---------------------------------------------------------------------------------------------------------*/
  75. /**
  76. * @brief Enable zero cross detect function.
  77. * @param[in] i2s is the base address of I2S module.
  78. * @param[in] u32ChMask is the mask for left or right channel. Valid values are:
  79. * - \ref I2S_RIGHT
  80. * - \ref I2S_LEFT
  81. * @return none
  82. * \hideinitializer
  83. */
  84. static __INLINE void I2S_ENABLE_TX_ZCD(I2S_T *i2s, uint32_t u32ChMask)
  85. {
  86. if(u32ChMask == I2S_RIGHT)
  87. i2s->CTL |= I2S_CTL_RZCEN_Msk;
  88. else
  89. i2s->CTL |= I2S_CTL_LZCEN_Msk;
  90. }
  91. /**
  92. * @brief Disable zero cross detect function.
  93. * @param[in] i2s is the base address of I2S module.
  94. * @param[in] u32ChMask is the mask for left or right channel. Valid values are:
  95. * - \ref I2S_RIGHT
  96. * - \ref I2S_LEFT
  97. * @return none
  98. * \hideinitializer
  99. */
  100. static __INLINE void I2S_DISABLE_TX_ZCD(I2S_T *i2s, uint32_t u32ChMask)
  101. {
  102. if(u32ChMask == I2S_RIGHT)
  103. i2s->CTL &= ~I2S_CTL_RZCEN_Msk;
  104. else
  105. i2s->CTL &= ~I2S_CTL_LZCEN_Msk;
  106. }
  107. /**
  108. * @brief Enable I2S Tx DMA function. I2S requests DMA to transfer data to Tx FIFO.
  109. * @param[in] i2s is the base address of I2S module.
  110. * @return none
  111. * \hideinitializer
  112. */
  113. #define I2S_ENABLE_TXDMA(i2s) ( (i2s)->CTL |= I2S_CTL_TXPDMAEN_Msk )
  114. /**
  115. * @brief Disable I2S Tx DMA function. I2S requests DMA to transfer data to Tx FIFO.
  116. * @param[in] i2s is the base address of I2S module.
  117. * @return none
  118. * \hideinitializer
  119. */
  120. #define I2S_DISABLE_TXDMA(i2s) ( (i2s)->CTL &= ~I2S_CTL_TXPDMAEN_Msk )
  121. /**
  122. * @brief Enable I2S Rx DMA function. I2S requests DMA to transfer data from Rx FIFO.
  123. * @param[in] i2s is the base address of I2S module.
  124. * @return none
  125. * \hideinitializer
  126. */
  127. #define I2S_ENABLE_RXDMA(i2s) ( (i2s)->CTL |= I2S_CTL_RXPDMAEN_Msk )
  128. /**
  129. * @brief Disable I2S Rx DMA function. I2S requests DMA to transfer data from Rx FIFO.
  130. * @param[in] i2s is the base address of I2S module.
  131. * @return none
  132. * \hideinitializer
  133. */
  134. #define I2S_DISABLE_RXDMA(i2s) ( (i2s)->CTL &= ~I2S_CTL_RXPDMAEN_Msk )
  135. /**
  136. * @brief Enable I2S Tx function .
  137. * @param[in] i2s is the base address of I2S module.
  138. * @return none
  139. * \hideinitializer
  140. */
  141. #define I2S_ENABLE_TX(i2s) ( (i2s)->CTL |= I2S_CTL_TXEN_Msk )
  142. /**
  143. * @brief Disable I2S Tx function .
  144. * @param[in] i2s is the base address of I2S module.
  145. * @return none
  146. * \hideinitializer
  147. */
  148. #define I2S_DISABLE_TX(i2s) ( (i2s)->CTL &= ~I2S_CTL_TXEN_Msk )
  149. /**
  150. * @brief Enable I2S Rx function .
  151. * @param[in] i2s is the base address of I2S module.
  152. * @return none
  153. * \hideinitializer
  154. */
  155. #define I2S_ENABLE_RX(i2s) ( (i2s)->CTL |= I2S_CTL_RXEN_Msk )
  156. /**
  157. * @brief Disable I2S Rx function .
  158. * @param[in] i2s is the base address of I2S module.
  159. * @return none
  160. * \hideinitializer
  161. */
  162. #define I2S_DISABLE_RX(i2s) ( (i2s)->CTL &= ~I2S_CTL_RXEN_Msk )
  163. /**
  164. * @brief Enable Tx Mute function .
  165. * @param[in] i2s is the base address of I2S module.
  166. * @return none
  167. * \hideinitializer
  168. */
  169. #define I2S_ENABLE_TX_MUTE(i2s) ( (i2s)->CTL |= I2S_CTL_MUTE_Msk )
  170. /**
  171. * @brief Disable Tx Mute function .
  172. * @param[in] i2s is the base address of I2S module.
  173. * @return none
  174. * \hideinitializer
  175. */
  176. #define I2S_DISABLE_TX_MUTE(i2s) ( (i2s)->CTL &= ~I2S_CTL_MUTE_Msk )
  177. /**
  178. * @brief Clear Tx FIFO. Internal pointer is reset to FIFO start point.
  179. * @param[in] i2s is the base address of I2S module.
  180. * @return none
  181. * \hideinitializer
  182. */
  183. #define I2S_CLR_TX_FIFO(i2s) ( (i2s)->CTL |= I2S_CTL_TXCLR_Msk )
  184. /**
  185. * @brief Clear Rx FIFO. Internal pointer is reset to FIFO start point.
  186. * @param[in] i2s is the base address of I2S module.
  187. * @return none
  188. * \hideinitializer
  189. */
  190. #define I2S_CLR_RX_FIFO(i2s) ( (i2s)->CTL |= I2S_CTL_RXCLR_Msk )
  191. /**
  192. * @brief This function sets the recording source channel when mono mode is used.
  193. * @param[in] i2s is the base address of I2S module.
  194. * @param[in] u32Ch left or right channel. Valid values are:
  195. * - \ref I2S_MONO_LEFT
  196. * - \ref I2S_MONO_RIGHT
  197. * @return none
  198. * \hideinitializer
  199. */
  200. static __INLINE void I2S_SET_MONO_RX_CHANNEL(I2S_T *i2s, uint32_t u32Ch)
  201. {
  202. u32Ch == I2S_MONO_LEFT ?
  203. (i2s->CTL |= I2S_CTL_RXLCH_Msk) :
  204. (i2s->CTL &= ~I2S_CTL_RXLCH_Msk);
  205. }
  206. /**
  207. * @brief Write data to I2S Tx FIFO.
  208. * @param[in] i2s is the base address of I2S module.
  209. * @param[in] u32Data: The data written to FIFO.
  210. * @return none
  211. * \hideinitializer
  212. */
  213. #define I2S_WRITE_TX_FIFO(i2s, u32Data) ( (i2s)->TX = u32Data )
  214. /**
  215. * @brief Read Rx FIFO.
  216. * @param[in] i2s is the base address of I2S module.
  217. * @return Data in Rx FIFO.
  218. * \hideinitializer
  219. */
  220. #define I2S_READ_RX_FIFO(i2s) ( (i2s)->RX )
  221. /**
  222. * @brief This function gets the interrupt flag according to the mask parameter.
  223. * @param[in] i2s is the base address of I2S module.
  224. * @param[in] u32Mask is the mask for the all interrupt flags.
  225. * @return The masked bit value of interrupt flag.
  226. * \hideinitializer
  227. */
  228. #define I2S_GET_INT_FLAG(i2s, u32Mask) ( (i2s)->STATUS & (u32Mask) )
  229. /**
  230. * @brief This function clears the interrupt flag according to the mask parameter.
  231. * @param[in] i2s is the base address of I2S module.
  232. * @param[in] u32Mask is the mask for the all interrupt flags.
  233. * @return none
  234. * \hideinitializer
  235. */
  236. #define I2S_CLR_INT_FLAG(i2s, u32Mask) ( (i2s)->STATUS |= (u32Mask) )
  237. /**
  238. * @brief Get transmit FIFO level
  239. * @param[in] i2s is the base address of I2S module.
  240. * @return FIFO level
  241. * \hideinitializer
  242. */
  243. #define I2S_GET_TX_FIFO_LEVEL(i2s) ( (((i2s)->STATUS & I2S_STATUS_TXCNT_Msk) >> I2S_STATUS_TXCNT_Pos) & 0xF )
  244. /**
  245. * @brief Get receive FIFO level
  246. * @param[in] i2s is the base address of I2S module.
  247. * @return FIFO level
  248. * \hideinitializer
  249. */
  250. #define I2S_GET_RX_FIFO_LEVEL(i2s) ( (((i2s)->STATUS & I2S_STATUS_RXCNT_Msk) >> I2S_STATUS_RXCNT_Pos) & 0xF )
  251. uint32_t I2S_Open(I2S_T *i2s, uint32_t u32MasterSlave, uint32_t u32SampleRate, uint32_t u32WordWidth, uint32_t u32Channels, uint32_t u32DataFormat, uint32_t u32AudioInterface);
  252. void I2S_Close(I2S_T *i2s);
  253. void I2S_EnableInt(I2S_T *i2s, uint32_t u32Mask);
  254. void I2S_DisableInt(I2S_T *i2s, uint32_t u32Mask);
  255. uint32_t I2S_EnableMCLK(I2S_T *i2s, uint32_t u32BusClock);
  256. void I2S_DisableMCLK(I2S_T *i2s);
  257. void I2S_SetFIFO(I2S_T *i2s, uint32_t u32TxThreshold, uint32_t u32RxThreshold);
  258. /*@}*/ /* end of group NUC472_442_I2S_EXPORTED_FUNCTIONS */
  259. /*@}*/ /* end of group NUC472_442_I2S_Driver */
  260. /*@}*/ /* end of group NUC472_442_Device_Driver */
  261. #ifdef __cplusplus
  262. }
  263. #endif
  264. #endif
  265. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/