nu_i2s.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**************************************************************************//**
  2. * @file nu_i2s.h
  3. * @version V3.00
  4. * @brief M460 series I2S driver header file
  5. *
  6. * @copyright SPDX-License-Identifier: Apache-2.0
  7. * @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef __NU_I2S_H__
  10. #define __NU_I2S_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup I2S_Driver I2S Driver
  19. @{
  20. */
  21. /** @addtogroup I2S_EXPORTED_CONSTANTS I2S Exported Constants
  22. @{
  23. */
  24. #define I2S_DATABIT_8 (0U << I2S_CTL0_DATWIDTH_Pos) /*!< I2S data width is 8-bit \hideinitializer */
  25. #define I2S_DATABIT_16 (1U << I2S_CTL0_DATWIDTH_Pos) /*!< I2S data width is 16-bit \hideinitializer */
  26. #define I2S_DATABIT_24 (2U << I2S_CTL0_DATWIDTH_Pos) /*!< I2S data width is 24-bit \hideinitializer */
  27. #define I2S_DATABIT_32 (3U << I2S_CTL0_DATWIDTH_Pos) /*!< I2S data width is 32-bit \hideinitializer */
  28. /* Audio Format */
  29. #define I2S_MONO I2S_CTL0_MONO_Msk /*!< Mono channel \hideinitializer */
  30. #define I2S_STEREO (0U) /*!< Stereo channel \hideinitializer */
  31. #define I2S_ENABLE_MONO I2S_MONO
  32. #define I2S_DISABLE_MONO I2S_STEREO
  33. /* I2S Data Format */
  34. #define I2S_FORMAT_I2S (0U << I2S_CTL0_FORMAT_Pos) /*!< I2S data format \hideinitializer */
  35. #define I2S_FORMAT_I2S_MSB (1U << I2S_CTL0_FORMAT_Pos) /*!< I2S MSB data format \hideinitializer */
  36. #define I2S_FORMAT_I2S_LSB (2U << I2S_CTL0_FORMAT_Pos) /*!< I2S LSB data format \hideinitializer */
  37. #define I2S_FORMAT_PCM (4U << I2S_CTL0_FORMAT_Pos) /*!< PCM data format \hideinitializer */
  38. #define I2S_FORMAT_PCM_MSB (5U << I2S_CTL0_FORMAT_Pos) /*!< PCM MSB data format \hideinitializer */
  39. #define I2S_FORMAT_PCM_LSB (6U << I2S_CTL0_FORMAT_Pos) /*!< PCM LSB data format \hideinitializer */
  40. /* I2S Data Format */
  41. #define I2S_ORDER_AT_MSB (0U) /*!< Channel data is at MSB \hideinitializer */
  42. #define I2S_ORDER_AT_LSB I2S_CTL0_ORDER_Msk /*!< Channel data is at LSB \hideinitializer */
  43. /* I2S TDM Channel Number */
  44. #define I2S_TDM_2CH 0U /*!< Use TDM 2 channel \hideinitializer */
  45. #define I2S_TDM_4CH 1U /*!< Use TDM 4 channel \hideinitializer */
  46. #define I2S_TDM_6CH 2U /*!< Use TDM 6 channel \hideinitializer */
  47. #define I2S_TDM_8CH 3U /*!< Use TDM 8 channel \hideinitializer */
  48. /* I2S TDM Channel Width */
  49. #define I2S_TDM_WIDTH_8BIT 0U /*!< TDM channel witch is 8-bit \hideinitializer */
  50. #define I2S_TDM_WIDTH_16BIT 1U /*!< TDM channel witch is 16-bit \hideinitializer */
  51. #define I2S_TDM_WIDTH_24BIT 2U /*!< TDM channel witch is 24-bit \hideinitializer */
  52. #define I2S_TDM_WIDTH_32BIT 3U /*!< TDM channel witch is 32-bit \hideinitializer */
  53. /* I2S TDM Sync Width */
  54. #define I2S_TDM_SYNC_ONE_BCLK 0U /*!< TDM sync widht is one BLCK period \hideinitializer */
  55. #define I2S_TDM_SYNC_ONE_CHANNEL 1U /*!< TDM sync widht is one channel period \hideinitializer */
  56. /* I2S Operation mode */
  57. #define I2S_MODE_SLAVE I2S_CTL0_SLAVE_Msk /*!< As slave mode \hideinitializer */
  58. #define I2S_MODE_MASTER (0U) /*!< As master mode \hideinitializer */
  59. /* I2S FIFO Threshold */
  60. #define I2S_FIFO_TX_LEVEL_WORD_0 (0U) /*!< TX threshold is 0 word \hideinitializer */
  61. #define I2S_FIFO_TX_LEVEL_WORD_1 (1U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 1 word \hideinitializer */
  62. #define I2S_FIFO_TX_LEVEL_WORD_2 (2U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 2 words \hideinitializer */
  63. #define I2S_FIFO_TX_LEVEL_WORD_3 (3U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 3 words \hideinitializer */
  64. #define I2S_FIFO_TX_LEVEL_WORD_4 (4U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 4 words \hideinitializer */
  65. #define I2S_FIFO_TX_LEVEL_WORD_5 (5U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 5 words \hideinitializer */
  66. #define I2S_FIFO_TX_LEVEL_WORD_6 (6U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 6 words \hideinitializer */
  67. #define I2S_FIFO_TX_LEVEL_WORD_7 (7U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 7 words \hideinitializer */
  68. #define I2S_FIFO_TX_LEVEL_WORD_8 (8U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 8 words \hideinitializer */
  69. #define I2S_FIFO_TX_LEVEL_WORD_9 (9U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 9 words \hideinitializer */
  70. #define I2S_FIFO_TX_LEVEL_WORD_10 (10U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 10 words \hideinitializer */
  71. #define I2S_FIFO_TX_LEVEL_WORD_11 (11U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 11 words \hideinitializer */
  72. #define I2S_FIFO_TX_LEVEL_WORD_12 (12U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 12 words \hideinitializer */
  73. #define I2S_FIFO_TX_LEVEL_WORD_13 (13U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 13 words \hideinitializer */
  74. #define I2S_FIFO_TX_LEVEL_WORD_14 (14U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 14 words \hideinitializer */
  75. #define I2S_FIFO_TX_LEVEL_WORD_15 (15U << I2S_CTL1_TXTH_Pos) /*!< TX threshold is 15 words \hideinitializer */
  76. #define I2S_FIFO_RX_LEVEL_WORD_1 (0U) /*!< RX threshold is 1 word \hideinitializer */
  77. #define I2S_FIFO_RX_LEVEL_WORD_2 (1U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 2 words \hideinitializer */
  78. #define I2S_FIFO_RX_LEVEL_WORD_3 (2U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 3 words \hideinitializer */
  79. #define I2S_FIFO_RX_LEVEL_WORD_4 (3U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 4 words \hideinitializer */
  80. #define I2S_FIFO_RX_LEVEL_WORD_5 (4U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 5 words \hideinitializer */
  81. #define I2S_FIFO_RX_LEVEL_WORD_6 (5U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 6 words \hideinitializer */
  82. #define I2S_FIFO_RX_LEVEL_WORD_7 (6U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 7 words \hideinitializer */
  83. #define I2S_FIFO_RX_LEVEL_WORD_8 (7U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 8 words \hideinitializer */
  84. #define I2S_FIFO_RX_LEVEL_WORD_9 (8U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 9 words \hideinitializer */
  85. #define I2S_FIFO_RX_LEVEL_WORD_10 (9U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 10 words \hideinitializer */
  86. #define I2S_FIFO_RX_LEVEL_WORD_11 (10U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 11 words \hideinitializer */
  87. #define I2S_FIFO_RX_LEVEL_WORD_12 (11U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 12 words \hideinitializer */
  88. #define I2S_FIFO_RX_LEVEL_WORD_13 (12U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 13 words \hideinitializer */
  89. #define I2S_FIFO_RX_LEVEL_WORD_14 (13U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 14 words \hideinitializer */
  90. #define I2S_FIFO_RX_LEVEL_WORD_15 (14U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 15 words \hideinitializer */
  91. #define I2S_FIFO_RX_LEVEL_WORD_16 (15U << I2S_CTL1_RXTH_Pos) /*!< RX threshold is 16 words \hideinitializer */
  92. /* I2S Record Channel */
  93. #define I2S_MONO_RIGHT (0U) /*!< Record mono right channel \hideinitializer */
  94. #define I2S_MONO_LEFT I2S_CTL0_RXLCH_Msk /*!< Record mono left channel \hideinitializer */
  95. /* I2S Channel */
  96. #define I2S_RIGHT (0U) /*!< Select right channel \hideinitializer */
  97. #define I2S_LEFT (1U) /*!< Select left channel \hideinitializer */
  98. /*@}*/ /* end of group I2S_EXPORTED_CONSTANTS */
  99. /** @addtogroup I2S_EXPORTED_FUNCTIONS I2S Exported Functions
  100. @{
  101. */
  102. /**
  103. * @brief Enable zero cross detect function.
  104. * @param[in] i2s is the base address of I2S module.
  105. * @param[in] u32ChMask is the mask for channel number (valid value is from (1~8).
  106. * @return none
  107. * \hideinitializer
  108. */
  109. __STATIC_INLINE void I2S_ENABLE_TX_ZCD(I2S_T *i2s, uint32_t u32ChMask)
  110. {
  111. if ((u32ChMask > 0U) && (u32ChMask < 9U))
  112. {
  113. i2s->CTL1 |= ((uint32_t)1U << (u32ChMask - 1U));
  114. }
  115. }
  116. /**
  117. * @brief Disable zero cross detect function.
  118. * @param[in] i2s is the base address of I2S module.
  119. * @param[in] u32ChMask is the mask for channel number (valid value is from (1~8).
  120. * @return none
  121. * \hideinitializer
  122. */
  123. __STATIC_INLINE void I2S_DISABLE_TX_ZCD(I2S_T *i2s, uint32_t u32ChMask)
  124. {
  125. if ((u32ChMask > 0U) && (u32ChMask < 9U))
  126. {
  127. i2s->CTL1 &= ~((uint32_t)1U << (u32ChMask - 1U));
  128. }
  129. }
  130. /**
  131. * @brief Enable I2S Tx DMA function. I2S requests DMA to transfer data to Tx FIFO.
  132. * @param[in] i2s is the base address of I2S module.
  133. * @return none
  134. * \hideinitializer
  135. */
  136. #define I2S_ENABLE_TXDMA(i2s) ( (i2s)->CTL0 |= I2S_CTL0_TXPDMAEN_Msk )
  137. /**
  138. * @brief Disable I2S Tx DMA function. I2S requests DMA to transfer data to Tx FIFO.
  139. * @param[in] i2s is the base address of I2S module.
  140. * @return none
  141. * \hideinitializer
  142. */
  143. #define I2S_DISABLE_TXDMA(i2s) ( (i2s)->CTL0 &= ~I2S_CTL0_TXPDMAEN_Msk )
  144. /**
  145. * @brief Enable I2S Rx DMA function. I2S requests DMA to transfer data from Rx FIFO.
  146. * @param[in] i2s is the base address of I2S module.
  147. * @return none
  148. * \hideinitializer
  149. */
  150. #define I2S_ENABLE_RXDMA(i2s) ( (i2s)->CTL0 |= I2S_CTL0_RXPDMAEN_Msk )
  151. /**
  152. * @brief Disable I2S Rx DMA function. I2S requests DMA to transfer data from Rx FIFO.
  153. * @param[in] i2s is the base address of I2S module.
  154. * @return none
  155. * \hideinitializer
  156. */
  157. #define I2S_DISABLE_RXDMA(i2s) ( (i2s)->CTL0 &= ~I2S_CTL0_RXPDMAEN_Msk )
  158. /**
  159. * @brief Enable I2S Tx function .
  160. * @param[in] i2s is the base address of I2S module.
  161. * @return none
  162. * \hideinitializer
  163. */
  164. #define I2S_ENABLE_TX(i2s) ( (i2s)->CTL0 |= I2S_CTL0_TXEN_Msk )
  165. /**
  166. * @brief Disable I2S Tx function .
  167. * @param[in] i2s is the base address of I2S module.
  168. * @return none
  169. * \hideinitializer
  170. */
  171. #define I2S_DISABLE_TX(i2s) ( (i2s)->CTL0 &= ~I2S_CTL0_TXEN_Msk )
  172. /**
  173. * @brief Enable I2S Rx function .
  174. * @param[in] i2s is the base address of I2S module.
  175. * @return none
  176. * \hideinitializer
  177. */
  178. #define I2S_ENABLE_RX(i2s) ( (i2s)->CTL0 |= I2S_CTL0_RXEN_Msk )
  179. /**
  180. * @brief Disable I2S Rx function .
  181. * @param[in] i2s is the base address of I2S module.
  182. * @return none
  183. * \hideinitializer
  184. */
  185. #define I2S_DISABLE_RX(i2s) ( (i2s)->CTL0 &= ~I2S_CTL0_RXEN_Msk )
  186. /**
  187. * @brief Enable Tx Mute function .
  188. * @param[in] i2s is the base address of I2S module.
  189. * @return none
  190. * \hideinitializer
  191. */
  192. #define I2S_ENABLE_TX_MUTE(i2s) ( (i2s)->CTL0 |= I2S_CTL0_MUTE_Msk )
  193. /**
  194. * @brief Disable Tx Mute function .
  195. * @param[in] i2s is the base address of I2S module.
  196. * @return none
  197. * \hideinitializer
  198. */
  199. #define I2S_DISABLE_TX_MUTE(i2s) ( (i2s)->CTL0 &= ~I2S_CTL0_MUTE_Msk )
  200. /**
  201. * @brief Clear Tx FIFO. Internal pointer is reset to FIFO start point.
  202. * @param[in] i2s is the base address of I2S module.
  203. * @return none
  204. * \hideinitializer
  205. */
  206. #define I2S_CLR_TX_FIFO(i2s) ( (i2s)->CTL0 |= I2S_CTL0_TXFBCLR_Msk )
  207. /**
  208. * @brief Clear Rx FIFO. Internal pointer is reset to FIFO start point.
  209. * @param[in] i2s is the base address of I2S module.
  210. * @return none
  211. * \hideinitializer
  212. */
  213. #define I2S_CLR_RX_FIFO(i2s) ( (i2s)->CTL0 |= I2S_CTL0_RXFBCLR_Msk )
  214. /**
  215. * @brief This function sets the recording source channel when mono mode is used.
  216. * @param[in] i2s is the base address of I2S module.
  217. * @param[in] u32Ch left or right channel. Valid values are:
  218. * - \ref I2S_MONO_LEFT
  219. * - \ref I2S_MONO_RIGHT
  220. * @return none
  221. * \hideinitializer
  222. */
  223. __STATIC_INLINE void I2S_SET_MONO_RX_CHANNEL(I2S_T *i2s, uint32_t u32Ch)
  224. {
  225. u32Ch == I2S_MONO_LEFT ?
  226. (i2s->CTL0 |= I2S_CTL0_RXLCH_Msk) :
  227. (i2s->CTL0 &= ~I2S_CTL0_RXLCH_Msk);
  228. }
  229. /**
  230. * @brief Write data to I2S Tx FIFO.
  231. * @param[in] i2s is the base address of I2S module.
  232. * @param[in] u32Data: The data written to FIFO.
  233. * @return none
  234. * \hideinitializer
  235. */
  236. #define I2S_WRITE_TX_FIFO(i2s, u32Data) ( (i2s)->TXFIFO = (u32Data) )
  237. /**
  238. * @brief Read Rx FIFO.
  239. * @param[in] i2s is the base address of I2S module.
  240. * @return Data in Rx FIFO.
  241. * \hideinitializer
  242. */
  243. #define I2S_READ_RX_FIFO(i2s) ( (i2s)->RXFIFO )
  244. /**
  245. * @brief This function gets the interrupt flag according to the mask parameter.
  246. * @param[in] i2s is the base address of I2S module.
  247. * @param[in] u32Mask is the mask for the all interrupt flags.
  248. * @return The masked bit value of interrupt flag.
  249. * \hideinitializer
  250. */
  251. #define I2S_GET_INT_FLAG(i2s, u32Mask) ( (i2s)->STATUS0 & (u32Mask) )
  252. /**
  253. * @brief This function clears the interrupt flag according to the mask parameter.
  254. * @param[in] i2s is the base address of I2S module.
  255. * @param[in] u32Mask is the mask for the all interrupt flags.
  256. * @return none
  257. * \hideinitializer
  258. */
  259. #define I2S_CLR_INT_FLAG(i2s, u32Mask) ( (i2s)->STATUS0 |= (u32Mask) )
  260. /**
  261. * @brief This function gets the zero crossing interrupt flag according to the mask parameter.
  262. * @param[in] i2s is the base address of I2S module.
  263. * @param[in] u32Mask is the mask for the all interrupt flags.
  264. * @return The masked bit value of interrupt flag.
  265. * \hideinitializer
  266. */
  267. #define I2S_GET_ZC_INT_FLAG(i2s, u32Mask) ( (i2s)->STATUS1 & (u32Mask) )
  268. /**
  269. * @brief This function clears the zero crossing interrupt flag according to the mask parameter.
  270. * @param[in] i2s is the base address of I2S module.
  271. * @param[in] u32Mask is the mask for the all interrupt flags.
  272. * @return none
  273. * \hideinitializer
  274. */
  275. #define I2S_CLR_ZC_INT_FLAG(i2s, u32Mask) ( (i2s)->STATUS1 |= (u32Mask) )
  276. /**
  277. * @brief Get transmit FIFO level
  278. * @param[in] i2s is the base address of I2S module.
  279. * @return FIFO level
  280. * \hideinitializer
  281. */
  282. #define I2S_GET_TX_FIFO_LEVEL(i2s) ( (((i2s)->STATUS1 & I2S_STATUS1_TXCNT_Msk) >> I2S_STATUS1_TXCNT_Pos) & 0xF )
  283. /**
  284. * @brief Get receive FIFO level
  285. * @param[in] i2s is the base address of I2S module.
  286. * @return FIFO level
  287. * \hideinitializer
  288. */
  289. #define I2S_GET_RX_FIFO_LEVEL(i2s) ( (((i2s)->STATUS1 & I2S_STATUS1_RXCNT_Msk) >> I2S_STATUS1_RXCNT_Pos) & 0xF )
  290. uint32_t I2S_Open(I2S_T *i2s, uint32_t u32MasterSlave, uint32_t u32SampleRate, uint32_t u32WordWidth, uint32_t u32MonoData, uint32_t u32DataFormat);
  291. void I2S_Close(I2S_T *i2s);
  292. void I2S_EnableInt(I2S_T *i2s, uint32_t u32Mask);
  293. void I2S_DisableInt(I2S_T *i2s, uint32_t u32Mask);
  294. uint32_t I2S_EnableMCLK(I2S_T *i2s, uint32_t u32BusClock);
  295. void I2S_DisableMCLK(I2S_T *i2s);
  296. void I2S_SetFIFO(I2S_T *i2s, uint32_t u32TxThreshold, uint32_t u32RxThreshold);
  297. void I2S_ConfigureTDM(I2S_T *i2s, uint32_t u32ChannelWidth, uint32_t u32ChannelNum, uint32_t u32SyncWidth);
  298. /*@}*/ /* end of group I2S_EXPORTED_FUNCTIONS */
  299. /*@}*/ /* end of group I2S_Driver */
  300. /*@}*/ /* end of group Standard_Driver */
  301. #ifdef __cplusplus
  302. }
  303. #endif
  304. #endif /* __NU_I2S_H__ */