InPlay API
hal_i2s.h
1 
13 #ifndef HAL_I2S_H
14 #define HAL_I2S_H
15 
25 #include "stdint.h"
26 #include "in_mmap.h"
27 #include "in_compile.h"
28 /*
29  * Defines
30  ****************************************************************************************
31  */
32 
33 #define I2S_CH0_BASE_OFS (0x20)
34 #define I2S_CH1_BASE_OFS (0x60)
35 
36 #define MI2S_CH0_BASE (I2S_MASTER_BASE + I2S_CH0_BASE_OFS)
37 #define MI2S_CH1_BASE (I2S_MASTER_BASE + I2S_CH1_BASE_OFS)
38 
39 #define SI2S_CH0_BASE (I2S_SLAVE_BASE + I2S_CH0_BASE_OFS)
40 #define SI2S_CH1_BASE (I2S_SLAVE_BASE + I2S_CH1_BASE_OFS)
41 
42 #define I2S_REG_IER_OFS 0x00000000UL
43 #define I2S_REG_IRER_OFS 0x00000004UL
44 #define I2S_REG_ITER_OFS 0x00000008UL
45 #define I2S_REG_CER_OFS 0x0000000CUL
46 #define I2S_REG_CCR_OFS 0x00000010UL
47 #define I2S_REG_RXFFR_OFS 0x00000014UL
48 #define I2S_REG_TXFFR_OFS 0x00000018UL
49 #define I2S_REG_LRBR_OFS 0x00000000UL
50 #define I2S_REG_LTHR_OFS 0x00000000UL
51 #define I2S_REG_RRBR_OFS 0x00000004UL
52 #define I2S_REG_RTHR_OFS 0x00000004UL
53 #define I2S_REG_RER_OFS 0x00000008UL
54 #define I2S_REG_TER_OFS 0x0000000CUL
55 #define I2S_REG_RCR_OFS 0x00000010UL
56 #define I2S_REG_TCR_OFS 0x00000014UL
57 #define I2S_REG_ISR_OFS 0x00000018UL
58 #define I2S_REG_IMR_OFS 0x0000001CUL
59 #define I2S_REG_ROR_OFS 0x00000020UL
60 #define I2S_REG_TOR_OFS 0x00000024UL
61 #define I2S_REG_RFCR_OFS 0x00000028UL
62 #define I2S_REG_TFCR_OFS 0x0000002CUL
63 #define I2S_REG_RFF_OFS 0x00000030UL
64 #define I2S_REG_TFF_OFS 0x00000034UL
65 #define I2S_REG_RXDMA_OFS 0x000001C0UL
66 #define I2S_REG_RRXDMA_OFS 0x000001C4UL
67 #define I2S_REG_TXDMA_OFS 0x000001C8UL
68 #define I2S_REG_RTXDMA_OFS 0x000001CCUL
69 
70 /*
71  * Enumerations
72  ****************************************************************************************
73  */
74 
76 enum i2s_id {
78  MI2S_ID = 1,
80  SI2S_ID = 2,
81 };
82 
91 };
92 
94 enum i2s_sr {
96  I2S_SR_8K = 8000,
98  I2S_SR_16K = 16000,
100  I2S_SR_24K = 24000,
102  I2S_SR_32K = 32000,
104  I2S_SR_36K = 36000,
106  I2S_SR_44K = 44100,
108  I2S_SR_48K = 48000,
109 };
110 
112 enum i2s_type {
114  I2S_MONO = 1,
117 };
118 
120 enum i2s_ch_id {
127 };
128 
135 };
136 
151 };
152 
154 enum i2s_error {
169 } ;
170 
171 /*
172  * Inline Functions
173  ****************************************************************************************
174  */
175 
176 static INLINE void i2s_enable(uint32_t base)
177 {
178  WR_WORD((base + I2S_REG_IER_OFS), 1);
179 }
180 
181 static INLINE void i2s_disable(uint32_t base)
182 {
183  WR_WORD((base + I2S_REG_IER_OFS), 0);
184 }
185 
186 static INLINE void i2s_rx_enable(uint32_t base)
187 {
188  WR_WORD((base + I2S_REG_IRER_OFS), 1);
189 }
190 
191 static INLINE void i2s_rx_disable(uint32_t base)
192 {
193  WR_WORD((base + I2S_REG_IRER_OFS), 0);
194 }
195 
196 static INLINE void i2s_tx_enable(uint32_t base)
197 {
198  WR_WORD((base + I2S_REG_ITER_OFS), 1);
199 }
200 
201 static INLINE void i2s_tx_disable(uint32_t base)
202 {
203  WR_WORD((base + I2S_REG_ITER_OFS), 0);
204 }
205 
206 static INLINE void i2s_sclk_enable(uint32_t base)
207 {
208  WR_WORD((base + I2S_REG_CER_OFS), 1);
209 }
210 
211 static INLINE void i2s_sclk_disable(uint32_t base)
212 {
213  WR_WORD((base + I2S_REG_CER_OFS), 0);
214 }
215 
216 #define I2S_CCR_SCLKG 0x00000007UL
217 #define I2S_CCR_WSS 0x00000018UL
218 
219 #define I2S_CCR_SCLKG_NG 0
220 #define I2S_CCR_SCLKG_12 1
221 #define I2S_CCR_SCLKG_16 2
222 #define I2S_CCR_SCLKG_20 3
223 #define I2S_CCR_SCLKG_24 4
224 
226 #define I2S_CCR_WSS_16 0
227 #define I2S_CCR_WSS_24 1
228 #define I2S_CCR_WSS_32 2
229 static INLINE void i2s_sclk_wss(uint32_t base, int wss)
230 {
231  uint32_t reg = RD_WORD(base + I2S_REG_CCR_OFS);
232 
233  reg &= ~I2S_CCR_WSS;
234  reg |= ((wss & 0x3) << 3);
235 
236  WR_WORD((base + I2S_REG_CCR_OFS), reg);
237 }
238 
239 static INLINE void i2s_sclk_gate(uint32_t base, int gated_clk)
240 {
241  uint32_t reg = RD_WORD(base + I2S_REG_CCR_OFS);
242 
243  reg &= ~I2S_CCR_SCLKG;
244  reg |= (gated_clk & 0x7);
245 
246  WR_WORD((base + I2S_REG_CCR_OFS), reg);
247 }
248 
249 static INLINE void i2s_rx_fifo_reset(uint32_t base)
250 {
251  WR_WORD((base + I2S_REG_RXFFR_OFS), 1);
252 }
253 
254 static INLINE void i2s_tx_fifo_reset(uint32_t base)
255 {
256  WR_WORD((base + I2S_REG_TXFFR_OFS), 1);
257 }
258 
259 static INLINE uint32_t i2s_chx_lrx_data(uint32_t ch_base)
260 {
261  return RD_WORD(ch_base + I2S_REG_LRBR_OFS);
262 }
263 
264 static INLINE void i2s_chx_ltx_data(uint32_t ch_base, uint32_t data)
265 {
266  WR_WORD((ch_base + I2S_REG_LTHR_OFS), data);
267 }
268 
269 static INLINE uint32_t i2s_chx_rrx_data(uint32_t ch_base)
270 {
271  return RD_WORD(ch_base + I2S_REG_RRBR_OFS);
272 }
273 
274 static INLINE void i2s_chx_rtx_data(uint32_t ch_base, uint32_t data)
275 {
276  WR_WORD((ch_base + I2S_REG_RTHR_OFS), data);
277 }
278 
279 static INLINE void i2s_chx_rx_enable(uint32_t ch_base)
280 {
281  WR_WORD((ch_base + I2S_REG_RER_OFS), 1);
282 }
283 
284 static INLINE void i2s_chx_rx_disable(uint32_t ch_base)
285 {
286  WR_WORD((ch_base + I2S_REG_RER_OFS), 0);
287 }
288 
289 static INLINE void i2s_chx_tx_enable(uint32_t ch_base)
290 {
291  WR_WORD((ch_base + I2S_REG_TER_OFS), 1);
292 }
293 
294 static INLINE void i2s_chx_tx_disable(uint32_t ch_base)
295 {
296  WR_WORD((ch_base + I2S_REG_TER_OFS), 0);
297 }
298 
299 #define I2S_CHx_RCR_WLEN 0x00000007UL
300 
301 #define I2S_CHx_WLEN_0 0
302 #define I2S_CHx_WLEN_12 1
303 #define I2S_CHx_WLEN_16 2
304 #define I2S_CHx_WLEN_20 3
305 #define I2S_CHx_WLEN_24 4
306 #define I2S_CHx_WLEN_32 5
307 
308 static INLINE void i2s_chx_rx_wlen(uint32_t ch_base, int word_len)
309 {
310  WR_WORD((ch_base + I2S_REG_RCR_OFS), (word_len & 0x7));
311 }
312 
313 #define I2S_CHx_TCR_WLEN 0x00000007UL
314 
315 static INLINE void i2s_chx_tx_wlen(uint32_t ch_base, int word_len)
316 {
317  WR_WORD((ch_base + I2S_REG_TCR_OFS), (word_len & 0x7));
318 }
319 
320 #define I2S_CHx_IT_RXDA 0x00000001UL
321 #define I2S_CHx_IT_RXFO 0x00000002UL
322 #define I2S_CHx_IT_TXFE 0x00000010UL
323 #define I2S_CHx_IT_TXFO 0x00000020UL
324 #define I2S_CHx_IT_ALL 0x00000033UL
325 
326 static INLINE uint32_t i2s_chx_intr_status(uint32_t ch_base)
327 {
328  return (RD_WORD(ch_base + I2S_REG_ISR_OFS));
329 }
330 
331 static INLINE void i2s_chx_intr_mask(uint32_t ch_base, uint32_t mask)
332 {
333  uint32_t reg = RD_WORD(ch_base + I2S_REG_IMR_OFS);
334  reg |= mask;
335  WR_WORD((ch_base + I2S_REG_IMR_OFS), reg);
336 }
337 
338 static INLINE void i2s_chx_intr_unmask(uint32_t ch_base, uint32_t mask)
339 {
340  uint32_t reg = RD_WORD(ch_base + I2S_REG_IMR_OFS);
341  reg &= ~mask;
342  WR_WORD((ch_base + I2S_REG_IMR_OFS), reg);
343 }
344 
345 static INLINE void i2s_chx_rov_clr(uint32_t ch_base)
346 {
347  RD_WORD(ch_base + I2S_REG_ROR_OFS);
348 }
349 
350 static INLINE void i2s_chx_tov_clr(uint32_t ch_base)
351 {
352  RD_WORD(ch_base + I2S_REG_TOR_OFS);
353 }
354 
355 #define I2S_CHx_RFCR_RXCHDT 0x0000000FUL
356 static INLINE void i2s_chx_rfifo_tl(uint32_t ch_base, uint32_t tl)
357 {
358  WR_WORD((ch_base + I2S_REG_RFCR_OFS), (tl & 0xF));
359 }
360 
361 #define I2S_CHx_TFCR_TXCHDT 0x0000000FUL
362 static INLINE void i2s_chx_tfifo_tl(uint32_t ch_base, uint32_t tl)
363 {
364  WR_WORD((ch_base + I2S_REG_TFCR_OFS), (tl & 0xF));
365 }
366 
367 static INLINE void i2s_chx_rfifo_flush(uint32_t ch_base)
368 {
369  WR_WORD((ch_base + I2S_REG_RFF_OFS), 1);
370 }
371 
372 static INLINE void i2s_chx_tfifo_flush(uint32_t ch_base)
373 {
374  WR_WORD((ch_base + I2S_REG_TFF_OFS), 1);
375 }
376 
377 static INLINE void i2s_sd_oe(int id, int ch_id, int oe)
378 {
379  uint32_t reg = RD_WORD(GLOBAL_REG_I2S_OEN_CTRL);
380  if (id == MI2S_ID) {
381  if (ch_id == I2S_CH_1) {
382  if (oe) {
383  reg &= ~GLOBAL_REG_I2S_OEN_CTRL_CTL_I2S_MASTER_PIN0_OEN;
384  } else {
385  reg |= GLOBAL_REG_I2S_OEN_CTRL_CTL_I2S_MASTER_PIN0_OEN;
386  }
387  } else {
388  if (oe) {
389  reg &= ~GLOBAL_REG_I2S_OEN_CTRL_CTL_I2S_MASTER_PIN1_OEN;
390  } else {
391  reg |= GLOBAL_REG_I2S_OEN_CTRL_CTL_I2S_MASTER_PIN1_OEN;
392  }
393  }
394  } else {
395  if (ch_id == I2S_CH_1) {
396  if (oe) {
397  reg &= ~GLOBAL_REG_I2S_OEN_CTRL_CTL_I2S_SLAVE_SD_OEN;
398  } else {
399  reg |= GLOBAL_REG_I2S_OEN_CTRL_CTL_I2S_SLAVE_SD_OEN;
400  }
401  } else {
402  if (oe) {
403  reg &= ~GLOBAL_REG_I2S_OEN_CTRL_CTL_I2S_SLAVE_SD_BKUP_OEN;
404  } else {
405  reg |= GLOBAL_REG_I2S_OEN_CTRL_CTL_I2S_SLAVE_SD_BKUP_OEN;
406  }
407  }
408  }
409  WR_WORD(GLOBAL_REG_I2S_OEN_CTRL, reg);
410 }
411 
412 static INLINE void i2s_txdma_start(uint32_t base)
413 {
414  WR_WORD((base + I2S_REG_TXDMA_OFS), 0);
415 }
416 
417 /*
418  * APIs
419  ****************************************************************************************
420  */
421 
433 void *hal_i2s_open(int id, int sr, int word_sz);
434 
445 int hal_i2s_close(void *hdl) ;
446 
491 int hal_i2s_ch_en(void *hdl, int ch_id, int dir, int ch_word_sz, void *buffer0, void *buffer1, uint16_t buffer_len, void *arg, void (*callback)(void * arg, int id, int status) );
492 
504 int hal_i2s_ch_dis(void *hdl, int ch_id, int dir);
505 
507 
508 #endif
Don&#39;t care.
Definition: hal_i2s.h:140
i2s_error
I2S driver return code.
Definition: hal_i2s.h:154
Sample rate at 32Khz.
Definition: hal_i2s.h:102
Error. DMA tranfer.
Definition: hal_i2s.h:168
I2S Slave Core.
Definition: hal_i2s.h:80
Stereo data.
Definition: hal_i2s.h:116
i2s_type
I2S data type.
Definition: hal_i2s.h:112
Sample rate at 44.1Khz.
Definition: hal_i2s.h:106
Error. Invalid state.
Definition: hal_i2s.h:160
Channel word size is 32 bits.
Definition: hal_i2s.h:150
Transmit.
Definition: hal_i2s.h:132
int hal_i2s_close(void *hdl)
Close I2S driver.
Channel word size is 24 bits.
Definition: hal_i2s.h:148
Error. I2S channel TX overflow.
Definition: hal_i2s.h:164
Channel word size is 20 bits.
Definition: hal_i2s.h:146
Max sample word size is 16 bits.
Definition: hal_i2s.h:86
i2s_ch_dir
Each I2S channel can be either TX or RX.
Definition: hal_i2s.h:130
Max sample word size is 32 bits.
Definition: hal_i2s.h:90
I2S Channel 1.
Definition: hal_i2s.h:122
Error. Invalid param.
Definition: hal_i2s.h:158
int hal_i2s_ch_en(void *hdl, int ch_id, int dir, int ch_word_sz, void *buffer0, void *buffer1, uint16_t buffer_len, void *arg, void(*callback)(void *arg, int id, int status))
Enable I2S channel.
Error. DMA is not available.
Definition: hal_i2s.h:166
int hal_i2s_ch_dis(void *hdl, int ch_id, int dir)
Disable I2S channel.
void * hal_i2s_open(int id, int sr, int word_sz)
Initialize I2S driver.
Sample rate at 36Khz.
Definition: hal_i2s.h:104
i2s_sr
I2S sample rate.
Definition: hal_i2s.h:94
i2s_ch_word_sz
Each I2S channel word size.
Definition: hal_i2s.h:138
Sample rate at 8Khz.
Definition: hal_i2s.h:96
i2s_id
I2S Core ID.
Definition: hal_i2s.h:76
Channel word size is 16 bits.
Definition: hal_i2s.h:144
Error. I2S channel RX overflow.
Definition: hal_i2s.h:162
Receive.
Definition: hal_i2s.h:134
Sample rate at 24Khz.
Definition: hal_i2s.h:100
I2S Master Core.
Definition: hal_i2s.h:78
Mono data.
Definition: hal_i2s.h:114
Sample rate at 16Khz.
Definition: hal_i2s.h:98
Sample rate at 48Khz.
Definition: hal_i2s.h:108
I2S max channel.
Definition: hal_i2s.h:126
Max sample word size is 24 bits.
Definition: hal_i2s.h:88
i2s_word_size
I2S Core max word size.
Definition: hal_i2s.h:84
I2S Channel 2.
Definition: hal_i2s.h:124
Channel word size is 12 bits.
Definition: hal_i2s.h:142
No error. Good.
Definition: hal_i2s.h:156
i2s_ch_id
Each I2S Core can have 2 Channel.
Definition: hal_i2s.h:120