備忘録代わりの投稿.
ESP32-S3-DevKitC-1 を入手したので,I2S接続の Audio CODEC つなげて動かしてみた.
開発環境はArduino IDE.ESP32のボードマネージャはバージョン2.0.5をインストールした.
(2.0.3以降でS3に対応しているらしい)

CODECには PCM3060PW を使った.
まずESP32用のI2Sのスケッチ例があったので試してみたがまともに動作せず.これらは libraries フォルダにある I2S.h, I2S.cpp を使っていたのでその中身を見てみると,サンプリング周波数が16kHz以下かつ16bitで使うようにコメントされていて無駄な時間を費やしてしまった.
結局 driver フォルダの i2s.h を使い,下記のコードでうまく動作した.
// I2S on ESP32-S3
// T.Uebo October 1 , 2022
// I2S Master MODE 48kHz/32bit
//
// Mclk GPIO 0
// Bclk GPIO 42
// LRclk GPIO 2
// Dout GPIO 41
// Din GPIO 1
#include <driver/i2s.h>
#define fs 48000
#define BLOCK_SAMPLES 128
#define Reset_CODEC 40 // HW Reset signal for CODEC
//buffers
int rxbuf[BLOCK_SAMPLES*2], txbuf[BLOCK_SAMPLES*2];
float Lch_in[BLOCK_SAMPLES], Rch_in[BLOCK_SAMPLES];
float Lch_out[BLOCK_SAMPLES], Rch_out[BLOCK_SAMPLES];
void setup(void) {
//HW Reset for CODEC
pinMode(Reset_CODEC, OUTPUT);
digitalWrite(Reset_CODEC, HIGH);
delay(1);
digitalWrite(Reset_CODEC, LOW);
delay(1);
digitalWrite(Reset_CODEC, HIGH);
// setup I2S
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX),
.sample_rate = fs,
.bits_per_sample = (i2s_bits_per_sample_t)32,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_I2S),
.intr_alloc_flags = 0,
.dma_buf_count = 6,
.dma_buf_len = BLOCK_SAMPLES*4,
.use_apll = false,
.tx_desc_auto_clear = true,
.fixed_mclk = 0,
};
i2s_driver_install( I2S_NUM_0, &i2s_config, 0, NULL);
i2s_pin_config_t pin_config = {
.bck_io_num = 42,
.ws_io_num = 2,
.data_out_num = 41,
.data_in_num = 1
};
i2s_set_pin( I2S_NUM_0, &pin_config);
i2s_zero_dma_buffer(I2S_NUM_0);
}
void loop(void) {
size_t readsize = 0;
//Input from I2S codec
esp_err_t rxfb = i2s_read(I2S_NUM_0, &rxbuf[0], BLOCK_SAMPLES*2*4, &readsize, portMAX_DELAY);
if (rxfb == ESP_OK && readsize==BLOCK_SAMPLES*2*4) {
int j=0;
for (int i=0; i<BLOCK_SAMPLES; i++) {
Lch_in[i] = (float) rxbuf[j];
Rch_in[i] = (float) rxbuf[j+1];
j+=2;
}
//-------Signal process -------------------------------
for (int i=0; i<BLOCK_SAMPLES; i++) {
Lch_out[i] = Lch_in[i];
Rch_out[i] = Rch_in[i];
}
//------------------------------------------------------
//Output to I2S codec
j=0;
for (int i=0; i<BLOCK_SAMPLES; i++) {
txbuf[j] = (int) Lch_out[i];
txbuf[j+1] = (int) Rch_out[i];
j+=2;
}
i2s_write( I2S_NUM_0, &txbuf[0], BLOCK_SAMPLES*2*4, &readsize, portMAX_DELAY);
}
}