From 183f2459e61d2cb2b57d9d34506fb9808420eb95 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Tue, 8 Dec 2020 22:28:55 +0100 Subject: [PATCH 1/3] i2s:adds i2s_rxtxdac_begin(rx,tx,dac): A dac parameter is added to allow using only i2s-out-data pin. This is necessary in no-DAC mode when i2so-bck and i2so-ws are repurposed especially because they overlap with SPI GPIO. --- cores/esp8266/core_esp8266_i2s.cpp | 17 +++++++++++++---- cores/esp8266/i2s.h | 3 +++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/core_esp8266_i2s.cpp b/cores/esp8266/core_esp8266_i2s.cpp index d783d1afc9..da6f6c4197 100644 --- a/cores/esp8266/core_esp8266_i2s.cpp +++ b/cores/esp8266/core_esp8266_i2s.cpp @@ -64,6 +64,7 @@ typedef struct i2s_state { // Callback function should be defined as 'void ICACHE_RAM_ATTR function_name()', // and be placed in IRAM for faster execution. Avoid long computational tasks in this // function, use it to set flags and process later. + bool txDAC; } i2s_state_t; // RX = I2S receive (i.e. microphone), TX = I2S transmit (i.e. DAC) @@ -493,6 +494,10 @@ float i2s_get_real_rate(){ } bool i2s_rxtx_begin(bool enableRx, bool enableTx) { + return i2s_rxtxdac_begin(enableRx, enableTx, true); +} + +bool i2s_rxtxdac_begin(bool enableRx, bool enableTx, bool txDAC) { if (tx || rx) { i2s_end(); // Stop and free any ongoing stuff } @@ -503,9 +508,11 @@ bool i2s_rxtx_begin(bool enableRx, bool enableTx) { // Nothing to clean up yet return false; // OOM Error! } - pinMode(I2SO_WS, FUNCTION_1); pinMode(I2SO_DATA, FUNCTION_1); - pinMode(I2SO_BCK, FUNCTION_1); + if ((tx->txDAC = txDAC)) { + pinMode(I2SO_WS, FUNCTION_1); + pinMode(I2SO_BCK, FUNCTION_1); + } } if (enableRx) { rx = (i2s_state_t*)calloc(1, sizeof(*rx)); @@ -579,8 +586,10 @@ void i2s_end() { if (tx) { pinMode(I2SO_DATA, INPUT); - pinMode(I2SO_BCK, INPUT); - pinMode(I2SO_WS, INPUT); + if (tx->txDAC) { + pinMode(I2SO_BCK, INPUT); + pinMode(I2SO_WS, INPUT); + } free(tx); tx = NULL; } diff --git a/cores/esp8266/i2s.h b/cores/esp8266/i2s.h index 70587fd2fb..3b31e500fe 100644 --- a/cores/esp8266/i2s.h +++ b/cores/esp8266/i2s.h @@ -21,6 +21,8 @@ #ifndef I2S_h #define I2S_h +#define I2S_HAS_BEGIN_RXTXDAC 1 + /* How does this work? Basically, to get sound, you need to: - Connect an I2S codec to the I2S pins on the ESP. @@ -41,6 +43,7 @@ extern "C" { #endif void i2s_begin(); // Enable TX only, for compatibility +bool i2s_rxtxdac_begin(bool enableRx, bool enableTx, bool txDAC); // Allow TX and/or RX, returns false on OOM error bool i2s_rxtx_begin(bool enableRx, bool enableTx); // Allow TX and/or RX, returns false on OOM error void i2s_end(); void i2s_set_rate(uint32_t rate);//Sample Rate in Hz (ex 44100, 48000) From 66bea1e9f70231b1569087fd6a54492a11505de2 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Fri, 11 Dec 2020 18:26:27 +0100 Subject: [PATCH 2/3] extends I2S begin api to rx, better naming, per review --- cores/esp8266/core_esp8266_i2s.cpp | 28 +++++++++++++++++----------- cores/esp8266/i2s.h | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/cores/esp8266/core_esp8266_i2s.cpp b/cores/esp8266/core_esp8266_i2s.cpp index da6f6c4197..dccd7c1df8 100644 --- a/cores/esp8266/core_esp8266_i2s.cpp +++ b/cores/esp8266/core_esp8266_i2s.cpp @@ -64,7 +64,7 @@ typedef struct i2s_state { // Callback function should be defined as 'void ICACHE_RAM_ATTR function_name()', // and be placed in IRAM for faster execution. Avoid long computational tasks in this // function, use it to set flags and process later. - bool txDAC; + bool driveClocks; } i2s_state_t; // RX = I2S receive (i.e. microphone), TX = I2S transmit (i.e. DAC) @@ -494,10 +494,10 @@ float i2s_get_real_rate(){ } bool i2s_rxtx_begin(bool enableRx, bool enableTx) { - return i2s_rxtxdac_begin(enableRx, enableTx, true); + return i2s_rxtxdrive_begin(enableRx, enableTx, true, true); } -bool i2s_rxtxdac_begin(bool enableRx, bool enableTx, bool txDAC) { +bool i2s_rxtxdrive_begin(bool enableRx, bool enableTx, bool driveRxClocks, bool driveTxClocks) { if (tx || rx) { i2s_end(); // Stop and free any ongoing stuff } @@ -508,8 +508,9 @@ bool i2s_rxtxdac_begin(bool enableRx, bool enableTx, bool txDAC) { // Nothing to clean up yet return false; // OOM Error! } + tx->driveClocks = driveTxClocks; pinMode(I2SO_DATA, FUNCTION_1); - if ((tx->txDAC = txDAC)) { + if (driveTxClocks) { pinMode(I2SO_WS, FUNCTION_1); pinMode(I2SO_BCK, FUNCTION_1); } @@ -520,12 +521,15 @@ bool i2s_rxtxdac_begin(bool enableRx, bool enableTx, bool txDAC) { i2s_end(); // Clean up any TX or pin changes return false; // OOM error! } - pinMode(I2SI_WS, OUTPUT); - pinMode(I2SI_BCK, OUTPUT); + rx->driveClocks = driveRxClocks; pinMode(I2SI_DATA, INPUT); + if (driveRxClocks) { + pinMode(I2SI_WS, OUTPUT); + pinMode(I2SI_BCK, OUTPUT); + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_I2SI_BCK); + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_I2SI_WS); + } PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_I2SI_DATA); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_I2SI_BCK); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_I2SI_WS); } if (!i2s_slc_begin()) { @@ -586,7 +590,7 @@ void i2s_end() { if (tx) { pinMode(I2SO_DATA, INPUT); - if (tx->txDAC) { + if (tx->driveClocks) { pinMode(I2SO_BCK, INPUT); pinMode(I2SO_WS, INPUT); } @@ -595,8 +599,10 @@ void i2s_end() { } if (rx) { pinMode(I2SI_DATA, INPUT); - pinMode(I2SI_BCK, INPUT); - pinMode(I2SI_WS, INPUT); + if (rx->driveClocks) { + pinMode(I2SI_BCK, INPUT); + pinMode(I2SI_WS, INPUT); + } free(rx); rx = NULL; } diff --git a/cores/esp8266/i2s.h b/cores/esp8266/i2s.h index 3b31e500fe..d93d0f7ba5 100644 --- a/cores/esp8266/i2s.h +++ b/cores/esp8266/i2s.h @@ -43,8 +43,8 @@ extern "C" { #endif void i2s_begin(); // Enable TX only, for compatibility -bool i2s_rxtxdac_begin(bool enableRx, bool enableTx, bool txDAC); // Allow TX and/or RX, returns false on OOM error bool i2s_rxtx_begin(bool enableRx, bool enableTx); // Allow TX and/or RX, returns false on OOM error +bool i2s_rxtxdrive_begin(bool enableRx, bool enableTx, bool driveRxClocks, bool driveTxClocks); void i2s_end(); void i2s_set_rate(uint32_t rate);//Sample Rate in Hz (ex 44100, 48000) void i2s_set_dividers(uint8_t div1, uint8_t div2);//Direct control over output rate From 05b5360deca5438445581e7b2a96327b77d23a9f Mon Sep 17 00:00:00 2001 From: david gauchard Date: Fri, 11 Dec 2020 18:38:08 +0100 Subject: [PATCH 3/3] fix macro name indicating new API availability --- cores/esp8266/i2s.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/i2s.h b/cores/esp8266/i2s.h index d93d0f7ba5..f2978286af 100644 --- a/cores/esp8266/i2s.h +++ b/cores/esp8266/i2s.h @@ -21,7 +21,7 @@ #ifndef I2S_h #define I2S_h -#define I2S_HAS_BEGIN_RXTXDAC 1 +#define I2S_HAS_BEGIN_RXTX_DRIVE_CLOCKS 1 /* How does this work? Basically, to get sound, you need to: