From 28af01195a60e8b8d69b7791d5b6dcc25f48bf62 Mon Sep 17 00:00:00 2001 From: "R. Sid Shaytay" <352722946+r-sidshaytay@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:46:33 -0800 Subject: [PATCH] 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 --- .github/workflows/build.yaml | 3 - gcovr.cfg | 3 + platformio.ini | 2 +- src/BoardProfile.cpp | 9 - src/BoardProfile.h | 15 +- src/RadioReadiness.cpp | 21 ++ src/RadioReadiness.h | 46 ++++ src/ShadeGpio.cpp | 6 +- src/ShadeGpioGuard.h | 14 ++ src/Transceiver.cpp | 33 ++- test/stubs/Arduino.h | 56 ++++- test/stubs/SPI.h | 19 ++ test/stubs/driver/gpio.h | 8 + test/test_boardprofile/test_boardprofile.cpp | 47 ++++ .../test_boardprofile_plus.cpp | 221 ++++++++++++++++++ .../test_radioreadiness.cpp | 129 ++++++++++ tests/board-profile/Arduino.h | 26 --- tests/board-profile/ArduinoJson.h | 2 - tests/board-profile/Preferences.h | 8 - tests/board-profile/SPI.h | 11 - tests/board-profile/board-profile-test.cpp | 95 -------- tests/board-profile/driver/gpio.h | 4 - tests/board-profile/esp_log.h | 9 - tests/board-profile/extract-radio-setup.py | 29 --- tests/board-profile/radio-setup-test.cpp | 162 ------------- tests/board-profile/shade-gpio-test.cpp | 93 -------- tools/test-board-profile.sh | 41 ---- 27 files changed, 595 insertions(+), 517 deletions(-) create mode 100644 src/RadioReadiness.cpp create mode 100644 src/RadioReadiness.h create mode 100644 src/ShadeGpioGuard.h create mode 100644 test/stubs/SPI.h create mode 100644 test/stubs/driver/gpio.h create mode 100644 test/test_boardprofile/test_boardprofile.cpp create mode 100644 test/test_boardprofile_plus/test_boardprofile_plus.cpp create mode 100644 test/test_radioreadiness/test_radioreadiness.cpp delete mode 100644 tests/board-profile/Arduino.h delete mode 100644 tests/board-profile/ArduinoJson.h delete mode 100644 tests/board-profile/Preferences.h delete mode 100644 tests/board-profile/SPI.h delete mode 100644 tests/board-profile/board-profile-test.cpp delete mode 100644 tests/board-profile/driver/gpio.h delete mode 100644 tests/board-profile/esp_log.h delete mode 100644 tests/board-profile/extract-radio-setup.py delete mode 100644 tests/board-profile/radio-setup-test.cpp delete mode 100644 tests/board-profile/shade-gpio-test.cpp delete mode 100755 tools/test-board-profile.sh diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 827205e..470a42c 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -38,9 +38,6 @@ jobs: - name: Run native unit tests run: pio test -e coverage - - name: Test integrated board GPIO and radio readiness - run: bash tools/test-board-profile.sh - - name: Test the release scripts run: bash test/scripts/test_set_version.sh diff --git a/gcovr.cfg b/gcovr.cfg index 04fd331..acf69d3 100644 --- a/gcovr.cfg +++ b/gcovr.cfg @@ -7,3 +7,6 @@ sort = uncovered-percent print-summary = yes html-title = ESPSomfy-RTS native tests html-details = coverage/ +# BoardProfile.cpp is built once per board by the board profile tests, so the +# same function sits on different lines in each build. +merge-mode-functions = merge-use-line-min diff --git a/platformio.ini b/platformio.ini index df35719..de73424 100644 --- a/platformio.ini +++ b/platformio.ini @@ -173,7 +173,7 @@ build_flags = platform = native test_framework = unity test_build_src = yes -build_src_filter = -<*> + + + + + + + + + + + + + + + + + + + + + + + + + +build_src_filter = -<*> + + + + + + + + + + + + + + + + + + + + + + + + + + lib_deps = bblanchon/ArduinoJson@7.4.3 build_flags = diff --git a/src/BoardProfile.cpp b/src/BoardProfile.cpp index 802e715..1aad81d 100644 --- a/src/BoardProfile.cpp +++ b/src/BoardProfile.cpp @@ -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 } diff --git a/src/BoardProfile.h b/src/BoardProfile.h index 0e547e6..877a4be 100644 --- a/src/BoardProfile.h +++ b/src/BoardProfile.h @@ -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 } diff --git a/src/RadioReadiness.cpp b/src/RadioReadiness.cpp new file mode 100644 index 0000000..d2ac7ef --- /dev/null +++ b/src/RadioReadiness.cpp @@ -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; +} diff --git a/src/RadioReadiness.h b/src/RadioReadiness.h new file mode 100644 index 0000000..9c2ff82 --- /dev/null +++ b/src/RadioReadiness.h @@ -0,0 +1,46 @@ +#ifndef RADIO_READINESS_H +#define RADIO_READINESS_H +#include + +// 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 diff --git a/src/ShadeGpio.cpp b/src/ShadeGpio.cpp index 7085f95..85e48f5 100644 --- a/src/ShadeGpio.cpp +++ b/src/ShadeGpio.cpp @@ -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 diff --git a/src/ShadeGpioGuard.h b/src/ShadeGpioGuard.h new file mode 100644 index 0000000..b8b9a4e --- /dev/null +++ b/src/ShadeGpioGuard.h @@ -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 diff --git a/src/Transceiver.cpp b/src/Transceiver.cpp index e341e42..b3f04de 100644 --- a/src/Transceiver.cpp +++ b/src/Transceiver.cpp @@ -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(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(ELECHOUSE_cc1101.SpiReadReg(CC1101_FREQ2)) << 16) | (static_cast(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()); } diff --git a/test/stubs/Arduino.h b/test/stubs/Arduino.h index f247c50..9607a85 100644 --- a/test/stubs/Arduino.h +++ b/test/stubs/Arduino.h @@ -10,12 +10,64 @@ #include #include #include +#include #include typedef uint8_t byte; -// Fixed so anything formatted from it is deterministic. -inline unsigned long millis() { return 0; } +// Starts at 0 and only moves when a test calls delay() or sets it, so anything +// formatted from it is deterministic. +inline unsigned long stubMillis = 0; +inline unsigned long millis() { return stubMillis; } +inline void delay(unsigned long ms) { stubMillis += ms; } + +// A fake GPIO bank for board code. Every pin call is also logged in order +// so tests can check the startup sequence, not only the final levels. +constexpr int LOW = 0; +constexpr int HIGH = 1; +constexpr int INPUT = 0x01; +constexpr int OUTPUT = 0x03; +struct StubGpioEvent { + enum Kind : char { Mode = 'M', Level = 'L', Write = 'W', SpiBegin = 'B', SpiEnd = 'E' }; + Kind kind; + uint8_t pin; + int value; +}; +inline std::vector stubGpioLog; +inline int stubPinLevel[64] = {}; +inline int stubPinMode[64] = {}; +inline int stubPinReadOverride[64] = {}; // -1 for none, set by stubGpioReset() +inline void stubGpioReset() { + stubGpioLog.clear(); + for(int i = 0; i < 64; i++) { stubPinLevel[i] = 0; stubPinMode[i] = 0; stubPinReadOverride[i] = -1; } + stubMillis = 0; +} +// The ESP-IDF call that sets the output latch whatever the pin mode is. +inline void stubSetLatch(uint8_t pin, int level) { + stubPinLevel[pin] = level; + stubGpioLog.push_back({StubGpioEvent::Level, pin, level}); +} +inline void pinMode(uint8_t pin, uint8_t mode) { + stubPinMode[pin] = mode; + stubGpioLog.push_back({StubGpioEvent::Mode, pin, mode}); +} +// Arduino 3 only accepts writes once pinMode has made the pin an output. +inline void digitalWrite(uint8_t pin, uint8_t level) { + if(stubPinMode[pin] != OUTPUT) return; + stubPinLevel[pin] = level; + stubGpioLog.push_back({StubGpioEvent::Write, pin, level}); +} +inline int digitalRead(uint8_t pin) { + return stubPinReadOverride[pin] >= 0 ? stubPinReadOverride[pin] : stubPinLevel[pin]; +} + +// Serial output is discarded. +struct StubSerial { + template void printf(const char *, Args...) {} + void println(const char *) {} +}; +inline StubSerial Serial; + // Tests that measure time set this. inline unsigned long stubMicros = 0; inline unsigned long micros() { return stubMicros; } diff --git a/test/stubs/SPI.h b/test/stubs/SPI.h new file mode 100644 index 0000000..ef813fe --- /dev/null +++ b/test/stubs/SPI.h @@ -0,0 +1,19 @@ +// Arduino SPI stand-in for the native test env: remembers whether the bus is +// up and logs begin/end in order with the pin calls (see Arduino.h). +#pragma once +#include +struct StubSPI { + bool active = false; + unsigned int begins = 0; + unsigned int ends = 0; + int8_t sck = -1, miso = -1, mosi = -1, cs = -1; + void begin(int8_t s, int8_t mi, int8_t mo, int8_t c) { + active = true; begins++; sck = s; miso = mi; mosi = mo; cs = c; + stubGpioLog.push_back({StubGpioEvent::SpiBegin, 0, 0}); + } + void end() { + active = false; ends++; + stubGpioLog.push_back({StubGpioEvent::SpiEnd, 0, 0}); + } +}; +inline StubSPI SPI; diff --git a/test/stubs/driver/gpio.h b/test/stubs/driver/gpio.h new file mode 100644 index 0000000..c4da958 --- /dev/null +++ b/test/stubs/driver/gpio.h @@ -0,0 +1,8 @@ +// ESP-IDF gpio_set_level stand-in for the native test env; see Arduino.h. +#pragma once +#include +using gpio_num_t = int; +inline int gpio_set_level(gpio_num_t pin, uint32_t level) { + stubSetLatch(static_cast(pin), static_cast(level)); + return 0; +} diff --git a/test/test_boardprofile/test_boardprofile.cpp b/test/test_boardprofile/test_boardprofile.cpp new file mode 100644 index 0000000..bf8c915 --- /dev/null +++ b/test/test_boardprofile/test_boardprofile.cpp @@ -0,0 +1,47 @@ +// A generic board: the profile reserves nothing and does nothing. +#include +#include "BoardProfile.cpp" +#include "ShadeGpioGuard.h" + +void setUp() { + stubGpioReset(); + SPI = StubSPI(); +} +void tearDown() {} + +static void test_there_is_no_integrated_radio() { + TEST_ASSERT_FALSE(BoardProfile::integratedRadio); +} +static void test_no_pad_is_reserved() { + for(unsigned pin = 0; pin < 256; pin++) TEST_ASSERT_FALSE(BoardProfile::usesPin((uint8_t)pin)); +} +static void test_begin_leaves_the_hardware_alone() { + BoardProfile::begin(); + TEST_ASSERT_EQUAL_UINT(0, stubGpioLog.size()); + TEST_ASSERT_FALSE(SPI.active); + TEST_ASSERT_EQUAL_UINT(0, SPI.begins); +} +static void test_any_frequency_is_accepted_without_touching_the_bus() { + TEST_ASSERT_TRUE(BoardProfile::prepareRadio(868.0f)); + TEST_ASSERT_TRUE(BoardProfile::prepareRadio(433.42f)); + TEST_ASSERT_EQUAL_UINT(0, stubGpioLog.size()); + TEST_ASSERT_EQUAL_UINT(0, SPI.begins); + TEST_ASSERT_EQUAL_UINT(0, SPI.ends); +} +static void test_shade_outputs_may_use_any_pad() { + for(unsigned pin = 0; pin < 256; pin++) { + const uint8_t p = (uint8_t)pin; + TEST_ASSERT_FALSE(shadeOutputsUseBoardPins(radio_proto::GP_Relay, p, p, p)); + TEST_ASSERT_FALSE(shadeOutputsUseBoardPins(radio_proto::GP_Remote, p, p, p)); + } +} + +int main(int, char **) { + UNITY_BEGIN(); + RUN_TEST(test_there_is_no_integrated_radio); + RUN_TEST(test_no_pad_is_reserved); + RUN_TEST(test_begin_leaves_the_hardware_alone); + RUN_TEST(test_any_frequency_is_accepted_without_touching_the_bus); + RUN_TEST(test_shade_outputs_may_use_any_pad); + return UNITY_END(); +} diff --git a/test/test_boardprofile_plus/test_boardprofile_plus.cpp b/test/test_boardprofile_plus/test_boardprofile_plus.cpp new file mode 100644 index 0000000..7c84caf --- /dev/null +++ b/test/test_boardprofile_plus/test_boardprofile_plus.cpp @@ -0,0 +1,221 @@ +// The LILYGO T-Embed CC1101 profile: which pads the board owns, the hardware +// startup order and the radio bring-up. BoardProfile.cpp is included so it +// is built for this board whatever the env's flags are. +#include +#include +#define ESPSOMFY_LILYGO_T_EMBED_CC1101 1 +#define CONFIG_IDF_TARGET_ESP32S3 1 +#include "BoardProfile.cpp" +#include "ShadeGpioGuard.h" + +using E = StubGpioEvent; +static const uint8_t RESERVED[] = {3, 9, 10, 11, 12, 13, 15, 21, 38, 41, 43, 44, 47, 48}; + +void setUp() { + // begin() runs once per boot, so what it set up stays between tests. + int modes[64]; + memcpy(modes, stubPinMode, sizeof(modes)); + stubGpioReset(); + memcpy(stubPinMode, modes, sizeof(modes)); + SPI = StubSPI(); +} +void tearDown() {} + +static unsigned countEvents(E::Kind kind, uint8_t pin, int value) { + unsigned n = 0; + for(const auto &e : stubGpioLog) if(e.kind == kind && e.pin == pin && e.value == value) n++; + return n; +} +static int firstEvent(E::Kind kind, uint8_t pin) { + for(size_t i = 0; i < stubGpioLog.size(); i++) + if(stubGpioLog[i].kind == kind && stubGpioLog[i].pin == pin) return (int)i; + return -1; +} + +// --- reserved pads --- + +static void test_profile_has_an_integrated_radio() { + TEST_ASSERT_TRUE(BoardProfile::integratedRadio); +} +static void test_the_wiring_is_the_lilygo_one() { + TEST_ASSERT_EQUAL(15, BoardProfile::power); + TEST_ASSERT_EQUAL(11, BoardProfile::sck); + TEST_ASSERT_EQUAL(9, BoardProfile::mosi); + TEST_ASSERT_EQUAL(10, BoardProfile::miso); + TEST_ASSERT_EQUAL(12, BoardProfile::cs); + TEST_ASSERT_EQUAL(3, BoardProfile::tx); + TEST_ASSERT_EQUAL(38, BoardProfile::rx); +} +static void test_every_board_pad_is_reserved() { + for(uint8_t pin : RESERVED) TEST_ASSERT_TRUE_MESSAGE(BoardProfile::usesPin(pin), "pad not reserved"); +} +static void test_usb_and_flash_pads_are_reserved() { + TEST_ASSERT_TRUE(BoardProfile::usesPin(19)); + TEST_ASSERT_TRUE(BoardProfile::usesPin(20)); + for(uint8_t pin = 26; pin <= 37; pin++) TEST_ASSERT_TRUE(BoardProfile::usesPin(pin)); +} +static void test_other_pads_are_free() { + TEST_ASSERT_FALSE(BoardProfile::usesPin(0)); + TEST_ASSERT_FALSE(BoardProfile::usesPin(16)); + TEST_ASSERT_FALSE(BoardProfile::usesPin(18)); + TEST_ASSERT_FALSE(BoardProfile::usesPin(25)); + TEST_ASSERT_FALSE(BoardProfile::usesPin(39)); + TEST_ASSERT_FALSE(BoardProfile::usesPin(46)); + TEST_ASSERT_FALSE(BoardProfile::usesPin(255)); +} + +// --- shade outputs on board pads (a backup may carry another board's pins) --- + +static void test_a_relay_output_on_any_board_pad_is_refused() { + for(uint8_t pin = 0; pin < 64; pin++) { + if(!BoardProfile::usesPin(pin)) continue; + TEST_ASSERT_TRUE(shadeOutputsUseBoardPins(radio_proto::GP_Relay, pin, 17, 18)); + TEST_ASSERT_TRUE(shadeOutputsUseBoardPins(radio_proto::GP_Relay, 16, pin, 18)); + TEST_ASSERT_TRUE(shadeOutputsUseBoardPins(radio_proto::GP_Remote, pin, 17, 18)); + TEST_ASSERT_TRUE(shadeOutputsUseBoardPins(radio_proto::GP_Remote, 16, pin, 18)); + TEST_ASSERT_TRUE(shadeOutputsUseBoardPins(radio_proto::GP_Remote, 16, 17, pin)); + } +} +static void test_a_relay_has_no_my_output() { + for(uint8_t pin = 0; pin < 64; pin++) + if(BoardProfile::usesPin(pin)) + TEST_ASSERT_FALSE(shadeOutputsUseBoardPins(radio_proto::GP_Relay, 16, 17, pin)); +} +static void test_free_pads_are_allowed() { + TEST_ASSERT_FALSE(shadeOutputsUseBoardPins(radio_proto::GP_Relay, 16, 17, 18)); + TEST_ASSERT_FALSE(shadeOutputsUseBoardPins(radio_proto::GP_Remote, 16, 17, 18)); +} +static void test_radio_shades_have_no_gpio_outputs() { + TEST_ASSERT_FALSE(shadeOutputsUseBoardPins(radio_proto::RTS, 3, 9, 10)); + TEST_ASSERT_FALSE(shadeOutputsUseBoardPins(radio_proto::RTW, 3, 9, 10)); + TEST_ASSERT_FALSE(shadeOutputsUseBoardPins(radio_proto::RTV, 3, 9, 10)); +} + +// --- startup order; this runs first as begin() only acts once --- + +static void test_begin_deselects_every_slave_before_the_power_rail() { + BoardProfile::begin(); + const int power = firstEvent(E::Level, BoardProfile::power); + TEST_ASSERT_TRUE(power >= 0); + TEST_ASSERT_EQUAL_UINT(1, countEvents(E::Level, BoardProfile::power, HIGH)); + // Nothing happens after the rail comes up except settling. + TEST_ASSERT_EQUAL((int)stubGpioLog.size() - 2, power); // then its own pinMode + TEST_ASSERT_EQUAL(E::Mode, stubGpioLog.back().kind); + TEST_ASSERT_EQUAL(BoardProfile::power, stubGpioLog.back().pin); + const uint8_t high[] = {BoardProfile::cs, BoardProfile::sdCS, BoardProfile::displayCS, + BoardProfile::nrfCS, BoardProfile::sw1, BoardProfile::sw0}; + const uint8_t low[] = {BoardProfile::nrfCE, BoardProfile::backlight}; + for(uint8_t pin : high) { + TEST_ASSERT_TRUE(firstEvent(E::Level, pin) < power); + TEST_ASSERT_EQUAL_UINT(1, countEvents(E::Level, pin, HIGH)); + TEST_ASSERT_EQUAL(OUTPUT, stubPinMode[pin]); + } + for(uint8_t pin : low) { + TEST_ASSERT_TRUE(firstEvent(E::Level, pin) < power); + TEST_ASSERT_EQUAL_UINT(1, countEvents(E::Level, pin, LOW)); + TEST_ASSERT_EQUAL(OUTPUT, stubPinMode[pin]); + } + TEST_ASSERT_EQUAL(OUTPUT, stubPinMode[BoardProfile::power]); + TEST_ASSERT_EQUAL(10, (int)stubMillis); // rail settling + // Each pin's safe level is latched before it is made an output. + for(const auto &e : stubGpioLog) { + if(e.kind != E::Mode) continue; + TEST_ASSERT_EQUAL(OUTPUT, e.value); + int latch = -1, mode = -1; + for(size_t i = 0; i < stubGpioLog.size(); i++) { + if(stubGpioLog[i].pin != e.pin) continue; + if(stubGpioLog[i].kind == E::Level && latch < 0) latch = (int)i; + if(stubGpioLog[i].kind == E::Mode && mode < 0) mode = (int)i; + } + TEST_ASSERT_TRUE_MESSAGE(latch >= 0 && latch < mode, "pin became an output before its level was latched"); + } + // The shared SPI pads are left alone until the radio is prepared. + for(uint8_t pin : {BoardProfile::sck, BoardProfile::mosi, BoardProfile::miso}) + TEST_ASSERT_EQUAL(-1, firstEvent(E::Mode, pin)); + TEST_ASSERT_FALSE(SPI.active); +} +static void test_a_second_begin_does_nothing() { + // begin() ran in the previous test; starting again changes nothing. + stubGpioLog.clear(); + BoardProfile::begin(); + TEST_ASSERT_EQUAL_UINT(0, stubGpioLog.size()); +} + +// --- radio bring-up --- + +static void test_prepare_releases_spi_before_it_probes_the_pads() { + TEST_ASSERT_TRUE(BoardProfile::prepareRadio(433.42f)); + const int end = firstEvent(E::SpiEnd, 0); + TEST_ASSERT_TRUE(end >= 0); + for(uint8_t pin : {BoardProfile::sck, BoardProfile::mosi, BoardProfile::miso}) + TEST_ASSERT_TRUE(firstEvent(E::Mode, pin) > end); + TEST_ASSERT_EQUAL(INPUT, stubPinMode[BoardProfile::miso]); + TEST_ASSERT_EQUAL(OUTPUT, stubPinMode[BoardProfile::mosi]); + TEST_ASSERT_EQUAL(OUTPUT, stubPinMode[BoardProfile::sck]); + TEST_ASSERT_EQUAL(HIGH, stubPinLevel[BoardProfile::cs]); // released after the probe + TEST_ASSERT_TRUE(SPI.active); + TEST_ASSERT_EQUAL_UINT(1, SPI.begins); + TEST_ASSERT_EQUAL(11, SPI.sck); + TEST_ASSERT_EQUAL(10, SPI.miso); + TEST_ASSERT_EQUAL(9, SPI.mosi); + TEST_ASSERT_EQUAL(12, SPI.cs); +} +static void test_prepare_can_run_again_on_a_running_bus() { + TEST_ASSERT_TRUE(BoardProfile::prepareRadio(433.42f)); + TEST_ASSERT_TRUE(BoardProfile::prepareRadio(433.42f)); + TEST_ASSERT_EQUAL_UINT(2, SPI.begins); + TEST_ASSERT_EQUAL_UINT(2, SPI.ends); + TEST_ASSERT_TRUE(SPI.active); +} +static void test_prepare_refuses_a_frequency_outside_the_433_band() { + TEST_ASSERT_FALSE(BoardProfile::prepareRadio(868.0f)); + TEST_ASSERT_FALSE(BoardProfile::prepareRadio(386.9f)); + TEST_ASSERT_FALSE(BoardProfile::prepareRadio(464.1f)); + TEST_ASSERT_FALSE(BoardProfile::prepareRadio(0.0f)); + TEST_ASSERT_FALSE(BoardProfile::prepareRadio(NAN)); + TEST_ASSERT_TRUE(BoardProfile::prepareRadio(387.0f)); + TEST_ASSERT_TRUE(BoardProfile::prepareRadio(464.0f)); +} +static void test_a_rejected_frequency_leaves_the_bus_alone() { + BoardProfile::prepareRadio(868.0f); + TEST_ASSERT_EQUAL_UINT(0, stubGpioLog.size()); + TEST_ASSERT_EQUAL_UINT(0, SPI.begins); +} +static void test_prepare_fails_fast_when_the_radio_holds_miso_high() { + stubPinReadOverride[BoardProfile::miso] = HIGH; + TEST_ASSERT_FALSE(BoardProfile::prepareRadio(433.42f)); + TEST_ASSERT_TRUE(stubMillis <= 25); // not a hang inside the radio driver + TEST_ASSERT_EQUAL(HIGH, stubPinLevel[BoardProfile::cs]); + TEST_ASSERT_FALSE(SPI.active); + TEST_ASSERT_EQUAL_UINT(0, SPI.begins); +} +static void test_a_retry_after_a_fault_does_not_power_cycle_the_board() { + stubPinReadOverride[BoardProfile::miso] = HIGH; + TEST_ASSERT_FALSE(BoardProfile::prepareRadio(433.42f)); + stubPinReadOverride[BoardProfile::miso] = -1; + TEST_ASSERT_TRUE(BoardProfile::prepareRadio(433.42f)); + TEST_ASSERT_EQUAL_UINT(0, countEvents(E::Level, BoardProfile::power, HIGH)); + TEST_ASSERT_EQUAL_UINT(0, countEvents(E::Level, BoardProfile::power, LOW)); +} + +int main(int, char **) { + UNITY_BEGIN(); + RUN_TEST(test_begin_deselects_every_slave_before_the_power_rail); + RUN_TEST(test_a_second_begin_does_nothing); + RUN_TEST(test_profile_has_an_integrated_radio); + RUN_TEST(test_the_wiring_is_the_lilygo_one); + RUN_TEST(test_every_board_pad_is_reserved); + RUN_TEST(test_usb_and_flash_pads_are_reserved); + RUN_TEST(test_other_pads_are_free); + RUN_TEST(test_a_relay_output_on_any_board_pad_is_refused); + RUN_TEST(test_a_relay_has_no_my_output); + RUN_TEST(test_free_pads_are_allowed); + RUN_TEST(test_radio_shades_have_no_gpio_outputs); + RUN_TEST(test_prepare_releases_spi_before_it_probes_the_pads); + RUN_TEST(test_prepare_can_run_again_on_a_running_bus); + RUN_TEST(test_prepare_refuses_a_frequency_outside_the_433_band); + RUN_TEST(test_a_rejected_frequency_leaves_the_bus_alone); + RUN_TEST(test_prepare_fails_fast_when_the_radio_holds_miso_high); + RUN_TEST(test_a_retry_after_a_fault_does_not_power_cycle_the_board); + return UNITY_END(); +} diff --git a/test/test_radioreadiness/test_radioreadiness.cpp b/test/test_radioreadiness/test_radioreadiness.cpp new file mode 100644 index 0000000..0c44edb --- /dev/null +++ b/test/test_radioreadiness/test_radioreadiness.cpp @@ -0,0 +1,129 @@ +#include +#include +#include "RadioReadiness.h" + +void setUp() {} +void tearDown() {} + +// What a healthy CC1101 reports when set up for 433.42 MHz async OOK. +static radio_readings_t healthy() { + radio_readings_t r; + r.partnum = 0; + r.version = 0x14; + r.iocfg0 = 0x0D; + r.iocfg2 = 0x0D; + r.pktctrl0 = 0x32; + r.mdmcfg2 = 0x3C; + r.freqWord = 0x10AB85; + return r; +} +static const float MHZ = 433.42f; + +static void test_frequency_word_converts_to_mhz() { + TEST_ASSERT_FLOAT_WITHIN(0.01f, 433.42f, radioFrequencyMHz(0x10AB85)); + TEST_ASSERT_EQUAL_FLOAT(0.0f, radioFrequencyMHz(0)); +} +static void test_a_healthy_radio_is_ready() { + TEST_ASSERT_TRUE(radioRegistersReady(healthy(), MHZ)); +} +static void test_a_part_that_is_not_a_cc1101_is_refused() { + radio_readings_t r = healthy(); + r.partnum = 1; + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); + r.partnum = 0xFF; + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); +} +static void test_no_chip_answering_is_refused() { + radio_readings_t r = healthy(); + r.version = 0; + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); + r.version = 0xFF; + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); +} +static void test_gdo_pins_must_carry_the_async_data() { + radio_readings_t r = healthy(); + r.iocfg0 = 0; + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); + r = healthy(); + r.iocfg2 = 0; + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); +} +static void test_packet_format_and_modulation_must_be_set() { + radio_readings_t r = healthy(); + r.pktctrl0 = 0; + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); + r = healthy(); + r.pktctrl0 = 0x10; // only part of the async format bits + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); + r = healthy(); + r.mdmcfg2 = 0; + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); + r = healthy(); + r.mdmcfg2 = 0x0C; // 2-FSK instead of OOK + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); +} +static void test_unrelated_register_bits_are_ignored() { + radio_readings_t r = healthy(); + r.pktctrl0 = 0x3F; + r.mdmcfg2 = 0xBF; + TEST_ASSERT_TRUE(radioRegistersReady(r, MHZ)); +} +static void test_the_frequency_readback_must_match_the_request() { + radio_readings_t r = healthy(); + r.freqWord = 0; + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); + r.freqWord = 0x10AB85 + 100; // 0.04 MHz away + TEST_ASSERT_FALSE(radioRegistersReady(r, MHZ)); + TEST_ASSERT_FALSE(radioRegistersReady(healthy(), 433.92f)); + TEST_ASSERT_FALSE(radioRegistersReady(healthy(), NAN)); +} + +static void test_rx_state_ready() { + TEST_ASSERT_EQUAL(rx_outcome_t::ready, radioRxOutcome(RADIO_MARCSTATE_RX, 0, 1)); + TEST_ASSERT_EQUAL(rx_outcome_t::ready, radioRxOutcome(0xED, 0, 1)); // upper bits are not state +} +static void test_a_radio_not_in_rx_is_unavailable_with_one_try() { + TEST_ASSERT_EQUAL(rx_outcome_t::unavailable, radioRxOutcome(0x00, 0, 1)); + TEST_ASSERT_EQUAL(rx_outcome_t::unavailable, radioRxOutcome(0x01, 0, 1)); // IDLE + TEST_ASSERT_EQUAL(rx_outcome_t::unavailable, radioRxOutcome(0x11, 0, 1)); // RXFIFO_OVERFLOW +} +static void test_a_radio_not_in_rx_may_retry_while_tries_are_left() { + TEST_ASSERT_EQUAL(rx_outcome_t::retry, radioRxOutcome(0x01, 0, 2)); + TEST_ASSERT_EQUAL(rx_outcome_t::unavailable, radioRxOutcome(0x01, 1, 2)); + TEST_ASSERT_EQUAL(rx_outcome_t::ready, radioRxOutcome(RADIO_MARCSTATE_RX, 1, 2)); +} +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_a_generic_radio_may_always_queue_and_send() { + TEST_ASSERT_TRUE(radioMayTransmit(false, false)); + TEST_ASSERT_TRUE(radioMayTransmit(false, true)); +} +static void test_the_integrated_radio_transmits_only_when_ready() { + TEST_ASSERT_TRUE(radioMayTransmit(true, true)); + TEST_ASSERT_FALSE(radioMayTransmit(true, false)); +} + +int main(int, char **) { + UNITY_BEGIN(); + RUN_TEST(test_frequency_word_converts_to_mhz); + RUN_TEST(test_a_healthy_radio_is_ready); + RUN_TEST(test_a_part_that_is_not_a_cc1101_is_refused); + RUN_TEST(test_no_chip_answering_is_refused); + RUN_TEST(test_gdo_pins_must_carry_the_async_data); + RUN_TEST(test_packet_format_and_modulation_must_be_set); + RUN_TEST(test_unrelated_register_bits_are_ignored); + RUN_TEST(test_the_frequency_readback_must_match_the_request); + RUN_TEST(test_rx_state_ready); + 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_a_generic_radio_may_always_queue_and_send); + RUN_TEST(test_the_integrated_radio_transmits_only_when_ready); + return UNITY_END(); +} diff --git a/tests/board-profile/Arduino.h b/tests/board-profile/Arduino.h deleted file mode 100644 index b7a69a0..0000000 --- a/tests/board-profile/Arduino.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include -#include -using byte = uint8_t; -class String; - -constexpr int LOW = 0; -constexpr int HIGH = 1; -constexpr int INPUT = 0x01; -constexpr int OUTPUT = 0x03; -void pinMode(uint8_t pin, uint8_t mode); -void digitalWrite(uint8_t pin, uint8_t value); -int digitalRead(uint8_t pin); -unsigned long millis(); -void delay(unsigned long duration); - -struct TestSerial { - template void printf(const char *, Args...) {} - void println(const char *) {} -}; -extern TestSerial Serial; -struct TestESP { - unsigned int getFlashChipSize() { return 16777216; } - unsigned int getPsramSize() { return 8388608; } -}; -extern TestESP ESP; diff --git a/tests/board-profile/ArduinoJson.h b/tests/board-profile/ArduinoJson.h deleted file mode 100644 index 4d66643..0000000 --- a/tests/board-profile/ArduinoJson.h +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -class JsonObject; diff --git a/tests/board-profile/Preferences.h b/tests/board-profile/Preferences.h deleted file mode 100644 index daac39a..0000000 --- a/tests/board-profile/Preferences.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -extern bool testRadioInit; -struct Preferences { - void begin(const char *) {} - bool getBool(const char *, bool) { return testRadioInit; } - void putBool(const char *, bool value) { testRadioInit = value; } - void end() {} -}; diff --git a/tests/board-profile/SPI.h b/tests/board-profile/SPI.h deleted file mode 100644 index 2bc0ca5..0000000 --- a/tests/board-profile/SPI.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include - -struct TestSPI { - bool active = false; - unsigned int starts = 0; - unsigned int stops = 0; - void begin(int8_t sck, int8_t miso, int8_t mosi, int8_t cs); - void end(); -}; -extern TestSPI SPI; diff --git a/tests/board-profile/board-profile-test.cpp b/tests/board-profile/board-profile-test.cpp deleted file mode 100644 index 13f99c8..0000000 --- a/tests/board-profile/board-profile-test.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include -#include "BoardProfile.h" -#include "SPI.h" -#include - -TestSerial Serial; -TestESP ESP; -TestSPI SPI; -void TestSPI::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t cs) { - assert(sck == 11 && miso == 10 && mosi == 9 && cs == 12); - assert(!active); - active = true; - starts++; -} -void TestSPI::end() { active = false; stops++; } -static int levels[49] = {}; -static int modes[49] = {}; -static unsigned long clockMs = 0; -static bool stuckMiso = false; -static unsigned int powerTransitions = 0; - -void pinMode(uint8_t pin, uint8_t mode) { - // Arduino 3 must release SPI ownership before preflight changes its pins. - if(pin == 9 || pin == 10 || pin == 11) assert(!SPI.active); - if(mode == OUTPUT) { - // Initial output enable must already have the safe level latched. - const bool high = pin == 12 || pin == 13 || pin == 15 || pin == 41 || - pin == 44 || pin == 47 || pin == 48; - assert(levels[pin] == (high ? HIGH : LOW)); - } - modes[pin] = mode; -} -int gpio_set_level(gpio_num_t pin, uint32_t value) { - if(pin == 15 && value == HIGH) { - // No other SPI device may drive MISO when the rail comes up. - assert(levels[12] == HIGH && levels[13] == HIGH); - assert(levels[41] == HIGH && levels[44] == HIGH); - assert(levels[43] == LOW && levels[21] == LOW); - assert(levels[47] == HIGH && levels[48] == HIGH); - const uint8_t controls[] = {12, 13, 21, 41, 43, 44, 47, 48}; - for(auto control : controls) assert(modes[control] == OUTPUT); - powerTransitions++; - } - levels[pin] = value; - return 0; -} -void digitalWrite(uint8_t pin, uint8_t value) { - // Arduino 3 only accepts writes after pinMode establishes GPIO ownership. - if(modes[pin] == OUTPUT) gpio_set_level(pin, value); -} -int digitalRead(uint8_t pin) { - if(pin == 10) return stuckMiso ? HIGH : LOW; - return levels[pin]; -} -unsigned long millis() { return clockMs; } -void delay(unsigned long duration) { clockMs += duration; } - -int main() { - BoardProfile::begin(); - if(BoardProfile::integratedRadio) { - assert(powerTransitions == 1); - assert(modes[15] == OUTPUT); - assert(BoardProfile::prepareRadio(433.42f)); - assert(SPI.active && SPI.starts == 1); - assert(modes[10] == INPUT && modes[9] == OUTPUT && modes[11] == OUTPUT); - assert(levels[12] == HIGH); // Release CS after readiness probe. - assert(BoardProfile::prepareRadio(433.42f)); // Reconfigure a running bus safely. - assert(SPI.active && SPI.starts == 2 && SPI.stops == 2); - assert(!BoardProfile::prepareRadio(868.0f)); - assert(!BoardProfile::prepareRadio(0.0f / 0.0f)); - stuckMiso = true; - auto started = millis(); - assert(!BoardProfile::prepareRadio(433.42f)); - assert(millis() - started <= 25); // Fail safely instead of hanging in SmartRC. - assert(levels[12] == HIGH); - assert(!SPI.active); // A failed preflight must not initialize SPI. - stuckMiso = false; - assert(BoardProfile::prepareRadio(433.42f)); - assert(powerTransitions == 1); // Retrying must not power-cycle shared devices. - const uint8_t reserved[] = {3, 9, 10, 11, 12, 13, 15, 21, 38, 41, 43, 44, 47, 48}; - for(auto pin : reserved) assert(BoardProfile::usesPin(pin)); - assert(BoardProfile::usesPin(19) && BoardProfile::usesPin(20)); - for(uint8_t pin = 26; pin <= 37; pin++) assert(BoardProfile::usesPin(pin)); - assert(!BoardProfile::usesPin(255)); - assert(!BoardProfile::usesPin(16)); - } - else { - assert(powerTransitions == 0); - assert(!SPI.active && SPI.starts == 0 && SPI.stops == 0); - assert(BoardProfile::prepareRadio(868.0f)); - for(uint8_t pin = 0; pin < 49; pin++) assert(!BoardProfile::usesPin(pin)); - } - puts("Board profile checks passed"); -} diff --git a/tests/board-profile/driver/gpio.h b/tests/board-profile/driver/gpio.h deleted file mode 100644 index 94b03e7..0000000 --- a/tests/board-profile/driver/gpio.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once -#include -using gpio_num_t = int; -int gpio_set_level(gpio_num_t pin, uint32_t value); diff --git a/tests/board-profile/esp_log.h b/tests/board-profile/esp_log.h deleted file mode 100644 index 5ddc030..0000000 --- a/tests/board-profile/esp_log.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -// Keep the test logger a function call so unused firmware format arguments are -// still type-checked and the production TAG is referenced. -template inline void testLog(const char *, const char *, Args...) {} -#define ESP_LOGD(...) testLog(__VA_ARGS__) - -#define ESP_LOGE(...) testLog(__VA_ARGS__) -#define ESP_LOGW(...) testLog(__VA_ARGS__) -#define ESP_LOGI(...) testLog(__VA_ARGS__) diff --git a/tests/board-profile/extract-radio-setup.py b/tests/board-profile/extract-radio-setup.py deleted file mode 100644 index 845abed..0000000 --- a/tests/board-profile/extract-radio-setup.py +++ /dev/null @@ -1,29 +0,0 @@ -"""Compile selected production methods without the unrelated RTOS/network code.""" -from pathlib import Path -import sys - -source = Path(sys.argv[1]).read_text() -names = [ - 'static bool checkBoardRadio(', - 'void transceiver_config_t::applyBoardPins(', - 'void transceiver_config_t::apply(', - 'void Transceiver::setConfig(', - 'bool Transceiver::usesPin(', - 'void Transceiver::enableReceive(', - 'bool Transceiver::startReceive(', - 'void Transceiver::disableReceive(', - 'uint32_t Transceiver::submitTx(', -] -methods = [] -for name in names: - assert source.count(name) == 1, f'Production setup method changed: {name}' - start = source.index(name) - brace = source.index('{', start) - depth = 1 - end = brace + 1 - while depth: - assert end < len(source), f'Unterminated method: {name}' - depth += (source[end] == '{') - (source[end] == '}') - end += 1 - methods.append(source[start:end]) -Path(sys.argv[2]).write_text('\n\n'.join(methods) + '\n') diff --git a/tests/board-profile/radio-setup-test.cpp b/tests/board-profile/radio-setup-test.cpp deleted file mode 100644 index c8fbd72..0000000 --- a/tests/board-profile/radio-setup-test.cpp +++ /dev/null @@ -1,162 +0,0 @@ -// Exercise the actual radio setup, RX and queue-admission methods, replacing -// only hardware/RTOS boundaries. The script extracts those definitions from -// Transceiver.cpp so the test cannot silently exercise a copy of the logic. -#include -#include -#include -#include -#include "Transceiver.h" -#include "SPI.h" -#include "esp_log.h" -#include -#include - -TestSerial Serial; -TestESP ESP; -TestSPI SPI; -void TestSPI::begin(int8_t, int8_t, int8_t, int8_t) { active = true; } -void TestSPI::end() { active = false; } -static int levels[49] = {}; -static bool stuckMiso; -static unsigned long clockMs; -void pinMode(uint8_t, uint8_t) {} -void digitalWrite(uint8_t pin, uint8_t value) { levels[pin] = value; } -int digitalRead(uint8_t pin) { return pin == 10 && stuckMiso ? HIGH : levels[pin]; } -int gpio_set_level(gpio_num_t pin, uint32_t value) { levels[pin] = value; return 0; } -unsigned long millis() { return clockMs; } -void delay(unsigned long ms) { clockMs += ms; } -static unsigned int attached; -static unsigned int rxStarts; -static unsigned int initializations; -static unsigned int saves; -static bool observedReadyDuringRx; -static uint32_t queuedDuringRx; -int digitalPinToInterrupt(uint8_t pin) { return pin; } -constexpr int CHANGE = 1; -void attachInterrupt(int, void (*)(), int) { attached++; } -void detachInterrupt(int) { attached = 0; } -static const char *TAG = "Somfy"; -static uint8_t rxmode; -static int interruptPin; -uint8_t radioBitLength; -bool testRadioInit = true; -struct { Transceiver transceiver; } somfy; -struct RadioLock { RadioLock(int = 0, bool = false) {} }; -struct TxQueueLock { ~TxQueueLock() {} }; -constexpr int portMAX_DELAY = 0; -static bool scanEmitPending; -static void *txTaskHandle = reinterpret_cast(1); -void xTaskNotifyGive(void *) {} -void Transceiver::handleReceive() {} -void Transceiver::resumeTx(uint8_t) {} -static void warnBootPin(const char *, uint8_t) {} -void transceiver_config_t::save() { - saves++; - if(BoardProfile::integratedRadio) - assert(SCKPin == 11 && MISOPin == 10 && MOSIPin == 9 && CSNPin == 12 && TXPin == 3 && RXPin == 38); -} - -ELECHOUSE_CC1101 ELECHOUSE_cc1101; -static bool detected; -static uint8_t part, version, gdo0, gdo2, packet, modem, rxState; -static uint32_t frequencyWord; -void ELECHOUSE_CC1101::Init() { initializations++; } -bool ELECHOUSE_CC1101::getCC1101() { return detected; } -byte ELECHOUSE_CC1101::SpiReadStatus(byte addr) { - if(addr == CC1101_PARTNUM) return part; - if(addr == CC1101_VERSION) return version; - if(addr == CC1101_MARCSTATE) return rxState; - assert(false); return 0; -} -byte ELECHOUSE_CC1101::SpiReadReg(byte addr) { - if(addr == CC1101_IOCFG0) return gdo0; - if(addr == CC1101_IOCFG2) return gdo2; - if(addr == CC1101_PKTCTRL0) return packet; - if(addr == CC1101_MDMCFG2) return modem; - if(addr == CC1101_FREQ2) return frequencyWord >> 16; - if(addr == CC1101_FREQ1) return frequencyWord >> 8; - if(addr == CC1101_FREQ0) return frequencyWord; - assert(false); return 0; -} -void ELECHOUSE_CC1101::setSpiPin(byte sck, byte miso, byte mosi, byte cs) { - if(BoardProfile::integratedRadio) assert(sck == 11 && miso == 10 && mosi == 9 && cs == 12); -} -void ELECHOUSE_CC1101::SetRx() { - rxStarts++; - observedReadyDuringRx = somfy.transceiver.available(); - somfy_tx_job_t job; - queuedDuringRx = somfy.transceiver.submitTx(job); -} -void ELECHOUSE_CC1101::setSidle() {} -void ELECHOUSE_CC1101::setGDO(byte, byte) {} -void ELECHOUSE_CC1101::setGDO0(byte) {} -#define NOOP(name, type) void ELECHOUSE_CC1101::name(type) {} -NOOP(setCCMode, bool) NOOP(setMHZ, float) NOOP(setRxBW, float) -NOOP(setDeviation, float) NOOP(setPA, int) NOOP(setModulation, byte) -NOOP(setManchester, bool) NOOP(setPktFormat, byte) NOOP(setDcFilterOff, bool) -NOOP(setCrc, bool) NOOP(setCRC_AF, bool) NOOP(setSyncMode, byte) NOOP(setAdrChk, byte) - -#include "radio-setup-methods.inc" - -static void reset() { - somfy.transceiver.~Transceiver(); - new (&somfy.transceiver) Transceiver; - detected = true; part = 0; version = 0x14; - gdo0 = gdo2 = 0x0D; packet = 0x32; modem = 0x3C; - frequencyWord = 0x10AB85; rxState = 0x0D; - attached = rxStarts = initializations = saves = 0; - observedReadyDuringRx = false; queuedDuringRx = 0; - testRadioInit = true; stuckMiso = false; rxmode = 0; clockMs = 0; -} -static void assertUnavailable() { - auto &radio = somfy.transceiver; - assert(!radio.available() && !radio.config.radioInit && attached == 0); - somfy_tx_job_t job; - assert(radio.submitTx(job) == 0); - radio.enableReceive(); - assert(attached == 0); -} -int main() { - reset(); auto &radio = somfy.transceiver; - if(BoardProfile::integratedRadio) { - radio.config.SCKPin = radio.config.TXPin = radio.config.RXPin = 16; - radio.config.apply(); - assert(radio.available() && radio.config.radioInit && attached == 1); - assert(!observedReadyDuringRx && queuedDuringRx == 0); - assert(radio.config.TXPin == 3 && radio.config.RXPin == 38 && radio.config.SCKPin == 11); - somfy_tx_job_t job; assert(radio.submitTx(job) != 0); - stuckMiso = true; radio.config.apply(); assertUnavailable(); - stuckMiso = false; radio.config.apply(); assert(radio.available()); - transceiver_config_t next = radio.config; - next.enabled = false; - next.SCKPin = next.MOSIPin = next.MISOPin = next.CSNPin = next.TXPin = next.RXPin = 16; - radio.setConfig(next); assertUnavailable(); assert(saves == 1); - for(uint8_t pin : {uint8_t(3), uint8_t(12), uint8_t(19), uint8_t(33), uint8_t(48)}) assert(radio.usesPin(pin)); - assert(!radio.usesPin(16)); - reset(); stuckMiso = true; radio.config.apply(); assertUnavailable(); assert(initializations == 0); - reset(); testRadioInit = false; radio.config.apply(); assertUnavailable(); assert(initializations == 0); - reset(); detected = false; radio.config.apply(); assertUnavailable(); assert(rxStarts == 0); - reset(); part = 1; radio.config.apply(); assertUnavailable(); - reset(); version = 0; radio.config.apply(); assertUnavailable(); - reset(); version = 0xFF; radio.config.apply(); assertUnavailable(); assert(rxStarts == 0); - reset(); gdo0 = 0; radio.config.apply(); assertUnavailable(); - reset(); gdo2 = 0; radio.config.apply(); assertUnavailable(); - reset(); packet = 0; radio.config.apply(); assertUnavailable(); - reset(); modem = 0; radio.config.apply(); assertUnavailable(); - reset(); frequencyWord = 0; radio.config.apply(); assertUnavailable(); - reset(); rxState = 0; radio.config.apply(); assertUnavailable(); assert(rxStarts == 1 && millis() <= 25); - rxState = 0x0D; radio.config.apply(); assert(radio.available() && attached == 1); - } else { - // Generic radios retain upstream's enabled-based availability and queue - // admission behavior, including when disabled or detection fails. - radio.config.apply(); - somfy_tx_job_t job; assert(radio.submitTx(job) != 0); - radio.config.enabled = true; detected = false; radio.config.apply(); - assert(radio.available() && !radio.config.radioInit); - radio.config.apply(); // Still no Plus-specific register gating. - detected = true; rxState = 0; radio.config.apply(); - assert(radio.available() && radio.config.radioInit && attached == 1); - assert(observedReadyDuringRx && queuedDuringRx != 0); - } - puts("Radio readiness and RX/queue gating checks passed"); -} diff --git a/tests/board-profile/shade-gpio-test.cpp b/tests/board-profile/shade-gpio-test.cpp deleted file mode 100644 index 2c46ed8..0000000 --- a/tests/board-profile/shade-gpio-test.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include -#include -#include "BoardProfile.h" -#include -#include "SomfyFrame.h" -#include "ShadeSettings.h" - -// Substitute only the shade model/storage boundary. The GPIO methods below -// are compiled from the production ShadeGpio.cpp and observe real board policy. -#define SOMFY_H -enum class gpio_flags_t : byte { LowLevelTrigger = 0x01 }; -struct SomfyRemote { - void triggerGPIOs(somfy_frame_t &frame); -}; -struct SomfyShade : SomfyRemote, ShadeSettings { - radio_proto proto = radio_proto::GP_Relay; - uint8_t gpioFlags = 0; - int8_t gpioDir = 0; - uint8_t gpioUp = 16, gpioDown = 17, gpioMy = 18; - uint32_t gpioRelease = 0; - struct State { int8_t direction = -1, tiltDirection = 0; float currentPos = 0; } position; - const State &state() const { return position; } - bool isToggle() { return isToggleShade(shadeType); } - void initGPIOs(); - void setGPIOs(); - void triggerGPIOs(somfy_frame_t &frame); - bool usesPin(uint8_t pin); -}; -#include "ShadeGpio.cpp" - -TestSerial Serial; -TestESP ESP; -static unsigned int writes = 0; -static unsigned int modes = 0; -static int pinModes[49] = {}; -static int levels[49] = {}; -int gpio_set_level(gpio_num_t pin, uint32_t value) { levels[pin] = value; return 0; } -void pinMode(uint8_t pin, uint8_t mode) { modes++; pinModes[pin] = mode; } -void digitalWrite(uint8_t pin, uint8_t value) { writes++; levels[pin] = value; } -int digitalRead(uint8_t pin) { return levels[pin]; } -unsigned long millis() { return 100; } -void delay(unsigned long) {} -// BoardProfile preflight is linked but this test never starts the hardware. -#include "SPI.h" -TestSPI SPI; -void TestSPI::begin(int8_t, int8_t, int8_t, int8_t) {} -void TestSPI::end() {} - -int main() { - SomfyShade shade; - shade.initGPIOs(); - assert(modes == 2 && pinModes[16] == OUTPUT && pinModes[17] == OUTPUT); - shade.proto = radio_proto::GP_Remote; - shade.initGPIOs(); - assert(modes == 5 && pinModes[18] == OUTPUT); - shade.proto = radio_proto::RTS; - shade.initGPIOs(); - assert(modes == 5); - shade.proto = radio_proto::GP_Relay; - shade.setGPIOs(); - assert(writes == 2 && levels[16] == HIGH && levels[17] == LOW); - somfy_frame_t frame; - frame.cmd = somfy_commands::Down; - frame.repeats = 2; - shade.proto = radio_proto::GP_Remote; - shade.triggerGPIOs(frame); - assert(writes == 5 && levels[17] == HIGH); - const unsigned int before = writes; - // These assignments can arrive from an old backup with the radio disabled. - // Test every reserved pad in each output slot, including USB/flash pads. - for(uint8_t pin = 0; pin < 49; pin++) { - if(!BoardProfile::usesPin(pin)) continue; - for(uint8_t slot = 0; slot < 3; slot++) { - shade.gpioUp = slot == 0 ? pin : 16; - shade.gpioDown = slot == 1 ? pin : 17; - shade.gpioMy = slot == 2 ? pin : 18; - shade.proto = radio_proto::GP_Remote; - const unsigned int beforeModes = modes; - shade.initGPIOs(); - assert(modes == beforeModes); - shade.triggerGPIOs(frame); - shade.setGPIOs(); - if(slot != 2) { - shade.proto = radio_proto::GP_Relay; - shade.initGPIOs(); - assert(modes == beforeModes); - shade.setGPIOs(); - } - assert(writes == before); - } - } - puts("Reserved shade GPIO checks passed"); -} diff --git a/tools/test-board-profile.sh b/tools/test-board-profile.sh deleted file mode 100755 index 748eab0..0000000 --- a/tools/test-board-profile.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail -repo="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" -scratch="$(mktemp -d)" -trap 'rm -rf -- "$scratch"' EXIT -compiler="${CXX:-c++}" -common=(-std=c++11 -Wall -Wextra -Werror -Wno-unused-parameter -I"$repo/tests/board-profile" -I"$repo/src") -sources=("$repo/tests/board-profile/board-profile-test.cpp" "$repo/src/BoardProfile.cpp") -"$compiler" "${common[@]}" -DESPSOMFY_LILYGO_T_EMBED_CC1101 -DCONFIG_IDF_TARGET_ESP32S3 \ - "${sources[@]}" -o "$scratch/lilygo-test" -"$scratch/lilygo-test" -"$compiler" "${common[@]}" "${sources[@]}" -o "$scratch/generic-test" -"$scratch/generic-test" -for profile in generic lilygo; do - flags=() - if [[ "$profile" == lilygo ]]; then - flags=(-DESPSOMFY_LILYGO_T_EMBED_CC1101 -DCONFIG_IDF_TARGET_ESP32S3) - fi - "$compiler" "${common[@]}" "${flags[@]}" \ - "$repo/tests/board-profile/shade-gpio-test.cpp" "$repo/src/BoardProfile.cpp" -o "$scratch/shade-gpio-$profile" - "$scratch/shade-gpio-$profile" -done -if "$compiler" "${common[@]}" -DESPSOMFY_LILYGO_T_EMBED_CC1101 \ - "${sources[@]}" -o "$scratch/wrong-target" > "$scratch/wrong-target.log" 2>&1; then - echo 'LILYGO profile incorrectly accepted a non-S3 target' >&2 - exit 1 -fi -echo 'Non-S3 LILYGO build rejected as expected' - -# Test production setup/RX/queue methods against register and readiness faults. -python3 "$repo/tests/board-profile/extract-radio-setup.py" "$repo/src/Transceiver.cpp" "$scratch/radio-setup-methods.inc" -for profile in generic lilygo; do - flags=() - if [[ "$profile" == lilygo ]]; then - flags=(-DESPSOMFY_LILYGO_T_EMBED_CC1101 -DCONFIG_IDF_TARGET_ESP32S3) - fi - "$compiler" "${common[@]}" -I"$scratch" -I"$repo/lib/SmartRC-CC1101-Driver-Lib" "${flags[@]}" \ - "$repo/tests/board-profile/radio-setup-test.cpp" "$repo/src/BoardProfile.cpp" \ - "$repo/src/RadioScheduler.cpp" -o "$scratch/radio-$profile" - "$scratch/radio-$profile" -done