From 95200e8f6b532d4498cd8e7738109d7996db54b3 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Mon, 18 Aug 2025 16:33:52 -0500 Subject: [PATCH] Adds rfswitch on Portduino (#7663) * Initial attempt to get rfswitch working on Portduino * Make portduino_config global --- src/mesh/LR11x0Interface.cpp | 15 ++++----- src/platform/portduino/PortduinoGlue.cpp | 43 ++++++++++++++++++++++++ src/platform/portduino/PortduinoGlue.h | 10 +++++- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/mesh/LR11x0Interface.cpp b/src/mesh/LR11x0Interface.cpp index a20db808e..a0d992c42 100644 --- a/src/mesh/LR11x0Interface.cpp +++ b/src/mesh/LR11x0Interface.cpp @@ -6,6 +6,10 @@ #include "mesh/NodeDB.h" #ifdef LR11X0_DIO_AS_RF_SWITCH #include "rfswitch.h" +#elif ARCH_PORTDUINO +#include "PortduinoGlue.h" +#define rfswitch_dio_pins portduino_config.rfswitch_dio_pins +#define rfswitch_table portduino_config.rfswitch_table #else static const uint32_t rfswitch_dio_pins[] = {RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC}; static const Module::RfSwitchMode_t rfswitch_table[] = { @@ -14,10 +18,6 @@ static const Module::RfSwitchMode_t rfswitch_table[] = { }; #endif -#ifdef ARCH_PORTDUINO -#include "PortduinoGlue.h" -#endif - // Particular boards might define a different max power based on what their hardware can do, default to max power output if not // specified (may be dangerous if using external PA and LR11x0 power config forgotten) #if ARCH_PORTDUINO @@ -117,17 +117,14 @@ template bool LR11x0Interface::init() #ifdef LR11X0_DIO_AS_RF_SWITCH bool dioAsRfSwitch = true; #elif defined(ARCH_PORTDUINO) - bool dioAsRfSwitch = false; - if (settingsMap[dio2_as_rf_switch]) { - dioAsRfSwitch = true; - } + bool dioAsRfSwitch = portduino_config.has_rfswitch_table; #else bool dioAsRfSwitch = false; #endif if (dioAsRfSwitch) { lora.setRfSwitchTable(rfswitch_dio_pins, rfswitch_table); - LOG_DEBUG("Set DIO RF switch", res); + LOG_DEBUG("Set DIO RF switch"); } if (res == RADIOLIB_ERR_NONE) { diff --git a/src/platform/portduino/PortduinoGlue.cpp b/src/platform/portduino/PortduinoGlue.cpp index 5f99ec2c3..ac4e79af1 100644 --- a/src/platform/portduino/PortduinoGlue.cpp +++ b/src/platform/portduino/PortduinoGlue.cpp @@ -29,6 +29,7 @@ std::map settingsMap; std::map settingsStrings; +portduino_config_struct portduino_config; std::ofstream traceFile; Ch341Hal *ch341Hal = nullptr; char *configPath = nullptr; @@ -553,6 +554,48 @@ bool loadConfig(const char *configPath) } } } + if (yamlConfig["Lora"]["rfswitch_table"]) { + portduino_config.has_rfswitch_table = true; + portduino_config.rfswitch_table[0].mode = LR11x0::MODE_STBY; + portduino_config.rfswitch_table[1].mode = LR11x0::MODE_RX; + portduino_config.rfswitch_table[2].mode = LR11x0::MODE_TX; + portduino_config.rfswitch_table[3].mode = LR11x0::MODE_TX_HP; + portduino_config.rfswitch_table[4].mode = LR11x0::MODE_TX_HF; + portduino_config.rfswitch_table[5].mode = LR11x0::MODE_GNSS; + portduino_config.rfswitch_table[6].mode = LR11x0::MODE_WIFI; + portduino_config.rfswitch_table[7] = END_OF_MODE_TABLE; + + for (int i = 0; i < 5; i++) { + + // set up the pin array first + if (yamlConfig["Lora"]["rfswitch_table"]["pins"][i].as("") == "DIO5") + portduino_config.rfswitch_dio_pins[i] = RADIOLIB_LR11X0_DIO5; + if (yamlConfig["Lora"]["rfswitch_table"]["pins"][i].as("") == "DIO6") + portduino_config.rfswitch_dio_pins[i] = RADIOLIB_LR11X0_DIO6; + if (yamlConfig["Lora"]["rfswitch_table"]["pins"][i].as("") == "DIO7") + portduino_config.rfswitch_dio_pins[i] = RADIOLIB_LR11X0_DIO7; + if (yamlConfig["Lora"]["rfswitch_table"]["pins"][i].as("") == "DIO8") + portduino_config.rfswitch_dio_pins[i] = RADIOLIB_LR11X0_DIO8; + if (yamlConfig["Lora"]["rfswitch_table"]["pins"][i].as("") == "DIO10") + portduino_config.rfswitch_dio_pins[i] = RADIOLIB_LR11X0_DIO10; + + // now fill in the table + if (yamlConfig["Lora"]["rfswitch_table"]["MODE_STBY"][i].as("") == "HIGH") + portduino_config.rfswitch_table[0].values[i] = HIGH; + if (yamlConfig["Lora"]["rfswitch_table"]["MODE_RX"][i].as("") == "HIGH") + portduino_config.rfswitch_table[1].values[i] = HIGH; + if (yamlConfig["Lora"]["rfswitch_table"]["MODE_TX"][i].as("") == "HIGH") + portduino_config.rfswitch_table[2].values[i] = HIGH; + if (yamlConfig["Lora"]["rfswitch_table"]["MODE_TX_HP"][i].as("") == "HIGH") + portduino_config.rfswitch_table[3].values[i] = HIGH; + if (yamlConfig["Lora"]["rfswitch_table"]["MODE_TX_HF"][i].as("") == "HIGH") + portduino_config.rfswitch_table[4].values[i] = HIGH; + if (yamlConfig["Lora"]["rfswitch_table"]["MODE_GNSS"][i].as("") == "HIGH") + portduino_config.rfswitch_table[5].values[i] = HIGH; + if (yamlConfig["Lora"]["rfswitch_table"]["MODE_WIFI"][i].as("") == "HIGH") + portduino_config.rfswitch_table[6].values[i] = HIGH; + } + } } if (yamlConfig["GPIO"]) { settingsMap[userButtonPin] = yamlConfig["GPIO"]["User"].as(RADIOLIB_NC); diff --git a/src/platform/portduino/PortduinoGlue.h b/src/platform/portduino/PortduinoGlue.h index 288870eef..64277322a 100644 --- a/src/platform/portduino/PortduinoGlue.h +++ b/src/platform/portduino/PortduinoGlue.h @@ -3,6 +3,8 @@ #include #include +#include "LR11x0Interface.h" +#include "Module.h" #include "platform/portduino/USBHal.h" // Product strings for auto-configuration @@ -126,4 +128,10 @@ bool loadConfig(const char *configPath); static bool ends_with(std::string_view str, std::string_view suffix); void getMacAddr(uint8_t *dmac); bool MAC_from_string(std::string mac_str, uint8_t *dmac); -std::string exec(const char *cmd); \ No newline at end of file +std::string exec(const char *cmd); + +extern struct portduino_config_struct { + bool has_rfswitch_table = false; + uint32_t rfswitch_dio_pins[5] = {RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC}; + Module::RfSwitchMode_t rfswitch_table[8]; +} portduino_config; \ No newline at end of file