Report a dead radio clearly, retry receive once and drop stale repeats

A frequency scan was accepted when the radio was enabled but had failed its
checks, and the client waited for results that never came. It is now refused
with radioUnavailable: "The radio is disabled" or "The radio did not
respond". Generic boards are unchanged (available() equals config.enabled).

One missed switch into RX after a transmission or scan used to disable the
radio until reboot. startReceive() now goes idle and calls SetRx once more
(RADIO_RX_ATTEMPTS = 2, decided by radioRxOutcome) and gives up only after
that. Repeater frames are dropped while the integrated radio is unavailable
instead of being retried and sent stale later.

Serial output in startReceive(), checkBoardRadio() and BoardProfile moves to
ESP_LOG*, and the per-transmission RF switch readback is gone.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
R. Sid Shaytay 2026-11-14 18:50:41 -08:00
parent 28af01195a
commit 60d1872f35
No known key found for this signature in database
6 changed files with 105 additions and 21 deletions

View file

@ -1,4 +1,5 @@
#include "BoardProfile.h"
#include <esp_log.h>
#include <SPI.h>
#if defined(ESPSOMFY_LILYGO_T_EMBED_CC1101)
#include <driver/gpio.h>
@ -6,6 +7,7 @@
namespace BoardProfile {
#if defined(ESPSOMFY_LILYGO_T_EMBED_CC1101)
static const char *TAG = "Somfy";
static bool initialized = false;
static void output(uint8_t pin, uint8_t level) {
@ -36,7 +38,7 @@ bool prepareRadio(float frequency) {
begin();
// This hardware profile selects only the 433 MHz RF filter path.
if(!(frequency >= 387.0f && frequency <= 464.0f)) {
Serial.println("Radio disabled: frequency is outside the selected 433 MHz band");
ESP_LOGW(TAG, "Radio disabled: frequency is outside the selected 433 MHz band");
return false;
}
// Arduino 3 owns these pads while SPI is active. Release that ownership
@ -50,7 +52,7 @@ bool prepareRadio(float frequency) {
while(digitalRead(miso) == HIGH && millis() - started < 20) delay(1);
bool ready = digitalRead(miso) == LOW;
digitalWrite(cs, HIGH);
if(!ready) Serial.println("CC1101 not ready: MISO stayed HIGH; check power/shared SPI before retrying");
if(!ready) ESP_LOGW(TAG, "CC1101 not ready: MISO stayed HIGH; check power/shared SPI before retrying");
if(ready) SPI.begin(sck, miso, mosi, cs);
return ready;
}

View file

@ -2,6 +2,7 @@
#include <WiFi.h>
#include <atomic>
#include <new>
#include "RadioReadiness.h"
#include "esp_log.h"
#include "DeviceOps.h"
#include "ConfigSettings.h"
@ -235,8 +236,8 @@ void runDeviceOp(const loop_item_t &item) {
protoSocket.endReply(item.replyTo);
break;
case proto_type_t::beginFrequencyScan:
if(!somfy.transceiver.config.enabled) {
protoSocket.replyRefused(item.replyTo, proto_error_t::radioUnavailable, "The radio is disabled");
if(const char *why = radioRefusalReason(somfy.transceiver.config.enabled, somfy.transceiver.available())) {
protoSocket.replyRefused(item.replyTo, proto_error_t::radioUnavailable, why);
break;
}
somfy.transceiver.beginFrequencyScan();

View file

@ -19,3 +19,13 @@ rx_outcome_t radioRxOutcome(uint8_t marcstate, uint8_t attempt, uint8_t maxAttem
bool radioMayTransmit(bool integratedRadio, bool radioAvailable) {
return !integratedRadio || radioAvailable;
}
const char *radioRefusalReason(bool enabled, bool radioAvailable) {
if(!enabled) return "The radio is disabled";
if(!radioAvailable) return "The radio did not respond";
return nullptr;
}
bool repeaterFrameDropped(bool txSuspended, bool integratedRadio, bool radioAvailable) {
return txSuspended || !radioMayTransmit(integratedRadio, radioAvailable);
}

View file

@ -27,7 +27,9 @@ 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
// One retry: go idle and call SetRx again, so a single missed switch after a
// transmission does not disable the radio until reboot.
#define RADIO_RX_ATTEMPTS 2
enum class rx_outcome_t : uint8_t {
ready, // the radio is receiving
@ -43,4 +45,14 @@ rx_outcome_t radioRxOutcome(uint8_t marcstate, uint8_t attempt, uint8_t maxAttem
// always allowed, as before; the integrated one only once it is ready.
bool radioMayTransmit(bool integratedRadio, bool radioAvailable);
// Why a request that needs the radio (a frequency scan) is refused, or nullptr
// when it may go ahead. Texts are the ones clients show. A generic radio is
// available exactly when it is enabled, so it only ever gets the first one.
const char *radioRefusalReason(bool enabled, bool radioAvailable);
// Whether a queued repeater frame is discarded instead of kept for another
// try: while transmitting is suspended, or the integrated radio is unavailable
// (a stale repeat must not go out when the radio comes back).
bool repeaterFrameDropped(bool txSuspended, bool integratedRadio, bool radioAvailable);
#endif

View file

@ -379,24 +379,32 @@ bool Transceiver::startReceive() {
rxmode = 1;
pinMode(this->config.RXPin, INPUT);
interruptPin = digitalPinToInterrupt(this->config.RXPin);
ELECHOUSE_cc1101.SetRx();
if(BoardProfile::integratedRadio) {
uint8_t state;
do {
delay(1);
state = ELECHOUSE_cc1101.SpiReadStatus(CC1101_MARCSTATE) & 0x1F;
} 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(radioRxOutcome(state, 0, RADIO_RX_ATTEMPTS) != rx_outcome_t::ready) {
Serial.println("CC1101 failed to enter RX; radio unavailable");
// One missed switch into RX is retried from idle before the radio is
// given up as unavailable.
for(uint8_t attempt = 0;; attempt++) {
ELECHOUSE_cc1101.SetRx();
const uint32_t tried = millis();
uint8_t state;
do {
delay(1);
state = ELECHOUSE_cc1101.SpiReadStatus(CC1101_MARCSTATE) & 0x1F;
} while(state != RADIO_MARCSTATE_RX && millis() - tried < 20);
const rx_outcome_t outcome = radioRxOutcome(state, attempt, RADIO_RX_ATTEMPTS);
if(outcome == rx_outcome_t::ready) break;
ELECHOUSE_cc1101.setSidle();
if(outcome == rx_outcome_t::retry) {
ESP_LOGW(TAG, "CC1101 MARCSTATE=0x%02X, not RX; trying again", state);
continue;
}
ESP_LOGW(TAG, "CC1101 MARCSTATE=0x%02X, failed to enter RX; radio unavailable", state);
this->config.radioInit = false;
this->_available = false;
rxmode = 0;
return false;
}
}
else ELECHOUSE_cc1101.SetRx();
//attachInterrupt(interruptPin, handleReceive, FALLING);
attachInterrupt(interruptPin, handleReceive, CHANGE);
ESP_LOGD(TAG, "Enabled receive on Pin #%d Timing: %ld", this->config.RXPin, millis() - timing);
@ -624,9 +632,9 @@ static bool checkBoardRadio(float frequency) {
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);
Serial.printf("CC1101 PARTNUM=0x%02X VERSION=0x%02X IOCFG0=0x%02X IOCFG2=0x%02X PKTCTRL0=0x%02X MDMCFG2=0x%02X\n",
ESP_LOGD(TAG, "CC1101 PARTNUM=0x%02X VERSION=0x%02X IOCFG0=0x%02X IOCFG2=0x%02X PKTCTRL0=0x%02X MDMCFG2=0x%02X",
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",
ESP_LOGD(TAG, "CC1101 frequency requested=%.3f readback=%.3f MHz; GDO0 TX=%u GDO2 RX=%u",
frequency, radioFrequencyMHz(r.freqWord), BoardProfile::tx, BoardProfile::rx);
return radioRegistersReady(r, frequency);
}
@ -813,7 +821,9 @@ void Transceiver::loop() {
snprintf(frame_buf + fpos, sizeof(frame_buf) - fpos, "]");
ESP_LOGD(TAG, "%s", frame_buf);
}
if(this->submitTx(job) || this->txSuspended()) {
// Drop the frame rather than keep retrying it while the radio cannot
// send: a stale repeat must not go out when the radio comes back.
if(repeaterFrameDropped(this->txSuspended(), BoardProfile::integratedRadio, this->available()) || this->submitTx(job)) {
somfy_tx_t sent;
tx_queue.pop(&sent);
}