Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi gpiochip support for native environment #5743

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 53 additions & 107 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,116 +825,62 @@ void setup()
#endif

#ifdef ARCH_PORTDUINO
if (settingsMap[use_sx1262]) {
if (!rIf) {
LOG_DEBUG("Activate sx1262 radio on SPI port %s", settingsStrings[spidev].c_str());
if (settingsStrings[spidev] == "ch341") {
RadioLibHAL = ch341Hal;
} else {
RadioLibHAL = new LockingArduinoHal(SPI, spiSettings);
}
rIf = new SX1262Interface((LockingArduinoHal *)RadioLibHAL, settingsMap[cs], settingsMap[irq], settingsMap[reset],
settingsMap[busy]);
if (!rIf->init()) {
LOG_WARN("No SX1262 radio");
delete rIf;
exit(EXIT_FAILURE);
} else {
LOG_INFO("SX1262 init success");
}
}
} else if (settingsMap[use_rf95]) {
if (!rIf) {
LOG_DEBUG("Activate rf95 radio on SPI port %s", settingsStrings[spidev].c_str());
RadioLibHAL = new LockingArduinoHal(SPI, spiSettings);
rIf = new RF95Interface((LockingArduinoHal *)RadioLibHAL, settingsMap[cs], settingsMap[irq], settingsMap[reset],
settingsMap[busy]);
if (!rIf->init()) {
LOG_WARN("No RF95 radio");
delete rIf;
rIf = NULL;
exit(EXIT_FAILURE);
} else {
LOG_INFO("RF95 init success");
}
}
} else if (settingsMap[use_sx1280]) {
if (!rIf) {
LOG_DEBUG("Activate sx1280 radio on SPI port %s", settingsStrings[spidev].c_str());
RadioLibHAL = new LockingArduinoHal(SPI, spiSettings);
rIf = new SX1280Interface((LockingArduinoHal *)RadioLibHAL, settingsMap[cs], settingsMap[irq], settingsMap[reset],
settingsMap[busy]);
if (!rIf->init()) {
LOG_WARN("No SX1280 radio");
delete rIf;
rIf = NULL;
exit(EXIT_FAILURE);
} else {
LOG_INFO("SX1280 init success");
}
}
} else if (settingsMap[use_lr1110]) {
if (!rIf) {
LOG_DEBUG("Activate lr1110 radio on SPI port %s", settingsStrings[spidev].c_str());
LockingArduinoHal *RadioLibHAL = new LockingArduinoHal(SPI, spiSettings);
rIf = new LR1110Interface((LockingArduinoHal *)RadioLibHAL, settingsMap[cs], settingsMap[irq], settingsMap[reset],
settingsMap[busy]);
if (!rIf->init()) {
LOG_WARN("No LR1110 radio");
delete rIf;
rIf = NULL;
exit(EXIT_FAILURE);
} else {
LOG_INFO("LR1110 init success");
}
}
} else if (settingsMap[use_lr1120]) {
if (!rIf) {
LOG_DEBUG("Activate lr1120 radio on SPI port %s", settingsStrings[spidev].c_str());
LockingArduinoHal *RadioLibHAL = new LockingArduinoHal(SPI, spiSettings);
rIf = new LR1120Interface((LockingArduinoHal *)RadioLibHAL, settingsMap[cs], settingsMap[irq], settingsMap[reset],
settingsMap[busy]);
if (!rIf->init()) {
LOG_WARN("No LR1120 radio");
delete rIf;
rIf = NULL;
exit(EXIT_FAILURE);
} else {
LOG_INFO("LR1120 init success");
}
}
} else if (settingsMap[use_lr1121]) {
if (!rIf) {
LOG_DEBUG("Activate lr1121 radio on SPI port %s", settingsStrings[spidev].c_str());
LockingArduinoHal *RadioLibHAL = new LockingArduinoHal(SPI, spiSettings);
rIf = new LR1121Interface((LockingArduinoHal *)RadioLibHAL, settingsMap[cs], settingsMap[irq], settingsMap[reset],
settingsMap[busy]);
if (!rIf->init()) {
LOG_WARN("No LR1121 radio");
delete rIf;
rIf = NULL;
exit(EXIT_FAILURE);
} else {
LOG_INFO("LR1121 init success");
}
}
} else if (settingsMap[use_sx1268]) {
if (!rIf) {
LOG_DEBUG("Activate sx1268 radio on SPI port %s", settingsStrings[spidev].c_str());
RadioLibHAL = new LockingArduinoHal(SPI, spiSettings);
rIf = new SX1268Interface((LockingArduinoHal *)RadioLibHAL, settingsMap[cs], settingsMap[irq], settingsMap[reset],
settingsMap[busy]);
if (!rIf->init()) {
LOG_WARN("No SX1268 radio");
delete rIf;
rIf = NULL;
exit(EXIT_FAILURE);
} else {
LOG_INFO("SX1268 init success");
const struct { configNames cfgName;
std::string strName;
bool ch341halsupport;
} loraModules[] = {
{ use_rf95, "RF95", false },
{ use_sx1262, "sx1262", true },
{ use_sx1268, "sx1268", false },
{ use_sx1280, "sx1280", false },
{ use_lr1110, "lr1110", false },
{ use_lr1120, "lr1120", false },
psiegl marked this conversation as resolved.
Show resolved Hide resolved
{ use_lr1121, "lr1121", false }
};
// 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);
default:
assert(0); // shouldn't happen
return (RadioInterface*)nullptr;
}
};
for (auto& loraModule : loraModules) {
if (settingsMap[loraModule.cfgName]) {
if (!rIf) {
LOG_DEBUG("Activate %s radio on SPI port %s", loraModule.strName, settingsStrings[spidev].c_str());
if (loraModule.ch341halsupport
&& 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]);
if (!rIf->init()) {
LOG_WARN("No %s radio", loraModule.strName);
delete rIf;
rIf = NULL;
exit(EXIT_FAILURE);
} else {
LOG_INFO("%s init success", loraModule.strName);
}
}
}
}

#elif defined(HW_SPI1_DEVICE)
LockingArduinoHal *RadioLibHAL = new LockingArduinoHal(SPI1, spiSettings);
#else // HW_SPI1_DEVICE
Expand Down
22 changes: 11 additions & 11 deletions src/mesh/RF95Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ void RF95Interface::setTransmitEnable(bool txon)
#ifdef RF95_TXEN
digitalWrite(RF95_TXEN, txon ? 1 : 0);
#elif ARCH_PORTDUINO
if (settingsMap[txen] != RADIOLIB_NC) {
digitalWrite(settingsMap[txen], txon ? 1 : 0);
if (settingsMap[txen_pin] != RADIOLIB_NC) {
digitalWrite(settingsMap[txen_pin], txon ? 1 : 0);
}
#endif

#ifdef RF95_RXEN
digitalWrite(RF95_RXEN, txon ? 0 : 1);
#elif ARCH_PORTDUINO
if (settingsMap[rxen] != RADIOLIB_NC) {
digitalWrite(settingsMap[rxen], txon ? 0 : 1);
if (settingsMap[rxen_pin] != RADIOLIB_NC) {
digitalWrite(settingsMap[rxen_pin], txon ? 0 : 1);
}
#endif
}
Expand Down Expand Up @@ -164,13 +164,13 @@ bool RF95Interface::init()
digitalWrite(RF95_RXEN, 1);
#endif
#if ARCH_PORTDUINO
if (settingsMap[txen] != RADIOLIB_NC) {
pinMode(settingsMap[txen], OUTPUT);
digitalWrite(settingsMap[txen], 0);
if (settingsMap[txen_pin] != RADIOLIB_NC) {
pinMode(settingsMap[txen_pin], OUTPUT);
digitalWrite(settingsMap[txen_pin], 0);
}
if (settingsMap[rxen] != RADIOLIB_NC) {
pinMode(settingsMap[rxen], OUTPUT);
digitalWrite(settingsMap[rxen], 0);
if (settingsMap[rxen_pin] != RADIOLIB_NC) {
pinMode(settingsMap[rxen_pin], OUTPUT);
digitalWrite(settingsMap[rxen_pin], 0);
}
#endif
setTransmitEnable(false);
Expand Down Expand Up @@ -337,4 +337,4 @@ bool RF95Interface::sleep()

return true;
}
#endif
#endif
12 changes: 6 additions & 6 deletions src/mesh/SX126xInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ template <typename T> bool SX126xInterface<T>::init()

#if ARCH_PORTDUINO
float tcxoVoltage = (float)settingsMap[dio3_tcxo_voltage] / 1000;
if (settingsMap[sx126x_ant_sw] != RADIOLIB_NC) {
digitalWrite(settingsMap[sx126x_ant_sw], HIGH);
pinMode(settingsMap[sx126x_ant_sw], OUTPUT);
if (settingsMap[sx126x_ant_sw_pin] != RADIOLIB_NC) {
digitalWrite(settingsMap[sx126x_ant_sw_pin], HIGH);
pinMode(settingsMap[sx126x_ant_sw_pin], OUTPUT);
}
// FIXME: correct logic to default to not using TCXO if no voltage is specified for SX126X_DIO3_TCXO_VOLTAGE
#elif !defined(SX126X_DIO3_TCXO_VOLTAGE)
Expand Down Expand Up @@ -121,8 +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], settingsMap[txen]);
lora.setRfSwitchPins(settingsMap[rxen], settingsMap[txen]);
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
#ifndef SX126X_RXEN
Expand Down Expand Up @@ -341,4 +341,4 @@ template <typename T> bool SX126xInterface<T>::sleep()

return true;
}
#endif
#endif
42 changes: 21 additions & 21 deletions src/mesh/SX128xInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ template <typename T> bool SX128xInterface<T>::init()
#endif

#if ARCH_PORTDUINO
if (settingsMap[rxen] != RADIOLIB_NC) {
pinMode(settingsMap[rxen], OUTPUT);
digitalWrite(settingsMap[rxen], LOW); // Set low before becoming an output
if (settingsMap[rxen_pin] != RADIOLIB_NC) {
pinMode(settingsMap[rxen_pin], OUTPUT);
digitalWrite(settingsMap[rxen_pin], LOW); // Set low before becoming an output
}
if (settingsMap[txen] != RADIOLIB_NC) {
pinMode(settingsMap[txen], OUTPUT);
digitalWrite(settingsMap[txen], LOW); // Set low before becoming an output
if (settingsMap[txen_pin] != RADIOLIB_NC) {
pinMode(settingsMap[txen_pin], OUTPUT);
digitalWrite(settingsMap[txen_pin], LOW); // Set low before becoming an output
}
#else
#if defined(SX128X_RXEN) && (SX128X_RXEN != RADIOLIB_NC) // set not rx or tx mode
Expand Down Expand Up @@ -93,8 +93,8 @@ template <typename T> bool SX128xInterface<T>::init()
lora.setRfSwitchPins(SX128X_RXEN, SX128X_TXEN);
}
#elif ARCH_PORTDUINO
if (res == RADIOLIB_ERR_NONE && settingsMap[rxen] != RADIOLIB_NC && settingsMap[txen] != RADIOLIB_NC) {
lora.setRfSwitchPins(settingsMap[rxen], settingsMap[txen]);
if (res == RADIOLIB_ERR_NONE && settingsMap[rxen_pin] != RADIOLIB_NC && settingsMap[txen_pin] != RADIOLIB_NC) {
lora.setRfSwitchPins(settingsMap[rxen_pin], settingsMap[txen_pin]);
}
#endif

Expand Down Expand Up @@ -174,11 +174,11 @@ template <typename T> void SX128xInterface<T>::setStandby()
LOG_ERROR("SX128x standby %s%d", radioLibErr, err);
assert(err == RADIOLIB_ERR_NONE);
#if ARCH_PORTDUINO
if (settingsMap[rxen] != RADIOLIB_NC) {
digitalWrite(settingsMap[rxen], LOW);
if (settingsMap[rxen_pin] != RADIOLIB_NC) {
digitalWrite(settingsMap[rxen_pin], LOW);
}
if (settingsMap[txen] != RADIOLIB_NC) {
digitalWrite(settingsMap[txen], LOW);
if (settingsMap[txen_pin] != RADIOLIB_NC) {
digitalWrite(settingsMap[txen_pin], LOW);
}
#else
#if defined(SX128X_RXEN) && (SX128X_RXEN != RADIOLIB_NC) // we have RXEN/TXEN control - turn off RX and TX power
Expand Down Expand Up @@ -210,11 +210,11 @@ template <typename T> void SX128xInterface<T>::addReceiveMetadata(meshtastic_Mes
template <typename T> void SX128xInterface<T>::configHardwareForSend()
{
#if ARCH_PORTDUINO
if (settingsMap[txen] != RADIOLIB_NC) {
digitalWrite(settingsMap[txen], HIGH);
if (settingsMap[txen_pin] != RADIOLIB_NC) {
digitalWrite(settingsMap[txen_pin], HIGH);
}
if (settingsMap[rxen] != RADIOLIB_NC) {
digitalWrite(settingsMap[rxen], LOW);
if (settingsMap[rxen_pin] != RADIOLIB_NC) {
digitalWrite(settingsMap[rxen_pin], LOW);
}

#else
Expand All @@ -241,11 +241,11 @@ template <typename T> void SX128xInterface<T>::startReceive()
setStandby();

#if ARCH_PORTDUINO
if (settingsMap[rxen] != RADIOLIB_NC) {
digitalWrite(settingsMap[rxen], HIGH);
if (settingsMap[rxen_pin] != RADIOLIB_NC) {
digitalWrite(settingsMap[rxen_pin], HIGH);
}
if (settingsMap[txen] != RADIOLIB_NC) {
digitalWrite(settingsMap[txen], LOW);
if (settingsMap[txen_pin] != RADIOLIB_NC) {
digitalWrite(settingsMap[txen_pin], LOW);
}

#else
Expand Down Expand Up @@ -315,4 +315,4 @@ template <typename T> bool SX128xInterface<T>::sleep()

return true;
}
#endif
#endif
Loading
Loading