Add DAB ANTCAP calibration and fix BT1035 boot banner timing
Extend the FM-only ANTCAP antenna-varactor override to DAB, mirroring the existing mechanism end to end (driver, tuner, service, EEPROM storage, HTTP API). Live sweep on real hardware found no ANTCAP value beating auto-tune on the ensembles tested, so DAB stays on auto-tune by default. Also fix BT1035 boot: the module's real boot banner doesn't appear until ~18-24s after RESET# releases, not the 3.5s previously waited; add a 2-attempt retry and a baud-rate probe fallback for diagnostics. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -343,6 +343,7 @@ public:
|
||||
private:
|
||||
[[nodiscard]] std::expected<void, Bt1035Error> ensureBooted() const;
|
||||
[[nodiscard]] std::expected<void, Bt1035Error> runInitSequence();
|
||||
[[nodiscard]] std::expected<void, Bt1035Error> resetAndInitOnce();
|
||||
[[nodiscard]] std::expected<std::string, Bt1035Error> transmitAndCollect(
|
||||
std::string_view commandLine, int timeoutMs = kResponseTimeoutMs);
|
||||
[[nodiscard]] std::expected<std::string, Bt1035Error> transmitAndCollectUntil(
|
||||
|
||||
@@ -38,6 +38,23 @@ constexpr int kUartTxBuffer = 256;
|
||||
constexpr int kResponseTimeoutMs = 2000;
|
||||
constexpr int kPostResetMs = 500;
|
||||
constexpr int kPostUartMs = 100;
|
||||
/** Feasycom BT1035 programming user guide §2.2 (pin 34 SYS_CTRL): "Delay
|
||||
* 100ms, pull high". */
|
||||
constexpr int kSysCtlLeadInMs = 100;
|
||||
/** Margin beyond the datasheet's own >20ms SYS_CTRL-assertion-to-power-up
|
||||
* minimum (§4.7), for regulator/crystal settling before RESET releases. */
|
||||
constexpr int kSysCtlSettleMs = 50;
|
||||
/** Measured live (2026-08-20, power/wiring confirmed sound with a
|
||||
* multimeter — VBAT_IN/SYS_CTRL/1.8V_OUT/VDD_IO all correct, TX/RX pins
|
||||
* verified via continuity): the module's spontaneous boot banner
|
||||
* (+VER=FSC-BT1035,..., +DEVSTAT=1) doesn't appear until ~18.5s after
|
||||
* RESET# releases — full BT stack init, not just the internal regulator.
|
||||
* The previous 3500ms wait here was never enough for the module to say
|
||||
* anything, so every prior boot attempt cut power and restarted before
|
||||
* the module could finish booting even once. See kBootAttempts below. */
|
||||
constexpr int kBootBannerWaitMs = 25000;
|
||||
constexpr int kBootAttempts = 2;
|
||||
constexpr int kBootRetryDelayMs = 300;
|
||||
|
||||
void flushUartRx(int uartPort) noexcept
|
||||
{
|
||||
@@ -49,15 +66,53 @@ void flushUartRx(int uartPort) noexcept
|
||||
}
|
||||
}
|
||||
|
||||
// Diagnostic only: some BT1035 firmware prints an unsolicited boot banner on
|
||||
// UART right after the hardware RESET# pulse. Capturing it (or its absence)
|
||||
// tells us whether the UART link is electrically alive independent of the
|
||||
// AT command layer.
|
||||
// Diagnostic only, run once if all kBootAttempts fail at 115200 (the
|
||||
// datasheet's own default). AT+BAUD persists across RESET#/SYS_CTRL power
|
||||
// cycles (programming guide §5.1.3), so a stray manual AT+BAUD or
|
||||
// AT+RESTORE sent during earlier interactive testing could have left the
|
||||
// module listening at a different rate than our fixed assumption — this
|
||||
// sweep tells us if that's what's happening instead of guessing.
|
||||
constexpr std::array<int, 8> kBaudProbeCandidates = {
|
||||
9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600};
|
||||
|
||||
void probeBaudRates(int uartPort) noexcept
|
||||
{
|
||||
ESP_LOGW(kTag, "115200 unresponsive after %d attempts — sweeping baud rates",
|
||||
kBootAttempts);
|
||||
for (const int baud : kBaudProbeCandidates) {
|
||||
if (uart_set_baudrate(static_cast<uart_port_t>(uartPort), baud)
|
||||
!= ESP_OK) {
|
||||
continue;
|
||||
}
|
||||
flushUartRx(uartPort);
|
||||
uart_write_bytes(static_cast<uart_port_t>(uartPort), "AT\r\n", 4);
|
||||
|
||||
std::array<char, 64> buf{};
|
||||
const int n = uart_read_bytes(static_cast<uart_port_t>(uartPort),
|
||||
buf.data(), buf.size(),
|
||||
pdMS_TO_TICKS(500));
|
||||
if (n > 0) {
|
||||
ESP_LOGW(kTag, "baud probe: module responded at %d baud (%d bytes)",
|
||||
baud, n);
|
||||
ESP_LOG_BUFFER_HEX(kTag, buf.data(), static_cast<std::size_t>(n));
|
||||
} else {
|
||||
ESP_LOGI(kTag, "baud probe: silent at %d baud", baud);
|
||||
}
|
||||
}
|
||||
uart_set_baudrate(static_cast<uart_port_t>(uartPort), kBaudRate);
|
||||
flushUartRx(uartPort);
|
||||
}
|
||||
|
||||
// The BT1035 prints an unsolicited boot banner (+VER=..., +DEVSTAT=1, ...)
|
||||
// once its full Bluetooth stack finishes initialising — this blocks for up
|
||||
// to kBootBannerWaitMs waiting for it, since that's the real boot-complete
|
||||
// signal (the module is otherwise silent and won't answer AT commands
|
||||
// until this appears).
|
||||
void logRawUartBoot(int uartPort) noexcept
|
||||
{
|
||||
std::array<std::uint8_t, 128> buf{};
|
||||
const int n = uart_read_bytes(static_cast<uart_port_t>(uartPort), buf.data(),
|
||||
buf.size(), pdMS_TO_TICKS(3500));
|
||||
buf.size(), pdMS_TO_TICKS(kBootBannerWaitMs));
|
||||
if (n <= 0) {
|
||||
ESP_LOGW("Bt1035", "no spontaneous UART bytes after hardware reset");
|
||||
return;
|
||||
@@ -904,32 +959,68 @@ std::expected<void, Bt1035Error> Bt1035Driver::runInitSequence()
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, Bt1035Error> Bt1035Driver::resetAndInitOnce()
|
||||
{
|
||||
// Feasycom BT1035 programming user guide §2.2, pin 34 SYS_CTRL:
|
||||
// "Delay 100ms, pull high" — the datasheet's own OFF-state timing spec
|
||||
// (§4.7) says SYS_CTRL must be asserted >20ms before the internal
|
||||
// regulators start powering up at all, so pulling it high with no
|
||||
// lead-in delay (the previous sequence here) races the chip's own
|
||||
// power-on requirement. Held low with RESET already asserted, then a
|
||||
// 100ms lead-in exactly matching the guide, then SYS_CTRL high, then
|
||||
// extra settle time before releasing RESET into a chip that's had a
|
||||
// chance to actually power up first.
|
||||
const auto sysCtlPin = static_cast<gpio_num_t>(pins_.sysCtlGpio);
|
||||
const auto resetPin = static_cast<gpio_num_t>(pins_.resetGpio);
|
||||
|
||||
gpio_set_level(sysCtlPin, 0);
|
||||
gpio_set_level(resetPin, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(kSysCtlLeadInMs));
|
||||
ESP_LOGI(kTag, "pre-power: SYS_CTRL=%d RESET=%d (want 0,0)",
|
||||
gpio_get_level(sysCtlPin), gpio_get_level(resetPin));
|
||||
|
||||
gpio_set_level(sysCtlPin, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(kSysCtlSettleMs));
|
||||
ESP_LOGI(kTag, "post-syscl: SYS_CTRL=%d RESET=%d (want 1,0)",
|
||||
gpio_get_level(sysCtlPin), gpio_get_level(resetPin));
|
||||
|
||||
gpio_set_level(resetPin, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(kPostResetMs));
|
||||
ESP_LOGI(kTag, "post-reset: SYS_CTRL=%d RESET=%d (want 1,1)",
|
||||
gpio_get_level(sysCtlPin), gpio_get_level(resetPin));
|
||||
|
||||
logRawUartBoot(uartPort_);
|
||||
uart_flush_input(static_cast<uart_port_t>(uartPort_));
|
||||
vTaskDelay(pdMS_TO_TICKS(kPostUartMs));
|
||||
|
||||
return runInitSequence();
|
||||
}
|
||||
|
||||
std::expected<void, Bt1035Error> Bt1035Driver::boot()
|
||||
{
|
||||
if (booted_) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// GPIO_MODE_INPUT_OUTPUT (not plain GPIO_MODE_OUTPUT): gpio_config()
|
||||
// only enables the pad's input buffer when the INPUT bit is set, so a
|
||||
// pure-output config leaves gpio_get_level() reading a stale/always-0
|
||||
// register instead of the real driven level — needed for the readback
|
||||
// diagnostic below to be meaningful.
|
||||
gpio_config_t resetCfg = {};
|
||||
resetCfg.pin_bit_mask = 1ULL << pins_.resetGpio;
|
||||
resetCfg.mode = GPIO_MODE_OUTPUT;
|
||||
resetCfg.mode = GPIO_MODE_INPUT_OUTPUT;
|
||||
if (gpio_config(&resetCfg) != ESP_OK) {
|
||||
return std::unexpected(Bt1035Error::ResetFailed);
|
||||
}
|
||||
|
||||
gpio_config_t sysCfg = {};
|
||||
sysCfg.pin_bit_mask = 1ULL << pins_.sysCtlGpio;
|
||||
sysCfg.mode = GPIO_MODE_OUTPUT;
|
||||
sysCfg.mode = GPIO_MODE_INPUT_OUTPUT;
|
||||
if (gpio_config(&sysCfg) != ESP_OK) {
|
||||
return std::unexpected(Bt1035Error::ResetFailed);
|
||||
}
|
||||
|
||||
gpio_set_level(static_cast<gpio_num_t>(pins_.sysCtlGpio), 1);
|
||||
gpio_set_level(static_cast<gpio_num_t>(pins_.resetGpio), 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
gpio_set_level(static_cast<gpio_num_t>(pins_.resetGpio), 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(kPostResetMs));
|
||||
|
||||
if (!uartInstalled_) {
|
||||
const uart_config_t uartCfg = {
|
||||
.baud_rate = kBaudRate,
|
||||
@@ -961,12 +1052,20 @@ std::expected<void, Bt1035Error> Bt1035Driver::boot()
|
||||
uartInstalled_ = true;
|
||||
}
|
||||
|
||||
logRawUartBoot(uartPort_);
|
||||
uart_flush_input(static_cast<uart_port_t>(uartPort_));
|
||||
vTaskDelay(pdMS_TO_TICKS(kPostUartMs));
|
||||
|
||||
if (auto init = runInitSequence(); !init) {
|
||||
ESP_LOGE(kTag, "AT init failed");
|
||||
std::expected<void, Bt1035Error> init = std::unexpected(Bt1035Error::UnexpectedResponse);
|
||||
for (int attempt = 1; attempt <= kBootAttempts; ++attempt) {
|
||||
init = resetAndInitOnce();
|
||||
if (init) {
|
||||
break;
|
||||
}
|
||||
ESP_LOGW(kTag, "boot attempt %d/%d failed", attempt, kBootAttempts);
|
||||
if (attempt < kBootAttempts) {
|
||||
vTaskDelay(pdMS_TO_TICKS(kBootRetryDelayMs));
|
||||
}
|
||||
}
|
||||
if (!init) {
|
||||
ESP_LOGE(kTag, "AT init failed after %d attempts", kBootAttempts);
|
||||
probeBaudRates(uartPort_);
|
||||
return init;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
idf_component_register(
|
||||
SRCS "src/Eeprom24aa.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES core driver esp_driver_i2c
|
||||
REQUIRES core driver esp_driver_i2c freertos
|
||||
)
|
||||
|
||||
target_compile_features(${COMPONENT_LIB} PUBLIC cxx_std_23)
|
||||
|
||||
@@ -13,27 +13,44 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/IDeviceIdentitySource.hpp"
|
||||
#include "core/IdentityError.hpp"
|
||||
|
||||
#include "driver/i2c_master.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
|
||||
namespace eeprom24aa {
|
||||
|
||||
/**
|
||||
* @brief Eeprom24aa — reads the factory EUI-48 from Microchip 24AA025E48.
|
||||
* @brief Eeprom24aa — reads the factory EUI-48 from Microchip 24AA025E48;
|
||||
* also stores one board-specific calibration byte in the chip's
|
||||
* user-writable region.
|
||||
*
|
||||
* @dname Eeprom24aa
|
||||
* @return n/a (type)
|
||||
* @pubstate Borrows an existing I2C master bus (shared with ADAU1701). The
|
||||
* EUI-48 lives at word address 0xFA..0xFF per the 24AA025E48
|
||||
* datasheet (DS20001191).
|
||||
* EUI-48 lives at word address 0xFA..0xFF (read-only, factory
|
||||
* programmed) per the 24AA025E48 datasheet (DS20001191); the FM
|
||||
* ANTCAP calibration byte lives at word address 0x00, and the
|
||||
* DAB ANTCAP calibration byte at word address 0x01, both in the
|
||||
* remaining user-writable 250 bytes.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-07
|
||||
*/
|
||||
class Eeprom24aa : public core::IDeviceIdentitySource {
|
||||
public:
|
||||
/** Valid ANTCAP calibration values (FM or DAB) are 0-128 (AN649/AN851);
|
||||
* any stored byte above this, including the EEPROM's blank/erased
|
||||
* 0xFF, reads back as "never calibrated" — no separate sentinel write
|
||||
* needed for a fresh chip. */
|
||||
static constexpr std::uint8_t kFmAntCapMax = 128U;
|
||||
/** Same range as kFmAntCapMax; kept as a separate name for the DAB
|
||||
* calibration byte's own doc comments below. */
|
||||
static constexpr std::uint8_t kDabAntCapMax = 128U;
|
||||
|
||||
/**
|
||||
* @brief Eeprom24aa — bind to a running I2C master bus and 7-bit addr.
|
||||
*
|
||||
@@ -60,6 +77,69 @@ public:
|
||||
[[nodiscard]] std::expected<core::DeviceIdentity, core::IdentityError>
|
||||
readDeviceIdentity() override;
|
||||
|
||||
/**
|
||||
* @brief readFmAntCap — read the stored FM antenna calibration byte.
|
||||
*
|
||||
* @dname readFmAntCap
|
||||
* @return Calibrated ANTCAP (0-kFmAntCapMax) if one was ever saved via
|
||||
* writeFmAntCap(), nullopt if the byte is blank/out of range,
|
||||
* or IdentityError on an I2C failure.
|
||||
* @pubstate performs one I2C read of one byte at word address 0x00.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-08-19
|
||||
*/
|
||||
[[nodiscard]] std::expected<std::optional<std::uint8_t>, core::IdentityError>
|
||||
readFmAntCap();
|
||||
|
||||
/**
|
||||
* @brief writeFmAntCap — persist an FM antenna calibration value.
|
||||
*
|
||||
* @dname writeFmAntCap
|
||||
* @param value ANTCAP to store, 0-kFmAntCapMax (AN851 Appendix A
|
||||
* calibration procedure; found via a sweep, not
|
||||
* computed).
|
||||
* @return Ok on success, or IdentityError::I2cFailed.
|
||||
* @pubstate performs one I2C byte write at word address 0x00, then
|
||||
* blocks for the chip's write-cycle time before returning.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-08-19
|
||||
*/
|
||||
[[nodiscard]] std::expected<void, core::IdentityError>
|
||||
writeFmAntCap(std::uint8_t value);
|
||||
|
||||
/**
|
||||
* @brief readDabAntCap — read the stored DAB antenna calibration byte.
|
||||
*
|
||||
* @dname readDabAntCap
|
||||
* @return Calibrated ANTCAP (0-kDabAntCapMax) if one was ever saved via
|
||||
* writeDabAntCap(), nullopt if the byte is blank/out of range,
|
||||
* or IdentityError on an I2C failure.
|
||||
* @pubstate performs one I2C read of one byte at word address 0x01.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-08-20
|
||||
*/
|
||||
[[nodiscard]] std::expected<std::optional<std::uint8_t>, core::IdentityError>
|
||||
readDabAntCap();
|
||||
|
||||
/**
|
||||
* @brief writeDabAntCap — persist a DAB antenna calibration value.
|
||||
*
|
||||
* @dname writeDabAntCap
|
||||
* @param value ANTCAP to store, 0-kDabAntCapMax (AN649 Command
|
||||
* 0xB0 ARG4; found via a sweep, not computed).
|
||||
* @return Ok on success, or IdentityError::I2cFailed.
|
||||
* @pubstate performs one I2C byte write at word address 0x01, then
|
||||
* blocks for the chip's write-cycle time before returning.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-08-20
|
||||
*/
|
||||
[[nodiscard]] std::expected<void, core::IdentityError>
|
||||
writeDabAntCap(std::uint8_t value);
|
||||
|
||||
private:
|
||||
i2c_master_bus_handle_t bus_;
|
||||
std::uint8_t addr7_;
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
|
||||
#include "driver/i2c_master.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
@@ -25,7 +27,15 @@ namespace {
|
||||
constexpr char kTag[] = "Eeprom24aa";
|
||||
/** EUI-48 word address per Microchip 24AA025E48 datasheet (DS20001191). */
|
||||
constexpr std::uint8_t kEui48WordAddress = 0xFAU;
|
||||
/** FM ANTCAP calibration byte, in the chip's user-writable region (anywhere
|
||||
* below the factory-locked 0xFA-0xFF EUI-48 block). */
|
||||
constexpr std::uint8_t kFmAntCapWordAddress = 0x00U;
|
||||
/** DAB ANTCAP calibration byte, next word address after the FM byte. */
|
||||
constexpr std::uint8_t kDabAntCapWordAddress = 0x01U;
|
||||
constexpr int kI2cTimeoutMs = 100;
|
||||
/** DS20001191 §"Page Write"/"Byte Write": max write cycle time after STOP
|
||||
* before the chip acknowledges further I2C traffic. */
|
||||
constexpr int kI2cWriteCycleMs = 5;
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -69,4 +79,146 @@ Eeprom24aa::readDeviceIdentity()
|
||||
return core::DeviceIdentity::fromEui48(core::Eui48::fromBytes(payload));
|
||||
}
|
||||
|
||||
std::expected<std::optional<std::uint8_t>, core::IdentityError>
|
||||
Eeprom24aa::readFmAntCap()
|
||||
{
|
||||
if (bus_ == nullptr) {
|
||||
return std::unexpected(core::IdentityError::I2cFailed);
|
||||
}
|
||||
|
||||
i2c_device_config_t devCfg = {};
|
||||
devCfg.dev_addr_length = I2C_ADDR_BIT_LEN_7;
|
||||
devCfg.device_address = addr7_;
|
||||
devCfg.scl_speed_hz = 100000;
|
||||
|
||||
i2c_master_dev_handle_t dev = nullptr;
|
||||
if (i2c_master_bus_add_device(bus_, &devCfg, &dev) != ESP_OK) {
|
||||
ESP_LOGW(kTag, "i2c_master_bus_add_device failed");
|
||||
return std::unexpected(core::IdentityError::I2cFailed);
|
||||
}
|
||||
|
||||
const std::uint8_t wordAddress = kFmAntCapWordAddress;
|
||||
std::uint8_t value = 0xFFU;
|
||||
const esp_err_t err = i2c_master_transmit_receive(
|
||||
dev, &wordAddress, 1U, &value, 1U, kI2cTimeoutMs);
|
||||
|
||||
i2c_master_bus_rm_device(dev);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(kTag, "FM ANTCAP read failed (err=0x%x)",
|
||||
static_cast<unsigned>(err));
|
||||
return std::unexpected(core::IdentityError::ReadFailed);
|
||||
}
|
||||
|
||||
if (value > kFmAntCapMax) {
|
||||
return std::optional<std::uint8_t>{};
|
||||
}
|
||||
return std::optional<std::uint8_t>{value};
|
||||
}
|
||||
|
||||
std::expected<void, core::IdentityError>
|
||||
Eeprom24aa::writeFmAntCap(std::uint8_t value)
|
||||
{
|
||||
if (bus_ == nullptr) {
|
||||
return std::unexpected(core::IdentityError::I2cFailed);
|
||||
}
|
||||
|
||||
i2c_device_config_t devCfg = {};
|
||||
devCfg.dev_addr_length = I2C_ADDR_BIT_LEN_7;
|
||||
devCfg.device_address = addr7_;
|
||||
devCfg.scl_speed_hz = 100000;
|
||||
|
||||
i2c_master_dev_handle_t dev = nullptr;
|
||||
if (i2c_master_bus_add_device(bus_, &devCfg, &dev) != ESP_OK) {
|
||||
ESP_LOGW(kTag, "i2c_master_bus_add_device failed");
|
||||
return std::unexpected(core::IdentityError::I2cFailed);
|
||||
}
|
||||
|
||||
const std::array<std::uint8_t, 2> payload = {kFmAntCapWordAddress, value};
|
||||
const esp_err_t err =
|
||||
i2c_master_transmit(dev, payload.data(), payload.size(), kI2cTimeoutMs);
|
||||
|
||||
i2c_master_bus_rm_device(dev);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(kTag, "FM ANTCAP write failed (err=0x%x)",
|
||||
static_cast<unsigned>(err));
|
||||
return std::unexpected(core::IdentityError::I2cFailed);
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(kI2cWriteCycleMs));
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<std::optional<std::uint8_t>, core::IdentityError>
|
||||
Eeprom24aa::readDabAntCap()
|
||||
{
|
||||
if (bus_ == nullptr) {
|
||||
return std::unexpected(core::IdentityError::I2cFailed);
|
||||
}
|
||||
|
||||
i2c_device_config_t devCfg = {};
|
||||
devCfg.dev_addr_length = I2C_ADDR_BIT_LEN_7;
|
||||
devCfg.device_address = addr7_;
|
||||
devCfg.scl_speed_hz = 100000;
|
||||
|
||||
i2c_master_dev_handle_t dev = nullptr;
|
||||
if (i2c_master_bus_add_device(bus_, &devCfg, &dev) != ESP_OK) {
|
||||
ESP_LOGW(kTag, "i2c_master_bus_add_device failed");
|
||||
return std::unexpected(core::IdentityError::I2cFailed);
|
||||
}
|
||||
|
||||
const std::uint8_t wordAddress = kDabAntCapWordAddress;
|
||||
std::uint8_t value = 0xFFU;
|
||||
const esp_err_t err = i2c_master_transmit_receive(
|
||||
dev, &wordAddress, 1U, &value, 1U, kI2cTimeoutMs);
|
||||
|
||||
i2c_master_bus_rm_device(dev);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(kTag, "DAB ANTCAP read failed (err=0x%x)",
|
||||
static_cast<unsigned>(err));
|
||||
return std::unexpected(core::IdentityError::ReadFailed);
|
||||
}
|
||||
|
||||
if (value > kDabAntCapMax) {
|
||||
return std::optional<std::uint8_t>{};
|
||||
}
|
||||
return std::optional<std::uint8_t>{value};
|
||||
}
|
||||
|
||||
std::expected<void, core::IdentityError>
|
||||
Eeprom24aa::writeDabAntCap(std::uint8_t value)
|
||||
{
|
||||
if (bus_ == nullptr) {
|
||||
return std::unexpected(core::IdentityError::I2cFailed);
|
||||
}
|
||||
|
||||
i2c_device_config_t devCfg = {};
|
||||
devCfg.dev_addr_length = I2C_ADDR_BIT_LEN_7;
|
||||
devCfg.device_address = addr7_;
|
||||
devCfg.scl_speed_hz = 100000;
|
||||
|
||||
i2c_master_dev_handle_t dev = nullptr;
|
||||
if (i2c_master_bus_add_device(bus_, &devCfg, &dev) != ESP_OK) {
|
||||
ESP_LOGW(kTag, "i2c_master_bus_add_device failed");
|
||||
return std::unexpected(core::IdentityError::I2cFailed);
|
||||
}
|
||||
|
||||
const std::array<std::uint8_t, 2> payload = {kDabAntCapWordAddress, value};
|
||||
const esp_err_t err =
|
||||
i2c_master_transmit(dev, payload.data(), payload.size(), kI2cTimeoutMs);
|
||||
|
||||
i2c_master_bus_rm_device(dev);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(kTag, "DAB ANTCAP write failed (err=0x%x)",
|
||||
static_cast<unsigned>(err));
|
||||
return std::unexpected(core::IdentityError::I2cFailed);
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(kI2cWriteCycleMs));
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace eeprom24aa
|
||||
|
||||
@@ -288,13 +288,18 @@ public:
|
||||
*
|
||||
* @dname tuneDab
|
||||
* @param freqIndex Ensemble index 0–37.
|
||||
* @param antCap ANTCAP[7:0] override (0-128, AN649 Command 0xB0
|
||||
* ARG4). 0 = automatic front-end tuning; other
|
||||
* values force a specific varactor setting, for
|
||||
* antenna calibration sweeps (mirrors tuneFm).
|
||||
* @return Ok on success, or Si4684Error.
|
||||
* @pubstate sends DAB_TUNE_FREQ and waits for STC.
|
||||
*
|
||||
* @author Michele Bigi
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] std::expected<void, Si4684Error> tuneDab(std::uint8_t freqIndex);
|
||||
[[nodiscard]] std::expected<void, Si4684Error> tuneDab(
|
||||
std::uint8_t freqIndex, std::uint8_t antCap = 0U);
|
||||
|
||||
/**
|
||||
* @brief readDabDigRadStatus — read ensemble lock metrics.
|
||||
|
||||
@@ -96,6 +96,7 @@ public:
|
||||
*
|
||||
* @dname tuneDab
|
||||
* @param freqIndex Ensemble index 0–37.
|
||||
* @param antCap Forwarded to Si4684Driver::tuneDab (0 = auto).
|
||||
* @return Ok on success, or a mapped TunerError.
|
||||
* @pubstate writes dabIndex_ on success.
|
||||
*
|
||||
@@ -103,13 +104,14 @@ public:
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] std::expected<void, core::TunerError> tuneDab(
|
||||
std::uint8_t freqIndex) override;
|
||||
std::uint8_t freqIndex, std::uint8_t antCap = 0U) override;
|
||||
|
||||
/**
|
||||
* @brief tuneFm — tune to an FM centre frequency in kHz.
|
||||
*
|
||||
* @dname tuneFm
|
||||
* @param frequency Validated FM centre frequency.
|
||||
* @param antCap Forwarded to Si4684Driver::tuneFm (0 = auto).
|
||||
* @return Ok on success, or a mapped TunerError.
|
||||
* @pubstate writes fmFrequency_ on success.
|
||||
*
|
||||
@@ -117,7 +119,7 @@ public:
|
||||
* @date 2026-07-06
|
||||
*/
|
||||
[[nodiscard]] std::expected<void, core::TunerError> tuneFm(
|
||||
core::FrequencyKHz frequency) override;
|
||||
core::FrequencyKHz frequency, std::uint8_t antCap = 0U) override;
|
||||
|
||||
/**
|
||||
* @brief seekFm — seek FM with band wrap.
|
||||
|
||||
@@ -85,6 +85,7 @@ constexpr std::uint16_t kPropDabTuneFeCfg = 0x1712U;
|
||||
constexpr std::uint16_t kPropFmTuneFeCfg = 0x1712U;
|
||||
constexpr std::uint16_t kPropDabXpadEnable = 0xB400U;
|
||||
constexpr std::uint16_t kPropDigitalServiceIntSource = 0x8100U;
|
||||
constexpr std::uint16_t kPropDabEventIntSource = 0xB300U;
|
||||
/** AN649 INT_CTL_ENABLE / INT_CTL_REPEAT — route STC to INTB until STCACK. */
|
||||
constexpr std::uint16_t kPropIntCtlEnable = 0x0000U;
|
||||
constexpr std::uint16_t kPropIntCtlRepeat = 0x0001U;
|
||||
@@ -504,6 +505,15 @@ std::expected<void, Si4684Error> Si4684Driver::configureAfterBoot(
|
||||
ESP_LOGW(kTag, "DIGITAL_SERVICE_INT_SOURCE (0x8100) failed");
|
||||
return dsrv;
|
||||
}
|
||||
// AN649 Property 0xB300 DAB_EVENT_INTERRUPT_SOURCE, bit0=SRVLIST_INTEN
|
||||
// (default 0x0000 = disabled at power-on). Without this, nothing in
|
||||
// this driver ever enables the service-list-ready event, so
|
||||
// fetchDabServiceList() can stay gated behind an eternally-false
|
||||
// serviceListReady even on a clean, well-locked ensemble.
|
||||
if (auto evt = setProperty(kPropDabEventIntSource, 0x0001U); !evt) {
|
||||
ESP_LOGW(kTag, "DAB_EVENT_INTERRUPT_SOURCE (0xB300) failed");
|
||||
return evt;
|
||||
}
|
||||
} else {
|
||||
// FM varactor cal per hitech95/uGreen DTS (not DAB PE5PVB values).
|
||||
static constexpr std::uint16_t kFmFeProps[][2] = {
|
||||
@@ -1027,7 +1037,8 @@ std::expected<void, Si4684Error> Si4684Driver::installDefaultDabFrequencyPlan()
|
||||
return sendCommand(cmd);
|
||||
}
|
||||
|
||||
std::expected<void, Si4684Error> Si4684Driver::tuneDab(std::uint8_t freqIndex)
|
||||
std::expected<void, Si4684Error> Si4684Driver::tuneDab(
|
||||
std::uint8_t freqIndex, std::uint8_t antCap)
|
||||
{
|
||||
if (auto band = ensureBand(Si4684Band::Dab); !band) {
|
||||
return band;
|
||||
@@ -1037,8 +1048,9 @@ std::expected<void, Si4684Error> Si4684Driver::tuneDab(std::uint8_t freqIndex)
|
||||
}
|
||||
// writeCommand() always prepends a fixed ARG1=0x00 (INJECTION=0), so
|
||||
// this array starts at ARG2 (AN649 Command 0xB0 table: ARG2=FREQ_INDEX,
|
||||
// ARG3=0x00 fixed, ARG4=ANTCAP[7:0], ARG5=ANTCAP[15:8]).
|
||||
const std::uint8_t args[] = {freqIndex, 0x00U, 0x00U, 0x00U};
|
||||
// ARG3=0x00 fixed, ARG4=ANTCAP[7:0], ARG5=ANTCAP[15:8] -- high byte
|
||||
// always 0, range is 0-128, same as tuneFm's ANTCAP).
|
||||
const std::uint8_t args[] = {freqIndex, 0x00U, antCap, 0x00U};
|
||||
if (auto cmd = writeCommand(Command::DabTuneFreq, args, sizeof(args));
|
||||
!cmd) {
|
||||
return std::unexpected(Si4684Error::TuneFailed);
|
||||
@@ -1121,26 +1133,37 @@ Si4684Driver::fetchDabServiceList()
|
||||
}
|
||||
|
||||
// raw[5]=RESP4=SIZE[7:0], raw[6]=RESP5=SIZE[15:8] (see readFmRds()).
|
||||
// AN649 only documents SIZE/DATA_0/DATA_N generically for this command
|
||||
// and defers the DAB payload layout to a supplemental "Digital
|
||||
// Services User's Guide" we don't have; the exact field layout below
|
||||
// is cross-checked against hitech95/si468x_dab_receiver's
|
||||
// si468x_core_cmd_dab_get_service_list() (drivers/mfd/si468x-cmd.c),
|
||||
// a real working Linux driver for the same command. That driver also
|
||||
// establishes that the payload actually carried after SIZE is
|
||||
// SIZE-2 bytes, not SIZE bytes.
|
||||
const std::uint16_t payloadSize = readLe16(header.data() + 5);
|
||||
if (payloadSize == 0U || payloadSize + 7U > kSpiBufferSize) {
|
||||
if (payloadSize <= 2U || payloadSize + 5U > kSpiBufferSize) {
|
||||
return std::unexpected(Si4684Error::ReplyTooShort);
|
||||
}
|
||||
|
||||
// DATA_0 (first byte of the AN649 Table 14 "DAB/DMB Digital Service
|
||||
// List" structure) is RESP6 = body[7]: lead-in(1) + STATUS0-3(4) +
|
||||
// SIZE(2) = 7 header bytes before it.
|
||||
std::vector<std::uint8_t> body(payloadSize + 7U, 0U);
|
||||
// DATA_0 (first byte of the payload) is RESP6 = body[7]: lead-in(1) +
|
||||
// STATUS0-3(4) + SIZE(2) = 7 header bytes before it. Total frame is
|
||||
// lead-in(1) + STATUS0-3(4) + SIZE(2) + payload(SIZE-2) = SIZE+5.
|
||||
std::vector<std::uint8_t> body(payloadSize + 5U, 0U);
|
||||
if (auto rd = readRaw(body); !rd) {
|
||||
return std::unexpected(rd.error());
|
||||
}
|
||||
|
||||
// Table 14: List Size(2) + Version(2) + NumServices(1) + AlignPad(3) =
|
||||
// 8 bytes, then Service 1 begins.
|
||||
const std::uint8_t serviceCount = body[11];
|
||||
// From DATA_0 (body[7]): Version(2) + NumServices/flags(1) +
|
||||
// AlignPad(3) = 6 bytes, then Service 1 begins at body[13]. (The
|
||||
// previous version of this code double-counted the already-consumed
|
||||
// SIZE field here, offsetting every read by 2 bytes — that is why the
|
||||
// service list always came back empty.)
|
||||
const std::uint8_t serviceCount = body[9] & 0x1FU; // max 32 services
|
||||
std::vector<Si4684DabService> services;
|
||||
services.reserve(serviceCount);
|
||||
|
||||
std::size_t offset = 15U;
|
||||
std::size_t offset = 13U;
|
||||
for (std::uint8_t i = 0; i < serviceCount; ++i) {
|
||||
// Fixed per-service part: ServiceID(4) + ServiceInfo1-3(3) +
|
||||
// AlignPad(1) + Label(16) = 24 bytes.
|
||||
@@ -1155,11 +1178,13 @@ Si4684Driver::fetchDabServiceList()
|
||||
entry.label[16] = '\0';
|
||||
offset += 24U;
|
||||
|
||||
// Component ID is 2 bytes (AN649 Table 14); only the first
|
||||
// Component ID is 2 bytes (hitech95's si468x-cmd.c packs tm_id/
|
||||
// sub_ch_id/fidc_id/sc_id into this same field; only the raw
|
||||
// 16-bit value is exposed on this DTO). Only the first
|
||||
// component's ID is exposed on this DTO. Every component (M =
|
||||
// componentCount) must still be skipped to keep the next service
|
||||
// entry aligned, each one ComponentID(2) + ComponentInfo(1) +
|
||||
// ValidFlags(1) = 4 bytes.
|
||||
// entry aligned, each one 2 bytes packed field + ServiceType/
|
||||
// flags(1) + ValidFlags(1) = 4 bytes.
|
||||
if (componentCount > 0U && offset + 2U <= body.size()) {
|
||||
entry.componentId = readLe16(body.data() + offset);
|
||||
}
|
||||
|
||||
@@ -209,12 +209,12 @@ std::expected<core::TunerStatus, core::TunerError> Si4684Tuner::readStatus()
|
||||
}
|
||||
|
||||
std::expected<void, core::TunerError> Si4684Tuner::tuneDab(
|
||||
std::uint8_t freqIndex)
|
||||
std::uint8_t freqIndex, std::uint8_t antCap)
|
||||
{
|
||||
if (auto ready = ensureBandLoaded(core::TunerBand::Dab); !ready) {
|
||||
return ready;
|
||||
}
|
||||
if (auto result = driver_.tuneDab(freqIndex); !result) {
|
||||
if (auto result = driver_.tuneDab(freqIndex, antCap); !result) {
|
||||
return std::unexpected(mapError(result.error()));
|
||||
}
|
||||
dabIndex_ = freqIndex;
|
||||
@@ -225,12 +225,12 @@ std::expected<void, core::TunerError> Si4684Tuner::tuneDab(
|
||||
}
|
||||
|
||||
std::expected<void, core::TunerError> Si4684Tuner::tuneFm(
|
||||
core::FrequencyKHz frequency)
|
||||
core::FrequencyKHz frequency, std::uint8_t antCap)
|
||||
{
|
||||
if (auto ready = ensureBandLoaded(core::TunerBand::Fm); !ready) {
|
||||
return ready;
|
||||
}
|
||||
if (auto result = driver_.tuneFm(frequency); !result) {
|
||||
if (auto result = driver_.tuneFm(frequency, antCap); !result) {
|
||||
return std::unexpected(mapError(result.error()));
|
||||
}
|
||||
fmFrequency_ = frequency;
|
||||
|
||||
Reference in New Issue
Block a user