Si4684 boot fix: flash encryption off, 16MB flash, NVS enc off, main task stack 8KB, CTS timeout 10s (fixes BootCmd error 6), DMA-safe HOST_LOAD buffer

This commit is contained in:
2026-08-05 08:57:43 +02:00
parent a28120b44e
commit 34f247019a
19 changed files with 396 additions and 53 deletions
@@ -25,7 +25,8 @@ namespace adau1701 {
namespace {
constexpr char kTag[] = "FlashDsp";
constexpr std::uint8_t kDspPartitionSubtype = 0x40U;
constexpr esp_partition_subtype_t kDspPartitionSubtype =
static_cast<esp_partition_subtype_t>(0x40U);
[[nodiscard]] const esp_partition_t* dspPartition()
{
@@ -244,6 +244,8 @@ private:
[[nodiscard]] std::expected<void, Bt1035Error> transmitAndExpectOk(
std::string_view commandLine);
static constexpr int kResponseTimeoutMs = 2000;
Bt1035Pins pins_;
bool booted_;
bool uartInstalled_;
@@ -105,7 +105,8 @@ std::expected<std::string, Bt1035Error> Bt1035Driver::transmitAndCollect(
std::expected<void, Bt1035Error> Bt1035Driver::transmitAndExpectOk(
std::string_view commandLine)
{
if (auto collected = transmitAndCollect(commandLine); collected) {
auto collected = transmitAndCollect(commandLine);
if (collected) {
return {};
}
return std::unexpected(collected.error());
@@ -23,6 +23,7 @@
#include <array>
#include <cstring>
#include <span>
#include "esp_heap_caps.h"
namespace si4684 {
@@ -31,7 +32,7 @@ namespace {
constexpr char kTag[] = "Si4684";
constexpr std::size_t kSpiBufferSize = 4096U;
constexpr int kCtsPollMs = 2;
constexpr int kCtsRetries = 200;
constexpr int kCtsRetries = 5000;
constexpr int kStcRetries = 250;
constexpr int kStcPollMs = 20;
@@ -213,36 +214,57 @@ std::expected<void, Si4684Error> Si4684Driver::writeCommand(
std::expected<void, Si4684Error> Si4684Driver::hostLoadBlob(
const core::IFirmwareBlobReader& blob, std::size_t chunkPayload)
{
// Buffer DMA-capable e allineato: obbligatorio per spi_device_transmit
// con trasferimenti grandi. Un buffer sullo stack non e' DMA-safe e puo'
// corrompere i dati sui blob grossi (patch/firmware).
const std::size_t txSize = 4U + chunkPayload;
auto* tx = static_cast<std::uint8_t*>(
heap_caps_malloc(txSize, MALLOC_CAP_DMA | MALLOC_CAP_8BIT));
if (tx == nullptr) {
return std::unexpected(Si4684Error::ImageLoadFailed);
}
std::array<std::byte, 2044> payload = {};
std::size_t offset = 0U;
Si4684Error err = Si4684Error::ImageLoadFailed;
bool failed = false;
while (offset < blob.size()) {
const std::size_t maxChunk = std::min(chunkPayload, payload.size());
const std::size_t copied =
blob.read(offset, std::span<std::byte>(payload.data(), maxChunk));
if (copied == 0U) {
return std::unexpected(Si4684Error::ImageLoadFailed);
failed = true;
break;
}
std::array<std::uint8_t, kSpiBufferSize> tx = {};
std::memset(tx, 0, txSize);
tx[0] = static_cast<std::uint8_t>(Command::HostLoad);
tx[1] = 0x00U;
tx[2] = 0x00U;
tx[3] = 0x00U;
std::memcpy(tx.data() + 4U, payload.data(), copied);
std::memcpy(tx + 4U, payload.data(), copied);
spi_transaction_t txn = {};
txn.length = (4U + copied) * 8U;
txn.tx_buffer = tx.data();
txn.length = txSize * 8U;
txn.tx_buffer = tx;
if (spi_device_transmit(static_cast<spi_device_handle_t>(spiDevice_),
&txn) != ESP_OK) {
return std::unexpected(Si4684Error::ImageLoadFailed);
failed = true;
break;
}
if (auto cts = waitCts(); !cts) {
return cts;
err = cts.error();
failed = true;
break;
}
offset += copied;
}
heap_caps_free(tx);
if (failed) {
return std::unexpected(err);
}
return {};
}
@@ -396,7 +418,7 @@ std::expected<void, Si4684Error> Si4684Driver::boot(Si4684Band band)
gpio_set_level(static_cast<gpio_num_t>(pins_.rstbGpio), 0);
vTaskDelay(pdMS_TO_TICKS(5));
gpio_set_level(static_cast<gpio_num_t>(pins_.rstbGpio), 1);
vTaskDelay(pdMS_TO_TICKS(3));
vTaskDelay(pdMS_TO_TICKS(20));
if (!spiBusActive_) {
spi_bus_config_t busCfg = {};
@@ -442,7 +464,7 @@ std::expected<void, Si4684Error> Si4684Driver::boot(Si4684Band band)
return std::unexpected(Si4684Error::PowerUpFailed);
}
vTaskDelay(pdMS_TO_TICKS(1));
vTaskDelay(pdMS_TO_TICKS(20));
if (auto li = writeCommand(Command::LoadInit, nullptr, 0U); !li) {
return std::unexpected(Si4684Error::PatchLoadFailed);
@@ -557,18 +579,18 @@ std::expected<Si4684FmRsq, Si4684Error> Si4684Driver::readFmRsq()
return std::unexpected(rd.error());
}
Si4684FmRsq rsq = {};
const auto khz = chipFmFreqToKHz(readLe16(raw.data() + 6));
if (auto freq = core::FrequencyKHz::tryFromKhz(khz); freq) {
rsq.frequency = *freq;
} else {
return std::unexpected(Si4684Error::CommandFailed);
Si4684FmRsq rsq{
*freq,
static_cast<std::int8_t>(raw[8]),
static_cast<std::int8_t>(raw[9]),
(raw[4] & 0x01U) != 0U,
(raw[4] & 0x02U) != 0U,
};
return rsq;
}
rsq.valid = (raw[4] & 0x01U) != 0U;
rsq.stereo = (raw[4] & 0x02U) != 0U;
rsq.rssiDbuV = static_cast<std::int8_t>(raw[8]);
rsq.snrDb = static_cast<std::int8_t>(raw[9]);
return rsq;
return std::unexpected(Si4684Error::CommandFailed);
}
std::expected<Si4684FmRdsStatus, Si4684Error> Si4684Driver::readFmRds()
@@ -172,7 +172,8 @@ std::expected<void, core::TunerError> Si4684Tuner::tuneFm(
std::expected<core::FrequencyKHz, core::TunerError> Si4684Tuner::seekFm(
core::SeekDirection direction)
{
if (auto result = driver_.seekFm(direction, SeekBandWrap::Wrap); !result) {
const auto result = driver_.seekFm(direction, SeekBandWrap::Wrap);
if (!result) {
return std::unexpected(mapError(result.error()));
}
fmFrequency_ = *result;
@@ -191,7 +192,8 @@ Si4684Tuner::listDabServices()
return std::unexpected(mapError(events.error()));
}
if (auto list = driver_.fetchDabServiceList(); list) {
const auto list = driver_.fetchDabServiceList();
if (list) {
std::vector<core::TunerServiceEntry> out;
out.reserve(list->size());
for (const auto& item : *list) {
@@ -99,6 +99,21 @@ public:
NetBootstrap(const NetBootstrap&) = delete;
NetBootstrap& operator=(const NetBootstrap&) = delete;
/**
* @brief NetBootstrap — construct a started bootstrap.
*
* @dname NetBootstrap
* @param softAp Optional SoftAP mode host.
* @param sta Optional STA client instance.
* @param webServer HTTP server instance.
* @param state Initial network state.
* @pubstate Transfers ownership of optional network resources.
*/
NetBootstrap(std::optional<SoftApHost> softAp,
std::optional<StaClient> sta,
SetupWebServer webServer,
NetState state);
/**
* @brief NetBootstrap — move-construct from a started bootstrap.
*
@@ -148,11 +163,6 @@ public:
[[nodiscard]] NetState state() const noexcept;
private:
NetBootstrap(std::optional<SoftApHost> softAp,
std::optional<StaClient> sta,
SetupWebServer webServer,
NetState state);
std::optional<SoftApHost> softAp_;
std::optional<StaClient> sta_;
SetupWebServer webServer_;
@@ -23,10 +23,9 @@
#include "net/NetError.hpp"
#include "net/NetState.hpp"
#include "esp_http_server.h"
#include <expected>
struct httpd_req;
namespace audio {
class AudioService;
} // namespace audio
@@ -172,7 +171,7 @@ public:
const core::DeviceIdentity& deviceIdentity);
private:
httpd_handle* server_;
httpd_handle_t server_;
core::ISecureStore* store_;
NetState netState_;
tuner::TunerService* tuner_;
+17 -13
View File
@@ -63,10 +63,10 @@ constexpr char kTag[] = "SetupWebServer";
constexpr char kFirmwareVersion[] = "0.8.5";
constexpr unsigned kRebootDelaySec = 3;
extern const uint8_t www_index_html_gz_start[] asm(
"_binary_www_index_html_gz_start");
extern const uint8_t www_index_html_gz_end[] asm(
"_binary_www_index_html_gz_end");
extern const uint8_t index_html_gz_start[] asm(
"_binary_index_html_gz_start");
extern const uint8_t index_html_gz_end[] asm(
"_binary_index_html_gz_end");
/**
* @brief routeContextFrom — read handler dependencies from user_ctx.
@@ -81,7 +81,7 @@ extern const uint8_t www_index_html_gz_end[] asm(
*/
[[nodiscard]] HttpRouteContext* routeContextFrom(httpd_req_t* req) noexcept
{
return static_cast<HttpRouteContext*>(httpd_req_get_user_ctx(req));
return static_cast<HttpRouteContext*>(req->user_ctx);
}
/**
@@ -421,7 +421,7 @@ esp_err_t tunerSeekPostHandler(httpd_req_t* req)
}
std::array<char, 128> body{};
readRequestBody(req, body);
(void)readRequestBody(req, body);
const auto direction = core::parseTunerSeekJson(std::string_view(body.data()));
if (!direction) {
const std::string json =
@@ -620,11 +620,11 @@ esp_err_t audioBassEnhancePostHandler(httpd_req_t* req)
esp_err_t indexGetHandler(httpd_req_t* req)
{
const size_t length =
static_cast<size_t>(www_index_html_gz_end - www_index_html_gz_start);
static_cast<size_t>(index_html_gz_end - index_html_gz_start);
httpd_resp_set_type(req, "text/html");
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
return httpd_resp_send(req,
reinterpret_cast<const char*>(www_index_html_gz_start),
reinterpret_cast<const char*>(index_html_gz_start),
length);
}
@@ -935,7 +935,7 @@ esp_err_t bluetoothAutoReconnectPostHandler(httpd_req_t* req)
}
std::array<char, 128> body{};
readRequestBody(req, body);
(void)readRequestBody(req, body);
const auto times =
core::parseBluetoothAutoReconnectJson(std::string_view(body.data()));
if (!times) {
@@ -1112,7 +1112,8 @@ SetupWebServer::SetupWebServer()
, stations_(nullptr)
, integration_(nullptr)
, routeContext_{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, {}}
nullptr, core::CompanionChipStatus{false, false, false},
core::DeviceIdentity::unknown()}
{
}
@@ -1163,7 +1164,8 @@ SetupWebServer& SetupWebServer::operator=(SetupWebServer&& other) noexcept
other.stations_ = nullptr;
other.integration_ = nullptr;
other.routeContext_ = {nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, {}, core::DeviceIdentity::unknown()};
nullptr, nullptr, core::CompanionChipStatus{false, false, false},
core::DeviceIdentity::unknown()};
}
return *this;
}
@@ -1175,7 +1177,8 @@ SetupWebServer::~SetupWebServer()
server_ = nullptr;
}
routeContext_ = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, {}, core::DeviceIdentity::unknown()};
nullptr, core::CompanionChipStatus{false, false, false},
core::DeviceIdentity::unknown()};
}
std::expected<void, NetError> SetupWebServer::start(
@@ -1218,7 +1221,8 @@ std::expected<void, NetError> SetupWebServer::start(
if (httpd_start(&server_, &config) != ESP_OK) {
ESP_LOGE(kTag, "httpd_start failed");
routeContext_ = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, {}, core::DeviceIdentity::unknown()};
nullptr, core::CompanionChipStatus{false, false, false},
core::DeviceIdentity::unknown()};
return std::unexpected(NetError::HttpServerStartFailed);
}