diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index f1d427ef..3d1ecddd 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -1,6 +1,11 @@ #include // needed for PlatformIO #include -#include + +#if defined(NRF52_PLATFORM) + #include +#elif defined(ESP32) + #include +#endif #define RADIOLIB_STATIC_ONLY 1 #include @@ -54,6 +59,10 @@ #include #include static ESP32Board board; +#elif defined(RAK_4631) + #include + #include + static RAK4631Board board; #else #error "need to provide a 'board' object" #endif @@ -351,7 +360,9 @@ public: } }; -#if defined(P_LORA_SCLK) +#if defined(NRF52_PLATFORM) +RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, SPI); +#elif defined(P_LORA_SCLK) SPIClass spi; RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, spi); #else @@ -379,12 +390,13 @@ void setup() { float tcxo = 1.6f; #endif -#if defined(P_LORA_SCLK) +#if defined(NRF52_PLATFORM) + SPI.setPins(P_LORA_MISO, P_LORA_SCLK, P_LORA_MOSI); + SPI.begin(); +#elif defined(P_LORA_SCLK) spi.begin(P_LORA_SCLK, P_LORA_MISO, P_LORA_MOSI); - int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo); -#else - int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo); #endif + int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo); if (status != RADIOLIB_ERR_NONE) { delay(5000); Serial.print("ERROR: radio init failed: "); @@ -402,8 +414,15 @@ void setup() { radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); #endif +#if defined(NRF52_PLATFORM) + InternalFS.begin(); + IdentityStore store(InternalFS, "/identity"); +#elif defined(ESP32) SPIFFS.begin(true); IdentityStore store(SPIFFS, "/identity"); +#else + #error "need to define filesystem" +#endif if (!store.load("_main", the_mesh.self_id)) { the_mesh.self_id = mesh::LocalIdentity(the_mesh.getRNG()); // create new random identity store.save("_main", the_mesh.self_id); diff --git a/platformio.ini b/platformio.ini index 83817ac7..7636f856 100644 --- a/platformio.ini +++ b/platformio.ini @@ -175,3 +175,40 @@ build_flags = -D ADMIN_PASSWORD="\"password\"" ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 + +; ----------------- NRF52 --------------------- +[nrf52_base] +extends = arduino_base +platform = nordicnrf52 +build_flags = ${arduino_base.build_flags} + -D NRF52_PLATFORM + +[nrf52840_base] +extends = nrf52_base +build_flags = ${nrf52_base.build_flags} +lib_deps = + ${nrf52_base.lib_deps} + rweather/Crypto @ ^0.4.0 + https://github.com/adafruit/Adafruit_nRF52_Arduino + +[rak4631] +extends = nrf52840_base +board = wiscore_rak4631 +board_check = true +build_flags = ${nrf52840_base.build_flags} + -D RAK_4631 + +[env:RAK_4631_Repeater] +extends = rak4631 +build_src_filter = ${rak4631.build_src_filter} +<../examples/simple_repeater/main.cpp> +build_flags = + ${rak4631.build_flags} + -D RADIO_CLASS=CustomSX1262 + -D WRAPPER_CLASS=CustomSX1262Wrapper + -D LORA_TX_POWER=22 + -D ADVERT_NAME="\"RAK4631 Repeater\"" + -D ADVERT_LAT=-37.0 + -D ADVERT_LON=145.0 + -D ADMIN_PASSWORD="\"password\"" +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 diff --git a/src/helpers/IdentityStore.cpp b/src/helpers/IdentityStore.cpp index 7b9c33e8..1a344ad4 100644 --- a/src/helpers/IdentityStore.cpp +++ b/src/helpers/IdentityStore.cpp @@ -17,7 +17,13 @@ bool IdentityStore::load(const char *name, mesh::LocalIdentity& id) { bool IdentityStore::save(const char *name, const mesh::LocalIdentity& id) { char filename[40]; sprintf(filename, "%s/%s.id", _dir, name); + +#if defined(NRF52_PLATFORM) + File file = _fs->open(filename, FILE_O_WRITE); + if (file) { file.seek(0); file.truncate(); } +#else File file = _fs->open(filename, "w", true); +#endif if (file) { id.writeTo(file); file.close(); diff --git a/src/helpers/IdentityStore.h b/src/helpers/IdentityStore.h index dc24238d..098e1014 100644 --- a/src/helpers/IdentityStore.h +++ b/src/helpers/IdentityStore.h @@ -1,13 +1,22 @@ #pragma once -#include +#if defined(ESP32) + #include + #define FILESYSTEM fs::FS +#elif defined(NRF52_PLATFORM) + #include + #define FILESYSTEM Adafruit_LittleFS + + using namespace Adafruit_LittleFS_Namespace; +#endif + #include class IdentityStore { - fs::FS* _fs; + FILESYSTEM* _fs; const char* _dir; public: - IdentityStore(fs::FS& fs, const char* dir): _fs(&fs), _dir(dir) { } + IdentityStore(FILESYSTEM& fs, const char* dir): _fs(&fs), _dir(dir) { } void begin() { _fs->mkdir(_dir); } bool load(const char *name, mesh::LocalIdentity& id); diff --git a/src/helpers/RAK4631Board.h b/src/helpers/RAK4631Board.h new file mode 100644 index 00000000..904fd8ea --- /dev/null +++ b/src/helpers/RAK4631Board.h @@ -0,0 +1,63 @@ +#pragma once + +#include +#include + +#if defined(NRF52_PLATFORM) + +// LoRa radio module pins for RAK4631 +#define P_LORA_DIO_1 47 +#define P_LORA_NSS 42 +#define P_LORA_RESET RADIOLIB_NC // 38 +#define P_LORA_BUSY 46 +#define P_LORA_SCLK 43 +#define P_LORA_MISO 45 +#define P_LORA_MOSI 44 +#define SX126X_POWER_EN 37 + +#define SX126X_DIO2_AS_RF_SWITCH true +#define SX126X_DIO3_TCXO_VOLTAGE 1.8 + +// built-ins +#define PIN_VBAT_READ 1 +#define ADC_MULTIPLIER 6.17 + +class RAK4631Board : public mesh::MainBoard { +protected: + uint8_t startup_reason; + +public: + void begin() { + // for future use, sub-classes SHOULD call this from their begin() + startup_reason = BD_STARTUP_NORMAL; + + pinMode(SX126X_POWER_EN, OUTPUT); + digitalWrite(SX126X_POWER_EN, HIGH); + } + + uint8_t getStartupReason() const override { return startup_reason; } + + #define BATTERY_SAMPLES 10 + + uint16_t getBattMilliVolts() override { + analogReadResolution(12); + + uint32_t raw = 0; + for (int i = 0; i < BATTERY_SAMPLES; i++) { + raw += analogRead(PIN_VBAT_READ); + } + raw = raw / BATTERY_SAMPLES; + + return (ADC_MULTIPLIER * raw) / 4096; + } + + const char* getManufacturerName() const override { + return "RAK 4631"; + } + + void reboot() override { + NVIC_SystemReset(); + } +}; + +#endif