33 *
44 * SPDX-License-Identifier: MIT
55 *
6- * @Hardwares: M5Cores3 + Module Audio
6+ * @Hardwares: M5CoreS3 + Module Audio
77 * @Dependent Library:
88 * M5Unified@^0.2.5: https://github.com/m5stack/M5Unified
99 * Module Audio: https://github.com/m5stack/M5Module-Audio
10+ *
11+ * Please rollback these libraries if you encounter I2S initialization issues.
12+ * Board Manager M5Stack@2.0.7;
13+ * M5Unified@0.1.17;
1014 */
1115
1216#include " M5Unified.h"
1317#include " audio_i2c.hpp"
1418#include " es8388.hpp"
15- # include " driver/i2s.h "
19+
1620AudioI2c device;
1721
22+ #define USE_NEW_I2S_API 1
23+ #define RECORD_SIZE (44100 * 2 * 2 * 2 )
24+
25+ #if USE_NEW_I2S_API
26+ #include < ESP_I2S.h>
27+ I2SClass I2S ;
28+ #else
29+ #include " driver/i2s.h"
30+ #endif
31+
1832#define SYS_I2C_SDA_PIN 12
1933#define SYS_I2C_SCL_PIN 11
2034#define SYS_I2S_MCLK_PIN 0
@@ -28,30 +42,11 @@ AudioI2c device;
2842#define SYS_SPI_CS_PIN 4
2943
3044ES8388 es8388 (&Wire, SYS_I2C_SDA_PIN , SYS_I2C_SCL_PIN );
31-
32- uint16_t rxbuf[256 ], txbuf[256 ];
45+ static uint8_t *buffer = nullptr ;
46+ uint16_t rxbuf[1024 ], txbuf[1024 ];
3347size_t readsize = 0 ;
3448byte error, address;
3549const uint32_t color[] = {0xFF0000 , 0xFF0000 , 0xFF0000 , 0x00FF00 , 0xFFFF00 , 0xFFFFFF , 0xFFFFFF , 0xFFFFFF , 0xFFFFFF };
36- i2s_config_t i2s_config = {.mode = (i2s_mode_t )(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX ),
37- .sample_rate = 44100 ,
38- .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT ,
39- .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT ,
40- .communication_format = I2S_COMM_FORMAT_STAND_I2S ,
41- .intr_alloc_flags = 0 ,
42- .dma_buf_count = 8 ,
43- .dma_buf_len = 512 ,
44- .use_apll = false ,
45- .tx_desc_auto_clear = true ,
46- .fixed_mclk = 0 };
47-
48- i2s_pin_config_t pin_config = {
49- .mck_io_num = SYS_I2S_MCLK_PIN ,
50- .bck_io_num = SYS_I2S_SCLK_PIN ,
51- .ws_io_num = SYS_I2S_LRCK_PIN ,
52- .data_out_num = SYS_I2S_DOUT_PIN ,
53- .data_in_num = SYS_I2S_DIN_PIN ,
54- };
5550
5651void setup ()
5752{
@@ -96,21 +91,117 @@ void setup()
9691 reg = es8388.readAllReg ();
9792 Serial.printf (" Reg-%02d = 0x%02x\r\n " , i, reg[i]);
9893 }
99- // i2s
94+ #if USE_NEW_I2S_API
95+ uint32_t sample_rate = 44100 ;
96+ uint8_t i2s_bck = SYS_I2S_SCLK_PIN ;
97+ uint8_t i2s_ws = SYS_I2S_LRCK_PIN ;
98+ uint8_t i2s_do = SYS_I2S_DOUT_PIN ;
99+ uint8_t i2s_di = SYS_I2S_DIN_PIN ;
100+ uint8_t i2s_mc = SYS_I2S_MCLK_PIN ;
101+ I2S .setPins (i2s_bck, i2s_ws, i2s_do, i2s_di, i2s_mc); // SCK, WS, SDOUT, SDIN, MCLK
102+ I2S .begin (I2S_MODE_STD , 44100 , I2S_DATA_BIT_WIDTH_16BIT , I2S_SLOT_MODE_STEREO );
103+ I2S .configureRX (
104+ 44100 ,
105+ I2S_DATA_BIT_WIDTH_16BIT ,
106+ I2S_SLOT_MODE_STEREO
107+ );
108+
109+ I2S .configureTX (
110+ 44100 ,
111+ I2S_DATA_BIT_WIDTH_16BIT ,
112+ I2S_SLOT_MODE_STEREO
113+ );
114+ #else
115+ i2s_config_t i2s_config = {.mode = (i2s_mode_t )(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX ),
116+ .sample_rate = 44100 ,
117+ .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT ,
118+ .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT ,
119+ .communication_format = I2S_COMM_FORMAT_STAND_I2S ,
120+ .intr_alloc_flags = 0 ,
121+ .dma_buf_count = 8 ,
122+ .dma_buf_len = 512 ,
123+ .use_apll = false ,
124+ .tx_desc_auto_clear = true ,
125+ .fixed_mclk = 0 };
126+
127+ i2s_pin_config_t pin_config = {
128+ .mck_io_num = SYS_I2S_MCLK_PIN ,
129+ .bck_io_num = SYS_I2S_SCLK_PIN ,
130+ .ws_io_num = SYS_I2S_LRCK_PIN ,
131+ .data_out_num = SYS_I2S_DOUT_PIN ,
132+ .data_in_num = SYS_I2S_DIN_PIN ,
133+ };
134+
100135 PIN_FUNC_SELECT (PERIPHS_IO_MUX_GPIO0_U , FUNC_GPIO0_CLK_OUT1 );
101136 WRITE_PERI_REG (PIN_CTRL , 0xFFF0 );
102137 i2s_driver_install (I2S_NUM_0 , &i2s_config, 0 , NULL );
103138 i2s_set_pin (I2S_NUM_0 , &pin_config);
104- }
105139
140+ #endif
141+ buffer = (uint8_t *)malloc (RECORD_SIZE ); // Allocate memory for the record buffer.
142+ // Check if memory allocation was successful.
143+ if (buffer == nullptr ) {
144+ // If memory allocation fails, enter an infinite loop.
145+ while (true ) {
146+ Serial.println (" Failed to allocate memory :(" );
147+ delay (1000 );
148+ }
149+ }
150+ }
106151void loop ()
107152{
108- i2s_read (I2S_NUM_0 , &rxbuf[0 ], 256 * 2 , &readsize, 1000 );
109- for (int i = 0 ; i < 256 ; i++) {
153+ size_t bytes_to_record = RECORD_SIZE ; // sample_rate * bytes_per_sample * channels
154+
155+ size_t bytes_read = 0 ;
156+
157+ #if USE_NEW_I2S_API
158+ Serial.println (" Start recording" );
159+ bytes_read = I2S .readBytes ((char *)buffer, bytes_to_record);
160+ if (bytes_read < 0 ) {
161+ ESP_LOGI (TAG , " Recording failed during I2S read" );
162+ }
163+
164+ #else
165+ esp_err_t err = i2s_read (I2S_NUM_0 , &rxbuf[0 ], 1024 * 96 , &readsize, 1000 );
166+
167+ if (err != ESP_OK ) {
168+ ESP_LOGI (TAG , " Recording failed during I2S read" );
169+ }else ESP_LOGI (TAG , " Recording" );
170+ #endif
171+ delay (100 );
172+ size_t samples = bytes_read / sizeof (int16_t );
173+ int16_t * pcm = (int16_t *)buffer;
174+
175+ int16_t peak = 0 ;
176+
177+ for (size_t i = 0 ; i < samples; i++) {
178+ int16_t v = pcm[i];
179+ if (v < 0 ) v = -v;
180+ if (v > peak) peak = v;
181+ }
182+
183+ float level = (float )peak / 32767 .0f ;
184+
185+ Serial.printf (" Level: %.3f\n " , level);
186+ for (int i = 0 ; i < 1024 ; i++) {
110187 // direct transfer too txbuff
111188 txbuf[i] = rxbuf[i];
112189 // txbuf[i] = 0; //mute
113190 }
114191 // play received buffer
115- i2s_write (I2S_NUM_0 , &txbuf[0 ], 256 * 2 , &readsize, 1000 );
116- }
192+ size_t bytes_written = 0 ;
193+ #if USE_NEW_I2S_API
194+ Serial.println (" Start playing" );
195+ bytes_written = I2S .write (buffer, RECORD_SIZE );
196+ if (bytes_written < 0 ) {
197+ ESP_LOGI (TAG , " Playback failed" );
198+ }
199+
200+ #else
201+ err = i2s_write (I2S_NUM_0 , &txbuf[0 ], 1024 * 96 , &readsize, 1000 );
202+ if (err != ESP_OK ) {
203+ ESP_LOGI (TAG , " Playback failed during I2S write" );
204+ }
205+ #endif
206+ delay (100 );
207+ }
0 commit comments