From 079b2b53d434869df419da1373aba239990c34d9 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 24 Aug 2023 19:18:57 +1000 Subject: [PATCH] Improve DAC startup for r6 --- src/drivers/i2s_dac.cpp | 30 ++++++++++++++++++++++++------ src/drivers/include/gpios.hpp | 3 ++- src/drivers/include/i2s_dac.hpp | 1 + src/system_fsm/booting.cpp | 4 ++-- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/drivers/i2s_dac.cpp b/src/drivers/i2s_dac.cpp index 4fe939f5..af2125a6 100644 --- a/src/drivers/i2s_dac.cpp +++ b/src/drivers/i2s_dac.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include "assert.h" #include "driver/i2c.h" @@ -100,11 +101,14 @@ I2SDac::~I2SDac() { } auto I2SDac::Start() -> void { + std::lock_guard lock(configure_mutex_); + + gpio_->WriteSync(IGpios::Pin::kAmplifierUnmute, false); // Ramp up the amplifier power supply. gpio_->WriteSync(IGpios::Pin::kAmplifierEnable, true); // Wait for voltage to stabilise - vTaskDelay(pdMS_TO_TICKS(1)); + vTaskDelay(pdMS_TO_TICKS(5)); // Ensure the DAC powers up to a muted state. wm8523::WriteRegister(wm8523::Register::kPsCtrl, 0b10); @@ -112,26 +116,40 @@ auto I2SDac::Start() -> void { // Enable MCLK; this has the side effect of triggering the DAC's startup // sequence. i2s_channel_enable(i2s_handle_); + i2s_active_ = true; // Wait for DAC output lines to stabilise - vTaskDelay(pdMS_TO_TICKS(1)); + vTaskDelay(pdMS_TO_TICKS(5)); - // FIXME: Pull the amp's EN pin here. wm8523::WriteRegister(wm8523::Register::kPsCtrl, 0b11); - i2s_active_ = true; + + vTaskDelay(pdMS_TO_TICKS(5)); + gpio_->WriteSync(IGpios::Pin::kAmplifierUnmute, true); } auto I2SDac::Stop() -> void { + std::lock_guard lock(configure_mutex_); + + // Mute the DAC. + wm8523::WriteRegister(wm8523::Register::kPsCtrl, 0b10); + vTaskDelay(pdMS_TO_TICKS(5)); + // Silence the output. + gpio_->WriteSync(IGpios::Pin::kAmplifierUnmute, false); + + vTaskDelay(pdMS_TO_TICKS(5)); + + // Turn everything off. wm8523::WriteRegister(wm8523::Register::kPsCtrl, 0b0); i2s_channel_disable(i2s_handle_); + i2s_active_ = false; gpio_->WriteSync(IGpios::Pin::kAmplifierEnable, false); - - i2s_active_ = false; } auto I2SDac::Reconfigure(Channels ch, BitsPerSample bps, SampleRate rate) -> void { + std::lock_guard lock(configure_mutex_); + if (i2s_active_) { // Ramp down into mute instead of just outright stopping to minimise any // clicks and pops. diff --git a/src/drivers/include/gpios.hpp b/src/drivers/include/gpios.hpp index fe330e3b..18f71551 100644 --- a/src/drivers/include/gpios.hpp +++ b/src/drivers/include/gpios.hpp @@ -54,7 +54,8 @@ class IGpios { kPhoneDetect = 8, kAmplifierEnable = 9, kSdCardDetect = 10, - // 11 through 15 are unused + kAmplifierUnmute = 11, + // 12 through 15 are unused }; /* Nicer value names for use with kSdMuxSwitch. */ diff --git a/src/drivers/include/i2s_dac.hpp b/src/drivers/include/i2s_dac.hpp index 889ba68c..c7faed2f 100644 --- a/src/drivers/include/i2s_dac.hpp +++ b/src/drivers/include/i2s_dac.hpp @@ -73,6 +73,7 @@ class I2SDac { i2s_chan_handle_t i2s_handle_; bool i2s_active_; StreamBufferHandle_t buffer_; + std::mutex configure_mutex_; i2s_std_clk_config_t clock_config_; i2s_std_slot_config_t slot_config_; diff --git a/src/system_fsm/booting.cpp b/src/system_fsm/booting.cpp index 78653592..33ed39d1 100644 --- a/src/system_fsm/booting.cpp +++ b/src/system_fsm/booting.cpp @@ -80,8 +80,8 @@ auto Booting::entry() -> void { true, NULL, battery_timer_cb); xTimerStart(battery_timer, portMAX_DELAY); - ESP_LOGI(kTag, "starting bluetooth"); - sBluetooth.reset(new drivers::Bluetooth(sNvs.get())); + // ESP_LOGI(kTag, "starting bluetooth"); + // sBluetooth.reset(new drivers::Bluetooth(sNvs.get())); // sBluetooth->Enable(); // At this point we've done all of the essential boot tasks. Start remaining