From e65c1ce4dc02b751ff9a570f27ee881f4fdb7f17 Mon Sep 17 00:00:00 2001 From: uncarvedblock78 Date: Thu, 19 Jan 2023 09:44:33 -0500 Subject: [PATCH] add ALC in voice mode --- PlatformIO/src/devices/AudioKit.hpp | 8 +++- PlatformIO/src/devices/ES8388Control.cpp | 53 ++++++++++++++++++++++++ PlatformIO/src/devices/ES8388Control.h | 9 ++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/PlatformIO/src/devices/AudioKit.hpp b/PlatformIO/src/devices/AudioKit.hpp index 8ea5f3e..61197d6 100644 --- a/PlatformIO/src/devices/AudioKit.hpp +++ b/PlatformIO/src/devices/AudioKit.hpp @@ -287,6 +287,12 @@ void AudioKit::InitI2SSpeakerOrMic(int mode) // ES8388Control requires MCLK output. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1); WRITE_PERI_REG(PIN_CTRL, 0xFFF0); + + if (mode == MODE_MIC) { + es8388.setALCmode(VOICE); + } else { + es8388.setALCmode(DISABLE); + } } return; } @@ -468,4 +474,4 @@ void AudioKit::ampOutput(int ampOut) void AudioKit::updateBrightness(int brightness) { indicator_light->setMaxBrightness((brightness * indicator_light->limit) / 100); -} \ No newline at end of file +} diff --git a/PlatformIO/src/devices/ES8388Control.cpp b/PlatformIO/src/devices/ES8388Control.cpp index 277ab0d..2f79520 100644 --- a/PlatformIO/src/devices/ES8388Control.cpp +++ b/PlatformIO/src/devices/ES8388Control.cpp @@ -149,6 +149,59 @@ bool ES8388Control::begin(int sda, int scl, uint32_t frequency) return res; } +// Recommended ALC setting from User Guide +// DISABLE -> Disable ALC +// GENERIC -> Generic Mode +// VOICE -> Voice Mode +// MUSIC -> Music Mode +bool ES8388Control::setALCmode(alcmodesel_t alc) { + bool res = true; + + // generic ALC setting + uint8_t ALCSEL = 0b11; // stereo + uint8_t ALCLVL = 0b0011; //-12db + uint8_t MAXGAIN = 0b111; //+35.5db + uint8_t MINGAIN = 0b000; //-12db + uint8_t ALCHLD = 0b0000; // 0ms + uint8_t ALCDCY = 0b0101; // 13.1ms/step + uint8_t ALCATK = 0b0111; // 13.3ms/step + uint8_t ALCMODE = 0b0; // ALC + uint8_t ALCZC = 0b0; // ZC off + uint8_t TIME_OUT = 0b0; // disable + uint8_t NGAT = 0b1; // enable + uint8_t NGTH = 0b10001; //-51db + uint8_t NGG = 0b00; // hold gain + uint8_t WIN_SIZE = 0b00110; // default + + if (alc == DISABLE) + ALCSEL = 0b00; + else if (alc == MUSIC) { + ALCDCY = 0b1010; // 420ms/step + ALCATK = 0b0110; // 6.66ms/step + NGTH = 0b01011; // -60db + } else if (alc == VOICE) { + ALCLVL = 0b1100; // -4.5db + MAXGAIN = 0b101; // +23.5db + MINGAIN = 0b010; // 0db + ALCDCY = 0b0001; // 820us/step + ALCATK = 0b0010; // 416us/step + NGTH = 0b11000; // -40.5db + NGG = 0b01; // mute ADC + res &= write_reg(ES8388_ADDR, ES8388_ADCCONTROL1, 0x77); + } + res &= write_reg(ES8388_ADDR, ES8388_ADCCONTROL10, + ALCSEL << 6 | MAXGAIN << 3 | MINGAIN); + res &= write_reg(ES8388_ADDR, ES8388_ADCCONTROL11, + ALCLVL << 4 | ALCHLD); + res &= write_reg(ES8388_ADDR, ES8388_ADCCONTROL12, ALCDCY << 4 | ALCATK); + res &= write_reg(ES8388_ADDR, ES8388_ADCCONTROL13, + ALCMODE << 7 | ALCZC << 6 | TIME_OUT << 5 | WIN_SIZE); + res &= write_reg(ES8388_ADDR, ES8388_ADCCONTROL14, + NGTH << 3 | NGG << 2 | NGAT); + + return res; +} + /** * @brief (un)mute one of the two outputs or main dac output of the ES8388 by switching of the output register bits. * Does not really mute the selected output, causes an attenuation. hence should be used in conjunction with appropriate diff --git a/PlatformIO/src/devices/ES8388Control.h b/PlatformIO/src/devices/ES8388Control.h index f617225..f1e3f30 100644 --- a/PlatformIO/src/devices/ES8388Control.h +++ b/PlatformIO/src/devices/ES8388Control.h @@ -1,6 +1,13 @@ #pragma once #include +typedef enum { + DISABLE, // ALC Disabled + GENERIC, // ALC Generic Mode + VOICE, // ALC Voice Mode + MUSIC, // ALC Music Mode +} alcmodesel_t; + class ES8388Control { @@ -20,4 +27,6 @@ class ES8388Control void mute(const ES8388_OUT out, const bool muted); void volume(const ES8388_OUT out, const uint8_t vol); + + bool setALCmode(alcmodesel_t alc); };