From 55264a826f006c51990abc84949734ee8ec0646b Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 6 Oct 2022 12:56:13 +1100 Subject: [PATCH] Almost working DAC --- main/dac.cpp | 51 +++++++++++++++++++++++++++++++++++++++----- main/dac.h | 1 + main/gay-ipod-fw.cpp | 6 ++---- main/gpio-expander.h | 2 +- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/main/dac.cpp b/main/dac.cpp index 625c924a..44955491 100644 --- a/main/dac.cpp +++ b/main/dac.cpp @@ -44,6 +44,25 @@ void AudioDac::Start(SampleRate sample_rate, BitDepth bit_depth) { return; } + // Next, wait for the IC to boot up before we try configuring it. + bool is_booted = false; + for (int i=0; i<10; i++) { + uint8_t result = ReadPowerState(); + is_booted = result >> 7; + if (is_booted) { + break; + } else { + ESP_LOGI(TAG, "Waiting for boot..."); + vTaskDelay(pdMS_TO_TICKS(1)); + } + } + + if (!is_booted) { + // TODO: properly handle + ESP_LOGE(TAG, "Timed out waiting for boot!"); + return; + } + // Now do all the math required to correctly set up the internal clocks. int p, j, d, r; int nmac, ndac, ncp, dosr, idac; @@ -153,15 +172,36 @@ void AudioDac::Start(SampleRate sample_rate, BitDepth bit_depth) { i2c_master_stop(handle); + ESP_LOGI(TAG, "Configuring DAC"); // TODO: Handle this gracefully. - ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, handle, 50)); + ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, handle, kPCM5122Timeout)); i2c_cmd_link_delete(handle); - vTaskDelay(pdMS_TO_TICKS(10)); + // The DAC takes a moment to reconfigure itself. Give it some time before we + // start asking for its state. + vTaskDelay(pdMS_TO_TICKS(5)); + + // TODO: investigate why it's stuck waiting for CP voltage. + /* + bool is_configured = false; + for (int i=0; i<10; i++) { + uint8_t result = ReadPowerState(); + is_configured = (result & 0b1111) == 0b1001; + if (is_configured) { + break; + } else { + ESP_LOGI(TAG, "Waiting for configure..."); + vTaskDelay(pdMS_TO_TICKS(1)); + } + } - // TODO: Handle this gracefully. - assert(ReadPowerState() == 0x05); + if (!is_configured) { + // TODO: properly handle + ESP_LOGE(TAG, "Timed out waiting for configure!"); + return; + } + */ } uint8_t AudioDac::ReadPowerState() { @@ -176,10 +216,11 @@ uint8_t AudioDac::ReadPowerState() { i2c_master_write_byte(handle, (kPCM5122Address << 1 | I2C_MASTER_WRITE), true); i2c_master_write_byte(handle, DSP_BOOT_POWER_STATE, true); i2c_master_start(handle); + i2c_master_write_byte(handle, (kPCM5122Address << 1 | I2C_MASTER_READ), true); i2c_master_read_byte(handle, &result, I2C_MASTER_NACK); i2c_master_stop(handle); - ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, handle, 50)); + ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, handle, kPCM5122Timeout)); i2c_cmd_link_delete(handle); diff --git a/main/dac.h b/main/dac.h index f196b315..bb06a0e7 100644 --- a/main/dac.h +++ b/main/dac.h @@ -6,6 +6,7 @@ namespace gay_ipod { static const uint8_t kPCM5122Address = 0x4C; + static const uint8_t kPCM5122Timeout = 100 / portTICK_RATE_MS; /** * PCM5122PWR diff --git a/main/gay-ipod-fw.cpp b/main/gay-ipod-fw.cpp index 3b6c855f..bc03a32b 100644 --- a/main/gay-ipod-fw.cpp +++ b/main/gay-ipod-fw.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -113,16 +114,13 @@ extern "C" void app_main(void) ESP_LOGI(TAG, "Everything looks good! Waiting a mo for debugger."); vTaskDelay(pdMS_TO_TICKS(1500)); - /* - * TODO: not working :( ESP_LOGI(TAG, "Trying to init DAC"); gay_ipod::AudioDac dac(&expander); dac.Start( - gay_ipod::AudioDac::SAMPLE_RATE_44_1K, + gay_ipod::AudioDac::SAMPLE_RATE_48K, gay_ipod::AudioDac::BIT_DEPTH_16); vTaskDelay(pdMS_TO_TICKS(1000)); - */ ESP_LOGI(TAG, "Looks okay? Let's list some files!"); vTaskDelay(pdMS_TO_TICKS(1000)); diff --git a/main/gpio-expander.h b/main/gpio-expander.h index 2b213ddc..c1003f9d 100644 --- a/main/gpio-expander.h +++ b/main/gpio-expander.h @@ -51,7 +51,7 @@ class GpioExpander { // 6 - sd chip select // 7 - display chip select // All power switches low, chip selects high, active-low charge power high - uint8_t port_a_ = 0b11010000; + uint8_t port_a_ = 0b11010001; // Port B: // 0 - 3.5mm jack detect (active low)