Files
DigiRadio/Software/main/hardware_bootstrap.hpp
T
micheleandClaude Sonnet 5 f610121fd3 Add FM ANTCAP antenna calibration: HTTP sweep control + EEPROM persistence
Confirmed live tonight that the Si4684's automatic front-end tuning
(FE_VARM/VARB properties) is measurably suboptimal on this board: a
129-value ANTCAP sweep (0-128, AN851 Appendix A) at four different FM
frequencies found antcap=102 beats auto-tune by +6 to +11 dB RSSI and
+3 to +11 dB SNR everywhere tested, consistent with the front-end
network component mismatch already logged in
docs/si4684-rf-investigation-report.md — the board's actual matching
network differs from the AN851 reference network those auto-tune
constants were derived from, so a fixed empirical override
compensates for a gap the chip's own algorithm can't see.

Wiring, bottom to top:
- Si4684Driver::tuneFm() already took an antCap byte; threaded it
  through core::ITuner::tuneFm() and si4684::Si4684Tuner::tuneFm() as
  a new parameter (default 0 = auto, unchanged behaviour for every
  existing caller).
- TunerService::tuneFm() takes an optional override instead: omitted,
  it falls back to a new defaultFmAntCap_ member so every ordinary FM
  tune (seek, scan, station recall, live UI) benefits automatically
  once calibrated, not just calls that pass antcap explicitly.
- POST /api/tuner/tune gained an optional "antcap" field for sweeping
  live without touching the saved calibration.
- POST /api/tuner/calibrate-antenna commits a sweep result: writes it
  to the 24AA025E48 EEPROM's user-writable region (word address 0x00,
  separate from the factory-locked EUI-48 at 0xFA-0xFF) via a new
  Eeprom24aa::writeFmAntCap()/readFmAntCap() pair, and immediately
  updates the live TunerService default — no reboot needed to take
  effect, though HardwareBootstrap::boot() also loads it at every
  boot so it survives power cycles. Same net::AntennaCalibration
  function-pointer bridge pattern as PhoneStreamSink/BleProvisioning,
  so components/net stays free of eeprom24aa headers.

Verified end to end on hardware: swept and found 102, saved it via
the new endpoint, confirmed the live default changed immediately,
then power-cycled and confirmed the boot log reports "FM ANTCAP
calibration loaded: 102" and a subsequent default tune reflects it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0178rASQ6ZETPMUamvpoR2KR
2026-08-19 02:01:09 +02:00

174 lines
5.1 KiB
C++

/**
* @file hardware_bootstrap.hpp
* @brief Boot Si4684 and ADAU1701 companion chips at application start.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-07-06
*/
#pragma once
#include "core/CompanionChipStatus.hpp"
#include "core/DeviceIdentity.hpp"
#include "bt1035/Bt1035Driver.hpp"
#include <cstdint>
#include <expected>
#include <optional>
namespace audio {
class AudioService;
} // namespace audio
namespace si4684 {
class Si4684Tuner;
} // namespace si4684
namespace hardware {
/**
* @brief HardwareBootError — companion-chip boot failure causes.
*
* @dname HardwareBootError
* @return n/a (type)
* @pubstate n/a
*
* @author Michele Bigi
* @date 2026-07-06
*/
enum class HardwareBootError {
Si4684BootFailed,
Adau1701BootFailed,
Bt1035BootFailed,
};
/**
* @brief HardwareBootstrap — orchestrates Si4684 then ADAU1701 bring-up.
*
* @dname HardwareBootstrap
* @return n/a (type)
* @pubstate Owns static driver instances for the process lifetime. Must be
* called once from app_main before network start.
*
* @author Michele Bigi
* @date 2026-07-06
*/
class HardwareBootstrap {
public:
/**
* @brief boot — load Si4684 DAB image then replay ADAU1701 program.
*
* @dname boot
* @return Ok on success, or HardwareBootError.
* @pubstate constructs static Si4684, ADAU1701, and BT1035 drivers on first call.
*
* Fail-closed: callers must not start Wi-Fi when this returns an error.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] static std::expected<void, HardwareBootError> boot();
/**
* @brief si4684Tuner — borrow the Si4684 ITuner adapter after boot.
*
* @dname si4684Tuner
* @return Reference to the static Si4684Tuner instance.
* @pubstate reads static storage initialised by boot().
*
* Valid only after a successful boot() call in the same process.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] static si4684::Si4684Tuner& si4684Tuner();
/**
* @brief audioService — borrow the audio orchestration service after boot.
*
* @dname audioService
* @return Reference to the static AudioService instance.
* @pubstate reads static storage initialised by boot().
*
* Valid only after a successful boot() call in the same process.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] static audio::AudioService& audioService();
/**
* @brief companionChipStatus — snapshot of companion-chip boot flags.
*
* @dname companionChipStatus
* @return Ready flags for Si4684, ADAU1701, BT1035.
* @pubstate reads static driver isBooted() after boot().
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] static core::CompanionChipStatus companionChipStatus() noexcept;
/**
* @brief bt1035Driver — borrow the BT1035 driver after boot.
*
* @dname bt1035Driver
* @return Reference to the static Bt1035Driver instance.
* @pubstate reads static storage initialised by boot().
*
* Valid only after a successful boot() call in the same process.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] static bt1035::Bt1035Driver& bt1035Driver();
/**
* @brief deviceIdentity — per-board identity from the 24AA025E48 EUI-48.
*
* @dname deviceIdentity
* @return Reference to identity loaded during boot().
* @pubstate reads gDeviceIdentity after boot(); unknown() on EEPROM failure.
*
* @author Michele Bigi
* @date 2026-07-07
*/
[[nodiscard]] static const core::DeviceIdentity& deviceIdentity() noexcept;
/**
* @brief fmAntCapCalibration — saved FM antenna calibration, if any.
*
* @dname fmAntCapCalibration
* @return Calibrated ANTCAP (0-128) read from the 24AA025E48 during
* boot(), or nullopt if never calibrated / the read failed.
* @pubstate reads gFmAntCapCalibration; set once during boot().
*
* @author Michele Bigi
* @date 2026-08-19
*/
[[nodiscard]] static std::optional<std::uint8_t>
fmAntCapCalibration() noexcept;
/**
* @brief saveFmAntCapCalibration — persist a new FM ANTCAP to EEPROM.
*
* @dname saveFmAntCapCalibration
* @param antCap Value found via a calibration sweep (0-128).
* @return true on success, false on an I2C failure.
* @pubstate writes the 24AA025E48 user region and gFmAntCapCalibration.
* Does not itself change any live tuner state — callers must
* also call TunerService::setDefaultFmAntCap() to apply it.
*
* @author Michele Bigi
* @date 2026-08-19
*/
[[nodiscard]] static bool saveFmAntCapCalibration(std::uint8_t antCap);
};
} // namespace hardware