From a0ae39befe11c2a5a78ee5877cd5dd9cda90e27c Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 30 Mar 2023 09:31:08 +1100 Subject: Update pinouts for R3 --- src/drivers/dac.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/drivers/dac.cpp') diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index 1ab562f9..c9af0d99 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -105,14 +105,14 @@ AudioDac::AudioDac(GpioExpander* gpio, i2s_chan_handle_t i2s_handle) clock_config_(I2S_STD_CLK_DEFAULT_CONFIG(44100)), slot_config_(I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) { - gpio_->set_pin(GpioExpander::AUDIO_POWER_ENABLE, true); + gpio_->set_pin(GpioExpander::AMP_EN, true); gpio_->Write(); } AudioDac::~AudioDac() { i2s_channel_disable(i2s_handle_); i2s_del_channel(i2s_handle_); - gpio_->set_pin(GpioExpander::AUDIO_POWER_ENABLE, false); + gpio_->set_pin(GpioExpander::AMP_EN, false); gpio_->Write(); } -- cgit v1.2.3 From a9531c86a433c8b7ae1f77ff0266c27c39eca7f4 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 10 Mar 2023 11:28:33 +1100 Subject: mostly single task pipeline --- src/drivers/dac.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/drivers/dac.cpp') diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index c9af0d99..4d3aca1d 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -192,7 +192,8 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { WriteRegister(Register::POWER_MODE, 0); } -auto AudioDac::WriteData(cpp::span data) -> std::size_t { +auto AudioDac::WriteData(const cpp::span& data) + -> std::size_t { std::size_t bytes_written = 0; esp_err_t err = i2s_channel_write(i2s_handle_, data.data(), data.size_bytes(), &bytes_written, 0); -- cgit v1.2.3 From 7c6fd654f50e6665efa4226c6b927f9762734182 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Sat, 1 Apr 2023 13:22:21 +1100 Subject: New pipeline building, still needs proper control --- src/drivers/dac.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/drivers/dac.cpp') diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index 4d3aca1d..1f3ba557 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -192,15 +192,13 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { WriteRegister(Register::POWER_MODE, 0); } -auto AudioDac::WriteData(const cpp::span& data) - -> std::size_t { +auto AudioDac::WriteData(const cpp::span& data) -> void { std::size_t bytes_written = 0; esp_err_t err = i2s_channel_write(i2s_handle_, data.data(), data.size_bytes(), - &bytes_written, 0); + &bytes_written, portMAX_DELAY); if (err != ESP_ERR_TIMEOUT) { ESP_ERROR_CHECK(err); } - return bytes_written; } auto AudioDac::Stop() -> void { -- cgit v1.2.3 From 3836768bb8b95188e6657ab69027d1d9e4b13a77 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 3 Apr 2023 14:06:30 +1000 Subject: new pipeline working(?), but the dac eludes me --- src/drivers/dac.cpp | 66 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 23 deletions(-) (limited to 'src/drivers/dac.cpp') diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index 1f3ba557..60679678 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -13,6 +13,7 @@ #include "esp_log.h" #include "freertos/portmacro.h" #include "freertos/projdefs.h" +#include "hal/gpio_types.h" #include "hal/i2c_types.h" #include "gpio_expander.hpp" @@ -50,21 +51,23 @@ auto AudioDac::create(GpioExpander* expander) i2s_std_config_t i2s_config = { .clk_cfg = dac->clock_config_, .slot_cfg = dac->slot_config_, - .gpio_cfg = - {// TODO: investigate running in three wire mode for less noise - .mclk = GPIO_NUM_0, - .bclk = GPIO_NUM_26, - .ws = GPIO_NUM_27, - .dout = GPIO_NUM_5, - .din = I2S_GPIO_UNUSED, - .invert_flags = - { - .mclk_inv = false, - .bclk_inv = false, - .ws_inv = false, - }}, + .gpio_cfg = {.mclk = GPIO_NUM_0, + //.mclk = I2S_GPIO_UNUSED, + .bclk = GPIO_NUM_26, + .ws = GPIO_NUM_27, + .dout = GPIO_NUM_5, + .din = I2S_GPIO_UNUSED, + .invert_flags = + { + .mclk_inv = false, + .bclk_inv = false, + .ws_inv = false, + }}, }; + // gpio_set_direction(GPIO_NUM_0, GPIO_MODE_OUTPUT); + // gpio_set_level(GPIO_NUM_0, 0); + if (esp_err_t err = i2s_channel_init_std_mode(i2s_handle, &i2s_config) != ESP_OK) { ESP_LOGE(kTag, "failed to initialise i2s channel %x", err); @@ -81,20 +84,29 @@ auto AudioDac::create(GpioExpander* expander) // The DAC should be booted but in power down mode, but it might not be if we // didn't shut down cleanly. Reset it to ensure it is in a consistent state. - dac->WriteRegister(Register::POWER_MODE, 0b10001); dac->WriteRegister(Register::POWER_MODE, 1 << 4); dac->WriteRegister(Register::RESET, 0b10001); + // Use BCK for the internal PLL. + // dac->WriteRegister(Register::PLL_CLOCK_SOURCE, 1 << 4); + + // dac->WriteRegister(Register::PLL_ENABLE, 0); + dac->WriteRegister(Register::INTERPOLATION, 1 << 4); + + dac->Reconfigure(BPS_16, SAMPLE_RATE_44_1); + dac->WriteRegister(Register::POWER_MODE, 0); + // Now configure the DAC for standard auto-clock SCK mode. - dac->WriteRegister(Register::DAC_CLOCK_SOURCE, 0b11 << 5); + // dac->WriteRegister(Register::DAC_CLOCK_SOURCE, 0b11 << 5); // Enable auto clocking, and do your best to carry on despite errors. // dac->WriteRegister(Register::CLOCK_ERRORS, 0b1111101); - i2s_channel_enable(dac->i2s_handle_); + // i2s_channel_enable(dac->i2s_handle_); - dac->WaitForPowerState( - [](bool booted, PowerState state) { return state == STANDBY; }); + dac->WaitForPowerState([](bool booted, PowerState state) { + return state == RUN || state == STANDBY; + }); return dac; } @@ -102,6 +114,7 @@ auto AudioDac::create(GpioExpander* expander) AudioDac::AudioDac(GpioExpander* gpio, i2s_chan_handle_t i2s_handle) : gpio_(gpio), i2s_handle_(i2s_handle), + i2s_active_(false), clock_config_(I2S_STD_CLK_DEFAULT_CONFIG(44100)), slot_config_(I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) { @@ -163,9 +176,10 @@ bool AudioDac::WaitForPowerState( } auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { - // Disable the current output, if it isn't already stopped. - WriteRegister(Register::POWER_MODE, 1 << 4); - i2s_channel_disable(i2s_handle_); + WriteRegister(Register::RESYNC_REQUEST, 1); + if (i2s_active_) { + i2s_channel_disable(i2s_handle_); + } // I2S reconfiguration. @@ -181,15 +195,21 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { ESP_ERROR_CHECK(i2s_channel_reconfig_std_clock(i2s_handle_, &clock_config_)); // DAC reconfiguration. + if (rate == SAMPLE_RATE_44_1) { + WriteRegister(Register::DE_EMPHASIS, 1 << 4); + } else { + WriteRegister(Register::DE_EMPHASIS, 0); + } // TODO: base on BPS - WriteRegister(Register::I2S_FORMAT, 0); + WriteRegister(Register::I2S_FORMAT, 0b00); // Configuration is all done, so we can now bring the DAC and I2S stream back // up. I2S first, since otherwise the DAC will see that there's no clocks and // shut itself down. + WriteRegister(Register::RESYNC_REQUEST, 0); ESP_ERROR_CHECK(i2s_channel_enable(i2s_handle_)); - WriteRegister(Register::POWER_MODE, 0); + i2s_active_ = true; } auto AudioDac::WriteData(const cpp::span& data) -> void { -- cgit v1.2.3 From 40a9734b04c48339cfdf6ed9043aa3f6f0dda62d Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 4 Apr 2023 09:46:52 +1000 Subject: Redo pcm registers to include pages --- src/drivers/dac.cpp | 117 ++++++++++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 59 deletions(-) (limited to 'src/drivers/dac.cpp') diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index 60679678..40663219 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -5,9 +5,7 @@ #include "assert.h" #include "driver/i2c.h" -#include "driver/i2s_common.h" -#include "driver/i2s_std.h" -#include "driver/i2s_types.h" +#include "driver/i2s_types_legacy.h" #include "esp_attr.h" #include "esp_err.h" #include "esp_log.h" @@ -19,6 +17,7 @@ #include "gpio_expander.hpp" #include "hal/i2s_types.h" #include "i2c.hpp" +#include "soc/clk_tree_defs.h" #include "sys/_stdint.h" namespace drivers { @@ -38,6 +37,7 @@ auto AudioDac::create(GpioExpander* expander) i2s_chan_handle_t i2s_handle; i2s_chan_config_t channel_config = I2S_CHANNEL_DEFAULT_CONFIG(kI2SPort, I2S_ROLE_MASTER); + channel_config.auto_clear = true; ESP_ERROR_CHECK(i2s_new_channel(&channel_config, &i2s_handle, NULL)); // @@ -84,20 +84,22 @@ auto AudioDac::create(GpioExpander* expander) // The DAC should be booted but in power down mode, but it might not be if we // didn't shut down cleanly. Reset it to ensure it is in a consistent state. - dac->WriteRegister(Register::POWER_MODE, 1 << 4); - dac->WriteRegister(Register::RESET, 0b10001); + dac->WriteRegister(pcm512x::POWER, 1 << 4); + dac->WriteRegister(pcm512x::RESET, 0b10001); // Use BCK for the internal PLL. // dac->WriteRegister(Register::PLL_CLOCK_SOURCE, 1 << 4); + // dac->WriteRegister(Register::DAC_CLOCK_SOURCE, 0b11 << 5); - // dac->WriteRegister(Register::PLL_ENABLE, 0); - dac->WriteRegister(Register::INTERPOLATION, 1 << 4); + //dac->WriteRegister(Register::PLL_ENABLE, 0); + //dac->WriteRegister(Register::DAC_CLOCK_SOURCE, 0b0110000); + //dac->WriteRegister(Register::CLOCK_ERRORS, 0b01000001); + //dac->WriteRegister(Register::I2S_FORMAT, 0b110000); + // dac->WriteRegister(Register::INTERPOLATION, 1 << 4); dac->Reconfigure(BPS_16, SAMPLE_RATE_44_1); - dac->WriteRegister(Register::POWER_MODE, 0); // Now configure the DAC for standard auto-clock SCK mode. - // dac->WriteRegister(Register::DAC_CLOCK_SOURCE, 0b11 << 5); // Enable auto clocking, and do your best to carry on despite errors. // dac->WriteRegister(Register::CLOCK_ERRORS, 0b1111101); @@ -115,9 +117,11 @@ AudioDac::AudioDac(GpioExpander* gpio, i2s_chan_handle_t i2s_handle) : gpio_(gpio), i2s_handle_(i2s_handle), i2s_active_(false), + active_page_(), clock_config_(I2S_STD_CLK_DEFAULT_CONFIG(44100)), - slot_config_(I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, - I2S_SLOT_MODE_STEREO)) { + slot_config_(I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, + I2S_SLOT_MODE_STEREO)) { + clock_config_.clk_src = I2S_CLK_SRC_PLL_160M; gpio_->set_pin(GpioExpander::AMP_EN, true); gpio_->Write(); } @@ -130,29 +134,14 @@ AudioDac::~AudioDac() { } void AudioDac::WriteVolume(uint8_t volume) { - WriteRegister(Register::DIGITAL_VOLUME_L, volume); - WriteRegister(Register::DIGITAL_VOLUME_R, volume); + // Left channel. + WriteRegister(pcm512x::DIGITAL_VOLUME_2, volume); + // Right channel. + WriteRegister(pcm512x::DIGITAL_VOLUME_3, volume); } std::pair AudioDac::ReadPowerState() { - uint8_t result = 0; - - I2CTransaction transaction; - transaction.start() - .write_addr(kPcm5122Address, I2C_MASTER_WRITE) - .write_ack(DSP_BOOT_POWER_STATE) - .start() - .write_addr(kPcm5122Address, I2C_MASTER_READ) - .read(&result, I2C_MASTER_NACK) - .stop(); - - esp_err_t err = transaction.Execute(); - if (err == ESP_ERR_TIMEOUT) { - return std::pair(false, POWERDOWN); - } else { - } - ESP_ERROR_CHECK(err); - + uint8_t result = ReadRegister(pcm512x::POWER_STATE); bool is_booted = result >> 7; PowerState detail = (PowerState)(result & 0b1111); return std::pair(is_booted, detail); @@ -176,17 +165,17 @@ bool AudioDac::WaitForPowerState( } auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { - WriteRegister(Register::RESYNC_REQUEST, 1); if (i2s_active_) { + WriteRegister(pcm512x::POWER, 1 << 4); i2s_channel_disable(i2s_handle_); } // I2S reconfiguration. - slot_config_.slot_bit_width = (i2s_slot_bit_width_t)bps; + slot_config_.slot_bit_width = I2S_SLOT_BIT_WIDTH_16BIT; ESP_ERROR_CHECK(i2s_channel_reconfig_std_slot(i2s_handle_, &slot_config_)); - clock_config_.sample_rate_hz = rate; + clock_config_.sample_rate_hz = 44100; // If we have an MCLK/SCK, then it must be a multiple of both the sample rate // and the bit clock. At 24 BPS, we therefore have to change the MCLK multiple // to avoid issues at some sample rates. (e.g. 48KHz) @@ -194,21 +183,19 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { bps == BPS_24 ? I2S_MCLK_MULTIPLE_384 : I2S_MCLK_MULTIPLE_256; ESP_ERROR_CHECK(i2s_channel_reconfig_std_clock(i2s_handle_, &clock_config_)); - // DAC reconfiguration. - if (rate == SAMPLE_RATE_44_1) { - WriteRegister(Register::DE_EMPHASIS, 1 << 4); - } else { - WriteRegister(Register::DE_EMPHASIS, 0); - } - // TODO: base on BPS - WriteRegister(Register::I2S_FORMAT, 0b00); + // WriteRegister(Register::I2S_FORMAT, 0b110000); // Configuration is all done, so we can now bring the DAC and I2S stream back // up. I2S first, since otherwise the DAC will see that there's no clocks and // shut itself down. - WriteRegister(Register::RESYNC_REQUEST, 0); ESP_ERROR_CHECK(i2s_channel_enable(i2s_handle_)); + WriteRegister(pcm512x::POWER, 0); + WriteRegister(pcm512x::SYNCHRONIZE, 1); + vTaskDelay(pdMS_TO_TICKS(10)); + WriteRegister(pcm512x::SYNCHRONIZE, 0); + vTaskDelay(pdMS_TO_TICKS(10)); + LogStatus(); i2s_active_ = true; } @@ -223,7 +210,7 @@ auto AudioDac::WriteData(const cpp::span& data) -> void { auto AudioDac::Stop() -> void { LogStatus(); - WriteRegister(Register::POWER_MODE, 1 << 4); + WriteRegister(pcm512x::POWER, 1 << 4); i2s_channel_disable(i2s_handle_); } @@ -237,35 +224,47 @@ auto AudioDac::Stop() -> void { auto AudioDac::LogStatus() -> void { uint8_t res; - res = ReadRegister(Register::SAMPLE_RATE_DETECTION); - ESP_LOGI(kTag, "detected sample rate (want 3): %u", (res >> 4) && 0b111); + res = ReadRegister(pcm512x::RATE_DET_1); + ESP_LOGI(kTag, "detected sample rate (want 3): %u", (res & 0b01110000) >> 4); ESP_LOGI(kTag, "detected SCK ratio (want 6): %u", res && 0b1111); - res = ReadRegister(Register::BCK_DETECTION); + res = ReadRegister(pcm512x::RATE_DET_3); ESP_LOGI(kTag, "detected BCK (want... 16? 32?): %u", res); - res = ReadRegister(Register::CLOCK_ERROR_STATE); + res = ReadRegister(pcm512x::RATE_DET_4); ESP_LOGI(kTag, "clock errors (want zeroes): "); ESP_LOGI(kTag, BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(res & 0b1111111)); - res = ReadRegister(Register::CLOCK_STATUS); + res = ReadRegister(pcm512x::CLOCK_STATUS); ESP_LOGI(kTag, "clock status (want zeroes): "); ESP_LOGI(kTag, BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(res & 0b10111)); - res = ReadRegister(Register::AUTO_MUTE_STATE); - ESP_LOGI(kTag, "automute status (want 3): %u", res & 0b11); - - res = ReadRegister(Register::SOFT_MUTE_STATE); - ESP_LOGI(kTag, "soft mute pin status (want 3): %u", res & 0b11); - - res = ReadRegister(Register::SAMPLE_RATE_STATE); - ESP_LOGI(kTag, "detected sample speed mode (want 0): %u", res & 0b11); + res = ReadRegister(pcm512x::DIGITAL_MUTE_DET); + ESP_LOGI(kTag, "automute status (want 0): %u", res & 0b10001); auto power = ReadPowerState(); ESP_LOGI(kTag, "current power state (want 5): %u", power.second); } -void AudioDac::WriteRegister(Register reg, uint8_t val) { +void AudioDac::WriteRegister(pcm512x::Register r, uint8_t val) { + SelectPage(r.page); + WriteRegisterRaw(r.reg, val); +} + +uint8_t AudioDac::ReadRegister(pcm512x::Register r) { + SelectPage(r.page); + return ReadRegisterRaw(r.reg); +} + +void AudioDac::SelectPage(uint8_t page) { + if (active_page_ && active_page_ == page) { + return; + } + WriteRegisterRaw(0, page); + active_page_ = page; +} + +void AudioDac::WriteRegisterRaw(uint8_t reg, uint8_t val) { I2CTransaction transaction; transaction.start() .write_addr(kPcm5122Address, I2C_MASTER_WRITE) @@ -275,7 +274,7 @@ void AudioDac::WriteRegister(Register reg, uint8_t val) { transaction.Execute(); } -uint8_t AudioDac::ReadRegister(Register reg) { +uint8_t AudioDac::ReadRegisterRaw(uint8_t reg) { uint8_t result = 0; I2CTransaction transaction; transaction.start() -- cgit v1.2.3 From 561f9d2a07ee6ee1c2f18dc375125f87ea7b0d55 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 19 Apr 2023 13:00:42 +1000 Subject: Ensure the sink buffer is large enough to not fully drain during playback --- src/drivers/dac.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/drivers/dac.cpp') diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index 40663219..c99c88b0 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -91,11 +91,11 @@ auto AudioDac::create(GpioExpander* expander) // dac->WriteRegister(Register::PLL_CLOCK_SOURCE, 1 << 4); // dac->WriteRegister(Register::DAC_CLOCK_SOURCE, 0b11 << 5); - //dac->WriteRegister(Register::PLL_ENABLE, 0); - //dac->WriteRegister(Register::DAC_CLOCK_SOURCE, 0b0110000); - //dac->WriteRegister(Register::CLOCK_ERRORS, 0b01000001); - //dac->WriteRegister(Register::I2S_FORMAT, 0b110000); - // dac->WriteRegister(Register::INTERPOLATION, 1 << 4); + // dac->WriteRegister(Register::PLL_ENABLE, 0); + // dac->WriteRegister(Register::DAC_CLOCK_SOURCE, 0b0110000); + // dac->WriteRegister(Register::CLOCK_ERRORS, 0b01000001); + // dac->WriteRegister(Register::I2S_FORMAT, 0b110000); + // dac->WriteRegister(Register::INTERPOLATION, 1 << 4); dac->Reconfigure(BPS_16, SAMPLE_RATE_44_1); -- cgit v1.2.3 From 4c77950e702a329f3136456a932efbea36e03d42 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 19 Apr 2023 16:45:50 +1000 Subject: Pipeline working and outputting correctly, but noisy --- src/drivers/dac.cpp | 73 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 17 deletions(-) (limited to 'src/drivers/dac.cpp') diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index c99c88b0..0fe75a5e 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -5,7 +5,8 @@ #include "assert.h" #include "driver/i2c.h" -#include "driver/i2s_types_legacy.h" +#include "driver/i2s_common.h" +#include "driver/i2s_std.h" #include "esp_attr.h" #include "esp_err.h" #include "esp_log.h" @@ -24,13 +25,8 @@ namespace drivers { static const char* kTag = "AUDIODAC"; static const uint8_t kPcm5122Address = 0x4C; -static const uint8_t kPcm5122Timeout = pdMS_TO_TICKS(100); static const i2s_port_t kI2SPort = I2S_NUM_0; -static const AudioDac::SampleRate kDefaultSampleRate = - AudioDac::SAMPLE_RATE_44_1; -static const AudioDac::BitsPerSample kDefaultBps = AudioDac::BPS_16; - auto AudioDac::create(GpioExpander* expander) -> cpp::result, Error> { // TODO: tune. @@ -119,7 +115,7 @@ AudioDac::AudioDac(GpioExpander* gpio, i2s_chan_handle_t i2s_handle) i2s_active_(false), active_page_(), clock_config_(I2S_STD_CLK_DEFAULT_CONFIG(44100)), - slot_config_(I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, + slot_config_(I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) { clock_config_.clk_src = I2S_CLK_SRC_PLL_160M; gpio_->set_pin(GpioExpander::AMP_EN, true); @@ -171,11 +167,24 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { } // I2S reconfiguration. - - slot_config_.slot_bit_width = I2S_SLOT_BIT_WIDTH_16BIT; + uint8_t bps_bits = 0; + switch (bps) { + case BPS_16: + slot_config_.data_bit_width = I2S_DATA_BIT_WIDTH_16BIT; + bps_bits = 0; + break; + case BPS_24: + slot_config_.data_bit_width = I2S_DATA_BIT_WIDTH_24BIT; + bps_bits = 0b10; + break; + case BPS_32: + slot_config_.data_bit_width = I2S_DATA_BIT_WIDTH_32BIT; + bps_bits = 0b11; + break; + } ESP_ERROR_CHECK(i2s_channel_reconfig_std_slot(i2s_handle_, &slot_config_)); - clock_config_.sample_rate_hz = 44100; + clock_config_.sample_rate_hz = rate; // If we have an MCLK/SCK, then it must be a multiple of both the sample rate // and the bit clock. At 24 BPS, we therefore have to change the MCLK multiple // to avoid issues at some sample rates. (e.g. 48KHz) @@ -183,19 +192,14 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { bps == BPS_24 ? I2S_MCLK_MULTIPLE_384 : I2S_MCLK_MULTIPLE_256; ESP_ERROR_CHECK(i2s_channel_reconfig_std_clock(i2s_handle_, &clock_config_)); - // TODO: base on BPS - // WriteRegister(Register::I2S_FORMAT, 0b110000); + WriteRegister(pcm512x::I2S_1, (0b11 << 4) | bps_bits); + WriteRegister(pcm512x::I2S_2, 0); // Configuration is all done, so we can now bring the DAC and I2S stream back // up. I2S first, since otherwise the DAC will see that there's no clocks and // shut itself down. ESP_ERROR_CHECK(i2s_channel_enable(i2s_handle_)); WriteRegister(pcm512x::POWER, 0); - WriteRegister(pcm512x::SYNCHRONIZE, 1); - vTaskDelay(pdMS_TO_TICKS(10)); - WriteRegister(pcm512x::SYNCHRONIZE, 0); - vTaskDelay(pdMS_TO_TICKS(10)); - LogStatus(); i2s_active_ = true; } @@ -208,6 +212,41 @@ auto AudioDac::WriteData(const cpp::span& data) -> void { } } +IRAM_ATTR auto callback(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx) -> bool { + if (event == nullptr || user_ctx == nullptr) { + return false; + } + if (event->data == nullptr || event->size == 0) { + return false; + } + StreamBufferHandle_t *src = reinterpret_cast(user_ctx); + BaseType_t ret = false; + std::size_t bytes_received = xStreamBufferReceiveFromISR(*src, event->data, event->size, &ret); + if (bytes_received < event->size) { + // TODO(jacqueline): zero-pad. + } + return ret; +} + +auto AudioDac::SetSource(StreamBufferHandle_t *buffer) -> void { + if (i2s_active_) { + ESP_ERROR_CHECK(i2s_channel_disable(i2s_handle_)); + } + i2s_event_callbacks_t callbacks { + .on_recv = NULL, + .on_recv_q_ovf = NULL, + .on_sent = NULL, + .on_send_q_ovf = NULL, + }; + if (buffer != nullptr) { + callbacks.on_sent = &callback; + } + i2s_channel_register_event_callback(i2s_handle_, &callbacks, buffer); + if (i2s_active_) { + ESP_ERROR_CHECK(i2s_channel_enable(i2s_handle_)); + } +} + auto AudioDac::Stop() -> void { LogStatus(); WriteRegister(pcm512x::POWER, 1 << 4); -- cgit v1.2.3 From 731b2cfa77a063e98da8fa26acc1e7ed1de8c169 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 20 Apr 2023 11:25:43 +1000 Subject: working isr-based sink, but still grainy --- src/drivers/dac.cpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'src/drivers/dac.cpp') diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index 0fe75a5e..dc9b51c8 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -33,7 +33,7 @@ auto AudioDac::create(GpioExpander* expander) i2s_chan_handle_t i2s_handle; i2s_chan_config_t channel_config = I2S_CHANNEL_DEFAULT_CONFIG(kI2SPort, I2S_ROLE_MASTER); - channel_config.auto_clear = true; + // channel_config.auto_clear = true; ESP_ERROR_CHECK(i2s_new_channel(&channel_config, &i2s_handle, NULL)); // @@ -115,7 +115,7 @@ AudioDac::AudioDac(GpioExpander* gpio, i2s_chan_handle_t i2s_handle) i2s_active_(false), active_page_(), clock_config_(I2S_STD_CLK_DEFAULT_CONFIG(44100)), - slot_config_(I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, + slot_config_(I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) { clock_config_.clk_src = I2S_CLK_SRC_PLL_160M; gpio_->set_pin(GpioExpander::AMP_EN, true); @@ -192,8 +192,8 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { bps == BPS_24 ? I2S_MCLK_MULTIPLE_384 : I2S_MCLK_MULTIPLE_256; ESP_ERROR_CHECK(i2s_channel_reconfig_std_clock(i2s_handle_, &clock_config_)); - WriteRegister(pcm512x::I2S_1, (0b11 << 4) | bps_bits); - WriteRegister(pcm512x::I2S_2, 0); + WriteRegister(pcm512x::I2S_1, bps_bits); + // WriteRegister(pcm512x::I2S_2, 0); // Configuration is all done, so we can now bring the DAC and I2S stream back // up. I2S first, since otherwise the DAC will see that there's no clocks and @@ -212,31 +212,35 @@ auto AudioDac::WriteData(const cpp::span& data) -> void { } } -IRAM_ATTR auto callback(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx) -> bool { +extern "C" IRAM_ATTR auto callback(i2s_chan_handle_t handle, + i2s_event_data_t* event, + void* user_ctx) -> bool { if (event == nullptr || user_ctx == nullptr) { return false; } if (event->data == nullptr || event->size == 0) { return false; } - StreamBufferHandle_t *src = reinterpret_cast(user_ctx); + uint8_t** buf = reinterpret_cast(event->data); + StreamBufferHandle_t src = reinterpret_cast(user_ctx); BaseType_t ret = false; - std::size_t bytes_received = xStreamBufferReceiveFromISR(*src, event->data, event->size, &ret); + std::size_t bytes_received = + xStreamBufferReceiveFromISR(src, *buf, event->size, &ret); if (bytes_received < event->size) { - // TODO(jacqueline): zero-pad. + memset(*buf + bytes_received, 0, event->size - bytes_received); } return ret; } -auto AudioDac::SetSource(StreamBufferHandle_t *buffer) -> void { +auto AudioDac::SetSource(StreamBufferHandle_t buffer) -> void { if (i2s_active_) { ESP_ERROR_CHECK(i2s_channel_disable(i2s_handle_)); } - i2s_event_callbacks_t callbacks { - .on_recv = NULL, - .on_recv_q_ovf = NULL, - .on_sent = NULL, - .on_send_q_ovf = NULL, + i2s_event_callbacks_t callbacks{ + .on_recv = NULL, + .on_recv_q_ovf = NULL, + .on_sent = NULL, + .on_send_q_ovf = NULL, }; if (buffer != nullptr) { callbacks.on_sent = &callback; -- cgit v1.2.3 From 2d95b637272f15ba2d74abc2371d708a08407d91 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 20 Apr 2023 14:47:32 +1000 Subject: Working without big distortion :) --- src/drivers/dac.cpp | 123 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 117 insertions(+), 6 deletions(-) (limited to 'src/drivers/dac.cpp') diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index dc9b51c8..9fdd7b94 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -33,6 +33,12 @@ auto AudioDac::create(GpioExpander* expander) i2s_chan_handle_t i2s_handle; i2s_chan_config_t channel_config = I2S_CHANNEL_DEFAULT_CONFIG(kI2SPort, I2S_ROLE_MASTER); + // Use the maximum possible DMA buffer size, since a smaller number of large + // copies is faster than a large number of small copies. + channel_config.dma_frame_num = 1024; + // Triple buffering should be enough to keep samples flowing smoothly. + // TODO(jacqueline): verify this with 192kHz 32bps. + channel_config.dma_desc_num = 8; // channel_config.auto_clear = true; ESP_ERROR_CHECK(i2s_new_channel(&channel_config, &i2s_handle, NULL)); @@ -47,8 +53,7 @@ auto AudioDac::create(GpioExpander* expander) i2s_std_config_t i2s_config = { .clk_cfg = dac->clock_config_, .slot_cfg = dac->slot_config_, - .gpio_cfg = {.mclk = GPIO_NUM_0, - //.mclk = I2S_GPIO_UNUSED, + .gpio_cfg = {.mclk = I2S_GPIO_UNUSED, .bclk = GPIO_NUM_26, .ws = GPIO_NUM_27, .dout = GPIO_NUM_5, @@ -57,7 +62,7 @@ auto AudioDac::create(GpioExpander* expander) { .mclk_inv = false, .bclk_inv = false, - .ws_inv = false, + .ws_inv = true, }}, }; @@ -115,7 +120,7 @@ AudioDac::AudioDac(GpioExpander* gpio, i2s_chan_handle_t i2s_handle) i2s_active_(false), active_page_(), clock_config_(I2S_STD_CLK_DEFAULT_CONFIG(44100)), - slot_config_(I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, + slot_config_(I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) { clock_config_.clk_src = I2S_CLK_SRC_PLL_160M; gpio_->set_pin(GpioExpander::AMP_EN, true); @@ -192,8 +197,114 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { bps == BPS_24 ? I2S_MCLK_MULTIPLE_384 : I2S_MCLK_MULTIPLE_256; ESP_ERROR_CHECK(i2s_channel_reconfig_std_clock(i2s_handle_, &clock_config_)); - WriteRegister(pcm512x::I2S_1, bps_bits); - // WriteRegister(pcm512x::I2S_2, 0); + // DAC reconfiguration. + //See here : https://e2e.ti.com/support/data_converters/audio_converters/f/64/t/428281 + // for a config example + + // Check that the bit clock (PLL input) is between 1MHz and 50MHz + uint32_t bckFreq = rate * bps * 2; + if (bckFreq < 1000000 || bckFreq > 50000000) { + ESP_LOGE(kTag, "bck freq out of range"); + return; + } + + // 24 bits is not supported for 44.1kHz and 48kHz. + if ((rate == SAMPLE_RATE_44_1 || rate == SAMPLE_RATE_48) && bps == BPS_24) { + // TODO(jacqueline): implement + ESP_LOGE(kTag, "sample rate and bps mismatch"); + return; + } + + //Initialize system clock from the I2S BCK input + WriteRegister(pcm512x::ERROR_DETECT, 0x1A); // Disable clock autoset and ignore SCK detection + WriteRegister(pcm512x::PLL_REF, 0x10); // Set PLL clock source to BCK + WriteRegister(pcm512x::DAC_REF, 0x10); // Set DAC clock source to PLL output + + //PLL configuration + int p, j, d, r; + + //Clock dividers + int nmac, ndac, ncp, dosr, idac; + + if (rate == SAMPLE_RATE_11_025 || rate == SAMPLE_RATE_22_05 || rate == SAMPLE_RATE_44_1) + { + //44.1kHz and derivatives. + //P = 1, R = 2, D = 0 for all supported combinations. + //Set J to have PLL clk = 90.3168 MHz + p = 1; + r = 2; + j = 90316800 / bckFreq / r; + d = 0; + + //Derive clocks from the 90.3168MHz PLL + nmac = 2; + ndac = 16; + ncp = 4; + dosr = 8; + idac = 1024; // DSP clock / sample rate + } + else + { + //8kHz and multiples. + //PLL config for a 98.304 MHz PLL clk + if (bps == BPS_24 && bckFreq > 1536000) { + p = 3; + } else if (bckFreq > 12288000) { + p = 2; + } else { + p = 1; + } + + r = 2; + j = 98304000 / (bckFreq / p) / r; + d = 0; + + //Derive clocks from the 98.304MHz PLL + switch (rate) { + case SAMPLE_RATE_16: nmac = 6; break; + case SAMPLE_RATE_32: nmac = 3; break; + default: nmac = 2; break; + } + + ndac = 16; + ncp = 4; + dosr = 384000 / rate; + idac = 98304000 / nmac / rate; // DSP clock / sample rate + } + + + // Configure PLL + WriteRegister(pcm512x::PLL_COEFF_0, p - 1); + WriteRegister(pcm512x::PLL_COEFF_1, j); + WriteRegister(pcm512x::PLL_COEFF_2, (d >> 8) & 0x3F); + WriteRegister(pcm512x::PLL_COEFF_3, d & 0xFF); + WriteRegister(pcm512x::PLL_COEFF_4, r - 1); + + // Clock dividers + WriteRegister(pcm512x::DSP_CLKDIV, nmac - 1); + WriteRegister(pcm512x::DAC_CLKDIV, ndac - 1); + WriteRegister(pcm512x::NCP_CLKDIV, ncp - 1); + WriteRegister(pcm512x::OSR_CLKDIV, dosr - 1); + + // IDAC (nb of DSP clock cycles per sample) + WriteRegister(pcm512x::IDAC_1, (idac >> 8) & 0xFF); + WriteRegister(pcm512x::IDAC_2, idac & 0xFF); + + // FS speed mode + int speedMode; + if (rate <= SAMPLE_RATE_48) { + speedMode = 0; + } else if (rate <= SAMPLE_RATE_96) { + speedMode = 1; + } else if (rate <= SAMPLE_RATE_192) { + speedMode = 2; + } else { + speedMode = 3; + } + WriteRegister(pcm512x::FS_SPEED_MODE, speedMode); + + WriteRegister(pcm512x::I2S_1, (0b11 << 4) | bps_bits); + WriteRegister(pcm512x::I2S_2, 0); // Configuration is all done, so we can now bring the DAC and I2S stream back // up. I2S first, since otherwise the DAC will see that there's no clocks and -- cgit v1.2.3 From 27c63ebb957aa5b942939aea33431ac50d101a26 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 20 Apr 2023 21:25:22 +1000 Subject: Switch to an MVP-ready 16bit three wire DAC setup --- src/drivers/dac.cpp | 76 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 31 deletions(-) (limited to 'src/drivers/dac.cpp') diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index 9fdd7b94..ac283600 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -38,7 +38,7 @@ auto AudioDac::create(GpioExpander* expander) channel_config.dma_frame_num = 1024; // Triple buffering should be enough to keep samples flowing smoothly. // TODO(jacqueline): verify this with 192kHz 32bps. - channel_config.dma_desc_num = 8; + channel_config.dma_desc_num = 4; // channel_config.auto_clear = true; ESP_ERROR_CHECK(i2s_new_channel(&channel_config, &i2s_handle, NULL)); @@ -53,7 +53,7 @@ auto AudioDac::create(GpioExpander* expander) i2s_std_config_t i2s_config = { .clk_cfg = dac->clock_config_, .slot_cfg = dac->slot_config_, - .gpio_cfg = {.mclk = I2S_GPIO_UNUSED, + .gpio_cfg = {.mclk = GPIO_NUM_0, .bclk = GPIO_NUM_26, .ws = GPIO_NUM_27, .dout = GPIO_NUM_5, @@ -121,7 +121,7 @@ AudioDac::AudioDac(GpioExpander* gpio, i2s_chan_handle_t i2s_handle) active_page_(), clock_config_(I2S_STD_CLK_DEFAULT_CONFIG(44100)), slot_config_(I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, - I2S_SLOT_MODE_STEREO)) { + I2S_SLOT_MODE_STEREO)) { clock_config_.clk_src = I2S_CLK_SRC_PLL_160M; gpio_->set_pin(GpioExpander::AMP_EN, true); gpio_->Write(); @@ -167,6 +167,8 @@ bool AudioDac::WaitForPowerState( auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { if (i2s_active_) { + WriteRegister(pcm512x::MUTE, 0b10001); + vTaskDelay(1); WriteRegister(pcm512x::POWER, 1 << 4); i2s_channel_disable(i2s_handle_); } @@ -198,10 +200,10 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { ESP_ERROR_CHECK(i2s_channel_reconfig_std_clock(i2s_handle_, &clock_config_)); // DAC reconfiguration. - //See here : https://e2e.ti.com/support/data_converters/audio_converters/f/64/t/428281 - // for a config example + // Inspired heavily by https://github.com/tommag/PCM51xx_Arduino (MIT). - // Check that the bit clock (PLL input) is between 1MHz and 50MHz + // Check that the bit clock (PLL input) is between 1MHz and 50MHz. It always + // should be. uint32_t bckFreq = rate * bps * 2; if (bckFreq < 1000000 || bckFreq > 50000000) { ESP_LOGE(kTag, "bck freq out of range"); @@ -210,43 +212,45 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { // 24 bits is not supported for 44.1kHz and 48kHz. if ((rate == SAMPLE_RATE_44_1 || rate == SAMPLE_RATE_48) && bps == BPS_24) { - // TODO(jacqueline): implement + // TODO(jacqueline): I think this *can* be implemented, but requires a bunch + // of maths. ESP_LOGE(kTag, "sample rate and bps mismatch"); return; } - //Initialize system clock from the I2S BCK input - WriteRegister(pcm512x::ERROR_DETECT, 0x1A); // Disable clock autoset and ignore SCK detection - WriteRegister(pcm512x::PLL_REF, 0x10); // Set PLL clock source to BCK - WriteRegister(pcm512x::DAC_REF, 0x10); // Set DAC clock source to PLL output + // Initialize system clock from the I2S BCK input + // Disable clock autoset and ignore SCK detection + WriteRegister(pcm512x::ERROR_DETECT, 0x1A); + // Set PLL clock source to BCK + WriteRegister(pcm512x::PLL_REF, 0x10); + // Set DAC clock source to PLL output + WriteRegister(pcm512x::DAC_REF, 0x10); - //PLL configuration + // PLL configuration int p, j, d, r; - //Clock dividers + // Clock dividers int nmac, ndac, ncp, dosr, idac; - if (rate == SAMPLE_RATE_11_025 || rate == SAMPLE_RATE_22_05 || rate == SAMPLE_RATE_44_1) - { - //44.1kHz and derivatives. - //P = 1, R = 2, D = 0 for all supported combinations. - //Set J to have PLL clk = 90.3168 MHz + if (rate == SAMPLE_RATE_11_025 || rate == SAMPLE_RATE_22_05 || + rate == SAMPLE_RATE_44_1) { + // 44.1kHz and derivatives. + // P = 1, R = 2, D = 0 for all supported combinations. + // Set J to have PLL clk = 90.3168 MHz p = 1; r = 2; j = 90316800 / bckFreq / r; d = 0; - //Derive clocks from the 90.3168MHz PLL + // Derive clocks from the 90.3168MHz PLL nmac = 2; ndac = 16; ncp = 4; dosr = 8; - idac = 1024; // DSP clock / sample rate - } - else - { - //8kHz and multiples. - //PLL config for a 98.304 MHz PLL clk + idac = 1024; // DSP clock / sample rate + } else { + // 8kHz and multiples. + // PLL config for a 98.304 MHz PLL clk if (bps == BPS_24 && bckFreq > 1536000) { p = 3; } else if (bckFreq > 12288000) { @@ -259,20 +263,25 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { j = 98304000 / (bckFreq / p) / r; d = 0; - //Derive clocks from the 98.304MHz PLL + // Derive clocks from the 98.304MHz PLL switch (rate) { - case SAMPLE_RATE_16: nmac = 6; break; - case SAMPLE_RATE_32: nmac = 3; break; - default: nmac = 2; break; + case SAMPLE_RATE_16: + nmac = 6; + break; + case SAMPLE_RATE_32: + nmac = 3; + break; + default: + nmac = 2; + break; } ndac = 16; ncp = 4; dosr = 384000 / rate; - idac = 98304000 / nmac / rate; // DSP clock / sample rate + idac = 98304000 / nmac / rate; // DSP clock / sample rate } - // Configure PLL WriteRegister(pcm512x::PLL_COEFF_0, p - 1); WriteRegister(pcm512x::PLL_COEFF_1, j); @@ -311,6 +320,11 @@ auto AudioDac::Reconfigure(BitsPerSample bps, SampleRate rate) -> void { // shut itself down. ESP_ERROR_CHECK(i2s_channel_enable(i2s_handle_)); WriteRegister(pcm512x::POWER, 0); + + if (i2s_active_) { + vTaskDelay(1); + WriteRegister(pcm512x::MUTE, 0); + } i2s_active_ = true; } -- cgit v1.2.3 From 7083459cf3c62c32d0c039a4665e702d70a27bba Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 21 Apr 2023 15:27:57 +1000 Subject: wrap driver instance ownership + di in a class --- src/drivers/dac.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/drivers/dac.cpp') diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index ac283600..e82f0d27 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -27,8 +27,7 @@ static const char* kTag = "AUDIODAC"; static const uint8_t kPcm5122Address = 0x4C; static const i2s_port_t kI2SPort = I2S_NUM_0; -auto AudioDac::create(GpioExpander* expander) - -> cpp::result, Error> { +auto AudioDac::create(GpioExpander* expander) -> cpp::result { // TODO: tune. i2s_chan_handle_t i2s_handle; i2s_chan_config_t channel_config = @@ -111,7 +110,7 @@ auto AudioDac::create(GpioExpander* expander) return state == RUN || state == STANDBY; }); - return dac; + return dac.release(); } AudioDac::AudioDac(GpioExpander* gpio, i2s_chan_handle_t i2s_handle) -- cgit v1.2.3