From 693f52d984d5247d5631007a37a507879fd39e04 Mon Sep 17 00:00:00 2001 From: Ewoud Date: Wed, 5 Apr 2023 11:02:21 +0200 Subject: [PATCH 01/11] Merge pull request #5 from netmindz/ES8388 WiP - ES8388 --- usermods/audioreactive/audio_reactive.h | 9 ++ usermods/audioreactive/audio_source.h | 114 ++++++++++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/usermods/audioreactive/audio_reactive.h b/usermods/audioreactive/audio_reactive.h index 09cc931c9..ff5483fdc 100644 --- a/usermods/audioreactive/audio_reactive.h +++ b/usermods/audioreactive/audio_reactive.h @@ -1159,6 +1159,13 @@ class AudioReactive : public Usermod { if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin); break; #endif + case 6: + DEBUGSR_PRINTLN(F("AR: ES8388 Source")); + audioSource = new ES8388Source(SAMPLE_RATE, BLOCK_SIZE); + delay(100); + 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) // ADC over I2S is only possible on "classic" ESP32 case 0: @@ -1752,6 +1759,8 @@ class AudioReactive : public Usermod { #if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) oappend(SET_F("addOption(dd,'Generic I2S PDM',5);")); #endif + oappend(SET_F("addOption(dd,'ES8388',6);")); + oappend(SET_F("dd=addDropdown('AudioReactive','config:AGC');")); oappend(SET_F("addOption(dd,'Off',0);")); oappend(SET_F("addOption(dd,'Normal',1);")); diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 16bbbb658..bd97f6bc6 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -462,6 +462,120 @@ public: int8_t pin_ES7243_SCL; }; +/* ES8388 Sound Modude + This is an I2S sound processing unit that requires ininitialization over + I2C before I2S data can be received. +*/ +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 + Wire.beginTransmission(0x10); + #define ES8388_ADDR 0x10 // default address +#else + Wire.beginTransmission(ES8388_ADDR); +#endif + Wire.write((uint8_t)reg); + Wire.write((uint8_t)val); + uint8_t i2cErr = Wire.endTransmission(); // i2cErr == 0 means OK + if (i2cErr != 0) { + DEBUGSR_PRINTF("AR: ES8388 I2C write failed with error=%d (addr=0x%X, reg 0x%X, val 0x%X).\n", i2cErr, ES8388_ADDR, reg, val); + } + } + + void _es8388InitAdc() { + // This is by no means 100% figured but it's working for line-in + // with a little too much noise for my liking... + // https://dl.radxa.com/rock2/docs/hw/ds/ES8388%20user%20Guide.pdf Section 10.1 + // https://docs.google.com/spreadsheets/d/1CN3MvhkcPVESuxKyx1xRYqfUit5hOdsG45St9BCUm-g/edit#gid=0 generally + _es8388I2cBegin(); + _es8388I2cWrite(0x08,0x00); // I2S to slave + _es8388I2cWrite(0x02,0xf3); // Power down DEM and STM + _es8388I2cWrite(0x2b,0x80); // Set same LRCK + _es8388I2cWrite(0x00,0x05); // Set chip to Play & Record Mode + _es8388I2cWrite(0x01,0x40); // Power up Analog and lbias ... These 5 (to here) need to be done in order + _es8388I2cWrite(0x03,0x00); // Power up ADC, Analog Input, and Mic Bias + _es8388I2cWrite(0x04,0x3C); // ** In guide, not in working example tho. ** + _es8388I2cWrite(0x0a,0x50); // Use Lin2/Rin2 for ADC input + _es8388I2cWrite(0x09,0x00); // Select Analog Input PGA Gain for ADC to 0dB ** + _es8388I2cWrite(0x0c,0x0c); // I2S format, 24-bit + _es8388I2cWrite(0x0d,0x02); // Set MCLK/LRCK ratio to 256 + _es8388I2cWrite(0x10,0x00); // Set ADC digital volume attenuation to 0dB (left) + _es8388I2cWrite(0x11,0x00); // Set ADC digital volume attenuation to 0dB (right) + _es8388I2cWrite(0x17,0x18); // Set format for DAC (I2S, 24bit) + _es8388I2cWrite(0x18,0x02); // Set DAC MCLK/LRCK ratio to 256 + _es8388I2cWrite(0x1a,0x00); // DAC Volume attenuation 0dB (left) + _es8388I2cWrite(0x1b,0x00); // DAC Volume attenuation 0dB (right) + _es8388I2cWrite(0x2e,0x00); // LOUT1 volume - 00 = -45dB + _es8388I2cWrite(0x2f,0x00); // ROUT1 volume - 00 = -45dB + _es8388I2cWrite(0x0C,0b00000001); // ADC digital format - I2S + Left Justified + _es8388I2cWrite(0x17,0b00000010); // DAC digital format - I2S + Left Justified + _es8388I2cWrite(0x02,0x00); // Power up DEM and STM + // end of guide init ^^^ + _es8388I2cWrite(0x02,0b01000000); // Power. Guide says it's only 6 bits but that 1 means "turn on sometthing for line-out voltage" + _es8388I2cWrite(0x04,0x0c); // LOUT2 an ROUT2 powered + _es8388I2cWrite(0x30,0b00011110); // LOUT2VOL - 0 = -45dB, 0b00011110 = +0dB + _es8388I2cWrite(0x31,0b00011110); // ROUT2VOL - 0 = -45dB, 0b00011110 = +0dB + _es8388I2cWrite(0x26,0x09); // Mixer + _es8388I2cWrite(0x27,0x50); // Mixer + _es8388I2cWrite(0x2a,0x50); // Mixer + _es8388I2cWrite(0x03,0x00); // Power + } + + public: + ES8388Source(SRate_t sampleRate, int blockSize, float sampleScale = 1.0f, bool i2sMaster=true) : + I2SSource(sampleRate, blockSize, sampleScale, i2sMaster) { + _config.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT; + }; + + 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 ES8388 I2C pins: SDA=%d, SCL=%d\n", sdaPin, sclPin); + return; + } + + if ((i2sckPin < 0) || (mclkPin < 0)) { + ERRORSR_PRINTF("\nAR: invalid I2S pin: SCK=%d, MCLK=%d\n", i2sckPin, mclkPin); + 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) #if !defined(SOC_I2S_SUPPORTS_ADC) && !defined(SOC_I2S_SUPPORTS_ADC_DAC) From b88d8c85cb96049f3aba841454d7d844bd2f9860 Mon Sep 17 00:00:00 2001 From: TroyHacks <5659019+troyhacks@users.noreply.github.com> Date: Wed, 5 Apr 2023 11:00:14 -0400 Subject: [PATCH 02/11] ES8388 init optimizations and fixes --- usermods/audioreactive/audio_source.h | 50 ++++++++++----------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index bd97f6bc6..40cb35d53 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -492,42 +492,28 @@ class ES8388Source : public I2SSource { } void _es8388InitAdc() { - // This is by no means 100% figured but it's working for line-in - // with a little too much noise for my liking... // https://dl.radxa.com/rock2/docs/hw/ds/ES8388%20user%20Guide.pdf Section 10.1 // https://docs.google.com/spreadsheets/d/1CN3MvhkcPVESuxKyx1xRYqfUit5hOdsG45St9BCUm-g/edit#gid=0 generally - _es8388I2cBegin(); - _es8388I2cWrite(0x08,0x00); // I2S to slave - _es8388I2cWrite(0x02,0xf3); // Power down DEM and STM - _es8388I2cWrite(0x2b,0x80); // Set same LRCK - _es8388I2cWrite(0x00,0x05); // Set chip to Play & Record Mode - _es8388I2cWrite(0x01,0x40); // Power up Analog and lbias ... These 5 (to here) need to be done in order - _es8388I2cWrite(0x03,0x00); // Power up ADC, Analog Input, and Mic Bias - _es8388I2cWrite(0x04,0x3C); // ** In guide, not in working example tho. ** - _es8388I2cWrite(0x0a,0x50); // Use Lin2/Rin2 for ADC input - _es8388I2cWrite(0x09,0x00); // Select Analog Input PGA Gain for ADC to 0dB ** - _es8388I2cWrite(0x0c,0x0c); // I2S format, 24-bit - _es8388I2cWrite(0x0d,0x02); // Set MCLK/LRCK ratio to 256 - _es8388I2cWrite(0x10,0x00); // Set ADC digital volume attenuation to 0dB (left) - _es8388I2cWrite(0x11,0x00); // Set ADC digital volume attenuation to 0dB (right) - _es8388I2cWrite(0x17,0x18); // Set format for DAC (I2S, 24bit) - _es8388I2cWrite(0x18,0x02); // Set DAC MCLK/LRCK ratio to 256 - _es8388I2cWrite(0x1a,0x00); // DAC Volume attenuation 0dB (left) - _es8388I2cWrite(0x1b,0x00); // DAC Volume attenuation 0dB (right) - _es8388I2cWrite(0x2e,0x00); // LOUT1 volume - 00 = -45dB - _es8388I2cWrite(0x2f,0x00); // ROUT1 volume - 00 = -45dB - _es8388I2cWrite(0x0C,0b00000001); // ADC digital format - I2S + Left Justified - _es8388I2cWrite(0x17,0b00000010); // DAC digital format - I2S + Left Justified - _es8388I2cWrite(0x02,0x00); // Power up DEM and STM - // end of guide init ^^^ - _es8388I2cWrite(0x02,0b01000000); // Power. Guide says it's only 6 bits but that 1 means "turn on sometthing for line-out voltage" - _es8388I2cWrite(0x04,0x0c); // LOUT2 an ROUT2 powered + // Sets ADC to around what AudioReactive expects, and loops line-in to line-out/headphone for monitoring. + _es8388I2cBegin(); + _es8388I2cWrite(0x08,0x00); // I2S to slave + _es8388I2cWrite(0x02,0xf3); // Power down DEM and STM + _es8388I2cWrite(0x2b,0x80); // Set same LRCK + _es8388I2cWrite(0x00,0x05); // Set chip to Play & Record Mode + _es8388I2cWrite(0x0d,0x02); // Set MCLK/LRCK ratio to 256 + _es8388I2cWrite(0x01,0x40); // Power up analog and lbias + _es8388I2cWrite(0x03,0x00); // Power up ADC, Analog Input, and Mic Bias + _es8388I2cWrite(0x0a,0x50); // Use Lin2/Rin2 for ADC input ("line-in") + _es8388I2cWrite(0x09,0x00); // Select Analog Input PGA Gain for ADC to 0dB (L+R) + _es8388I2cWrite(0x10,0b01000000); // Set ADC digital volume attenuation to -32dB (left) + _es8388I2cWrite(0x11,0b01000000); // Set ADC digital volume attenuation to -32dB (right) + _es8388I2cWrite(0x04,0x0c); // Turn on LOUT2 and ROUT2 power + _es8388I2cWrite(0x02,0b01000000); // Power up DEM and STM and undocumented bit for "turn on line-out amp" + _es8388I2cWrite(0x26,0x09); // Mixer - route LIN2/RIN2 to output + _es8388I2cWrite(0x27,0b01010000); // Mixer - route LIN to left mixer, 0dB gain + _es8388I2cWrite(0x2a,0b01010000); // Mixer - route RIN to right mixer, 0dB gain _es8388I2cWrite(0x30,0b00011110); // LOUT2VOL - 0 = -45dB, 0b00011110 = +0dB _es8388I2cWrite(0x31,0b00011110); // ROUT2VOL - 0 = -45dB, 0b00011110 = +0dB - _es8388I2cWrite(0x26,0x09); // Mixer - _es8388I2cWrite(0x27,0x50); // Mixer - _es8388I2cWrite(0x2a,0x50); // Mixer - _es8388I2cWrite(0x03,0x00); // Power } public: From 2bc8ffefcef48481679b62ec663aea9d5f89d1a2 Mon Sep 17 00:00:00 2001 From: TroyHacks <5659019+troyhacks@users.noreply.github.com> Date: Fri, 21 Apr 2023 13:58:41 -0400 Subject: [PATCH 03/11] Some fixes for LyraT and also better inits/docs --- usermods/audioreactive/audio_source.h | 39 ++++++++++++++------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 40cb35d53..5bf334649 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -493,27 +493,30 @@ class ES8388Source : public I2SSource { void _es8388InitAdc() { // https://dl.radxa.com/rock2/docs/hw/ds/ES8388%20user%20Guide.pdf Section 10.1 + // http://www.everest-semi.com/pdf/ES8388%20DS.pdf Better spec sheet, more clear. Registries are decimal, settings are binary. // https://docs.google.com/spreadsheets/d/1CN3MvhkcPVESuxKyx1xRYqfUit5hOdsG45St9BCUm-g/edit#gid=0 generally // Sets ADC to around what AudioReactive expects, and loops line-in to line-out/headphone for monitoring. _es8388I2cBegin(); - _es8388I2cWrite(0x08,0x00); // I2S to slave - _es8388I2cWrite(0x02,0xf3); // Power down DEM and STM - _es8388I2cWrite(0x2b,0x80); // Set same LRCK - _es8388I2cWrite(0x00,0x05); // Set chip to Play & Record Mode - _es8388I2cWrite(0x0d,0x02); // Set MCLK/LRCK ratio to 256 - _es8388I2cWrite(0x01,0x40); // Power up analog and lbias - _es8388I2cWrite(0x03,0x00); // Power up ADC, Analog Input, and Mic Bias - _es8388I2cWrite(0x0a,0x50); // Use Lin2/Rin2 for ADC input ("line-in") - _es8388I2cWrite(0x09,0x00); // Select Analog Input PGA Gain for ADC to 0dB (L+R) - _es8388I2cWrite(0x10,0b01000000); // Set ADC digital volume attenuation to -32dB (left) - _es8388I2cWrite(0x11,0b01000000); // Set ADC digital volume attenuation to -32dB (right) - _es8388I2cWrite(0x04,0x0c); // Turn on LOUT2 and ROUT2 power - _es8388I2cWrite(0x02,0b01000000); // Power up DEM and STM and undocumented bit for "turn on line-out amp" - _es8388I2cWrite(0x26,0x09); // Mixer - route LIN2/RIN2 to output - _es8388I2cWrite(0x27,0b01010000); // Mixer - route LIN to left mixer, 0dB gain - _es8388I2cWrite(0x2a,0b01010000); // Mixer - route RIN to right mixer, 0dB gain - _es8388I2cWrite(0x30,0b00011110); // LOUT2VOL - 0 = -45dB, 0b00011110 = +0dB - _es8388I2cWrite(0x31,0b00011110); // ROUT2VOL - 0 = -45dB, 0b00011110 = +0dB + _es8388I2cWrite( 8,0b00000000); // I2S to slave + _es8388I2cWrite( 2,0b11110011); // Power down DEM and STM + _es8388I2cWrite(43,0b10000000); // Set same LRCK + _es8388I2cWrite( 0,0b00000101); // Set chip to Play & Record Mode + _es8388I2cWrite(13,0b00000010); // Set MCLK/LRCK ratio to 256 + _es8388I2cWrite( 1,0b01000000); // Power up analog and lbias + _es8388I2cWrite( 3,0b00000000); // Power up ADC, Analog Input, and Mic Bias + _es8388I2cWrite(10,0b01010000); // Use Lin2/Rin2 for ADC input ("line-in") + _es8388I2cWrite( 9,0b00000000); // Select Analog Input PGA Gain for ADC to 0dB (L+R) + _es8388I2cWrite(16,0b01000000); // Set ADC digital volume attenuation to -32dB (left) + _es8388I2cWrite(17,0b01000000); // Set ADC digital volume attenuation to -32dB (right) + _es8388I2cWrite( 4,0b00111100); // Turn on LOUT1 and ROUT1 and LOUT2 and ROUT2 power + _es8388I2cWrite( 2,0b01000000); // Power up DEM and STM and undocumented bit for "turn on line-out amp" + _es8388I2cWrite(38,0b00001001); // Mixer - route LIN2/RIN2 to output + _es8388I2cWrite(39,0b10010000); // Mixer - route LIN to mixL, 0dB gain was 01.. + _es8388I2cWrite(42,0b10010000); // Mixer - route RIN to mixR, 0dB gain was 01.. + _es8388I2cWrite(46,0b00011110); // LOUT1VOL - 0 = -45dB, 0b00011110 = +0dB + _es8388I2cWrite(47,0b00011110); // ROUT1VOL - 0 = -45dB, 0b00011110 = +0dB + _es8388I2cWrite(48,0b00011110); // LOUT2VOL - 0 = -45dB, 0b00011110 = +0dB + _es8388I2cWrite(49,0b00011110); // ROUT2VOL - 0 = -45dB, 0b00011110 = +0dB } public: From c14a921e5a4b9d39e494f67e7564721539cfb29b Mon Sep 17 00:00:00 2001 From: TroyHacks <5659019+troyhacks@users.noreply.github.com> Date: Tue, 25 Apr 2023 23:45:09 -0300 Subject: [PATCH 04/11] Mic settings for ES8388. Also ALC, pass-thru, etc. --- usermods/audioreactive/audio_source.h | 60 +++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 5bf334649..8986180f1 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -504,19 +504,59 @@ class ES8388Source : public I2SSource { _es8388I2cWrite(13,0b00000010); // Set MCLK/LRCK ratio to 256 _es8388I2cWrite( 1,0b01000000); // Power up analog and lbias _es8388I2cWrite( 3,0b00000000); // Power up ADC, Analog Input, and Mic Bias + _es8388I2cWrite( 4,0b11111100); // Power down DAC, Turn on LOUT1 and ROUT1 and LOUT2 and ROUT2 power + _es8388I2cWrite( 2,0b01000000); // Power up DEM and STM and undocumented bit for "turn on line-out amp" + + // #define use_es8388_mic + + #ifdef use_es8388_mic + // Pick one. If you have to use the mics, use a Lyra-T over an AudioKit. + // + // The mics *and* line-in are BOTH connected to LIN2/RIN2 on the AudioKit + // so there's no way to completely eliminate the mics. It's also hella noisy. + // Line-in works OK on the AudioKit, generally speaking, as the mics really need + // amplification to be noticable. + // + // The Lyra-T does a reasonable job with mic input as configured below. + // + _es8388I2cWrite(10,0b00000000); // Use Lin1/Rin1 for ADC input (mic on Lyra-T) + //_es8388I2cWrite(10,0b01010000); // Use Lin2/Rin2 for ADC input (mic *and* line-in on AudioKit) + + _es8388I2cWrite( 9,0b10001000); // Select Analog Input PGA Gain for ADC to +24dB (L+R) + _es8388I2cWrite(16,0b00000000); // Set ADC digital volume attenuation to 0dB (left) + _es8388I2cWrite(17,0b00000000); // Set ADC digital volume attenuation to 0dB (right) + _es8388I2cWrite(38,0b00011011); // Mixer - route LIN1/RIN1 to output after mic gain + + _es8388I2cWrite(39,0b01000000); // Mixer - route LIN to mixL, +6dB gain + _es8388I2cWrite(42,0b01000000); // Mixer - route RIN to mixR, +6dB gain + _es8388I2cWrite(46,0b00100001); // LOUT1VOL - 0b00100001 = +4.5dB + _es8388I2cWrite(47,0b00100001); // ROUT1VOL - 0b00100001 = +4.5dB + _es8388I2cWrite(48,0b00100001); // LOUT2VOL - 0b00100001 = +4.5dB + _es8388I2cWrite(49,0b00100001); // ROUT2VOL - 0b00100001 = +4.5dB + + // Music ALC - the mics like Auto Level Control + // You can also use this for line-in, but it's not really needed. + // + _es8388I2cWrite(18,0b11111000); // ALC: stereo, max gain +35.5dB, min gain -12dB + _es8388I2cWrite(19,0b00110000); // ALC: target -1.5dB, 0ms hold time + _es8388I2cWrite(20,0b10100110); // ALC: gain ramp up = 420ms/93ms, gain ramp down = check manual for calc + _es8388I2cWrite(21,0b00000110); // ALC: use "ALC" mode, no zero-cross, window 96 samples + _es8388I2cWrite(22,0b01011001); // ALC: noise gate threshold, PGA gain constant, noise gate enabled + #else _es8388I2cWrite(10,0b01010000); // Use Lin2/Rin2 for ADC input ("line-in") _es8388I2cWrite( 9,0b00000000); // Select Analog Input PGA Gain for ADC to 0dB (L+R) _es8388I2cWrite(16,0b01000000); // Set ADC digital volume attenuation to -32dB (left) _es8388I2cWrite(17,0b01000000); // Set ADC digital volume attenuation to -32dB (right) - _es8388I2cWrite( 4,0b00111100); // Turn on LOUT1 and ROUT1 and LOUT2 and ROUT2 power - _es8388I2cWrite( 2,0b01000000); // Power up DEM and STM and undocumented bit for "turn on line-out amp" _es8388I2cWrite(38,0b00001001); // Mixer - route LIN2/RIN2 to output - _es8388I2cWrite(39,0b10010000); // Mixer - route LIN to mixL, 0dB gain was 01.. - _es8388I2cWrite(42,0b10010000); // Mixer - route RIN to mixR, 0dB gain was 01.. - _es8388I2cWrite(46,0b00011110); // LOUT1VOL - 0 = -45dB, 0b00011110 = +0dB - _es8388I2cWrite(47,0b00011110); // ROUT1VOL - 0 = -45dB, 0b00011110 = +0dB - _es8388I2cWrite(48,0b00011110); // LOUT2VOL - 0 = -45dB, 0b00011110 = +0dB - _es8388I2cWrite(49,0b00011110); // ROUT2VOL - 0 = -45dB, 0b00011110 = +0dB + + _es8388I2cWrite(39,0b01010000); // Mixer - route LIN to mixL, 0dB gain + _es8388I2cWrite(42,0b01010000); // Mixer - route RIN to mixR, 0dB gain + _es8388I2cWrite(46,0b00011011); // LOUT1VOL - 0b00011110 = +0dB, 0b00011011 = Lyra-T balance fix + _es8388I2cWrite(47,0b00011110); // ROUT1VOL - 0b00011110 = +0dB + _es8388I2cWrite(48,0b00011110); // LOUT2VOL - 0b00011110 = +0dB + _es8388I2cWrite(49,0b00011110); // ROUT2VOL - 0b00011110 = +0dB + #endif + } public: @@ -527,6 +567,10 @@ class ES8388Source : public I2SSource { 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); From 65f2490e891992fcd247ab59937e799c1c21f764 Mon Sep 17 00:00:00 2001 From: TroyHacks <5659019+troyhacks@users.noreply.github.com> Date: Tue, 25 Apr 2023 23:47:28 -0300 Subject: [PATCH 05/11] It's offically the LyraT, not Lyra-T. --- usermods/audioreactive/audio_source.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 8986180f1..877086d9c 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -510,16 +510,16 @@ class ES8388Source : public I2SSource { // #define use_es8388_mic #ifdef use_es8388_mic - // Pick one. If you have to use the mics, use a Lyra-T over an AudioKit. + // Pick one. If you have to use the mics, use a LyraT over an AudioKit. // // The mics *and* line-in are BOTH connected to LIN2/RIN2 on the AudioKit // so there's no way to completely eliminate the mics. It's also hella noisy. // Line-in works OK on the AudioKit, generally speaking, as the mics really need // amplification to be noticable. // - // The Lyra-T does a reasonable job with mic input as configured below. + // The LyraT does a reasonable job with mic input as configured below. // - _es8388I2cWrite(10,0b00000000); // Use Lin1/Rin1 for ADC input (mic on Lyra-T) + _es8388I2cWrite(10,0b00000000); // Use Lin1/Rin1 for ADC input (mic on LyraT) //_es8388I2cWrite(10,0b01010000); // Use Lin2/Rin2 for ADC input (mic *and* line-in on AudioKit) _es8388I2cWrite( 9,0b10001000); // Select Analog Input PGA Gain for ADC to +24dB (L+R) @@ -551,7 +551,7 @@ class ES8388Source : public I2SSource { _es8388I2cWrite(39,0b01010000); // Mixer - route LIN to mixL, 0dB gain _es8388I2cWrite(42,0b01010000); // Mixer - route RIN to mixR, 0dB gain - _es8388I2cWrite(46,0b00011011); // LOUT1VOL - 0b00011110 = +0dB, 0b00011011 = Lyra-T balance fix + _es8388I2cWrite(46,0b00011011); // LOUT1VOL - 0b00011110 = +0dB, 0b00011011 = LyraT balance fix _es8388I2cWrite(47,0b00011110); // ROUT1VOL - 0b00011110 = +0dB _es8388I2cWrite(48,0b00011110); // LOUT2VOL - 0b00011110 = +0dB _es8388I2cWrite(49,0b00011110); // ROUT2VOL - 0b00011110 = +0dB From 90e37439e526e2388c62851bbe5517ecbc04be0c Mon Sep 17 00:00:00 2001 From: TroyHacks <5659019+troyhacks@users.noreply.github.com> Date: Mon, 1 May 2023 21:07:33 -0400 Subject: [PATCH 06/11] Better ES8388 init and mic support --- usermods/audioreactive/audio_source.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 877086d9c..1c2d78220 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -493,9 +493,12 @@ class ES8388Source : public I2SSource { void _es8388InitAdc() { // https://dl.radxa.com/rock2/docs/hw/ds/ES8388%20user%20Guide.pdf Section 10.1 - // http://www.everest-semi.com/pdf/ES8388%20DS.pdf Better spec sheet, more clear. Registries are decimal, settings are binary. + // http://www.everest-semi.com/pdf/ES8388%20DS.pdf Better spec sheet, more clear. // https://docs.google.com/spreadsheets/d/1CN3MvhkcPVESuxKyx1xRYqfUit5hOdsG45St9BCUm-g/edit#gid=0 generally // Sets ADC to around what AudioReactive expects, and loops line-in to line-out/headphone for monitoring. + // 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 From 9831e7bc3b85ba58287593a120102f4ecd67d2fd Mon Sep 17 00:00:00 2001 From: TroyHacks <5659019+troyhacks@users.noreply.github.com> Date: Thu, 4 May 2023 08:55:00 -0400 Subject: [PATCH 07/11] Warnings about AudioKit rubbish --- usermods/audioreactive/audio_source.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 1c2d78220..e7ccf44df 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -513,15 +513,16 @@ class ES8388Source : public I2SSource { // #define use_es8388_mic #ifdef use_es8388_mic - // Pick one. If you have to use the mics, use a LyraT over an AudioKit. - // // The mics *and* line-in are BOTH connected to LIN2/RIN2 on the AudioKit // so there's no way to completely eliminate the mics. It's also hella noisy. // Line-in works OK on the AudioKit, generally speaking, as the mics really need - // amplification to be noticable. + // amplification to be noticable in a quiet room. If you're in a very loud room, + // the mics on the AudioKit WILL pick up sound even in line-in mode. + // TL;DR: Don't use the AudioKit for anything, use the LyraT. // // The LyraT does a reasonable job with mic input as configured below. - // + + // Pick one of these. If you have to use the mics, use a LyraT over an AudioKit if you can: _es8388I2cWrite(10,0b00000000); // Use Lin1/Rin1 for ADC input (mic on LyraT) //_es8388I2cWrite(10,0b01010000); // Use Lin2/Rin2 for ADC input (mic *and* line-in on AudioKit) From dd1cf0ec561daf762add59be0892c3ae270a14a8 Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Mon, 19 Jun 2023 13:27:54 +0100 Subject: [PATCH 08/11] AC lacks ERRORSR_PRINTF --- usermods/audioreactive/audio_source.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index e7ccf44df..7ab4faec5 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -577,12 +577,12 @@ class ES8388Source : public I2SSource { // check that pins are valid if ((sdaPin < 0) || (sclPin < 0)) { - ERRORSR_PRINTF("\nAR: invalid ES8388 I2C pins: SDA=%d, SCL=%d\n", sdaPin, sclPin); + DEBUGSR_PRINTF("\nAR: invalid ES8388 I2C pins: SDA=%d, SCL=%d\n", sdaPin, sclPin); return; } if ((i2sckPin < 0) || (mclkPin < 0)) { - ERRORSR_PRINTF("\nAR: invalid I2S pin: SCK=%d, MCLK=%d\n", i2sckPin, mclkPin); + DEBUGSR_PRINTF("\nAR: invalid I2S pin: SCK=%d, MCLK=%d\n", i2sckPin, mclkPin); return; } @@ -590,7 +590,7 @@ class ES8388Source : public I2SSource { 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); + DEBUGSR_PRINTF("\nAR: Failed to allocate ES8388 I2C pins: SDA=%d, SCL=%d\n", sdaPin, sclPin); return; } From b29611509eb0944c445ec1bab4a989161722957a Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Mon, 19 Jun 2023 13:33:16 +0100 Subject: [PATCH 09/11] AC lacks ERRORSR_PRINTLN --- usermods/audioreactive/audio_source.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 7ab4faec5..21155c48f 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -472,7 +472,7 @@ class ES8388Source : public I2SSource { 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.")); + DEBUGSR_PRINTLN(F("AR: ES8388 failed to initialize I2C bus driver.")); } } From 63df85f7f967133038a80f0dc00c67e0088519d1 Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Mon, 19 Jun 2023 13:33:46 +0100 Subject: [PATCH 10/11] AC lacks i2sMaster param to I2SSource --- usermods/audioreactive/audio_source.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usermods/audioreactive/audio_source.h b/usermods/audioreactive/audio_source.h index 21155c48f..fdafee1d7 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -565,7 +565,7 @@ class ES8388Source : public I2SSource { public: ES8388Source(SRate_t sampleRate, int blockSize, float sampleScale = 1.0f, bool i2sMaster=true) : - I2SSource(sampleRate, blockSize, sampleScale, i2sMaster) { + I2SSource(sampleRate, blockSize, sampleScale) { _config.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT; }; From 7d4fe341f1a660d15c0c696701f948c1d7b7128b Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Tue, 18 Jul 2023 17:21:45 +0100 Subject: [PATCH 11/11] Remove direct setup for I2C from ES8388 driver --- usermods/audioreactive/audio_reactive.h | 2 +- usermods/audioreactive/audio_source.h | 37 +------------------------ 2 files changed, 2 insertions(+), 37 deletions(-) diff --git a/usermods/audioreactive/audio_reactive.h b/usermods/audioreactive/audio_reactive.h index b0964d4b9..357e612c3 100644 --- a/usermods/audioreactive/audio_reactive.h +++ b/usermods/audioreactive/audio_reactive.h @@ -1153,7 +1153,7 @@ class AudioReactive : public Usermod { DEBUGSR_PRINTLN(F("AR: ES8388 Source")); audioSource = new ES8388Source(SAMPLE_RATE, BLOCK_SIZE); delay(100); - if (audioSource) audioSource->initialize(sdaPin, sclPin, i2swsPin, i2ssdPin, i2sckPin, mclkPin); + if (audioSource) audioSource->initialize(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 c230dcc03..0ba24e8bf 100644 --- a/usermods/audioreactive/audio_source.h +++ b/usermods/audioreactive/audio_source.h @@ -433,13 +433,6 @@ 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) { - DEBUGSR_PRINTLN(F("AR: ES8388 failed to initialize I2C bus driver.")); - } - } void _es8388I2cWrite(uint8_t reg, uint8_t val) { #ifndef ES8388_ADDR @@ -464,7 +457,6 @@ 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 @@ -534,49 +526,22 @@ class ES8388Source : public I2SSource { _config.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT; }; - 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)) { - DEBUGSR_PRINTF("\nAR: invalid ES8388 I2C pins: SDA=%d, SCL=%d\n", sdaPin, sclPin); - return; - } + void initialize(int8_t i2swsPin, int8_t i2ssdPin, int8_t i2sckPin, int8_t mclkPin) { if ((i2sckPin < 0) || (mclkPin < 0)) { DEBUGSR_PRINTF("\nAR: invalid I2S pin: SCK=%d, MCLK=%d\n", i2sckPin, mclkPin); 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); - DEBUGSR_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)