From a0798c7887731b7f04349cfdb36e031ace49df08 Mon Sep 17 00:00:00 2001 From: ailurux Date: Wed, 22 Feb 2023 11:51:26 +1100 Subject: [PATCH 01/31] Software rotation for display --- src/drivers/display.cpp | 3 +++ 1 file changed, 3 insertions(+) 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(); From 0fce4fcc06a600aac6b0cb6fc1ef1ebb91bcdf27 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 8 Mar 2023 12:47:56 +1100 Subject: [PATCH 02/31] update to faceplate bodge --- src/drivers/display.cpp | 10 +++++++++- src/drivers/include/gpio_expander.hpp | 12 ++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) 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, }; From b9a75cd55a11fd404a1977539acb64a6705f3809 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 10 Mar 2023 12:26:32 +1100 Subject: [PATCH 03/31] LDO up at boot --- src/drivers/include/gpio_expander.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) From 78ec09c494faadf9e7d06dc7d3e04531c3a34ff7 Mon Sep 17 00:00:00 2001 From: ailurux Date: Mon, 13 Mar 2023 15:14:32 +1100 Subject: [PATCH 04/31] Touchwheel test --- lib/esp-adf | 1 + 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 ++++++++++++++++++++++++++++++ src/main/main.cpp | 13 +++++ 7 files changed, 174 insertions(+), 4 deletions(-) create mode 160000 lib/esp-adf create mode 100644 src/drivers/include/touchwheel.hpp create mode 100644 src/drivers/touchwheel.cpp diff --git a/lib/esp-adf b/lib/esp-adf new file mode 160000 index 00000000..9ffbb5a1 --- /dev/null +++ b/lib/esp-adf @@ -0,0 +1 @@ +Subproject commit 9ffbb5a1b8be06484e23e67202607e6b0b0d3a8e 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 diff --git a/src/main/main.cpp b/src/main/main.cpp index 91e17451..bd0a06af 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -35,6 +35,7 @@ #include "i2c.hpp" #include "spi.hpp" #include "storage.hpp" +#include "touchwheel.hpp" static const char* TAG = "MAIN"; @@ -102,6 +103,7 @@ extern "C" void app_main(void) { ESP_LOGI(TAG, "Enable power rails for development"); expander->with([&](auto& gpio) { gpio.set_pin(drivers::GpioExpander::AUDIO_POWER_ENABLE, 1); + gpio.set_pin(drivers::GpioExpander::DISPLAY_LED, 0); gpio.set_pin(drivers::GpioExpander::USB_INTERFACE_POWER_ENABLE, 0); gpio.set_pin(drivers::GpioExpander::SD_CARD_POWER_ENABLE, 1); gpio.set_pin(drivers::GpioExpander::SD_MUX_SWITCH, @@ -121,6 +123,15 @@ extern "C" void app_main(void) { storage = std::move(storage_res.value()); } + ESP_LOGI(TAG, "Init touch wheel"); + auto touchwheel_res = drivers::TouchWheel::create(expander); + std::shared_ptr touchwheel; + if (touchwheel_res.has_error()) { + ESP_LOGE(TAG, "Failed!"); + } else { + touchwheel = std::move(touchwheel_res.value()); + } + LvglArgs* lvglArgs = (LvglArgs*)calloc(1, sizeof(LvglArgs)); lvglArgs->gpio_expander = expander; xTaskCreateStaticPinnedToCore(&lvgl_main, "LVGL", kLvglStackSize, @@ -146,6 +157,8 @@ extern "C" void app_main(void) { console.Launch(); while (1) { + touchwheel->Update(); + ESP_LOGI(TAG, "Touch wheel pos: %d", touchwheel->GetTouchWheelData().wheel_position); vTaskDelay(pdMS_TO_TICKS(100)); } } From 53ad4fc3f260ea7de56d1bce24e284b48c0e856b Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 13 Mar 2023 15:19:22 +1100 Subject: [PATCH 05/31] daniel wat --- lib/esp-adf | 1 - 1 file changed, 1 deletion(-) delete mode 160000 lib/esp-adf diff --git a/lib/esp-adf b/lib/esp-adf deleted file mode 160000 index 9ffbb5a1..00000000 --- a/lib/esp-adf +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9ffbb5a1b8be06484e23e67202607e6b0b0d3a8e From a0ae39befe11c2a5a78ee5877cd5dd9cda90e27c Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 30 Mar 2023 09:31:08 +1100 Subject: [PATCH 06/31] 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 +++++++++++++-------------- src/main/main.cpp | 12 ++--- 6 files changed, 59 insertions(+), 55 deletions(-) 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, }; /** diff --git a/src/main/main.cpp b/src/main/main.cpp index bd0a06af..57d88f96 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -102,12 +102,10 @@ extern "C" void app_main(void) { ESP_LOGI(TAG, "Enable power rails for development"); expander->with([&](auto& gpio) { - gpio.set_pin(drivers::GpioExpander::AUDIO_POWER_ENABLE, 1); - gpio.set_pin(drivers::GpioExpander::DISPLAY_LED, 0); - gpio.set_pin(drivers::GpioExpander::USB_INTERFACE_POWER_ENABLE, 0); - gpio.set_pin(drivers::GpioExpander::SD_CARD_POWER_ENABLE, 1); + gpio.set_pin(drivers::GpioExpander::SD_MUX_EN_ACTIVE_LOW, 0); gpio.set_pin(drivers::GpioExpander::SD_MUX_SWITCH, drivers::GpioExpander::SD_MUX_ESP); + gpio.set_pin(drivers::GpioExpander::SD_CARD_POWER_ENABLE, 1); }); ESP_LOGI(TAG, "Init battery measurement"); @@ -123,6 +121,7 @@ extern "C" void app_main(void) { storage = std::move(storage_res.value()); } + /* ESP_LOGI(TAG, "Init touch wheel"); auto touchwheel_res = drivers::TouchWheel::create(expander); std::shared_ptr touchwheel; @@ -131,6 +130,7 @@ extern "C" void app_main(void) { } else { touchwheel = std::move(touchwheel_res.value()); } + */ LvglArgs* lvglArgs = (LvglArgs*)calloc(1, sizeof(LvglArgs)); lvglArgs->gpio_expander = expander; @@ -157,8 +157,8 @@ extern "C" void app_main(void) { console.Launch(); while (1) { - touchwheel->Update(); - ESP_LOGI(TAG, "Touch wheel pos: %d", touchwheel->GetTouchWheelData().wheel_position); + //touchwheel->Update(); + //ESP_LOGI(TAG, "Touch wheel pos: %d", touchwheel->GetTouchWheelData().wheel_position); vTaskDelay(pdMS_TO_TICKS(100)); } } From 9799ab458d8ad870aaec048abf5b1b1d93aa7aff Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 30 Mar 2023 10:15:09 +1100 Subject: [PATCH 07/31] fix sd card on for dev + mux pin --- src/drivers/include/gpio_expander.hpp | 8 ++++---- src/main/main.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) 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, }; /** diff --git a/src/main/main.cpp b/src/main/main.cpp index 57d88f96..692b97fb 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -105,7 +105,7 @@ extern "C" void app_main(void) { gpio.set_pin(drivers::GpioExpander::SD_MUX_EN_ACTIVE_LOW, 0); gpio.set_pin(drivers::GpioExpander::SD_MUX_SWITCH, drivers::GpioExpander::SD_MUX_ESP); - gpio.set_pin(drivers::GpioExpander::SD_CARD_POWER_ENABLE, 1); + gpio.set_pin(drivers::GpioExpander::SD_CARD_POWER_ENABLE, 0); }); ESP_LOGI(TAG, "Init battery measurement"); From 1b245316fe282035f094a3656f717d419e8bd526 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 4 Apr 2023 14:48:35 +1000 Subject: [PATCH 08/31] fix up touchpad timeouts, make it less chatty --- src/drivers/i2c.cpp | 1 - src/drivers/include/touchwheel.hpp | 33 +++++-------- src/drivers/touchwheel.cpp | 79 ++++++++++++++---------------- src/main/main.cpp | 21 ++++---- 4 files changed, 57 insertions(+), 77 deletions(-) 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 diff --git a/src/main/main.cpp b/src/main/main.cpp index 692b97fb..8860befe 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -121,16 +122,9 @@ extern "C" void app_main(void) { storage = std::move(storage_res.value()); } - /* ESP_LOGI(TAG, "Init touch wheel"); - auto touchwheel_res = drivers::TouchWheel::create(expander); - std::shared_ptr touchwheel; - if (touchwheel_res.has_error()) { - ESP_LOGE(TAG, "Failed!"); - } else { - touchwheel = std::move(touchwheel_res.value()); - } - */ + std::unique_ptr touchwheel = + std::make_unique(); LvglArgs* lvglArgs = (LvglArgs*)calloc(1, sizeof(LvglArgs)); lvglArgs->gpio_expander = expander; @@ -156,9 +150,14 @@ extern "C" void app_main(void) { console::AppConsole console(playback.get()); console.Launch(); + uint8_t prev_position = 0; while (1) { - //touchwheel->Update(); - //ESP_LOGI(TAG, "Touch wheel pos: %d", touchwheel->GetTouchWheelData().wheel_position); + touchwheel->Update(); + auto wheel_data = touchwheel->GetTouchWheelData(); + if (wheel_data.wheel_position != prev_position) { + prev_position = wheel_data.wheel_position; + ESP_LOGI(TAG, "Touch wheel pos: %u", prev_position); + } vTaskDelay(pdMS_TO_TICKS(100)); } } From c93ed8efad85996e8ec3cec68bdc8feeae71b8fc Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 6 Apr 2023 16:18:04 +1000 Subject: [PATCH 09/31] fix some display issues --- src/drivers/display.cpp | 47 +++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) 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_); From a1cef17c5bb66c453e4a8e83e52a56fe9173346c Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 12 Apr 2023 16:37:27 +1000 Subject: [PATCH 10/31] 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(-) 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? From 35ecf8c49ba76922ca73f83c97a709a3329866f5 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 17 Apr 2023 16:39:06 +1000 Subject: [PATCH 11/31] rename --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 86c54a3c..14b93bcf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,4 +10,4 @@ idf_build_set_property(COMPILE_OPTIONS "-DTCB_SPAN_NO_CONTRACT_CHECKING" APPEND) # Include all app components. list(APPEND EXTRA_COMPONENT_DIRS "$ENV{PROJ_PATH}/src") -project(gay-ipod-fw) +project(tangara) From 2a46eecdc6334c31cee2b40427d2536b48cbb6be Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 19 Apr 2023 10:27:33 +1000 Subject: [PATCH 12/31] Temporarily allow the touchwheel to be missing --- src/drivers/touchwheel.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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) { From a9531c86a433c8b7ae1f77ff0266c27c39eca7f4 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 10 Mar 2023 11:28:33 +1100 Subject: [PATCH 13/31] mostly single task pipeline --- src/audio/CMakeLists.txt | 2 +- src/audio/audio_decoder.cpp | 153 ++++++++--------- src/audio/audio_playback.cpp | 60 ++++--- src/audio/audio_task.cpp | 212 +++++++++++------------- src/audio/fatfs_audio_input.cpp | 89 +++------- src/audio/i2s_audio_output.cpp | 83 ++++------ src/audio/include/audio_decoder.hpp | 19 +-- src/audio/include/audio_element.hpp | 58 +------ src/audio/include/audio_playback.hpp | 13 +- src/audio/include/audio_task.hpp | 31 +++- src/audio/include/fatfs_audio_input.hpp | 17 +- src/audio/include/i2s_audio_output.hpp | 17 +- src/audio/include/pipeline.hpp | 42 +++++ src/audio/include/stream_info.hpp | 63 ++++++- src/audio/pipeline.cpp | 52 ++++++ src/codecs/include/codec.hpp | 7 +- src/drivers/dac.cpp | 3 +- src/drivers/include/dac.hpp | 2 +- src/memory/CMakeLists.txt | 2 +- src/memory/include/himem.hpp | 78 +++++++++ 20 files changed, 543 insertions(+), 460 deletions(-) create mode 100644 src/audio/include/pipeline.hpp create mode 100644 src/audio/pipeline.cpp create mode 100644 src/memory/include/himem.hpp diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt index d4c89214..6ad35f8f 100644 --- a/src/audio/CMakeLists.txt +++ b/src/audio/CMakeLists.txt @@ -1,7 +1,7 @@ idf_component_register( SRCS "audio_decoder.cpp" "audio_task.cpp" "chunk.cpp" "fatfs_audio_input.cpp" "stream_message.cpp" "i2s_audio_output.cpp" "stream_buffer.cpp" - "audio_playback.cpp" "stream_event.cpp" "audio_element.cpp" + "audio_playback.cpp" "stream_event.cpp" "audio_element.cpp" "pipeline.cpp" INCLUDE_DIRS "include" REQUIRES "codecs" "drivers" "cbor" "result" "tasks" "span" "memory") diff --git a/src/audio/audio_decoder.cpp b/src/audio/audio_decoder.cpp index 9879b042..f8614478 100644 --- a/src/audio/audio_decoder.cpp +++ b/src/audio/audio_decoder.cpp @@ -2,13 +2,16 @@ #include +#include #include #include #include +#include "cbor/tinycbor/src/cborinternal_p.h" #include "freertos/FreeRTOS.h" #include "esp_heap_caps.h" +#include "esp_log.h" #include "freertos/message_buffer.h" #include "freertos/portmacro.h" @@ -21,126 +24,106 @@ namespace audio { static const char* kTag = "DEC"; -static const std::size_t kChunkSize = 1024; -static const std::size_t kReadahead = 8; - AudioDecoder::AudioDecoder() : IAudioElement(), - arena_(kChunkSize, kReadahead, MALLOC_CAP_SPIRAM), stream_info_({}), has_samples_to_send_(false), needs_more_input_(true) {} AudioDecoder::~AudioDecoder() {} -auto AudioDecoder::HasUnprocessedInput() -> bool { - return !needs_more_input_ || has_samples_to_send_; -} - -auto AudioDecoder::IsOverBuffered() -> bool { - return arena_.BlocksFree() == 0; -} - -auto AudioDecoder::ProcessStreamInfo(const StreamInfo& info) -> void { - stream_info_ = info; - - if (info.chunk_size) { - chunk_reader_.emplace(info.chunk_size.value()); - } else { - ESP_LOGE(kTag, "no chunk size given"); - return; +auto AudioDecoder::ProcessStreamInfo(const StreamInfo& info) -> bool { + if (!std::holds_alternative(info.data)) { + return false; } + const auto& encoded = std::get(info.data); // Reuse the existing codec if we can. This will help with gapless playback, // since we can potentially just continue to decode as we were before, // without any setup overhead. + // TODO: use audio type from stream if (current_codec_ != nullptr && - current_codec_->CanHandleFile(info.path.value_or(""))) { + current_codec_->CanHandleType(encoded.type)) { current_codec_->ResetForNewStream(); - return; + return true; } - auto result = codecs::CreateCodecForFile(info.path.value_or("")); + // TODO: use audio type from stream + auto result = codecs::CreateCodecForType(encoded.type); if (result.has_value()) { current_codec_ = std::move(result.value()); } else { ESP_LOGE(kTag, "no codec for this file"); - return; - } - - stream_info_ = info; - has_sent_stream_info_ = false; -} - -auto AudioDecoder::ProcessChunk(const cpp::span& chunk) -> void { - if (current_codec_ == nullptr || !chunk_reader_) { - // Should never happen, but fail explicitly anyway. - ESP_LOGW(kTag, "received chunk without chunk size or codec"); - return; + return false; } - ESP_LOGI(kTag, "received new chunk (size %u)", chunk.size()); - current_codec_->SetInput(chunk_reader_->HandleNewData(chunk)); - needs_more_input_ = false; + return true; } -auto AudioDecoder::ProcessEndOfStream() -> void { - has_samples_to_send_ = false; - needs_more_input_ = true; - current_codec_.reset(); +auto AudioDecoder::Process(std::vector* inputs, MutableStream* output) + -> void { + // We don't really expect multiple inputs, so just pick the first that + // contains data. If none of them contain data, then we can still flush + // pending samples. + auto input = + std::find_if(inputs->begin(), inputs->end(), + [](const Stream& s) { return s.data.size_bytes() > 0; }); + + if (input != inputs->end()) { + const StreamInfo* info = input->info; + if (!stream_info_ || *stream_info_ != *info) { + // The input stream has changed! Immediately throw everything away and + // start from scratch. + // TODO: special case gapless playback? needs thought. + stream_info_ = *info; + has_samples_to_send_ = false; + has_set_stream_info_ = false; + + ProcessStreamInfo(*info); + } - SendOrBufferEvent(std::unique_ptr( - StreamEvent::CreateEndOfStream(input_events_))); -} + current_codec_->SetInput(input->data); + } -auto AudioDecoder::Process() -> void { - if (has_samples_to_send_) { - // Writing samples is relatively quick (it's just a bunch of memcopy's), so - // do them all at once. - while (has_samples_to_send_ && !IsOverBuffered()) { - if (!has_sent_stream_info_) { - has_sent_stream_info_ = true; + while (true) { + if (has_samples_to_send_) { + if (!has_set_stream_info_) { + has_set_stream_info_ = true; auto format = current_codec_->GetOutputFormat(); - stream_info_->bits_per_sample = format.bits_per_sample; - stream_info_->sample_rate = format.sample_rate_hz; - stream_info_->channels = format.num_channels; - stream_info_->chunk_size = kChunkSize; - - auto event = - StreamEvent::CreateStreamInfo(input_events_, *stream_info_); - SendOrBufferEvent(std::unique_ptr(event)); - } - - auto block = arena_.Acquire(); - if (!block) { - return; + output->info->data.emplace( + format.bits_per_sample, format.sample_rate_hz, format.num_channels); } - auto write_res = - current_codec_->WriteOutputSamples({block->start, block->size}); - block->used_size = write_res.first; + auto write_res = current_codec_->WriteOutputSamples( + output->data.subspan(output->info->bytes_in_stream)); + output->info->bytes_in_stream += write_res.first; has_samples_to_send_ = !write_res.second; - auto chunk = std::unique_ptr( - StreamEvent::CreateArenaChunk(input_events_, *block)); - if (!SendOrBufferEvent(std::move(chunk))) { - return; + if (has_samples_to_send_) { + // We weren't able to fit all the generated samples into the output + // buffer. Stop trying; we'll finish up during the next pass. + break; } } - // We will process the next frame during the next call to this method. - } - - if (!needs_more_input_) { - auto res = current_codec_->ProcessNextFrame(); - if (res.has_error()) { - // TODO(jacqueline): Handle errors. - return; - } - needs_more_input_ = res.value(); - has_samples_to_send_ = true; - if (needs_more_input_) { - chunk_reader_->HandleBytesUsed(current_codec_->GetInputPosition()); + if (input != inputs->end()) { + auto res = current_codec_->ProcessNextFrame(); + if (res.has_error()) { + // TODO(jacqueline): Handle errors. + return; + } + input->data = input->data.subspan(current_codec_->GetInputPosition()); + + if (res.value()) { + // We're out of data in this buffer. Finish immediately; there's nothing + // to send. + break; + } else { + has_samples_to_send_ = true; + } + } else { + // No input; nothing to do. + break; } } } diff --git a/src/audio/audio_playback.cpp b/src/audio/audio_playback.cpp index c95a5d63..613f629c 100644 --- a/src/audio/audio_playback.cpp +++ b/src/audio/audio_playback.cpp @@ -13,6 +13,7 @@ #include "fatfs_audio_input.hpp" #include "gpio_expander.hpp" #include "i2s_audio_output.hpp" +#include "pipeline.hpp" #include "storage.hpp" #include "stream_buffer.hpp" #include "stream_info.hpp" @@ -20,11 +21,10 @@ namespace audio { -auto AudioPlayback::create(drivers::GpioExpander* expander, - std::shared_ptr storage) +auto AudioPlayback::create(drivers::GpioExpander* expander) -> cpp::result, Error> { // Create everything - auto source = std::make_shared(storage); + auto source = std::make_shared(); auto codec = std::make_shared(); auto sink_res = I2SAudioOutput::create(expander); @@ -35,41 +35,47 @@ auto AudioPlayback::create(drivers::GpioExpander* expander, auto playback = std::make_unique(); - // Configure the pipeline - playback->ConnectElements(source.get(), codec.get()); - playback->ConnectElements(codec.get(), sink.get()); + Pipeline *pipeline = new Pipeline(sink.get()); + pipeline->AddInput(codec.get())->AddInput(source.get()); - // Launch! - StartAudioTask("src", {}, source); - StartAudioTask("dec", {}, codec); - StartAudioTask("sink", 0, sink); - - playback->input_handle_ = source->InputEventQueue(); + task::Start(pipeline); return playback; } -AudioPlayback::AudioPlayback() {} +AudioPlayback::AudioPlayback() { + // Create everything + auto source = std::make_shared(); + auto codec = std::make_shared(); -AudioPlayback::~AudioPlayback() {} + auto sink_res = I2SAudioOutput::create(expander); + if (sink_res.has_error()) { + return cpp::fail(ERR_INIT_ELEMENT); + } + auto sink = sink_res.value(); -auto AudioPlayback::Play(const std::string& filename) -> void { - StreamInfo info; - info.path = filename; - auto event = StreamEvent::CreateStreamInfo(input_handle_, info); - xQueueSend(input_handle_, &event, portMAX_DELAY); - event = StreamEvent::CreateEndOfStream(input_handle_); - xQueueSend(input_handle_, &event, portMAX_DELAY); + auto playback = std::make_unique(); + + Pipeline *pipeline = new Pipeline(sink.get()); + pipeline->AddInput(codec.get())->AddInput(source.get()); + + task::Start(pipeline); + + return playback; } -auto AudioPlayback::LogStatus() -> void { - auto event = StreamEvent::CreateLogStatus(); - xQueueSendToFront(input_handle_, &event, portMAX_DELAY); +AudioPlayback::~AudioPlayback() { + pipeline_->Quit(); } -auto AudioPlayback::ConnectElements(IAudioElement* src, IAudioElement* sink) - -> void { - src->OutputEventQueue(sink->InputEventQueue()); +auto AudioPlayback::Play(const std::string& filename) -> void { + // TODO: concurrency, yo! + file_source->OpenFile(filename); + pipeline_->Play(); +} + +auto AudioPlayback::LogStatus() -> void { + // TODO. } } // namespace audio diff --git a/src/audio/audio_task.cpp b/src/audio/audio_task.cpp index ce6d724e..542bada8 100644 --- a/src/audio/audio_task.cpp +++ b/src/audio/audio_task.cpp @@ -2,16 +2,20 @@ #include +#include +#include #include #include #include #include "cbor.h" +#include "esp_err.h" #include "esp_heap_caps.h" #include "esp_log.h" #include "freertos/portmacro.h" #include "freertos/projdefs.h" #include "freertos/queue.h" +#include "pipeline.hpp" #include "span.hpp" #include "arena.hpp" @@ -25,25 +29,26 @@ namespace audio { +namespace task { + static const char* kTag = "task"; +static const std::size_t kStackSize = 24 * 1024; +static const uint8_t kAudioCore = 0; -auto StartAudioTask(const std::string& name, - std::optional core_id, - std::shared_ptr element) -> void { - auto task_handle = std::make_unique(); +auto Start(Pipeline* pipeline) -> Handle* { + auto input_queue = xQueueCreate(8, 1); // Newly created task will free this. - AudioTaskArgs* args = new AudioTaskArgs{.element = element}; - - ESP_LOGI(kTag, "starting audio task %s", name.c_str()); - if (core_id) { - xTaskCreatePinnedToCore(&AudioTaskMain, name.c_str(), - element->StackSizeBytes(), args, kTaskPriorityAudio, - task_handle.get(), *core_id); - } else { - xTaskCreate(&AudioTaskMain, name.c_str(), element->StackSizeBytes(), args, - kTaskPriorityAudio, task_handle.get()); - } + AudioTaskArgs* args = new AudioTaskArgs{ + .pipeline = pipeline, + .input = input_queue, + }; + + ESP_LOGI(kTag, "starting audio task"); + xTaskCreatePinnedToCore(&AudioTaskMain, "pipeline", kStackSize, args, + kTaskPriorityAudio, NULL, kAudioCore); + + return new Handle(input_queue); } void AudioTaskMain(void* args) { @@ -51,113 +56,88 @@ void AudioTaskMain(void* args) { // called before the task quits. { AudioTaskArgs* real_args = reinterpret_cast(args); - std::shared_ptr element = std::move(real_args->element); + std::unique_ptr pipeline(real_args->pipeline); + QueueHandle_t input; + StreamBufferHandle_t output; delete real_args; - // Queue of events that we have received on our input queue, but not yet - // processed. - std::deque> pending_events; - - // TODO(jacqueline): quit event - while (true) { - // First, we pull events from our input queue into pending_events. This - // keeps us responsive to any events that need to be handled immediately. - // Then we check if there's any events to flush downstream. - // Then we pass anything requiring processing to the element. - - bool has_work_to_do = - (!pending_events.empty() || element->HasUnflushedOutput() || - element->HasUnprocessedInput()) && - !element->IsOverBuffered(); - - if (has_work_to_do) { - ESP_LOGD(kTag, "checking for events"); - } else { - ESP_LOGD(kTag, "waiting for events"); - } - - // If we have no new events to process and the element has nothing left to - // do, then just delay forever waiting for a new event. - TickType_t ticks_to_wait = has_work_to_do ? 0 : portMAX_DELAY; - - StreamEvent* new_event = nullptr; - bool has_event = - xQueueReceive(element->InputEventQueue(), &new_event, ticks_to_wait); - - if (has_event) { - if (new_event->tag == StreamEvent::UNINITIALISED) { - ESP_LOGE(kTag, "discarding invalid event!!"); - } else if (new_event->tag == StreamEvent::CHUNK_NOTIFICATION) { - delete new_event; - } else if (new_event->tag == StreamEvent::LOG_STATUS) { - element->ProcessLogStatus(); - if (element->OutputEventQueue() != nullptr) { - xQueueSendToFront(element->OutputEventQueue(), &new_event, 0); - } else { - delete new_event; - } - } else { - // This isn't an event that needs to be actioned immediately. Add it - // to our work queue. - pending_events.emplace_back(new_event); - ESP_LOGD(kTag, "deferring event"); + std::vector elements = pipeline->GetIterationOrder(); + std::size_t max_inputs = + (*std::max_element(elements.begin(), elements.end(), + [](Pipeline const* first, Pipeline const* second) { + return first->NumInputs() < second->NumInputs(); + })) + ->NumInputs(); + + // We need to be able to simultaneously map all of an element's inputs, plus + // its output. So preallocate that many ranges. + std::vector> in_regions(max_inputs); + MappableRegion out_region; + std::for_each(in_regions.begin(), in_regions.end(), + [](const MappableRegion& region) { + assert(region.is_valid); + }); + assert(out_region.is_valid); + + // Each element has exactly one output buffer. + std::vector> buffers(elements.size()); + std::vector buffer_infos(buffers.size()); + std::for_each(buffers.begin(), buffers.end(), + [](const HimemAlloc& alloc) { + assert(alloc.is_valid); + }); + + bool playing = true; + bool quit = false; + while (!quit) { + // TODO: full event here? + Command cmd; + bool has_cmd = xQueueReceive(input, &cmd, 0); + if (has_cmd) { + switch (cmd) { + case PLAY: + playing = true; + break; + case PAUSE: + playing = false; + break; + case QUIT: + quit = true; + break; } - // Loop again, so that we service all incoming events before doing our - // possibly expensive processing. - continue; } - - if (element->HasUnflushedOutput()) { - ESP_LOGD(kTag, "flushing output"); - } - - // We have no new events. Next, see if there's anything that needs to be - // flushed. - if (element->HasUnflushedOutput() && !element->FlushBufferedOutput()) { - // We had things to flush, but couldn't send it all. This probably - // implies that the downstream element is having issues servicing its - // input queue, so hold off for a moment before retrying. - ESP_LOGW(kTag, "failed to flush buffered output"); - vTaskDelay(pdMS_TO_TICKS(100)); - continue; + if (quit) { + break; } - if (element->HasUnprocessedInput()) { - ESP_LOGD(kTag, "processing input events"); - element->Process(); - continue; - } - - // The element ran out of data, so now it's time to let it process more - // input. - while (!pending_events.empty()) { - std::unique_ptr event; - pending_events.front().swap(event); - pending_events.pop_front(); - ESP_LOGD(kTag, "processing event, tag %i", event->tag); - - if (event->tag == StreamEvent::STREAM_INFO) { - ESP_LOGD(kTag, "processing stream info"); - - element->ProcessStreamInfo(*event->stream_info); - - } else if (event->tag == StreamEvent::ARENA_CHUNK) { - ESP_LOGD(kTag, "processing arena data"); - - memory::ArenaRef ref(event->arena_chunk); - auto callback = - StreamEvent::CreateChunkNotification(element->InputEventQueue()); - if (!xQueueSend(event->source, &callback, 0)) { - ESP_LOGW(kTag, "failed to send chunk notif"); - continue; + if (playing) { + for (int i = 0; i < elements.size(); i++) { + std::vector in_streams; + elements.at(i)->InStreams(&in_regions, &in_streams); + MutableStream out_stream = elements.at(i)->OutStream(&out_region); + + // Crop the input and output streams to the ranges that are safe to + // touch. For the input streams, this is the region that contains + // data. For the output stream, this is the region that does *not* + // already contain data. + std::vector cropped_in_streams; + std::for_each(in_streams.begin(), in_streams.end(), + [&](MutableStream& s) { + cropped_in_streams.emplace_back( + s.info, s.data.first(s.info->bytes_in_stream)); + }); + + elements.at(i)->OutputElement()->Process(&cropped_in_streams, + &out_stream); + + for (int stream = 0; stream < in_streams.size(); stream++) { + MutableStream& orig_stream = in_streams.at(stream); + Stream& cropped_stream = cropped_in_streams.at(stream); + std::move(cropped_stream.data.begin(), cropped_stream.data.end(), + orig_stream.data.begin()); + orig_stream.info->bytes_in_stream = + cropped_stream.data.size_bytes(); } - - // TODO(jacqueline): Consider giving the element a full ArenaRef here, - // so that it can hang on to it and potentially save an alloc+copy. - element->ProcessChunk({ref.ptr.start, ref.ptr.used_size}); - - // TODO: think about whether to do the whole queue - break; } } } @@ -165,4 +145,6 @@ void AudioTaskMain(void* args) { vTaskDelete(NULL); } +} // namespace task + } // namespace audio diff --git a/src/audio/fatfs_audio_input.cpp b/src/audio/fatfs_audio_input.cpp index 5354c5fd..bd8748eb 100644 --- a/src/audio/fatfs_audio_input.cpp +++ b/src/audio/fatfs_audio_input.cpp @@ -7,6 +7,8 @@ #include "arena.hpp" #include "esp_heap_caps.h" +#include "esp_log.h" +#include "ff.h" #include "freertos/portmacro.h" #include "audio_element.hpp" @@ -15,43 +17,23 @@ #include "stream_event.hpp" #include "stream_info.hpp" #include "stream_message.hpp" +#include "types.hpp" static const char* kTag = "SRC"; namespace audio { -static const std::size_t kChunkSize = 24 * 1024; -static const std::size_t kChunkReadahead = 2; - -FatfsAudioInput::FatfsAudioInput(std::shared_ptr storage) - : IAudioElement(), - arena_(kChunkSize, kChunkReadahead, MALLOC_CAP_SPIRAM), - storage_(storage), - current_file_(), - is_file_open_(false) {} +FatfsAudioInput::FatfsAudioInput() + : IAudioElement(), current_file_(), is_file_open_(false) {} FatfsAudioInput::~FatfsAudioInput() {} -auto FatfsAudioInput::HasUnprocessedInput() -> bool { - return is_file_open_; -} - -auto FatfsAudioInput::IsOverBuffered() -> bool { - return arena_.BlocksFree() == 0; -} - -auto FatfsAudioInput::ProcessStreamInfo(const StreamInfo& info) -> void { +auto FatfsAudioInput::OpenFile(const std::string& path) -> void { if (is_file_open_) { f_close(¤t_file_); is_file_open_ = false; } - - if (!info.path) { - // TODO(jacqueline): Handle errors. - return; - } - ESP_LOGI(kTag, "opening file %s", info.path->c_str()); - std::string path = *info.path; + ESP_LOGI(kTag, "opening file %s", path.c_str()); FRESULT res = f_open(¤t_file_, path.c_str(), FA_READ); if (res != FR_OK) { ESP_LOGE(kTag, "failed to open file! res: %i", res); @@ -60,51 +42,30 @@ auto FatfsAudioInput::ProcessStreamInfo(const StreamInfo& info) -> void { } is_file_open_ = true; - - StreamInfo new_info(info); - new_info.chunk_size = kChunkSize; - ESP_LOGI(kTag, "chunk size: %u bytes", kChunkSize); - - auto event = StreamEvent::CreateStreamInfo(input_events_, new_info); - SendOrBufferEvent(std::unique_ptr(event)); } -auto FatfsAudioInput::ProcessChunk(const cpp::span& chunk) -> void {} - -auto FatfsAudioInput::ProcessEndOfStream() -> void { - if (is_file_open_) { - f_close(¤t_file_); - is_file_open_ = false; - SendOrBufferEvent(std::unique_ptr( - StreamEvent::CreateEndOfStream(input_events_))); +auto FatfsAudioInput::Process(std::vector* inputs, + MutableStream* output) -> void { + if (!is_file_open_) { + return; } -} - -auto FatfsAudioInput::Process() -> void { - if (is_file_open_) { - auto dest_block = memory::ArenaRef::Acquire(&arena_); - if (!dest_block) { - return; - } - FRESULT result = f_read(¤t_file_, dest_block->ptr.start, - dest_block->ptr.size, &dest_block->ptr.used_size); - if (result != FR_OK) { - ESP_LOGE(kTag, "file I/O error %d", result); - // TODO(jacqueline): Handle errors. - return; - } - - if (dest_block->ptr.used_size < dest_block->ptr.size || - f_eof(¤t_file_)) { - f_close(¤t_file_); - is_file_open_ = false; - } + FRESULT result = + f_read(¤t_file_, output->data.data(), output->data.size_bytes(), + &output->info->bytes_in_stream); + if (result != FR_OK) { + ESP_LOGE(kTag, "file I/O error %d", result); + // TODO(jacqueline): Handle errors. + return; + } - auto dest_event = std::unique_ptr( - StreamEvent::CreateArenaChunk(input_events_, dest_block->Release())); + // TODO: read from filename? + output->info->data = StreamInfo::Encoded{codecs::STREAM_MP3}; - SendOrBufferEvent(std::move(dest_event)); + if (output->info->bytes_in_stream < output->data.size_bytes() || + f_eof(¤t_file_)) { + f_close(¤t_file_); + is_file_open_ = false; } } diff --git a/src/audio/i2s_audio_output.cpp b/src/audio/i2s_audio_output.cpp index 110227cf..7766ebed 100644 --- a/src/audio/i2s_audio_output.cpp +++ b/src/audio/i2s_audio_output.cpp @@ -1,6 +1,7 @@ #include "i2s_audio_output.hpp" #include +#include #include "esp_err.h" #include "freertos/portmacro.h" @@ -10,14 +11,12 @@ #include "freertos/projdefs.h" #include "gpio_expander.hpp" #include "result.hpp" +#include "stream_info.hpp" -static const TickType_t kIdleTimeBeforeMute = pdMS_TO_TICKS(1000); static const char* kTag = "I2SOUT"; namespace audio { -static const std::size_t kDmaQueueLength = 8; - auto I2SAudioOutput::create(drivers::GpioExpander* expander) -> cpp::result, Error> { // First, we need to perform initial configuration of the DAC chip. @@ -38,40 +37,26 @@ auto I2SAudioOutput::create(drivers::GpioExpander* expander) I2SAudioOutput::I2SAudioOutput(drivers::GpioExpander* expander, std::unique_ptr dac) - : expander_(expander), - dac_(std::move(dac)), - chunk_reader_(), - latest_chunk_() {} + : expander_(expander), dac_(std::move(dac)), current_config_() {} I2SAudioOutput::~I2SAudioOutput() {} -auto I2SAudioOutput::HasUnprocessedInput() -> bool { - return latest_chunk_.size() > 0; -} - -auto I2SAudioOutput::IsOverBuffered() -> bool { - return false; -} - -auto I2SAudioOutput::ProcessStreamInfo(const StreamInfo& info) -> void { - // TODO(jacqueline): probs do something with the channel hey - - if (!info.bits_per_sample || !info.sample_rate) { - ESP_LOGE(kTag, "audio stream missing bits or sample rate"); - return; +auto I2SAudioOutput::ProcessStreamInfo(const StreamInfo& info) -> bool { + if (!std::holds_alternative(info.data)) { + return false; } - if (!info.chunk_size) { - ESP_LOGE(kTag, "audio stream missing chunk size"); - return; + StreamInfo::Pcm pcm = std::get(info.data); + + if (current_config_ && pcm == *current_config_) { + return true; } - chunk_reader_.emplace(*info.chunk_size); - ESP_LOGI(kTag, "incoming audio stream: %u bpp @ %u Hz", *info.bits_per_sample, - *info.sample_rate); + ESP_LOGI(kTag, "incoming audio stream: %u bpp @ %u Hz", pcm.bits_per_sample, + pcm.sample_rate); drivers::AudioDac::BitsPerSample bps; - switch (*info.bits_per_sample) { + switch (pcm.bits_per_sample) { case 16: bps = drivers::AudioDac::BPS_16; break; @@ -83,11 +68,11 @@ auto I2SAudioOutput::ProcessStreamInfo(const StreamInfo& info) -> void { break; default: ESP_LOGE(kTag, "dropping stream with unknown bps"); - return; + return false; } drivers::AudioDac::SampleRate sample_rate; - switch (*info.sample_rate) { + switch (pcm.sample_rate) { case 44100: sample_rate = drivers::AudioDac::SAMPLE_RATE_44_1; break; @@ -96,37 +81,25 @@ auto I2SAudioOutput::ProcessStreamInfo(const StreamInfo& info) -> void { break; default: ESP_LOGE(kTag, "dropping stream with unknown rate"); - return; + return false; } - dac_->Reconfigure(bps, sample_rate); -} - -auto I2SAudioOutput::ProcessChunk(const cpp::span& chunk) -> void { - latest_chunk_ = chunk_reader_->HandleNewData(chunk); -} + // TODO(jacqueline): probs do something with the channel hey -auto I2SAudioOutput::ProcessEndOfStream() -> void { - dac_->Stop(); - SendOrBufferEvent(std::unique_ptr( - StreamEvent::CreateEndOfStream(input_events_))); -} + dac_->Reconfigure(bps, sample_rate); + current_config_ = pcm; -auto I2SAudioOutput::ProcessLogStatus() -> void { - dac_->LogStatus(); + return true; } -auto I2SAudioOutput::Process() -> void { - // Note: avoid logging here! We need to get bytes from the chunk buffer into - // the I2S DMA buffer as fast as possible, to avoid running out of samples. - std::size_t bytes_written = dac_->WriteData(latest_chunk_); - if (bytes_written == latest_chunk_.size_bytes()) { - latest_chunk_ = cpp::span(); - chunk_reader_->HandleBytesLeftOver(0); - } else { - latest_chunk_ = latest_chunk_.subspan(bytes_written); - } - return; +auto I2SAudioOutput::Process(std::vector* inputs, MutableStream* output) + -> void { + std::for_each(inputs->begin(), inputs->end(), [&](Stream& s) { + if (ProcessStreamInfo(s.info)) { + std::size_t bytes_written = dac_->WriteData(s.data); + s.data = s.data.subspan(bytes_written); + } + }); } auto I2SAudioOutput::SetVolume(uint8_t volume) -> void { diff --git a/src/audio/include/audio_decoder.hpp b/src/audio/include/audio_decoder.hpp index 47642469..be8daf99 100644 --- a/src/audio/include/audio_decoder.hpp +++ b/src/audio/include/audio_decoder.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "chunk.hpp" #include "ff.h" @@ -10,6 +11,7 @@ #include "audio_element.hpp" #include "codec.hpp" +#include "stream_info.hpp" namespace audio { @@ -22,28 +24,21 @@ class AudioDecoder : public IAudioElement { AudioDecoder(); ~AudioDecoder(); - auto StackSizeBytes() const -> std::size_t override { return 10 * 1024; }; - - auto HasUnprocessedInput() -> bool override; - auto IsOverBuffered() -> bool override; - - auto ProcessStreamInfo(const StreamInfo& info) -> void override; - auto ProcessChunk(const cpp::span& chunk) -> void override; - auto ProcessEndOfStream() -> void override; - auto Process() -> void override; + auto Process(std::vector* inputs, MutableStream* output) + -> void override; AudioDecoder(const AudioDecoder&) = delete; AudioDecoder& operator=(const AudioDecoder&) = delete; private: - memory::Arena arena_; std::unique_ptr current_codec_; std::optional stream_info_; - std::optional chunk_reader_; - bool has_sent_stream_info_; + bool has_set_stream_info_; bool has_samples_to_send_; bool needs_more_input_; + + auto ProcessStreamInfo(const StreamInfo& info) -> bool; }; } // namespace audio diff --git a/src/audio/include/audio_element.hpp b/src/audio/include/audio_element.hpp index 91036348..c9192e4a 100644 --- a/src/audio/include/audio_element.hpp +++ b/src/audio/include/audio_element.hpp @@ -40,62 +40,8 @@ class IAudioElement { IAudioElement(); virtual ~IAudioElement(); - /* - * Returns the stack size in bytes that this element requires. This should - * be tuned according to the observed stack size of each element, as different - * elements have fairly different stack requirements (particular decoders). - */ - virtual auto StackSizeBytes() const -> std::size_t { return 4096; }; - - /* Returns this element's input buffer. */ - auto InputEventQueue() const -> QueueHandle_t { return input_events_; } - /* Returns this element's output buffer. */ - auto OutputEventQueue() const -> QueueHandle_t { return output_events_; } - auto OutputEventQueue(const QueueHandle_t q) -> void { output_events_ = q; } - - virtual auto HasUnprocessedInput() -> bool = 0; - - virtual auto IsOverBuffered() -> bool { return false; } - - auto HasUnflushedOutput() -> bool { return !buffered_output_.empty(); } - auto FlushBufferedOutput() -> bool; - - /* - * Called when a StreamInfo message is received. Used to configure this - * element in preperation for incoming chunks. - */ - virtual auto ProcessStreamInfo(const StreamInfo& info) -> void = 0; - - /* - * Called when a ChunkHeader message is received. Includes the data associated - * with this chunk of stream data. This method should return the number of - * bytes in this chunk that were actually used; leftover bytes will be - * prepended to the next call. - */ - virtual auto ProcessChunk(const cpp::span& chunk) -> void = 0; - - virtual auto ProcessEndOfStream() -> void = 0; - - virtual auto ProcessLogStatus() -> void {} - - /* - * Called when there has been no data received over the input buffer for some - * time. This could be used to synthesize output, or to save memory by - * releasing unused resources. - */ - virtual auto Process() -> void = 0; - - protected: - auto SendOrBufferEvent(std::unique_ptr event) -> bool; - - // Queue for events coming into this element. Owned by us. - QueueHandle_t input_events_; - // Queue for events going into the next element. Not owned by us, may be null - // if we're not yet in a pipeline. - // FIXME: it would be nicer if this was non-nullable. - QueueHandle_t output_events_; - // Output events that have been generated, but are yet to be sent downstream. - std::deque> buffered_output_; + virtual auto Process(std::vector* inputs, MutableStream* output) + -> void = 0; }; } // namespace audio diff --git a/src/audio/include/audio_playback.hpp b/src/audio/include/audio_playback.hpp index a1bea64f..507e6f73 100644 --- a/src/audio/include/audio_playback.hpp +++ b/src/audio/include/audio_playback.hpp @@ -5,7 +5,9 @@ #include #include +#include "audio_task.hpp" #include "esp_err.h" +#include "fatfs_audio_input.hpp" #include "result.hpp" #include "span.hpp" @@ -23,12 +25,10 @@ namespace audio { class AudioPlayback { public: enum Error { ERR_INIT_ELEMENT, ERR_MEM }; - static auto create(drivers::GpioExpander* expander, - std::shared_ptr storage) + static auto create(drivers::GpioExpander* expander) -> cpp::result, Error>; - // TODO(jacqueline): configure on the fly once we have things to configure. - AudioPlayback(); + AudioPlayback(FatfsAudioInput *file_input); ~AudioPlayback(); /* @@ -44,9 +44,10 @@ class AudioPlayback { AudioPlayback& operator=(const AudioPlayback&) = delete; private: - auto ConnectElements(IAudioElement* src, IAudioElement* sink) -> void; + FatfsAudioInput *file_source; - QueueHandle_t input_handle_; + std::vector> all_elements_; + std::unique_ptr pipeline_; }; } // namespace audio diff --git a/src/audio/include/audio_task.hpp b/src/audio/include/audio_task.hpp index df70ebaa..8db99850 100644 --- a/src/audio/include/audio_task.hpp +++ b/src/audio/include/audio_task.hpp @@ -6,17 +6,38 @@ #include "audio_element.hpp" #include "freertos/portmacro.h" +#include "pipeline.hpp" namespace audio { +namespace task { struct AudioTaskArgs { - std::shared_ptr& element; + Pipeline* pipeline; + QueueHandle_t input; }; -auto StartAudioTask(const std::string& name, - std::optional core_id, - std::shared_ptr element) -> void; +extern "C" void AudioTaskMain(void* args); -void AudioTaskMain(void* args); +enum Command { PLAY, PAUSE, QUIT }; + +class Handle { + public: + explicit Handle(QueueHandle_t input); + ~Handle(); + + auto SetStreamInfo() -> void; + auto Play() -> void; + auto Pause() -> void; + auto Quit() -> void; + + auto OutputBuffer() -> StreamBufferHandle_t; + + private: + QueueHandle_t input; +}; + +auto Start(Pipeline* pipeline) -> Handle*; + +} // namespace task } // namespace audio diff --git a/src/audio/include/fatfs_audio_input.hpp b/src/audio/include/fatfs_audio_input.hpp index 9f2d676c..b3a6d843 100644 --- a/src/audio/include/fatfs_audio_input.hpp +++ b/src/audio/include/fatfs_audio_input.hpp @@ -3,41 +3,36 @@ #include #include #include +#include #include "arena.hpp" #include "chunk.hpp" #include "freertos/FreeRTOS.h" +#include "ff.h" #include "freertos/message_buffer.h" #include "freertos/queue.h" #include "span.hpp" #include "audio_element.hpp" -#include "storage.hpp" #include "stream_buffer.hpp" namespace audio { class FatfsAudioInput : public IAudioElement { public: - explicit FatfsAudioInput(std::shared_ptr storage); + explicit FatfsAudioInput(); ~FatfsAudioInput(); - auto HasUnprocessedInput() -> bool override; - auto IsOverBuffered() -> bool override; + auto OpenFile(const std::string& path) -> void; - auto ProcessStreamInfo(const StreamInfo& info) -> void override; - auto ProcessChunk(const cpp::span& chunk) -> void override; - auto ProcessEndOfStream() -> void override; - auto Process() -> void override; + auto Process(std::vector* inputs, MutableStream* output) + -> void override; FatfsAudioInput(const FatfsAudioInput&) = delete; FatfsAudioInput& operator=(const FatfsAudioInput&) = delete; private: - memory::Arena arena_; - std::shared_ptr storage_; - FIL current_file_; bool is_file_open_; }; diff --git a/src/audio/include/i2s_audio_output.hpp b/src/audio/include/i2s_audio_output.hpp index 2bea091b..57881b35 100644 --- a/src/audio/include/i2s_audio_output.hpp +++ b/src/audio/include/i2s_audio_output.hpp @@ -2,6 +2,7 @@ #include #include +#include #include "audio_element.hpp" #include "chunk.hpp" @@ -9,6 +10,7 @@ #include "dac.hpp" #include "gpio_expander.hpp" +#include "stream_info.hpp" namespace audio { @@ -22,14 +24,8 @@ class I2SAudioOutput : public IAudioElement { std::unique_ptr dac); ~I2SAudioOutput(); - auto HasUnprocessedInput() -> bool override; - auto IsOverBuffered() -> bool override; - - auto ProcessStreamInfo(const StreamInfo& info) -> void override; - auto ProcessChunk(const cpp::span& chunk) -> void override; - auto ProcessEndOfStream() -> void override; - auto ProcessLogStatus() -> void override; - auto Process() -> void override; + auto Process(std::vector* inputs, MutableStream* output) + -> void override; I2SAudioOutput(const I2SAudioOutput&) = delete; I2SAudioOutput& operator=(const I2SAudioOutput&) = delete; @@ -40,8 +36,9 @@ class I2SAudioOutput : public IAudioElement { drivers::GpioExpander* expander_; std::unique_ptr dac_; - std::optional chunk_reader_; - cpp::span latest_chunk_; + std::optional current_config_; + + auto ProcessStreamInfo(const StreamInfo& info) -> bool; }; } // namespace audio diff --git a/src/audio/include/pipeline.hpp b/src/audio/include/pipeline.hpp new file mode 100644 index 00000000..42f70828 --- /dev/null +++ b/src/audio/include/pipeline.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include +#include + +#include "freertos/portmacro.h" + +#include "audio_element.hpp" +#include "himem.hpp" +#include "stream_info.hpp" + +namespace audio { + +static const std::size_t kPipelineBufferSize = 32 * 1024; + +class Pipeline { + public: + Pipeline(IAudioElement* output); + ~Pipeline(); + auto AddInput(IAudioElement* input) -> Pipeline*; + + auto OutputElement() const -> IAudioElement*; + + auto NumInputs() const -> std::size_t; + + auto InStreams(std::vector>*, + std::vector*) -> void; + + auto OutStream(MappableRegion*) -> MutableStream; + + auto GetIterationOrder() -> std::vector; + + private: + IAudioElement* root_; + std::vector> subtrees_; + + HimemAlloc output_buffer_; + StreamInfo output_info_; +}; + +} // namespace audio diff --git a/src/audio/include/stream_info.hpp b/src/audio/include/stream_info.hpp index bf67364f..47f65649 100644 --- a/src/audio/include/stream_info.hpp +++ b/src/audio/include/stream_info.hpp @@ -4,19 +4,68 @@ #include #include #include +#include -#include "cbor.h" #include "result.hpp" -#include "sys/_stdint.h" +#include "span.hpp" +#include "types.hpp" namespace audio { struct StreamInfo { - std::optional path; - std::optional channels; - std::optional bits_per_sample; - std::optional sample_rate; - std::optional chunk_size; + // The number of bytes that are available for consumption within this + // stream's buffer. + std::size_t bytes_in_stream; + + // The total length of this stream, in case its source is finite (e.g. a + // file on disk). May be absent for endless streams (internet streams, + // generated audio, etc.) + std::optional length_bytes; + + struct Encoded { + // The codec that this stream is associated with. + codecs::StreamType type; + + bool operator==(const Encoded&) const = default; + }; + + struct Pcm { + // Number of channels in this stream. + uint8_t channels; + // Number of bits per sample. + uint8_t bits_per_sample; + // The sample rate. + uint16_t sample_rate; + + bool operator==(const Pcm&) const = default; + }; + + std::variant data; + + bool operator==(const StreamInfo&) const = default; +}; + +class MutableStream { + public: + StreamInfo* info; + cpp::span data; + + MutableStream(StreamInfo* i, cpp::span d) + : info(i), data(d) {} +}; + +/* + * A byte buffer + associated metadata, which is not allowed to modify any of + * the underlying data. + */ +class Stream { + public: + explicit Stream(const MutableStream& s) : info(*s.info), data(s.data) {} + + const StreamInfo& info; + // `data` itself left mutable for signalling how much of the stream was + // consumed + cpp::span data; }; } // namespace audio diff --git a/src/audio/pipeline.cpp b/src/audio/pipeline.cpp new file mode 100644 index 00000000..f42e6853 --- /dev/null +++ b/src/audio/pipeline.cpp @@ -0,0 +1,52 @@ +#include "pipeline.hpp" +#include "stream_info.hpp" + +namespace audio { + +Pipeline::Pipeline(IAudioElement* output) : root_(output), subtrees_() {} +Pipeline::~Pipeline() {} + +auto Pipeline::AddInput(IAudioElement* input) -> Pipeline* { + subtrees_.emplace_back(input); + return subtrees_.back().get(); +} + +auto Pipeline::OutputElement() const -> IAudioElement* { + return root_; +} + +auto Pipeline::NumInputs() const -> std::size_t { + return subtrees_.size(); +} + +auto Pipeline::InStreams( + std::vector>* regions, + std::vector* out) -> void { + for (int i = 0; i < subtrees_.size(); i++) { + MutableStream s = subtrees_[i]->OutStream(®ions->at(i)); + out->push_back(s); + } +} + +auto Pipeline::OutStream(MappableRegion* region) + -> MutableStream { + return {&output_info_, region->Map(output_buffer_)}; +} + +auto Pipeline::GetIterationOrder() -> std::vector { + std::vector to_search{this}; + std::vector found; + + while (!to_search.empty()) { + Pipeline* current = to_search.back(); + to_search.pop_back(); + found.push_back(current); + + to_search.insert(to_search.end(), current->subtrees_.begin(), + current->subtrees_.end()); + } + + return found; +} + +} // namespace audio diff --git a/src/codecs/include/codec.hpp b/src/codecs/include/codec.hpp index 6897acf2..4595f877 100644 --- a/src/codecs/include/codec.hpp +++ b/src/codecs/include/codec.hpp @@ -10,6 +10,7 @@ #include "result.hpp" #include "span.hpp" +#include "types.hpp" namespace codecs { @@ -17,7 +18,7 @@ class ICodec { public: virtual ~ICodec() {} - virtual auto CanHandleFile(const std::string& path) -> bool = 0; + virtual auto CanHandleType(StreamType type) -> bool = 0; struct OutputFormat { uint8_t num_channels; @@ -31,7 +32,7 @@ class ICodec { virtual auto ResetForNewStream() -> void = 0; - virtual auto SetInput(cpp::span input) -> void = 0; + virtual auto SetInput(cpp::span input) -> void = 0; /* * Returns the codec's next read position within the input buffer. If the @@ -63,7 +64,7 @@ class ICodec { enum CreateCodecError { UNKNOWN_EXTENSION }; -auto CreateCodecForFile(const std::string& file) +auto CreateCodecForType(StreamType type) -> cpp::result, CreateCodecError>; } // namespace codecs 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; diff --git a/src/memory/CMakeLists.txt b/src/memory/CMakeLists.txt index 67e64267..69afc454 100644 --- a/src/memory/CMakeLists.txt +++ b/src/memory/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "arena.cpp" INCLUDE_DIRS "include" REQUIRES "span") +idf_component_register(SRCS "arena.cpp" INCLUDE_DIRS "include" REQUIRES "span" "esp_psram") target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS}) diff --git a/src/memory/include/himem.hpp b/src/memory/include/himem.hpp new file mode 100644 index 00000000..c65091d7 --- /dev/null +++ b/src/memory/include/himem.hpp @@ -0,0 +1,78 @@ +#pragma once + +#include +#include + +#include "esp32/himem.h" +#include "span.hpp" + +/* + * Wrapper around an ESP-IDF himem allocation, which uses RAII to clean up after + * itself. + */ +template +class HimemAlloc { + public: + esp_himem_handle_t handle; + const bool is_valid; + + HimemAlloc() : is_valid(esp_himem_alloc(size, &handle) == ESP_OK) {} + + ~HimemAlloc() { + if (is_valid) { + esp_himem_free(handle); + } + } + + // Not copyable or movable. + HimemAlloc(const HimemAlloc&) = delete; + HimemAlloc& operator=(const HimemAlloc&) = delete; +}; + +/* + * Wrapper around an ESP-IDF himem allocation, which maps a HimemAlloc into the + * usable address space. Instances always contain the last memory region that + * was mapped within them. + */ +template +class MappableRegion { + private: + std::byte* bytes_; + + public: + esp_himem_rangehandle_t range_handle; + const bool is_valid; + + MappableRegion() + : bytes_(nullptr), + is_valid(esp_himem_alloc_map_range(size, &range_handle) == ESP_OK) {} + + ~MappableRegion() { + if (bytes_ != nullptr) { + esp_himem_unmap(range_handle, bytes_, size); + } + if (is_valid) { + esp_himem_free_map_range(range_handle); + } + } + + auto Get() -> cpp::span { + if (bytes_ != nullptr) { + return {}; + } + return {bytes_, size}; + } + + auto Map(const HimemAlloc &alloc) -> cpp::span { + if (bytes_ != nullptr) { + ESP_ERROR_CHECK(esp_himem_unmap(range_handle, bytes_, size)); + } + ESP_ERROR_CHECK(esp_himem_map(alloc.handle, range_handle, 0, 0, size, 0, + reinterpret_cast(&bytes_))); + return Get(); + } + + // Not copyable or movable. + MappableRegion(const MappableRegion&) = delete; + MappableRegion& operator=(const MappableRegion&) = delete; +}; From 3817ec0c77b8d44e54b35ea9f76e7bb4666c6c08 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 30 Mar 2023 10:21:33 +1100 Subject: [PATCH 14/31] turn on amp for development --- src/main/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/main.cpp b/src/main/main.cpp index 8860befe..a30dc675 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -107,6 +107,7 @@ extern "C" void app_main(void) { gpio.set_pin(drivers::GpioExpander::SD_MUX_SWITCH, drivers::GpioExpander::SD_MUX_ESP); gpio.set_pin(drivers::GpioExpander::SD_CARD_POWER_ENABLE, 0); + gpio.set_pin(drivers::GpioExpander::AMP_EN, 1); }); ESP_LOGI(TAG, "Init battery measurement"); From 7c6fd654f50e6665efa4226c6b927f9762734182 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Sat, 1 Apr 2023 13:22:21 +1100 Subject: [PATCH 15/31] New pipeline building, still needs proper control --- src/audio/CMakeLists.txt | 2 +- src/audio/audio_decoder.cpp | 97 +++++++++--------- src/audio/audio_element.cpp | 56 ---------- src/audio/audio_playback.cpp | 48 +++------ src/audio/audio_task.cpp | 129 ++++++++++++++---------- src/audio/fatfs_audio_input.cpp | 20 ++-- src/audio/i2s_audio_output.cpp | 23 ++--- src/audio/include/audio_decoder.hpp | 8 +- src/audio/include/audio_element.hpp | 8 +- src/audio/include/audio_playback.hpp | 10 +- src/audio/include/audio_sink.hpp | 22 ++++ src/audio/include/audio_task.hpp | 34 +++---- src/audio/include/fatfs_audio_input.hpp | 3 +- src/audio/include/i2s_audio_output.hpp | 11 +- src/audio/include/pipeline.hpp | 7 +- src/audio/include/stream_info.hpp | 69 +++++++++++-- src/audio/pipeline.cpp | 14 +-- src/codecs/codec.cpp | 2 +- src/codecs/include/mad.hpp | 4 +- src/codecs/mad.cpp | 7 +- src/drivers/dac.cpp | 6 +- src/drivers/include/dac.hpp | 2 +- src/main/main.cpp | 4 +- src/memory/include/himem.hpp | 2 +- 24 files changed, 296 insertions(+), 292 deletions(-) delete mode 100644 src/audio/audio_element.cpp create mode 100644 src/audio/include/audio_sink.hpp diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt index 6ad35f8f..34fcd8ee 100644 --- a/src/audio/CMakeLists.txt +++ b/src/audio/CMakeLists.txt @@ -1,7 +1,7 @@ idf_component_register( SRCS "audio_decoder.cpp" "audio_task.cpp" "chunk.cpp" "fatfs_audio_input.cpp" "stream_message.cpp" "i2s_audio_output.cpp" "stream_buffer.cpp" - "audio_playback.cpp" "stream_event.cpp" "audio_element.cpp" "pipeline.cpp" + "audio_playback.cpp" "stream_event.cpp" "pipeline.cpp" INCLUDE_DIRS "include" REQUIRES "codecs" "drivers" "cbor" "result" "tasks" "span" "memory") diff --git a/src/audio/audio_decoder.cpp b/src/audio/audio_decoder.cpp index f8614478..ada1f8f7 100644 --- a/src/audio/audio_decoder.cpp +++ b/src/audio/audio_decoder.cpp @@ -26,22 +26,22 @@ static const char* kTag = "DEC"; AudioDecoder::AudioDecoder() : IAudioElement(), - stream_info_({}), - has_samples_to_send_(false), - needs_more_input_(true) {} + current_codec_(), + current_input_format_(), + current_output_format_(), + has_samples_to_send_(false) {} AudioDecoder::~AudioDecoder() {} auto AudioDecoder::ProcessStreamInfo(const StreamInfo& info) -> bool { - if (!std::holds_alternative(info.data)) { + if (!std::holds_alternative(info.format)) { return false; } - const auto& encoded = std::get(info.data); + const auto& encoded = std::get(info.format); // Reuse the existing codec if we can. This will help with gapless playback, // since we can potentially just continue to decode as we were before, // without any setup overhead. - // TODO: use audio type from stream if (current_codec_ != nullptr && current_codec_->CanHandleType(encoded.type)) { current_codec_->ResetForNewStream(); @@ -60,43 +60,47 @@ auto AudioDecoder::ProcessStreamInfo(const StreamInfo& info) -> bool { return true; } -auto AudioDecoder::Process(std::vector* inputs, MutableStream* output) - -> void { +auto AudioDecoder::Process(const std::vector& inputs, + OutputStream* output) -> void { // We don't really expect multiple inputs, so just pick the first that // contains data. If none of them contain data, then we can still flush // pending samples. - auto input = - std::find_if(inputs->begin(), inputs->end(), - [](const Stream& s) { return s.data.size_bytes() > 0; }); - - if (input != inputs->end()) { - const StreamInfo* info = input->info; - if (!stream_info_ || *stream_info_ != *info) { - // The input stream has changed! Immediately throw everything away and - // start from scratch. - // TODO: special case gapless playback? needs thought. - stream_info_ = *info; - has_samples_to_send_ = false; - has_set_stream_info_ = false; - - ProcessStreamInfo(*info); - } + auto input = std::find_if( + inputs.begin(), inputs.end(), + [](const InputStream& s) { return s.data().size_bytes() > 0; }); + if (input == inputs.end()) { + input = inputs.begin(); + } + + const StreamInfo& info = input->info(); + if (!current_input_format_ || *current_input_format_ != info.format) { + // The input stream has changed! Immediately throw everything away and + // start from scratch. + current_input_format_ = info.format; + has_samples_to_send_ = false; - current_codec_->SetInput(input->data); + ProcessStreamInfo(info); } + current_codec_->SetInput(input->data()); + while (true) { if (has_samples_to_send_) { - if (!has_set_stream_info_) { - has_set_stream_info_ = true; + if (!current_output_format_) { auto format = current_codec_->GetOutputFormat(); - output->info->data.emplace( - format.bits_per_sample, format.sample_rate_hz, format.num_channels); + current_output_format_ = StreamInfo::Pcm{ + .channels = format.num_channels, + .bits_per_sample = format.bits_per_sample, + .sample_rate = format.sample_rate_hz, + }; + } + + if (!output->prepare(*current_output_format_)) { + break; } - auto write_res = current_codec_->WriteOutputSamples( - output->data.subspan(output->info->bytes_in_stream)); - output->info->bytes_in_stream += write_res.first; + auto write_res = current_codec_->WriteOutputSamples(output->data()); + output->add(write_res.first); has_samples_to_send_ = !write_res.second; if (has_samples_to_send_) { @@ -106,26 +110,23 @@ auto AudioDecoder::Process(std::vector* inputs, MutableStream* output) } } - if (input != inputs->end()) { - auto res = current_codec_->ProcessNextFrame(); - if (res.has_error()) { - // TODO(jacqueline): Handle errors. - return; - } - input->data = input->data.subspan(current_codec_->GetInputPosition()); + auto res = current_codec_->ProcessNextFrame(); + if (res.has_error()) { + // TODO(jacqueline): Handle errors. + return; + } - if (res.value()) { - // We're out of data in this buffer. Finish immediately; there's nothing - // to send. - break; - } else { - has_samples_to_send_ = true; - } - } else { - // No input; nothing to do. + if (res.value()) { + // We're out of useable data in this buffer. Finish immediately; there's + // nothing to send. + input->mark_incomplete(); break; + } else { + has_samples_to_send_ = true; } } + + input->consume(current_codec_->GetInputPosition()); } } // namespace audio diff --git a/src/audio/audio_element.cpp b/src/audio/audio_element.cpp deleted file mode 100644 index cef54631..00000000 --- a/src/audio/audio_element.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "audio_element.hpp" -#include - -namespace audio { - -IAudioElement::IAudioElement() - : input_events_(xQueueCreate(kEventQueueSize, sizeof(void*))), - output_events_(nullptr), - buffered_output_() {} - -IAudioElement::~IAudioElement() { - // Ensure we don't leak any memory from events leftover in the queue. - while (uxQueueSpacesAvailable(input_events_) < kEventQueueSize) { - StreamEvent* event; - if (xQueueReceive(input_events_, &event, 0)) { - free(event); - } else { - break; - } - } - // Technically there's a race here if someone is still adding to the queue, - // but hopefully the whole pipeline is stopped if an element is being - // destroyed. - vQueueDelete(input_events_); -} - -auto IAudioElement::SendOrBufferEvent(std::unique_ptr event) - -> bool { - if (!buffered_output_.empty()) { - // To ensure we send data in order, don't try to send if we've already - // failed to send something. - buffered_output_.push_back(std::move(event)); - return false; - } - StreamEvent* raw_event = event.release(); - if (!xQueueSend(output_events_, &raw_event, 0)) { - event.reset(raw_event); - buffered_output_.push_back(std::move(event)); - return false; - } - return true; -} - -auto IAudioElement::FlushBufferedOutput() -> bool { - while (!buffered_output_.empty()) { - StreamEvent* raw_event = buffered_output_.front().release(); - buffered_output_.pop_front(); - if (!xQueueSend(output_events_, &raw_event, 0)) { - buffered_output_.emplace_front(raw_event); - return false; - } - } - return true; -} - -} // namespace audio diff --git a/src/audio/audio_playback.cpp b/src/audio/audio_playback.cpp index 613f629c..89139ec4 100644 --- a/src/audio/audio_playback.cpp +++ b/src/audio/audio_playback.cpp @@ -8,6 +8,7 @@ #include "freertos/portmacro.h" #include "audio_decoder.hpp" +#include "audio_element.hpp" #include "audio_task.hpp" #include "chunk.hpp" #include "fatfs_audio_input.hpp" @@ -23,55 +24,30 @@ namespace audio { auto AudioPlayback::create(drivers::GpioExpander* expander) -> cpp::result, Error> { - // Create everything - auto source = std::make_shared(); - auto codec = std::make_shared(); - auto sink_res = I2SAudioOutput::create(expander); if (sink_res.has_error()) { return cpp::fail(ERR_INIT_ELEMENT); } - auto sink = sink_res.value(); - - auto playback = std::make_unique(); - - Pipeline *pipeline = new Pipeline(sink.get()); - pipeline->AddInput(codec.get())->AddInput(source.get()); - - task::Start(pipeline); - - return playback; + return std::make_unique(std::move(sink_res.value())); } -AudioPlayback::AudioPlayback() { - // Create everything - auto source = std::make_shared(); - auto codec = std::make_shared(); +AudioPlayback::AudioPlayback(std::unique_ptr output) + : file_source_(), i2s_output_(std::move(output)) { + AudioDecoder* codec = new AudioDecoder(); + elements_.emplace_back(codec); - auto sink_res = I2SAudioOutput::create(expander); - if (sink_res.has_error()) { - return cpp::fail(ERR_INIT_ELEMENT); - } - auto sink = sink_res.value(); - - auto playback = std::make_unique(); + Pipeline* pipeline = new Pipeline(elements_.front().get()); + pipeline->AddInput(file_source_.get()); - Pipeline *pipeline = new Pipeline(sink.get()); - pipeline->AddInput(codec.get())->AddInput(source.get()); - - task::Start(pipeline); - - return playback; + task::StartPipeline(pipeline, i2s_output_.get()); + task::StartDrain(i2s_output_.get()); } -AudioPlayback::~AudioPlayback() { - pipeline_->Quit(); -} +AudioPlayback::~AudioPlayback() {} auto AudioPlayback::Play(const std::string& filename) -> void { // TODO: concurrency, yo! - file_source->OpenFile(filename); - pipeline_->Play(); + file_source_->OpenFile(filename); } auto AudioPlayback::LogStatus() -> void { diff --git a/src/audio/audio_task.cpp b/src/audio/audio_task.cpp index 542bada8..e6c7778c 100644 --- a/src/audio/audio_task.cpp +++ b/src/audio/audio_task.cpp @@ -8,7 +8,9 @@ #include #include +#include "audio_sink.hpp" #include "cbor.h" +#include "dac.hpp" #include "esp_err.h" #include "esp_heap_caps.h" #include "esp_log.h" @@ -33,22 +35,29 @@ namespace task { static const char* kTag = "task"; static const std::size_t kStackSize = 24 * 1024; +static const std::size_t kDrainStackSize = 1024; static const uint8_t kAudioCore = 0; -auto Start(Pipeline* pipeline) -> Handle* { - auto input_queue = xQueueCreate(8, 1); - +auto StartPipeline(Pipeline* pipeline, IAudioSink* sink) -> void { // Newly created task will free this. - AudioTaskArgs* args = new AudioTaskArgs{ - .pipeline = pipeline, - .input = input_queue, - }; + AudioTaskArgs* args = new AudioTaskArgs{.pipeline = pipeline, .sink = sink}; - ESP_LOGI(kTag, "starting audio task"); + ESP_LOGI(kTag, "starting audio pipeline task"); xTaskCreatePinnedToCore(&AudioTaskMain, "pipeline", kStackSize, args, kTaskPriorityAudio, NULL, kAudioCore); +} - return new Handle(input_queue); +auto StartDrain(IAudioSink* sink) -> void { + auto command = new std::atomic(PLAY); + // Newly created task will free this. + AudioDrainArgs* drain_args = new AudioDrainArgs{ + .sink = sink, + .command = command, + }; + + ESP_LOGI(kTag, "starting audio drain task"); + xTaskCreatePinnedToCore(&AudioDrainMain, "drain", kDrainStackSize, drain_args, + kTaskPriorityAudio, NULL, kAudioCore); } void AudioTaskMain(void* args) { @@ -57,10 +66,11 @@ void AudioTaskMain(void* args) { { AudioTaskArgs* real_args = reinterpret_cast(args); std::unique_ptr pipeline(real_args->pipeline); - QueueHandle_t input; - StreamBufferHandle_t output; + IAudioSink* sink = real_args->sink; delete real_args; + std::optional output_format; + std::vector elements = pipeline->GetIterationOrder(); std::size_t max_inputs = (*std::max_element(elements.begin(), elements.end(), @@ -74,9 +84,7 @@ void AudioTaskMain(void* args) { std::vector> in_regions(max_inputs); MappableRegion out_region; std::for_each(in_regions.begin(), in_regions.end(), - [](const MappableRegion& region) { - assert(region.is_valid); - }); + [](const auto& region) { assert(region.is_valid); }); assert(out_region.is_valid); // Each element has exactly one output buffer. @@ -90,55 +98,68 @@ void AudioTaskMain(void* args) { bool playing = true; bool quit = false; while (!quit) { - // TODO: full event here? - Command cmd; - bool has_cmd = xQueueReceive(input, &cmd, 0); - if (has_cmd) { - switch (cmd) { - case PLAY: - playing = true; - break; - case PAUSE: - playing = false; - break; - case QUIT: - quit = true; - break; - } - } - if (quit) { - break; - } - if (playing) { for (int i = 0; i < elements.size(); i++) { - std::vector in_streams; - elements.at(i)->InStreams(&in_regions, &in_streams); - MutableStream out_stream = elements.at(i)->OutStream(&out_region); + std::vector raw_in_streams; + elements.at(i)->InStreams(&in_regions, &raw_in_streams); + RawStream raw_out_stream = elements.at(i)->OutStream(&out_region); // Crop the input and output streams to the ranges that are safe to // touch. For the input streams, this is the region that contains // data. For the output stream, this is the region that does *not* // already contain data. - std::vector cropped_in_streams; - std::for_each(in_streams.begin(), in_streams.end(), - [&](MutableStream& s) { - cropped_in_streams.emplace_back( - s.info, s.data.first(s.info->bytes_in_stream)); - }); - - elements.at(i)->OutputElement()->Process(&cropped_in_streams, - &out_stream); - - for (int stream = 0; stream < in_streams.size(); stream++) { - MutableStream& orig_stream = in_streams.at(stream); - Stream& cropped_stream = cropped_in_streams.at(stream); - std::move(cropped_stream.data.begin(), cropped_stream.data.end(), - orig_stream.data.begin()); - orig_stream.info->bytes_in_stream = - cropped_stream.data.size_bytes(); + std::vector in_streams; + std::for_each(raw_in_streams.begin(), raw_in_streams.end(), + [&](RawStream& s) { in_streams.emplace_back(&s); }); + OutputStream out_stream(&raw_out_stream); + + elements.at(i)->OutputElement()->Process(in_streams, &out_stream); + } + + RawStream raw_sink_stream = elements.back()->OutStream(&out_region); + InputStream sink_stream(&raw_sink_stream); + + if (!output_format || output_format != sink_stream.info().format) { + // The format of the stream within the sink stream has changed. We + // need to reconfigure the sink, but shouldn't do so until we've fully + // drained the current buffer. + if (xStreamBufferIsEmpty(sink->buffer())) { + output_format = sink_stream.info().format; + sink->Configure(*output_format); } } + + // We've reconfigured the sink, or it was already configured correctly. + // Send through some data. + if (output_format == sink_stream.info().format) { + // TODO: tune the delay on this, as it's currently the only way to + // throttle this task's CPU time. Maybe also hold off on the pipeline + // if the buffer is already close to full? + std::size_t sent = xStreamBufferSend( + sink->buffer(), sink_stream.data().data(), + sink_stream.data().size_bytes(), pdMS_TO_TICKS(10)); + sink_stream.consume(sent); + } + } + } + } + vTaskDelete(NULL); +} + +void AudioDrainMain(void* args) { + { + AudioDrainArgs* real_args = reinterpret_cast(args); + IAudioSink* sink = real_args->sink; + std::atomic* command = real_args->command; + delete real_args; + + // TODO(jacqueline): implement PAUSE without busy-waiting. + while (*command != QUIT) { + std::byte buf[64]; + std::size_t len = + xStreamBufferReceive(sink->buffer(), buf, sizeof(buf), portMAX_DELAY); + if (len > 0) { + sink->Send({buf, len}); } } } diff --git a/src/audio/fatfs_audio_input.cpp b/src/audio/fatfs_audio_input.cpp index bd8748eb..b9882711 100644 --- a/src/audio/fatfs_audio_input.cpp +++ b/src/audio/fatfs_audio_input.cpp @@ -44,26 +44,30 @@ auto FatfsAudioInput::OpenFile(const std::string& path) -> void { is_file_open_ = true; } -auto FatfsAudioInput::Process(std::vector* inputs, - MutableStream* output) -> void { +auto FatfsAudioInput::Process(const std::vector& inputs, + OutputStream* output) -> void { if (!is_file_open_) { return; } + StreamInfo::Format format = StreamInfo::Encoded{codecs::STREAM_MP3}; + if (!output->prepare(format)) { + return; + } + + std::size_t max_size = output->data().size_bytes(); + std::size_t size = 0; FRESULT result = - f_read(¤t_file_, output->data.data(), output->data.size_bytes(), - &output->info->bytes_in_stream); + f_read(¤t_file_, output->data().data(), max_size, &size); if (result != FR_OK) { ESP_LOGE(kTag, "file I/O error %d", result); // TODO(jacqueline): Handle errors. return; } - // TODO: read from filename? - output->info->data = StreamInfo::Encoded{codecs::STREAM_MP3}; + output->add(size); - if (output->info->bytes_in_stream < output->data.size_bytes() || - f_eof(¤t_file_)) { + if (size < max_size || f_eof(¤t_file_)) { f_close(¤t_file_); is_file_open_ = false; } diff --git a/src/audio/i2s_audio_output.cpp b/src/audio/i2s_audio_output.cpp index 7766ebed..55d45001 100644 --- a/src/audio/i2s_audio_output.cpp +++ b/src/audio/i2s_audio_output.cpp @@ -1,6 +1,7 @@ #include "i2s_audio_output.hpp" #include +#include #include #include "esp_err.h" @@ -18,7 +19,7 @@ static const char* kTag = "I2SOUT"; namespace audio { auto I2SAudioOutput::create(drivers::GpioExpander* expander) - -> cpp::result, Error> { + -> cpp::result, Error> { // First, we need to perform initial configuration of the DAC chip. auto dac_result = drivers::AudioDac::create(expander); if (dac_result.has_error()) { @@ -32,7 +33,7 @@ auto I2SAudioOutput::create(drivers::GpioExpander* expander) // dac->WriteVolume(255); dac->WriteVolume(120); // for testing - return std::make_shared(expander, std::move(dac)); + return std::make_unique(expander, std::move(dac)); } I2SAudioOutput::I2SAudioOutput(drivers::GpioExpander* expander, @@ -41,18 +42,18 @@ I2SAudioOutput::I2SAudioOutput(drivers::GpioExpander* expander, I2SAudioOutput::~I2SAudioOutput() {} -auto I2SAudioOutput::ProcessStreamInfo(const StreamInfo& info) -> bool { - if (!std::holds_alternative(info.data)) { +auto I2SAudioOutput::Configure(const StreamInfo::Format& format) -> bool { + if (!std::holds_alternative(format)) { return false; } - StreamInfo::Pcm pcm = std::get(info.data); + StreamInfo::Pcm pcm = std::get(format); if (current_config_ && pcm == *current_config_) { return true; } - ESP_LOGI(kTag, "incoming audio stream: %u bpp @ %u Hz", pcm.bits_per_sample, + ESP_LOGI(kTag, "incoming audio stream: %u bpp @ %lu Hz", pcm.bits_per_sample, pcm.sample_rate); drivers::AudioDac::BitsPerSample bps; @@ -92,14 +93,8 @@ auto I2SAudioOutput::ProcessStreamInfo(const StreamInfo& info) -> bool { return true; } -auto I2SAudioOutput::Process(std::vector* inputs, MutableStream* output) - -> void { - std::for_each(inputs->begin(), inputs->end(), [&](Stream& s) { - if (ProcessStreamInfo(s.info)) { - std::size_t bytes_written = dac_->WriteData(s.data); - s.data = s.data.subspan(bytes_written); - } - }); +auto I2SAudioOutput::Send(const cpp::span& data) -> void { + dac_->WriteData(data); } auto I2SAudioOutput::SetVolume(uint8_t volume) -> void { diff --git a/src/audio/include/audio_decoder.hpp b/src/audio/include/audio_decoder.hpp index be8daf99..6a1b5177 100644 --- a/src/audio/include/audio_decoder.hpp +++ b/src/audio/include/audio_decoder.hpp @@ -24,7 +24,7 @@ class AudioDecoder : public IAudioElement { AudioDecoder(); ~AudioDecoder(); - auto Process(std::vector* inputs, MutableStream* output) + auto Process(const std::vector& inputs, OutputStream* output) -> void override; AudioDecoder(const AudioDecoder&) = delete; @@ -32,11 +32,9 @@ class AudioDecoder : public IAudioElement { private: std::unique_ptr current_codec_; - std::optional stream_info_; - - bool has_set_stream_info_; + std::optional current_input_format_; + std::optional current_output_format_; bool has_samples_to_send_; - bool needs_more_input_; auto ProcessStreamInfo(const StreamInfo& info) -> bool; }; diff --git a/src/audio/include/audio_element.hpp b/src/audio/include/audio_element.hpp index c9192e4a..5884f7b2 100644 --- a/src/audio/include/audio_element.hpp +++ b/src/audio/include/audio_element.hpp @@ -37,11 +37,11 @@ static const size_t kEventQueueSize = 8; */ class IAudioElement { public: - IAudioElement(); - virtual ~IAudioElement(); + IAudioElement() {} + virtual ~IAudioElement() {} - virtual auto Process(std::vector* inputs, MutableStream* output) - -> void = 0; + virtual auto Process(const std::vector& inputs, + OutputStream* output) -> void = 0; }; } // namespace audio diff --git a/src/audio/include/audio_playback.hpp b/src/audio/include/audio_playback.hpp index 507e6f73..88dc29aa 100644 --- a/src/audio/include/audio_playback.hpp +++ b/src/audio/include/audio_playback.hpp @@ -8,6 +8,7 @@ #include "audio_task.hpp" #include "esp_err.h" #include "fatfs_audio_input.hpp" +#include "i2s_audio_output.hpp" #include "result.hpp" #include "span.hpp" @@ -28,7 +29,7 @@ class AudioPlayback { static auto create(drivers::GpioExpander* expander) -> cpp::result, Error>; - AudioPlayback(FatfsAudioInput *file_input); + explicit AudioPlayback(std::unique_ptr output); ~AudioPlayback(); /* @@ -44,10 +45,9 @@ class AudioPlayback { AudioPlayback& operator=(const AudioPlayback&) = delete; private: - FatfsAudioInput *file_source; - - std::vector> all_elements_; - std::unique_ptr pipeline_; + std::unique_ptr file_source_; + std::unique_ptr i2s_output_; + std::vector> elements_; }; } // namespace audio diff --git a/src/audio/include/audio_sink.hpp b/src/audio/include/audio_sink.hpp new file mode 100644 index 00000000..ed7eb02b --- /dev/null +++ b/src/audio/include/audio_sink.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "audio_element.hpp" +#include "stream_info.hpp" +namespace audio { + +class IAudioSink { + private: + static const std::size_t kDrainBufferSize = 8 * 1024; + StreamBufferHandle_t buffer_; + + public: + IAudioSink() : buffer_(xStreamBufferCreate(kDrainBufferSize, 1)) {} + virtual ~IAudioSink() { vStreamBufferDelete(buffer_); } + + virtual auto Configure(const StreamInfo::Format& format) -> bool = 0; + virtual auto Send(const cpp::span& data) -> void = 0; + + auto buffer() const -> StreamBufferHandle_t { return buffer_; } +}; + +} // namespace audio diff --git a/src/audio/include/audio_task.hpp b/src/audio/include/audio_task.hpp index 8db99850..a7b7a0fa 100644 --- a/src/audio/include/audio_task.hpp +++ b/src/audio/include/audio_task.hpp @@ -5,38 +5,32 @@ #include #include "audio_element.hpp" +#include "audio_sink.hpp" +#include "dac.hpp" #include "freertos/portmacro.h" #include "pipeline.hpp" +#include "stream_buffer.hpp" namespace audio { namespace task { + +enum Command { PLAY, PAUSE, QUIT }; + struct AudioTaskArgs { Pipeline* pipeline; - QueueHandle_t input; + IAudioSink* sink; +}; +struct AudioDrainArgs { + IAudioSink* sink; + std::atomic* command; }; extern "C" void AudioTaskMain(void* args); +extern "C" void AudioDrainMain(void* args); -enum Command { PLAY, PAUSE, QUIT }; - -class Handle { - public: - explicit Handle(QueueHandle_t input); - ~Handle(); - - auto SetStreamInfo() -> void; - auto Play() -> void; - auto Pause() -> void; - auto Quit() -> void; - - auto OutputBuffer() -> StreamBufferHandle_t; - - private: - QueueHandle_t input; -}; - -auto Start(Pipeline* pipeline) -> Handle*; +auto StartPipeline(Pipeline* pipeline, IAudioSink* sink) -> void; +auto StartDrain(IAudioSink* sink) -> void; } // namespace task diff --git a/src/audio/include/fatfs_audio_input.hpp b/src/audio/include/fatfs_audio_input.hpp index b3a6d843..24f62e3c 100644 --- a/src/audio/include/fatfs_audio_input.hpp +++ b/src/audio/include/fatfs_audio_input.hpp @@ -16,6 +16,7 @@ #include "audio_element.hpp" #include "stream_buffer.hpp" +#include "stream_info.hpp" namespace audio { @@ -26,7 +27,7 @@ class FatfsAudioInput : public IAudioElement { auto OpenFile(const std::string& path) -> void; - auto Process(std::vector* inputs, MutableStream* output) + auto Process(const std::vector& inputs, OutputStream* output) -> void override; FatfsAudioInput(const FatfsAudioInput&) = delete; diff --git a/src/audio/include/i2s_audio_output.hpp b/src/audio/include/i2s_audio_output.hpp index 57881b35..77019228 100644 --- a/src/audio/include/i2s_audio_output.hpp +++ b/src/audio/include/i2s_audio_output.hpp @@ -5,6 +5,7 @@ #include #include "audio_element.hpp" +#include "audio_sink.hpp" #include "chunk.hpp" #include "result.hpp" @@ -14,18 +15,18 @@ namespace audio { -class I2SAudioOutput : public IAudioElement { +class I2SAudioOutput : public IAudioSink { public: enum Error { DAC_CONFIG, I2S_CONFIG, STREAM_INIT }; static auto create(drivers::GpioExpander* expander) - -> cpp::result, Error>; + -> cpp::result, Error>; I2SAudioOutput(drivers::GpioExpander* expander, std::unique_ptr dac); ~I2SAudioOutput(); - auto Process(std::vector* inputs, MutableStream* output) - -> void override; + auto Configure(const StreamInfo::Format& format) -> bool override; + auto Send(const cpp::span& data) -> void override; I2SAudioOutput(const I2SAudioOutput&) = delete; I2SAudioOutput& operator=(const I2SAudioOutput&) = delete; @@ -37,8 +38,6 @@ class I2SAudioOutput : public IAudioElement { std::unique_ptr dac_; std::optional current_config_; - - auto ProcessStreamInfo(const StreamInfo& info) -> bool; }; } // namespace audio diff --git a/src/audio/include/pipeline.hpp b/src/audio/include/pipeline.hpp index 42f70828..2e9247bc 100644 --- a/src/audio/include/pipeline.hpp +++ b/src/audio/include/pipeline.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "freertos/portmacro.h" @@ -16,7 +17,7 @@ static const std::size_t kPipelineBufferSize = 32 * 1024; class Pipeline { public: - Pipeline(IAudioElement* output); + explicit Pipeline(IAudioElement* output); ~Pipeline(); auto AddInput(IAudioElement* input) -> Pipeline*; @@ -25,9 +26,9 @@ class Pipeline { auto NumInputs() const -> std::size_t; auto InStreams(std::vector>*, - std::vector*) -> void; + std::vector*) -> void; - auto OutStream(MappableRegion*) -> MutableStream; + auto OutStream(MappableRegion*) -> RawStream; auto GetIterationOrder() -> std::vector; diff --git a/src/audio/include/stream_info.hpp b/src/audio/include/stream_info.hpp index 47f65649..5622517f 100644 --- a/src/audio/include/stream_info.hpp +++ b/src/audio/include/stream_info.hpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include "result.hpp" @@ -35,37 +37,82 @@ struct StreamInfo { // Number of bits per sample. uint8_t bits_per_sample; // The sample rate. - uint16_t sample_rate; + uint32_t sample_rate; bool operator==(const Pcm&) const = default; }; - std::variant data; + typedef std::variant Format; + Format format; bool operator==(const StreamInfo&) const = default; }; -class MutableStream { +class RawStream { public: StreamInfo* info; cpp::span data; + bool is_incomplete; - MutableStream(StreamInfo* i, cpp::span d) - : info(i), data(d) {} + RawStream(StreamInfo* i, cpp::span d) + : info(i), data(d), is_incomplete(false) {} }; /* * A byte buffer + associated metadata, which is not allowed to modify any of * the underlying data. */ -class Stream { +class InputStream { public: - explicit Stream(const MutableStream& s) : info(*s.info), data(s.data) {} + explicit InputStream(RawStream* s) : raw_(s) {} - const StreamInfo& info; - // `data` itself left mutable for signalling how much of the stream was - // consumed - cpp::span data; + void consume(std::size_t bytes) const { + auto new_data = raw_->data.subspan(bytes); + std::move(new_data.begin(), new_data.end(), raw_->data.begin()); + raw_->info->bytes_in_stream = new_data.size_bytes(); + } + + void mark_incomplete() const { raw_->is_incomplete = true; } + + const StreamInfo& info() const { return *raw_->info; } + + cpp::span data() const { + return raw_->data.first(raw_->info->bytes_in_stream); + } + + private: + RawStream* raw_; +}; + +class OutputStream { + public: + explicit OutputStream(RawStream* s) : raw_(s) {} + + void add(std::size_t bytes) const { raw_->info->bytes_in_stream += bytes; } + + bool prepare(const StreamInfo::Format& new_format) { + if (new_format == raw_->info->format) { + raw_->info->format = new_format; + return true; + } + if (raw_->is_incomplete) { + raw_->info->format = new_format; + raw_->info->bytes_in_stream = 0; + return true; + } + return false; + } + + const StreamInfo& info() const { return *raw_->info; } + + cpp::span data() const { + return raw_->data.subspan(raw_->info->bytes_in_stream); + } + + bool is_incomplete() const { return raw_->is_incomplete; } + + private: + RawStream* raw_; }; } // namespace audio diff --git a/src/audio/pipeline.cpp b/src/audio/pipeline.cpp index f42e6853..8af8f215 100644 --- a/src/audio/pipeline.cpp +++ b/src/audio/pipeline.cpp @@ -1,4 +1,5 @@ #include "pipeline.hpp" +#include #include "stream_info.hpp" namespace audio { @@ -7,7 +8,7 @@ Pipeline::Pipeline(IAudioElement* output) : root_(output), subtrees_() {} Pipeline::~Pipeline() {} auto Pipeline::AddInput(IAudioElement* input) -> Pipeline* { - subtrees_.emplace_back(input); + subtrees_.push_back(std::make_unique(input)); return subtrees_.back().get(); } @@ -21,15 +22,15 @@ auto Pipeline::NumInputs() const -> std::size_t { auto Pipeline::InStreams( std::vector>* regions, - std::vector* out) -> void { + std::vector* out) -> void { for (int i = 0; i < subtrees_.size(); i++) { - MutableStream s = subtrees_[i]->OutStream(®ions->at(i)); + RawStream s = subtrees_[i]->OutStream(®ions->at(i)); out->push_back(s); } } auto Pipeline::OutStream(MappableRegion* region) - -> MutableStream { + -> RawStream { return {&output_info_, region->Map(output_buffer_)}; } @@ -42,8 +43,9 @@ auto Pipeline::GetIterationOrder() -> std::vector { to_search.pop_back(); found.push_back(current); - to_search.insert(to_search.end(), current->subtrees_.begin(), - current->subtrees_.end()); + for (const auto& i : current->subtrees_) { + to_search.push_back(i.get()); + } } return found; diff --git a/src/codecs/codec.cpp b/src/codecs/codec.cpp index 4e9a6a47..bdc8f51e 100644 --- a/src/codecs/codec.cpp +++ b/src/codecs/codec.cpp @@ -5,7 +5,7 @@ namespace codecs { -auto CreateCodecForFile(const std::string& file) +auto CreateCodecForType(StreamType type) -> cpp::result, CreateCodecError> { return std::make_unique(); // TODO. } diff --git a/src/codecs/include/mad.hpp b/src/codecs/include/mad.hpp index 1f5791b9..074784fb 100644 --- a/src/codecs/include/mad.hpp +++ b/src/codecs/include/mad.hpp @@ -17,10 +17,10 @@ class MadMp3Decoder : public ICodec { MadMp3Decoder(); ~MadMp3Decoder(); - auto CanHandleFile(const std::string& path) -> bool override; + auto CanHandleType(StreamType type) -> bool override; auto GetOutputFormat() -> OutputFormat override; auto ResetForNewStream() -> void override; - auto SetInput(cpp::span input) -> void override; + auto SetInput(cpp::span input) -> void override; auto GetInputPosition() -> std::size_t override; auto ProcessNextFrame() -> cpp::result override; auto WriteOutputSamples(cpp::span output) diff --git a/src/codecs/mad.cpp b/src/codecs/mad.cpp index 1112bd62..eec2d633 100644 --- a/src/codecs/mad.cpp +++ b/src/codecs/mad.cpp @@ -5,6 +5,7 @@ #include "mad.h" #include "codec.hpp" +#include "types.hpp" namespace codecs { @@ -35,8 +36,8 @@ MadMp3Decoder::~MadMp3Decoder() { mad_header_finish(&header_); } -auto MadMp3Decoder::CanHandleFile(const std::string& path) -> bool { - return true; // TODO. +auto MadMp3Decoder::CanHandleType(StreamType type) -> bool { + return type == STREAM_MP3; } auto MadMp3Decoder::GetOutputFormat() -> OutputFormat { @@ -52,7 +53,7 @@ auto MadMp3Decoder::ResetForNewStream() -> void { has_decoded_header_ = false; } -auto MadMp3Decoder::SetInput(cpp::span input) -> void { +auto MadMp3Decoder::SetInput(cpp::span input) -> void { mad_stream_buffer(&stream_, reinterpret_cast(input.data()), input.size()); 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; diff --git a/src/main/main.cpp b/src/main/main.cpp index a30dc675..98a571b6 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -133,10 +133,10 @@ extern "C" void app_main(void) { (void*)lvglArgs, 1, sLvglStack, &sLvglTaskBuffer, 1); - std::shared_ptr playback; + std::unique_ptr playback; if (storage) { ESP_LOGI(TAG, "Init audio pipeline"); - auto playback_res = audio::AudioPlayback::create(expander, storage); + auto playback_res = audio::AudioPlayback::create(expander); if (playback_res.has_error()) { ESP_LOGE(TAG, "Failed! Playback will not work."); } else { diff --git a/src/memory/include/himem.hpp b/src/memory/include/himem.hpp index c65091d7..f71e912f 100644 --- a/src/memory/include/himem.hpp +++ b/src/memory/include/himem.hpp @@ -63,7 +63,7 @@ class MappableRegion { return {bytes_, size}; } - auto Map(const HimemAlloc &alloc) -> cpp::span { + auto Map(const HimemAlloc& alloc) -> cpp::span { if (bytes_ != nullptr) { ESP_ERROR_CHECK(esp_himem_unmap(range_handle, bytes_, size)); } From 3836768bb8b95188e6657ab69027d1d9e4b13a77 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 3 Apr 2023 14:06:30 +1000 Subject: [PATCH 16/31] new pipeline working(?), but the dac eludes me --- src/audio/audio_decoder.cpp | 10 ++++ src/audio/audio_playback.cpp | 5 +- src/audio/audio_task.cpp | 20 +++++++- src/audio/fatfs_audio_input.cpp | 3 ++ src/audio/i2s_audio_output.cpp | 6 +++ src/audio/include/audio_sink.hpp | 1 + src/audio/include/i2s_audio_output.hpp | 1 + src/audio/include/stream_info.hpp | 13 +++-- src/audio/pipeline.cpp | 5 +- src/drivers/dac.cpp | 66 +++++++++++++++++--------- src/drivers/include/dac.hpp | 5 ++ src/main/main.cpp | 2 +- src/memory/include/himem.hpp | 13 +++-- 13 files changed, 112 insertions(+), 38 deletions(-) diff --git a/src/audio/audio_decoder.cpp b/src/audio/audio_decoder.cpp index ada1f8f7..03c7e998 100644 --- a/src/audio/audio_decoder.cpp +++ b/src/audio/audio_decoder.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "cbor/tinycbor/src/cborinternal_p.h" #include "freertos/FreeRTOS.h" @@ -37,6 +38,7 @@ auto AudioDecoder::ProcessStreamInfo(const StreamInfo& info) -> bool { if (!std::holds_alternative(info.format)) { return false; } + ESP_LOGI(kTag, "got new stream"); const auto& encoded = std::get(info.format); // Reuse the existing codec if we can. This will help with gapless playback, @@ -45,12 +47,14 @@ auto AudioDecoder::ProcessStreamInfo(const StreamInfo& info) -> bool { if (current_codec_ != nullptr && current_codec_->CanHandleType(encoded.type)) { current_codec_->ResetForNewStream(); + ESP_LOGI(kTag, "reusing existing decoder"); return true; } // TODO: use audio type from stream auto result = codecs::CreateCodecForType(encoded.type); if (result.has_value()) { + ESP_LOGI(kTag, "creating new decoder"); current_codec_ = std::move(result.value()); } else { ESP_LOGE(kTag, "no codec for this file"); @@ -73,6 +77,9 @@ auto AudioDecoder::Process(const std::vector& inputs, } const StreamInfo& info = input->info(); + if (std::holds_alternative(info.format)) { + return; + } if (!current_input_format_ || *current_input_format_ != info.format) { // The input stream has changed! Immediately throw everything away and // start from scratch. @@ -100,6 +107,9 @@ auto AudioDecoder::Process(const std::vector& inputs, } auto write_res = current_codec_->WriteOutputSamples(output->data()); + if (write_res.first > 0) { + ESP_LOGI(kTag, "wrote %u bytes of samples", write_res.first); + } output->add(write_res.first); has_samples_to_send_ = !write_res.second; diff --git a/src/audio/audio_playback.cpp b/src/audio/audio_playback.cpp index 89139ec4..9a978535 100644 --- a/src/audio/audio_playback.cpp +++ b/src/audio/audio_playback.cpp @@ -32,7 +32,8 @@ auto AudioPlayback::create(drivers::GpioExpander* expander) } AudioPlayback::AudioPlayback(std::unique_ptr output) - : file_source_(), i2s_output_(std::move(output)) { + : file_source_(std::make_unique()), + i2s_output_(std::move(output)) { AudioDecoder* codec = new AudioDecoder(); elements_.emplace_back(codec); @@ -51,7 +52,7 @@ auto AudioPlayback::Play(const std::string& filename) -> void { } auto AudioPlayback::LogStatus() -> void { - // TODO. + i2s_output_->Log(); } } // namespace audio diff --git a/src/audio/audio_task.cpp b/src/audio/audio_task.cpp index e6c7778c..eb33611b 100644 --- a/src/audio/audio_task.cpp +++ b/src/audio/audio_task.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "audio_sink.hpp" #include "cbor.h" @@ -114,16 +115,29 @@ void AudioTaskMain(void* args) { OutputStream out_stream(&raw_out_stream); elements.at(i)->OutputElement()->Process(in_streams, &out_stream); + + std::for_each(in_regions.begin(), in_regions.end(), + [](auto&& r) { r.Unmap(); }); + out_region.Unmap(); } - RawStream raw_sink_stream = elements.back()->OutStream(&out_region); + RawStream raw_sink_stream = elements.front()->OutStream(&out_region); InputStream sink_stream(&raw_sink_stream); - if (!output_format || output_format != sink_stream.info().format) { + if (sink_stream.data().size_bytes() == 0) { + out_region.Unmap(); + vTaskDelay(pdMS_TO_TICKS(100)); + continue; + } + + if ((!output_format || output_format != sink_stream.info().format) && + !std::holds_alternative( + sink_stream.info().format)) { // The format of the stream within the sink stream has changed. We // need to reconfigure the sink, but shouldn't do so until we've fully // drained the current buffer. if (xStreamBufferIsEmpty(sink->buffer())) { + ESP_LOGI(kTag, "reconfiguring dac"); output_format = sink_stream.info().format; sink->Configure(*output_format); } @@ -140,6 +154,8 @@ void AudioTaskMain(void* args) { sink_stream.data().size_bytes(), pdMS_TO_TICKS(10)); sink_stream.consume(sent); } + + out_region.Unmap(); } } } diff --git a/src/audio/fatfs_audio_input.cpp b/src/audio/fatfs_audio_input.cpp index b9882711..240f7084 100644 --- a/src/audio/fatfs_audio_input.cpp +++ b/src/audio/fatfs_audio_input.cpp @@ -65,6 +65,9 @@ auto FatfsAudioInput::Process(const std::vector& inputs, return; } + if (size > 0) { + ESP_LOGI(kTag, "read %u bytes", size); + } output->add(size); if (size < max_size || f_eof(¤t_file_)) { diff --git a/src/audio/i2s_audio_output.cpp b/src/audio/i2s_audio_output.cpp index 55d45001..2d336152 100644 --- a/src/audio/i2s_audio_output.cpp +++ b/src/audio/i2s_audio_output.cpp @@ -44,12 +44,14 @@ I2SAudioOutput::~I2SAudioOutput() {} auto I2SAudioOutput::Configure(const StreamInfo::Format& format) -> bool { if (!std::holds_alternative(format)) { + ESP_LOGI(kTag, "ignoring non-pcm stream (%d)", format.index()); return false; } StreamInfo::Pcm pcm = std::get(format); if (current_config_ && pcm == *current_config_) { + ESP_LOGI(kTag, "ignoring unchanged format"); return true; } @@ -97,6 +99,10 @@ auto I2SAudioOutput::Send(const cpp::span& data) -> void { dac_->WriteData(data); } +auto I2SAudioOutput::Log() -> void { + dac_->LogStatus(); +} + auto I2SAudioOutput::SetVolume(uint8_t volume) -> void { dac_->WriteVolume(volume); } diff --git a/src/audio/include/audio_sink.hpp b/src/audio/include/audio_sink.hpp index ed7eb02b..ad63ec2e 100644 --- a/src/audio/include/audio_sink.hpp +++ b/src/audio/include/audio_sink.hpp @@ -15,6 +15,7 @@ class IAudioSink { virtual auto Configure(const StreamInfo::Format& format) -> bool = 0; virtual auto Send(const cpp::span& data) -> void = 0; + virtual auto Log() -> void {} auto buffer() const -> StreamBufferHandle_t { return buffer_; } }; diff --git a/src/audio/include/i2s_audio_output.hpp b/src/audio/include/i2s_audio_output.hpp index 77019228..31510a91 100644 --- a/src/audio/include/i2s_audio_output.hpp +++ b/src/audio/include/i2s_audio_output.hpp @@ -27,6 +27,7 @@ class I2SAudioOutput : public IAudioSink { auto Configure(const StreamInfo::Format& format) -> bool override; auto Send(const cpp::span& data) -> void override; + auto Log() -> void override; I2SAudioOutput(const I2SAudioOutput&) = delete; I2SAudioOutput& operator=(const I2SAudioOutput&) = delete; diff --git a/src/audio/include/stream_info.hpp b/src/audio/include/stream_info.hpp index 5622517f..5a36384c 100644 --- a/src/audio/include/stream_info.hpp +++ b/src/audio/include/stream_info.hpp @@ -17,12 +17,12 @@ namespace audio { struct StreamInfo { // The number of bytes that are available for consumption within this // stream's buffer. - std::size_t bytes_in_stream; + std::size_t bytes_in_stream{0}; // The total length of this stream, in case its source is finite (e.g. a // file on disk). May be absent for endless streams (internet streams, // generated audio, etc.) - std::optional length_bytes; + std::optional length_bytes{}; struct Encoded { // The codec that this stream is associated with. @@ -42,8 +42,8 @@ struct StreamInfo { bool operator==(const Pcm&) const = default; }; - typedef std::variant Format; - Format format; + typedef std::variant Format; + Format format{}; bool operator==(const StreamInfo&) const = default; }; @@ -91,8 +91,11 @@ class OutputStream { void add(std::size_t bytes) const { raw_->info->bytes_in_stream += bytes; } bool prepare(const StreamInfo::Format& new_format) { - if (new_format == raw_->info->format) { + if (std::holds_alternative(raw_->info->format)) { raw_->info->format = new_format; + raw_->info->bytes_in_stream = 0; + } + if (new_format == raw_->info->format) { return true; } if (raw_->is_incomplete) { diff --git a/src/audio/pipeline.cpp b/src/audio/pipeline.cpp index 8af8f215..bab2f3ff 100644 --- a/src/audio/pipeline.cpp +++ b/src/audio/pipeline.cpp @@ -4,7 +4,10 @@ namespace audio { -Pipeline::Pipeline(IAudioElement* output) : root_(output), subtrees_() {} +Pipeline::Pipeline(IAudioElement* output) : root_(output), subtrees_() { + assert(output != nullptr); +} + Pipeline::~Pipeline() {} auto Pipeline::AddInput(IAudioElement* input) -> Pipeline* { 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, diff --git a/src/main/main.cpp b/src/main/main.cpp index 98a571b6..77a1e14b 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -41,7 +41,7 @@ static const char* TAG = "MAIN"; void IRAM_ATTR tick_hook(void) { - lv_tick_inc(1); + // lv_tick_inc(1); } static const size_t kLvglStackSize = 8 * 1024; diff --git a/src/memory/include/himem.hpp b/src/memory/include/himem.hpp index f71e912f..517ebfdf 100644 --- a/src/memory/include/himem.hpp +++ b/src/memory/include/himem.hpp @@ -57,21 +57,26 @@ class MappableRegion { } auto Get() -> cpp::span { - if (bytes_ != nullptr) { + if (bytes_ == nullptr) { return {}; } return {bytes_, size}; } auto Map(const HimemAlloc& alloc) -> cpp::span { - if (bytes_ != nullptr) { - ESP_ERROR_CHECK(esp_himem_unmap(range_handle, bytes_, size)); - } + assert(bytes_ == nullptr); ESP_ERROR_CHECK(esp_himem_map(alloc.handle, range_handle, 0, 0, size, 0, reinterpret_cast(&bytes_))); return Get(); } + auto Unmap() -> void { + if (bytes_ != nullptr) { + ESP_ERROR_CHECK(esp_himem_unmap(range_handle, bytes_, size)); + bytes_ = nullptr; + } + } + // Not copyable or movable. MappableRegion(const MappableRegion&) = delete; MappableRegion& operator=(const MappableRegion&) = delete; From 40a9734b04c48339cfdf6ed9043aa3f6f0dda62d Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 4 Apr 2023 09:46:52 +1000 Subject: [PATCH 17/31] Redo pcm registers to include pages --- src/audio/audio_decoder.cpp | 3 - src/audio/audio_task.cpp | 9 +-- src/audio/fatfs_audio_input.cpp | 3 - src/codecs/mad.cpp | 2 +- src/drivers/dac.cpp | 117 ++++++++++++++++---------------- src/drivers/include/dac.hpp | 117 +++++++++++++++++++++++++------- 6 files changed, 155 insertions(+), 96 deletions(-) diff --git a/src/audio/audio_decoder.cpp b/src/audio/audio_decoder.cpp index 03c7e998..b829f959 100644 --- a/src/audio/audio_decoder.cpp +++ b/src/audio/audio_decoder.cpp @@ -107,9 +107,6 @@ auto AudioDecoder::Process(const std::vector& inputs, } auto write_res = current_codec_->WriteOutputSamples(output->data()); - if (write_res.first > 0) { - ESP_LOGI(kTag, "wrote %u bytes of samples", write_res.first); - } output->add(write_res.first); has_samples_to_send_ = !write_res.second; diff --git a/src/audio/audio_task.cpp b/src/audio/audio_task.cpp index eb33611b..babe0a97 100644 --- a/src/audio/audio_task.cpp +++ b/src/audio/audio_task.cpp @@ -162,6 +162,8 @@ void AudioTaskMain(void* args) { vTaskDelete(NULL); } +static std::byte sDrainBuf[1024]; + void AudioDrainMain(void* args) { { AudioDrainArgs* real_args = reinterpret_cast(args); @@ -171,11 +173,10 @@ void AudioDrainMain(void* args) { // TODO(jacqueline): implement PAUSE without busy-waiting. while (*command != QUIT) { - std::byte buf[64]; - std::size_t len = - xStreamBufferReceive(sink->buffer(), buf, sizeof(buf), portMAX_DELAY); + std::size_t len = xStreamBufferReceive(sink->buffer(), sDrainBuf, + sizeof(sDrainBuf), portMAX_DELAY); if (len > 0) { - sink->Send({buf, len}); + sink->Send({sDrainBuf, len}); } } } diff --git a/src/audio/fatfs_audio_input.cpp b/src/audio/fatfs_audio_input.cpp index 240f7084..b9882711 100644 --- a/src/audio/fatfs_audio_input.cpp +++ b/src/audio/fatfs_audio_input.cpp @@ -65,9 +65,6 @@ auto FatfsAudioInput::Process(const std::vector& inputs, return; } - if (size > 0) { - ESP_LOGI(kTag, "read %u bytes", size); - } output->add(size); if (size < max_size || f_eof(¤t_file_)) { diff --git a/src/codecs/mad.cpp b/src/codecs/mad.cpp index eec2d633..020e0d57 100644 --- a/src/codecs/mad.cpp +++ b/src/codecs/mad.cpp @@ -114,7 +114,7 @@ auto MadMp3Decoder::WriteOutputSamples(cpp::span output) } while (current_sample_ < synth_.pcm.length) { - if (output_byte + (3 * synth_.pcm.channels) >= output.size()) { + if (output_byte + (2 * synth_.pcm.channels) >= output.size()) { return std::make_pair(output_byte, false); } 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 From 7a54ff0df9c18b662e5bdc11ac2e26ff052cfa4d Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 4 Apr 2023 14:12:01 +1000 Subject: [PATCH 18/31] WIP track down new pipeline memory issues --- src/audio/audio_decoder.cpp | 18 ++++++------------ src/audio/audio_task.cpp | 6 ++---- src/audio/fatfs_audio_input.cpp | 2 ++ src/drivers/include/dac.hpp | 2 +- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/audio/audio_decoder.cpp b/src/audio/audio_decoder.cpp index b829f959..faaadb3e 100644 --- a/src/audio/audio_decoder.cpp +++ b/src/audio/audio_decoder.cpp @@ -66,20 +66,13 @@ auto AudioDecoder::ProcessStreamInfo(const StreamInfo& info) -> bool { auto AudioDecoder::Process(const std::vector& inputs, OutputStream* output) -> void { - // We don't really expect multiple inputs, so just pick the first that - // contains data. If none of them contain data, then we can still flush - // pending samples. - auto input = std::find_if( - inputs.begin(), inputs.end(), - [](const InputStream& s) { return s.data().size_bytes() > 0; }); - if (input == inputs.end()) { - input = inputs.begin(); - } - + auto input = inputs.begin(); const StreamInfo& info = input->info(); - if (std::holds_alternative(info.format)) { + if (std::holds_alternative(info.format) || info.bytes_in_stream == 0) { + output->prepare({}); return; } + if (!current_input_format_ || *current_input_format_ != info.format) { // The input stream has changed! Immediately throw everything away and // start from scratch. @@ -133,7 +126,8 @@ auto AudioDecoder::Process(const std::vector& inputs, } } - input->consume(current_codec_->GetInputPosition()); + ESP_LOGI(kTag, "decoded %u bytes", current_codec_->GetInputPosition() - 1); + input->consume(current_codec_->GetInputPosition() - 1); } } // namespace audio diff --git a/src/audio/audio_task.cpp b/src/audio/audio_task.cpp index babe0a97..464879d8 100644 --- a/src/audio/audio_task.cpp +++ b/src/audio/audio_task.cpp @@ -130,9 +130,7 @@ void AudioTaskMain(void* args) { continue; } - if ((!output_format || output_format != sink_stream.info().format) && - !std::holds_alternative( - sink_stream.info().format)) { + if (!output_format || output_format != sink_stream.info().format) { // The format of the stream within the sink stream has changed. We // need to reconfigure the sink, but shouldn't do so until we've fully // drained the current buffer. @@ -145,7 +143,7 @@ void AudioTaskMain(void* args) { // We've reconfigured the sink, or it was already configured correctly. // Send through some data. - if (output_format == sink_stream.info().format) { + if (output_format == sink_stream.info().format && !std::holds_alternative(*output_format)) { // TODO: tune the delay on this, as it's currently the only way to // throttle this task's CPU time. Maybe also hold off on the pipeline // if the buffer is already close to full? diff --git a/src/audio/fatfs_audio_input.cpp b/src/audio/fatfs_audio_input.cpp index b9882711..b4e6db75 100644 --- a/src/audio/fatfs_audio_input.cpp +++ b/src/audio/fatfs_audio_input.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "arena.hpp" #include "esp_heap_caps.h" @@ -47,6 +48,7 @@ auto FatfsAudioInput::OpenFile(const std::string& path) -> void { auto FatfsAudioInput::Process(const std::vector& inputs, OutputStream* output) -> void { if (!is_file_open_) { + output->prepare({}); return; } 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); From 25c5896dc5c239e3a175c6c8fc5d7368946adeb6 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 6 Apr 2023 16:19:02 +1000 Subject: [PATCH 19/31] add some bounds checking asserts --- src/audio/include/stream_info.hpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/audio/include/stream_info.hpp b/src/audio/include/stream_info.hpp index 5a36384c..6256f2ee 100644 --- a/src/audio/include/stream_info.hpp +++ b/src/audio/include/stream_info.hpp @@ -67,6 +67,7 @@ class InputStream { explicit InputStream(RawStream* s) : raw_(s) {} void consume(std::size_t bytes) const { + assert(raw_->info->bytes_in_stream >= bytes); auto new_data = raw_->data.subspan(bytes); std::move(new_data.begin(), new_data.end(), raw_->data.begin()); raw_->info->bytes_in_stream = new_data.size_bytes(); @@ -88,7 +89,10 @@ class OutputStream { public: explicit OutputStream(RawStream* s) : raw_(s) {} - void add(std::size_t bytes) const { raw_->info->bytes_in_stream += bytes; } + void add(std::size_t bytes) const { + assert(raw_->info->bytes_in_stream + bytes <= raw_->data.size_bytes()); + raw_->info->bytes_in_stream += bytes; + } bool prepare(const StreamInfo::Format& new_format) { if (std::holds_alternative(raw_->info->format)) { From 561f9d2a07ee6ee1c2f18dc375125f87ea7b0d55 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 19 Apr 2023 13:00:42 +1000 Subject: [PATCH 20/31] Ensure the sink buffer is large enough to not fully drain during playback --- src/audio/CMakeLists.txt | 2 +- src/audio/audio_decoder.cpp | 7 +++-- src/audio/audio_task.cpp | 7 +++-- src/audio/fatfs_audio_input.cpp | 3 +- src/audio/include/audio_sink.hpp | 3 +- src/audio/include/stream_info.hpp | 52 ++++++++----------------------- src/drivers/dac.cpp | 10 +++--- 7 files changed, 31 insertions(+), 53 deletions(-) diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt index 34fcd8ee..6361f827 100644 --- a/src/audio/CMakeLists.txt +++ b/src/audio/CMakeLists.txt @@ -1,7 +1,7 @@ idf_component_register( SRCS "audio_decoder.cpp" "audio_task.cpp" "chunk.cpp" "fatfs_audio_input.cpp" "stream_message.cpp" "i2s_audio_output.cpp" "stream_buffer.cpp" - "audio_playback.cpp" "stream_event.cpp" "pipeline.cpp" + "audio_playback.cpp" "stream_event.cpp" "pipeline.cpp" "stream_info.cpp" INCLUDE_DIRS "include" REQUIRES "codecs" "drivers" "cbor" "result" "tasks" "span" "memory") diff --git a/src/audio/audio_decoder.cpp b/src/audio/audio_decoder.cpp index faaadb3e..af9abb94 100644 --- a/src/audio/audio_decoder.cpp +++ b/src/audio/audio_decoder.cpp @@ -68,8 +68,10 @@ auto AudioDecoder::Process(const std::vector& inputs, OutputStream* output) -> void { auto input = inputs.begin(); const StreamInfo& info = input->info(); - if (std::holds_alternative(info.format) || info.bytes_in_stream == 0) { - output->prepare({}); + if (std::holds_alternative(info.format) || + info.bytes_in_stream == 0) { + // TODO(jacqueline): should we clear the stream format? + // output->prepare({}); return; } @@ -126,7 +128,6 @@ auto AudioDecoder::Process(const std::vector& inputs, } } - ESP_LOGI(kTag, "decoded %u bytes", current_codec_->GetInputPosition() - 1); input->consume(current_codec_->GetInputPosition() - 1); } diff --git a/src/audio/audio_task.cpp b/src/audio/audio_task.cpp index 464879d8..3a2a5941 100644 --- a/src/audio/audio_task.cpp +++ b/src/audio/audio_task.cpp @@ -124,7 +124,7 @@ void AudioTaskMain(void* args) { RawStream raw_sink_stream = elements.front()->OutStream(&out_region); InputStream sink_stream(&raw_sink_stream); - if (sink_stream.data().size_bytes() == 0) { + if (sink_stream.info().bytes_in_stream == 0) { out_region.Unmap(); vTaskDelay(pdMS_TO_TICKS(100)); continue; @@ -143,7 +143,8 @@ void AudioTaskMain(void* args) { // We've reconfigured the sink, or it was already configured correctly. // Send through some data. - if (output_format == sink_stream.info().format && !std::holds_alternative(*output_format)) { + if (output_format == sink_stream.info().format && + !std::holds_alternative(*output_format)) { // TODO: tune the delay on this, as it's currently the only way to // throttle this task's CPU time. Maybe also hold off on the pipeline // if the buffer is already close to full? @@ -160,7 +161,7 @@ void AudioTaskMain(void* args) { vTaskDelete(NULL); } -static std::byte sDrainBuf[1024]; +static std::byte sDrainBuf[8 * 1024]; void AudioDrainMain(void* args) { { diff --git a/src/audio/fatfs_audio_input.cpp b/src/audio/fatfs_audio_input.cpp index b4e6db75..22d707d6 100644 --- a/src/audio/fatfs_audio_input.cpp +++ b/src/audio/fatfs_audio_input.cpp @@ -48,7 +48,8 @@ auto FatfsAudioInput::OpenFile(const std::string& path) -> void { auto FatfsAudioInput::Process(const std::vector& inputs, OutputStream* output) -> void { if (!is_file_open_) { - output->prepare({}); + // TODO(jacqueline): should we clear the stream format? + // output->prepare({}); return; } diff --git a/src/audio/include/audio_sink.hpp b/src/audio/include/audio_sink.hpp index ad63ec2e..03a4690d 100644 --- a/src/audio/include/audio_sink.hpp +++ b/src/audio/include/audio_sink.hpp @@ -6,7 +6,8 @@ namespace audio { class IAudioSink { private: - static const std::size_t kDrainBufferSize = 8 * 1024; + // TODO: tune. at least about 12KiB seems right for mp3 + static const std::size_t kDrainBufferSize = 24 * 1024; StreamBufferHandle_t buffer_; public: diff --git a/src/audio/include/stream_info.hpp b/src/audio/include/stream_info.hpp index 6256f2ee..28095935 100644 --- a/src/audio/include/stream_info.hpp +++ b/src/audio/include/stream_info.hpp @@ -66,20 +66,13 @@ class InputStream { public: explicit InputStream(RawStream* s) : raw_(s) {} - void consume(std::size_t bytes) const { - assert(raw_->info->bytes_in_stream >= bytes); - auto new_data = raw_->data.subspan(bytes); - std::move(new_data.begin(), new_data.end(), raw_->data.begin()); - raw_->info->bytes_in_stream = new_data.size_bytes(); - } + void consume(std::size_t bytes) const; - void mark_incomplete() const { raw_->is_incomplete = true; } + void mark_incomplete() const; - const StreamInfo& info() const { return *raw_->info; } + const StreamInfo& info() const; - cpp::span data() const { - return raw_->data.first(raw_->info->bytes_in_stream); - } + cpp::span data() const; private: RawStream* raw_; @@ -89,34 +82,15 @@ class OutputStream { public: explicit OutputStream(RawStream* s) : raw_(s) {} - void add(std::size_t bytes) const { - assert(raw_->info->bytes_in_stream + bytes <= raw_->data.size_bytes()); - raw_->info->bytes_in_stream += bytes; - } - - bool prepare(const StreamInfo::Format& new_format) { - if (std::holds_alternative(raw_->info->format)) { - raw_->info->format = new_format; - raw_->info->bytes_in_stream = 0; - } - if (new_format == raw_->info->format) { - return true; - } - if (raw_->is_incomplete) { - raw_->info->format = new_format; - raw_->info->bytes_in_stream = 0; - return true; - } - return false; - } - - const StreamInfo& info() const { return *raw_->info; } - - cpp::span data() const { - return raw_->data.subspan(raw_->info->bytes_in_stream); - } - - bool is_incomplete() const { return raw_->is_incomplete; } + void add(std::size_t bytes) const; + + bool prepare(const StreamInfo::Format& new_format); + + const StreamInfo& info() const; + + cpp::span data() const; + + bool is_incomplete() const; private: RawStream* raw_; 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); From 4c77950e702a329f3136456a932efbea36e03d42 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 19 Apr 2023 16:45:50 +1000 Subject: [PATCH 21/31] Pipeline working and outputting correctly, but noisy --- src/audio/audio_decoder.cpp | 5 ++- src/audio/audio_task.cpp | 12 +++--- src/audio/i2s_audio_output.cpp | 8 +++- src/audio/include/audio_sink.hpp | 24 +++++++++-- src/audio/stream_info.cpp | 70 ++++++++++++++++++++++++++++++ src/codecs/mad.cpp | 12 +++--- src/drivers/dac.cpp | 73 ++++++++++++++++++++++++-------- src/drivers/include/dac.hpp | 2 + src/tasks/tasks.cpp | 3 +- src/tasks/tasks.hpp | 3 +- 10 files changed, 174 insertions(+), 38 deletions(-) create mode 100644 src/audio/stream_info.cpp diff --git a/src/audio/audio_decoder.cpp b/src/audio/audio_decoder.cpp index af9abb94..4b9826a9 100644 --- a/src/audio/audio_decoder.cpp +++ b/src/audio/audio_decoder.cpp @@ -128,7 +128,10 @@ auto AudioDecoder::Process(const std::vector& inputs, } } - input->consume(current_codec_->GetInputPosition() - 1); + std::size_t pos = current_codec_->GetInputPosition(); + if (pos > 0) { + input->consume(pos - 1); + } } } // namespace audio diff --git a/src/audio/audio_task.cpp b/src/audio/audio_task.cpp index 3a2a5941..1670f9f6 100644 --- a/src/audio/audio_task.cpp +++ b/src/audio/audio_task.cpp @@ -45,7 +45,7 @@ auto StartPipeline(Pipeline* pipeline, IAudioSink* sink) -> void { ESP_LOGI(kTag, "starting audio pipeline task"); xTaskCreatePinnedToCore(&AudioTaskMain, "pipeline", kStackSize, args, - kTaskPriorityAudio, NULL, kAudioCore); + kTaskPriorityAudioPipeline, NULL, kAudioCore); } auto StartDrain(IAudioSink* sink) -> void { @@ -57,8 +57,8 @@ auto StartDrain(IAudioSink* sink) -> void { }; ESP_LOGI(kTag, "starting audio drain task"); - xTaskCreatePinnedToCore(&AudioDrainMain, "drain", kDrainStackSize, drain_args, - kTaskPriorityAudio, NULL, kAudioCore); + xTaskCreate(&AudioDrainMain, "drain", kDrainStackSize, drain_args, + kTaskPriorityAudioDrain, NULL); } void AudioTaskMain(void* args) { @@ -134,7 +134,7 @@ void AudioTaskMain(void* args) { // The format of the stream within the sink stream has changed. We // need to reconfigure the sink, but shouldn't do so until we've fully // drained the current buffer. - if (xStreamBufferIsEmpty(sink->buffer())) { + if (xStreamBufferIsEmpty(*sink->buffer())) { ESP_LOGI(kTag, "reconfiguring dac"); output_format = sink_stream.info().format; sink->Configure(*output_format); @@ -149,7 +149,7 @@ void AudioTaskMain(void* args) { // throttle this task's CPU time. Maybe also hold off on the pipeline // if the buffer is already close to full? std::size_t sent = xStreamBufferSend( - sink->buffer(), sink_stream.data().data(), + *sink->buffer(), sink_stream.data().data(), sink_stream.data().size_bytes(), pdMS_TO_TICKS(10)); sink_stream.consume(sent); } @@ -172,7 +172,7 @@ void AudioDrainMain(void* args) { // TODO(jacqueline): implement PAUSE without busy-waiting. while (*command != QUIT) { - std::size_t len = xStreamBufferReceive(sink->buffer(), sDrainBuf, + std::size_t len = xStreamBufferReceive(*sink->buffer(), sDrainBuf, sizeof(sDrainBuf), portMAX_DELAY); if (len > 0) { sink->Send({sDrainBuf, len}); diff --git a/src/audio/i2s_audio_output.cpp b/src/audio/i2s_audio_output.cpp index 2d336152..7e9e9353 100644 --- a/src/audio/i2s_audio_output.cpp +++ b/src/audio/i2s_audio_output.cpp @@ -38,9 +38,13 @@ auto I2SAudioOutput::create(drivers::GpioExpander* expander) I2SAudioOutput::I2SAudioOutput(drivers::GpioExpander* expander, std::unique_ptr dac) - : expander_(expander), dac_(std::move(dac)), current_config_() {} + : expander_(expander), dac_(std::move(dac)), current_config_() { + //dac_->SetSource(buffer()); + } -I2SAudioOutput::~I2SAudioOutput() {} +I2SAudioOutput::~I2SAudioOutput() { + dac_->SetSource(nullptr); +} auto I2SAudioOutput::Configure(const StreamInfo::Format& format) -> bool { if (!std::holds_alternative(format)) { diff --git a/src/audio/include/audio_sink.hpp b/src/audio/include/audio_sink.hpp index 03a4690d..a11a9c92 100644 --- a/src/audio/include/audio_sink.hpp +++ b/src/audio/include/audio_sink.hpp @@ -1,6 +1,9 @@ #pragma once +#include #include "audio_element.hpp" +#include "esp_heap_caps.h" +#include "freertos/FreeRTOS.h" #include "stream_info.hpp" namespace audio { @@ -8,17 +11,30 @@ class IAudioSink { private: // TODO: tune. at least about 12KiB seems right for mp3 static const std::size_t kDrainBufferSize = 24 * 1024; - StreamBufferHandle_t buffer_; + uint8_t *buffer_; + StaticStreamBuffer_t *metadata_; + StreamBufferHandle_t *handle_; public: - IAudioSink() : buffer_(xStreamBufferCreate(kDrainBufferSize, 1)) {} - virtual ~IAudioSink() { vStreamBufferDelete(buffer_); } + IAudioSink() : + buffer_(reinterpret_cast(heap_caps_malloc(kDrainBufferSize, MALLOC_CAP_DMA))), + metadata_(reinterpret_cast(heap_caps_malloc(sizeof(StaticStreamBuffer_t), MALLOC_CAP_DMA))), + handle_(reinterpret_cast(heap_caps_malloc(sizeof(StreamBufferHandle_t), MALLOC_CAP_DMA))) { + *handle_ = xStreamBufferCreateStatic(kDrainBufferSize, 1, buffer_, metadata_); + } + + virtual ~IAudioSink() { + vStreamBufferDelete(*handle_); + free(buffer_); + free(handle_); + free(metadata_); + } virtual auto Configure(const StreamInfo::Format& format) -> bool = 0; virtual auto Send(const cpp::span& data) -> void = 0; virtual auto Log() -> void {} - auto buffer() const -> StreamBufferHandle_t { return buffer_; } + auto buffer() -> StreamBufferHandle_t* { return handle_; } }; } // namespace audio diff --git a/src/audio/stream_info.cpp b/src/audio/stream_info.cpp new file mode 100644 index 00000000..7d833d25 --- /dev/null +++ b/src/audio/stream_info.cpp @@ -0,0 +1,70 @@ +#include "stream_info.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "result.hpp" +#include "span.hpp" +#include "types.hpp" + +namespace audio { + +void InputStream::consume(std::size_t bytes) const { + assert(raw_->info->bytes_in_stream >= bytes); + auto new_data = raw_->data.subspan(bytes); + std::move(new_data.begin(), new_data.end(), raw_->data.begin()); + raw_->info->bytes_in_stream = new_data.size_bytes(); +} + +void InputStream::mark_incomplete() const { + raw_->is_incomplete = true; +} + +const StreamInfo& InputStream::info() const { + return *raw_->info; +} + +cpp::span InputStream::data() const { + return raw_->data.first(raw_->info->bytes_in_stream); +} + +void OutputStream::add(std::size_t bytes) const { + assert(raw_->info->bytes_in_stream + bytes <= raw_->data.size_bytes()); + raw_->info->bytes_in_stream += bytes; +} + +bool OutputStream::prepare(const StreamInfo::Format& new_format) { + if (std::holds_alternative(raw_->info->format)) { + raw_->info->format = new_format; + raw_->info->bytes_in_stream = 0; + return true; + } + if (new_format == raw_->info->format) { + return true; + } + if (raw_->is_incomplete) { + raw_->info->format = new_format; + raw_->info->bytes_in_stream = 0; + return true; + } + return false; +} + +const StreamInfo& OutputStream::info() const { + return *raw_->info; +} + +cpp::span OutputStream::data() const { + return raw_->data.subspan(raw_->info->bytes_in_stream); +} + +bool OutputStream::is_incomplete() const { + return raw_->is_incomplete; +} + +} // namespace audio diff --git a/src/codecs/mad.cpp b/src/codecs/mad.cpp index 020e0d57..7e5b350b 100644 --- a/src/codecs/mad.cpp +++ b/src/codecs/mad.cpp @@ -9,7 +9,7 @@ namespace codecs { -static int scaleTo24Bits(mad_fixed_t sample) { +static int scaleTo16Bits(mad_fixed_t sample) { // Round the bottom bits. sample += (1L << (MAD_F_FRACBITS - 16)); @@ -19,7 +19,7 @@ static int scaleTo24Bits(mad_fixed_t sample) { else if (sample < -MAD_F_ONE) sample = -MAD_F_ONE; - /* quantize */ + // Quantize. return sample >> (MAD_F_FRACBITS + 1 - 16); } @@ -119,10 +119,10 @@ auto MadMp3Decoder::WriteOutputSamples(cpp::span output) } for (int channel = 0; channel < synth_.pcm.channels; channel++) { - uint32_t sample_24 = - scaleTo24Bits(synth_.pcm.samples[channel][current_sample_]); - output[output_byte++] = static_cast((sample_24 >> 8) & 0xFF); - output[output_byte++] = static_cast((sample_24)&0xFF); + uint16_t sample_16 = + scaleTo16Bits(synth_.pcm.samples[channel][current_sample_]); + output[output_byte++] = static_cast((sample_16 >> 8) & 0xFF); + output[output_byte++] = static_cast((sample_16)&0xFF); } current_sample_++; } 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; diff --git a/src/tasks/tasks.cpp b/src/tasks/tasks.cpp index 32de431a..b9fce7ec 100644 --- a/src/tasks/tasks.cpp +++ b/src/tasks/tasks.cpp @@ -1,4 +1,5 @@ #include "tasks.hpp" const UBaseType_t kTaskPriorityLvgl = 4; -const UBaseType_t kTaskPriorityAudio = 5; +const UBaseType_t kTaskPriorityAudioPipeline = 5; +const UBaseType_t kTaskPriorityAudioDrain = 6; diff --git a/src/tasks/tasks.hpp b/src/tasks/tasks.hpp index 24f8509a..47668aea 100644 --- a/src/tasks/tasks.hpp +++ b/src/tasks/tasks.hpp @@ -3,4 +3,5 @@ #include "freertos/portmacro.h" extern const UBaseType_t kTaskPriorityLvgl; -extern const UBaseType_t kTaskPriorityAudio; +extern const UBaseType_t kTaskPriorityAudioPipeline; +extern const UBaseType_t kTaskPriorityAudioDrain; From 731b2cfa77a063e98da8fa26acc1e7ed1de8c169 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 20 Apr 2023 11:25:43 +1000 Subject: [PATCH 22/31] working isr-based sink, but still grainy --- src/audio/audio_playback.cpp | 2 +- src/audio/audio_task.cpp | 8 ++++---- src/audio/i2s_audio_output.cpp | 4 ++-- src/audio/include/audio_sink.hpp | 28 ++++++++++++++++------------ src/drivers/dac.cpp | 32 ++++++++++++++++++-------------- src/drivers/include/dac.hpp | 2 +- 6 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/audio/audio_playback.cpp b/src/audio/audio_playback.cpp index 9a978535..fac4bfab 100644 --- a/src/audio/audio_playback.cpp +++ b/src/audio/audio_playback.cpp @@ -41,7 +41,7 @@ AudioPlayback::AudioPlayback(std::unique_ptr output) pipeline->AddInput(file_source_.get()); task::StartPipeline(pipeline, i2s_output_.get()); - task::StartDrain(i2s_output_.get()); + // task::StartDrain(i2s_output_.get()); } AudioPlayback::~AudioPlayback() {} diff --git a/src/audio/audio_task.cpp b/src/audio/audio_task.cpp index 1670f9f6..45db0b60 100644 --- a/src/audio/audio_task.cpp +++ b/src/audio/audio_task.cpp @@ -58,7 +58,7 @@ auto StartDrain(IAudioSink* sink) -> void { ESP_LOGI(kTag, "starting audio drain task"); xTaskCreate(&AudioDrainMain, "drain", kDrainStackSize, drain_args, - kTaskPriorityAudioDrain, NULL); + kTaskPriorityAudioDrain, NULL); } void AudioTaskMain(void* args) { @@ -134,7 +134,7 @@ void AudioTaskMain(void* args) { // The format of the stream within the sink stream has changed. We // need to reconfigure the sink, but shouldn't do so until we've fully // drained the current buffer. - if (xStreamBufferIsEmpty(*sink->buffer())) { + if (xStreamBufferIsEmpty(sink->buffer())) { ESP_LOGI(kTag, "reconfiguring dac"); output_format = sink_stream.info().format; sink->Configure(*output_format); @@ -149,7 +149,7 @@ void AudioTaskMain(void* args) { // throttle this task's CPU time. Maybe also hold off on the pipeline // if the buffer is already close to full? std::size_t sent = xStreamBufferSend( - *sink->buffer(), sink_stream.data().data(), + sink->buffer(), sink_stream.data().data(), sink_stream.data().size_bytes(), pdMS_TO_TICKS(10)); sink_stream.consume(sent); } @@ -172,7 +172,7 @@ void AudioDrainMain(void* args) { // TODO(jacqueline): implement PAUSE without busy-waiting. while (*command != QUIT) { - std::size_t len = xStreamBufferReceive(*sink->buffer(), sDrainBuf, + std::size_t len = xStreamBufferReceive(sink->buffer(), sDrainBuf, sizeof(sDrainBuf), portMAX_DELAY); if (len > 0) { sink->Send({sDrainBuf, len}); diff --git a/src/audio/i2s_audio_output.cpp b/src/audio/i2s_audio_output.cpp index 7e9e9353..63563a10 100644 --- a/src/audio/i2s_audio_output.cpp +++ b/src/audio/i2s_audio_output.cpp @@ -39,8 +39,8 @@ auto I2SAudioOutput::create(drivers::GpioExpander* expander) I2SAudioOutput::I2SAudioOutput(drivers::GpioExpander* expander, std::unique_ptr dac) : expander_(expander), dac_(std::move(dac)), current_config_() { - //dac_->SetSource(buffer()); - } + dac_->SetSource(buffer()); +} I2SAudioOutput::~I2SAudioOutput() { dac_->SetSource(nullptr); diff --git a/src/audio/include/audio_sink.hpp b/src/audio/include/audio_sink.hpp index a11a9c92..eaed04ec 100644 --- a/src/audio/include/audio_sink.hpp +++ b/src/audio/include/audio_sink.hpp @@ -11,22 +11,26 @@ class IAudioSink { private: // TODO: tune. at least about 12KiB seems right for mp3 static const std::size_t kDrainBufferSize = 24 * 1024; - uint8_t *buffer_; - StaticStreamBuffer_t *metadata_; - StreamBufferHandle_t *handle_; + uint8_t* buffer_; + StaticStreamBuffer_t* metadata_; + StreamBufferHandle_t handle_; public: - IAudioSink() : - buffer_(reinterpret_cast(heap_caps_malloc(kDrainBufferSize, MALLOC_CAP_DMA))), - metadata_(reinterpret_cast(heap_caps_malloc(sizeof(StaticStreamBuffer_t), MALLOC_CAP_DMA))), - handle_(reinterpret_cast(heap_caps_malloc(sizeof(StreamBufferHandle_t), MALLOC_CAP_DMA))) { - *handle_ = xStreamBufferCreateStatic(kDrainBufferSize, 1, buffer_, metadata_); - } + IAudioSink() + : buffer_(reinterpret_cast( + heap_caps_malloc(kDrainBufferSize, + MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT))), + metadata_(reinterpret_cast( + heap_caps_malloc(sizeof(StaticStreamBuffer_t), + MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT))), + handle_(xStreamBufferCreateStatic(kDrainBufferSize, + 1, + buffer_, + metadata_)) {} virtual ~IAudioSink() { - vStreamBufferDelete(*handle_); + vStreamBufferDelete(handle_); free(buffer_); - free(handle_); free(metadata_); } @@ -34,7 +38,7 @@ class IAudioSink { virtual auto Send(const cpp::span& data) -> void = 0; virtual auto Log() -> void {} - auto buffer() -> StreamBufferHandle_t* { return handle_; } + auto buffer() -> StreamBufferHandle_t { return handle_; } }; } // namespace audio 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; From 2d95b637272f15ba2d74abc2371d708a08407d91 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 20 Apr 2023 14:47:32 +1000 Subject: [PATCH 23/31] Working without big distortion :) --- src/audio/i2s_audio_output.cpp | 2 +- src/drivers/dac.cpp | 123 +++++++++++++++++++++++++++++++-- src/drivers/include/dac.hpp | 6 ++ 3 files changed, 124 insertions(+), 7 deletions(-) diff --git a/src/audio/i2s_audio_output.cpp b/src/audio/i2s_audio_output.cpp index 63563a10..e5c672f9 100644 --- a/src/audio/i2s_audio_output.cpp +++ b/src/audio/i2s_audio_output.cpp @@ -31,7 +31,7 @@ auto I2SAudioOutput::create(drivers::GpioExpander* expander) // Soft mute immediately, in order to minimise any clicks and pops caused by // the initial output element and pipeline configuration. // dac->WriteVolume(255); - dac->WriteVolume(120); // for testing + dac->WriteVolume(127); // for testing return std::make_unique(expander, std::move(dac)); } 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? From 27c63ebb957aa5b942939aea33431ac50d101a26 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 20 Apr 2023 21:25:22 +1000 Subject: [PATCH 24/31] Switch to an MVP-ready 16bit three wire DAC setup --- src/audio/audio_task.cpp | 12 +++-- src/audio/include/audio_sink.hpp | 2 +- src/audio/include/pipeline.hpp | 2 +- src/codecs/mad.cpp | 20 +++++++-- src/drivers/dac.cpp | 76 +++++++++++++++++++------------- 5 files changed, 72 insertions(+), 40 deletions(-) diff --git a/src/audio/audio_task.cpp b/src/audio/audio_task.cpp index 45db0b60..07f49085 100644 --- a/src/audio/audio_task.cpp +++ b/src/audio/audio_task.cpp @@ -37,15 +37,14 @@ namespace task { static const char* kTag = "task"; static const std::size_t kStackSize = 24 * 1024; static const std::size_t kDrainStackSize = 1024; -static const uint8_t kAudioCore = 0; auto StartPipeline(Pipeline* pipeline, IAudioSink* sink) -> void { // Newly created task will free this. AudioTaskArgs* args = new AudioTaskArgs{.pipeline = pipeline, .sink = sink}; ESP_LOGI(kTag, "starting audio pipeline task"); - xTaskCreatePinnedToCore(&AudioTaskMain, "pipeline", kStackSize, args, - kTaskPriorityAudioPipeline, NULL, kAudioCore); + xTaskCreate(&AudioTaskMain, "pipeline", kStackSize, args, + kTaskPriorityAudioPipeline, NULL); } auto StartDrain(IAudioSink* sink) -> void { @@ -151,6 +150,13 @@ void AudioTaskMain(void* args) { std::size_t sent = xStreamBufferSend( sink->buffer(), sink_stream.data().data(), sink_stream.data().size_bytes(), pdMS_TO_TICKS(10)); + if (sent > 0) { + ESP_LOGI(kTag, "sunk %u bytes out of %u (%d %%)", sent, + sink_stream.info().bytes_in_stream, + (int)(((float)sent / + (float)sink_stream.info().bytes_in_stream) * + 100)); + } sink_stream.consume(sent); } diff --git a/src/audio/include/audio_sink.hpp b/src/audio/include/audio_sink.hpp index eaed04ec..7a535c35 100644 --- a/src/audio/include/audio_sink.hpp +++ b/src/audio/include/audio_sink.hpp @@ -10,7 +10,7 @@ namespace audio { class IAudioSink { private: // TODO: tune. at least about 12KiB seems right for mp3 - static const std::size_t kDrainBufferSize = 24 * 1024; + static const std::size_t kDrainBufferSize = 48 * 1024; uint8_t* buffer_; StaticStreamBuffer_t* metadata_; StreamBufferHandle_t handle_; diff --git a/src/audio/include/pipeline.hpp b/src/audio/include/pipeline.hpp index 2e9247bc..7a658a38 100644 --- a/src/audio/include/pipeline.hpp +++ b/src/audio/include/pipeline.hpp @@ -13,7 +13,7 @@ namespace audio { -static const std::size_t kPipelineBufferSize = 32 * 1024; +static const std::size_t kPipelineBufferSize = 64 * 1024; class Pipeline { public: diff --git a/src/codecs/mad.cpp b/src/codecs/mad.cpp index 7e5b350b..dd839537 100644 --- a/src/codecs/mad.cpp +++ b/src/codecs/mad.cpp @@ -1,4 +1,5 @@ #include "mad.hpp" +#include #include @@ -9,9 +10,9 @@ namespace codecs { -static int scaleTo16Bits(mad_fixed_t sample) { +static uint32_t scaleToBits(mad_fixed_t sample, uint8_t bits) { // Round the bottom bits. - sample += (1L << (MAD_F_FRACBITS - 16)); + sample += (1L << (MAD_F_FRACBITS - bits)); // Clip the leftover bits to within range. if (sample >= MAD_F_ONE) @@ -20,7 +21,7 @@ static int scaleTo16Bits(mad_fixed_t sample) { sample = -MAD_F_ONE; // Quantize. - return sample >> (MAD_F_FRACBITS + 1 - 16); + return sample >> (MAD_F_FRACBITS + 1 - bits); } MadMp3Decoder::MadMp3Decoder() { @@ -119,8 +120,19 @@ auto MadMp3Decoder::WriteOutputSamples(cpp::span output) } for (int channel = 0; channel < synth_.pcm.channels; channel++) { + // TODO(jacqueline): output 24 bit samples when (if?) we have a downmix + // step in the pipeline. + /* + uint32_t sample_24 = + scaleToBits(synth_.pcm.samples[channel][current_sample_], 24); + output[output_byte++] = static_cast((sample_24 >> 16) & 0xFF); + output[output_byte++] = static_cast((sample_24 >> 8) & 0xFF); + output[output_byte++] = static_cast((sample_24)&0xFF); + // 24 bit samples must still be aligned to 32 bits. The LSB is ignored. + output[output_byte++] = static_cast(0); + */ uint16_t sample_16 = - scaleTo16Bits(synth_.pcm.samples[channel][current_sample_]); + scaleToBits(synth_.pcm.samples[channel][current_sample_], 16); output[output_byte++] = static_cast((sample_16 >> 8) & 0xFF); output[output_byte++] = static_cast((sample_16)&0xFF); } 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; } From f9558044955827b476ce21548d544530c1457026 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 21 Apr 2023 10:00:05 +1000 Subject: [PATCH 25/31] use a freertos timer for lvgl ticks --- src/main/main.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/main.cpp b/src/main/main.cpp index 77a1e14b..b1cee384 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -18,6 +18,8 @@ #include "esp_log.h" #include "font/lv_font.h" #include "freertos/portmacro.h" +#include "freertos/projdefs.h" +#include "freertos/timers.h" #include "hal/gpio_types.h" #include "hal/spi_types.h" #include "lvgl/lvgl.h" @@ -40,8 +42,8 @@ static const char* TAG = "MAIN"; -void IRAM_ATTR tick_hook(void) { - // lv_tick_inc(1); +auto tick_hook(TimerHandle_t xTimer) -> void { + lv_tick_inc(1); } static const size_t kLvglStackSize = 8 * 1024; @@ -64,7 +66,7 @@ extern "C" void lvgl_main(void* voidArgs) { lv_init(); // LVGL has been initialised, so we can now start reporting ticks to it. - esp_register_freertos_tick_hook(&tick_hook); + xTimerCreate("lv_tick", pdMS_TO_TICKS(1), pdTRUE, NULL, &tick_hook); ESP_LOGI(TAG, "init display"); std::unique_ptr display = From cc3fd9571eadf8ad49bd7a5806a504555dc885db Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 21 Apr 2023 10:30:52 +1000 Subject: [PATCH 26/31] split out lvgl task into its own component --- src/main/CMakeLists.txt | 2 +- src/main/main.cpp | 70 ++-------------------- src/ui/CMakeLists.txt | 5 ++ src/ui/include/lvgl_task.hpp | 16 +++++ src/ui/lvgl_task.cpp | 111 +++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 67 deletions(-) create mode 100644 src/ui/CMakeLists.txt create mode 100644 src/ui/include/lvgl_task.hpp create mode 100644 src/ui/lvgl_task.cpp diff --git a/src/main/CMakeLists.txt b/src/main/CMakeLists.txt index df0d929b..49a54cd5 100644 --- a/src/main/CMakeLists.txt +++ b/src/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS "main.cpp" "app_console.cpp" INCLUDE_DIRS "." - REQUIRES "audio" "drivers" "dev_console" "drivers") + REQUIRES "audio" "drivers" "dev_console" "ui") target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS}) diff --git a/src/main/main.cpp b/src/main/main.cpp index b1cee384..796c70f3 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -6,8 +6,6 @@ #include #include -#include "core/lv_disp.h" -#include "core/lv_obj_pos.h" #include "driver/gpio.h" #include "driver/i2c.h" #include "driver/sdspi_host.h" @@ -16,17 +14,10 @@ #include "esp_freertos_hooks.h" #include "esp_intr_alloc.h" #include "esp_log.h" -#include "font/lv_font.h" #include "freertos/portmacro.h" #include "freertos/projdefs.h" -#include "freertos/timers.h" #include "hal/gpio_types.h" #include "hal/spi_types.h" -#include "lvgl/lvgl.h" -#include "misc/lv_color.h" -#include "misc/lv_style.h" -#include "misc/lv_timer.h" -#include "widgets/lv_label.h" #include "app_console.hpp" #include "audio_playback.hpp" @@ -36,64 +27,13 @@ #include "display_init.hpp" #include "gpio_expander.hpp" #include "i2c.hpp" +#include "lvgl_task.hpp" #include "spi.hpp" #include "storage.hpp" #include "touchwheel.hpp" static const char* TAG = "MAIN"; -auto tick_hook(TimerHandle_t xTimer) -> void { - lv_tick_inc(1); -} - -static const size_t kLvglStackSize = 8 * 1024; -static StaticTask_t sLvglTaskBuffer = {}; -static StackType_t sLvglStack[kLvglStackSize] = {0}; - -struct LvglArgs { - drivers::GpioExpander* gpio_expander; -}; - -extern "C" void lvgl_main(void* voidArgs) { - ESP_LOGI(TAG, "starting LVGL task"); - LvglArgs* args = (LvglArgs*)voidArgs; - drivers::GpioExpander* gpio_expander = args->gpio_expander; - - // Dispose of the args now that we've gotten everything out of them. - delete args; - - ESP_LOGI(TAG, "init lvgl"); - lv_init(); - - // LVGL has been initialised, so we can now start reporting ticks to it. - xTimerCreate("lv_tick", pdMS_TO_TICKS(1), pdTRUE, NULL, &tick_hook); - - ESP_LOGI(TAG, "init display"); - std::unique_ptr display = - drivers::Display::create(gpio_expander, drivers::displays::kST7735R); - - lv_style_t style; - lv_style_init(&style); - lv_style_set_text_color(&style, LV_COLOR_MAKE(0xFF, 0, 0)); - // TODO: find a nice bitmap font for this display size and density. - // lv_style_set_text_font(&style, &lv_font_montserrat_24); - - auto label = lv_label_create(NULL); - lv_label_set_text(label, "COLOURS!!"); - lv_obj_add_style(label, &style, 0); - - lv_obj_center(label); - lv_scr_load(label); - - while (1) { - lv_timer_handler(); - vTaskDelay(pdMS_TO_TICKS(10)); - } - - // TODO: break from the loop to kill this task, so that we can do our RAII - // cleanup, unregister our tick callback and so on. -} - extern "C" void app_main(void) { ESP_LOGI(TAG, "Initialising peripherals"); @@ -129,11 +69,9 @@ extern "C" void app_main(void) { std::unique_ptr touchwheel = std::make_unique(); - LvglArgs* lvglArgs = (LvglArgs*)calloc(1, sizeof(LvglArgs)); - lvglArgs->gpio_expander = expander; - xTaskCreateStaticPinnedToCore(&lvgl_main, "LVGL", kLvglStackSize, - (void*)lvglArgs, 1, sLvglStack, - &sLvglTaskBuffer, 1); + std::atomic lvgl_quit; + TaskHandle_t lvgl_task_handle; + ui::StartLvgl(expander, &lvgl_quit, &lvgl_task_handle); std::unique_ptr playback; if (storage) { diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt new file mode 100644 index 00000000..bdccbbdb --- /dev/null +++ b/src/ui/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register( + SRCS "lvgl_task.cpp" + INCLUDE_DIRS "include" + REQUIRES "drivers") +target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS}) diff --git a/src/ui/include/lvgl_task.hpp b/src/ui/include/lvgl_task.hpp new file mode 100644 index 00000000..1a2f425c --- /dev/null +++ b/src/ui/include/lvgl_task.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "gpio_expander.hpp" + +namespace ui { + +auto StartLvgl(drivers::GpioExpander* gpios, + std::atomic* quit, + TaskHandle_t* handle) -> bool; + +} // namespace ui diff --git a/src/ui/lvgl_task.cpp b/src/ui/lvgl_task.cpp new file mode 100644 index 00000000..d66f6282 --- /dev/null +++ b/src/ui/lvgl_task.cpp @@ -0,0 +1,111 @@ +#include "lvgl_task.hpp" + +#include +#include +#include + +#include +#include +#include + +#include "core/lv_disp.h" +#include "core/lv_obj.h" +#include "core/lv_obj_pos.h" +#include "core/lv_obj_tree.h" +#include "esp_log.h" +#include "font/lv_font.h" +#include "freertos/portmacro.h" +#include "freertos/projdefs.h" +#include "freertos/timers.h" +#include "hal/gpio_types.h" +#include "hal/spi_types.h" +#include "lvgl/lvgl.h" +#include "misc/lv_color.h" +#include "misc/lv_style.h" +#include "misc/lv_timer.h" +#include "widgets/lv_label.h" + +#include "display.hpp" +#include "display_init.hpp" +#include "gpio_expander.hpp" + +namespace ui { + +static const char* kTag = "lv_task"; + +auto tick_hook(TimerHandle_t xTimer) -> void { + lv_tick_inc(1); +} + +struct LvglArgs { + drivers::GpioExpander* gpio_expander; + std::atomic* quit; +}; + +void LvglMain(void* voidArgs) { + LvglArgs* args = (LvglArgs*)voidArgs; + drivers::GpioExpander* gpio_expander = args->gpio_expander; + std::atomic* quit = args->quit; + + // Dispose of the args now that we've gotten everything out of them. + delete args; + + { + ESP_LOGI(kTag, "init lvgl"); + lv_init(); + + // LVGL has been initialised, so we can now start reporting ticks to it. + TimerHandle_t tick_timer = + xTimerCreate("lv_tick", pdMS_TO_TICKS(1), pdTRUE, NULL, &tick_hook); + + ESP_LOGI(kTag, "init display"); + std::unique_ptr display = + drivers::Display::create(gpio_expander, drivers::displays::kST7735R); + + lv_style_t style; + lv_style_init(&style); + lv_style_set_text_color(&style, LV_COLOR_MAKE(0xFF, 0, 0)); + // TODO: find a nice bitmap font for this display size and density. + // lv_style_set_text_font(&style, &lv_font_montserrat_24); + + auto label = lv_label_create(NULL); + lv_label_set_text(label, "COLOURS!!"); + lv_obj_add_style(label, &style, 0); + + lv_obj_center(label); + lv_scr_load(label); + + while (!quit->load()) { + lv_timer_handler(); + vTaskDelay(pdMS_TO_TICKS(10)); + } + + // TODO(robin? daniel?): De-init the UI stack here. + lv_obj_del(label); + lv_style_reset(&style); + + xTimerDelete(tick_timer, portMAX_DELAY); + + lv_deinit(); + } + + vTaskDelete(NULL); +} + +static const size_t kLvglStackSize = 8 * 1024; +static StaticTask_t sLvglTaskBuffer = {}; +static StackType_t sLvglStack[kLvglStackSize] = {0}; + +auto StartLvgl(drivers::GpioExpander* gpios, + std::atomic* quit, + TaskHandle_t* handle) -> bool { + LvglArgs* args = new LvglArgs(); + args->gpio_expander = gpios; + args->quit = quit; + + return xTaskCreateStaticPinnedToCore(&LvglMain, "LVGL", kLvglStackSize, + reinterpret_cast(args), 1, + sLvglStack, &sLvglTaskBuffer, 1); +} + +} // namespace ui From 130009eb142a5ce8bb6cf9cbb59d69a4d96ed5f9 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 21 Apr 2023 10:35:39 +1000 Subject: [PATCH 27/31] Pin audio pipeline to the app core --- src/audio/audio_task.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/audio/audio_task.cpp b/src/audio/audio_task.cpp index 07f49085..b2a8062e 100644 --- a/src/audio/audio_task.cpp +++ b/src/audio/audio_task.cpp @@ -43,8 +43,8 @@ auto StartPipeline(Pipeline* pipeline, IAudioSink* sink) -> void { AudioTaskArgs* args = new AudioTaskArgs{.pipeline = pipeline, .sink = sink}; ESP_LOGI(kTag, "starting audio pipeline task"); - xTaskCreate(&AudioTaskMain, "pipeline", kStackSize, args, - kTaskPriorityAudioPipeline, NULL); + xTaskCreatePinnedToCore(&AudioTaskMain, "pipeline", kStackSize, args, + kTaskPriorityAudioPipeline, NULL, 1); } auto StartDrain(IAudioSink* sink) -> void { From 7083459cf3c62c32d0c039a4665e702d70a27bba Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 21 Apr 2023 15:27:57 +1000 Subject: [PATCH 28/31] wrap driver instance ownership + di in a class --- src/audio/audio_playback.cpp | 16 +++------- src/audio/i2s_audio_output.cpp | 22 ++------------ src/audio/include/audio_playback.hpp | 7 ++--- src/audio/include/i2s_audio_output.hpp | 8 ++--- 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 +++++++---- src/main/main.cpp | 42 +++++++++----------------- src/ui/include/lvgl_task.hpp | 5 +-- src/ui/lvgl_task.cpp | 17 +++++------ 14 files changed, 57 insertions(+), 101 deletions(-) diff --git a/src/audio/audio_playback.cpp b/src/audio/audio_playback.cpp index fac4bfab..c51e41fb 100644 --- a/src/audio/audio_playback.cpp +++ b/src/audio/audio_playback.cpp @@ -5,6 +5,7 @@ #include #include +#include "driver_cache.hpp" #include "freertos/portmacro.h" #include "audio_decoder.hpp" @@ -21,19 +22,10 @@ #include "stream_message.hpp" namespace audio { - -auto AudioPlayback::create(drivers::GpioExpander* expander) - -> cpp::result, Error> { - auto sink_res = I2SAudioOutput::create(expander); - if (sink_res.has_error()) { - return cpp::fail(ERR_INIT_ELEMENT); - } - return std::make_unique(std::move(sink_res.value())); -} - -AudioPlayback::AudioPlayback(std::unique_ptr output) +AudioPlayback::AudioPlayback(drivers::DriverCache* drivers) : file_source_(std::make_unique()), - i2s_output_(std::move(output)) { + i2s_output_(std::make_unique(drivers->AcquireGpios(), + drivers->AcquireDac())) { AudioDecoder* codec = new AudioDecoder(); elements_.emplace_back(codec); diff --git a/src/audio/i2s_audio_output.cpp b/src/audio/i2s_audio_output.cpp index e5c672f9..e69cc5ac 100644 --- a/src/audio/i2s_audio_output.cpp +++ b/src/audio/i2s_audio_output.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "esp_err.h" @@ -18,27 +19,10 @@ static const char* kTag = "I2SOUT"; namespace audio { -auto I2SAudioOutput::create(drivers::GpioExpander* expander) - -> cpp::result, Error> { - // First, we need to perform initial configuration of the DAC chip. - auto dac_result = drivers::AudioDac::create(expander); - if (dac_result.has_error()) { - ESP_LOGE(kTag, "failed to init dac: %d", dac_result.error()); - return cpp::fail(DAC_CONFIG); - } - std::unique_ptr dac = std::move(dac_result.value()); - - // Soft mute immediately, in order to minimise any clicks and pops caused by - // the initial output element and pipeline configuration. - // dac->WriteVolume(255); - dac->WriteVolume(127); // for testing - - return std::make_unique(expander, std::move(dac)); -} - I2SAudioOutput::I2SAudioOutput(drivers::GpioExpander* expander, - std::unique_ptr dac) + std::shared_ptr dac) : expander_(expander), dac_(std::move(dac)), current_config_() { + dac->WriteVolume(127); // for testing dac_->SetSource(buffer()); } diff --git a/src/audio/include/audio_playback.hpp b/src/audio/include/audio_playback.hpp index 88dc29aa..cd4be3e7 100644 --- a/src/audio/include/audio_playback.hpp +++ b/src/audio/include/audio_playback.hpp @@ -6,6 +6,7 @@ #include #include "audio_task.hpp" +#include "driver_cache.hpp" #include "esp_err.h" #include "fatfs_audio_input.hpp" #include "i2s_audio_output.hpp" @@ -25,11 +26,7 @@ namespace audio { */ class AudioPlayback { public: - enum Error { ERR_INIT_ELEMENT, ERR_MEM }; - static auto create(drivers::GpioExpander* expander) - -> cpp::result, Error>; - - explicit AudioPlayback(std::unique_ptr output); + explicit AudioPlayback(drivers::DriverCache* drivers); ~AudioPlayback(); /* diff --git a/src/audio/include/i2s_audio_output.hpp b/src/audio/include/i2s_audio_output.hpp index 31510a91..07430777 100644 --- a/src/audio/include/i2s_audio_output.hpp +++ b/src/audio/include/i2s_audio_output.hpp @@ -17,12 +17,8 @@ namespace audio { class I2SAudioOutput : public IAudioSink { public: - enum Error { DAC_CONFIG, I2S_CONFIG, STREAM_INIT }; - static auto create(drivers::GpioExpander* expander) - -> cpp::result, Error>; - I2SAudioOutput(drivers::GpioExpander* expander, - std::unique_ptr dac); + std::shared_ptr dac); ~I2SAudioOutput(); auto Configure(const StreamInfo::Format& format) -> bool override; @@ -36,7 +32,7 @@ class I2SAudioOutput : public IAudioSink { auto SetVolume(uint8_t volume) -> void; drivers::GpioExpander* expander_; - std::unique_ptr dac_; + std::shared_ptr dac_; std::optional current_config_; }; 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, diff --git a/src/main/main.cpp b/src/main/main.cpp index 796c70f3..2a345899 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -11,6 +11,7 @@ #include "driver/sdspi_host.h" #include "driver/spi_common.h" #include "driver/spi_master.h" +#include "driver_cache.hpp" #include "esp_freertos_hooks.h" #include "esp_intr_alloc.h" #include "esp_log.h" @@ -39,50 +40,37 @@ extern "C" void app_main(void) { ESP_ERROR_CHECK(drivers::init_i2c()); ESP_ERROR_CHECK(drivers::init_spi()); + std::unique_ptr drivers = + std::make_unique(); ESP_LOGI(TAG, "Init GPIOs"); - drivers::GpioExpander* expander = new drivers::GpioExpander(); + drivers::GpioExpander* expander = drivers->AcquireGpios(); ESP_LOGI(TAG, "Enable power rails for development"); - expander->with([&](auto& gpio) { - gpio.set_pin(drivers::GpioExpander::SD_MUX_EN_ACTIVE_LOW, 0); - gpio.set_pin(drivers::GpioExpander::SD_MUX_SWITCH, - drivers::GpioExpander::SD_MUX_ESP); - gpio.set_pin(drivers::GpioExpander::SD_CARD_POWER_ENABLE, 0); - gpio.set_pin(drivers::GpioExpander::AMP_EN, 1); - }); + expander->with( + [&](auto& gpio) { gpio.set_pin(drivers::GpioExpander::AMP_EN, 1); }); ESP_LOGI(TAG, "Init battery measurement"); drivers::Battery* battery = new drivers::Battery(); ESP_LOGI(TAG, "it's reading %d mV!", (int)battery->Millivolts()); ESP_LOGI(TAG, "Init SD card"); - auto storage_res = drivers::SdStorage::create(expander); - std::shared_ptr storage; - if (storage_res.has_error()) { + auto storage = drivers->AcquireStorage(); + if (!storage) { ESP_LOGE(TAG, "Failed! Do you have an SD card?"); - } else { - storage = std::move(storage_res.value()); } ESP_LOGI(TAG, "Init touch wheel"); - std::unique_ptr touchwheel = - std::make_unique(); + std::shared_ptr touchwheel = + drivers->AcquireTouchWheel(); std::atomic lvgl_quit; TaskHandle_t lvgl_task_handle; - ui::StartLvgl(expander, &lvgl_quit, &lvgl_task_handle); - - std::unique_ptr playback; - if (storage) { - ESP_LOGI(TAG, "Init audio pipeline"); - auto playback_res = audio::AudioPlayback::create(expander); - if (playback_res.has_error()) { - ESP_LOGE(TAG, "Failed! Playback will not work."); - } else { - playback = std::move(playback_res.value()); - } - } + ui::StartLvgl(drivers.get(), &lvgl_quit, &lvgl_task_handle); + + ESP_LOGI(TAG, "Init audio pipeline"); + std::unique_ptr playback = + std::make_unique(drivers.get()); ESP_LOGI(TAG, "Waiting for background tasks before launching console..."); vTaskDelay(pdMS_TO_TICKS(1000)); diff --git a/src/ui/include/lvgl_task.hpp b/src/ui/include/lvgl_task.hpp index 1a2f425c..ca3fc771 100644 --- a/src/ui/include/lvgl_task.hpp +++ b/src/ui/include/lvgl_task.hpp @@ -5,11 +5,12 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "gpio_expander.hpp" + +#include "driver_cache.hpp" namespace ui { -auto StartLvgl(drivers::GpioExpander* gpios, +auto StartLvgl(drivers::DriverCache* drivers, std::atomic* quit, TaskHandle_t* handle) -> bool; diff --git a/src/ui/lvgl_task.cpp b/src/ui/lvgl_task.cpp index d66f6282..12dfd34e 100644 --- a/src/ui/lvgl_task.cpp +++ b/src/ui/lvgl_task.cpp @@ -26,7 +26,7 @@ #include "widgets/lv_label.h" #include "display.hpp" -#include "display_init.hpp" +#include "driver_cache.hpp" #include "gpio_expander.hpp" namespace ui { @@ -38,16 +38,14 @@ auto tick_hook(TimerHandle_t xTimer) -> void { } struct LvglArgs { - drivers::GpioExpander* gpio_expander; + drivers::DriverCache* drivers; std::atomic* quit; }; void LvglMain(void* voidArgs) { - LvglArgs* args = (LvglArgs*)voidArgs; - drivers::GpioExpander* gpio_expander = args->gpio_expander; + LvglArgs* args = reinterpret_cast(voidArgs); + drivers::DriverCache* drivers = args->drivers; std::atomic* quit = args->quit; - - // Dispose of the args now that we've gotten everything out of them. delete args; { @@ -59,8 +57,7 @@ void LvglMain(void* voidArgs) { xTimerCreate("lv_tick", pdMS_TO_TICKS(1), pdTRUE, NULL, &tick_hook); ESP_LOGI(kTag, "init display"); - std::unique_ptr display = - drivers::Display::create(gpio_expander, drivers::displays::kST7735R); + std::shared_ptr display = drivers->AcquireDisplay(); lv_style_t style; lv_style_init(&style); @@ -96,11 +93,11 @@ static const size_t kLvglStackSize = 8 * 1024; static StaticTask_t sLvglTaskBuffer = {}; static StackType_t sLvglStack[kLvglStackSize] = {0}; -auto StartLvgl(drivers::GpioExpander* gpios, +auto StartLvgl(drivers::DriverCache* drivers, std::atomic* quit, TaskHandle_t* handle) -> bool { LvglArgs* args = new LvglArgs(); - args->gpio_expander = gpios; + args->drivers = drivers; args->quit = quit; return xTaskCreateStaticPinnedToCore(&LvglMain, "LVGL", kLvglStackSize, From 84f45e604585ccfb96be64fc463f4b816faa44e1 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 21 Apr 2023 18:42:37 +1000 Subject: [PATCH 29/31] Don't crash if there's no sd card :) --- src/main/main.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/main.cpp b/src/main/main.cpp index 2a345899..2970285f 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -68,9 +68,11 @@ extern "C" void app_main(void) { TaskHandle_t lvgl_task_handle; ui::StartLvgl(drivers.get(), &lvgl_quit, &lvgl_task_handle); - ESP_LOGI(TAG, "Init audio pipeline"); - std::unique_ptr playback = - std::make_unique(drivers.get()); + std::unique_ptr playback; + if (storage) { + ESP_LOGI(TAG, "Init audio pipeline"); + playback = std::make_unique(drivers.get()); + } ESP_LOGI(TAG, "Waiting for background tasks before launching console..."); vTaskDelay(pdMS_TO_TICKS(1000)); From 464a4bf527b9da30985a6576f702429621a849a6 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 21 Apr 2023 18:42:49 +1000 Subject: [PATCH 30/31] 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 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 From 5575378c1c8171cd716b79d3ab89df1e56ceb9d3 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 21 Apr 2023 19:03:23 +1000 Subject: [PATCH 31/31] fix use-after-move --- src/audio/i2s_audio_output.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/audio/i2s_audio_output.cpp b/src/audio/i2s_audio_output.cpp index e69cc5ac..8c374351 100644 --- a/src/audio/i2s_audio_output.cpp +++ b/src/audio/i2s_audio_output.cpp @@ -22,7 +22,7 @@ namespace audio { I2SAudioOutput::I2SAudioOutput(drivers::GpioExpander* expander, std::shared_ptr dac) : expander_(expander), dac_(std::move(dac)), current_config_() { - dac->WriteVolume(127); // for testing + dac_->WriteVolume(127); // for testing dac_->SetSource(buffer()); }