Add internet radio streaming with runtime API, modernize web UI, remove auto-tune/beep at boot
Streaming (main feature this session): - New WebRadioConfig/WebRadioJson core types, ISecureStore-backed persistence - New webradio::WebRadioService (thread-safe live config) + GET/POST /api/streaming - web_radio_stream task now runtime-toggleable (no reboot), no hardcoded URL - Content-Type diagnostic: warns clearly when a URL is a webpage, not an audio stream Boot cleanup: - Removed boot-time auto FM/DAB tune, auto-beep, and the (now-concluded) Si4684 crystal IBIAS/CTUN empirical sweep from main.cpp — tuning/beep are on-demand via the existing REST API only Web UI: - Modernized styling (cards, gradients, toggle switches, light/dark theme) - New Stream tab wired to /api/streaming Fixes found via real idf.py build (not just clangd): - Restored wrongly-removed si4684/Si4684Tuner.hpp include in main.cpp - Fixed MP3Decode() argument types in web_radio_stream.cpp (unsigned char**/int*) Quality-gate fixes: - Host-test stub headers (esp_log.h, freertos/*) so TunerService.cpp's scanForStation logging/pacing compiles for station_service_test / integration_service_test instead of running stale binaries - Added WifiScanner and WebRadioService manual sections; filled in missing Doxygen docs on BluetoothService, i2s_sdata_probe, test_firmware, Bt1035At - Ignore clangd's .cache/ index directory Also includes prior uncommitted work carried in the tree: Wi-Fi/Bluetooth device scan REST API and UI (WifiScanner, BT scan), SigmaStudio TCP bridge, and the current ADAU1701 SigmaStudio DSP program export. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -220,6 +220,20 @@ public:
|
||||
core::EqBandIndex band, core::GainDb gain, core::FrequencyHz center,
|
||||
float q);
|
||||
|
||||
/**
|
||||
* @brief setBeepEnabled — gate the SigmaStudio Beep1 tone generator.
|
||||
*
|
||||
* @dname setBeepEnabled
|
||||
* @param enabled true unmutes Beep1, false mutes it.
|
||||
* @return Ok on success, or Adau1701Error.
|
||||
* @pubstate writes parameter RAM via safeload (ADDR_BEEP1_ENABLE).
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-08-07
|
||||
*/
|
||||
[[nodiscard]] std::expected<void, Adau1701Error> setBeepEnabled(
|
||||
bool enabled);
|
||||
|
||||
private:
|
||||
[[nodiscard]] std::expected<void, Adau1701Error> ensureBooted() const;
|
||||
[[nodiscard]] std::expected<void, Adau1701Error> safeloadGain(
|
||||
|
||||
@@ -61,6 +61,9 @@ public:
|
||||
core::EqBandIndex band, core::GainDb gain, core::FrequencyHz center,
|
||||
float q) override;
|
||||
|
||||
[[nodiscard]] std::expected<void, core::DspError> setBeepEnabled(
|
||||
bool enabled) override;
|
||||
|
||||
private:
|
||||
[[nodiscard]] static core::DspError mapError(Adau1701Error error) noexcept;
|
||||
|
||||
|
||||
@@ -27,273 +27,345 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
|
||||
} // extern "C"
|
||||
|
||||
namespace adau1701 {
|
||||
|
||||
namespace {
|
||||
constexpr char kTag[] = "Adau1701";
|
||||
constexpr int kI2cPort = 0;
|
||||
/** Index 0 is the fixed high-pass band (SigmaStudio band 1); not safeloaded. */
|
||||
constexpr std::uint8_t kFixedHighPassBandIndex = 0U;
|
||||
} // namespace
|
||||
|
||||
Adau1701Driver::Adau1701Driver(Adau1701Pins pins,
|
||||
core::IDspProgramSource& programSource)
|
||||
: pins_(pins)
|
||||
, programSource_(programSource)
|
||||
, booted_(false)
|
||||
, i2cBus_(nullptr)
|
||||
, i2cDev_(nullptr)
|
||||
namespace adau1701
|
||||
{
|
||||
}
|
||||
|
||||
Adau1701Driver::~Adau1701Driver()
|
||||
{
|
||||
auto* dev = static_cast<i2c_master_dev_handle_t>(i2cDev_);
|
||||
auto* bus = static_cast<i2c_master_bus_handle_t>(i2cBus_);
|
||||
if (dev != nullptr) {
|
||||
i2c_master_bus_rm_device(dev);
|
||||
namespace
|
||||
{
|
||||
constexpr char kTag[] = "Adau1701";
|
||||
constexpr int kI2cPort = 0;
|
||||
/** Index 0 is the fixed high-pass band (SigmaStudio band 1); not safeloaded. */
|
||||
constexpr std::uint8_t kFixedHighPassBandIndex = 0U;
|
||||
} // namespace
|
||||
|
||||
Adau1701Driver::Adau1701Driver(Adau1701Pins pins,
|
||||
core::IDspProgramSource &programSource)
|
||||
: pins_(pins), programSource_(programSource), booted_(false), i2cBus_(nullptr), i2cDev_(nullptr)
|
||||
{
|
||||
}
|
||||
if (bus != nullptr) {
|
||||
i2c_del_master_bus(bus);
|
||||
}
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::replayProgram(
|
||||
const core::DspProgram& program)
|
||||
{
|
||||
const unsigned char deviceAddr =
|
||||
static_cast<unsigned char>(pins_.i2cAddr7 << 1);
|
||||
for (const core::RegisterWrite& write : program.writes()) {
|
||||
const auto data = write.data();
|
||||
if (data.empty()) {
|
||||
return std::unexpected(Adau1701Error::DownloadFailed);
|
||||
Adau1701Driver::~Adau1701Driver()
|
||||
{
|
||||
auto *dev = static_cast<i2c_master_dev_handle_t>(i2cDev_);
|
||||
auto *bus = static_cast<i2c_master_bus_handle_t>(i2cBus_);
|
||||
if (dev != nullptr)
|
||||
{
|
||||
i2c_master_bus_rm_device(dev);
|
||||
}
|
||||
if (bus != nullptr)
|
||||
{
|
||||
i2c_del_master_bus(bus);
|
||||
}
|
||||
SIGMA_WRITE_REGISTER_BLOCK(
|
||||
deviceAddr,
|
||||
write.address(),
|
||||
static_cast<unsigned int>(data.size()),
|
||||
const_cast<ADI_REG_TYPE*>(
|
||||
reinterpret_cast<const ADI_REG_TYPE*>(data.data())));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::boot()
|
||||
{
|
||||
if (booted_) {
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::replayProgram(
|
||||
const core::DspProgram &program)
|
||||
{
|
||||
const unsigned char deviceAddr =
|
||||
static_cast<unsigned char>(pins_.i2cAddr7 << 1);
|
||||
for (const core::RegisterWrite &write : program.writes())
|
||||
{
|
||||
const auto data = write.data();
|
||||
if (data.empty())
|
||||
{
|
||||
return std::unexpected(Adau1701Error::DownloadFailed);
|
||||
}
|
||||
SIGMA_WRITE_REGISTER_BLOCK(
|
||||
deviceAddr,
|
||||
write.address(),
|
||||
static_cast<unsigned int>(data.size()),
|
||||
const_cast<ADI_REG_TYPE *>(
|
||||
reinterpret_cast<const ADI_REG_TYPE *>(data.data())));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
gpio_config_t resetCfg = {};
|
||||
resetCfg.pin_bit_mask = 1ULL << pins_.resetGpio;
|
||||
resetCfg.mode = GPIO_MODE_OUTPUT;
|
||||
if (gpio_config(&resetCfg) != ESP_OK) {
|
||||
return std::unexpected(Adau1701Error::ResetFailed);
|
||||
}
|
||||
|
||||
gpio_set_level(static_cast<gpio_num_t>(pins_.resetGpio), 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
gpio_set_level(static_cast<gpio_num_t>(pins_.resetGpio), 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
|
||||
i2c_master_bus_config_t busCfg = {};
|
||||
busCfg.i2c_port = static_cast<i2c_port_num_t>(kI2cPort);
|
||||
busCfg.sda_io_num = static_cast<gpio_num_t>(pins_.i2cSda);
|
||||
busCfg.scl_io_num = static_cast<gpio_num_t>(pins_.i2cScl);
|
||||
busCfg.clk_source = I2C_CLK_SRC_DEFAULT;
|
||||
busCfg.glitch_ignore_cnt = 7;
|
||||
busCfg.flags.enable_internal_pullup = true;
|
||||
|
||||
i2c_master_bus_handle_t bus = nullptr;
|
||||
if (i2c_new_master_bus(&busCfg, &bus) != ESP_OK) {
|
||||
ESP_LOGE(kTag, "i2c_new_master_bus failed");
|
||||
return std::unexpected(Adau1701Error::I2cInitFailed);
|
||||
}
|
||||
i2cBus_ = bus;
|
||||
|
||||
i2c_device_config_t devCfg = {};
|
||||
devCfg.dev_addr_length = I2C_ADDR_BIT_LEN_7;
|
||||
devCfg.device_address = static_cast<uint16_t>(pins_.i2cAddr7);
|
||||
devCfg.scl_speed_hz = 100000;
|
||||
|
||||
i2c_master_dev_handle_t dev = nullptr;
|
||||
if (i2c_master_bus_add_device(bus, &devCfg, &dev) != ESP_OK) {
|
||||
ESP_LOGE(kTag, "i2c_master_bus_add_device failed");
|
||||
return std::unexpected(Adau1701Error::I2cInitFailed);
|
||||
}
|
||||
i2cDev_ = dev;
|
||||
|
||||
sigma_studio_bind_i2c(kI2cPort, static_cast<unsigned char>(pins_.i2cAddr7));
|
||||
sigma_studio_set_device(dev);
|
||||
|
||||
const auto program = programSource_.loadProgram();
|
||||
if (!program) {
|
||||
ESP_LOGE(kTag, "DSP program load failed");
|
||||
return std::unexpected(Adau1701Error::DownloadFailed);
|
||||
}
|
||||
if (auto replay = replayProgram(*program); !replay) {
|
||||
return replay;
|
||||
}
|
||||
|
||||
booted_ = true;
|
||||
ESP_LOGI(kTag, "SigmaStudio program loaded");
|
||||
return {};
|
||||
}
|
||||
|
||||
bool Adau1701Driver::isBooted() const noexcept
|
||||
{
|
||||
return booted_;
|
||||
}
|
||||
|
||||
void* Adau1701Driver::i2cBusHandle() const noexcept
|
||||
{
|
||||
return i2cBus_;
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::ensureBooted() const
|
||||
{
|
||||
if (!booted_) {
|
||||
return std::unexpected(Adau1701Error::NotBooted);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::safeloadFixpoint(
|
||||
unsigned paramAddr, std::int32_t fixpoint)
|
||||
{
|
||||
if (sigma_safeload_param(paramAddr, fixpoint) != 0) {
|
||||
return std::unexpected(Adau1701Error::SafeloadFailed);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::safeloadGain(
|
||||
unsigned paramAddr, core::GainDb gain)
|
||||
{
|
||||
return safeloadFixpoint(paramAddr, core::gainDbToLinearFixpoint(gain));
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::setInputVolume(
|
||||
core::MixSource source, core::GainDb left, core::GainDb right)
|
||||
{
|
||||
if (auto ready = ensureBooted(); !ready) {
|
||||
return ready;
|
||||
}
|
||||
if (auto result = safeloadGain(paramAddrInputLeft(source), left); !result) {
|
||||
return result;
|
||||
}
|
||||
return safeloadGain(paramAddrInputRight(source), right);
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::setMasterVolume(
|
||||
core::GainDb left, core::GainDb right)
|
||||
{
|
||||
if (auto ready = ensureBooted(); !ready) {
|
||||
return ready;
|
||||
}
|
||||
if (auto result = safeloadGain(static_cast<unsigned>(ADDR_MULTIPLE1), left);
|
||||
!result) {
|
||||
return result;
|
||||
}
|
||||
return safeloadGain(static_cast<unsigned>(ADDR_MULTIPLE1_1), right);
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::applyMixer(
|
||||
const core::MixerState& mixer)
|
||||
{
|
||||
if (auto ready = ensureBooted(); !ready) {
|
||||
return ready;
|
||||
}
|
||||
if (auto result = setInputVolume(core::MixSource::Si4684, mixer.si4684Left,
|
||||
mixer.si4684Right);
|
||||
!result) {
|
||||
return result;
|
||||
}
|
||||
if (auto result = setInputVolume(core::MixSource::Esp32, mixer.esp32Left,
|
||||
mixer.esp32Right);
|
||||
!result) {
|
||||
return result;
|
||||
}
|
||||
if (auto result =
|
||||
safeloadGain(static_cast<unsigned>(ADDR_STMIXER1_ST0_VOLUME),
|
||||
mixer.mixLeft);
|
||||
!result) {
|
||||
return result;
|
||||
}
|
||||
return safeloadGain(static_cast<unsigned>(ADDR_STMIXER1_ST1_VOLUME),
|
||||
mixer.mixRight);
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::setEqBand(
|
||||
core::EqBandIndex band, core::GainDb gain, core::FrequencyHz center, float q)
|
||||
{
|
||||
if (band.value() == kFixedHighPassBandIndex) {
|
||||
return std::unexpected(Adau1701Error::InvalidParameter);
|
||||
}
|
||||
|
||||
if (auto ready = ensureBooted(); !ready) {
|
||||
return ready;
|
||||
}
|
||||
|
||||
const core::BiquadCoefficients coeffs =
|
||||
core::designPeakingEq(center, gain, q);
|
||||
const auto fixpoints = coeffs.toFixpoint823();
|
||||
const unsigned baseAddr = paramAddrEqBandBase(band.value());
|
||||
|
||||
unsigned addrs[5U];
|
||||
int values[5U];
|
||||
for (unsigned i = 0U; i < 5U; ++i) {
|
||||
addrs[i] = baseAddr + i;
|
||||
values[i] = fixpoints[i];
|
||||
}
|
||||
|
||||
if (sigma_safeload_block(5U, addrs, values) != 0) {
|
||||
return std::unexpected(Adau1701Error::SafeloadFailed);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::applyEq(
|
||||
const core::EqProfile& eq)
|
||||
{
|
||||
if (auto ready = ensureBooted(); !ready) {
|
||||
return ready;
|
||||
}
|
||||
|
||||
for (std::uint8_t i = 0; i < core::EqBandIndex::kBandCount; ++i) {
|
||||
if (i == kFixedHighPassBandIndex) {
|
||||
continue;
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::boot()
|
||||
{
|
||||
if (booted_)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
const auto index = core::EqBandIndex::tryFromIndex(i);
|
||||
if (!index) {
|
||||
|
||||
gpio_config_t resetCfg = {};
|
||||
resetCfg.pin_bit_mask = 1ULL << pins_.resetGpio;
|
||||
resetCfg.mode = GPIO_MODE_OUTPUT;
|
||||
if (gpio_config(&resetCfg) != ESP_OK)
|
||||
{
|
||||
return std::unexpected(Adau1701Error::ResetFailed);
|
||||
}
|
||||
|
||||
gpio_set_level(static_cast<gpio_num_t>(pins_.resetGpio), 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
gpio_set_level(static_cast<gpio_num_t>(pins_.resetGpio), 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
|
||||
i2c_master_bus_config_t busCfg = {};
|
||||
busCfg.i2c_port = static_cast<i2c_port_num_t>(kI2cPort);
|
||||
busCfg.sda_io_num = static_cast<gpio_num_t>(pins_.i2cSda);
|
||||
busCfg.scl_io_num = static_cast<gpio_num_t>(pins_.i2cScl);
|
||||
busCfg.clk_source = I2C_CLK_SRC_DEFAULT;
|
||||
busCfg.glitch_ignore_cnt = 7;
|
||||
busCfg.flags.enable_internal_pullup = true;
|
||||
|
||||
i2c_master_bus_handle_t bus = nullptr;
|
||||
if (i2c_new_master_bus(&busCfg, &bus) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(kTag, "i2c_new_master_bus failed");
|
||||
return std::unexpected(Adau1701Error::I2cInitFailed);
|
||||
}
|
||||
i2cBus_ = bus;
|
||||
|
||||
i2c_device_config_t devCfg = {};
|
||||
devCfg.dev_addr_length = I2C_ADDR_BIT_LEN_7;
|
||||
devCfg.device_address = static_cast<uint16_t>(pins_.i2cAddr7);
|
||||
devCfg.scl_speed_hz = 100000;
|
||||
|
||||
i2c_master_dev_handle_t dev = nullptr;
|
||||
if (i2c_master_bus_add_device(bus, &devCfg, &dev) != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(kTag, "i2c_master_bus_add_device failed");
|
||||
return std::unexpected(Adau1701Error::I2cInitFailed);
|
||||
}
|
||||
i2cDev_ = dev;
|
||||
|
||||
sigma_studio_bind_i2c(kI2cPort, static_cast<unsigned char>(pins_.i2cAddr7));
|
||||
sigma_studio_set_device(dev);
|
||||
|
||||
const auto program = programSource_.loadProgram();
|
||||
if (!program)
|
||||
{
|
||||
ESP_LOGE(kTag, "DSP program load failed");
|
||||
return std::unexpected(Adau1701Error::DownloadFailed);
|
||||
}
|
||||
ESP_LOGI(kTag,
|
||||
"DSP program contains %u writes",
|
||||
static_cast<unsigned>(program->writes().size()));
|
||||
for (const auto &w : program->writes())
|
||||
{
|
||||
ESP_LOGI(kTag,
|
||||
"ADDR=0x%04X LEN=%u",
|
||||
static_cast<unsigned>(w.address()),
|
||||
static_cast<unsigned>(w.data().size()));
|
||||
}
|
||||
if (auto replay = replayProgram(*program); !replay)
|
||||
{
|
||||
return replay;
|
||||
}
|
||||
|
||||
booted_ = true;
|
||||
ESP_LOGI(kTag, "SigmaStudio program loaded");
|
||||
return {};
|
||||
}
|
||||
|
||||
bool Adau1701Driver::isBooted() const noexcept
|
||||
{
|
||||
return booted_;
|
||||
}
|
||||
|
||||
void *Adau1701Driver::i2cBusHandle() const noexcept
|
||||
{
|
||||
return i2cBus_;
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::ensureBooted() const
|
||||
{
|
||||
if (!booted_)
|
||||
{
|
||||
return std::unexpected(Adau1701Error::NotBooted);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::safeloadFixpoint(
|
||||
unsigned paramAddr, std::int32_t fixpoint)
|
||||
{
|
||||
sigma_studio_lock();
|
||||
const int result = sigma_safeload_param(paramAddr, fixpoint);
|
||||
sigma_studio_unlock();
|
||||
if (result != 0)
|
||||
{
|
||||
return std::unexpected(Adau1701Error::SafeloadFailed);
|
||||
}
|
||||
const core::EqBandSettings& band = eq.band(*index);
|
||||
if (auto result = setEqBand(*index, band.gain, band.center, band.q);
|
||||
!result) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::safeloadGain(
|
||||
unsigned paramAddr, core::GainDb gain)
|
||||
{
|
||||
return safeloadFixpoint(paramAddr, core::gainDbToLinearFixpoint(gain));
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::setInputVolume(
|
||||
core::MixSource source, core::GainDb left, core::GainDb right)
|
||||
{
|
||||
if (auto ready = ensureBooted(); !ready)
|
||||
{
|
||||
return ready;
|
||||
}
|
||||
if (auto result = safeloadGain(paramAddrInputLeft(source), left); !result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return safeloadGain(paramAddrInputRight(source), right);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::applyProfile(
|
||||
const core::AudioProfile& profile)
|
||||
{
|
||||
if (auto ready = ensureBooted(); !ready) {
|
||||
return ready;
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::setMasterVolume(
|
||||
core::GainDb left, core::GainDb right)
|
||||
{
|
||||
if (auto ready = ensureBooted(); !ready)
|
||||
{
|
||||
return ready;
|
||||
}
|
||||
if (auto result = safeloadGain(static_cast<unsigned>(ADDR_MULTIPLE1), left);
|
||||
!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return safeloadGain(static_cast<unsigned>(ADDR_MULTIPLE1_1), right);
|
||||
}
|
||||
if (auto result = applyMixer(profile.mixer); !result) {
|
||||
return result;
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::applyMixer(
|
||||
const core::MixerState &mixer)
|
||||
{
|
||||
if (auto ready = ensureBooted(); !ready)
|
||||
{
|
||||
return ready;
|
||||
}
|
||||
if (auto result = setInputVolume(core::MixSource::Si4684, mixer.si4684Left,
|
||||
mixer.si4684Right);
|
||||
!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
if (auto result = setInputVolume(core::MixSource::Esp32, mixer.esp32Left,
|
||||
mixer.esp32Right);
|
||||
!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
if (auto result =
|
||||
safeloadGain(static_cast<unsigned>(ADDR_STMIXER1_ST0_VOLUME),
|
||||
mixer.mixLeft);
|
||||
!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return safeloadGain(static_cast<unsigned>(ADDR_STMIXER1_ST1_VOLUME),
|
||||
mixer.mixRight);
|
||||
}
|
||||
if (auto result = applyEq(profile.eq); !result) {
|
||||
return result;
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::setEqBand(
|
||||
core::EqBandIndex band, core::GainDb gain, core::FrequencyHz center, float q)
|
||||
{
|
||||
if (band.value() == kFixedHighPassBandIndex)
|
||||
{
|
||||
return std::unexpected(Adau1701Error::InvalidParameter);
|
||||
}
|
||||
|
||||
if (auto ready = ensureBooted(); !ready)
|
||||
{
|
||||
return ready;
|
||||
}
|
||||
|
||||
const core::BiquadCoefficients coeffs =
|
||||
core::designPeakingEq(center, gain, q);
|
||||
const auto fixpoints = coeffs.toFixpoint823();
|
||||
const unsigned baseAddr = paramAddrEqBandBase(band.value());
|
||||
|
||||
unsigned addrs[5U];
|
||||
int values[5U];
|
||||
for (unsigned i = 0U; i < 5U; ++i)
|
||||
{
|
||||
addrs[i] = baseAddr + i;
|
||||
values[i] = fixpoints[i];
|
||||
}
|
||||
|
||||
sigma_studio_lock();
|
||||
const int result = sigma_safeload_block(5U, addrs, values);
|
||||
sigma_studio_unlock();
|
||||
if (result != 0)
|
||||
{
|
||||
return std::unexpected(Adau1701Error::SafeloadFailed);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::setBeepEnabled(
|
||||
bool enabled)
|
||||
{
|
||||
if (auto ready = ensureBooted(); !ready)
|
||||
{
|
||||
return ready;
|
||||
}
|
||||
// Beep1 ("Beep - variable gain", ADI Sound Generation toolbox,
|
||||
// DigiRadio.params) ENABLE/KICK: unity fixpoint 0x00800000 = on,
|
||||
// exact zero = off. Not continuous gains, so bypass
|
||||
// safeloadGain/GainDb (whose quietest value is -96 dB, not true
|
||||
// zero) and write the raw fixpoint. KICK is the cell's trigger
|
||||
// input (per its own SigmaStudio parameter name); ENABLE alone
|
||||
// may leave the generator gated shut without it.
|
||||
const std::int32_t value =
|
||||
enabled ? core::gainDbToLinearFixpoint(core::GainDb::zero()) : 0;
|
||||
if (auto result = safeloadFixpoint(
|
||||
static_cast<unsigned>(ADDR_BEEP1_ENABLE), value);
|
||||
!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return safeloadFixpoint(static_cast<unsigned>(ADDR_BEEP1_KICK), value);
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::applyEq(
|
||||
const core::EqProfile &eq)
|
||||
{
|
||||
if (auto ready = ensureBooted(); !ready)
|
||||
{
|
||||
return ready;
|
||||
}
|
||||
|
||||
for (std::uint8_t i = 0; i < core::EqBandIndex::kBandCount; ++i)
|
||||
{
|
||||
if (i == kFixedHighPassBandIndex)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
const auto index = core::EqBandIndex::tryFromIndex(i);
|
||||
if (!index)
|
||||
{
|
||||
return std::unexpected(Adau1701Error::SafeloadFailed);
|
||||
}
|
||||
const core::EqBandSettings &band = eq.band(*index);
|
||||
if (auto result = setEqBand(*index, band.gain, band.center, band.q);
|
||||
!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, Adau1701Error> Adau1701Driver::applyProfile(
|
||||
const core::AudioProfile &profile)
|
||||
{
|
||||
if (auto ready = ensureBooted(); !ready)
|
||||
{
|
||||
return ready;
|
||||
}
|
||||
if (auto result = applyMixer(profile.mixer); !result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
if (auto result = applyEq(profile.eq); !result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return setMasterVolume(profile.masterLeft, profile.masterRight);
|
||||
}
|
||||
return setMasterVolume(profile.masterLeft, profile.masterRight);
|
||||
}
|
||||
|
||||
} // namespace adau1701
|
||||
|
||||
@@ -89,4 +89,12 @@ std::expected<void, core::DspError> Adau1701Dsp::setEqBand(
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, core::DspError> Adau1701Dsp::setBeepEnabled(bool enabled)
|
||||
{
|
||||
if (auto result = driver_.setBeepEnabled(enabled); !result) {
|
||||
return std::unexpected(mapError(result.error()));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace adau1701
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_partition.h"
|
||||
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
|
||||
namespace adau1701 {
|
||||
@@ -75,26 +75,60 @@ FlashDspProgramSource::loadProgram()
|
||||
return std::unexpected(core::DspProgramError::Empty);
|
||||
}
|
||||
|
||||
// Partizione non vuota: alloca il backing store con new(nothrow), cosi'
|
||||
// un OOM ritorna nullptr invece di abortire (nessuna eccezione).
|
||||
auto* raw = new (std::nothrow) std::uint8_t[part->size];
|
||||
// Determine exact blob size by scanning DRAD write records before
|
||||
// allocating — the full partition (256 KB) exceeds the available heap
|
||||
// on ESP32-S3, and new(nothrow) still invokes __cxa_allocate_exception
|
||||
// when exceptions are disabled, causing abort().
|
||||
|
||||
// Read DRAD header (12 bytes) to get write_count.
|
||||
constexpr std::size_t kHdrSize = 12U;
|
||||
std::uint8_t hdr[kHdrSize] = {};
|
||||
if (esp_partition_read(part, 0, hdr, kHdrSize) != ESP_OK) {
|
||||
return std::unexpected(core::DspProgramError::FlashReadFailed);
|
||||
}
|
||||
// Quick magic/version check (full validation done by parseDspProgramBlob).
|
||||
if (hdr[0] != 'D' || hdr[1] != 'R' || hdr[2] != 'A' || hdr[3] != 'D'
|
||||
|| static_cast<std::uint16_t>(hdr[4] | (hdr[5] << 8)) != 1U) {
|
||||
return std::unexpected(core::DspProgramError::FlashReadFailed);
|
||||
}
|
||||
const auto writeCount =
|
||||
static_cast<std::uint16_t>(hdr[6] | (hdr[7] << 8));
|
||||
if (writeCount == 0U || writeCount > 32U) {
|
||||
return std::unexpected(core::DspProgramError::FlashReadFailed);
|
||||
}
|
||||
|
||||
// Scan write record headers (4 bytes each) to compute total payload size.
|
||||
std::size_t payloadSize = 0U;
|
||||
for (std::uint16_t i = 0U; i < writeCount; ++i) {
|
||||
std::uint8_t rec[4] = {};
|
||||
if (esp_partition_read(part, kHdrSize + payloadSize, rec, 4U)
|
||||
!= ESP_OK) {
|
||||
return std::unexpected(core::DspProgramError::FlashReadFailed);
|
||||
}
|
||||
const auto dataLen =
|
||||
static_cast<std::uint16_t>(rec[2] | (rec[3] << 8));
|
||||
if (dataLen == 0U || dataLen > 16384U) {
|
||||
return std::unexpected(core::DspProgramError::FlashReadFailed);
|
||||
}
|
||||
payloadSize += 4U + dataLen;
|
||||
}
|
||||
|
||||
const std::size_t totalSize = kHdrSize + payloadSize;
|
||||
|
||||
// Use malloc — avoids C++ exception machinery entirely (no nothrow workaround).
|
||||
auto* raw = static_cast<std::uint8_t*>(::malloc(totalSize));
|
||||
if (raw == nullptr) {
|
||||
ESP_LOGE(kTag, "dsp buffer alloc failed (%u byte)",
|
||||
static_cast<unsigned>(part->size));
|
||||
ESP_LOGE(kTag, "dsp buffer alloc failed (%u bytes)",
|
||||
static_cast<unsigned>(totalSize));
|
||||
return std::unexpected(core::DspProgramError::FlashReadFailed);
|
||||
}
|
||||
std::unique_ptr<std::uint8_t[]> guard(raw);
|
||||
std::unique_ptr<std::uint8_t, decltype(&::free)> guard(raw, ::free);
|
||||
|
||||
if (esp_partition_read(part, 0, raw, part->size) != ESP_OK) {
|
||||
if (esp_partition_read(part, 0, raw, totalSize) != ESP_OK) {
|
||||
return std::unexpected(core::DspProgramError::FlashReadFailed);
|
||||
}
|
||||
|
||||
std::span<const std::uint8_t> view(raw, part->size);
|
||||
if (partitionLooksEmpty(view)) {
|
||||
return std::unexpected(core::DspProgramError::Empty);
|
||||
}
|
||||
|
||||
return core::parseDspProgramBlob(view);
|
||||
return core::parseDspProgramBlob({raw, totalSize});
|
||||
}
|
||||
|
||||
std::expected<void, core::DspProgramError>
|
||||
|
||||
@@ -14,10 +14,13 @@
|
||||
#include "SigmaStudioFW.h"
|
||||
|
||||
#include "driver/i2c_master.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static i2c_master_dev_handle_t s_dev = NULL;
|
||||
static SemaphoreHandle_t s_lock = NULL;
|
||||
|
||||
void sigma_studio_bind_i2c(int port, unsigned char addr7)
|
||||
{
|
||||
@@ -28,6 +31,45 @@ void sigma_studio_bind_i2c(int port, unsigned char addr7)
|
||||
void sigma_studio_set_device(void* i2cDevHandle)
|
||||
{
|
||||
s_dev = (i2c_master_dev_handle_t)i2cDevHandle;
|
||||
if (s_lock == NULL) {
|
||||
s_lock = xSemaphoreCreateMutex();
|
||||
}
|
||||
}
|
||||
|
||||
void sigma_studio_lock(void)
|
||||
{
|
||||
if (s_lock != NULL) {
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
void sigma_studio_unlock(void)
|
||||
{
|
||||
if (s_lock != NULL) {
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The ADAU1701 memory map is word-indexed with a region-dependent word
|
||||
* width (Param RAM = 4 bytes, Program RAM = 5 bytes, Control regs =
|
||||
* 2 bytes) — confirmed by DigiRadio's own generated export
|
||||
* (DigiRadio_IC_1.h: PROGRAM_ADDR_IC_1=1024/PROGRAM_SIZE_IC_1=5120,
|
||||
* PARAM_ADDR_IC_1=0/PARAM_SIZE_IC_1=4096; DigiRadio_IC_1_REG.h:
|
||||
* REG_COREREGISTER_IC_1_ADDR=0x81C) and independently by the
|
||||
* ADAU1701-TCPi-ESP32 reference project's directWrite(). Chunk
|
||||
* boundaries must land on whole words, or the address advance for the
|
||||
* next I2C transaction (and the data itself, mid-word) is wrong.
|
||||
*/
|
||||
static unsigned int sigmaWordSize(unsigned int address)
|
||||
{
|
||||
if (address >= 0x0400U && address <= 0x07FFU) {
|
||||
return 5U;
|
||||
}
|
||||
if (address >= 0x0800U) {
|
||||
return 2U;
|
||||
}
|
||||
return 4U;
|
||||
}
|
||||
|
||||
void SIGMA_WRITE_REGISTER_BLOCK(unsigned char devAddress,
|
||||
@@ -40,25 +82,45 @@ void SIGMA_WRITE_REGISTER_BLOCK(unsigned char devAddress,
|
||||
return;
|
||||
}
|
||||
|
||||
enum { kChunk = 64U };
|
||||
enum { kChunkBytesMax = 64U };
|
||||
const unsigned int wordSize = sigmaWordSize(address);
|
||||
const unsigned int wordsPerChunk = kChunkBytesMax / wordSize;
|
||||
const unsigned int chunkBytes = wordsPerChunk * wordSize;
|
||||
|
||||
unsigned int addr = address;
|
||||
unsigned int remaining = length;
|
||||
ADI_REG_TYPE* cursor = pData;
|
||||
|
||||
while (remaining > 0U) {
|
||||
const unsigned int chunk =
|
||||
remaining > kChunk ? kChunk : remaining;
|
||||
unsigned char buf[2U + 64U];
|
||||
remaining > chunkBytes ? chunkBytes : remaining;
|
||||
const unsigned int words = chunk / wordSize;
|
||||
unsigned char buf[2U + kChunkBytesMax];
|
||||
buf[0] = (unsigned char)((addr >> 8) & 0xFFU);
|
||||
buf[1] = (unsigned char)(addr & 0xFFU);
|
||||
memcpy(buf + 2U, cursor, chunk);
|
||||
i2c_master_transmit(s_dev, buf, (size_t)(2U + chunk), 1000);
|
||||
addr += chunk;
|
||||
addr += words;
|
||||
cursor += chunk;
|
||||
remaining -= chunk;
|
||||
}
|
||||
}
|
||||
|
||||
int sigma_i2c_read(unsigned int reg, unsigned char* data, unsigned int length)
|
||||
{
|
||||
if (s_dev == NULL || data == NULL || length == 0U) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char addrBuf[2U];
|
||||
addrBuf[0] = (unsigned char)((reg >> 8) & 0xFFU);
|
||||
addrBuf[1] = (unsigned char)(reg & 0xFFU);
|
||||
|
||||
const esp_err_t err = i2c_master_transmit_receive(
|
||||
s_dev, addrBuf, sizeof(addrBuf), data, (size_t)length, 1000);
|
||||
return err == ESP_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
static int sigma_i2c_write(unsigned int reg, const unsigned char* data,
|
||||
unsigned char length)
|
||||
{
|
||||
@@ -66,7 +128,7 @@ static int sigma_i2c_write(unsigned int reg, const unsigned char* data,
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char buf[2U + 4U];
|
||||
unsigned char buf[2U + 5U];
|
||||
if ((size_t)length + 2U > sizeof(buf)) {
|
||||
return -1;
|
||||
}
|
||||
@@ -78,14 +140,25 @@ static int sigma_i2c_write(unsigned int reg, const unsigned char* data,
|
||||
return err == ESP_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Safeload Data registers are 5 bytes wide (fixed 0x00 qualifier byte +
|
||||
* full sign-extended 32-bit value, MSB first) — confirmed against the
|
||||
* ADAU1701-TCPi-ESP32 reference project's safeloadChunk(), which sends
|
||||
* the identical 5-byte layout. `fixpoint` is a full-range Q8.23
|
||||
* two's-complement value (core::floatToFixpoint823): the previous
|
||||
* 4-byte payload here dropped the top (sign) byte entirely, silently
|
||||
* corrupting every negative coefficient (e.g. biquad a1/b1, routinely
|
||||
* negative for peaking/shelving EQ bands).
|
||||
*/
|
||||
static int sigma_write_fixpoint_reg(unsigned int reg, int fixpoint)
|
||||
{
|
||||
unsigned char payload[4U];
|
||||
unsigned char payload[5U];
|
||||
payload[0] = 0U;
|
||||
payload[1] = (unsigned char)((fixpoint >> 16) & 0xFFU);
|
||||
payload[2] = (unsigned char)((fixpoint >> 8) & 0xFFU);
|
||||
payload[3] = (unsigned char)(fixpoint & 0xFFU);
|
||||
return sigma_i2c_write(reg, payload, 4U);
|
||||
payload[1] = (unsigned char)(((unsigned int)fixpoint >> 24) & 0xFFU);
|
||||
payload[2] = (unsigned char)(((unsigned int)fixpoint >> 16) & 0xFFU);
|
||||
payload[3] = (unsigned char)(((unsigned int)fixpoint >> 8) & 0xFFU);
|
||||
payload[4] = (unsigned char)((unsigned int)fixpoint & 0xFFU);
|
||||
return sigma_i2c_write(reg, payload, 5U);
|
||||
}
|
||||
|
||||
static int sigma_write_param_addr(unsigned int reg, unsigned int paramAddr)
|
||||
@@ -133,3 +206,30 @@ int sigma_safeload_block(unsigned char count, const unsigned int* paramAddrs,
|
||||
|
||||
return sigma_trigger_safeload();
|
||||
}
|
||||
|
||||
int sigma_safeload_raw_block(unsigned char count, const unsigned int* paramAddrs,
|
||||
const unsigned char* rawWords)
|
||||
{
|
||||
if (count == 0U || count > 5U || paramAddrs == NULL || rawWords == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (unsigned char i = 0U; i < count; ++i) {
|
||||
const unsigned int dataReg =
|
||||
ADAU1701_SAFELOAD_DATA_BASE + (unsigned int)i;
|
||||
const unsigned int addrReg =
|
||||
ADAU1701_SAFELOAD_ADDR_BASE + (unsigned int)i;
|
||||
|
||||
unsigned char payload[5U];
|
||||
payload[0] = 0U;
|
||||
memcpy(payload + 1U, rawWords + ((unsigned int)i * 4U), 4U);
|
||||
if (sigma_i2c_write(dataReg, payload, 5U) != 0) {
|
||||
return -1;
|
||||
}
|
||||
if (sigma_write_param_addr(addrReg, paramAddrs[i]) != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return sigma_trigger_safeload();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user