wm_i2s.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #ifndef __WM_I2S_H
  2. #define __WM_I2S_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <stdbool.h>
  7. //#include "wm_regs_cm3.h"
  8. #include "wm_regs.h"
  9. #include "wm_debug.h"
  10. #include "wm_dma.h"
  11. typedef void (*tls_i2s_callback)(uint32_t *data, uint16_t *len);
  12. typedef struct {
  13. __IO uint32_t CTRL;
  14. __IO uint32_t INT_MASK;
  15. __IO uint32_t INT_SRC;
  16. __I uint32_t INT_STATUS;
  17. __O uint32_t TX;
  18. __I uint32_t RX;
  19. } I2S_T;
  20. typedef struct {
  21. uint32_t I2S_Mode_MS; //master or slave mode
  22. uint32_t I2S_Mode_SS; //stereo or single channel
  23. uint32_t I2S_Mode_LR; //left or right channel
  24. uint32_t I2S_Trans_STD;
  25. uint32_t I2S_DataFormat;
  26. uint32_t I2S_AudioFreq;
  27. uint32_t I2S_MclkFreq;
  28. } I2S_InitDef;
  29. typedef struct _wm_dma_desc
  30. {
  31. unsigned int valid;
  32. unsigned int dma_ctrl;
  33. unsigned int src_addr;
  34. unsigned int dest_addr;
  35. struct _wm_dma_desc * next;
  36. }wm_dma_desc;
  37. typedef struct _dma_handler_type
  38. {
  39. uint8_t channel;
  40. void (* XferCpltCallback)( struct _dma_handler_type * hdma); /*!< DMA transfer complete callback */
  41. void (* XferHalfCpltCallback)( struct _dma_handler_type * hdma); /*!< DMA Half transfer complete callback */
  42. }wm_dma_handler_type;
  43. #define I2S ((I2S_T *)HR_I2S_REG_BASE)
  44. #define I2S_MODE_MASTER ((bool)0x0)
  45. #define I2S_MODE_SLAVE ((bool)0x1)
  46. #define I2S_RIGHT_CHANNEL ((bool)0x0)
  47. #define I2S_LEFT_CHANNEL ((bool)0x1)
  48. #define I2S_Standard (0x0UL)
  49. #define I2S_Standard_MSB (0x1000000UL)
  50. #define I2S_Standard_PCMA (0x2000000UL)
  51. #define I2S_Standard_PCMB (0x3000000UL)
  52. #define I2S_DataFormat_8 (8)
  53. #define I2S_DataFormat_16 (16)
  54. #define I2S_DataFormat_24 (24)
  55. #define I2S_DataFormat_32 (32)
  56. #define I2S_CTRL_CHSEL_MASK (1UL<<23)
  57. #define I2S_CTRL_CHSEL_LEFT (1UL<<23)
  58. #define I2S_CTRL_MONO (1UL<<22)
  59. #define I2S_CTRL_STEREO (0UL<<22)
  60. #define I2S_CTRL_RXDMA_EN (1UL<<21)
  61. #define I2S_CTRL_TXDMA_EN (1UL<<20)
  62. #define I2S_CTRL_RX_CLR (1UL<<19)
  63. #define I2S_CTRL_TX_CLR (1UL<<18)
  64. #define I2S_CTRL_LZCEN (1UL<<17)
  65. #define I2S_CTRL_RZCEN (1UL<<16)
  66. #define I2S_CTRL_RXTH(n) ((n-1)<<12)
  67. #define I2S_CTRL_TXTH(n) ((n)<<9)
  68. #define I2S_CTRL_SLAVE_SEL (1UL<<8)
  69. #define I2S_CTRL_MUTE (1UL<<3)
  70. #define I2S_CTRL_RXE (1UL<<2)
  71. #define I2S_CTRL_TXE (1UL<<1)
  72. #define I2S_CTRL_EN (1UL<<0)
  73. #define I2S_INT_MASK_LZC ((uint16_t)0x200)
  74. #define I2S_INT_MASK_RZC ((uint16_t)0x100)
  75. #define I2S_INT_MASK_TXDONE ((uint16_t)0x080)
  76. #define I2S_INT_MASK_TXTH ((uint16_t)0x040)
  77. #define I2S_INT_MASK_TXOV ((uint16_t)0x020)
  78. #define I2S_INT_MASK_TXUD ((uint16_t)0x010)
  79. #define I2S_INT_MASK_RXDONE ((uint16_t)0x008)
  80. #define I2S_INT_MASK_RXTH ((uint16_t)0x004)
  81. #define I2S_INT_MASK_RXOV ((uint16_t)0x002)
  82. #define I2S_INT_MASK_RXUD ((uint16_t)0x002)
  83. #define I2S_FLAG_TX ((uint16_t)0x1000)
  84. #define I2S_FLAG_RX ((uint16_t)0x0800)
  85. #define I2S_FLAG_I2S ((uint16_t)0x0400)
  86. #define I2S_FLAG_LZC ((uint16_t)0x0200)
  87. #define I2S_FLAG_RZC ((uint16_t)0x0100)
  88. #define I2S_FLAG_TXDONE ((uint16_t)0x0080)
  89. #define I2S_FLAG_TXTH ((uint16_t)0x0040)
  90. #define I2S_FLAG_TXOV ((uint16_t)0x0020)
  91. #define I2S_FLAG_TXUD ((uint16_t)0x0010)
  92. #define I2S_FLAG_RXDONE ((uint16_t)0x0008)
  93. #define I2S_FLAG_RXTH ((uint16_t)0x0004)
  94. #define I2S_FLAG_RXOV ((uint16_t)0x0002)
  95. #define I2S_FLAG_RXUD ((uint16_t)0x0001)
  96. #define WM_I2S_TX_DMA_CHANNEL (1)
  97. #define WM_I2S_RX_DMA_CHANNEL (5)
  98. typedef struct wm_i2s_buf_s {
  99. volatile uint32_t *txbuf;
  100. volatile uint32_t txlen;
  101. volatile uint32_t txtail;
  102. volatile uint32_t *rxbuf;
  103. volatile uint32_t rxlen;
  104. volatile uint32_t int_txlen;
  105. volatile uint32_t rxhead;
  106. volatile uint8_t rxdata_ready;
  107. volatile uint8_t txdata_done;
  108. /** function pointer for data receiver */
  109. void (*rx_callback)(void);
  110. /** function pointer for data transmit */
  111. void (*tx_callback)(uint32_t *data, uint16_t *len);
  112. } wm_i2s_buf_t;
  113. /**
  114. * @defgroup Driver_APIs Driver APIs
  115. * @brief Driver APIs
  116. */
  117. /**
  118. * @addtogroup Driver_APIs
  119. * @{
  120. */
  121. /**
  122. * @defgroup I2S_Driver_APIs I2S Driver APIs
  123. * @brief I2S driver APIs
  124. */
  125. /**
  126. * @addtogroup I2S_Driver_APIs
  127. * @{
  128. */
  129. void wm_i2s_enable(bool bl);
  130. void wm_i2s_tx_dma_enable(bool bl);
  131. void wm_i2s_tx_enable(bool bl);
  132. void wm_i2s_rx_enable(bool bl);
  133. /**
  134. * @brief Register a callback function
  135. * @param callback pointer to a callback function in which you can prepare the next buffer
  136. * @param callback->data pointer to data buffer to be prepared
  137. * @param callback->len size of the data buffer to be prepared in 32-bit
  138. * @note The registerred callback function will be called as long as the transmission begins
  139. * @retval none
  140. */
  141. void wm_i2s_register_callback(tls_i2s_callback callback);
  142. /**
  143. * @brief Initializes the I2S according to the specified parameters
  144. * in the I2S_InitDef.
  145. * @param opts pointer to a I2S_InitDef structure that contains
  146. * the configuration information for I2S module
  147. * @retval status
  148. */
  149. int wm_i2s_port_init(I2S_InitDef *opts);
  150. /**
  151. * @brief stop i2s module
  152. * @retval none
  153. */
  154. void wm_i2s_tx_rx_stop(void);
  155. /**
  156. * @brief Transmit an amount of data in blocking mode with Interrupt
  157. * @param data a 16-bit pointer to data buffer.
  158. * @param len number of data sample to be sent:
  159. * @param next_data a 16-bit pointer to the next data buffer, same size with data; set to NULL if it's not needed
  160. * @note the len parameter means the number of 16-bit data length.
  161. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  162. * between Master and Slave(example: audio streaming).
  163. * @note This function will block its task until the transmission is over,so perpare the next data
  164. * buffer at another task during this interval.
  165. * @note This function will call the registerred callback function as long as the transmission begins
  166. * @retval status
  167. */
  168. int wm_i2s_tx_int(int16_t *data, uint16_t len, int16_t *next_data);
  169. /**
  170. * @brief Transmit an amount of data in blocking mode with DMA's normal mode
  171. * @param data a 16-bit pointer to data buffer.
  172. * @param len number of data sample to be sent:
  173. * @param next_data a 16-bit pointer to the next data buffer, same size with data; set to NULL if it's not needed
  174. * @note the len parameter means the number of 32-bit data length.
  175. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  176. * between Master and Slave(example: audio streaming).
  177. * @note This function will block its task until the transmission is over,so perpare the next data
  178. * buffer at another task during this interval.
  179. * @note This function will call the registerred callback function as long as the transmission begins
  180. * @retval status
  181. */
  182. int wm_i2s_tx_dma(int16_t *data, uint16_t len, int16_t *next_data);
  183. /**
  184. * @brief Transmit an amount of data in blocking mode with DMA's link mode
  185. * @param data a 16-bit pointer to data buffer.
  186. * @param len number of data sample to be sent:
  187. * @param next_data a 16-bit pointer to the next data buffer, same size with data:
  188. * @note the len parameter means the number of 32-bit data length.
  189. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  190. * between Master and Slave(example: audio streaming).
  191. * @note This function will block its task until the transmission is over,so perpare the next data
  192. * buffer at another task during this interval.Set len to 0xffff will exit this rountine.
  193. * @note This function will call the registerred callback function as long as the data or next_data
  194. * is sent out.So prepare it in the callback.
  195. * @note See the demo for detail use.
  196. * @retval status
  197. */
  198. int wm_i2s_tx_dma_link(int16_t *data, uint16_t len, int16_t *next_data);
  199. /**
  200. * @brief Receive an amount of data in blocking mode with Interrupt
  201. * @param data a 16-bit pointer to the Receive data buffer.
  202. * @param len number of data sample to be received:
  203. * @note the len parameter means the number of 16-bit data length.
  204. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  205. * between Master and Slave(example: audio streaming).
  206. * @note This function will block its task until the transmission is over,so perpare the next data
  207. * buffer at another task during this interval.
  208. * @retval status
  209. */
  210. int wm_i2s_rx_int(int16_t *data, uint16_t len);
  211. /**
  212. * @brief Receive an amount of data in blocking mode with DMA
  213. * @param data a 16-bit pointer to the Receive data buffer.
  214. * @param len number of data sample to be received:
  215. * @note the len parameter means the number of 16-bit data length.
  216. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  217. * between Master and Slave(example: audio streaming).
  218. * @note This function will block its task until the transmission is over,so perpare the next data
  219. * buffer at another task during this interval.
  220. * @retval status
  221. */
  222. int wm_i2s_rx_dma(int16_t *data, uint16_t len);
  223. /**
  224. * @brief Full-Duplex Transmit/Receive data in blocking mode using Interrupt
  225. * @param opts pointer to a I2S_InitDef structure that contains
  226. * the configuration information for I2S module
  227. * @param data_tx a 16-bit pointer to the Transmit data buffer.
  228. * @param data_rx a 16-bit pointer to the Receive data buffer.
  229. * @param len number of data sample to be sent:
  230. * @note the len parameter means the number of 16-bit data length.
  231. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  232. * between Master and Slave(example: audio streaming).
  233. * @note This function will block its task until the transmission is over,so perpare the next data
  234. * buffer at another task during this interval.
  235. * @retval status
  236. */
  237. int wm_i2s_tx_rx_int(I2S_InitDef *opts, int16_t *data_tx, int16_t *data_rx, uint16_t len);
  238. /**
  239. * @brief Full-Duplex Transmit/Receive data in blocking mode using DMA
  240. * @param opts pointer to a I2S_InitDef structure that contains
  241. * the configuration information for I2S module
  242. * @param data_tx a 16-bit pointer to the Transmit data buffer.
  243. * @param data_rx a 16-bit pointer to the Receive data buffer.
  244. * @param len number of data sample to be sent:
  245. * @note the len parameter means the number of 16-bit data length.
  246. * @note The I2S is kept enabled at the end of transaction to avoid the clock de-synchronization
  247. * between Master and Slave(example: audio streaming).
  248. * @note This function will block its task until the transmission is over,so perpare the next data
  249. * buffer at another task during this interval.
  250. * @retval status
  251. */
  252. int wm_i2s_tx_rx_dma(I2S_InitDef *opts, int16_t *data_tx, int16_t *data_rx, uint16_t len);
  253. int wm_i2s_transmit_dma(wm_dma_handler_type *hdma, uint16_t *data, uint16_t len);
  254. int wm_i2s_receive_dma(wm_dma_handler_type *hdma, uint16_t *data, uint16_t len);
  255. void wm_i2s_mono_select(bool bl);
  256. void wm_i2s_left_channel_sel(bool bl);
  257. void wm_i2s_set_word_len(uint8_t len);
  258. void wm_i2s_set_freq(uint32_t lr_freq, uint32_t mclk);
  259. /**
  260. * @}
  261. */
  262. /**
  263. * @}
  264. */
  265. #ifdef __cplusplus
  266. }
  267. #endif
  268. #endif