103 lines
3.3 KiB
C++
103 lines
3.3 KiB
C++
#include "infra/I2S.h"
|
|
#include "config/config.h"
|
|
|
|
#define SAMPLE_RATE (16000)
|
|
|
|
// Define pins for I2S_NUM_0
|
|
#define PIN_I2S_0_BCLK (GPIO_NUM_26)
|
|
#define PIN_I2S_0_LRC (GPIO_NUM_25)
|
|
#define PIN_I2S_0_DIN (GPIO_NUM_3)
|
|
#define PIN_I2S_0_DOUT (GPIO_NUM_22)
|
|
#define PIN_I2S_0_MCK (GPIO_NUM_0)
|
|
|
|
// Define pins for I2S_NUM_1
|
|
#define PIN_I2S_1_BCLK (GPIO_NUM_32)
|
|
#define PIN_I2S_1_LRC (GPIO_NUM_33)
|
|
#define PIN_I2S_1_DIN (GPIO_NUM_35)
|
|
#define PIN_I2S_1_DOUT (GPIO_NUM_0)
|
|
#define PIN_I2S_1_MCK (GPIO_NUM_0)
|
|
|
|
SemaphoreHandle_t dmaInterruptSemaphore;
|
|
static intr_handle_t dmaInterruptRetHandle;
|
|
|
|
// This I2S specification :
|
|
// - LRC high is channel 2 (right).
|
|
// - LRC signal transitions once each word.
|
|
// - DATA is valid on the CLOCK rising edge.
|
|
// - Data bits are MSB first.
|
|
// - DATA bits are left-aligned with respect to LRC edge.
|
|
// - DATA bits are right-shifted by one with respect to LRC edges.
|
|
I2S::I2S(MicType micType, i2s_port_t i2sNum) : _i2sNum(i2sNum) {
|
|
if (micType == M5GO || micType == M5STACKFIRE ) {
|
|
BITS_PER_SAMPLE = I2S_BITS_PER_SAMPLE_16BIT;
|
|
i2s_config_t i2s_config = {
|
|
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN),
|
|
.sample_rate = SAMPLE_RATE,
|
|
.bits_per_sample = BITS_PER_SAMPLE,
|
|
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
|
|
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S_MSB),
|
|
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
|
|
.dma_buf_count = 2,
|
|
.dma_buf_len = 1024
|
|
};
|
|
i2s_driver_install(_i2sNum, &i2s_config, 0, NULL);
|
|
i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_6);
|
|
i2s_set_clk(_i2sNum, SAMPLE_RATE, BITS_PER_SAMPLE, I2S_CHANNEL_STEREO);
|
|
i2s_adc_enable(_i2sNum);
|
|
}
|
|
else if (micType == ADMP441 || micType == ICS43434 ) {
|
|
BITS_PER_SAMPLE = I2S_BITS_PER_SAMPLE_16BIT;// I2S_BITS_PER_SAMPLE_32BIT;
|
|
i2s_config_t i2s_config = {
|
|
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
|
|
.sample_rate = SAMPLE_RATE,
|
|
.bits_per_sample = BITS_PER_SAMPLE,
|
|
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
|
|
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
|
|
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
|
|
.dma_buf_count = 16,
|
|
.dma_buf_len = 60,
|
|
};
|
|
|
|
i2s_driver_install(_i2sNum, &i2s_config, 0, NULL);
|
|
_setPins();
|
|
i2s_set_clk(_i2sNum, SAMPLE_RATE, BITS_PER_SAMPLE, I2S_CHANNEL_STEREO);
|
|
i2s_zero_dma_buffer(_i2sNum);
|
|
}
|
|
|
|
dmaInterruptSemaphore = xSemaphoreCreateBinary();
|
|
}
|
|
|
|
void I2S::_setPins() {
|
|
i2s_pin_config_t pin_config;
|
|
|
|
if (_i2sNum == I2S_NUM_0) {
|
|
pin_config.bck_io_num = PIN_I2S_0_BCLK;
|
|
pin_config.ws_io_num = PIN_I2S_0_LRC;
|
|
pin_config.data_out_num = I2S_PIN_NO_CHANGE;
|
|
pin_config.data_in_num = PIN_I2S_0_DIN;
|
|
pin_config.mck_io_num = PIN_I2S_0_MCK;
|
|
} else if (_i2sNum == I2S_NUM_1) {
|
|
pin_config.bck_io_num = PIN_I2S_1_BCLK;
|
|
pin_config.ws_io_num = PIN_I2S_1_LRC;
|
|
pin_config.data_out_num = I2S_PIN_NO_CHANGE;
|
|
pin_config.data_in_num = PIN_I2S_1_DIN;
|
|
pin_config.mck_io_num = PIN_I2S_1_MCK;
|
|
}
|
|
|
|
i2s_set_pin(_i2sNum, &pin_config);
|
|
}
|
|
|
|
int I2S::Read(char* data, int numData) {
|
|
size_t bytes_read;
|
|
|
|
if (i2s_read(_i2sNum, (char *)data, numData, &bytes_read, portMAX_DELAY)) {
|
|
return (int)bytes_read;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
int I2S::GetBitPerSample() {
|
|
return (int)BITS_PER_SAMPLE;
|
|
}
|