diff --git a/.github/workflows/build-matrix.yml b/.github/workflows/build-matrix.yml new file mode 100644 index 0000000..4fb60c0 --- /dev/null +++ b/.github/workflows/build-matrix.yml @@ -0,0 +1,100 @@ +name: Build matrix + +# Verifies that the library builds and links on the major Arduino +# architectures. A hardware-independent smoke sketch is generated at build +# time (kept out of the repo so the library spec stays valid) and exercises +# every encode path, built against the repo library (not an installed copy) +# so PRs are validated against their exact content. + +on: + push: + branches: [master, main] + pull_request: + +jobs: + build: + name: ${{ matrix.name }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - name: AVR (Arduino UNO) + fqbn: arduino:avr:uno + core: arduino:avr + - name: ATSAMD (MKR 1000) + fqbn: arduino:samd:mkr1000 + core: arduino:samd + - name: ESP32 (Dev Module) + fqbn: esp32:esp32:esp32 + core: esp32:esp32 + - name: ESP8266 (NodeMCU 1.0) + fqbn: esp8266:esp8266:nodemcuv2 + core: esp8266:esp8266 + - name: Renesas (UNO R4 WiFi) + fqbn: arduino:renesas_uno:unor4wifi + core: arduino:renesas_uno + + steps: + - uses: actions/checkout@v4 + + - name: Set up arduino-cli + uses: arduino/setup-arduino-cli@v2 + with: + version: "1.x" + + - name: Add ESP8266 board manager URL + if: matrix.core == 'esp8266:esp8266' + run: arduino-cli config add board_manager.additional_urls https://arduino.esp8266.com/stable/package_esp8266com_index.json + + - name: Install core ${{ matrix.core }} + run: arduino-cli core install ${{ matrix.core }} + + - name: Generate build smoke sketch + run: | + mkdir -p build-smoke + cat > build-smoke/build-smoke.ino <<'SKELETON' + /* JTEncode build smoke test — no hardware required. + Exercises every encode path so the library is compiled and + linked against whatever core the sketch is built for. */ + #include + + JTEncode enc; + + void setup() + { + Serial.begin(115200); + // JT4 is the largest symbol buffer (207) + uint8_t symbols[JT4_SYMBOL_COUNT]; + + enc.jt65_encode("NT7S", symbols); + enc.jt9_encode("NT7S", symbols); + enc.jt4_encode("NT7S", symbols); + enc.ft8_encode("NT7S", symbols); + enc.wspr_encode("NT7S", "CN84", 10, symbols); + enc.fsq_encode("NT7S", "HELLO", symbols); + enc.fsq_dir_encode("NT7S", "CN84", ' ', "TEST", symbols); + + char grid[7]; + enc.latlon_to_grid(44.5, -122.5, grid); + + Serial.print("JTEncode built OK; symbols[0]="); + Serial.println(symbols[0]); + } + + void loop() {} + SKELETON + + - name: Compile smoke sketch against the repo library + run: | + set -o pipefail + arduino-cli compile --fqbn ${{ matrix.fqbn }} --library "$GITHUB_WORKSPACE" --verbose build-smoke 2>&1 | tee build.log + + - name: Verify the repo library (not an installed copy) is used + run: | + # arduino-cli silently falls back to an installed copy if the library + # is not found — fail loudly instead. The verbose log's compile + # commands must reference the repo's own sources. + grep -q "src/JTEncode.cpp" build.log + grep -q "src/crc14.c" build.log + grep -q "src/nhash.c" build.log diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1766a68 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# WiFi credentials for the WSPR beacon example — never commit these. +arduino_secrets.h diff --git a/README.md b/README.md index 222c49e..a22a3e9 100644 --- a/README.md +++ b/README.md @@ -315,6 +315,23 @@ Also, a big thank you to Murray Greenman, ZL1BPU for working allowing me to pick Changelog --------- +* v1.4.1 + + * Fix FCR macro collision with Renesas RA4M1 device header (R7FA4M1AB.h): + the dead `#define FCR (rs->fcr)` in src/int.h expanded the `FCR` hardware + register name and broke compilation on the Arduino UNO R4 WiFi (and other + Renesas targets). The macro was never used (the struct field is accessed + directly), so it was removed. + * Add a sanitized `WSPR_Beacon_UnoR4WiFi` example: a WSPR beacon for the + Uno R4 WiFi that transmits every 10 minutes on 30 m. Each transmission + picks a random frequency inside the 200 Hz WSPR window (10.1401-10.1403 MHz) + via the hardware TRNG, starts 1 second into the even UTC minute, and syncs + time over WiFi/NTP. Credentials come from a gitignored `arduino_secrets.h` + (never committed). + * Add a build-matrix CI workflow that compiles a hardware-independent smoke + sketch against the repo library on AVR, ATSAMD, ESP32, ESP8266 and Renesas, + verifying the library builds and links on the major Arduino architectures. + * v1.4.0 * Added test harness (17 checks) covering memory-safety and output-correctness bugs diff --git a/examples/WSPR_Beacon_UnoR4WiFi/WSPR_Beacon_UnoR4WiFi.ino b/examples/WSPR_Beacon_UnoR4WiFi/WSPR_Beacon_UnoR4WiFi.ino new file mode 100644 index 0000000..dbfd4a0 --- /dev/null +++ b/examples/WSPR_Beacon_UnoR4WiFi/WSPR_Beacon_UnoR4WiFi.ino @@ -0,0 +1,442 @@ +// ============================================================================ +// WSPR Beacon for Arduino Uno R4 WiFi +// ---------------------------------------------------------------------------- +// Sanitized example: transmits a WSPR packet every 10 minutes on 30 meters, +// using the Uno R4 WiFi's ESP32-S3 module for NTP time sync (accurate timing, +// no GPS needed). Edit the configuration block below with your own callsign, +// grid, and power level, and create "arduino_secrets.h" for WiFi credentials +// (see the note in that block). +// +// Hardware: +// - Arduino Uno R4 WiFi (Renesas RA4M1 + ESP32-S3 WiFi) +// - Si5351A clock generator (Etherkit Si5351A Breakout) wired over I2C +// - A 25 MHz crystal (or 27 MHz) on the Si5351 +// +// Libraries (install via Arduino Library Manager): +// - Etherkit Si5351Arduino (si5351.h) +// - Etherkit JTEncode (JTEncode.h) +// +// Wiring: +// Si5351 -> Uno R4 WiFi +// VIN -> 5V (or 3.3V per your Si5351 breakout) +// GND -> GND +// SDA -> A4 (Qwiic SDA) +// SCL -> A5 (Qwiic SCL) +// The Si5351 output is fed to your transmitter (e.g. via a buffer/driver). +// +// Timing: +// WSPR transmissions start 1 second into an even UTC minute (hh:XX:01), +// so this sketch waits until the top of the next 10-minute slot + 1 s, +// then transmits the 162-symbol packet (~110.6 s). Time comes from an +// NTP query over WiFi, re-synced every 60 s to correct drift. +// +// Credentials: +// Create a file "arduino_secrets.h" (next to this sketch) with: +// #define SECRET_SSID "your-network-ssid" +// #define SECRET_PASS "your-network-password" +// See arduino_secrets.h.example in this directory. The file is git-ignored +// by default; never commit it. +// +// Notes: +// - The 30 m WSPR dial frequency is 10.1387 MHz; the 200 Hz Tx window is +// 10.1401-10.1403 MHz (~1.4 kHz above the dial). Each transmit picks a +// random frequency inside that window (via the Uno R4 WiFi's hardware +// TRNG), per the WSPR spec's "choose a transmission frequency somewhere +// in the 200 Hz-wide standard WSPR bands". +// - WSPR Type 1 messages encode a 4-character grid locator (the first 4 +// characters of a 6-character Maidenhead grid). +// - A 10-minute interval keeps you well inside the ~20% even-minute +// etiquette for a WSPR beacon. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the Software) +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// ============================================================================ + +#include +#include +#include +#include "Wire.h" + +// Credentials — create "arduino_secrets.h" next to this sketch (see the +// arduino_secrets.h.example template in this directory). If the file is +// missing, placeholder credentials are used (the beacon will not connect, +// but the sketch still compiles). +#if __has_include("arduino_secrets.h") + #include "arduino_secrets.h" +#else + #warning "arduino_secrets.h not found; using placeholder credentials (see arduino_secrets.h.example)" + #define SECRET_SSID "your-ssid" + #define SECRET_PASS "your-password" +#endif + +// ---- Configuration ---------------------------------------------------------- +#define CALL_SIGN "N0CALL" // your callsign (Type 1, no slash/brackets) +#define GRID_LOCATOR "AA00" // 4-char grid; reduce your 6-char grid to its first 4 characters +#define POWER_DBM 10 // +10 dBm (valid WSPR levels: -30..+60) +// 30 m WSPR Tx window: 10.140100-10.140300 MHz (200 Hz wide, per the WSPR +// spec — a beacon picks a transmission frequency inside this window). +// Each transmit picks a random frequency in the window. +#define WSPR_WINDOW_MIN_HZ 10140100L +#define WSPR_WINDOW_MAX_HZ 10140300L +#define TONE_SPACING 146 // ~1.46 Hz per symbol (WSPR) +#define SYMBOL_DELAY 683 // ms per symbol (WSPR) +#define TX_INTERVAL 600 // seconds between transmissions (10 minutes) +#define SYMBOL_COUNT 162 // WSPR_SYMBOL_COUNT (from JTEncode) +#define LED_PIN LED_BUILTIN + +// NTP / time sync +#define NTP_PACKET_SIZE 48 +#define NTP_PORT 123 +#define SEVENTY_YEARS 2208988800UL // seconds between 1900-01-01 and 1970-01-01 +#define TIME_REFRESH 60000UL // re-sync NTP every 60 seconds + +// ---- Objects ---------------------------------------------------------------- +Si5351 si5351; +JTEncode jtencode; + +uint8_t tx_buffer[SYMBOL_COUNT]; + +// Last good NTP sync (epoch seconds) and the millis() at that moment +unsigned long last_sync_epoch = 0; +unsigned long last_sync_millis = 0; +bool time_valid = false; +bool si5351_ok = false; + +// WiFiUDP is declared here and begun in setup() +WiFiUDP Udp; + +// Frequency used for this transmit (picked randomly per transmit) +long tx_freq = (WSPR_WINDOW_MIN_HZ + WSPR_WINDOW_MAX_HZ) / 2; + +// Pick a random frequency inside the 30 m WSPR window (10.1401-10.1403 MHz). +// The Uno R4 WiFi's core implements random() with the hardware TRNG +// (SCE_TRNG_SUPPORT), so each call returns a genuinely random value — do NOT +// seed it with randomSeed(), which would disable the TRNG and fall back to a +// weak PRNG. random() can return -1 if the TRNG init fails, so fall back to +// the center of the window in that case. +void pickTxFrequency() +{ + long f = random(WSPR_WINDOW_MIN_HZ, WSPR_WINDOW_MAX_HZ + 1L); + if (f < WSPR_WINDOW_MIN_HZ || f > WSPR_WINDOW_MAX_HZ) + { + f = (WSPR_WINDOW_MIN_HZ + WSPR_WINDOW_MAX_HZ) / 2; + } + tx_freq = f; +} + +// ---- NTP --------------------------------------------------------------------- +// Sends an NTP request to pool.ntp.org and waits up to 5 s for a reply. +// Returns the current epoch seconds, or 0 if the query failed. +unsigned long getNTPTime() +{ + byte packet[NTP_PACKET_SIZE]; + memset(packet, 0, NTP_PACKET_SIZE); + // LI=0, version=3, mode=3 (client) + packet[0] = 0b11100011; + packet[1] = 0; // Stratum / type of clock + packet[2] = 6; // Polling interval + packet[3] = 0xEC; // Peer clock precision + packet[12] = 49; // NTP ID: 'P' + packet[13] = 0x4E; // 'N' + packet[14] = 49; // 'P' + packet[15] = 52; // 'R' + + Udp.beginPacket("pool.ntp.org", NTP_PORT); + Udp.write(packet, NTP_PACKET_SIZE); + Udp.endPacket(); + + // Wait for a reply (bounded, so a network hiccup can't hang the beacon) + unsigned long deadline = millis() + 5000UL; + while (millis() < deadline) + { + if (Udp.parsePacket()) + { + Udp.read(packet, NTP_PACKET_SIZE); + // Timestamp starts at byte 40: highWord << 16 | lowWord + unsigned long highWord = word(packet[40], packet[41]); + unsigned long lowWord = word(packet[42], packet[43]); + unsigned long secsSince1900 = highWord << 16 | lowWord; + unsigned long epoch = secsSince1900 - SEVENTY_YEARS; + if (epoch > 0) + { + return epoch; + } + } + delay(100); + } + return 0; +} + +// Re-sync the local clock from NTP. On failure, leaves the previous +// (still ticking) clock in place. +void syncTime() +{ + unsigned long t = getNTPTime(); + if (t > 0) + { + last_sync_epoch = t; + last_sync_millis = millis(); + time_valid = true; + } + else + { + time_valid = false; + } +} + +// Current time in epoch seconds (derived from the last good NTP sync). +unsigned long now() +{ + if (!time_valid) + { + return 0; + } + return last_sync_epoch + (millis() - last_sync_millis) / 1000UL; +} + +// ---- WiFi ------------------------------------------------------------------- +bool connectWiFi() +{ + Serial.print("Connecting to WiFi: "); + Serial.println(SECRET_SSID); + + if (WiFi.status() == WL_NO_MODULE) + { + Serial.println("WiFi module not detected!"); + return false; + } + + WiFi.begin(SECRET_SSID, SECRET_PASS); + + unsigned long start = millis(); + while (WiFi.status() != WL_CONNECTED && (millis() - start) < 60000UL) + { + delay(500); + } + + if (WiFi.status() == WL_CONNECTED) + { + Serial.print("Connected, IP: "); + Serial.println(WiFi.localIP()); + Serial.print("WiFi signal strength: "); + Serial.print(WiFi.RSSI()); + Serial.println(" dBm"); + return true; + } + + Serial.println("WiFi connection failed"); + return false; +} + +// ---- Time formatting --------------------------------------------------------- +// Prints "HH:MM:SS" (UTC) for an epoch time. +void printTime(unsigned long epoch) +{ + if (epoch == 0) + { + Serial.print("--:--:--"); + return; + } + unsigned long hh = (epoch % 86400UL) / 3600UL; + unsigned long mm = (epoch % 3600UL) / 60UL; + unsigned long ss = epoch % 60UL; + if (hh < 10) Serial.print('0'); + Serial.print(hh); + Serial.print(':'); + if (mm < 10) Serial.print('0'); + Serial.print(mm); + Serial.print(':'); + if (ss < 10) Serial.print('0'); + Serial.print(ss); +} + +// ---- Transmission ----------------------------------------------------------- +void transmit() +{ + pickTxFrequency(); + + Serial.print("Transmitting WSPR at "); + printTime(now()); + Serial.print(" ("); + Serial.print(tx_freq / 1000000.0, 6); + Serial.println(" MHz)"); + + digitalWrite(LED_PIN, HIGH); + si5351.output_enable(SI5351_CLK0, 1); + + for (uint8_t i = 0; i < SYMBOL_COUNT; i++) + { + si5351.set_freq((uint64_t)tx_freq * 100ULL + (uint64_t)tx_buffer[i] * TONE_SPACING, SI5351_CLK0); + delay(SYMBOL_DELAY); + } + + si5351.output_enable(SI5351_CLK0, 0); + digitalWrite(LED_PIN, LOW); + + Serial.print("Done. Packet took "); + Serial.print((SYMBOL_COUNT * SYMBOL_DELAY) / 1000.0, 1); + Serial.println(" s"); +} + +// ---- Setup ------------------------------------------------------------------ +void setup() +{ + Serial.begin(115200); + unsigned long start = millis(); + while (!Serial && (millis() - start) < 5000UL) + { + delay(100); + } + + Serial.println(); + Serial.println("=== WSPR Beacon for Uno R4 WiFi ==="); + Serial.println("Callsign: " CALL_SIGN); + Serial.println("Grid: " GRID_LOCATOR); + Serial.print("Power: +"); + Serial.print(POWER_DBM); + Serial.println(" dBm"); + Serial.print("Window: "); + Serial.print(WSPR_WINDOW_MIN_HZ / 1000000.0, 6); + Serial.print("-"); + Serial.print(WSPR_WINDOW_MAX_HZ / 1000000.0, 6); + Serial.println(" MHz (30 m, random per transmit)"); + Serial.print("Interval: "); + Serial.print(TX_INTERVAL / 60); + Serial.println(" minutes"); + + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, LOW); + + // Initialize the Si5351 (25 MHz reference crystal, no frequency correction) + si5351_ok = si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0, 0); + if (!si5351_ok) + { + Serial.println("WARNING: Si5351 not found on the I2C bus!"); + Serial.println("Check the Si5351 wiring (SDA/SCL) and that the Si5351 is powered."); + } + else + { + si5351.drive_strength(SI5351_CLK0, SI5351_DRIVE_8MA); + si5351.output_enable(SI5351_CLK0, 0); + Serial.println("Si5351 initialized."); + } + + // Encode the WSPR message once (it is fixed for this beacon) + jtencode.wspr_encode(CALL_SIGN, GRID_LOCATOR, POWER_DBM, tx_buffer); + Serial.println("WSPR message encoded."); + + // Begin the UDP socket (once) for the NTP queries + Udp.begin(2390); + + // Connect to WiFi (retried in loop() if it drops) + connectWiFi(); +} + +// ---- Loop ------------------------------------------------------------------- +void loop() +{ + // 0. Ensure the Si5351 is available (retry init every 60 s) + if (!si5351_ok) + { + static unsigned long last_retry = 0; + if (millis() - last_retry >= 60000UL) + { + last_retry = millis(); + si5351_ok = si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0, 0); + if (si5351_ok) + { + si5351.drive_strength(SI5351_CLK0, SI5351_DRIVE_8MA); + si5351.output_enable(SI5351_CLK0, 0); + Serial.println("Si5351 initialized on retry."); + } + } + } + + // 1. Ensure WiFi is connected + if (WiFi.status() != WL_CONNECTED) + { + if (!connectWiFi()) + { + Serial.println("Retrying in 30 s..."); + delay(30000); + return; + } + } + + // 2. Sync the clock (and keep it fresh in the wait loop) + syncTime(); + if (!time_valid) + { + Serial.println("NTP time sync failed; retrying in 30 s..."); + delay(30000); + return; + } + + // 3. Compute the next 10-minute slot and the transmit instant + // (1 second into the even minute, per the WSPR spec) + unsigned long next_slot = (last_sync_epoch / TX_INTERVAL + 1) * TX_INTERVAL; + unsigned long next_transmit = next_slot + 1; + + Serial.print("Next WSPR slot: "); + printTime(next_transmit); + Serial.print(" UTC (in "); + unsigned long wait = next_transmit - now(); + if (wait > 3600) + { + Serial.print(wait / 60); + Serial.print(" min"); + } + else + { + Serial.print(wait); + Serial.print(" s"); + } + Serial.println(")"); + + // 4. Wait for the slot, re-syncing time every TIME_REFRESH ms + for (;;) + { + if (now() >= next_transmit) + { + break; + } + if (millis() - last_sync_millis >= TIME_REFRESH) + { + syncTime(); + } + if (WiFi.status() != WL_CONNECTED) + { + // WiFi dropped: reconnect, re-sync, and recompute the slot + connectWiFi(); + syncTime(); + if (!time_valid) + { + Serial.println("Time sync lost; retrying in 30 s..."); + delay(30000); + return; + } + next_slot = (last_sync_epoch / TX_INTERVAL + 1) * TX_INTERVAL; + next_transmit = next_slot + 1; + continue; + } + delay(100); + } + + // 5. Transmit the packet + transmit(); +} diff --git a/examples/WSPR_Beacon_UnoR4WiFi/arduino_secrets.h.example b/examples/WSPR_Beacon_UnoR4WiFi/arduino_secrets.h.example new file mode 100644 index 0000000..c743d16 --- /dev/null +++ b/examples/WSPR_Beacon_UnoR4WiFi/arduino_secrets.h.example @@ -0,0 +1,7 @@ +// WiFi credentials for the WSPR Beacon example. +// +// Copy this file to "arduino_secrets.h" (next to the sketch) and edit the +// two defines below with your real network credentials. The real file is +// git-ignored and must never be committed. +#define SECRET_SSID "your-network-ssid" +#define SECRET_PASS "your-network-password" diff --git a/library.properties b/library.properties index 8a9e23b..8ead7bc 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Etherkit JTEncode -version=1.4.0 +version=1.4.1 author=Jason Milldrum maintainer=Jason Milldrum sentence=Generate JT65, JT9, JT4, FT8, WSPR, and FSQ symbols on your Arduino. diff --git a/src/int.h b/src/int.h index bb18f3e..7e00148 100644 --- a/src/int.h +++ b/src/int.h @@ -18,7 +18,6 @@ typedef uint8_t data_t; #define INDEX_OF (rs->index_of) #define GENPOLY (rs->genpoly) #define NROOTS (rs->nroots) -#define FCR (rs->fcr) #define PRIM (rs->prim) #define IPRIM (rs->iprim) #define PAD (rs->pad)