From 9f6c11500113ea408fcc759eda92da3120050d7a Mon Sep 17 00:00:00 2001 From: Maze X Date: Fri, 27 Mar 2026 17:33:23 +0100 Subject: [PATCH] Implement SX126X SPI and chip configuration for PoC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - radio.cpp: Add custom SPI bus initialization for boards with non-default pins (conditional on LORA_PIN_SCLK definition). Pass lora_spi object to Module instantiation for SX1262/LR1110. - radio.cpp: Add TCXO voltage to SX1262/LR1110 chip_begin() as 8th parameter (using SX126X_DIO3_TCXO_VOLTAGE macro or 0 if undefined). - radio.cpp: Add post-begin SX126X configuration: setDio2AsRfSwitch(), setCurrentLimit(), setRxBoostedGainMode() (conditional on build macros). - radio.cpp: Fix radio_tx() const conversion for RadioLib transmit() API compatibility. - config.h: Add LORA_TX_POWER → LORA_POWER_DBM alias for boards like Heltec V3 that define power via LORA_TX_POWER. Use #ifndef guard to avoid redefinition warnings. - platformio.ini: Add -Wall -Werror to treat warnings as fatal in our code (excluding SDK/framework). - platformio.ini: Add -std=c99 build flag for KISS C99 support. - src/kiss.c → src/kiss.cpp: Rename to .cpp so Arduino framework compiles it. Verified: Builds cleanly for seeed_xiao_s3_wio_sx1262 and heltec_v3 with no warnings. --- .../seeed/xiao_s3_wio_sx1262/platformio.ini | 2 +- platformio.ini | 8 ++- src/config.h | 6 ++- src/{kiss.c => kiss.cpp} | 0 src/radio.cpp | 54 +++++++++++++++++-- 5 files changed, 62 insertions(+), 8 deletions(-) rename src/{kiss.c => kiss.cpp} (100%) diff --git a/hardware/seeed/xiao_s3_wio_sx1262/platformio.ini b/hardware/seeed/xiao_s3_wio_sx1262/platformio.ini index af05c87..89af097 100644 --- a/hardware/seeed/xiao_s3_wio_sx1262/platformio.ini +++ b/hardware/seeed/xiao_s3_wio_sx1262/platformio.ini @@ -1,7 +1,7 @@ ; Seeed Xiao S3 + Wio SX1262 — ESP32-S3, SX1262 [env:seeed_xiao_s3_wio_sx1262] extends = soc_esp32s3, env:base -board = seeed_xiao_s3 +board = seeed_xiao_esp32s3 build_flags = ${soc_esp32s3.build_flags} ${env:base.build_flags} diff --git a/platformio.ini b/platformio.ini index 3b1e1dd..a5022b6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -4,8 +4,8 @@ [platformio] default_envs = - heltec_v3, - rak_rak4631, + heltec_v3 + rak_rak4631 heltec_v2 extra_configs = @@ -45,6 +45,10 @@ lib_deps = monitor_speed = 115200 build_flags = + ; Treat warnings as errors in our code, but not in third-party + -Wall -Werror -Wno-error=unused-function + ; C99 for KISS implementation + -std=c99 ; Default LoRa radio parameters — override per-board as needed -DLORA_FREQ_KHZ=869525UL -DLORA_BW_HZ=125000UL diff --git a/src/config.h b/src/config.h index 08c3497..1080ba6 100644 --- a/src/config.h +++ b/src/config.h @@ -14,7 +14,11 @@ # define LORA_CR 5 /* denominator: coding rate = 4/CR */ #endif #ifndef LORA_POWER_DBM -# define LORA_POWER_DBM 14 +# ifdef LORA_TX_POWER +# define LORA_POWER_DBM LORA_TX_POWER +# else +# define LORA_POWER_DBM 14 +# endif #endif #ifndef LORA_SYNCWORD # define LORA_SYNCWORD 0x34 /* LoRa public */ diff --git a/src/kiss.c b/src/kiss.cpp similarity index 100% rename from src/kiss.c rename to src/kiss.cpp diff --git a/src/radio.cpp b/src/radio.cpp index 445edf1..3bf6864 100644 --- a/src/radio.cpp +++ b/src/radio.cpp @@ -3,12 +3,27 @@ #include /* ── Chip instantiation ─────────────────────────────────────────────────── */ +/* Custom SPI bus — used when board defines non-default SPI pins */ +#if defined(LORA_PIN_SCLK) +static SPIClass lora_spi(FSPI); +#endif + #if defined(LORA_CHIP_SX1262) +# if defined(LORA_PIN_SCLK) +static SX1262 radio(new Module(LORA_PIN_NSS, LORA_PIN_DIO1, LORA_PIN_RESET, + LORA_PIN_BUSY, lora_spi)); +# else static SX1262 radio(new Module(LORA_PIN_NSS, LORA_PIN_DIO1, LORA_PIN_RESET, LORA_PIN_BUSY)); +# endif #elif defined(LORA_CHIP_LR1110) +# if defined(LORA_PIN_SCLK) +static LR1110 radio(new Module(LORA_PIN_NSS, LORA_PIN_DIO1, LORA_PIN_RESET, + LORA_PIN_BUSY, lora_spi)); +# else static LR1110 radio(new Module(LORA_PIN_NSS, LORA_PIN_DIO1, LORA_PIN_RESET, LORA_PIN_BUSY)); +# endif #elif defined(LORA_CHIP_SX1276) static SX1276 radio(new Module(LORA_PIN_NSS, LORA_PIN_DIO0, LORA_PIN_RESET, RADIOLIB_NC)); @@ -23,13 +38,23 @@ static radio_config_t current_cfg; #if defined(LORA_CHIP_SX1262) static int16_t chip_begin(SX1262 &r, const radio_config_t &cfg) { +#ifdef SX126X_DIO3_TCXO_VOLTAGE + constexpr float tcxo = SX126X_DIO3_TCXO_VOLTAGE; +#else + constexpr float tcxo = 0.0f; +#endif return r.begin(cfg.freq_khz / 1000.0f, cfg.bw_hz / 1000.0f, cfg.sf, - cfg.cr, cfg.syncword, cfg.power_dbm); + cfg.cr, cfg.syncword, cfg.power_dbm, 8, tcxo); } #elif defined(LORA_CHIP_LR1110) static int16_t chip_begin(LR1110 &r, const radio_config_t &cfg) { +#ifdef SX126X_DIO3_TCXO_VOLTAGE + constexpr float tcxo = SX126X_DIO3_TCXO_VOLTAGE; +#else + constexpr float tcxo = 0.0f; +#endif return r.begin(cfg.freq_khz / 1000.0f, cfg.bw_hz / 1000.0f, cfg.sf, - cfg.cr, cfg.syncword, cfg.power_dbm); + cfg.cr, cfg.syncword, cfg.power_dbm, 8, tcxo); } #elif defined(LORA_CHIP_SX1276) static int16_t chip_begin(SX1276 &r, const radio_config_t &cfg) { @@ -41,6 +66,11 @@ static int16_t chip_begin(SX1276 &r, const radio_config_t &cfg) { /* ── C API implementation ────────────────────────────────────────────────── */ int radio_init(void) { + /* Initialize custom SPI bus if board uses non-default SPI pins */ +#if defined(LORA_PIN_SCLK) + lora_spi.begin(LORA_PIN_SCLK, LORA_PIN_MISO, LORA_PIN_MOSI, LORA_PIN_NSS); +#endif + current_cfg = { .freq_khz = LORA_FREQ_KHZ, .bw_hz = LORA_BW_HZ, @@ -50,11 +80,27 @@ int radio_init(void) { .syncword = LORA_SYNCWORD, }; int16_t err = chip_begin(radio, current_cfg); - return (err == RADIOLIB_ERR_NONE) ? 0 : (int)err; + if (err != RADIOLIB_ERR_NONE) + return (int)err; + + /* SX126X post-begin configuration */ +#if defined(LORA_CHIP_SX1262) || defined(LORA_CHIP_LR1110) +# ifdef SX126X_DIO2_AS_RF_SWITCH + radio.setDio2AsRfSwitch(true); +# endif +# ifdef SX126X_CURRENT_LIMIT + radio.setCurrentLimit(SX126X_CURRENT_LIMIT); +# endif +# ifdef SX126X_RX_BOOSTED_GAIN + radio.setRxBoostedGainMode(true); +# endif +#endif + + return 0; } int radio_tx(const uint8_t *data, size_t len) { - int16_t err = radio.transmit(data, len); + int16_t err = radio.transmit((uint8_t *)data, len); if (err == RADIOLIB_ERR_NONE) { radio.startReceive(); }