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

YAML based config for PI / Portduino #2943

Merged
merged 15 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 15 additions & 0 deletions bin/config-dist.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#Waveshare SX126X XXXM

#USE_SX1262: true
#SX126X_DIO2_AS_RF_SWITCH: true
#SX126X_CS: 21
#SX126X_DIO1: 16
#SX126X_BUSY: 20
#SX126X_RESET: 18

#Waveshare SX1302 LISTEN ONLY AT THIS TIME!

#USE_SX1262: true
#SX126X_CS: 7
#SX126X_DIO1: 17
#SX126X_RESET: 22
6 changes: 3 additions & 3 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define REQUIRE_RADIO true // If true, we will fail to start if the radio is not found

/// Convert a preprocessor name into a quoted string
#define xstr(s) str(s)
#define str(s) #s
#define xstr(s) ystr(s)
#define ystr(s) #s

/// Convert a preprocessor name into a quoted string and if that string is empty use "unset"
#define optstr(s) (xstr(s)[0] ? xstr(s) : "unset")
Expand Down Expand Up @@ -209,4 +209,4 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef HW_VENDOR
#error HW_VENDOR must be defined
#endif
#endif
3 changes: 3 additions & 0 deletions src/gps/GPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#if defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(ARCH_ESP32)
HardwareSerial *GPS::_serial_gps = &Serial1;
#elif defined(ARCH_RASPBERRY_PI)
// need a translation layer to make _serial_gps work with pigpio https://abyz.me.uk/rpi/pigpio/cif.html#serOpen
HardwareSerial *GPS::_serial_gps = NULL;
#else
HardwareSerial *GPS::_serial_gps = NULL;
#endif
Expand Down
35 changes: 26 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ NRF52Bluetooth *nrf52Bluetooth;

#ifdef ARCH_RASPBERRY_PI
#include "platform/portduino/PiHal.h"
#include "platform/portduino/PortduinoGlue.h"
#include <fstream>
#include <iostream>
#include <string>
Expand Down Expand Up @@ -690,15 +691,31 @@ void setup()
#endif

#ifdef ARCH_RASPBERRY_PI
PiHal *RadioLibHAL = new PiHal(1);
if (!rIf) {
rIf = new SX1262Interface((LockingArduinoHal *)RadioLibHAL, 21, 16, 18, 20);
if (!rIf->init()) {
LOG_WARN("Failed to find SX1262 radio\n");
delete rIf;
rIf = NULL;
} else {
LOG_INFO("SX1262 Radio init succeeded, using SX1262 radio\n");
if (settingsMap[use_sx1262]) {
if (!rIf) {
PiHal *RadioLibHAL = new PiHal(1);
jp-bennett marked this conversation as resolved.
Show resolved Hide resolved
rIf = new SX1262Interface((LockingArduinoHal *)RadioLibHAL, settingsMap[sx126x_cs], settingsMap[sx126x_dio1],
settingsMap[sx126x_reset], settingsMap[sx126x_busy]);
if (!rIf->init()) {
LOG_WARN("Failed to find SX1262 radio\n");
delete rIf;
rIf = NULL;
} else {
LOG_INFO("SX1262 Radio init succeeded, using SX1262 radio\n");
}
}
} else if (settingsMap[use_rf95]) {
if (!rIf) {
PiHal *RadioLibHAL = new PiHal(1);
jp-bennett marked this conversation as resolved.
Show resolved Hide resolved
rIf = new RF95Interface((LockingArduinoHal *)RadioLibHAL, settingsMap[rf95_nss], settingsMap[rf95_irq],
settingsMap[rf95_reset], settingsMap[rf95_dio1]);
if (!rIf->init()) {
LOG_WARN("Failed to find RF95 radio\n");
delete rIf;
rIf = NULL;
} else {
LOG_INFO("RF95 Radio init succeeded, using RF95 radio\n");
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/mesh/SX126xInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "configuration.h"
#include "error.h"
#include "mesh/NodeDB.h"
#ifdef ARCH_RASPBERRY_PI
#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 SX126x power config forgotten)
Expand Down Expand Up @@ -74,6 +77,12 @@ template <typename T> bool SX126xInterface<T>::init()
#ifdef SX126X_DIO2_AS_RF_SWITCH
LOG_DEBUG("Setting DIO2 as RF switch\n");
bool dio2AsRfSwitch = true;
#elif defined(ARCH_RASPBERRY_PI)
bool dio2AsRfSwitch = false;
if (settingsMap[sx126x_dio2_as_rf_switch]) {
LOG_DEBUG("Setting DIO2 as RF switch\n");
dio2AsRfSwitch = true;
}
#else
LOG_DEBUG("Setting DIO2 as not RF switch\n");
bool dio2AsRfSwitch = false;
Expand Down Expand Up @@ -318,4 +327,4 @@ template <typename T> bool SX126xInterface<T>::sleep()
#endif

return true;
}
}
46 changes: 43 additions & 3 deletions src/platform/portduino/PortduinoGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
#include <assert.h>

#ifdef ARCH_RASPBERRY_PI
#include "PortduinoGlue.h"
#include "pigpio.h"
#include "yaml-cpp/yaml.h"
#include <iostream>
#include <map>

std::map<int, int> settingsMap;

#else
#include <linux/gpio/LinuxGPIOPin.h>
Expand All @@ -27,7 +33,7 @@ void cpuDeepSleep(uint32_t msecs)
}

void updateBatteryLevel(uint8_t level) NOT_IMPLEMENTED("updateBatteryLevel");

#ifndef ARCH_RASPBERRY_PI
/** a simulated pin for busted IRQ hardware
* Porduino helper class to do this i2c based polling:
*/
Expand All @@ -54,7 +60,7 @@ class PolledIrqPin : public GPIOPin
};

static GPIOPin *loraIrq;

#endif
int TCPPort = 4403;

static error_t parse_opt(int key, char *arg, struct argp_state *state)
Expand Down Expand Up @@ -94,6 +100,39 @@ void portduinoSetup()
printf("Setting up Meshtastic on Portduino...\n");

#ifdef ARCH_RASPBERRY_PI
YAML::Node yamlConfig;
try {
yamlConfig = YAML::LoadFile("config.yaml");
jp-bennett marked this conversation as resolved.
Show resolved Hide resolved
} catch (YAML::Exception e) {
std::cout << "*** Exception " << e.what() << std::endl;
}
if (yamlConfig.IsNull()) {
std::cout << "Looking in /etc/meshtastic/" << std::endl;
try {
yamlConfig = YAML::LoadFile("/etc/meshtastic/config.yaml");
} catch (YAML::Exception e) {
std::cout << "*** Exception " << e.what() << std::endl;
exit(EXIT_FAILURE);
}
}

try {
settingsMap[use_sx1262] = yamlConfig["USE_SX1262"].as<bool>(false);
settingsMap[sx126x_dio2_as_rf_switch] = yamlConfig["SX126X_DIO2_AS_RF_SWITCH"].as<bool>(false);
settingsMap[sx126x_cs] = yamlConfig["SX126X_CS"].as<int>(RADIOLIB_NC);
settingsMap[sx126x_dio1] = yamlConfig["SX126X_DIO1"].as<int>(RADIOLIB_NC);
settingsMap[sx126x_busy] = yamlConfig["SX126X_BUSY"].as<int>(RADIOLIB_NC);
settingsMap[sx126x_reset] = yamlConfig["SX126X_RESET"].as<int>(RADIOLIB_NC);
settingsMap[use_rf95] = yamlConfig["USE_RF95"].as<bool>(false);
settingsMap[rf95_nss] = yamlConfig["RF95_NSS"].as<int>(RADIOLIB_NC);
settingsMap[rf95_irq] = yamlConfig["RF95_IRQ"].as<int>(RADIOLIB_NC);
settingsMap[rf95_reset] = yamlConfig["RF95_RESET"].as<int>(RADIOLIB_NC);
settingsMap[rf95_dio1] = yamlConfig["RF95_DIO1"].as<int>(RADIOLIB_NC);

} catch (YAML::Exception e) {
std::cout << "*** Exception " << e.what() << std::endl;
exit(EXIT_FAILURE);
}
return;
#endif

Expand Down Expand Up @@ -121,7 +160,7 @@ void portduinoSetup()
gpioBind(loraCs);
} else
#endif

#ifndef ARCH_RASPBERRY_PI
{
// Set the random seed equal to TCPPort to have a different seed per instance
randomSeed(TCPPort);
Expand All @@ -140,4 +179,5 @@ void portduinoSetup()
}
// gpioBind((new SimGPIOPin(LORA_RESET, "LORA_RESET")));
// gpioBind((new SimGPIOPin(RF95_NSS, "RF95_NSS"))->setSilent());
#endif
}
21 changes: 21 additions & 0 deletions src/platform/portduino/PortduinoGlue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#ifdef ARCH_RASPBERRY_PI
#include <map>

extern std::map<int, int> settingsMap;

enum {
use_sx1262,
sx126x_cs,
sx126x_dio1,
sx126x_busy,
sx126x_reset,
sx126x_dio2_as_rf_switch,
use_rf95,
rf95_nss,
rf95_irq,
rf95_reset,
rf95_dio1
};

#endif
2 changes: 1 addition & 1 deletion variants/portduino/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ build_src_filter = ${portduino_base.build_src_filter}
; The Raspberry Pi actually has accessible SPI and GPIO, so we can support real hardware there.
[env:raspbian]
extends = portduino_base
build_flags = ${portduino_base.build_flags} -O0 -lgpiod -I variants/portduino -DARCH_RASPBERRY_PI -DRADIOLIB_DEBUG -lpigpio
build_flags = ${portduino_base.build_flags} -O0 -lgpiod -I variants/portduino -DARCH_RASPBERRY_PI -DRADIOLIB_DEBUG -lpigpio -lyaml-cpp
board = linux_arm
lib_deps = ${portduino_base.lib_deps}
build_src_filter = ${portduino_base.build_src_filter}
28 changes: 0 additions & 28 deletions variants/portduino/variant.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
#if defined(ARCH_RASPBERRY_PI)
#define HAS_RADIO 1
#define GPIOD_CHIP_LABEL "pinctrl-bcm2711"

// define USE_RF95
#define USE_SX1262
#define SX126X_TXEN 6
#define SX126X_DIO2_AS_RF_SWITCH
#define NO_SCREEN

#define RF95_SCK 11
#define RF95_MISO 9
#define RF95_MOSI 10
#define RF95_NSS RADIOLIB_NC

// #define LORA_DIO0 4 // a No connect on the SX1262 module
// #define LORA_DIO0_LABEL "GPIO_GCLK"
#define LORA_RESET 18
#define LORA_RESET_LABEL "GPIO18"
#define LORA_DIO1 16 // SX1262 IRQ, called DIO0 on pinelora schematic, pin 7 on ch341f "ack" - FIXME, enable hwints in linux
// #define LORA_DIO2 20 // SX1262 BUSY, actually connected to "DIO5" on pinelora schematic, pin 8 on ch341f "slct"
// #define LORA_DIO3 6 // Not connected on PCB, but internally on the TTGO SX1262, if DIO3 is high the TXCO is enabled

#ifdef USE_SX1262
#define SX126X_CS 21
#define SX126X_DIO1 16
#define SX126X_BUSY 20
#define SX126X_RESET LORA_RESET
// HOPE RFM90 does not have a TCXO therefore not SX126X_E22
#endif

#else // Pine64 mode.

// Pine64 uses a common pinout for their SX1262 vs RF95 modules - both can be enabled and we will probe at runtime for RF95 and if
Expand Down
Loading