Skip to content

Commit

Permalink
Trunk n stuff (#5833)
Browse files Browse the repository at this point in the history
* Trunk

* Allow new gpio syntax with defaults

* Exit on pin init failure
  • Loading branch information
jp-bennett authored Jan 12, 2025
1 parent 70296b4 commit 6b1c01c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 98 deletions.
67 changes: 31 additions & 36 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,51 +826,46 @@ void setup()
#endif

#ifdef ARCH_PORTDUINO
const struct { configNames cfgName;
std::string strName;
} loraModules[] = {
{ use_rf95, "RF95" },
{ use_sx1262, "sx1262" },
{ use_sx1268, "sx1268" },
{ use_sx1280, "sx1280" },
{ use_lr1110, "lr1110" },
{ use_lr1120, "lr1120" },
{ use_lr1121, "lr1121" },
{ use_llcc68, "LLCC68" }
};
const struct {
configNames cfgName;
std::string strName;
} loraModules[] = {{use_rf95, "RF95"}, {use_sx1262, "sx1262"}, {use_sx1268, "sx1268"}, {use_sx1280, "sx1280"},
{use_lr1110, "lr1110"}, {use_lr1120, "lr1120"}, {use_lr1121, "lr1121"}, {use_llcc68, "LLCC68"}};
// as one can't use a function pointer to the class constructor:
auto loraModuleInterface = [](configNames cfgName, LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE busy) {
switch (cfgName) {
case use_rf95:
return (RadioInterface*)new RF95Interface(hal, cs, irq, rst, busy);
case use_sx1262:
return (RadioInterface*)new SX1262Interface(hal, cs, irq, rst, busy);
case use_sx1268:
return (RadioInterface*)new SX1268Interface(hal, cs, irq, rst, busy);
case use_sx1280:
return (RadioInterface*)new SX1280Interface(hal, cs, irq, rst, busy);
case use_lr1110:
return (RadioInterface*)new LR1110Interface(hal, cs, irq, rst, busy);
case use_lr1120:
return (RadioInterface*)new LR1120Interface(hal, cs, irq, rst, busy);
case use_lr1121:
return (RadioInterface*)new LR1121Interface(hal, cs, irq, rst, busy);
case use_llcc68:
return (RadioInterface*)new LLCC68Interface(hal, cs, irq, rst, busy);
default:
assert(0); // shouldn't happen
return (RadioInterface*)nullptr;
}
auto loraModuleInterface = [](configNames cfgName, LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq,
RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE busy) {
switch (cfgName) {
case use_rf95:
return (RadioInterface *)new RF95Interface(hal, cs, irq, rst, busy);
case use_sx1262:
return (RadioInterface *)new SX1262Interface(hal, cs, irq, rst, busy);
case use_sx1268:
return (RadioInterface *)new SX1268Interface(hal, cs, irq, rst, busy);
case use_sx1280:
return (RadioInterface *)new SX1280Interface(hal, cs, irq, rst, busy);
case use_lr1110:
return (RadioInterface *)new LR1110Interface(hal, cs, irq, rst, busy);
case use_lr1120:
return (RadioInterface *)new LR1120Interface(hal, cs, irq, rst, busy);
case use_lr1121:
return (RadioInterface *)new LR1121Interface(hal, cs, irq, rst, busy);
case use_llcc68:
return (RadioInterface *)new LLCC68Interface(hal, cs, irq, rst, busy);
default:
assert(0); // shouldn't happen
return (RadioInterface *)nullptr;
}
};
for (auto& loraModule : loraModules) {
for (auto &loraModule : loraModules) {
if (settingsMap[loraModule.cfgName] && !rIf) {
LOG_DEBUG("Activate %s radio on SPI port %s", loraModule.strName.c_str(), settingsStrings[spidev].c_str());
if (settingsStrings[spidev] == "ch341") {
RadioLibHAL = ch341Hal;
} else {
RadioLibHAL = new LockingArduinoHal(SPI, spiSettings);
}
rIf = loraModuleInterface(loraModule.cfgName, (LockingArduinoHal *)RadioLibHAL, settingsMap[cs_pin], settingsMap[irq_pin], settingsMap[reset_pin], settingsMap[busy_pin]);
rIf = loraModuleInterface(loraModule.cfgName, (LockingArduinoHal *)RadioLibHAL, settingsMap[cs_pin],
settingsMap[irq_pin], settingsMap[reset_pin], settingsMap[busy_pin]);
if (!rIf->init()) {
LOG_WARN("No %s radio", loraModule.strName.c_str());
delete rIf;
Expand Down
3 changes: 2 additions & 1 deletion src/mesh/SX126xInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ template <typename T> bool SX126xInterface<T>::init()
// no effect
#if ARCH_PORTDUINO
if (res == RADIOLIB_ERR_NONE) {
LOG_DEBUG("Use MCU pin %i as RXEN and pin %i as TXEN to control RF switching", settingsMap[rxen_pin], settingsMap[txen_pin]);
LOG_DEBUG("Use MCU pin %i as RXEN and pin %i as TXEN to control RF switching", settingsMap[rxen_pin],
settingsMap[txen_pin]);
lora.setRfSwitchPins(settingsMap[rxen_pin], settingsMap[txen_pin]);
}
#else
Expand Down
106 changes: 45 additions & 61 deletions src/platform/portduino/PortduinoGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,10 @@ void portduinoSetup()
{
printf("Set up Meshtastic on Portduino...\n");
int max_GPIO = 0;
const configNames GPIO_lines[] = {cs_pin,
irq_pin,
busy_pin,
reset_pin,
sx126x_ant_sw_pin,
txen_pin,
rxen_pin,
displayDC,
displayCS,
displayBacklight,
displayBacklightPWMChannel,
displayReset,
touchscreenCS,
touchscreenIRQ,
user};
const configNames GPIO_lines[] = {
cs_pin, irq_pin, busy_pin, reset_pin, sx126x_ant_sw_pin, txen_pin,
rxen_pin, displayDC, displayCS, displayBacklight, displayBacklightPWMChannel, displayReset,
touchscreenCS, touchscreenIRQ, user};

std::string gpioChipName = "gpiochip";
settingsStrings[i2cdev] = "";
Expand Down Expand Up @@ -257,7 +246,6 @@ void portduinoSetup()
gpioInit(max_GPIO + 1); // Done here so we can inform Portduino how many GPIOs we need.

// Need to bind all the configured GPIO pins so they're not simulated
// TODO: Can we do this in the for loop above?
// TODO: If one of these fails, we should log and terminate
if (settingsMap.count(user) > 0 && settingsMap[user] != RADIOLIB_NC) {
if (initGPIOPin(settingsMap[user], defaultGpioChipName, settingsMap[user]) != ERRNO_OK) {
Expand All @@ -283,22 +271,25 @@ void portduinoSetup()

// Only initialize the radio pins when dealing with real, kernel controlled SPI hardware
if (settingsStrings[spidev] != "" && settingsStrings[spidev] != "ch341") {
const struct { configNames pin; configNames gpiochip; configNames line; } pinMappings[] = {
{ cs_pin, cs_gpiochip, cs_line },
{ irq_pin, irq_gpiochip, irq_line },
{ busy_pin, busy_gpiochip, busy_line },
{ reset_pin, reset_gpiochip, reset_line },
{ rxen_pin, rxen_gpiochip, rxen_line },
{ txen_pin, txen_gpiochip, txen_line },
{ sx126x_ant_sw_pin, sx126x_ant_sw_gpiochip, sx126x_ant_sw_line }
};
for (auto& pinMap : pinMappings) {
const struct {
configNames pin;
configNames gpiochip;
configNames line;
} pinMappings[] = {{cs_pin, cs_gpiochip, cs_line},
{irq_pin, irq_gpiochip, irq_line},
{busy_pin, busy_gpiochip, busy_line},
{reset_pin, reset_gpiochip, reset_line},
{rxen_pin, rxen_gpiochip, rxen_line},
{txen_pin, txen_gpiochip, txen_line},
{sx126x_ant_sw_pin, sx126x_ant_sw_gpiochip, sx126x_ant_sw_line}};
for (auto &pinMap : pinMappings) {
auto setMapIter = settingsMap.find(pinMap.pin);
if (setMapIter != settingsMap.end() && setMapIter->second != RADIOLIB_NC) {
if (initGPIOPin(setMapIter->second, gpioChipName + std::to_string(settingsMap[pinMap.gpiochip]), settingsMap[pinMap.line] ) != ERRNO_OK) {
settingsMap[pinMap.pin] = RADIOLIB_NC;
settingsMap[pinMap.gpiochip] = RADIOLIB_NC;
settingsMap[pinMap.line] = RADIOLIB_NC;
if (initGPIOPin(setMapIter->second, gpioChipName + std::to_string(settingsMap[pinMap.gpiochip]),
settingsMap[pinMap.line]) != ERRNO_OK) {
printf("Error setting pin number %d. It may not exist, or may already be in use.\n",
settingsMap[pinMap.line]);
exit(EXIT_FAILURE);
}
}
}
Expand Down Expand Up @@ -360,21 +351,16 @@ bool loadConfig(const char *configPath)
}
}
if (yamlConfig["Lora"]) {
const struct { configNames cfgName; std::string strName; } loraModules[] = {
{ use_rf95, "RF95" },
{ use_sx1262, "sx1262" },
{ use_sx1268, "sx1268" },
{ use_sx1280, "sx1280" },
{ use_lr1110, "lr1110" },
{ use_lr1120, "lr1120" },
{ use_lr1121, "lr1121" },
{ use_llcc68, "LLCC68" }
};
for (auto& loraModule : loraModules) {
const struct {
configNames cfgName;
std::string strName;
} loraModules[] = {{use_rf95, "RF95"}, {use_sx1262, "sx1262"}, {use_sx1268, "sx1268"}, {use_sx1280, "sx1280"},
{use_lr1110, "lr1110"}, {use_lr1120, "lr1120"}, {use_lr1121, "lr1121"}, {use_llcc68, "LLCC68"}};
for (auto &loraModule : loraModules) {
settingsMap[loraModule.cfgName] = false;
}
if (yamlConfig["Lora"]["Module"]) {
for (auto& loraModule : loraModules) {
for (auto &loraModule : loraModules) {
if (yamlConfig["Lora"]["Module"].as<std::string>("") == loraModule.strName) {
settingsMap[loraModule.cfgName] = true;
break;
Expand All @@ -391,28 +377,26 @@ bool loadConfig(const char *configPath)
// backwards API compatibility and to globally set gpiochip once
int defaultGpioChip = settingsMap[default_gpiochip] = yamlConfig["Lora"]["gpiochip"].as<int>(0);

const struct { configNames pin;
configNames gpiochip;
configNames line;
std::string strName; } pinMappings[] = {
{ cs_pin, cs_gpiochip, cs_line, "CS" },
{ irq_pin, irq_gpiochip, irq_line, "IRQ" },
{ busy_pin, busy_gpiochip, busy_line, "Busy" },
{ reset_pin, reset_gpiochip, reset_line, "Reset" },
{ txen_pin, txen_gpiochip, txen_line, "TXen" },
{ rxen_pin, rxen_gpiochip, rxen_line, "RXen" },
{ sx126x_ant_sw_pin, sx126x_ant_sw_gpiochip, sx126x_ant_sw_line, "SX126X_ANT_SW" },
const struct {
configNames pin;
configNames gpiochip;
configNames line;
std::string strName;
} pinMappings[] = {
{cs_pin, cs_gpiochip, cs_line, "CS"},
{irq_pin, irq_gpiochip, irq_line, "IRQ"},
{busy_pin, busy_gpiochip, busy_line, "Busy"},
{reset_pin, reset_gpiochip, reset_line, "Reset"},
{txen_pin, txen_gpiochip, txen_line, "TXen"},
{rxen_pin, rxen_gpiochip, rxen_line, "RXen"},
{sx126x_ant_sw_pin, sx126x_ant_sw_gpiochip, sx126x_ant_sw_line, "SX126X_ANT_SW"},
};
for (auto& pinMap : pinMappings) {
if (yamlConfig["Lora"][pinMap.strName].IsMap()
&& (yamlConfig["Lora"][pinMap.strName]["pin"]
|| yamlConfig["Lora"][pinMap.strName]["line"]
|| yamlConfig["Lora"][pinMap.strName]["gpiochip"])) {
for (auto &pinMap : pinMappings) {
if (yamlConfig["Lora"][pinMap.strName].IsMap()) {
settingsMap[pinMap.pin] = yamlConfig["Lora"][pinMap.strName]["pin"].as<int>(RADIOLIB_NC);
settingsMap[pinMap.line] = yamlConfig["Lora"][pinMap.strName]["line"].as<int>(settingsMap[pinMap.pin]);
settingsMap[pinMap.gpiochip] = yamlConfig["Lora"][pinMap.strName]["gpiochip"].as<int>(defaultGpioChip);
}
else { // backwards API compatibility
} else { // backwards API compatibility
settingsMap[pinMap.pin] = yamlConfig["Lora"][pinMap.strName].as<int>(RADIOLIB_NC);
settingsMap[pinMap.line] = settingsMap[pinMap.pin];
settingsMap[pinMap.gpiochip] = defaultGpioChip;
Expand Down Expand Up @@ -584,4 +568,4 @@ bool MAC_from_string(std::string mac_str, uint8_t *dmac)
} else {
return false;
}
}
}

0 comments on commit 6b1c01c

Please sign in to comment.