HPM SDK
HPMicro Software Development Kit
hpm_i2s_drv.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2023 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_I2S_DRV_H
9 #define HPM_I2S_DRV_H
10 #include "hpm_common.h"
11 #include "hpm_soc_feature.h"
12 #include "hpm_i2s_regs.h"
13 #include "hpm_i2s_common.h"
14 
25 #define I2S_DATA_LINE_0 (0U)
26 #define I2S_DATA_LINE_1 (1U)
27 #define I2S_DATA_LINE_2 (2U)
28 #define I2S_DATA_LINE_3 (3U)
29 #define I2S_DATA_LINE_MAX I2S_DATA_LINE_3
30 
34 typedef struct i2s_config {
49 
53 typedef struct i2x_transfer_config {
54  uint32_t sample_rate;
57  uint8_t channel_length; /* 16-bit or 32-bit */
58  uint8_t audio_depth; /* 16-bit, 24-bit, 32-bit */
60  uint8_t protocol;
61  uint8_t data_line;
64 
65 typedef enum {
68  i2s_fifo_error_irq_mask = I2S_CTRL_ERRIE_MASK, /*<! rx fifo overrun, tx fifo underrun */
70 
71 typedef enum {
72  i2s_data_line_rx_fifo_avail = 1U, /*<! data avail */
73  i2s_data_line_tx_fifo_avail = 2U, /*<! fifo empty avail */
77 
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81 
87 static inline void i2s_enable_tdm(I2S_Type *ptr)
88 {
89  ptr->CFGR |= I2S_CFGR_TDM_EN_MASK;
90 }
91 
97 static inline void i2s_disable_tdm(I2S_Type *ptr)
98 {
99  ptr->CFGR &= ~I2S_CFGR_TDM_EN_MASK;
100 }
101 
108 static inline void i2s_update_rx_fifo_threshold(I2S_Type *ptr, uint8_t threshold)
109 {
111  | I2S_FIFO_THRESH_RX_SET(threshold);
112 }
113 
120 static inline void i2s_update_tx_fifo_threshold(I2S_Type *ptr, uint8_t threshold)
121 {
123  | I2S_FIFO_THRESH_TX_SET(threshold);
124 }
125 
131 static inline void i2s_ungate_bclk(I2S_Type *ptr)
132 {
134 }
135 
141 static inline void i2s_gate_bclk(I2S_Type *ptr)
142 {
144 }
145 
151 static inline void i2s_ungate_mclk(I2S_Type *ptr)
152 {
154 }
155 
161 static inline void i2s_gate_mclk(I2S_Type *ptr)
162 {
164 }
165 
171 static inline void i2s_enable_tx_dma_request(I2S_Type *ptr)
172 {
174 }
175 
181 static inline void i2s_disable_tx_dma_request(I2S_Type *ptr)
182 {
183  ptr->CTRL &= ~I2S_CTRL_TX_DMA_EN_MASK;
184 }
185 
191 static inline void i2s_enable_rx_dma_request(I2S_Type *ptr)
192 {
194 }
195 
201 static inline void i2s_disable_rx_dma_request(I2S_Type *ptr)
202 {
203  ptr->CTRL &= ~I2S_CTRL_RX_DMA_EN_MASK;
204 }
205 
212 static inline void i2s_enable_irq(I2S_Type *ptr, uint32_t mask)
213 {
214  ptr->CTRL |= mask;
215 }
216 
223 static inline void i2s_disable_irq(I2S_Type *ptr, uint32_t mask)
224 {
225  ptr->CTRL &= ~mask;
226 }
227 
235 static inline void i2s_enable(I2S_Type *ptr)
236 {
237  ptr->CTRL |= I2S_CTRL_I2S_EN_MASK;
238 }
239 
247 static inline void i2s_disable(I2S_Type *ptr)
248 {
249  ptr->CTRL &= ~I2S_CTRL_I2S_EN_MASK;
250 }
251 
257 static inline void i2s_start(I2S_Type *ptr)
258 {
259  ptr->CTRL |= I2S_CTRL_I2S_EN_MASK;
260 }
261 
267 static inline void i2s_stop(I2S_Type *ptr)
268 {
269  ptr->CTRL &= ~I2S_CTRL_I2S_EN_MASK;
270 }
271 
278 static inline void i2s_enable_rx(I2S_Type *ptr, uint8_t rx_mask)
279 {
280  ptr->CTRL |= I2S_CTRL_RX_EN_SET(rx_mask);
281 }
282 
289 static inline void i2s_disable_rx(I2S_Type *ptr, uint8_t rx_mask)
290 {
291  ptr->CTRL &= ~I2S_CTRL_RX_EN_SET(rx_mask);
292 }
293 
300 static inline void i2s_enable_tx(I2S_Type *ptr, uint8_t tx_mask)
301 {
302  ptr->CTRL |= I2S_CTRL_TX_EN_SET(tx_mask);
303 }
304 
311 static inline void i2s_disable_tx(I2S_Type *ptr, uint8_t tx_mask)
312 {
313  ptr->CTRL &= ~I2S_CTRL_TX_EN_SET(tx_mask);
314 }
315 
321 static inline void i2s_reset_clock_gen(I2S_Type *ptr)
322 {
325 }
326 
335 static inline void i2s_reset_tx(I2S_Type *ptr)
336 {
337  /* disable I2S */
338  ptr->CTRL &= ~I2S_CTRL_I2S_EN_MASK;
339 
340  /* reset tx and clear fifo */
343 }
344 
353 static inline void i2s_reset_rx(I2S_Type *ptr)
354 {
355  /* disable I2S */
356  ptr->CTRL &= ~I2S_CTRL_I2S_EN_MASK;
357 
358  /* reset rx and clear fifo */
361 }
362 
371 static inline void i2s_reset_tx_rx(I2S_Type *ptr)
372 {
373  /* disable I2S */
374  ptr->CTRL &= ~I2S_CTRL_I2S_EN_MASK;
375 
376  /* reset tx/rx and clear fifo */
379 }
380 
390 void i2s_reset_all(I2S_Type *ptr);
391 
399 static inline uint32_t i2s_get_tx_fifo_level(I2S_Type *ptr)
400 {
401  return ptr->TFIFO_FILLINGS;
402 }
403 
412 static inline uint32_t i2s_get_tx_line_fifo_level(I2S_Type *ptr, uint8_t line)
413 {
414  return (i2s_get_tx_fifo_level(ptr) & (0xFF << (line << 3))) >> (line << 3);
415 }
416 
424 static inline uint32_t i2s_get_rx_fifo_level(I2S_Type *ptr)
425 {
426  return ptr->RFIFO_FILLINGS;
427 }
428 
437 static inline uint32_t i2s_get_rx_line_fifo_level(I2S_Type *ptr, uint8_t line)
438 {
439  return (i2s_get_rx_fifo_level(ptr) & (0xFF << (line << 3))) >> (line << 3);
440 }
441 
453 static inline uint32_t i2s_check_data_line_status(I2S_Type *ptr, uint8_t line)
454 {
455  volatile uint32_t reg_val = ptr->STA;
456  uint32_t bit_mask;
457  uint32_t stat = 0;
458 
459  bit_mask = 1 << (I2S_STA_RX_DA_SHIFT + line);
460  if ((bit_mask & reg_val) != 0) {
462  }
463 
464  bit_mask = 1 << (I2S_STA_TX_DN_SHIFT + line);
465  if ((bit_mask & reg_val) != 0) {
467  }
468 
469  bit_mask = 1 << (I2S_STA_RX_OV_SHIFT + line);
470  if ((bit_mask & reg_val) != 0) {
472  ptr->STA = bit_mask; /* clear flag: W1C*/
473  }
474 
475  bit_mask = 1 << (I2S_STA_TX_UD_SHIFT + line);
476  if ((bit_mask & reg_val) != 0) {
478  ptr->STA = bit_mask; /* clear flag: W1C*/
479  }
480 
481  return stat;
482 }
483 
491 static inline uint32_t i2s_get_irq_status(I2S_Type *ptr)
492 {
493  return ptr->STA;
494 }
495 
501 static inline void i2s_stop_transfer(I2S_Type *ptr)
502 {
503  i2s_disable(ptr);
504 }
505 
516 hpm_stat_t i2s_config_tx(I2S_Type *ptr, uint32_t mclk_in_hz, i2s_transfer_config_t *config);
517 
527 
538 hpm_stat_t i2s_config_rx(I2S_Type *ptr, uint32_t mclk_in_hz, i2s_transfer_config_t *config);
539 
550 
561 hpm_stat_t i2s_config_transfer(I2S_Type *ptr, uint32_t mclk_in_hz, i2s_transfer_config_t *config);
562 
573 
581 static inline void i2s_send_data(I2S_Type *ptr, uint8_t tx_line_index, uint32_t data)
582 {
583  ptr->TXD[tx_line_index] = data;
584 }
585 
593 static inline void i2s_receive_data(I2S_Type *ptr, uint8_t rx_line_index, uint32_t *data)
594 {
595  *data = ptr->RXD[rx_line_index];
596 }
597 
609 uint32_t i2s_send_buff(I2S_Type *ptr, uint8_t tx_line_index, uint8_t samplebits, uint8_t *src, uint32_t size);
610 
622 uint32_t i2s_receive_buff(I2S_Type *ptr, uint8_t rx_line_index, uint8_t samplebits, uint8_t *dst, uint32_t size);
623 
630 void i2s_get_default_config(I2S_Type *ptr, i2s_config_t *config);
631 
638 void i2s_init(I2S_Type *ptr, i2s_config_t *config);
639 
646 
653 
660 
674 hpm_stat_t i2s_fill_tx_dummy_data(I2S_Type *ptr, uint8_t data_line, uint8_t data_count);
675 
676 
677 #if defined(HPM_IP_FEATURE_I2S_BUFF_ALIGN_FRAME) && (HPM_IP_FEATURE_I2S_BUFF_ALIGN_FRAME)
685 static inline void i2s_enable_buff_align_frame(I2S_Type *ptr)
686 {
687  ptr->CTRL |= I2S_CTRL_FRC_ALIGN_FBUF_MASK;
688 }
689 
695 static inline void i2s_disable_buff_align_frame(I2S_Type *ptr)
696 {
697  ptr->CTRL &= ~I2S_CTRL_FRC_ALIGN_FBUF_MASK;
698 }
699 #endif
700 
701 
702 
707 #ifdef __cplusplus
708 }
709 #endif
710 
711 #endif /* HPM_I2S_DRV_H */
#define I2S_FIFO_THRESH_TX_SET(x)
Definition: hpm_i2s_regs.h:251
#define I2S_STA_TX_DN_SHIFT
Definition: hpm_i2s_regs.h:291
#define I2S_FIFO_THRESH_TX_MASK
Definition: hpm_i2s_regs.h:249
#define I2S_CTRL_TX_DMA_EN_MASK
Definition: hpm_i2s_regs.h:104
#define I2S_STA_RX_DA_SHIFT
Definition: hpm_i2s_regs.h:300
#define I2S_STA_RX_OV_SHIFT
Definition: hpm_i2s_regs.h:281
#define I2S_CTRL_TXFIFOCLR_MASK
Definition: hpm_i2s_regs.h:124
#define I2S_CTRL_RXDAIE_MASK
Definition: hpm_i2s_regs.h:81
#define I2S_CTRL_SFTRST_TX_MASK
Definition: hpm_i2s_regs.h:47
#define I2S_MISC_CFGR_MCLK_GATEOFF_MASK
Definition: hpm_i2s_regs.h:526
#define I2S_CTRL_RX_DMA_EN_MASK
Definition: hpm_i2s_regs.h:114
#define I2S_CFGR_BCLK_GATEOFF_MASK
Definition: hpm_i2s_regs.h:328
#define I2S_CTRL_I2S_EN_MASK
Definition: hpm_i2s_regs.h:164
#define I2S_CTRL_RX_EN_SET(x)
Definition: hpm_i2s_regs.h:156
#define I2S_FIFO_THRESH_RX_MASK
Definition: hpm_i2s_regs.h:259
#define I2S_CFGR_TDM_EN_MASK
Definition: hpm_i2s_regs.h:471
#define I2S_FIFO_THRESH_RX_SET(x)
Definition: hpm_i2s_regs.h:261
#define I2S_CTRL_SFTRST_CLKGEN_MASK
Definition: hpm_i2s_regs.h:57
#define I2S_CTRL_TXDNIE_MASK
Definition: hpm_i2s_regs.h:69
#define I2S_CTRL_ERRIE_MASK
Definition: hpm_i2s_regs.h:94
#define I2S_CTRL_TX_EN_SET(x)
Definition: hpm_i2s_regs.h:146
#define I2S_CTRL_RXFIFOCLR_MASK
Definition: hpm_i2s_regs.h:134
#define I2S_STA_TX_UD_SHIFT
Definition: hpm_i2s_regs.h:271
#define I2S_CTRL_SFTRST_RX_MASK
Definition: hpm_i2s_regs.h:37
uint32_t hpm_stat_t
Definition: hpm_common.h:123
static void i2s_enable_tdm(I2S_Type *ptr)
enable TDM
Definition: hpm_i2s_drv.h:87
i2s_data_line_stat_t
Definition: hpm_i2s_drv.h:71
void i2s_reset_all(I2S_Type *ptr)
I2S reset tx/rx and clock generator module.
Definition: hpm_i2s_drv.c:59
static void i2s_reset_tx_rx(I2S_Type *ptr)
I2S reset tx and rx function.
Definition: hpm_i2s_drv.h:371
static void i2s_enable_rx(I2S_Type *ptr, uint8_t rx_mask)
I2S enable rx function.
Definition: hpm_i2s_drv.h:278
static uint32_t i2s_get_tx_line_fifo_level(I2S_Type *ptr, uint8_t line)
I2S get data line tx fifo level.
Definition: hpm_i2s_drv.h:412
static void i2s_reset_rx(I2S_Type *ptr)
I2S reset rx function.
Definition: hpm_i2s_drv.h:353
static void i2s_disable_tx_dma_request(I2S_Type *ptr)
disable TX dma request
Definition: hpm_i2s_drv.h:181
void i2s_get_default_transfer_config_for_pdm(i2s_transfer_config_t *transfer)
I2S get default transfer config for pdm.
Definition: hpm_i2s_drv.c:442
hpm_stat_t i2s_config_rx(I2S_Type *ptr, uint32_t mclk_in_hz, i2s_transfer_config_t *config)
I2S config rx.
Definition: hpm_i2s_drv.c:318
static void i2s_stop(I2S_Type *ptr)
I2S stop.
Definition: hpm_i2s_drv.h:267
static void i2s_disable_tdm(I2S_Type *ptr)
disable TDM
Definition: hpm_i2s_drv.h:97
static void i2s_disable_tx(I2S_Type *ptr, uint8_t tx_mask)
I2S disbale tx function.
Definition: hpm_i2s_drv.h:311
struct i2x_transfer_config i2s_transfer_config_t
I2S transfer config.
hpm_stat_t i2s_config_tx_slave(I2S_Type *ptr, i2s_transfer_config_t *config)
I2S config tx for slave.
Definition: hpm_i2s_drv.c:310
static void i2s_enable(I2S_Type *ptr)
I2S enable.
Definition: hpm_i2s_drv.h:235
static void i2s_send_data(I2S_Type *ptr, uint8_t tx_line_index, uint32_t data)
I2S send data.
Definition: hpm_i2s_drv.h:581
static void i2s_enable_irq(I2S_Type *ptr, uint32_t mask)
enable IRQ
Definition: hpm_i2s_drv.h:212
static uint32_t i2s_get_irq_status(I2S_Type *ptr)
I2S get IRQ status.
Definition: hpm_i2s_drv.h:491
static void i2s_reset_tx(I2S_Type *ptr)
I2S reset tx function.
Definition: hpm_i2s_drv.h:335
static void i2s_stop_transfer(I2S_Type *ptr)
I2S stop transfer.
Definition: hpm_i2s_drv.h:501
hpm_stat_t i2s_config_transfer(I2S_Type *ptr, uint32_t mclk_in_hz, i2s_transfer_config_t *config)
I2S config transfer.
Definition: hpm_i2s_drv.c:343
struct i2s_config i2s_config_t
I2S config.
static void i2s_disable_rx_dma_request(I2S_Type *ptr)
disable RX dma request
Definition: hpm_i2s_drv.h:201
static void i2s_reset_clock_gen(I2S_Type *ptr)
I2S reset clock generator.
Definition: hpm_i2s_drv.h:321
static void i2s_gate_bclk(I2S_Type *ptr)
gete off BCLK
Definition: hpm_i2s_drv.h:141
static void i2s_enable_rx_dma_request(I2S_Type *ptr)
enable RX dma request
Definition: hpm_i2s_drv.h:191
hpm_stat_t i2s_config_transfer_slave(I2S_Type *ptr, i2s_transfer_config_t *config)
I2S config transfer for slave.
Definition: hpm_i2s_drv.c:360
void i2s_get_default_transfer_config_for_dao(i2s_transfer_config_t *transfer)
I2S get default transfer config for dao.
Definition: hpm_i2s_drv.c:452
static void i2s_enable_tx_dma_request(I2S_Type *ptr)
enable TX dma request
Definition: hpm_i2s_drv.h:171
static uint32_t i2s_get_rx_fifo_level(I2S_Type *ptr)
I2S get rx fifo level.
Definition: hpm_i2s_drv.h:424
i2s_irq_mask_t
Definition: hpm_i2s_drv.h:65
static uint32_t i2s_check_data_line_status(I2S_Type *ptr, uint8_t line)
Check I2S data line status.
Definition: hpm_i2s_drv.h:453
void i2s_init(I2S_Type *ptr, i2s_config_t *config)
I2S initialization.
Definition: hpm_i2s_drv.c:100
static uint32_t i2s_get_rx_line_fifo_level(I2S_Type *ptr, uint8_t line)
I2S get data line rx fifo level.
Definition: hpm_i2s_drv.h:437
static void i2s_ungate_bclk(I2S_Type *ptr)
open BCLK
Definition: hpm_i2s_drv.h:131
static void i2s_update_rx_fifo_threshold(I2S_Type *ptr, uint8_t threshold)
update rx fifo threshold
Definition: hpm_i2s_drv.h:108
hpm_stat_t i2s_config_rx_slave(I2S_Type *ptr, i2s_transfer_config_t *config)
I2S config rx for slave.
Definition: hpm_i2s_drv.c:335
void i2s_get_default_config(I2S_Type *ptr, i2s_config_t *config)
I2S get default config.
Definition: hpm_i2s_drv.c:82
static void i2s_update_tx_fifo_threshold(I2S_Type *ptr, uint8_t threshold)
update tx fifo threshold
Definition: hpm_i2s_drv.h:120
static void i2s_receive_data(I2S_Type *ptr, uint8_t rx_line_index, uint32_t *data)
I2S receive data.
Definition: hpm_i2s_drv.h:593
static void i2s_disable_irq(I2S_Type *ptr, uint32_t mask)
disable IRQ
Definition: hpm_i2s_drv.h:223
hpm_stat_t i2s_fill_tx_dummy_data(I2S_Type *ptr, uint8_t data_line, uint8_t data_count)
I2S fill dummy data into TX fifo.
Definition: hpm_i2s_drv.c:38
static uint32_t i2s_get_tx_fifo_level(I2S_Type *ptr)
I2S get tx fifo level.
Definition: hpm_i2s_drv.h:399
void i2s_get_default_transfer_config(i2s_transfer_config_t *transfer)
I2S get default transfer config.
Definition: hpm_i2s_drv.c:464
static void i2s_disable(I2S_Type *ptr)
I2S disable.
Definition: hpm_i2s_drv.h:247
static void i2s_start(I2S_Type *ptr)
I2S start.
Definition: hpm_i2s_drv.h:257
uint32_t i2s_receive_buff(I2S_Type *ptr, uint8_t rx_line_index, uint8_t samplebits, uint8_t *dst, uint32_t size)
I2S receive data in buff.
Definition: hpm_i2s_drv.c:404
static void i2s_ungate_mclk(I2S_Type *ptr)
open MCLK
Definition: hpm_i2s_drv.h:151
static void i2s_disable_rx(I2S_Type *ptr, uint8_t rx_mask)
I2S disable rx function.
Definition: hpm_i2s_drv.h:289
hpm_stat_t i2s_config_tx(I2S_Type *ptr, uint32_t mclk_in_hz, i2s_transfer_config_t *config)
I2S config tx.
Definition: hpm_i2s_drv.c:293
static void i2s_gate_mclk(I2S_Type *ptr)
gate off MCLK
Definition: hpm_i2s_drv.h:161
uint32_t i2s_send_buff(I2S_Type *ptr, uint8_t tx_line_index, uint8_t samplebits, uint8_t *src, uint32_t size)
I2S send data in buff.
Definition: hpm_i2s_drv.c:368
static void i2s_enable_tx(I2S_Type *ptr, uint8_t tx_mask)
I2S enable tx function.
Definition: hpm_i2s_drv.h:300
@ i2s_data_line_tx_fifo_avail
Definition: hpm_i2s_drv.h:73
@ i2s_data_line_tx_fifo_underrun
Definition: hpm_i2s_drv.h:75
@ i2s_data_line_rx_fifo_overrun
Definition: hpm_i2s_drv.h:74
@ i2s_data_line_rx_fifo_avail
Definition: hpm_i2s_drv.h:72
@ i2s_rx_fifo_threshold_irq_mask
Definition: hpm_i2s_drv.h:67
@ i2s_tx_fifo_threshold_irq_mask
Definition: hpm_i2s_drv.h:66
@ i2s_fifo_error_irq_mask
Definition: hpm_i2s_drv.h:68
static void size
Definition: hpm_math.h:6899
static hpm_stat_t transfer(void *host, hpm_serial_nor_transfer_seq_t *command_seq)
Definition: hpm_serial_nor_host_spi.c:55
Definition: hpm_i2s_regs.h:12
__R uint32_t RFIFO_FILLINGS
Definition: hpm_i2s_regs.h:14
__RW uint32_t CFGR
Definition: hpm_i2s_regs.h:22
__RW uint32_t CTRL
Definition: hpm_i2s_regs.h:13
__R uint32_t TFIFO_FILLINGS
Definition: hpm_i2s_regs.h:15
__R uint32_t RXD[4]
Definition: hpm_i2s_regs.h:19
__RW uint32_t FIFO_THRESH
Definition: hpm_i2s_regs.h:16
__RW uint32_t STA
Definition: hpm_i2s_regs.h:17
__RW uint32_t MISC_CFGR
Definition: hpm_i2s_regs.h:24
__W uint32_t TXD[4]
Definition: hpm_i2s_regs.h:20
I2S config.
Definition: hpm_i2s_drv.h:34
bool use_external_fclk
Definition: hpm_i2s_drv.h:43
bool invert_fclk_in
Definition: hpm_i2s_drv.h:42
bool invert_bclk_out
Definition: hpm_i2s_drv.h:38
bool invert_mclk_out
Definition: hpm_i2s_drv.h:35
bool use_external_bclk
Definition: hpm_i2s_drv.h:40
bool frame_start_at_rising_edge
Definition: hpm_i2s_drv.h:45
bool use_external_mclk
Definition: hpm_i2s_drv.h:37
bool invert_fclk_out
Definition: hpm_i2s_drv.h:41
uint16_t tx_fifo_threshold
Definition: hpm_i2s_drv.h:46
bool invert_bclk_in
Definition: hpm_i2s_drv.h:39
uint16_t rx_fifo_threshold
Definition: hpm_i2s_drv.h:47
bool enable_mclk_out
Definition: hpm_i2s_drv.h:44
bool invert_mclk_in
Definition: hpm_i2s_drv.h:36
I2S transfer config.
Definition: hpm_i2s_drv.h:53
uint8_t data_line
Definition: hpm_i2s_drv.h:61
uint8_t channel_length
Definition: hpm_i2s_drv.h:57
uint8_t channel_num_per_frame
Definition: hpm_i2s_drv.h:56
uint8_t audio_depth
Definition: hpm_i2s_drv.h:58
uint32_t sample_rate
Definition: hpm_i2s_drv.h:54
uint32_t channel_slot_mask
Definition: hpm_i2s_drv.h:62
uint8_t protocol
Definition: hpm_i2s_drv.h:60
bool enable_tdm_mode
Definition: hpm_i2s_drv.h:55
bool master_mode
Definition: hpm_i2s_drv.h:59