Add updatable ADAU1701 program via flash partition and HTTP API.

Decouple the SigmaStudio RAM download from compiled-in adau1701_program.c:
DRAD v1 blobs in the dsp partition with embedded fallback, POST /api/dsp/program,
and host-tested blob parse/serialize helpers.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-07 09:21:49 +02:00
co-authored by Cursor
parent cd94e36a9d
commit cc4a03168e
31 changed files with 1302 additions and 50 deletions
@@ -4,12 +4,14 @@ idf_component_register(
SRCS
"src/Adau1701Driver.cpp"
"src/Adau1701Dsp.cpp"
"src/EmbeddedDspProgramSource.cpp"
"src/FlashDspProgramSource.cpp"
"src/FallbackDspProgramSource.cpp"
"src/SigmaStudioFW.c"
"src/adau1701_program.c"
INCLUDE_DIRS
"include"
"${ADAU_FW_DIR}"
REQUIRES core driver esp_driver_gpio esp_driver_i2c
REQUIRES core driver esp_driver_gpio esp_driver_i2c esp_partition
)
target_compile_features(${COMPONENT_LIB} PUBLIC cxx_std_23)
@@ -19,6 +19,7 @@
#include "core/EqProfile.hpp"
#include "core/FrequencyHz.hpp"
#include "core/GainDb.hpp"
#include "core/IDspProgramSource.hpp"
#include "core/MixSource.hpp"
#include "core/MixerState.hpp"
@@ -48,10 +49,11 @@ struct Adau1701Pins {
* @brief Adau1701Driver — owns I2C + reset, boot, and safeload runtime.
*
* @dname Adau1701Driver
* @param pins Board wiring for I2C and RESET#.
* @param pins Board wiring for I2C and RESET#.
* @param programSource RAM download script (flash with embedded fallback).
* @return n/a (type)
* @pubstate Owns I2C bus/device handles (RAII). booted_ true after a
* successful default_download replay.
* successful program replay.
*
* Writes the SigmaStudio export from Firmware/ADAU1701-Firmware on every
* boot (no EEPROM self-boot on DigiRadio). Runtime EQ and mixer changes
@@ -63,16 +65,18 @@ struct Adau1701Pins {
class Adau1701Driver {
public:
/**
* @brief Adau1701Driver — construct with board pin map.
* @brief Adau1701Driver — construct with board pin map and program source.
*
* @dname Adau1701Driver
* @param pins SDA/SCL/reset/address configuration.
* @pubstate stores pins_; not booted until boot().
* @param pins SDA/SCL/reset/address configuration.
* @param programSource Download script provider for boot().
* @pubstate stores pins_ and programSource_; not booted until boot().
*
* @author Michele Bigi
* @date 2026-07-06
*/
explicit Adau1701Driver(Adau1701Pins pins);
explicit Adau1701Driver(Adau1701Pins pins,
core::IDspProgramSource& programSource);
/**
* @brief ~Adau1701Driver — release I2C resources.
@@ -222,8 +226,11 @@ private:
unsigned paramAddr, core::GainDb gain);
[[nodiscard]] std::expected<void, Adau1701Error> safeloadFixpoint(
unsigned paramAddr, std::int32_t fixpoint);
[[nodiscard]] std::expected<void, Adau1701Error> replayProgram(
const core::DspProgram& program);
Adau1701Pins pins_;
core::IDspProgramSource& programSource_;
bool booted_;
void* i2cBus_;
void* i2cDev_;
@@ -0,0 +1,45 @@
/**
* @file EmbeddedDspProgramSource.hpp
* @brief IDspProgramSource backed by the compiled SigmaStudio export.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#pragma once
#include "core/IDspProgramSource.hpp"
namespace adau1701 {
/**
* @brief EmbeddedDspProgramSource — factory default from DigiRadio_IC_1.h.
*
* @dname EmbeddedDspProgramSource
* @return n/a (type)
* @pubstate Builds the same five SIGMA_WRITE steps as default_download_IC_1().
*
* @author Michele Bigi
* @date 2026-07-07
*/
class EmbeddedDspProgramSource : public core::IDspProgramSource {
public:
/**
* @brief loadProgram — return the embedded SigmaStudio download script.
*
* @dname loadProgram
* @return DspProgram mirroring default_download_IC_1().
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] std::expected<core::DspProgram, core::DspProgramError>
loadProgram() override;
};
} // namespace adau1701
@@ -0,0 +1,63 @@
/**
* @file FallbackDspProgramSource.hpp
* @brief Tries flash partition first, then embedded SigmaStudio export.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#pragma once
#include "core/IDspProgramSource.hpp"
namespace adau1701 {
/**
* @brief FallbackDspProgramSource — flash then embedded program loader.
*
* @dname FallbackDspProgramSource
* @return n/a (type)
* @pubstate Borrows primary and fallback sources for the process lifetime.
*
* @author Michele Bigi
* @date 2026-07-07
*/
class FallbackDspProgramSource : public core::IDspProgramSource {
public:
/**
* @brief FallbackDspProgramSource — wire flash and embedded sources.
*
* @dname FallbackDspProgramSource
* @param primary Usually FlashDspProgramSource.
* @param fallback Usually EmbeddedDspProgramSource.
* @pubstate stores non-owning references.
*
* @author Michele Bigi
* @date 2026-07-07
*/
FallbackDspProgramSource(core::IDspProgramSource& primary,
core::IDspProgramSource& fallback);
/**
* @brief loadProgram — use flash when valid, else embedded export.
*
* @dname loadProgram
* @return DspProgram from primary or fallback.
* @pubstate logs when falling back to embedded.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] std::expected<core::DspProgram, core::DspProgramError>
loadProgram() override;
private:
core::IDspProgramSource& primary_;
core::IDspProgramSource& fallback_;
};
} // namespace adau1701
@@ -0,0 +1,62 @@
/**
* @file FlashDspProgramSource.hpp
* @brief IDspProgramSource reading the dsp flash partition.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#pragma once
#include "core/IDspProgramSource.hpp"
#include <cstdint>
#include <span>
namespace adau1701 {
/**
* @brief FlashDspProgramSource — loads a validated blob from partition dsp.
*
* @dname FlashDspProgramSource
* @return n/a (type)
* @pubstate Uses esp_partition API; empty/erased flash returns Empty.
*
* @author Michele Bigi
* @date 2026-07-07
*/
class FlashDspProgramSource : public core::IDspProgramSource {
public:
/**
* @brief loadProgram — read and parse the dsp data partition.
*
* @dname loadProgram
* @return DspProgram on success, or a parse/flash error.
* @pubstate reads the full partition into RAM once per call.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] std::expected<core::DspProgram, core::DspProgramError>
loadProgram() override;
/**
* @brief storeBlob — erase and write a validated blob to partition dsp.
*
* @dname storeBlob
* @param blob Framed program bytes (already validated by caller).
* @return Ok on success, or DspProgramError::FlashWriteFailed.
* @pubstate erases the dsp partition before writing.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] static std::expected<void, core::DspProgramError>
storeBlob(std::span<const std::uint8_t> blob);
};
} // namespace adau1701
@@ -16,6 +16,7 @@
#include "adau1701/Adau1701ParamMap.hpp"
#include "core/BiquadDesign.hpp"
#include "core/DspProgram.hpp"
#include "DigiRadio_IC_1_PARAM.h"
#include "SigmaStudioFW.h"
@@ -28,9 +29,6 @@
extern "C" {
/** @brief SigmaStudio default program download (generated C, not part of the C++ API). */
void adau1701_run_default_download(void);
} // extern "C"
namespace adau1701 {
@@ -42,8 +40,10 @@ constexpr int kI2cPort = 0;
constexpr std::uint8_t kFixedHighPassBandIndex = 0U;
} // namespace
Adau1701Driver::Adau1701Driver(Adau1701Pins pins)
Adau1701Driver::Adau1701Driver(Adau1701Pins pins,
core::IDspProgramSource& programSource)
: pins_(pins)
, programSource_(programSource)
, booted_(false)
, i2cBus_(nullptr)
, i2cDev_(nullptr)
@@ -62,6 +62,26 @@ Adau1701Driver::~Adau1701Driver()
}
}
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 {};
}
std::expected<void, Adau1701Error> Adau1701Driver::boot()
{
if (booted_) {
@@ -110,7 +130,14 @@ std::expected<void, Adau1701Error> Adau1701Driver::boot()
sigma_studio_bind_i2c(kI2cPort, static_cast<unsigned char>(pins_.i2cAddr7));
sigma_studio_set_device(dev);
adau1701_run_default_download();
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");
@@ -0,0 +1,60 @@
/**
* @file EmbeddedDspProgramSource.cpp
* @brief EmbeddedDspProgramSource implementation.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#include "adau1701/EmbeddedDspProgramSource.hpp"
#include "DigiRadio_IC_1.h"
#include "DigiRadio_IC_1_REG.h"
#include <vector>
namespace adau1701 {
namespace {
[[nodiscard]] std::vector<std::uint8_t> copyBytes(const ADI_REG_TYPE* data,
std::size_t size)
{
return std::vector<std::uint8_t>(
reinterpret_cast<const std::uint8_t*>(data),
reinterpret_cast<const std::uint8_t*>(data) + size);
}
} // namespace
std::expected<core::DspProgram, core::DspProgramError>
EmbeddedDspProgramSource::loadProgram()
{
std::vector<core::RegisterWrite> writes;
writes.reserve(5U);
writes.emplace_back(
static_cast<std::uint16_t>(REG_COREREGISTER_IC_1_ADDR),
copyBytes(R0_COREREGISTER_IC_1_Default, REG_COREREGISTER_IC_1_BYTE));
writes.emplace_back(
static_cast<std::uint16_t>(PROGRAM_ADDR_IC_1),
copyBytes(Program_Data_IC_1, PROGRAM_SIZE_IC_1));
writes.emplace_back(
static_cast<std::uint16_t>(PARAM_ADDR_IC_1),
copyBytes(Param_Data_IC_1, PARAM_SIZE_IC_1));
writes.emplace_back(
static_cast<std::uint16_t>(REG_COREREGISTER_IC_1_ADDR),
copyBytes(R3_HWCONFIGURATION_IC_1_Default, R3_HWCONFIGURATION_IC_1_SIZE));
writes.emplace_back(
static_cast<std::uint16_t>(REG_COREREGISTER_IC_1_ADDR),
copyBytes(R4_COREREGISTER_IC_1_Default, REG_COREREGISTER_IC_1_BYTE));
return core::DspProgram(std::move(writes));
}
} // namespace adau1701
@@ -0,0 +1,44 @@
/**
* @file FallbackDspProgramSource.cpp
* @brief FallbackDspProgramSource implementation.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#include "adau1701/FallbackDspProgramSource.hpp"
#include "esp_log.h"
namespace adau1701 {
namespace {
constexpr char kTag[] = "DspProgram";
} // namespace
FallbackDspProgramSource::FallbackDspProgramSource(
core::IDspProgramSource& primary,
core::IDspProgramSource& fallback)
: primary_(primary)
, fallback_(fallback)
{
}
std::expected<core::DspProgram, core::DspProgramError>
FallbackDspProgramSource::loadProgram()
{
if (auto primary = primary_.loadProgram(); primary) {
ESP_LOGI(kTag, "using flash DSP program");
return primary;
}
ESP_LOGW(kTag, "flash DSP program unavailable — using embedded export");
return fallback_.loadProgram();
}
} // namespace adau1701
@@ -0,0 +1,88 @@
/**
* @file FlashDspProgramSource.cpp
* @brief FlashDspProgramSource implementation.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-07
*/
#include "adau1701/FlashDspProgramSource.hpp"
#include "core/DspProgramBlob.hpp"
#include "esp_log.h"
#include "esp_partition.h"
#include <vector>
namespace adau1701 {
namespace {
constexpr char kTag[] = "FlashDsp";
constexpr std::uint8_t kDspPartitionSubtype = 0x40U;
[[nodiscard]] const esp_partition_t* dspPartition()
{
return esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
kDspPartitionSubtype,
"dsp");
}
[[nodiscard]] bool partitionLooksEmpty(std::span<const std::uint8_t> data)
{
for (const std::uint8_t byte : data) {
if (byte != 0xFFU) {
return false;
}
}
return true;
}
} // namespace
std::expected<core::DspProgram, core::DspProgramError>
FlashDspProgramSource::loadProgram()
{
const esp_partition_t* part = dspPartition();
if (part == nullptr) {
ESP_LOGW(kTag, "dsp partition missing");
return std::unexpected(core::DspProgramError::FlashReadFailed);
}
std::vector<std::uint8_t> buffer(part->size);
if (esp_partition_read(part, 0, buffer.data(), part->size) != ESP_OK) {
return std::unexpected(core::DspProgramError::FlashReadFailed);
}
if (partitionLooksEmpty(buffer)) {
return std::unexpected(core::DspProgramError::Empty);
}
return core::parseDspProgramBlob(buffer);
}
std::expected<void, core::DspProgramError>
FlashDspProgramSource::storeBlob(std::span<const std::uint8_t> blob)
{
const esp_partition_t* part = dspPartition();
if (part == nullptr || blob.size() > part->size) {
return std::unexpected(core::DspProgramError::FlashWriteFailed);
}
if (esp_partition_erase_range(part, 0, part->size) != ESP_OK) {
return std::unexpected(core::DspProgramError::FlashWriteFailed);
}
if (esp_partition_write(part, 0, blob.data(), blob.size()) != ESP_OK) {
return std::unexpected(core::DspProgramError::FlashWriteFailed);
}
ESP_LOGI(kTag, "stored %u byte DSP program blob", static_cast<unsigned>(blob.size()));
return {};
}
} // namespace adau1701
@@ -32,7 +32,7 @@ void sigma_studio_set_device(void* i2cDevHandle)
void SIGMA_WRITE_REGISTER_BLOCK(unsigned char devAddress,
unsigned int address,
unsigned char length,
unsigned int length,
ADI_REG_TYPE* pData)
{
(void)devAddress;
@@ -42,11 +42,11 @@ void SIGMA_WRITE_REGISTER_BLOCK(unsigned char devAddress,
enum { kChunk = 64U };
unsigned int addr = address;
unsigned char remaining = length;
unsigned int remaining = length;
ADI_REG_TYPE* cursor = pData;
while (remaining > 0U) {
const unsigned char chunk =
const unsigned int chunk =
remaining > kChunk ? kChunk : remaining;
unsigned char buf[2U + 64U];
buf[0] = (unsigned char)((addr >> 8) & 0xFFU);
@@ -55,7 +55,7 @@ void SIGMA_WRITE_REGISTER_BLOCK(unsigned char devAddress,
i2c_master_transmit(s_dev, buf, (size_t)(2U + chunk), 1000);
addr += chunk;
cursor += chunk;
remaining = (unsigned char)(remaining - chunk);
remaining -= chunk;
}
}
@@ -1,20 +0,0 @@
/**
* @file adau1701_program.c
* @brief SigmaStudio default download for DigiRadio ADAU1701.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-06
*/
#include "SigmaStudioFW.h"
#include "DigiRadio_IC_1.h"
void adau1701_run_default_download(void)
{
default_download_IC_1();
}