From 0ccf2b9c2e8ddbbedda5f649bf39694dc958f07e Mon Sep 17 00:00:00 2001 From: netmindz Date: Fri, 11 Aug 2023 20:59:34 +0100 Subject: [PATCH] =?UTF-8?q?Revert=20"Remove=20I2C=20related=20code=20from?= =?UTF-8?q?=20ES8388=20and=20ES7243=20as=20blazoncek=20proposes=20?= =?UTF-8?q?=E2=80=A6"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usermods/audioreactive/audio_reactive.h | 4 +- usermods/audioreactive/audio_source.h | 70 +++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/usermods/audioreactive/audio_reactive.h b/usermods/audioreactive/audio_reactive.h index 7fa9a013..297f85c7 100644 --- a/usermods/audioreactive/audio_reactive.h +++ b/usermods/audioreactive/audio_reactive.h @@ -1623,7 +1623,7 @@ class AudioReactive : public Usermod { //useInputFilter = 0; // in case you need to disable low-cut software filtering audioSource = new ES7243(SAMPLE_RATE, BLOCK_SIZE); delay(100); - if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin); + if (audioSource) audioSource->initialize(sdaPin, sclPin, i2swsPin, i2ssdPin, i2sckPin, mclkPin); break; case 3: DEBUGSR_PRINT(F("AR: SPH0645 Microphone - ")); DEBUGSR_PRINTLN(F(I2S_MIC_CHANNEL_TEXT)); @@ -1659,7 +1659,7 @@ class AudioReactive : public Usermod { audioSource = new ES8388Source(SAMPLE_RATE, BLOCK_SIZE, 1.0f); //useInputFilter = 0; // to disable low-cut software filtering and restore previous behaviour delay(100); - if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin); + if (audioSource) audioSource->initialize(sdaPin, sclPin, i2swsPin, i2ssdPin, i2sckPin, mclkPin); break; #if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index c46e6f17..e02c3107 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -422,6 +422,13 @@ class I2SSource : public AudioSource { */ class ES7243 : public I2SSource { private: + // I2C initialization functions for ES7243 + void _es7243I2cBegin() { + bool i2c_initialized = Wire.begin(pin_ES7243_SDA, pin_ES7243_SCL, 100000U); + if (i2c_initialized == false) { + ERRORSR_PRINTLN(F("AR: ES7243 failed to initialize I2C bus driver.")); + } + } void _es7243I2cWrite(uint8_t reg, uint8_t val) { #ifndef ES7243_ADDR @@ -439,6 +446,7 @@ class ES7243 : public I2SSource { } void _es7243InitAdc() { + _es7243I2cBegin(); _es7243I2cWrite(0x00, 0x01); _es7243I2cWrite(0x06, 0x00); _es7243I2cWrite(0x05, 0x1B); @@ -453,8 +461,12 @@ public: _config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT; }; - void initialize(int8_t i2swsPin, int8_t i2ssdPin, int8_t i2sckPin, int8_t mclkPin) { + void initialize(int8_t sdaPin, int8_t sclPin, int8_t i2swsPin, int8_t i2ssdPin, int8_t i2sckPin, int8_t mclkPin) { // check that pins are valid + if ((sdaPin < 0) || (sclPin < 0)) { + ERRORSR_PRINTF("\nAR: invalid ES7243 I2C pins: SDA=%d, SCL=%d\n", sdaPin, sclPin); + return; + } if ((i2c_sda < 0) || (i2c_scl < 0)) { // check that global I2C pins are not "undefined" ERRORSR_PRINTF("\nAR: invalid ES7243 global I2C pins: SDA=%d, SCL=%d\n", i2c_sda, i2c_scl); @@ -470,16 +482,32 @@ public: return; } + // Reserve SDA and SCL pins of the I2C interface + PinManagerPinType es7243Pins[2] = { { sdaPin, true }, { sclPin, true } }; + if (!pinManager.allocateMultiplePins(es7243Pins, 2, PinOwner::HW_I2C)) { + pinManager.deallocateMultiplePins(es7243Pins, 2, PinOwner::HW_I2C); + ERRORSR_PRINTF("\nAR: Failed to allocate ES7243 I2C pins: SDA=%d, SCL=%d\n", sdaPin, sclPin); + return; + } + + pin_ES7243_SDA = sdaPin; + pin_ES7243_SCL = sclPin; + // First route mclk, then configure ADC over I2C, then configure I2S _es7243InitAdc(); I2SSource::initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin); } void deinitialize() { + // Release SDA and SCL pins of the I2C interface + PinManagerPinType es7243Pins[2] = { { pin_ES7243_SDA, true }, { pin_ES7243_SCL, true } }; + pinManager.deallocateMultiplePins(es7243Pins, 2, PinOwner::HW_I2C); I2SSource::deinitialize(); } - + private: + int8_t pin_ES7243_SDA; + int8_t pin_ES7243_SCL; }; /* ES8388 Sound Modude @@ -488,6 +516,13 @@ public: */ class ES8388Source : public I2SSource { private: + // I2C initialization functions for ES8388 + void _es8388I2cBegin() { + bool i2c_initialized = Wire.begin(pin_ES8388_SDA, pin_ES8388_SCL, 100000U); + if (i2c_initialized == false) { + ERRORSR_PRINTLN(F("AR: ES8388 failed to initialize I2C bus driver.")); + } + } void _es8388I2cWrite(uint8_t reg, uint8_t val) { #ifndef ES8388_ADDR @@ -512,6 +547,7 @@ class ES8388Source : public I2SSource { // Registries are decimal, settings are binary as that's how everything is listed in the docs // ...which makes it easier to reference the docs. // + _es8388I2cBegin(); _es8388I2cWrite( 8,0b00000000); // I2S to slave _es8388I2cWrite( 2,0b11110011); // Power down DEM and STM _es8388I2cWrite(43,0b10000000); // Set same LRCK @@ -581,7 +617,17 @@ class ES8388Source : public I2SSource { _config.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT; }; - void initialize(int8_t i2swsPin, int8_t i2ssdPin, int8_t i2sckPin, int8_t mclkPin) { + void initialize(int8_t sdaPin, int8_t sclPin, int8_t i2swsPin, int8_t i2ssdPin, int8_t i2sckPin, int8_t mclkPin) { + + // BUG: "use global I2C pins" are valid as -1, and -1 is seen as invalid here. + // Workaround: Set I2C pins here, which will also set them globally. + // Bug also exists in ES7243. + + // check that pins are valid + if ((sdaPin < 0) || (sclPin < 0)) { + ERRORSR_PRINTF("\nAR: invalid ES8388 I2C pins: SDA=%d, SCL=%d\n", sdaPin, sclPin); + return; + } if ((i2c_sda < 0) || (i2c_scl < 0)) { // check that global I2C pins are not "undefined" ERRORSR_PRINTF("\nAR: invalid ES8388 global I2C pins: SDA=%d, SCL=%d\n", i2c_sda, i2c_scl); @@ -597,16 +643,32 @@ class ES8388Source : public I2SSource { return; } + // Reserve SDA and SCL pins of the I2C interface + PinManagerPinType es8388Pins[2] = { { sdaPin, true }, { sclPin, true } }; + if (!pinManager.allocateMultiplePins(es8388Pins, 2, PinOwner::HW_I2C)) { + pinManager.deallocateMultiplePins(es8388Pins, 2, PinOwner::HW_I2C); + ERRORSR_PRINTF("\nAR: Failed to allocate ES8388 I2C pins: SDA=%d, SCL=%d\n", sdaPin, sclPin); + return; + } + + pin_ES8388_SDA = sdaPin; + pin_ES8388_SCL = sclPin; + // First route mclk, then configure ADC over I2C, then configure I2S _es8388InitAdc(); I2SSource::initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin); } void deinitialize() { + // Release SDA and SCL pins of the I2C interface + PinManagerPinType es8388Pins[2] = { { pin_ES8388_SDA, true }, { pin_ES8388_SCL, true } }; + pinManager.deallocateMultiplePins(es8388Pins, 2, PinOwner::HW_I2C); I2SSource::deinitialize(); } - + private: + int8_t pin_ES8388_SDA; + int8_t pin_ES8388_SCL; }; #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0)