Move the board profile tests into the native Unity suite

The board tests ran through tools/test-board-profile.sh with their own stubs, assert() and a script that copied nine method bodies out of Transceiver.cpp, so they were outside pio test -e native and the coverage report.

Pure decisions now live in units the native env builds: BoardProfile::usesPin is constexpr, RadioReadiness decides register validity, the receive-state outcome (ready, retry or unavailable, with one try today) and whether a queued job may transmit, and ShadeGpioGuard decides whether a shade's outputs touch a board pad. Transceiver and ShadeGpio call them with identical behaviour. New Unity suites cover the generic and LILYGO profiles (reserved pads, startup order, radio bring-up) and the readiness unit; the old harness, its script, CI step and stubs are deleted.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
R. Sid Shaytay 2026-11-14 18:46:33 -08:00
parent 2b3d556729
commit 28af01195a
No known key found for this signature in database
27 changed files with 595 additions and 517 deletions

View file

@ -54,17 +54,8 @@ bool prepareRadio(float frequency) {
if(ready) SPI.begin(sck, miso, mosi, cs);
return ready;
}
bool usesPin(uint8_t pin) {
// USB CDC and in-package flash/OPI PSRAM must never become relay outputs.
return pin == 19 || pin == 20 || (pin >= 26 && pin <= 37) ||
pin == power || pin == sck || pin == mosi || pin == miso ||
pin == cs || pin == tx || pin == rx || pin == sw1 || pin == sw0 ||
pin == displayCS || pin == sdCS || pin == nrfCS || pin == nrfCE || pin == backlight;
}
#else
void begin() {}
bool prepareRadio(float) { return true; }
bool usesPin(uint8_t) { return false; }
#endif
}

View file

@ -29,5 +29,18 @@ constexpr uint8_t backlight = 21;
void begin();
bool prepareRadio(float frequency);
bool usesPin(uint8_t pin);
// Whether the board's own hardware uses this pad. Pure, so it is unit tested
// on the PC (pio test -e native).
#if defined(ESPSOMFY_LILYGO_T_EMBED_CC1101)
constexpr bool usesPin(uint8_t pin) {
// USB CDC and in-package flash/OPI PSRAM must never become relay outputs.
return pin == 19 || pin == 20 || (pin >= 26 && pin <= 37) ||
pin == power || pin == sck || pin == mosi || pin == miso ||
pin == cs || pin == tx || pin == rx || pin == sw1 || pin == sw0 ||
pin == displayCS || pin == sdCS || pin == nrfCS || pin == nrfCE || pin == backlight;
}
#else
constexpr bool usesPin(uint8_t) { return false; }
#endif
}

21
src/RadioReadiness.cpp Normal file
View file

@ -0,0 +1,21 @@
#include "RadioReadiness.h"
float radioFrequencyMHz(uint32_t freqWord) {
return freqWord * (26.0f / 65536.0f);
}
bool radioRegistersReady(const radio_readings_t &r, float requestedMHz) {
const float actual = radioFrequencyMHz(r.freqWord);
return r.partnum == 0 && r.version != 0 && r.version != 0xFF &&
r.iocfg0 == 0x0D && r.iocfg2 == 0x0D && (r.pktctrl0 & 0x30) == 0x30 &&
(r.mdmcfg2 & 0x70) == 0x30 && actual > requestedMHz - 0.01f && actual < requestedMHz + 0.01f;
}
rx_outcome_t radioRxOutcome(uint8_t marcstate, uint8_t attempt, uint8_t maxAttempts) {
if((marcstate & 0x1F) == RADIO_MARCSTATE_RX) return rx_outcome_t::ready;
return attempt + 1 < maxAttempts ? rx_outcome_t::retry : rx_outcome_t::unavailable;
}
bool radioMayTransmit(bool integratedRadio, bool radioAvailable) {
return !integratedRadio || radioAvailable;
}

46
src/RadioReadiness.h Normal file
View file

@ -0,0 +1,46 @@
#ifndef RADIO_READINESS_H
#define RADIO_READINESS_H
#include <stdint.h>
// Whether the integrated CC1101 is fit to use, and whether a queued job may
// still go on air. Every register is read in Transceiver.cpp and passed in,
// so this is free of SPI and the RTOS and can be unit tested on the PC
// (pio test -e native).
// What the radio reported after it was set up.
struct radio_readings_t {
uint8_t partnum = 0; // CC1101_PARTNUM, 0 for a CC1101
uint8_t version = 0; // CC1101_VERSION, 0 and 0xFF mean no chip answered
uint8_t iocfg0 = 0; // GDO0, TX data
uint8_t iocfg2 = 0; // GDO2, RX data
uint8_t pktctrl0 = 0;
uint8_t mdmcfg2 = 0;
uint32_t freqWord = 0; // FREQ2:FREQ1:FREQ0
};
// The carrier in MHz that FREQ2:FREQ1:FREQ0 selects with the 26 MHz crystal.
float radioFrequencyMHz(uint32_t freqWord);
// True when the registers say a CC1101 is wired up and configured for async
// OOK/ASK on the pins and frequency (MHz) we asked for.
bool radioRegistersReady(const radio_readings_t &r, float requestedMHz);
// CC1101_MARCSTATE value while receiving.
#define RADIO_MARCSTATE_RX 0x0D
// Tries at entering receive before the radio is given up as unavailable.
#define RADIO_RX_ATTEMPTS 1
enum class rx_outcome_t : uint8_t {
ready, // the radio is receiving
retry, // not receiving yet, and another try is allowed
unavailable // not receiving and no tries are left
};
// What to do after try number `attempt` (counting from 0) at entering receive
// left the radio in `marcstate`. Only the 5 state bits are looked at. A
// caller allowed N tries passes maxAttempts = N.
rx_outcome_t radioRxOutcome(uint8_t marcstate, uint8_t attempt, uint8_t maxAttempts);
// Whether a job for the radio may be queued or sent. A generic radio is
// always allowed, as before; the integrated one only once it is ready.
bool radioMayTransmit(bool integratedRadio, bool radioAvailable);
#endif

View file

@ -1,15 +1,13 @@
#include "esp_log.h"
#include "Somfy.h"
#include "BoardProfile.h"
#include "ShadeGpioGuard.h"
static const char *TAG = "Somfy";
// A backup may contain GPIO assignments from another board. Keep its shade
// identity, but never drive a pad used by this board's integrated hardware.
static bool usesBoardGPIOs(const SomfyShade &shade) {
if(shade.proto != radio_proto::GP_Relay && shade.proto != radio_proto::GP_Remote) return false;
return BoardProfile::usesPin(shade.gpioUp) || BoardProfile::usesPin(shade.gpioDown) ||
(shade.proto == radio_proto::GP_Remote && BoardProfile::usesPin(shade.gpioMy));
return shadeOutputsUseBoardPins(shade.proto, shade.gpioUp, shade.gpioDown, shade.gpioMy);
}
// Shared by saved/backup configuration loading and JSON updates. Refuse the

14
src/ShadeGpioGuard.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef SHADE_GPIO_GUARD_H
#define SHADE_GPIO_GUARD_H
#include "BoardProfile.h"
#include "SomfyFrame.h"
// A backup may carry GPIO assignments from another board. Whether a shade's
// outputs would drive a pad the board's own hardware uses. Header only so the
// native tests can build it for a given board; free of the shade model.
inline bool shadeOutputsUseBoardPins(radio_proto proto, uint8_t up, uint8_t down, uint8_t my) {
if(proto != radio_proto::GP_Relay && proto != radio_proto::GP_Remote) return false;
return BoardProfile::usesPin(up) || BoardProfile::usesPin(down) ||
(proto == radio_proto::GP_Remote && BoardProfile::usesPin(my));
}
#endif

View file

@ -8,6 +8,7 @@
#include "driver/gpio.h"
#include "esp_log.h"
#include "Utils.h"
#include "RadioReadiness.h"
#include "BootPins.h"
#include "ConfigSettings.h"
#include "Somfy.h"
@ -384,10 +385,10 @@ bool Transceiver::startReceive() {
do {
delay(1);
state = ELECHOUSE_cc1101.SpiReadStatus(CC1101_MARCSTATE) & 0x1F;
} while(state != 0x0D && millis() - timing < 20);
} while(state != RADIO_MARCSTATE_RX && millis() - timing < 20);
Serial.printf("CC1101 MARCSTATE=0x%02X (RX=0x0D); RF switch readback SW1=%d SW0=%d\n",
state, digitalRead(BoardProfile::sw1), digitalRead(BoardProfile::sw0));
if(state != 0x0D) {
if(radioRxOutcome(state, 0, RADIO_RX_ATTEMPTS) != rx_outcome_t::ready) {
Serial.println("CC1101 failed to enter RX; radio unavailable");
ELECHOUSE_cc1101.setSidle();
this->config.radioInit = false;
@ -613,23 +614,21 @@ static void warnBootPin(const char *name, uint8_t pin) {
}
static bool checkBoardRadio(float frequency) {
if(!BoardProfile::integratedRadio) return true;
uint8_t part = ELECHOUSE_cc1101.SpiReadStatus(CC1101_PARTNUM);
uint8_t version = ELECHOUSE_cc1101.SpiReadStatus(CC1101_VERSION);
uint8_t gdo0 = ELECHOUSE_cc1101.SpiReadReg(CC1101_IOCFG0);
uint8_t gdo2 = ELECHOUSE_cc1101.SpiReadReg(CC1101_IOCFG2);
uint8_t packet = ELECHOUSE_cc1101.SpiReadReg(CC1101_PKTCTRL0);
uint8_t modem = ELECHOUSE_cc1101.SpiReadReg(CC1101_MDMCFG2);
uint32_t word = (static_cast<uint32_t>(ELECHOUSE_cc1101.SpiReadReg(CC1101_FREQ2)) << 16) |
radio_readings_t r;
r.partnum = ELECHOUSE_cc1101.SpiReadStatus(CC1101_PARTNUM);
r.version = ELECHOUSE_cc1101.SpiReadStatus(CC1101_VERSION);
r.iocfg0 = ELECHOUSE_cc1101.SpiReadReg(CC1101_IOCFG0);
r.iocfg2 = ELECHOUSE_cc1101.SpiReadReg(CC1101_IOCFG2);
r.pktctrl0 = ELECHOUSE_cc1101.SpiReadReg(CC1101_PKTCTRL0);
r.mdmcfg2 = ELECHOUSE_cc1101.SpiReadReg(CC1101_MDMCFG2);
r.freqWord = (static_cast<uint32_t>(ELECHOUSE_cc1101.SpiReadReg(CC1101_FREQ2)) << 16) |
(static_cast<uint32_t>(ELECHOUSE_cc1101.SpiReadReg(CC1101_FREQ1)) << 8) |
ELECHOUSE_cc1101.SpiReadReg(CC1101_FREQ0);
float actual = word * (26.0f / 65536.0f);
Serial.printf("CC1101 PARTNUM=0x%02X VERSION=0x%02X IOCFG0=0x%02X IOCFG2=0x%02X PKTCTRL0=0x%02X MDMCFG2=0x%02X\n",
part, version, gdo0, gdo2, packet, modem);
r.partnum, r.version, r.iocfg0, r.iocfg2, r.pktctrl0, r.mdmcfg2);
Serial.printf("CC1101 frequency requested=%.3f readback=%.3f MHz; GDO0 TX=%u GDO2 RX=%u\n",
frequency, actual, BoardProfile::tx, BoardProfile::rx);
return part == 0 && version != 0 && version != 0xFF &&
gdo0 == 0x0D && gdo2 == 0x0D && (packet & 0x30) == 0x30 &&
(modem & 0x70) == 0x30 && actual > frequency - 0.01f && actual < frequency + 0.01f;
frequency, radioFrequencyMHz(r.freqWord), BoardProfile::tx, BoardProfile::rx);
return radioRegistersReady(r, frequency);
}
void transceiver_config_t::apply() {
@ -829,7 +828,7 @@ uint32_t Transceiver::submitTx(somfy_tx_job_t &job, bool priority, uint16_t dela
}
// Read published readiness without waiting behind a transmission. A queued
// job checks it again under the radio lock immediately before sending.
if(BoardProfile::integratedRadio && !this->available()) return 0;
if(!radioMayTransmit(BoardProfile::integratedRadio, this->available())) return 0;
uint32_t ticket;
{
TxQueueLock lock;
@ -903,7 +902,7 @@ void Transceiver::runJob(somfy_tx_job_t &job) {
start = this->sched.start(job.ticket, millis());
}
if(!start) return;
const bool complete = (!BoardProfile::integratedRadio || this->available()) && this->transmitJob(job);
const bool complete = radioMayTransmit(BoardProfile::integratedRadio, this->available()) && this->transmitJob(job);
TxQueueLock qlock;
this->sched.ended(job.ticket, complete, millis());
}