From a0798c7887731b7f04349cfdb36e031ace49df08 Mon Sep 17 00:00:00 2001 From: ailurux Date: Wed, 22 Feb 2023 11:51:26 +1100 Subject: Software rotation for display --- src/drivers/display.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/drivers') diff --git a/src/drivers/display.cpp b/src/drivers/display.cpp index 6ec82787..0ee02702 100644 --- a/src/drivers/display.cpp +++ b/src/drivers/display.cpp @@ -103,6 +103,9 @@ auto Display::create(GpioExpander* expander, display->driver_.draw_buf = &display->buffers_; display->driver_.hor_res = kDisplayWidth; display->driver_.ver_res = kDisplayHeight; + display->driver_.sw_rotate = 1; + display->driver_.rotated = LV_DISP_ROT_270; + display->driver_.antialiasing = 0; display->driver_.flush_cb = &FlushDataCallback; display->driver_.user_data = display.get(); -- cgit v1.2.3 From 0fce4fcc06a600aac6b0cb6fc1ef1ebb91bcdf27 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 8 Mar 2023 12:47:56 +1100 Subject: update to faceplate bodge --- src/drivers/display.cpp | 10 +++++++++- src/drivers/include/gpio_expander.hpp | 12 ++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/display.cpp b/src/drivers/display.cpp index 6ec82787..d3c2c923 100644 --- a/src/drivers/display.cpp +++ b/src/drivers/display.cpp @@ -60,7 +60,7 @@ auto Display::create(GpioExpander* expander, const displays::InitialisationData& init_data) -> std::unique_ptr { // First, turn on the LED backlight. - expander->set_pin(GpioExpander::DISPLAY_LED, 0); + expander->set_pin(GpioExpander::DISPLAY_LED, 1); expander->set_pin(GpioExpander::DISPLAY_POWER_ENABLE, 1); expander->Write(); @@ -118,6 +118,14 @@ Display::Display(GpioExpander* gpio, spi_device_handle_t handle) Display::~Display() {} void Display::SendInitialisationSequence(const uint8_t* data) { + // Reset the display manually to get it into a predictable state. + gpio_->set_pin(GpioExpander::DISPLAY_RESET, false); + gpio_->Write(); + vTaskDelay(pdMS_TO_TICKS(10)); + gpio_->set_pin(GpioExpander::DISPLAY_RESET, false); + gpio_->Write(); + vTaskDelay(pdMS_TO_TICKS(10)); + // Hold onto the bus for the entire sequence so that we're not interrupted // part way through. spi_device_acquire_bus(handle_, portMAX_DELAY); diff --git a/src/drivers/include/gpio_expander.hpp b/src/drivers/include/gpio_expander.hpp index a6e96d87..d03c26b0 100644 --- a/src/drivers/include/gpio_expander.hpp +++ b/src/drivers/include/gpio_expander.hpp @@ -49,8 +49,8 @@ class GpioExpander { // Port B: // 0 - 3.5mm jack detect (active low) // 1 - unused - // 2 - volume up - // 3 - volume down + // 2 - trackpad int + // 3 - display reset (active low) // 4 - lock switch // 5 - touchpad interupt // 6 - display DR @@ -111,10 +111,10 @@ class GpioExpander { // Port B PHONE_DETECT = 8, // Active-high input // UNUSED = 9, - VOL_UP = 10, - VOL_DOWN = 11, - LOCK = 12, - TOUCHPAD_INT = 13, + TOUCHPAD_INT = 10, + DISPLAY_RESET = 11, + // UNUSED = 12, + // UNUSED = 13, DISPLAY_DR = 14, DISPLAY_LED = 15, }; -- cgit v1.2.3 From b9a75cd55a11fd404a1977539acb64a6705f3809 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 10 Mar 2023 12:26:32 +1100 Subject: LDO up at boot --- src/drivers/include/gpio_expander.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/drivers') diff --git a/src/drivers/include/gpio_expander.hpp b/src/drivers/include/gpio_expander.hpp index d03c26b0..cb087df9 100644 --- a/src/drivers/include/gpio_expander.hpp +++ b/src/drivers/include/gpio_expander.hpp @@ -44,7 +44,7 @@ class GpioExpander { // 6 - LDO enable // 7 - charge power ok (active low) // All power switches low, sd mux pointing away from us, inputs high. - static const uint8_t kPortADefault = 0b10000010; + static const uint8_t kPortADefault = 0b11000010; // Port B: // 0 - 3.5mm jack detect (active low) -- cgit v1.2.3 From 78ec09c494faadf9e7d06dc7d3e04531c3a34ff7 Mon Sep 17 00:00:00 2001 From: ailurux Date: Mon, 13 Mar 2023 15:14:32 +1100 Subject: Touchwheel test --- src/drivers/CMakeLists.txt | 2 +- src/drivers/i2c.cpp | 8 +++- src/drivers/include/i2c.hpp | 2 +- src/drivers/include/touchwheel.hpp | 60 +++++++++++++++++++++++++ src/drivers/touchwheel.cpp | 92 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 src/drivers/include/touchwheel.hpp create mode 100644 src/drivers/touchwheel.cpp (limited to 'src/drivers') diff --git a/src/drivers/CMakeLists.txt b/src/drivers/CMakeLists.txt index c4e4c172..bf8f0c4e 100644 --- a/src/drivers/CMakeLists.txt +++ b/src/drivers/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( - SRCS "dac.cpp" "gpio_expander.cpp" "battery.cpp" "storage.cpp" "i2c.cpp" + SRCS "touchwheel.cpp" "dac.cpp" "gpio_expander.cpp" "battery.cpp" "storage.cpp" "i2c.cpp" "spi.cpp" "display.cpp" "display_init.cpp" INCLUDE_DIRS "include" REQUIRES "esp_adc" "fatfs" "result" "lvgl" "span") diff --git a/src/drivers/i2c.cpp b/src/drivers/i2c.cpp index 04a6d7d1..a66f54f0 100644 --- a/src/drivers/i2c.cpp +++ b/src/drivers/i2c.cpp @@ -36,6 +36,10 @@ esp_err_t init_i2c(void) { if (esp_err_t err = i2c_driver_install(kI2CPort, config.mode, 0, 0, 0)) { return err; } + if (esp_err_t err = i2c_set_timeout(kI2CPort, 400000)) { + return err; + } + // TODO: INT line @@ -57,8 +61,8 @@ I2CTransaction::~I2CTransaction() { free(buffer_); } -esp_err_t I2CTransaction::Execute() { - return i2c_master_cmd_begin(I2C_NUM_0, handle_, kI2CTimeout); +esp_err_t I2CTransaction::Execute(uint8_t port) { + return i2c_master_cmd_begin(port, handle_, kI2CTimeout); } I2CTransaction& I2CTransaction::start() { diff --git a/src/drivers/include/i2c.hpp b/src/drivers/include/i2c.hpp index dbdd8a11..811c9333 100644 --- a/src/drivers/include/i2c.hpp +++ b/src/drivers/include/i2c.hpp @@ -35,7 +35,7 @@ class I2CTransaction { * ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode. * ESP_ERR_TIMEOUT Operation timeout because the bus is busy. */ - esp_err_t Execute(); + esp_err_t Execute(uint8_t port = I2C_NUM_0); /* * Enqueues a start condition. May also be used for repeated start diff --git a/src/drivers/include/touchwheel.hpp b/src/drivers/include/touchwheel.hpp new file mode 100644 index 00000000..14215acd --- /dev/null +++ b/src/drivers/include/touchwheel.hpp @@ -0,0 +1,60 @@ +#pragma once + + +#include +#include + +#include "esp_err.h" +#include "result.hpp" + +#include "gpio_expander.hpp" + +namespace drivers { + +struct TouchWheelData { + bool is_touched = false; + uint8_t wheel_position = -1; +}; + +class TouchWheel { + public: + enum Error { + FAILED_TO_BOOT, + FAILED_TO_CONFIGURE, + }; + static auto create(GpioExpander* expander) + -> cpp::result, Error>; + + TouchWheel(GpioExpander* gpio); + ~TouchWheel(); + + // Not copyable or movable. + TouchWheel(const TouchWheel&) = delete; + TouchWheel& operator=(const TouchWheel&) = delete; + + auto Update() -> void; + auto GetTouchWheelData() const -> TouchWheelData; + + private: + GpioExpander* gpio_; + TouchWheelData data_; + + enum Register { + FIRMWARE_VERSION = 0x1, + DETECTION_STATUS = 0x2, + KEY_STATUS_A = 0x3, + KEY_STATUS_B = 0x4, + SLIDER_POSITION = 0x5, + CALIBRATE = 0x6, + RESET = 0x7, + LOW_POWER = 0x8, + SLIDER_OPTIONS = 0x14, + }; + + + void WriteRegister(uint8_t reg, uint8_t val); + void ReadRegister(uint8_t reg, uint8_t* data, uint8_t count); + +}; + +} // namespace drivers diff --git a/src/drivers/touchwheel.cpp b/src/drivers/touchwheel.cpp new file mode 100644 index 00000000..11a115fb --- /dev/null +++ b/src/drivers/touchwheel.cpp @@ -0,0 +1,92 @@ +#include "touchwheel.hpp" + +#include + +#include "assert.h" +#include "driver/i2c.h" +#include "esp_err.h" +#include "esp_log.h" +#include "hal/i2c_types.h" + +#include "i2c.hpp" + +namespace drivers { + +static const char* kTag = "TOUCHWHEEL"; +static const uint8_t kTouchWheelAddress = 0x1C; + +static const uint8_t kWriteMask = 0x80; +static const uint8_t kReadMask = 0xA0; + +double normalise(uint16_t min, uint16_t max, uint16_t value) { + if (value >= max) { + return 1.0; + } + if (value <= min) { + return 0.0; + } + uint16_t range = max - min; + return (double)(value - min) / range; +} + + +auto TouchWheel::create(GpioExpander* expander) + -> cpp::result, Error> { + std::unique_ptr wheel = std::make_unique(expander); + wheel->WriteRegister(Register::SLIDER_OPTIONS, 0xC0); + return wheel; +} + +TouchWheel::TouchWheel(GpioExpander* gpio) { + this->gpio_ = gpio; +}; + +TouchWheel::~TouchWheel(){ +}; + +void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) { + // uint8_t maskedReg = reg | kWriteMask; + uint8_t maskedReg = reg; + I2CTransaction transaction; + transaction.start() + .write_addr(kTouchWheelAddress, I2C_MASTER_WRITE) + .write_ack(maskedReg, val) + .stop(); + ESP_ERROR_CHECK(transaction.Execute()); +} + +void TouchWheel::ReadRegister(uint8_t reg, uint8_t* data, uint8_t count) { + // uint8_t maskedReg = reg | kReadMask; + uint8_t maskedReg = reg; + + if (count <= 0) { + return; + } + + I2CTransaction transaction; + transaction.start() + .write_addr(kTouchWheelAddress, I2C_MASTER_WRITE) + .write_ack(maskedReg) + .stop() + .start() + .write_addr(kTouchWheelAddress, I2C_MASTER_READ) + .read(data, I2C_MASTER_NACK) + .stop(); + + // TODO: Handle errors here. + ESP_ERROR_CHECK(transaction.Execute()); +} + +void TouchWheel::Update() { + // Read data from device into member struct + uint8_t position; + this->ReadRegister(Register::SLIDER_POSITION, &position, 1); + data_.wheel_position = position; +} + +TouchWheelData TouchWheel::GetTouchWheelData() const { + return data_; +} + + +} // namespace drivers -- cgit v1.2.3 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/battery.cpp | 4 +- src/drivers/dac.cpp | 4 +- src/drivers/display.cpp | 20 ++++++---- src/drivers/i2c.cpp | 4 +- src/drivers/include/gpio_expander.hpp | 70 +++++++++++++++++------------------ 5 files changed, 53 insertions(+), 49 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/battery.cpp b/src/drivers/battery.cpp index 00e7796a..8d747c07 100644 --- a/src/drivers/battery.cpp +++ b/src/drivers/battery.cpp @@ -13,8 +13,8 @@ static const adc_unit_t kAdcUnit = ADC_UNIT_1; // Max battery voltage should be a little over 2V due to our divider, so we need // the max attenuation to properly handle the full range. static const adc_atten_t kAdcAttenuation = ADC_ATTEN_DB_11; -// Corresponds to GPIO 34. -static const adc_channel_t kAdcChannel = ADC_CHANNEL_6; +// Corresponds to SENSOR_VP. +static const adc_channel_t kAdcChannel = ADC_CHANNEL_0; Battery::Battery() { adc_oneshot_unit_init_cfg_t unit_config = { 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(); } diff --git a/src/drivers/display.cpp b/src/drivers/display.cpp index e8ddb65e..d5c17744 100644 --- a/src/drivers/display.cpp +++ b/src/drivers/display.cpp @@ -28,6 +28,9 @@ static const uint8_t kDisplayWidth = 128 + 2; static const uint8_t kDisplayHeight = 160 + 1; static const uint8_t kTransactionQueueSize = 10; +static const gpio_num_t kDisplayDr = GPIO_NUM_33; +static const gpio_num_t kDisplayLedEn = GPIO_NUM_32; + /* * The size of each of our two display buffers. This is fundamentally a balance * between performance and memory usage. LVGL docs recommend a buffer 1/10th the @@ -59,10 +62,13 @@ extern "C" void FlushDataCallback(lv_disp_drv_t* disp_drv, auto Display::create(GpioExpander* expander, const displays::InitialisationData& init_data) -> std::unique_ptr { - // First, turn on the LED backlight. - expander->set_pin(GpioExpander::DISPLAY_LED, 1); - expander->set_pin(GpioExpander::DISPLAY_POWER_ENABLE, 1); - expander->Write(); + ESP_LOGI(kTag, "Init I/O pins"); + gpio_set_direction(kDisplayDr, GPIO_MODE_OUTPUT); + gpio_set_level(kDisplayDr, 0); + + // TODO: use pwm for the backlight. + gpio_set_direction(kDisplayLedEn, GPIO_MODE_OUTPUT); + gpio_set_level(kDisplayLedEn, 1); // Next, init the SPI device spi_device_interface_config_t spi_cfg = { @@ -122,7 +128,7 @@ Display::~Display() {} void Display::SendInitialisationSequence(const uint8_t* data) { // Reset the display manually to get it into a predictable state. - gpio_->set_pin(GpioExpander::DISPLAY_RESET, false); + gpio_->set_pin(GpioExpander::DISPLAY_RESET, true); gpio_->Write(); vTaskDelay(pdMS_TO_TICKS(10)); gpio_->set_pin(GpioExpander::DISPLAY_RESET, false); @@ -201,9 +207,7 @@ void Display::SendTransaction(TransactionType type, transaction.tx_buffer = data; } - // TODO(jacqueline): Move this to an on-board GPIO for speed. - gpio_->set_pin(GpioExpander::DISPLAY_DR, type); - gpio_->Write(); + gpio_set_level(kDisplayDr, type); // TODO(jacqueline): Handle these errors. esp_err_t ret = spi_device_polling_transmit(handle_, &transaction); diff --git a/src/drivers/i2c.cpp b/src/drivers/i2c.cpp index a66f54f0..3773055d 100644 --- a/src/drivers/i2c.cpp +++ b/src/drivers/i2c.cpp @@ -9,8 +9,8 @@ namespace drivers { static const i2c_port_t kI2CPort = I2C_NUM_0; -static const gpio_num_t kI2CSdaPin = GPIO_NUM_2; -static const gpio_num_t kI2CSclPin = GPIO_NUM_4; +static const gpio_num_t kI2CSdaPin = GPIO_NUM_4; +static const gpio_num_t kI2CSclPin = GPIO_NUM_2; static const uint32_t kI2CClkSpeed = 400'000; static constexpr int kCmdLinkSize = I2C_LINK_RECOMMENDED_SIZE(12); diff --git a/src/drivers/include/gpio_expander.hpp b/src/drivers/include/gpio_expander.hpp index cb087df9..86998e7e 100644 --- a/src/drivers/include/gpio_expander.hpp +++ b/src/drivers/include/gpio_expander.hpp @@ -35,28 +35,28 @@ class GpioExpander { static const uint8_t kPca8575Timeout = pdMS_TO_TICKS(100); // Port A: - // 0 - audio power enable - // 1 - usb interface power enable (active low) - // 2 - display power enable - // 3 - touchpad power enable - // 4 - sd card power enable - // 5 - sd mux switch - // 6 - LDO enable - // 7 - charge power ok (active low) - // All power switches low, sd mux pointing away from us, inputs high. - static const uint8_t kPortADefault = 0b11000010; + // 0 - sd card mux switch + // 1 - sd card mux enable (active low) + // 2 - key up + // 3 - key down + // 4 - key lock + // 5 - display reset + // 6 - NC + // 7 - sd card power + // Default to SD card off, inputs high. + static const uint8_t kPortADefault = 0b00011110; // Port B: - // 0 - 3.5mm jack detect (active low) - // 1 - unused - // 2 - trackpad int - // 3 - display reset (active low) - // 4 - lock switch - // 5 - touchpad interupt - // 6 - display DR - // 7 - display LED - // Inputs all high, all others low. - static const uint8_t kPortBDefault = 0b00111101; + // 0 - trs output enable + // 1 - 3.5mm jack detect (active low) + // 2 - NC + // 3 - NC + // 4 - NC + // 5 - NC + // 6 - NC + // 7 - NC + // Default input high, trs output low + static const uint8_t kPortBDefault = 0b00000010; /* * Convenience mehod for packing the port a and b bytes into a single 16 bit @@ -99,30 +99,30 @@ class GpioExpander { /* Maps each pin of the expander to its number in a `pack`ed uint16. */ enum Pin { // Port A - AUDIO_POWER_ENABLE = 0, - USB_INTERFACE_POWER_ENABLE = 1, - DISPLAY_POWER_ENABLE = 2, - TOUCHPAD_POWER_ENABLE = 3, - SD_CARD_POWER_ENABLE = 4, - SD_MUX_SWITCH = 5, - LDO_ENABLE = 6, - CHARGE_POWER_OK = 7, // Active-low input + SD_MUX_SWITCH = 0, + SD_MUX_EN_ACTIVE_LOW = 1, + KEY_UP = 2, + KEY_DOWN = 3, + KEY_LOCK = 4, + DISPLAY_RESET = 5, + // UNUSED = 6, + SD_CARD_POWER_ENABLE = 7, // Port B - PHONE_DETECT = 8, // Active-high input - // UNUSED = 9, - TOUCHPAD_INT = 10, - DISPLAY_RESET = 11, + AMP_EN = 8, + PHONE_DETECT = 9, + // UNUSED = 10, + // UNUSED = 11, // UNUSED = 12, // UNUSED = 13, - DISPLAY_DR = 14, - DISPLAY_LED = 15, + // UNUSED = 14, + // UNUSED = 15, }; /* Nicer value names for use with the SD_MUX_SWITCH pin. */ enum SdController { SD_MUX_ESP = 1, - SD_MUX_USB = 0, + SD_MUX_SAMD = 0, }; /** -- cgit v1.2.3 From 9799ab458d8ad870aaec048abf5b1b1d93aa7aff Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 30 Mar 2023 10:15:09 +1100 Subject: fix sd card on for dev + mux pin --- src/drivers/include/gpio_expander.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/include/gpio_expander.hpp b/src/drivers/include/gpio_expander.hpp index 86998e7e..d53a1982 100644 --- a/src/drivers/include/gpio_expander.hpp +++ b/src/drivers/include/gpio_expander.hpp @@ -42,9 +42,9 @@ class GpioExpander { // 4 - key lock // 5 - display reset // 6 - NC - // 7 - sd card power + // 7 - sd card power (active low) // Default to SD card off, inputs high. - static const uint8_t kPortADefault = 0b00011110; + static const uint8_t kPortADefault = 0b10011110; // Port B: // 0 - trs output enable @@ -121,8 +121,8 @@ class GpioExpander { /* Nicer value names for use with the SD_MUX_SWITCH pin. */ enum SdController { - SD_MUX_ESP = 1, - SD_MUX_SAMD = 0, + SD_MUX_ESP = 0, + SD_MUX_SAMD = 1, }; /** -- cgit v1.2.3 From 1b245316fe282035f094a3656f717d419e8bd526 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 4 Apr 2023 14:48:35 +1000 Subject: fix up touchpad timeouts, make it less chatty --- src/drivers/i2c.cpp | 1 - src/drivers/include/touchwheel.hpp | 33 ++++++---------- src/drivers/touchwheel.cpp | 79 +++++++++++++++++--------------------- 3 files changed, 47 insertions(+), 66 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/i2c.cpp b/src/drivers/i2c.cpp index 3773055d..6c6fc407 100644 --- a/src/drivers/i2c.cpp +++ b/src/drivers/i2c.cpp @@ -40,7 +40,6 @@ esp_err_t init_i2c(void) { return err; } - // TODO: INT line return ESP_OK; diff --git a/src/drivers/include/touchwheel.hpp b/src/drivers/include/touchwheel.hpp index 14215acd..3dfa182b 100644 --- a/src/drivers/include/touchwheel.hpp +++ b/src/drivers/include/touchwheel.hpp @@ -1,8 +1,7 @@ #pragma once - -#include #include +#include #include "esp_err.h" #include "result.hpp" @@ -18,14 +17,7 @@ struct TouchWheelData { class TouchWheel { public: - enum Error { - FAILED_TO_BOOT, - FAILED_TO_CONFIGURE, - }; - static auto create(GpioExpander* expander) - -> cpp::result, Error>; - - TouchWheel(GpioExpander* gpio); + TouchWheel(); ~TouchWheel(); // Not copyable or movable. @@ -36,25 +28,22 @@ class TouchWheel { auto GetTouchWheelData() const -> TouchWheelData; private: - GpioExpander* gpio_; TouchWheelData data_; - + enum Register { FIRMWARE_VERSION = 0x1, DETECTION_STATUS = 0x2, - KEY_STATUS_A = 0x3, - KEY_STATUS_B = 0x4, - SLIDER_POSITION = 0x5, - CALIBRATE = 0x6, - RESET = 0x7, - LOW_POWER = 0x8, - SLIDER_OPTIONS = 0x14, + KEY_STATUS_A = 0x3, + KEY_STATUS_B = 0x4, + SLIDER_POSITION = 0x5, + CALIBRATE = 0x6, + RESET = 0x7, + LOW_POWER = 0x8, + SLIDER_OPTIONS = 0x14, }; - void WriteRegister(uint8_t reg, uint8_t val); - void ReadRegister(uint8_t reg, uint8_t* data, uint8_t count); - + uint8_t ReadRegister(uint8_t reg); }; } // namespace drivers diff --git a/src/drivers/touchwheel.cpp b/src/drivers/touchwheel.cpp index 11a115fb..9e0d99af 100644 --- a/src/drivers/touchwheel.cpp +++ b/src/drivers/touchwheel.cpp @@ -1,11 +1,15 @@ #include "touchwheel.hpp" +#include #include #include "assert.h" +#include "driver/gpio.h" #include "driver/i2c.h" #include "esp_err.h" #include "esp_log.h" +#include "freertos/projdefs.h" +#include "hal/gpio_types.h" #include "hal/i2c_types.h" #include "i2c.hpp" @@ -15,34 +19,18 @@ namespace drivers { static const char* kTag = "TOUCHWHEEL"; static const uint8_t kTouchWheelAddress = 0x1C; -static const uint8_t kWriteMask = 0x80; -static const uint8_t kReadMask = 0xA0; +TouchWheel::TouchWheel() { + gpio_set_direction(GPIO_NUM_25, GPIO_MODE_INPUT); + gpio_set_pull_mode(GPIO_NUM_25, GPIO_PULLUP_ONLY); -double normalise(uint16_t min, uint16_t max, uint16_t value) { - if (value >= max) { - return 1.0; - } - if (value <= min) { - return 0.0; - } - uint16_t range = max - min; - return (double)(value - min) / range; -} - - -auto TouchWheel::create(GpioExpander* expander) - -> cpp::result, Error> { - std::unique_ptr wheel = std::make_unique(expander); - wheel->WriteRegister(Register::SLIDER_OPTIONS, 0xC0); - return wheel; + WriteRegister(Register::RESET, 1); + // TODO(daniel): do we need this? how long does reset take? + vTaskDelay(pdMS_TO_TICKS(1)); + WriteRegister(Register::SLIDER_OPTIONS, 0b11000000); + WriteRegister(Register::CALIBRATE, 1); } -TouchWheel::TouchWheel(GpioExpander* gpio) { - this->gpio_ = gpio; -}; - -TouchWheel::~TouchWheel(){ -}; +TouchWheel::~TouchWheel() {} void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) { // uint8_t maskedReg = reg | kWriteMask; @@ -55,38 +43,43 @@ void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) { ESP_ERROR_CHECK(transaction.Execute()); } -void TouchWheel::ReadRegister(uint8_t reg, uint8_t* data, uint8_t count) { - // uint8_t maskedReg = reg | kReadMask; - uint8_t maskedReg = reg; - - if (count <= 0) { - return; - } - +uint8_t TouchWheel::ReadRegister(uint8_t reg) { + uint8_t res; I2CTransaction transaction; transaction.start() .write_addr(kTouchWheelAddress, I2C_MASTER_WRITE) - .write_ack(maskedReg) - .stop() + .write_ack(reg) .start() .write_addr(kTouchWheelAddress, I2C_MASTER_READ) - .read(data, I2C_MASTER_NACK) + .read(&res, I2C_MASTER_NACK) .stop(); - - // TODO: Handle errors here. ESP_ERROR_CHECK(transaction.Execute()); + return res; } void TouchWheel::Update() { - // Read data from device into member struct - uint8_t position; - this->ReadRegister(Register::SLIDER_POSITION, &position, 1); - data_.wheel_position = position; + // Read data from device into member struct + bool has_data = !gpio_get_level(GPIO_NUM_25); + if (!has_data) { + return; + } + uint8_t status = ReadRegister(Register::DETECTION_STATUS); + if (status & 0b10000000) { + // Still calibrating. + return; + } + if (status & 0b10) { + // Slider detect. + data_.wheel_position = ReadRegister(Register::SLIDER_POSITION); + } + if (status & 0b1) { + // Key detect. + // TODO(daniel): implement me + } } TouchWheelData TouchWheel::GetTouchWheelData() const { return data_; } - } // namespace drivers -- cgit v1.2.3 From c93ed8efad85996e8ec3cec68bdc8feeae71b8fc Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 6 Apr 2023 16:18:04 +1000 Subject: fix some display issues --- src/drivers/display.cpp | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/display.cpp b/src/drivers/display.cpp index d5c17744..b981ec33 100644 --- a/src/drivers/display.cpp +++ b/src/drivers/display.cpp @@ -30,6 +30,7 @@ static const uint8_t kTransactionQueueSize = 10; static const gpio_num_t kDisplayDr = GPIO_NUM_33; static const gpio_num_t kDisplayLedEn = GPIO_NUM_32; +static const gpio_num_t kDisplayCs = GPIO_NUM_22; /* * The size of each of our two display buffers. This is fundamentally a balance @@ -63,11 +64,37 @@ auto Display::create(GpioExpander* expander, const displays::InitialisationData& init_data) -> std::unique_ptr { ESP_LOGI(kTag, "Init I/O pins"); - gpio_set_direction(kDisplayDr, GPIO_MODE_OUTPUT); + gpio_config_t dr_config{ + .pin_bit_mask = 1ULL << kDisplayDr, + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE, + }; + gpio_config(&dr_config); gpio_set_level(kDisplayDr, 0); + /* + gpio_config_t cs_config{ + .pin_bit_mask = 1ULL << kDisplayCs, + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE, + }; + gpio_config(&cs_config); + gpio_set_level(kDisplayCs, 1); + */ + // TODO: use pwm for the backlight. - gpio_set_direction(kDisplayLedEn, GPIO_MODE_OUTPUT); + gpio_config_t led_config{ + .pin_bit_mask = 1ULL << kDisplayLedEn, + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_ENABLE, + .intr_type = GPIO_INTR_DISABLE, + }; + gpio_config(&led_config); gpio_set_level(kDisplayLedEn, 1); // Next, init the SPI device @@ -82,7 +109,7 @@ auto Display::create(GpioExpander* expander, .cs_ena_posttrans = 0, .clock_speed_hz = SPI_MASTER_FREQ_40M, .input_delay_ns = 0, - .spics_io_num = GPIO_NUM_22, + .spics_io_num = kDisplayCs, .flags = 0, .queue_size = kTransactionQueueSize, .pre_cb = NULL, @@ -128,16 +155,17 @@ Display::~Display() {} void Display::SendInitialisationSequence(const uint8_t* data) { // Reset the display manually to get it into a predictable state. - gpio_->set_pin(GpioExpander::DISPLAY_RESET, true); - gpio_->Write(); - vTaskDelay(pdMS_TO_TICKS(10)); gpio_->set_pin(GpioExpander::DISPLAY_RESET, false); gpio_->Write(); - vTaskDelay(pdMS_TO_TICKS(10)); + vTaskDelay(pdMS_TO_TICKS(1)); + gpio_->set_pin(GpioExpander::DISPLAY_RESET, true); + gpio_->Write(); + vTaskDelay(pdMS_TO_TICKS(1)); // Hold onto the bus for the entire sequence so that we're not interrupted // part way through. spi_device_acquire_bus(handle_, portMAX_DELAY); + // gpio_set_level(kDisplayCs, 0); // First byte of the data is the number of commands. for (int i = *(data++); i > 0; i--) { @@ -156,12 +184,15 @@ void Display::SendInitialisationSequence(const uint8_t* data) { } // Avoid hanging on to the bus whilst delaying. + // gpio_set_level(kDisplayCs, 1); spi_device_release_bus(handle_); vTaskDelay(pdMS_TO_TICKS(sleep_duration_ms)); spi_device_acquire_bus(handle_, portMAX_DELAY); + // gpio_set_level(kDisplayCs, 0); } } + // gpio_set_level(kDisplayCs, 1); spi_device_release_bus(handle_); } @@ -220,6 +251,7 @@ void Display::OnLvglFlush(lv_disp_drv_t* disp_drv, // Ideally we want to complete a single flush as quickly as possible, so grab // the bus for this entire transaction sequence. spi_device_acquire_bus(handle_, portMAX_DELAY); + // gpio_set_level(kDisplayCs, 0); // First we need to specify the rectangle of the display we're writing into. uint16_t data[2] = {0, 0}; @@ -239,6 +271,7 @@ void Display::OnLvglFlush(lv_disp_drv_t* disp_drv, SendCommandWithData(displays::ST77XX_RAMWR, reinterpret_cast(color_map), size * 2); + // gpio_set_level(kDisplayCs, 1); spi_device_release_bus(handle_); lv_disp_flush_ready(&driver_); -- cgit v1.2.3 From a1cef17c5bb66c453e4a8e83e52a56fe9173346c Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 12 Apr 2023 16:37:27 +1000 Subject: Leave the display reset pin alone; we don't need it --- src/drivers/display.cpp | 31 +++---------------------------- src/drivers/include/gpio_expander.hpp | 2 +- src/drivers/touchwheel.cpp | 11 +++++++++-- 3 files changed, 13 insertions(+), 31 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/display.cpp b/src/drivers/display.cpp index b981ec33..56bd6e60 100644 --- a/src/drivers/display.cpp +++ b/src/drivers/display.cpp @@ -20,6 +20,7 @@ #include "display_init.hpp" #include "gpio_expander.hpp" +#include "soc/soc.h" static const char* kTag = "DISPLAY"; @@ -67,31 +68,19 @@ auto Display::create(GpioExpander* expander, gpio_config_t dr_config{ .pin_bit_mask = 1ULL << kDisplayDr, .mode = GPIO_MODE_OUTPUT, - .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_up_en = GPIO_PULLUP_ENABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE, .intr_type = GPIO_INTR_DISABLE, }; gpio_config(&dr_config); gpio_set_level(kDisplayDr, 0); - /* - gpio_config_t cs_config{ - .pin_bit_mask = 1ULL << kDisplayCs, - .mode = GPIO_MODE_OUTPUT, - .pull_up_en = GPIO_PULLUP_DISABLE, - .pull_down_en = GPIO_PULLDOWN_DISABLE, - .intr_type = GPIO_INTR_DISABLE, - }; - gpio_config(&cs_config); - gpio_set_level(kDisplayCs, 1); - */ - // TODO: use pwm for the backlight. gpio_config_t led_config{ .pin_bit_mask = 1ULL << kDisplayLedEn, .mode = GPIO_MODE_OUTPUT, .pull_up_en = GPIO_PULLUP_ENABLE, - .pull_down_en = GPIO_PULLDOWN_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, .intr_type = GPIO_INTR_DISABLE, }; gpio_config(&led_config); @@ -154,18 +143,9 @@ Display::Display(GpioExpander* gpio, spi_device_handle_t handle) Display::~Display() {} void Display::SendInitialisationSequence(const uint8_t* data) { - // Reset the display manually to get it into a predictable state. - gpio_->set_pin(GpioExpander::DISPLAY_RESET, false); - gpio_->Write(); - vTaskDelay(pdMS_TO_TICKS(1)); - gpio_->set_pin(GpioExpander::DISPLAY_RESET, true); - gpio_->Write(); - vTaskDelay(pdMS_TO_TICKS(1)); - // Hold onto the bus for the entire sequence so that we're not interrupted // part way through. spi_device_acquire_bus(handle_, portMAX_DELAY); - // gpio_set_level(kDisplayCs, 0); // First byte of the data is the number of commands. for (int i = *(data++); i > 0; i--) { @@ -184,15 +164,12 @@ void Display::SendInitialisationSequence(const uint8_t* data) { } // Avoid hanging on to the bus whilst delaying. - // gpio_set_level(kDisplayCs, 1); spi_device_release_bus(handle_); vTaskDelay(pdMS_TO_TICKS(sleep_duration_ms)); spi_device_acquire_bus(handle_, portMAX_DELAY); - // gpio_set_level(kDisplayCs, 0); } } - // gpio_set_level(kDisplayCs, 1); spi_device_release_bus(handle_); } @@ -251,7 +228,6 @@ void Display::OnLvglFlush(lv_disp_drv_t* disp_drv, // Ideally we want to complete a single flush as quickly as possible, so grab // the bus for this entire transaction sequence. spi_device_acquire_bus(handle_, portMAX_DELAY); - // gpio_set_level(kDisplayCs, 0); // First we need to specify the rectangle of the display we're writing into. uint16_t data[2] = {0, 0}; @@ -271,7 +247,6 @@ void Display::OnLvglFlush(lv_disp_drv_t* disp_drv, SendCommandWithData(displays::ST77XX_RAMWR, reinterpret_cast(color_map), size * 2); - // gpio_set_level(kDisplayCs, 1); spi_device_release_bus(handle_); lv_disp_flush_ready(&driver_); diff --git a/src/drivers/include/gpio_expander.hpp b/src/drivers/include/gpio_expander.hpp index d53a1982..cd3719a0 100644 --- a/src/drivers/include/gpio_expander.hpp +++ b/src/drivers/include/gpio_expander.hpp @@ -44,7 +44,7 @@ class GpioExpander { // 6 - NC // 7 - sd card power (active low) // Default to SD card off, inputs high. - static const uint8_t kPortADefault = 0b10011110; + static const uint8_t kPortADefault = 0b10111110; // Port B: // 0 - trs output enable diff --git a/src/drivers/touchwheel.cpp b/src/drivers/touchwheel.cpp index 9e0d99af..d5382b3d 100644 --- a/src/drivers/touchwheel.cpp +++ b/src/drivers/touchwheel.cpp @@ -18,10 +18,17 @@ namespace drivers { static const char* kTag = "TOUCHWHEEL"; static const uint8_t kTouchWheelAddress = 0x1C; +static const gpio_num_t kIntPin = GPIO_NUM_25; TouchWheel::TouchWheel() { - gpio_set_direction(GPIO_NUM_25, GPIO_MODE_INPUT); - gpio_set_pull_mode(GPIO_NUM_25, GPIO_PULLUP_ONLY); + gpio_config_t int_config{ + .pin_bit_mask = 1ULL << kIntPin, + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE, + }; + gpio_config(&int_config); WriteRegister(Register::RESET, 1); // TODO(daniel): do we need this? how long does reset take? -- cgit v1.2.3 From 2a46eecdc6334c31cee2b40427d2536b48cbb6be Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 19 Apr 2023 10:27:33 +1000 Subject: Temporarily allow the touchwheel to be missing --- src/drivers/touchwheel.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/drivers') diff --git a/src/drivers/touchwheel.cpp b/src/drivers/touchwheel.cpp index d5382b3d..51a67187 100644 --- a/src/drivers/touchwheel.cpp +++ b/src/drivers/touchwheel.cpp @@ -47,7 +47,10 @@ void TouchWheel::WriteRegister(uint8_t reg, uint8_t val) { .write_addr(kTouchWheelAddress, I2C_MASTER_WRITE) .write_ack(maskedReg, val) .stop(); - ESP_ERROR_CHECK(transaction.Execute()); + transaction.Execute(); + // TODO(jacqueline): check for errors again when i find where all the ffc + // cables went q.q + // ESP_ERROR_CHECK(transaction.Execute()); } uint8_t TouchWheel::ReadRegister(uint8_t reg) { -- 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 ++- src/drivers/include/dac.hpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'src/drivers') 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); diff --git a/src/drivers/include/dac.hpp b/src/drivers/include/dac.hpp index 06808a78..028d46cb 100644 --- a/src/drivers/include/dac.hpp +++ b/src/drivers/include/dac.hpp @@ -71,7 +71,7 @@ class AudioDac { // TODO(jacqueline): worth supporting channels here as well? auto Reconfigure(BitsPerSample bps, SampleRate rate) -> void; - auto WriteData(cpp::span data) -> std::size_t; + auto WriteData(const cpp::span& data) -> std::size_t; auto Stop() -> void; auto LogStatus() -> void; -- 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 ++---- src/drivers/include/dac.hpp | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'src/drivers') 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 { diff --git a/src/drivers/include/dac.hpp b/src/drivers/include/dac.hpp index 028d46cb..4a1b2a5b 100644 --- a/src/drivers/include/dac.hpp +++ b/src/drivers/include/dac.hpp @@ -71,7 +71,7 @@ class AudioDac { // TODO(jacqueline): worth supporting channels here as well? auto Reconfigure(BitsPerSample bps, SampleRate rate) -> void; - auto WriteData(const cpp::span& data) -> std::size_t; + auto WriteData(const cpp::span& data) -> void; auto Stop() -> void; auto LogStatus() -> 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 +++++++++++++++++++++++++++++---------------- src/drivers/include/dac.hpp | 5 ++++ 2 files changed, 48 insertions(+), 23 deletions(-) (limited to 'src/drivers') 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 { diff --git a/src/drivers/include/dac.hpp b/src/drivers/include/dac.hpp index 4a1b2a5b..6849d92c 100644 --- a/src/drivers/include/dac.hpp +++ b/src/drivers/include/dac.hpp @@ -83,6 +83,7 @@ class AudioDac { private: GpioExpander* gpio_; i2s_chan_handle_t i2s_handle_; + bool i2s_active_; i2s_std_clk_config_t clock_config_; i2s_std_slot_config_t slot_config_; @@ -97,9 +98,13 @@ class AudioDac { PAGE_SELECT = 0, RESET = 1, POWER_MODE = 2, + PLL_ENABLE = 4, DE_EMPHASIS = 7, + PLL_CLOCK_SOURCE = 13, DAC_CLOCK_SOURCE = 14, + RESYNC_REQUEST = 19, CLOCK_ERRORS = 37, + INTERPOLATION = 34, I2S_FORMAT = 40, DIGITAL_VOLUME_L = 61, DIGITAL_VOLUME_R = 62, -- 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 ++++++++++++++++++++++---------------------- src/drivers/include/dac.hpp | 117 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 149 insertions(+), 85 deletions(-) (limited to 'src/drivers') 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() diff --git a/src/drivers/include/dac.hpp b/src/drivers/include/dac.hpp index 6849d92c..6836cf59 100644 --- a/src/drivers/include/dac.hpp +++ b/src/drivers/include/dac.hpp @@ -20,6 +20,91 @@ namespace drivers { +namespace pcm512x { +class Register { + public: + uint8_t page; + uint8_t reg; + + constexpr Register(uint8_t page, uint8_t reg) : page(page), reg(reg) {} +}; + +constexpr Register RESET(0, 1); +constexpr Register POWER(0, 2); +constexpr Register MUTE(0, 3); +constexpr Register PLL_EN(0, 4); +constexpr Register SPI_MISO_FUNCTION(0, 6); +constexpr Register DSP(0, 7); +constexpr Register GPIO_EN(0, 8); +constexpr Register BCLK_LRCLK_CFG(0, 9); +constexpr Register DSP_GPIO_INPUT(0, 10); +constexpr Register MASTER_MODE(0, 12); +constexpr Register PLL_REF(0, 13); +constexpr Register DAC_REF(0, 14); +constexpr Register GPIO_DACIN(0, 16); +constexpr Register GPIO_PLLIN(0, 18); +constexpr Register SYNCHRONIZE(0, 19); +constexpr Register PLL_COEFF_0(0, 20); +constexpr Register PLL_COEFF_1(0, 21); +constexpr Register PLL_COEFF_2(0, 22); +constexpr Register PLL_COEFF_3(0, 23); +constexpr Register PLL_COEFF_4(0, 24); +constexpr Register DSP_CLKDIV(0, 27); +constexpr Register DAC_CLKDIV(0, 28); +constexpr Register NCP_CLKDIV(0, 29); +constexpr Register OSR_CLKDIV(0, 30); +constexpr Register MASTER_CLKDIV_1(0, 32); +constexpr Register MASTER_CLKDIV_2(0, 33); +constexpr Register FS_SPEED_MODE(0, 34); +constexpr Register IDAC_1(0, 35); +constexpr Register IDAC_2(0, 36); +constexpr Register ERROR_DETECT(0, 37); +constexpr Register I2S_1(0, 40); +constexpr Register I2S_2(0, 41); +constexpr Register DAC_ROUTING(0, 42); +constexpr Register DSP_PROGRAM(0, 43); +constexpr Register CLKDET(0, 44); +constexpr Register AUTO_MUTE(0, 59); +constexpr Register DIGITAL_VOLUME_1(0, 60); +constexpr Register DIGITAL_VOLUME_2(0, 61); +constexpr Register DIGITAL_VOLUME_3(0, 62); +constexpr Register DIGITAL_MUTE_1(0, 63); +constexpr Register DIGITAL_MUTE_2(0, 64); +constexpr Register DIGITAL_MUTE_3(0, 65); +constexpr Register GPIO_OUTPUT_1(0, 80); +constexpr Register GPIO_OUTPUT_2(0, 81); +constexpr Register GPIO_OUTPUT_3(0, 82); +constexpr Register GPIO_OUTPUT_4(0, 83); +constexpr Register GPIO_OUTPUT_5(0, 84); +constexpr Register GPIO_OUTPUT_6(0, 85); +constexpr Register GPIO_CONTROL_1(0, 86); +constexpr Register GPIO_CONTROL_2(0, 87); +constexpr Register OVERFLOW(0, 90); +constexpr Register RATE_DET_1(0, 91); +constexpr Register RATE_DET_2(0, 92); +constexpr Register RATE_DET_3(0, 93); +constexpr Register RATE_DET_4(0, 94); +constexpr Register CLOCK_STATUS(0, 95); +constexpr Register ANALOG_MUTE_DET(0, 108); +constexpr Register POWER_STATE(0, 118); +constexpr Register GPIN(0, 119); +constexpr Register DIGITAL_MUTE_DET(0, 120); + +constexpr Register OUTPUT_AMPLITUDE(1, 1); +constexpr Register ANALOG_GAIN_CTRL(1, 2); +constexpr Register UNDERVOLTAGE_PROT(1, 5); +constexpr Register ANALOG_MUTE_CTRL(1, 6); +constexpr Register ANALOG_GAIN_BOOST(1, 7); +constexpr Register VCOM_CTRL_1(1, 8); +constexpr Register VCOM_CTRL_2(1, 9); + +constexpr Register CRAM_CTRL(44, 1); + +constexpr Register FLEX_A(253, 63); +constexpr Register FLEX_B(253, 64); + +} // namespace pcm512x + /** * Interface for a PCM5122PWR DAC, configured over I2C. */ @@ -84,6 +169,7 @@ class AudioDac { GpioExpander* gpio_; i2s_chan_handle_t i2s_handle_; bool i2s_active_; + std::optional active_page_; i2s_std_clk_config_t clock_config_; i2s_std_slot_config_t slot_config_; @@ -94,33 +180,12 @@ class AudioDac { */ bool WaitForPowerState(std::function predicate); - enum Register { - PAGE_SELECT = 0, - RESET = 1, - POWER_MODE = 2, - PLL_ENABLE = 4, - DE_EMPHASIS = 7, - PLL_CLOCK_SOURCE = 13, - DAC_CLOCK_SOURCE = 14, - RESYNC_REQUEST = 19, - CLOCK_ERRORS = 37, - INTERPOLATION = 34, - I2S_FORMAT = 40, - DIGITAL_VOLUME_L = 61, - DIGITAL_VOLUME_R = 62, - - SAMPLE_RATE_DETECTION = 91, - BCK_DETECTION = 93, - CLOCK_ERROR_STATE = 94, - CLOCK_STATUS = 95, - AUTO_MUTE_STATE = 108, - SOFT_MUTE_STATE = 114, - SAMPLE_RATE_STATE = 115, - DSP_BOOT_POWER_STATE = 118, - }; + void WriteRegister(pcm512x::Register r, uint8_t val); + uint8_t ReadRegister(pcm512x::Register r); - void WriteRegister(Register reg, uint8_t val); - uint8_t ReadRegister(Register reg); + void SelectPage(uint8_t page); + void WriteRegisterRaw(uint8_t reg, uint8_t val); + uint8_t ReadRegisterRaw(uint8_t reg); }; } // namespace drivers -- cgit v1.2.3 From 7a54ff0df9c18b662e5bdc11ac2e26ff052cfa4d Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 4 Apr 2023 14:12:01 +1000 Subject: WIP track down new pipeline memory issues --- src/drivers/include/dac.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/drivers') diff --git a/src/drivers/include/dac.hpp b/src/drivers/include/dac.hpp index 6836cf59..b84f9bdb 100644 --- a/src/drivers/include/dac.hpp +++ b/src/drivers/include/dac.hpp @@ -26,7 +26,7 @@ class Register { uint8_t page; uint8_t reg; - constexpr Register(uint8_t page, uint8_t reg) : page(page), reg(reg) {} + constexpr Register(uint8_t p, uint8_t r) : page(p), reg(r) {} }; constexpr Register RESET(0, 1); -- 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') 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 ++++++++++++++++++++++++++++++++++----------- src/drivers/include/dac.hpp | 2 ++ 2 files changed, 58 insertions(+), 17 deletions(-) (limited to 'src/drivers') 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); diff --git a/src/drivers/include/dac.hpp b/src/drivers/include/dac.hpp index b84f9bdb..f2ee9b49 100644 --- a/src/drivers/include/dac.hpp +++ b/src/drivers/include/dac.hpp @@ -12,6 +12,7 @@ #include "esp_err.h" #include "freertos/FreeRTOS.h" #include "freertos/portmacro.h" +#include "freertos/stream_buffer.h" #include "result.hpp" #include "span.hpp" @@ -157,6 +158,7 @@ class AudioDac { auto Reconfigure(BitsPerSample bps, SampleRate rate) -> void; auto WriteData(const cpp::span& data) -> void; + auto SetSource(StreamBufferHandle_t *buffer) -> void; auto Stop() -> void; auto LogStatus() -> void; -- 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 ++++++++++++++++++-------------- src/drivers/include/dac.hpp | 2 +- 2 files changed, 19 insertions(+), 15 deletions(-) (limited to 'src/drivers') 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; diff --git a/src/drivers/include/dac.hpp b/src/drivers/include/dac.hpp index f2ee9b49..d27ed915 100644 --- a/src/drivers/include/dac.hpp +++ b/src/drivers/include/dac.hpp @@ -158,7 +158,7 @@ class AudioDac { auto Reconfigure(BitsPerSample bps, SampleRate rate) -> void; auto WriteData(const cpp::span& data) -> void; - auto SetSource(StreamBufferHandle_t *buffer) -> void; + auto SetSource(StreamBufferHandle_t buffer) -> void; auto Stop() -> void; auto LogStatus() -> void; -- 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 +++++++++++++++++++++++++++++++++++++++++--- src/drivers/include/dac.hpp | 6 +++ 2 files changed, 123 insertions(+), 6 deletions(-) (limited to 'src/drivers') 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 diff --git a/src/drivers/include/dac.hpp b/src/drivers/include/dac.hpp index d27ed915..acdd1743 100644 --- a/src/drivers/include/dac.hpp +++ b/src/drivers/include/dac.hpp @@ -150,8 +150,14 @@ class AudioDac { BPS_32 = I2S_DATA_BIT_WIDTH_32BIT, }; enum SampleRate { + SAMPLE_RATE_11_025 = 11025, + SAMPLE_RATE_16 = 16000, + SAMPLE_RATE_22_05 = 22050, + SAMPLE_RATE_32 = 32000, SAMPLE_RATE_44_1 = 44100, SAMPLE_RATE_48 = 48000, + SAMPLE_RATE_96 = 96000, + SAMPLE_RATE_192 = 192000, }; // TODO(jacqueline): worth supporting channels here as well? -- 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') 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/CMakeLists.txt | 2 +- src/drivers/dac.cpp | 5 ++--- src/drivers/display.cpp | 4 ++-- src/drivers/include/dac.hpp | 3 +-- src/drivers/include/display.hpp | 3 +-- src/drivers/include/storage.hpp | 7 +++---- src/drivers/storage.cpp | 17 +++++++++++------ 7 files changed, 21 insertions(+), 20 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/CMakeLists.txt b/src/drivers/CMakeLists.txt index bf8f0c4e..072a8b68 100644 --- a/src/drivers/CMakeLists.txt +++ b/src/drivers/CMakeLists.txt @@ -1,6 +1,6 @@ idf_component_register( SRCS "touchwheel.cpp" "dac.cpp" "gpio_expander.cpp" "battery.cpp" "storage.cpp" "i2c.cpp" - "spi.cpp" "display.cpp" "display_init.cpp" + "spi.cpp" "display.cpp" "display_init.cpp" "driver_cache.cpp" INCLUDE_DIRS "include" REQUIRES "esp_adc" "fatfs" "result" "lvgl" "span") target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS}) 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) diff --git a/src/drivers/display.cpp b/src/drivers/display.cpp index 56bd6e60..f8594a5a 100644 --- a/src/drivers/display.cpp +++ b/src/drivers/display.cpp @@ -63,7 +63,7 @@ extern "C" void FlushDataCallback(lv_disp_drv_t* disp_drv, auto Display::create(GpioExpander* expander, const displays::InitialisationData& init_data) - -> std::unique_ptr { + -> Display* { ESP_LOGI(kTag, "Init I/O pins"); gpio_config_t dr_config{ .pin_bit_mask = 1ULL << kDisplayDr, @@ -134,7 +134,7 @@ auto Display::create(GpioExpander* expander, ESP_LOGI(kTag, "Registering driver"); display->display_ = lv_disp_drv_register(&display->driver_); - return display; + return display.release(); } Display::Display(GpioExpander* gpio, spi_device_handle_t handle) diff --git a/src/drivers/include/dac.hpp b/src/drivers/include/dac.hpp index acdd1743..4952c992 100644 --- a/src/drivers/include/dac.hpp +++ b/src/drivers/include/dac.hpp @@ -117,8 +117,7 @@ class AudioDac { FAILED_TO_INSTALL_I2S, }; - static auto create(GpioExpander* expander) - -> cpp::result, Error>; + static auto create(GpioExpander* expander) -> cpp::result; AudioDac(GpioExpander* gpio, i2s_chan_handle_t i2s_handle); ~AudioDac(); diff --git a/src/drivers/include/display.hpp b/src/drivers/include/display.hpp index 8157c3a5..9e4a0224 100644 --- a/src/drivers/include/display.hpp +++ b/src/drivers/include/display.hpp @@ -23,8 +23,7 @@ class Display { * us back any kind of signal to tell us we're actually using them correctly. */ static auto create(GpioExpander* expander, - const displays::InitialisationData& init_data) - -> std::unique_ptr; + const displays::InitialisationData& init_data) -> Display*; Display(GpioExpander* gpio, spi_device_handle_t handle); ~Display(); diff --git a/src/drivers/include/storage.hpp b/src/drivers/include/storage.hpp index 64ce4782..c19ec935 100644 --- a/src/drivers/include/storage.hpp +++ b/src/drivers/include/storage.hpp @@ -25,14 +25,13 @@ class SdStorage { FAILED_TO_MOUNT, }; - static auto create(GpioExpander* gpio) - -> cpp::result, Error>; + static auto create(GpioExpander* gpio) -> cpp::result; SdStorage(GpioExpander* gpio, esp_err_t (*do_transaction)(sdspi_dev_handle_t, sdmmc_command_t*), sdspi_dev_handle_t handle_, - std::unique_ptr& host_, - std::unique_ptr& card_, + std::unique_ptr host_, + std::unique_ptr card_, FATFS* fs_); ~SdStorage(); diff --git a/src/drivers/storage.cpp b/src/drivers/storage.cpp index 88159744..d90bd811 100644 --- a/src/drivers/storage.cpp +++ b/src/drivers/storage.cpp @@ -49,8 +49,9 @@ static esp_err_t do_transaction(sdspi_dev_handle_t handle, } } // namespace callback -auto SdStorage::create(GpioExpander* gpio) - -> cpp::result, Error> { +auto SdStorage::create(GpioExpander* gpio) -> cpp::result { + gpio->set_pin(GpioExpander::SD_CARD_POWER_ENABLE, 0); + gpio->set_pin(GpioExpander::SD_MUX_EN_ACTIVE_LOW, 0); gpio->set_pin(GpioExpander::SD_MUX_SWITCH, GpioExpander::SD_MUX_ESP); gpio->Write(); @@ -103,16 +104,16 @@ auto SdStorage::create(GpioExpander* gpio) return cpp::fail(Error::FAILED_TO_MOUNT); } - return std::make_unique(gpio, do_transaction, handle, host, card, - fs); + return new SdStorage(gpio, do_transaction, handle, std::move(host), + std::move(card), fs); } SdStorage::SdStorage(GpioExpander* gpio, esp_err_t (*do_transaction)(sdspi_dev_handle_t, sdmmc_command_t*), sdspi_dev_handle_t handle, - std::unique_ptr& host, - std::unique_ptr& card, + std::unique_ptr host, + std::unique_ptr card, FATFS* fs) : gpio_(gpio), do_transaction_(do_transaction), @@ -136,6 +137,10 @@ SdStorage::~SdStorage() { // Uninstall the SPI driver sdspi_host_remove_device(this->handle_); sdspi_host_deinit(); + + gpio_->set_pin(GpioExpander::SD_CARD_POWER_ENABLE, 0); + gpio_->set_pin(GpioExpander::SD_MUX_EN_ACTIVE_LOW, 1); + gpio_->Write(); } auto SdStorage::HandleTransaction(sdspi_dev_handle_t handle, -- cgit v1.2.3 From 464a4bf527b9da30985a6576f702429621a849a6 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 21 Apr 2023 18:42:49 +1000 Subject: Add a RAII-friendly DI wrapper for driver instances --- src/drivers/driver_cache.cpp | 43 ++++++++++++++++++++++++++++ src/drivers/include/driver_cache.hpp | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/drivers/driver_cache.cpp create mode 100644 src/drivers/include/driver_cache.hpp (limited to 'src/drivers') diff --git a/src/drivers/driver_cache.cpp b/src/drivers/driver_cache.cpp new file mode 100644 index 00000000..650e6f16 --- /dev/null +++ b/src/drivers/driver_cache.cpp @@ -0,0 +1,43 @@ +#include "driver_cache.hpp" + +#include +#include + +#include "display.hpp" +#include "display_init.hpp" +#include "storage.hpp" +#include "touchwheel.hpp" + +namespace drivers { + +DriverCache::DriverCache() : gpios_(std::make_unique()) {} +DriverCache::~DriverCache() {} + +auto DriverCache::AcquireGpios() -> GpioExpander* { + return gpios_.get(); +} + +auto DriverCache::AcquireDac() -> std::shared_ptr { + return Acquire(dac_, [&]() -> AudioDac* { + return AudioDac::create(AcquireGpios()).value_or(nullptr); + }); +} + +auto DriverCache::AcquireDisplay() -> std::shared_ptr { + return Acquire(display_, [&]() -> Display* { + return Display::create(AcquireGpios(), displays::kST7735R); + }); +} + +auto DriverCache::AcquireStorage() -> std::shared_ptr { + return Acquire(storage_, [&]() -> SdStorage* { + return SdStorage::create(AcquireGpios()).value_or(nullptr); + }); +} + +auto DriverCache::AcquireTouchWheel() -> std::shared_ptr { + return Acquire(touchwheel_, + [&]() -> TouchWheel* { return new TouchWheel(); }); +} + +} // namespace drivers diff --git a/src/drivers/include/driver_cache.hpp b/src/drivers/include/driver_cache.hpp new file mode 100644 index 00000000..c56ebc3f --- /dev/null +++ b/src/drivers/include/driver_cache.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include +#include + +#include "dac.hpp" +#include "display.hpp" +#include "gpio_expander.hpp" +#include "storage.hpp" +#include "touchwheel.hpp" + +namespace drivers { + +class DriverCache { + private: + std::unique_ptr gpios_; + std::weak_ptr dac_; + std::weak_ptr display_; + std::weak_ptr storage_; + std::weak_ptr touchwheel_; + // TODO(jacqueline): Haptics, samd + + std::mutex mutex_; + + template + auto Acquire(std::weak_ptr ptr, F factory) -> std::shared_ptr { + std::shared_ptr acquired = ptr.lock(); + if (acquired) { + return acquired; + } + + std::lock_guard lock(mutex_); + + acquired = ptr.lock(); + if (acquired) { + return acquired; + } + acquired.reset(factory()); + ptr = acquired; + return acquired; + } + + public: + DriverCache(); + ~DriverCache(); + + auto AcquireGpios() -> GpioExpander*; + auto AcquireDac() -> std::shared_ptr; + auto AcquireDisplay() -> std::shared_ptr; + auto AcquireStorage() -> std::shared_ptr; + auto AcquireTouchWheel() -> std::shared_ptr; +}; + +} // namespace drivers -- cgit v1.2.3