I2S
概述
I2S(Inter-IC Sound Bus) 是为数字音频设备之间的音频数据传输而指定的一种总线标准,广泛应用于各种多媒体系统。I2S 主要有以下 3 种信号:
串行时钟信号 BCK:每一位串行数据都有一个BCK脉冲对应。
声道选择信号 WS:用于指示左右声道的数据。WS为 "0" 默认表示正在传输的是左声道数据,为 "1" 表示正在传输的是右声道数据。
串行数据信号 SDATA:实际音频数据。
WQ7036 有 2 个 I2S 模块,可以工作在不同的时钟频率。
功能特性
I2S 模块支持:
支持 2 个 I2S,包括 2 个 Tx, 2 个 Rx
支持每个 Tx/Rx 最大8 channel 数据
支持双通道/单通道的 16 bit,24 bit,32 bit 数据格式
支持 标准 Philips 和非 Philips 模式(左右对齐)
Standard Philips 格式:数据信号与 WS 信号相比有一个位的位移。WS 信号的占空比为 50%
Left justified 格式:与 Standard Philips 格式基本相同,但其数据没有位移。
Right justified 格式:数据有一个位的位移,同时 WS 信号变成脉冲,持续一个 BCLK 周期。
支持 TX/RX 分别配置成 master 或者 slave 模式
I2S 收发数据过程支持 DMA 传输
支持最高 49.152 MHz Bclk
支持 WS 脉冲形式, 1 slot 模式, 50% 占空比模式
支持 STANDARD/TDM/PCM 模式
支持 4 条数据线同时工作
支持外部时钟或多种内部时钟选择
支持 16 k, 24 k, 32 k, 48 k, 62.5 k, 96 k, 125 k, 128 k, 192 k, 256 k 等多种采样率配置 (需选择不同 AUDIO 时钟源)
最大支持上行 16 路channel、下行 8 channel 同时工作
内部时钟源选择:XTAL_CLK, APLL_CLK
XTAL_CLK: 32 MHz
APLL_CLK: 主要为307.2 MHz
数据流
I2S 收发数据流如下图所示。
I2S 收发数据流
API 介绍
This section introduces the I2S module's enum, structure, functions and how to use this driver.
-
struct i2s_tx_irq_cfg
Public Members
-
WQ_I2S_LINE_ID tx_data_line_id
I2s TX dataline id
-
WQ_I2S_TX_CHANNEL tx_ch
I2s TX channel select
-
WQ_I2S_INTC_TYPE intc_type
I2s TX interrupt type
-
WQ_I2S_LINE_ID tx_data_line_id
-
struct i2s_rx_irq_cfg
Public Members
-
WQ_I2S_LINE_ID rx_data_line_id
I2s RX dataline id
-
WQ_I2S_RX_CHANNEL rx_ch
I2s RX channel select
-
WQ_I2S_INTC_TYPE intc_type
I2s RX interrupt type
-
WQ_I2S_LINE_ID rx_data_line_id
-
struct i2s_tdm_mode_config
Public Members
-
WQ_I2S_TDM_WORK_MODE tdm_mode
I2s work mode
-
uint8_t chn_num
the number of channels used by I2S
-
WQ_I2S_TDM_WORK_MODE tdm_mode
-
struct i2s_gpio_config
Public Members
-
WQ_GPIO_ID ws
I2S ws GPIO ID
-
WQ_GPIO_ID bck
I2S bck GPIO ID
-
WQ_GPIO_ID data[WQ_I2S_LINE_ID_MAX]
I2S dataline GPIO ID
-
WQ_GPIO_ID mclk
I2S mclk GPIO ID
-
WQ_GPIO_ID ws
-
struct mclk_src_config
Public Members
-
bool mclk_en
I2S mclk enable
-
bool mclk_out
i2s mclk trans direction, true is out, false is in, effective when mclk_en is true
-
WQ_I2S_MCLK_SRC mclk_src
I2S mclk source
-
bool mclk_en
-
struct i2s_tx_line_config
Public Members
-
bool line_enable[WQ_I2S_LINE_ID_MAX]
I2S dataline enable state
-
WQ_I2S_TX_CHANNEL tx_channel[WQ_I2S_LINE_ID_MAX]
I2S all dataline cahnnel state
-
WQ_I2S_TX_DATA_SRC tx_data_src
data source of I2S tx
-
bool line_enable[WQ_I2S_LINE_ID_MAX]
-
struct i2s_rx_line_config
Public Members
-
bool line_enable[WQ_I2S_LINE_ID_MAX]
I2S dataline enable state
-
WQ_I2S_RX_CHANNEL rx_channel[WQ_I2S_LINE_ID_MAX]
I2S all dataline cahnnel state
-
WQ_I2S_RX_DATA_DST rx_data_dst
data destination of I2S rx
-
bool line_enable[WQ_I2S_LINE_ID_MAX]
-
union i2s_data_line_config
Public Members
-
i2s_tx_line_config_t tx_line_config
I2S tx dataline config
-
i2s_rx_line_config_t rx_line_config
I2S rx dataline config
-
i2s_tx_line_config_t tx_line_config
-
struct i2s_tx_register_irq
Public Members
-
wq_i2s_data_intc_hook tx_cb
I2S tx irq callback
-
i2s_tx_irq_cfg_t tx_irq_cfg
I2S tx irq config
-
wq_i2s_data_intc_hook tx_cb
-
struct i2s_rx_register_irq
Public Members
-
wq_i2s_data_intc_hook rx_cb
I2S rx irq callback
-
i2s_rx_irq_cfg_t rx_irq_cfg
I2S rx irq config
-
wq_i2s_data_intc_hook rx_cb
-
union i2s_register_irq
Public Members
-
i2s_tx_register_irq_t tx_register_irq
information related to tx interrupts
-
i2s_rx_register_irq_t rx_register_irq
information related to rx interrupts
-
i2s_tx_register_irq_t tx_register_irq
-
struct wq_i2s_config
Public Members
-
i2s_gpio_cfg_t gpio
I2S pin info
-
mclk_src_config_t mclk_cfg
I2S mclk info
-
i2s_data_line_config_u data_line
I2S dataline config info
-
i2s_tdm_mode_config_t tdm_mode_config
I2S work mode config info
-
i2s_register_irq_u register_irq
register I2S tx or rx irq
-
WQ_I2S_SAMPLE_RATE freq
config I2S sample rate
-
WQ_I2S_MODE trans_mode
config I2S transmode, such as master_tx、master_rx...
-
WQ_I2S_WORK_MODE word_mode
config I2S data alignment mode
-
WQ_I2S_DATA_FORMAT format
config I2S data format
-
WQ_I2S_MODULE i2s_module
the hardware module to which I2S belongs
-
WQ_I2S_TRIGGER_SRC trigger
trigger source of I2S
-
bool right_first
config transmit or receive right-channel data first
-
bool single_ch
I2S single channel transmit and receive enable
-
bool pcm_mode
ws clk use pcm or i2s mode
-
i2s_gpio_cfg_t gpio
-
struct wq_i2s_start_cfg
Public Members
-
WQ_I2S_MODULE i2s_module
the hardware module to which I2S belongs
-
WQ_I2S_MODULE i2s_module
Typedefs
-
typedef enum _WQ_I2S_TRANS_MODE WQ_I2S_TRANS_MODE
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_MODULE WQ_I2S_MODULE
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_TX_DATA_SRC WQ_I2S_TX_DATA_SRC
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_INTC_TYPE WQ_I2S_INTC_TYPE
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_TX_CHANNEL WQ_I2S_TX_CHANNEL
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_RX_CHANNEL WQ_I2S_RX_CHANNEL
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_MCLK_SRC WQ_I2S_MCLK_SRC
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_LINE_ID WQ_I2S_LINE_ID
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_WORK_MODE WQ_I2S_WORK_MODE
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_TDM_WORK_MODE WQ_I2S_TDM_WORK_MODE
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_MODE WQ_I2S_MODE
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_DATA_FORMAT WQ_I2S_DATA_FORMAT
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_RX_DATA_DST WQ_I2S_RX_DATA_DST
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_TRIGGER_SRC WQ_I2S_TRIGGER_SRC
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef enum _WQ_I2S_SAMPLE_RATE WQ_I2S_SAMPLE_RATE
For specific definitions, please refer to the header file wq_audio_resource.h.
-
typedef uint8_t (*wq_i2s_data_intc_hook)(uint32_t id)
This is the i2s interrupt callback function.
备注
When i2s is in tx mode and its data comes from txfifo, it is possible to register a txfifo empty interrupt and continue to package in the interrupt callback;When i2s is in rx mode and rxfifo receives data from i2s, it can register a rxfifo full interrupt and continue to package in the interrupt callback
-
typedef struct i2s_tx_irq_cfg i2s_tx_irq_cfg_t
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef struct i2s_rx_irq_cfg i2s_rx_irq_cfg_t
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef struct i2s_tdm_mode_config i2s_tdm_mode_config_t
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef struct i2s_gpio_config i2s_gpio_cfg_t
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef struct mclk_src_config mclk_src_config_t
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef struct i2s_tx_line_config i2s_tx_line_config_t
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef struct i2s_rx_line_config i2s_rx_line_config_t
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef union i2s_data_line_config i2s_data_line_config_u
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef struct i2s_tx_register_irq i2s_tx_register_irq_t
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef struct i2s_rx_register_irq i2s_rx_register_irq_t
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef union i2s_register_irq i2s_register_irq_u
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef struct wq_i2s_config wq_i2s_config_t
For specific definitions, please refer to the header file wq_i2s.h.
-
typedef struct wq_i2s_start_cfg wq_i2s_start_cfg_t
For specific definitions, please refer to the header file wq_i2s.h.
Functions
-
WQ_RET wq_i2s_init(WQ_I2S_TRANS_MODE mode)
This function is to init i2s.
- 参数:
mode -- i2s trans mode tx or rx
- 返回:
uint8_t WQ_RET_OK for success else error.
-
WQ_RET wq_i2s_deinit(WQ_I2S_TRANS_MODE mode)
This function is to deinit i2s.
- 参数:
mode -- i2s trans mode tx or rx
- 返回:
uint8_t WQ_RET_OK for success else error.
-
void wq_i2s_start_all_trigger(void)
This function is to start i2s by sync mode.
-
void wq_i2s_stop_all_trigger(void)
This function is to stop i2s by sync mode.
-
WQ_RET wq_i2s_start(const wq_i2s_start_cfg_t *cfg)
This function is to start i2s module.
- 参数:
cfg -- config i2s start parameter
- 返回:
uint8_t WQ_RET_OK for success else error.
-
WQ_RET wq_i2s_stop(WQ_I2S_MODULE module)
This function is to stop i2s module.
- 参数:
module -- i2s module, e.g: WQ_I2S_MODULE_RX0,WQ_I2S_MODULE_TX0
- 返回:
uint8_t WQ_RET_OK for success else error.
-
WQ_RET wq_i2s_open(const wq_i2s_config_t *i2s_config)
This function is to open i2s, mainly complete hardware pin functions, transmission data format, sampling rate and other configurations.
- 参数:
i2s_config -- the config to open i2s
- 返回:
uint8_t WQ_RET_OK for success else error.
-
WQ_RET wq_i2s_close(const wq_i2s_config_t *i2s_config)
This function is to close i2s, mainly responsible for clearing hardware pin functions, transmission data formats, sampling rates, and other configurations, which is opposite to the function of wq_i2s_open.
- 参数:
i2s_config -- the config to close i2s
- 返回:
uint8_t WQ_RET_OK for success else error.
-
int8_t wq_i2s_get_start_cnt(WQ_I2S_MODULE module)
get i2s open refcounter
- 参数:
module -- i2s module, e.g: WQ_I2S_MODULE_TX0
- 返回:
open refcounter
-
WQ_RET wq_i2s_ext_pa_on(WQ_I2S_MODULE module, uint8_t chn)
i2s external PA open
- 参数:
module -- i2s module, e.g: WQ_I2S_MODULE_TX0
chn -- 0:left,1:right,2:dual channel
- 返回:
WQ_RET for success else error.
-
WQ_RET wq_i2s_ext_pa_off(WQ_I2S_MODULE module, uint8_t chn)
i2s external PA close
- 参数:
module -- i2s module, e.g: WQ_I2S_MODULE_TX0
chn -- 0:left,1:right,2:dual channel
- 返回:
WQ_RET for success else error.
-
WQ_RET wq_i2s_ext_pa_set(uint8_t channels, bool on) __attribute__((weak))
weak function, Invoked when i2s tx data running by trigger
- 参数:
channels -- i2s data channel number
on -- true is external PA on,false is external PA off
- 返回:
uint8_t WQ_RET_OK for success else error.
-
WQ_RET wq_i2s_ext_pa_mute(uint8_t channels) __attribute__((weak))
weak function, mute pa digital gain
- 参数:
channels -- i2s data channel number
- 返回:
uint8_t WQ_RET_OK for success else error.
-
WQ_RET wq_i2s_ext_pa_unmute(uint8_t channels) __attribute__((weak))
weak function, unmute pa digital gain
- 参数:
channels -- i2s data channel number
- 返回:
uint8_t WQ_RET_OK for success else error.
-
WQ_RET wq_i2s_ext_pa_set_volume(uint8_t channels, float db) __attribute__((weak))
weak function, set pa digital gain volume
- 参数:
channels -- i2s data channel number
db -- volume unit is dB
- 返回:
uint8_t WQ_RET_OK for success else error.
-
float wq_i2s_ext_pa_get_volume(uint8_t channels) __attribute__((weak))
weak function, get pa digital gain volume
- 参数:
channels -- i2s data channel number
- 返回:
int16_t unit is dB