diff --git a/src/BoardProfile.cpp b/src/BoardProfile.cpp index 1aad81d..ff46c39 100644 --- a/src/BoardProfile.cpp +++ b/src/BoardProfile.cpp @@ -1,4 +1,5 @@ #include "BoardProfile.h" +#include #include #if defined(ESPSOMFY_LILYGO_T_EMBED_CC1101) #include @@ -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; } diff --git a/src/DeviceOps.cpp b/src/DeviceOps.cpp index 9b2a834..fe3b59d 100644 --- a/src/DeviceOps.cpp +++ b/src/DeviceOps.cpp @@ -2,6 +2,7 @@ #include #include #include +#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(); diff --git a/src/RadioReadiness.cpp b/src/RadioReadiness.cpp index d2ac7ef..4ef8b25 100644 --- a/src/RadioReadiness.cpp +++ b/src/RadioReadiness.cpp @@ -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); +} diff --git a/src/RadioReadiness.h b/src/RadioReadiness.h index 9c2ff82..c3e9dca 100644 --- a/src/RadioReadiness.h +++ b/src/RadioReadiness.h @@ -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 diff --git a/src/Transceiver.cpp b/src/Transceiver.cpp index b3f04de..235a203 100644 --- a/src/Transceiver.cpp +++ b/src/Transceiver.cpp @@ -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(ELECHOUSE_cc1101.SpiReadReg(CC1101_FREQ2)) << 16) | (static_cast(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); } diff --git a/test/test_radioreadiness/test_radioreadiness.cpp b/test/test_radioreadiness/test_radioreadiness.cpp index 0c44edb..0f2c19c 100644 --- a/test/test_radioreadiness/test_radioreadiness.cpp +++ b/test/test_radioreadiness/test_radioreadiness.cpp @@ -95,8 +95,47 @@ static void test_a_radio_not_in_rx_may_retry_while_tries_are_left() { static void test_zero_tries_never_retry() { TEST_ASSERT_EQUAL(rx_outcome_t::unavailable, radioRxOutcome(0x01, 0, 0)); } -static void test_the_firmware_makes_one_try_for_now() { - TEST_ASSERT_EQUAL(1, RADIO_RX_ATTEMPTS); +static void test_the_firmware_retries_receive_once() { + TEST_ASSERT_EQUAL(2, RADIO_RX_ATTEMPTS); +} +static void test_receive_retry_succeeds_on_the_second_try() { + TEST_ASSERT_EQUAL(rx_outcome_t::retry, radioRxOutcome(0x01, 0, RADIO_RX_ATTEMPTS)); + TEST_ASSERT_EQUAL(rx_outcome_t::ready, radioRxOutcome(RADIO_MARCSTATE_RX, 1, RADIO_RX_ATTEMPTS)); +} +static void test_receive_retry_fails_after_the_second_try() { + TEST_ASSERT_EQUAL(rx_outcome_t::retry, radioRxOutcome(0x01, 0, RADIO_RX_ATTEMPTS)); + TEST_ASSERT_EQUAL(rx_outcome_t::unavailable, radioRxOutcome(0x01, 1, RADIO_RX_ATTEMPTS)); +} + +static void test_refusal_text_for_a_disabled_radio() { + TEST_ASSERT_EQUAL_STRING("The radio is disabled", radioRefusalReason(false, false)); + TEST_ASSERT_EQUAL_STRING("The radio is disabled", radioRefusalReason(false, true)); +} +static void test_refusal_text_for_a_radio_that_did_not_respond() { + TEST_ASSERT_EQUAL_STRING("The radio did not respond", radioRefusalReason(true, false)); +} +static void test_a_working_radio_is_not_refused() { + TEST_ASSERT_NULL(radioRefusalReason(true, true)); +} +static void test_a_generic_radio_is_refused_only_when_disabled() { + // A generic radio is available exactly when enabled. + TEST_ASSERT_NULL(radioRefusalReason(true, true)); + TEST_ASSERT_EQUAL_STRING("The radio is disabled", radioRefusalReason(false, false)); +} + +static void test_repeater_frame_is_dropped_while_the_integrated_radio_is_unavailable() { + TEST_ASSERT_TRUE(repeaterFrameDropped(false, true, false)); +} +static void test_repeater_frame_is_kept_for_a_ready_radio() { + TEST_ASSERT_FALSE(repeaterFrameDropped(false, true, true)); + TEST_ASSERT_FALSE(repeaterFrameDropped(false, false, true)); +} +static void test_repeater_frame_is_dropped_while_tx_is_suspended() { + TEST_ASSERT_TRUE(repeaterFrameDropped(true, true, true)); + TEST_ASSERT_TRUE(repeaterFrameDropped(true, false, true)); +} +static void test_a_generic_radio_never_drops_a_repeat_for_availability() { + TEST_ASSERT_FALSE(repeaterFrameDropped(false, false, false)); } static void test_a_generic_radio_may_always_queue_and_send() { @@ -122,7 +161,17 @@ int main(int, char **) { RUN_TEST(test_a_radio_not_in_rx_is_unavailable_with_one_try); RUN_TEST(test_a_radio_not_in_rx_may_retry_while_tries_are_left); RUN_TEST(test_zero_tries_never_retry); - RUN_TEST(test_the_firmware_makes_one_try_for_now); + RUN_TEST(test_the_firmware_retries_receive_once); + RUN_TEST(test_receive_retry_succeeds_on_the_second_try); + RUN_TEST(test_receive_retry_fails_after_the_second_try); + RUN_TEST(test_refusal_text_for_a_disabled_radio); + RUN_TEST(test_refusal_text_for_a_radio_that_did_not_respond); + RUN_TEST(test_a_working_radio_is_not_refused); + RUN_TEST(test_a_generic_radio_is_refused_only_when_disabled); + RUN_TEST(test_repeater_frame_is_dropped_while_the_integrated_radio_is_unavailable); + RUN_TEST(test_repeater_frame_is_kept_for_a_ready_radio); + RUN_TEST(test_repeater_frame_is_dropped_while_tx_is_suspended); + RUN_TEST(test_a_generic_radio_never_drops_a_repeat_for_availability); RUN_TEST(test_a_generic_radio_may_always_queue_and_send); RUN_TEST(test_the_integrated_radio_transmits_only_when_ready); return UNITY_END();