From e1022791e870faffea0c8cac6d5f2967c07bc36d Mon Sep 17 00:00:00 2001 From: Alex Wolden Date: Tue, 4 Mar 2025 00:07:36 -0800 Subject: [PATCH 1/7] Add support for LilyGo T3 with SX1276 module - Add custom partition table for fitting larger firmware - Create LilyGoT3S3Board.h for board-specific functionality - Update all example files to support the T3 board with SX1276 - Configure build environment for optimizing firmware size - Set up the right pin configuration for SX1276 on LilyGo T3 --- default_4MB.csv | 5 + examples/companion_radio/main.cpp | 46 +++++-- examples/simple_repeater/main.cpp | 46 +++++-- examples/simple_room_server/main.cpp | 46 +++++-- examples/simple_secure_chat/main.cpp | 46 +++++-- platformio.ini | 183 +++++++++++++++++++++++++++ src/helpers/LilyGoT3S3Board.h | 88 +++++++++++++ 7 files changed, 412 insertions(+), 48 deletions(-) create mode 100644 default_4MB.csv create mode 100644 src/helpers/LilyGoT3S3Board.h diff --git a/default_4MB.csv b/default_4MB.csv new file mode 100644 index 000000000..8fb2b0939 --- /dev/null +++ b/default_4MB.csv @@ -0,0 +1,5 @@ +# Name, Type, SubType, Offset, Size, Flags +nvs, data, nvs, 0x9000, 0x5000, +otadata, data, ota, 0xe000, 0x2000, +app0, app, ota_0, 0x10000, 0x300000, +spiffs, data, spiffs, 0x310000,0xF0000, \ No newline at end of file diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index f81b9ffc8..d93276e4c 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -68,10 +68,22 @@ #include #include static XiaoC3Board board; -#elif defined(SEEED_XIAO_S3) || defined(LILYGO_T3S3) +#elif defined(SEEED_XIAO_S3) #include #include static ESP32Board board; +#elif defined(LILYGO_T3) + #include + #include + static ESP32Board board; +#elif defined(LILYGO_T3S3) + #include + #if defined(P_LORA_DIO_0) // If P_LORA_DIO_0 is defined, we're using SX1276 + #include + #else + #include + #endif + static LilyGoT3S3Board board; #elif defined(RAK_4631) #include #include @@ -1108,6 +1120,9 @@ class MyMesh : public BaseChatMesh { #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(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 +SPIClass spi; +RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_0, P_LORA_RESET, P_LORA_DIO_1, 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); @@ -1126,19 +1141,24 @@ void setup() { Serial.begin(115200); board.begin(); -#ifdef SX126X_DIO3_TCXO_VOLTAGE - float tcxo = SX126X_DIO3_TCXO_VOLTAGE; -#else - float tcxo = 1.6f; -#endif - #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); #endif + +#if defined(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 + int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX127X_SYNC_WORD, LORA_TX_POWER); +#else // SX126X module + #ifdef SX126X_DIO3_TCXO_VOLTAGE + float tcxo = SX126X_DIO3_TCXO_VOLTAGE; + #else + float tcxo = 1.6f; + #endif int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo); +#endif + if (status != RADIOLIB_ERR_NONE) { Serial.print("ERROR: radio init failed: "); Serial.println(status); @@ -1147,12 +1167,14 @@ void setup() { radio.setCRC(0); -#ifdef SX126X_CURRENT_LIMIT - radio.setCurrentLimit(SX126X_CURRENT_LIMIT); -#endif +#if !defined(LILYGO_T3) && !defined(HELTEC_LORA_V2) // SX126X specific settings + #ifdef SX126X_CURRENT_LIMIT + radio.setCurrentLimit(SX126X_CURRENT_LIMIT); + #endif -#ifdef SX126X_DIO2_AS_RF_SWITCH - radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); + #ifdef SX126X_DIO2_AS_RF_SWITCH + radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); + #endif #endif fast_rng.begin(radio.random(0x7FFFFFFF)); diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 2c35106c0..1d197749c 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -72,10 +72,22 @@ #include #include static XiaoC3Board board; -#elif defined(SEEED_XIAO_S3) || defined(LILYGO_T3S3) +#elif defined(SEEED_XIAO_S3) #include #include static ESP32Board board; +#elif defined(LILYGO_T3) + #include + #include + static ESP32Board board; +#elif defined(LILYGO_T3S3) + #include + #if defined(P_LORA_DIO_0) // If P_LORA_DIO_0 is defined, we're using SX1276 + #include + #else + #include + #endif + static LilyGoT3S3Board board; #elif defined(RAK_4631) #include #include @@ -604,6 +616,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { #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(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 +SPIClass spi; +RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_0, P_LORA_RESET, P_LORA_DIO_1, 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); @@ -638,19 +653,24 @@ void setup() { #endif rtc_clock.begin(Wire); -#ifdef SX126X_DIO3_TCXO_VOLTAGE - float tcxo = SX126X_DIO3_TCXO_VOLTAGE; -#else - float tcxo = 1.6f; -#endif - #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); #endif + +#if defined(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 + int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX127X_SYNC_WORD, LORA_TX_POWER); +#else // SX126X module + #ifdef SX126X_DIO3_TCXO_VOLTAGE + float tcxo = SX126X_DIO3_TCXO_VOLTAGE; + #else + float tcxo = 1.6f; + #endif int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo); +#endif + if (status != RADIOLIB_ERR_NONE) { delay(5000); Serial.print("ERROR: radio init failed: "); @@ -660,12 +680,14 @@ void setup() { radio.setCRC(0); -#ifdef SX126X_CURRENT_LIMIT - radio.setCurrentLimit(SX126X_CURRENT_LIMIT); -#endif +#if !defined(LILYGO_T3) && !defined(HELTEC_LORA_V2) // SX126X specific settings + #ifdef SX126X_CURRENT_LIMIT + radio.setCurrentLimit(SX126X_CURRENT_LIMIT); + #endif -#ifdef SX126X_DIO2_AS_RF_SWITCH - radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); + #ifdef SX126X_DIO2_AS_RF_SWITCH + radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); + #endif #endif fast_rng.begin(radio.random(0x7FFFFFFF)); diff --git a/examples/simple_room_server/main.cpp b/examples/simple_room_server/main.cpp index 4ef72e167..93870af9e 100644 --- a/examples/simple_room_server/main.cpp +++ b/examples/simple_room_server/main.cpp @@ -76,10 +76,22 @@ #include #include static XiaoC3Board board; -#elif defined(SEEED_XIAO_S3) +#elif defined(SEEED_XIAO_S3) #include #include static ESP32Board board; +#elif defined(LILYGO_T3) + #include + #include + static ESP32Board board; +#elif defined(LILYGO_T3S3) + #include + #if defined(P_LORA_DIO_0) // If P_LORA_DIO_0 is defined, we're using SX1276 + #include + #else + #include + #endif + static LilyGoT3S3Board board; #elif defined(RAK_4631) #include #include @@ -659,6 +671,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { #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(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 +SPIClass spi; +RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_0, P_LORA_RESET, P_LORA_DIO_1, 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); @@ -693,19 +708,24 @@ void setup() { #endif rtc_clock.begin(Wire); -#ifdef SX126X_DIO3_TCXO_VOLTAGE - float tcxo = SX126X_DIO3_TCXO_VOLTAGE; -#else - float tcxo = 1.6f; -#endif - #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); #endif + +#if defined(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 + int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX127X_SYNC_WORD, LORA_TX_POWER); +#else // SX126X module + #ifdef SX126X_DIO3_TCXO_VOLTAGE + float tcxo = SX126X_DIO3_TCXO_VOLTAGE; + #else + float tcxo = 1.6f; + #endif int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo); +#endif + if (status != RADIOLIB_ERR_NONE) { delay(5000); Serial.print("ERROR: radio init failed: "); @@ -715,12 +735,14 @@ void setup() { radio.setCRC(0); -#ifdef SX126X_CURRENT_LIMIT - radio.setCurrentLimit(SX126X_CURRENT_LIMIT); -#endif +#if !defined(LILYGO_T3) && !defined(HELTEC_LORA_V2) // SX126X specific settings + #ifdef SX126X_CURRENT_LIMIT + radio.setCurrentLimit(SX126X_CURRENT_LIMIT); + #endif -#ifdef SX126X_DIO2_AS_RF_SWITCH - radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); + #ifdef SX126X_DIO2_AS_RF_SWITCH + radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); + #endif #endif fast_rng.begin(radio.random(0x7FFFFFFF)); diff --git a/examples/simple_secure_chat/main.cpp b/examples/simple_secure_chat/main.cpp index 9ab8bfa2c..fb7fd9864 100644 --- a/examples/simple_secure_chat/main.cpp +++ b/examples/simple_secure_chat/main.cpp @@ -62,10 +62,22 @@ #include #include static XiaoC3Board board; -#elif defined(SEEED_XIAO_S3) || defined(LILYGO_T3S3) +#elif defined(SEEED_XIAO_S3) #include #include static ESP32Board board; +#elif defined(LILYGO_T3) + #include + #include + static ESP32Board board; +#elif defined(LILYGO_T3S3) + #include + #if defined(P_LORA_DIO_0) // If P_LORA_DIO_0 is defined, we're using SX1276 + #include + #else + #include + #endif + static LilyGoT3S3Board board; #elif defined(RAK_4631) #include #include @@ -541,6 +553,9 @@ class MyMesh : public BaseChatMesh, ContactVisitor { #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(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 +SPIClass spi; +RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_0, P_LORA_RESET, P_LORA_DIO_1, 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); @@ -559,19 +574,24 @@ void setup() { Serial.begin(115200); board.begin(); -#ifdef SX126X_DIO3_TCXO_VOLTAGE - float tcxo = SX126X_DIO3_TCXO_VOLTAGE; -#else - float tcxo = 1.6f; -#endif - #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); #endif + +#if defined(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 + int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX127X_SYNC_WORD, LORA_TX_POWER); +#else // SX126X module + #ifdef SX126X_DIO3_TCXO_VOLTAGE + float tcxo = SX126X_DIO3_TCXO_VOLTAGE; + #else + float tcxo = 1.6f; + #endif int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo); +#endif + if (status != RADIOLIB_ERR_NONE) { Serial.print("ERROR: radio init failed: "); Serial.println(status); @@ -580,12 +600,14 @@ void setup() { radio.setCRC(0); -#ifdef SX126X_CURRENT_LIMIT - radio.setCurrentLimit(SX126X_CURRENT_LIMIT); -#endif +#if !defined(LILYGO_T3) && !defined(HELTEC_LORA_V2) // SX126X specific settings + #ifdef SX126X_CURRENT_LIMIT + radio.setCurrentLimit(SX126X_CURRENT_LIMIT); + #endif -#ifdef SX126X_DIO2_AS_RF_SWITCH - radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); + #ifdef SX126X_DIO2_AS_RF_SWITCH + radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); + #endif #endif fast_rng.begin(radio.random(0x7FFFFFFF)); diff --git a/platformio.ini b/platformio.ini index df6d20937..065261262 100644 --- a/platformio.ini +++ b/platformio.ini @@ -320,6 +320,48 @@ lib_deps = densaugeo/base64 @ ~1.4.0 ; ============= +[LilyGo_T3_sx1276] +extends = esp32_base +board = ttgo-lora32-v1 ; TTGO/LilyGo T3 V1 ESP32 with SX1276 +board_build.partitions = default_4MB.csv ; Use the 4MB partition to allocate more space for firmware +build_unflags = -Os +build_type = release ; Set build type to release +build_flags = + ${esp32_base.build_flags} + -Os -ffunction-sections -fdata-sections ; Optimize for size + -D LILYGO_T3 + -D HELTEC_LORA_V2 ; Needed for CustomSX1276 support + -D P_LORA_DIO_0=26 ; SX1276 DIO0 interrupt pin + -D P_LORA_DIO_1=33 ; SX1276 DIO1 interrupt pin + -D P_LORA_NSS=18 ; Chip select - SS pin + -D P_LORA_RESET=14 ; Reset pin + -D P_LORA_SCLK=5 ; SPI clock + -D P_LORA_MISO=19 ; SPI MISO + -D P_LORA_MOSI=27 ; SPI MOSI + -D P_LORA_TX_LED=2 ; LED pin for TX indication + -D PIN_VBAT_READ=35 ; Battery voltage reading (analog pin) + -D RADIO_CLASS=CustomSX1276 + -D WRAPPER_CLASS=CustomSX1276Wrapper + -D LORA_TX_POWER=20 + +[LilyGo_T3S3_sx1276] +extends = esp32_base +board = t3_s3_v1_x ; ESP32-S3 specific board +build_flags = ${esp32_base.build_flags} + -D LILYGO_T3S3 + -D P_LORA_DIO_1=33 ; DIO1 interrupt pin + -D P_LORA_DIO_0=34 ; DIO0 interrupt pin (replaces BUSY which is for SX126x) + -D P_LORA_NSS=7 ; Chip select + -D P_LORA_RESET=8 ; Reset pin + -D P_LORA_SCLK=5 ; SPI clock + -D P_LORA_MISO=3 ; SPI MISO + -D P_LORA_MOSI=6 ; SPI MOSI + -D P_LORA_TX_LED=37 ; TX LED + -D PIN_VBAT_READ=1 ; Battery voltage reading + -D RADIO_CLASS=CustomSX1276 + -D WRAPPER_CLASS=CustomSX1276Wrapper + -D LORA_TX_POWER=20 + [LilyGo_T3S3_sx1262] extends = esp32_base board = t3_s3_v1_x @@ -398,6 +440,147 @@ lib_deps = ${LilyGo_T3S3_sx1262.lib_deps} densaugeo/base64 @ ~1.4.0 +; === LilyGo T3S3 with SX1276 environments === +[env:LilyGo_T3_sx1276_Repeater] +extends = LilyGo_T3_sx1276 +build_src_filter = ${LilyGo_T3_sx1276.build_src_filter} +<../examples/simple_repeater/main.cpp> +build_flags = + ${LilyGo_T3_sx1276.build_flags} + -D ADVERT_NAME="\"T3-1276 Repeater\"" + -D ADVERT_LAT=-37.0 + -D ADVERT_LON=145.0 + -D ADMIN_PASSWORD="\"password\"" +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 + +[env:LilyGo_T3S3_sx1276_Repeater] +extends = LilyGo_T3S3_sx1276 +build_src_filter = ${LilyGo_T3S3_sx1276.build_src_filter} +<../examples/simple_repeater/main.cpp> +build_flags = + ${LilyGo_T3S3_sx1276.build_flags} + -D ADVERT_NAME="\"T3S3-1276 Repeater\"" + -D ADVERT_LAT=-37.0 + -D ADVERT_LON=145.0 + -D ADMIN_PASSWORD="\"password\"" +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 + +[env:LilyGo_T3_sx1276_terminal_chat] +extends = LilyGo_T3_sx1276 +build_flags = + ${LilyGo_T3_sx1276.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${LilyGo_T3_sx1276.build_src_filter} +<../examples/simple_secure_chat/main.cpp> +lib_deps = + ${LilyGo_T3_sx1276.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:LilyGo_T3S3_sx1276_terminal_chat] +extends = LilyGo_T3S3_sx1276 +build_flags = + ${LilyGo_T3S3_sx1276.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${LilyGo_T3S3_sx1276.build_src_filter} +<../examples/simple_secure_chat/main.cpp> +lib_deps = + ${LilyGo_T3S3_sx1276.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:LilyGo_T3_sx1276_companion_radio_usb] +extends = LilyGo_T3_sx1276 +build_flags = + ${LilyGo_T3_sx1276.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=1 +; -D ENABLE_PRIVATE_KEY_IMPORT=1 +; -D ENABLE_PRIVATE_KEY_EXPORT=1 +; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 +; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 +build_src_filter = ${LilyGo_T3_sx1276.build_src_filter} +<../examples/companion_radio/main.cpp> +lib_deps = + ${LilyGo_T3_sx1276.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:LilyGo_T3S3_sx1276_companion_radio_usb] +extends = LilyGo_T3S3_sx1276 +build_flags = + ${LilyGo_T3S3_sx1276.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=1 +; -D ENABLE_PRIVATE_KEY_IMPORT=1 +; -D ENABLE_PRIVATE_KEY_EXPORT=1 +; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 +; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 +build_src_filter = ${LilyGo_T3S3_sx1276.build_src_filter} +<../examples/companion_radio/main.cpp> +lib_deps = + ${LilyGo_T3S3_sx1276.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:LilyGo_T3_sx1276_companion_radio_ble] +extends = LilyGo_T3_sx1276 +build_flags = + ${LilyGo_T3_sx1276.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=1 + -D BLE_PIN_CODE=123456 + -D BLE_DEBUG_LOGGING=1 +; -D ENABLE_PRIVATE_KEY_IMPORT=1 +; -D ENABLE_PRIVATE_KEY_EXPORT=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${LilyGo_T3_sx1276.build_src_filter} + +<../examples/companion_radio/main.cpp> +lib_deps = + ${LilyGo_T3_sx1276.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:LilyGo_T3_sx1276_room_server] +extends = LilyGo_T3_sx1276 +build_src_filter = ${LilyGo_T3_sx1276.build_src_filter} +<../examples/simple_room_server/main.cpp> +build_flags = + ${LilyGo_T3_sx1276.build_flags} + -D ADVERT_NAME="\"T3-1276 Room\"" + -D ADVERT_LAT=-37.0 + -D ADVERT_LON=145.0 + -D ADMIN_PASSWORD="\"password\"" + -D ROOM_PASSWORD="\"hello\"" +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 + +[env:LilyGo_T3S3_sx1276_companion_radio_ble] +extends = LilyGo_T3S3_sx1276 +build_flags = + ${LilyGo_T3S3_sx1276.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=1 + -D BLE_PIN_CODE=123456 + -D BLE_DEBUG_LOGGING=1 +; -D ENABLE_PRIVATE_KEY_IMPORT=1 +; -D ENABLE_PRIVATE_KEY_EXPORT=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${LilyGo_T3S3_sx1276.build_src_filter} + +<../examples/companion_radio/main.cpp> +lib_deps = + ${LilyGo_T3S3_sx1276.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:LilyGo_T3S3_sx1276_room_server] +extends = LilyGo_T3S3_sx1276 +build_src_filter = ${LilyGo_T3S3_sx1276.build_src_filter} +<../examples/simple_room_server/main.cpp> +build_flags = + ${LilyGo_T3S3_sx1276.build_flags} + -D ADVERT_NAME="\"T3S3-1276 Room\"" + -D ADVERT_LAT=-37.0 + -D ADVERT_LON=145.0 + -D ADMIN_PASSWORD="\"password\"" + -D ROOM_PASSWORD="\"hello\"" +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 + ; ----------------- NRF52 --------------------- [nrf52_base] extends = arduino_base diff --git a/src/helpers/LilyGoT3S3Board.h b/src/helpers/LilyGoT3S3Board.h new file mode 100644 index 000000000..f7287d211 --- /dev/null +++ b/src/helpers/LilyGoT3S3Board.h @@ -0,0 +1,88 @@ +#pragma once + +#include +#include "ESP32Board.h" +#include + +// LoRa radio module pins for LilyGo T3S3 +// These need to be defined here for the SX1276 version +#if defined(RADIO_CLASS) && defined(CustomSX1276) + // SX1276 pin definitions specific to LilyGo T3S3 + #define P_LORA_DIO_0 34 // DIO0 interrupt pin (SX1276 uses DIO0) + #define P_LORA_DIO_1 33 // DIO1 interrupt pin + #define P_LORA_NSS 7 // Chip select + #define P_LORA_RESET 8 // Reset pin + #define P_LORA_SCLK 5 // SPI clock + #define P_LORA_MISO 3 // SPI MISO + #define P_LORA_MOSI 6 // SPI MOSI +#endif + +// Base class for LilyGo T-series boards +class LilyGoTBoard : public ESP32Board { +public: + void begin() { + ESP32Board::begin(); + + esp_reset_reason_t reason = esp_reset_reason(); + if (reason == ESP_RST_DEEPSLEEP) { + long wakeup_source = esp_sleep_get_ext1_wakeup_status(); + if (wakeup_source & (1 << P_LORA_DIO_1)) { // received a LoRa packet (while in deep sleep) + startup_reason = BD_STARTUP_RX_PACKET; + } + + rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS); + rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1); + } + } + + void enterDeepSleep(uint32_t secs, int pin_wake_btn = -1) { + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); + + // Make sure the DIO1 and NSS GPIOs are hold on required levels during deep sleep + rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY); + rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1); + + rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS); + + if (pin_wake_btn < 0) { + esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet + } else { + esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1) | (1L << pin_wake_btn), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet OR wake btn + } + + if (secs > 0) { + esp_sleep_enable_timer_wakeup(secs * 1000000); + } + + // Finally set ESP32 into sleep + esp_deep_sleep_start(); // CPU halts here and never returns! + } + + uint16_t getBattMilliVolts() override { + analogReadResolution(12); + + uint32_t raw = 0; + for (int i = 0; i < 8; i++) { + raw += analogReadMilliVolts(PIN_VBAT_READ); + } + raw = raw / 8; + + return (2 * raw); + } +}; + +// Standard ESP32 version (for T3 board) +class LilyGoT3Board : public LilyGoTBoard { +public: + const char* getManufacturerName() const override { + return "LilyGo T3"; + } +}; + +// For S3 variant +class LilyGoT3S3Board : public LilyGoTBoard { +public: + const char* getManufacturerName() const override { + return "LilyGo T3S3"; + } +}; \ No newline at end of file From b777264eb769ccdd598c3add5e81df461b0968ed Mon Sep 17 00:00:00 2001 From: Alex Wolden Date: Tue, 4 Mar 2025 00:16:35 -0800 Subject: [PATCH 2/7] Remove duplicate entries from platformio.ini - Remove unnecessary T3S3 configurations - Keep only the LilyGo T3 with SX1276 configurations - Clean up the platformio.ini file to remove duplication --- platformio.ini | 167 +------------------------------------------------ 1 file changed, 1 insertion(+), 166 deletions(-) diff --git a/platformio.ini b/platformio.ini index 065261262..42f980bbe 100644 --- a/platformio.ini +++ b/platformio.ini @@ -343,104 +343,9 @@ build_flags = -D RADIO_CLASS=CustomSX1276 -D WRAPPER_CLASS=CustomSX1276Wrapper -D LORA_TX_POWER=20 - -[LilyGo_T3S3_sx1276] -extends = esp32_base -board = t3_s3_v1_x ; ESP32-S3 specific board -build_flags = ${esp32_base.build_flags} - -D LILYGO_T3S3 - -D P_LORA_DIO_1=33 ; DIO1 interrupt pin - -D P_LORA_DIO_0=34 ; DIO0 interrupt pin (replaces BUSY which is for SX126x) - -D P_LORA_NSS=7 ; Chip select - -D P_LORA_RESET=8 ; Reset pin - -D P_LORA_SCLK=5 ; SPI clock - -D P_LORA_MISO=3 ; SPI MISO - -D P_LORA_MOSI=6 ; SPI MOSI - -D P_LORA_TX_LED=37 ; TX LED - -D PIN_VBAT_READ=1 ; Battery voltage reading - -D RADIO_CLASS=CustomSX1276 - -D WRAPPER_CLASS=CustomSX1276Wrapper - -D LORA_TX_POWER=20 - -[LilyGo_T3S3_sx1262] -extends = esp32_base -board = t3_s3_v1_x -build_flags = ${esp32_base.build_flags} - -D LILYGO_T3S3 - -D P_LORA_DIO_1=33 - -D P_LORA_NSS=7 - -D P_LORA_RESET=8 ; RADIOLIB_NC - -D P_LORA_BUSY=34 - -D P_LORA_SCLK=5 - -D P_LORA_MISO=3 - -D P_LORA_MOSI=6 - -D P_LORA_TX_LED=37 - -D PIN_VBAT_READ=1 - -D SX126X_DIO2_AS_RF_SWITCH=true - -D SX126X_DIO3_TCXO_VOLTAGE=1.8 - -D SX126X_CURRENT_LIMIT=130 - -D RADIO_CLASS=CustomSX1262 - -D WRAPPER_CLASS=CustomSX1262Wrapper - -D LORA_TX_POWER=22 - -[env:LilyGo_T3S3_sx1262_Repeater] -extends = LilyGo_T3S3_sx1262 -build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} +<../examples/simple_repeater/main.cpp> -build_flags = - ${LilyGo_T3S3_sx1262.build_flags} - -D ADVERT_NAME="\"T3S3 Repeater\"" - -D ADVERT_LAT=-37.0 - -D ADVERT_LON=145.0 - -D ADMIN_PASSWORD="\"password\"" -; -D MESH_PACKET_LOGGING=1 -; -D MESH_DEBUG=1 - -[env:LilyGo_T3S3_sx1262_terminal_chat] -extends = LilyGo_T3S3_sx1262 -build_flags = - ${LilyGo_T3S3_sx1262.build_flags} - -D MAX_CONTACTS=100 - -D MAX_GROUP_CHANNELS=1 -; -D MESH_PACKET_LOGGING=1 -; -D MESH_DEBUG=1 -build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} +<../examples/simple_secure_chat/main.cpp> -lib_deps = - ${LilyGo_T3S3_sx1262.lib_deps} - densaugeo/base64 @ ~1.4.0 -[env:LilyGo_T3S3_sx1262_companion_radio_usb] -extends = LilyGo_T3S3_sx1262 -build_flags = - ${LilyGo_T3S3_sx1262.build_flags} - -D MAX_CONTACTS=100 - -D MAX_GROUP_CHANNELS=1 -; -D ENABLE_PRIVATE_KEY_IMPORT=1 -; -D ENABLE_PRIVATE_KEY_EXPORT=1 -; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 -; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 -build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} +<../examples/companion_radio/main.cpp> -lib_deps = - ${LilyGo_T3S3_sx1262.lib_deps} - densaugeo/base64 @ ~1.4.0 -[env:LilyGo_T3S3_sx1262_companion_radio_ble] -extends = LilyGo_T3S3_sx1262 -build_flags = - ${LilyGo_T3S3_sx1262.build_flags} - -D MAX_CONTACTS=100 - -D MAX_GROUP_CHANNELS=1 - -D BLE_PIN_CODE=123456 - -D BLE_DEBUG_LOGGING=1 -; -D ENABLE_PRIVATE_KEY_IMPORT=1 -; -D ENABLE_PRIVATE_KEY_EXPORT=1 -; -D MESH_PACKET_LOGGING=1 -; -D MESH_DEBUG=1 -build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} + +<../examples/companion_radio/main.cpp> -lib_deps = - ${LilyGo_T3S3_sx1262.lib_deps} - densaugeo/base64 @ ~1.4.0 - -; === LilyGo T3S3 with SX1276 environments === +; === LilyGo T3 with SX1276 environments === [env:LilyGo_T3_sx1276_Repeater] extends = LilyGo_T3_sx1276 build_src_filter = ${LilyGo_T3_sx1276.build_src_filter} +<../examples/simple_repeater/main.cpp> @@ -453,18 +358,6 @@ build_flags = ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 -[env:LilyGo_T3S3_sx1276_Repeater] -extends = LilyGo_T3S3_sx1276 -build_src_filter = ${LilyGo_T3S3_sx1276.build_src_filter} +<../examples/simple_repeater/main.cpp> -build_flags = - ${LilyGo_T3S3_sx1276.build_flags} - -D ADVERT_NAME="\"T3S3-1276 Repeater\"" - -D ADVERT_LAT=-37.0 - -D ADVERT_LON=145.0 - -D ADMIN_PASSWORD="\"password\"" -; -D MESH_PACKET_LOGGING=1 -; -D MESH_DEBUG=1 - [env:LilyGo_T3_sx1276_terminal_chat] extends = LilyGo_T3_sx1276 build_flags = @@ -478,19 +371,6 @@ lib_deps = ${LilyGo_T3_sx1276.lib_deps} densaugeo/base64 @ ~1.4.0 -[env:LilyGo_T3S3_sx1276_terminal_chat] -extends = LilyGo_T3S3_sx1276 -build_flags = - ${LilyGo_T3S3_sx1276.build_flags} - -D MAX_CONTACTS=100 - -D MAX_GROUP_CHANNELS=1 -; -D MESH_PACKET_LOGGING=1 -; -D MESH_DEBUG=1 -build_src_filter = ${LilyGo_T3S3_sx1276.build_src_filter} +<../examples/simple_secure_chat/main.cpp> -lib_deps = - ${LilyGo_T3S3_sx1276.lib_deps} - densaugeo/base64 @ ~1.4.0 - [env:LilyGo_T3_sx1276_companion_radio_usb] extends = LilyGo_T3_sx1276 build_flags = @@ -506,21 +386,6 @@ lib_deps = ${LilyGo_T3_sx1276.lib_deps} densaugeo/base64 @ ~1.4.0 -[env:LilyGo_T3S3_sx1276_companion_radio_usb] -extends = LilyGo_T3S3_sx1276 -build_flags = - ${LilyGo_T3S3_sx1276.build_flags} - -D MAX_CONTACTS=100 - -D MAX_GROUP_CHANNELS=1 -; -D ENABLE_PRIVATE_KEY_IMPORT=1 -; -D ENABLE_PRIVATE_KEY_EXPORT=1 -; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 -; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 -build_src_filter = ${LilyGo_T3S3_sx1276.build_src_filter} +<../examples/companion_radio/main.cpp> -lib_deps = - ${LilyGo_T3S3_sx1276.lib_deps} - densaugeo/base64 @ ~1.4.0 - [env:LilyGo_T3_sx1276_companion_radio_ble] extends = LilyGo_T3_sx1276 build_flags = @@ -551,36 +416,6 @@ build_flags = ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 -[env:LilyGo_T3S3_sx1276_companion_radio_ble] -extends = LilyGo_T3S3_sx1276 -build_flags = - ${LilyGo_T3S3_sx1276.build_flags} - -D MAX_CONTACTS=100 - -D MAX_GROUP_CHANNELS=1 - -D BLE_PIN_CODE=123456 - -D BLE_DEBUG_LOGGING=1 -; -D ENABLE_PRIVATE_KEY_IMPORT=1 -; -D ENABLE_PRIVATE_KEY_EXPORT=1 -; -D MESH_PACKET_LOGGING=1 -; -D MESH_DEBUG=1 -build_src_filter = ${LilyGo_T3S3_sx1276.build_src_filter} + +<../examples/companion_radio/main.cpp> -lib_deps = - ${LilyGo_T3S3_sx1276.lib_deps} - densaugeo/base64 @ ~1.4.0 - -[env:LilyGo_T3S3_sx1276_room_server] -extends = LilyGo_T3S3_sx1276 -build_src_filter = ${LilyGo_T3S3_sx1276.build_src_filter} +<../examples/simple_room_server/main.cpp> -build_flags = - ${LilyGo_T3S3_sx1276.build_flags} - -D ADVERT_NAME="\"T3S3-1276 Room\"" - -D ADVERT_LAT=-37.0 - -D ADVERT_LON=145.0 - -D ADMIN_PASSWORD="\"password\"" - -D ROOM_PASSWORD="\"hello\"" -; -D MESH_PACKET_LOGGING=1 -; -D MESH_DEBUG=1 - ; ----------------- NRF52 --------------------- [nrf52_base] extends = arduino_base From 1f25575b947a91c74c88d25c5047dde99189940d Mon Sep 17 00:00:00 2001 From: Alex Wolden Date: Tue, 4 Mar 2025 08:58:08 -0800 Subject: [PATCH 3/7] readded 1262 radio --- platformio.ini | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/platformio.ini b/platformio.ini index 42f980bbe..f3fdc6f94 100644 --- a/platformio.ini +++ b/platformio.ini @@ -344,6 +344,28 @@ build_flags = -D WRAPPER_CLASS=CustomSX1276Wrapper -D LORA_TX_POWER=20 +; ============= +[LilyGo_T3S3_sx1262] +extends = esp32_base +board = t3_s3_v1_x +build_flags = ${esp32_base.build_flags} + -D LILYGO_T3S3 + -D P_LORA_DIO_1=33 + -D P_LORA_NSS=7 + -D P_LORA_RESET=8 ; RADIOLIB_NC + -D P_LORA_BUSY=34 + -D P_LORA_SCLK=5 + -D P_LORA_MISO=3 + -D P_LORA_MOSI=6 + -D P_LORA_TX_LED=37 + -D PIN_VBAT_READ=1 + -D SX126X_DIO2_AS_RF_SWITCH=true + -D SX126X_DIO3_TCXO_VOLTAGE=1.8 + -D SX126X_CURRENT_LIMIT=130 + -D RADIO_CLASS=CustomSX1262 + -D WRAPPER_CLASS=CustomSX1262Wrapper + -D LORA_TX_POWER=22 + ; === LilyGo T3 with SX1276 environments === [env:LilyGo_T3_sx1276_Repeater] @@ -416,6 +438,65 @@ build_flags = ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 +; === LilyGo T3S3 with SX1262 environments === +[env:LilyGo_T3S3_sx1262_Repeater] +extends = LilyGo_T3S3_sx1262 +build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} +<../examples/simple_repeater/main.cpp> +build_flags = + ${LilyGo_T3S3_sx1262.build_flags} + -D ADVERT_NAME="\"T3S3-1262 Repeater\"" + -D ADVERT_LAT=-37.0 + -D ADVERT_LON=145.0 + -D ADMIN_PASSWORD="\"password\"" +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 + +[env:LilyGo_T3S3_sx1262_terminal_chat] +extends = LilyGo_T3S3_sx1262 +build_flags = + ${LilyGo_T3S3_sx1262.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} +<../examples/simple_secure_chat/main.cpp> +lib_deps = + ${LilyGo_T3S3_sx1262.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:LilyGo_T3S3_sx1262_companion_radio_usb] +extends = LilyGo_T3S3_sx1262 +build_flags = + ${LilyGo_T3S3_sx1262.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=1 +; -D ENABLE_PRIVATE_KEY_IMPORT=1 +; -D ENABLE_PRIVATE_KEY_EXPORT=1 +; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 +; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 +build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} +<../examples/companion_radio/main.cpp> +lib_deps = + ${LilyGo_T3S3_sx1262.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:LilyGo_T3S3_sx1262_companion_radio_ble] +extends = LilyGo_T3S3_sx1262 +build_flags = + ${LilyGo_T3S3_sx1262.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=1 + -D BLE_PIN_CODE=123456 + -D BLE_DEBUG_LOGGING=1 +; -D ENABLE_PRIVATE_KEY_IMPORT=1 +; -D ENABLE_PRIVATE_KEY_EXPORT=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${LilyGo_T3S3_sx1262.build_src_filter} + +<../examples/companion_radio/main.cpp> +lib_deps = + ${LilyGo_T3S3_sx1262.lib_deps} + densaugeo/base64 @ ~1.4.0 + + ; ----------------- NRF52 --------------------- [nrf52_base] extends = arduino_base From 21ba6116a3cc91c2d202b4774d690604d8e6ff1a Mon Sep 17 00:00:00 2001 From: Alex Wolden Date: Tue, 4 Mar 2025 10:07:49 -0800 Subject: [PATCH 4/7] fix memory --- default_4MB.csv | 5 ----- platformio.ini | 10 ++++++---- 2 files changed, 6 insertions(+), 9 deletions(-) delete mode 100644 default_4MB.csv diff --git a/default_4MB.csv b/default_4MB.csv deleted file mode 100644 index 8fb2b0939..000000000 --- a/default_4MB.csv +++ /dev/null @@ -1,5 +0,0 @@ -# Name, Type, SubType, Offset, Size, Flags -nvs, data, nvs, 0x9000, 0x5000, -otadata, data, ota, 0xe000, 0x2000, -app0, app, ota_0, 0x10000, 0x300000, -spiffs, data, spiffs, 0x310000,0xF0000, \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index f3fdc6f94..ed5118c20 100644 --- a/platformio.ini +++ b/platformio.ini @@ -323,14 +323,13 @@ lib_deps = [LilyGo_T3_sx1276] extends = esp32_base board = ttgo-lora32-v1 ; TTGO/LilyGo T3 V1 ESP32 with SX1276 -board_build.partitions = default_4MB.csv ; Use the 4MB partition to allocate more space for firmware build_unflags = -Os build_type = release ; Set build type to release +board_build.partitions = min_spiffs.csv ; get around 4mb flash limit build_flags = ${esp32_base.build_flags} -Os -ffunction-sections -fdata-sections ; Optimize for size -D LILYGO_T3 - -D HELTEC_LORA_V2 ; Needed for CustomSX1276 support -D P_LORA_DIO_0=26 ; SX1276 DIO0 interrupt pin -D P_LORA_DIO_1=33 ; SX1276 DIO1 interrupt pin -D P_LORA_NSS=18 ; Chip select - SS pin @@ -377,8 +376,11 @@ build_flags = -D ADVERT_LAT=-37.0 -D ADVERT_LON=145.0 -D ADMIN_PASSWORD="\"password\"" -; -D MESH_PACKET_LOGGING=1 -; -D MESH_DEBUG=1 + -D MESH_PACKET_LOGGING=1 + -D MESH_DEBUG=1 + -D CORE_DEBUG_LEVEL=3 + -D SPI_MAX_DMA_LEN=1024 + -D BLE_TASK_SIZE=4096 [env:LilyGo_T3_sx1276_terminal_chat] extends = LilyGo_T3_sx1276 From ed320ac1f5a29e6b4e7d2fd0903b9a13d14aa98e Mon Sep 17 00:00:00 2001 From: Alex Wolden Date: Tue, 4 Mar 2025 18:55:51 -0800 Subject: [PATCH 5/7] add libsodium --- platformio.ini | 11 ++++---- src/Identity.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/platformio.ini b/platformio.ini index ed5118c20..bdbdcab96 100644 --- a/platformio.ini +++ b/platformio.ini @@ -342,6 +342,7 @@ build_flags = -D RADIO_CLASS=CustomSX1276 -D WRAPPER_CLASS=CustomSX1276Wrapper -D LORA_TX_POWER=20 + -D USE_ESP32_ENCRYPTION=true ; ============= [LilyGo_T3S3_sx1262] @@ -376,11 +377,9 @@ build_flags = -D ADVERT_LAT=-37.0 -D ADVERT_LON=145.0 -D ADMIN_PASSWORD="\"password\"" - -D MESH_PACKET_LOGGING=1 - -D MESH_DEBUG=1 - -D CORE_DEBUG_LEVEL=3 - -D SPI_MAX_DMA_LEN=1024 - -D BLE_TASK_SIZE=4096 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +; -D CORE_DEBUG_LEVEL=3 [env:LilyGo_T3_sx1276_terminal_chat] extends = LilyGo_T3_sx1276 @@ -417,7 +416,7 @@ build_flags = -D MAX_CONTACTS=100 -D MAX_GROUP_CHANNELS=1 -D BLE_PIN_CODE=123456 - -D BLE_DEBUG_LOGGING=1 +; -D BLE_DEBUG_LOGGING=1 ; -D ENABLE_PRIVATE_KEY_IMPORT=1 ; -D ENABLE_PRIVATE_KEY_EXPORT=1 ; -D MESH_PACKET_LOGGING=1 diff --git a/src/Identity.cpp b/src/Identity.cpp index e7c57d7ef..aa0358564 100644 --- a/src/Identity.cpp +++ b/src/Identity.cpp @@ -3,6 +3,11 @@ #define ED25519_NO_SEED 1 #include +// For ESP32, we use libsodium for cryptographic operations to reduce stack usage +#ifdef USE_ESP32_ENCRYPTION +#include +#endif + namespace mesh { Identity::Identity() { @@ -14,7 +19,14 @@ Identity::Identity(const char* pub_hex) { } bool Identity::verify(const uint8_t* sig, const uint8_t* message, int msg_len) const { - return ed25519_verify(sig, message, msg_len, pub_key); + #ifdef USE_ESP32_ENCRYPTION + // Using libsodium for verification on ESP32 to reduce stack usage + // This function performs signature verification with much lower stack requirements + // than the default implementation + return crypto_sign_ed25519_verify_detached(sig, message, msg_len, pub_key) == 0; + #else + return ed25519_verify(sig, message, msg_len, pub_key); + #endif } bool Identity::readFrom(Stream& s) { @@ -32,6 +44,7 @@ void Identity::printTo(Stream& s) const { LocalIdentity::LocalIdentity() { memset(prv_key, 0, sizeof(prv_key)); } + LocalIdentity::LocalIdentity(const char* prv_hex, const char* pub_hex) : Identity(pub_hex) { Utils::fromHex(prv_key, PRV_KEY_SIZE, prv_hex); } @@ -39,7 +52,21 @@ LocalIdentity::LocalIdentity(const char* prv_hex, const char* pub_hex) : Identit LocalIdentity::LocalIdentity(RNG* rng) { uint8_t seed[SEED_SIZE]; rng->random(seed, SEED_SIZE); - ed25519_create_keypair(pub_key, prv_key, seed); + + #ifdef USE_ESP32_ENCRYPTION + // Use libsodium for keypair generation on ESP32 to reduce stack usage + // NOTE: Format differences between implementations: + // - The current ed25519 implementation (orlp/ed25519) uses a 64-byte private key format + // - Libsodium also uses a 64-byte format for Ed25519 secret keys, where: + // * First 32 bytes: the actual private key seed + // * Last 32 bytes: the corresponding public key + + // Generate keypair using libsodium with the provided seed + // This avoids the deep stack usage of the default implementation + crypto_sign_ed25519_seed_keypair(pub_key, prv_key, seed); + #else + ed25519_create_keypair(pub_key, prv_key, seed); + #endif } bool LocalIdentity::readFrom(Stream& s) { @@ -77,17 +104,51 @@ void LocalIdentity::readFrom(const uint8_t* src, size_t len) { memcpy(pub_key, &src[PRV_KEY_SIZE], PUB_KEY_SIZE); } else if (len == PRV_KEY_SIZE) { memcpy(prv_key, src, PRV_KEY_SIZE); - // now need to re-calculate the pub_key - ed25519_derive_pub(pub_key, prv_key); + + #ifdef USE_ESP32_ENCRYPTION + // In libsodium, the private key already contains the public key in its last 32 bytes + // We can just extract it directly, avoiding the expensive derivation calculation + // This significantly reduces stack usage on ESP32 + memcpy(pub_key, prv_key + 32, 32); + #else + // now need to re-calculate the pub_key + ed25519_derive_pub(pub_key, prv_key); + #endif } } void LocalIdentity::sign(uint8_t* sig, const uint8_t* message, int msg_len) const { - ed25519_sign(sig, message, msg_len, pub_key, prv_key); + #ifdef USE_ESP32_ENCRYPTION + // Use libsodium for signing on ESP32 to reduce stack usage + // The libsodium implementation uses less stack space than the default ed25519 implementation + crypto_sign_ed25519_detached(sig, NULL, message, msg_len, prv_key); + #else + ed25519_sign(sig, message, msg_len, pub_key, prv_key); + #endif } void LocalIdentity::calcSharedSecret(uint8_t* secret, const uint8_t* other_pub_key) { - ed25519_key_exchange(secret, other_pub_key, prv_key); + #ifdef USE_ESP32_ENCRYPTION + // NOTE: To calculate a shared secret with Ed25519 keys and libsodium, we need to: + // 1. Convert the Ed25519 keys to Curve25519 (X25519) format + // 2. Perform the key exchange using the converted keys + // + // The default implementation handles this conversion internally, + // but with libsodium we need to do these steps explicitly. + // This approach uses less stack space compared to the original implementation. + + unsigned char x25519_pk[crypto_scalarmult_curve25519_BYTES]; + unsigned char x25519_sk[crypto_scalarmult_curve25519_BYTES]; + + // Convert Ed25519 keys to Curve25519 keys for ECDH + crypto_sign_ed25519_pk_to_curve25519(x25519_pk, other_pub_key); + crypto_sign_ed25519_sk_to_curve25519(x25519_sk, prv_key); + + // Calculate shared secret using X25519 + crypto_scalarmult_curve25519(secret, x25519_sk, x25519_pk); + #else + ed25519_key_exchange(secret, other_pub_key, prv_key); + #endif } } \ No newline at end of file From d36da0ed8e7c273b406ca6293859f4883aa23037 Mon Sep 17 00:00:00 2001 From: Alex Wolden Date: Tue, 4 Mar 2025 21:32:17 -0800 Subject: [PATCH 6/7] Refactor naming --- examples/companion_radio/main.cpp | 47 +++++---------- examples/simple_repeater/main.cpp | 46 +++++---------- examples/simple_room_server/main.cpp | 47 +++++---------- examples/simple_secure_chat/main.cpp | 47 +++++---------- platformio.ini | 58 +++++++++--------- src/helpers/LilyGoT3S3Board.h | 88 ---------------------------- src/helpers/LilyGoTLoraBoard.h | 24 ++++++++ 7 files changed, 116 insertions(+), 241 deletions(-) delete mode 100644 src/helpers/LilyGoT3S3Board.h create mode 100644 src/helpers/LilyGoTLoraBoard.h diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index d93276e4c..3c561aba2 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -68,22 +68,14 @@ #include #include static XiaoC3Board board; -#elif defined(SEEED_XIAO_S3) +#elif defined(SEEED_XIAO_S3) || defined(LILYGO_T3S3) #include #include static ESP32Board board; -#elif defined(LILYGO_T3) - #include +#elif defined(LILYGO_TLORA) + #include #include - static ESP32Board board; -#elif defined(LILYGO_T3S3) - #include - #if defined(P_LORA_DIO_0) // If P_LORA_DIO_0 is defined, we're using SX1276 - #include - #else - #include - #endif - static LilyGoT3S3Board board; + static LilyGoTLoraBoard board; #elif defined(RAK_4631) #include #include @@ -1120,7 +1112,7 @@ class MyMesh : public BaseChatMesh { #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(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 +#elif defined(LILYGO_TLORA) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 SPIClass spi; RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_0, P_LORA_RESET, P_LORA_DIO_1, spi); #elif defined(P_LORA_SCLK) @@ -1141,24 +1133,19 @@ void setup() { Serial.begin(115200); board.begin(); +#ifdef SX126X_DIO3_TCXO_VOLTAGE + float tcxo = SX126X_DIO3_TCXO_VOLTAGE; +#else + float tcxo = 1.6f; +#endif + #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); #endif - -#if defined(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 - int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX127X_SYNC_WORD, LORA_TX_POWER); -#else // SX126X module - #ifdef SX126X_DIO3_TCXO_VOLTAGE - float tcxo = SX126X_DIO3_TCXO_VOLTAGE; - #else - float tcxo = 1.6f; - #endif int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo); -#endif - if (status != RADIOLIB_ERR_NONE) { Serial.print("ERROR: radio init failed: "); Serial.println(status); @@ -1167,14 +1154,12 @@ void setup() { radio.setCRC(0); -#if !defined(LILYGO_T3) && !defined(HELTEC_LORA_V2) // SX126X specific settings - #ifdef SX126X_CURRENT_LIMIT - radio.setCurrentLimit(SX126X_CURRENT_LIMIT); - #endif +#ifdef SX126X_CURRENT_LIMIT + radio.setCurrentLimit(SX126X_CURRENT_LIMIT); +#endif - #ifdef SX126X_DIO2_AS_RF_SWITCH - radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); - #endif +#ifdef SX126X_DIO2_AS_RF_SWITCH + radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); #endif fast_rng.begin(radio.random(0x7FFFFFFF)); diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 1d197749c..3eb16d261 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -72,22 +72,14 @@ #include #include static XiaoC3Board board; -#elif defined(SEEED_XIAO_S3) +#elif defined(SEEED_XIAO_S3) || defined(LILYGO_T3S3) #include #include static ESP32Board board; -#elif defined(LILYGO_T3) - #include + #elif defined(LILYGO_TLORA) + #include #include - static ESP32Board board; -#elif defined(LILYGO_T3S3) - #include - #if defined(P_LORA_DIO_0) // If P_LORA_DIO_0 is defined, we're using SX1276 - #include - #else - #include - #endif - static LilyGoT3S3Board board; + static LilyGoTLoraBoard board; #elif defined(RAK_4631) #include #include @@ -616,7 +608,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { #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(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 +#elif defined(LILYGO_TLORA) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 SPIClass spi; RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_0, P_LORA_RESET, P_LORA_DIO_1, spi); #elif defined(P_LORA_SCLK) @@ -653,24 +645,18 @@ void setup() { #endif rtc_clock.begin(Wire); +#ifdef SX126X_DIO3_TCXO_VOLTAGE + float tcxo = SX126X_DIO3_TCXO_VOLTAGE; +#else + float tcxo = 1.6f; +#endif #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); #endif - -#if defined(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 - int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX127X_SYNC_WORD, LORA_TX_POWER); -#else // SX126X module - #ifdef SX126X_DIO3_TCXO_VOLTAGE - float tcxo = SX126X_DIO3_TCXO_VOLTAGE; - #else - float tcxo = 1.6f; - #endif int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo); -#endif - if (status != RADIOLIB_ERR_NONE) { delay(5000); Serial.print("ERROR: radio init failed: "); @@ -680,14 +666,12 @@ void setup() { radio.setCRC(0); -#if !defined(LILYGO_T3) && !defined(HELTEC_LORA_V2) // SX126X specific settings - #ifdef SX126X_CURRENT_LIMIT - radio.setCurrentLimit(SX126X_CURRENT_LIMIT); - #endif +#ifdef SX126X_CURRENT_LIMIT + radio.setCurrentLimit(SX126X_CURRENT_LIMIT); +#endif - #ifdef SX126X_DIO2_AS_RF_SWITCH - radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); - #endif +#ifdef SX126X_DIO2_AS_RF_SWITCH + radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); #endif fast_rng.begin(radio.random(0x7FFFFFFF)); diff --git a/examples/simple_room_server/main.cpp b/examples/simple_room_server/main.cpp index 93870af9e..84f1f5524 100644 --- a/examples/simple_room_server/main.cpp +++ b/examples/simple_room_server/main.cpp @@ -76,22 +76,14 @@ #include #include static XiaoC3Board board; -#elif defined(SEEED_XIAO_S3) +#elif defined(SEEED_XIAO_S3) || defined(LILYGO_T3S3) #include #include static ESP32Board board; -#elif defined(LILYGO_T3) - #include +#elif defined(LILYGO_TLORA) + #include #include - static ESP32Board board; -#elif defined(LILYGO_T3S3) - #include - #if defined(P_LORA_DIO_0) // If P_LORA_DIO_0 is defined, we're using SX1276 - #include - #else - #include - #endif - static LilyGoT3S3Board board; + static LilyGoTLoraBoard board; #elif defined(RAK_4631) #include #include @@ -671,7 +663,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { #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(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 +#elif defined(LILYGO_TLORA) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 SPIClass spi; RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_0, P_LORA_RESET, P_LORA_DIO_1, spi); #elif defined(P_LORA_SCLK) @@ -708,24 +700,19 @@ void setup() { #endif rtc_clock.begin(Wire); +#ifdef SX126X_DIO3_TCXO_VOLTAGE + float tcxo = SX126X_DIO3_TCXO_VOLTAGE; +#else + float tcxo = 1.6f; +#endif + #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); #endif - -#if defined(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 - int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX127X_SYNC_WORD, LORA_TX_POWER); -#else // SX126X module - #ifdef SX126X_DIO3_TCXO_VOLTAGE - float tcxo = SX126X_DIO3_TCXO_VOLTAGE; - #else - float tcxo = 1.6f; - #endif int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo); -#endif - if (status != RADIOLIB_ERR_NONE) { delay(5000); Serial.print("ERROR: radio init failed: "); @@ -735,14 +722,12 @@ void setup() { radio.setCRC(0); -#if !defined(LILYGO_T3) && !defined(HELTEC_LORA_V2) // SX126X specific settings - #ifdef SX126X_CURRENT_LIMIT - radio.setCurrentLimit(SX126X_CURRENT_LIMIT); - #endif +#ifdef SX126X_CURRENT_LIMIT + radio.setCurrentLimit(SX126X_CURRENT_LIMIT); +#endif - #ifdef SX126X_DIO2_AS_RF_SWITCH - radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); - #endif +#ifdef SX126X_DIO2_AS_RF_SWITCH + radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); #endif fast_rng.begin(radio.random(0x7FFFFFFF)); diff --git a/examples/simple_secure_chat/main.cpp b/examples/simple_secure_chat/main.cpp index fb7fd9864..3e280047f 100644 --- a/examples/simple_secure_chat/main.cpp +++ b/examples/simple_secure_chat/main.cpp @@ -62,22 +62,14 @@ #include #include static XiaoC3Board board; -#elif defined(SEEED_XIAO_S3) +#elif defined(SEEED_XIAO_S3) || defined(LILYGO_T3S3) #include #include static ESP32Board board; -#elif defined(LILYGO_T3) - #include +#elif defined(LILYGO_TLORA) + #include #include - static ESP32Board board; -#elif defined(LILYGO_T3S3) - #include - #if defined(P_LORA_DIO_0) // If P_LORA_DIO_0 is defined, we're using SX1276 - #include - #else - #include - #endif - static LilyGoT3S3Board board; + static LilyGoTLoraBoard board; #elif defined(RAK_4631) #include #include @@ -553,7 +545,7 @@ class MyMesh : public BaseChatMesh, ContactVisitor { #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(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 +#elif defined(LILYGO_TLORA) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 SPIClass spi; RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_0, P_LORA_RESET, P_LORA_DIO_1, spi); #elif defined(P_LORA_SCLK) @@ -574,24 +566,19 @@ void setup() { Serial.begin(115200); board.begin(); +#ifdef SX126X_DIO3_TCXO_VOLTAGE + float tcxo = SX126X_DIO3_TCXO_VOLTAGE; +#else + float tcxo = 1.6f; +#endif + #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); #endif - -#if defined(LILYGO_T3) || defined(HELTEC_LORA_V2) // ESP32 with SX1276 - int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX127X_SYNC_WORD, LORA_TX_POWER); -#else // SX126X module - #ifdef SX126X_DIO3_TCXO_VOLTAGE - float tcxo = SX126X_DIO3_TCXO_VOLTAGE; - #else - float tcxo = 1.6f; - #endif int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo); -#endif - if (status != RADIOLIB_ERR_NONE) { Serial.print("ERROR: radio init failed: "); Serial.println(status); @@ -600,14 +587,12 @@ void setup() { radio.setCRC(0); -#if !defined(LILYGO_T3) && !defined(HELTEC_LORA_V2) // SX126X specific settings - #ifdef SX126X_CURRENT_LIMIT - radio.setCurrentLimit(SX126X_CURRENT_LIMIT); - #endif +#ifdef SX126X_CURRENT_LIMIT + radio.setCurrentLimit(SX126X_CURRENT_LIMIT); +#endif - #ifdef SX126X_DIO2_AS_RF_SWITCH - radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); - #endif +#ifdef SX126X_DIO2_AS_RF_SWITCH + radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH); #endif fast_rng.begin(radio.random(0x7FFFFFFF)); diff --git a/platformio.ini b/platformio.ini index bdbdcab96..d5b6bce43 100644 --- a/platformio.ini +++ b/platformio.ini @@ -320,16 +320,16 @@ lib_deps = densaugeo/base64 @ ~1.4.0 ; ============= -[LilyGo_T3_sx1276] +[LilyGo_TLora_V2_1_1_6] extends = esp32_base -board = ttgo-lora32-v1 ; TTGO/LilyGo T3 V1 ESP32 with SX1276 +board = ttgo-lora32-v1 ; LILYGO T-LoRa V2.1-1.6 ESP32 with SX1276 build_unflags = -Os build_type = release ; Set build type to release board_build.partitions = min_spiffs.csv ; get around 4mb flash limit build_flags = ${esp32_base.build_flags} -Os -ffunction-sections -fdata-sections ; Optimize for size - -D LILYGO_T3 + -D LILYGO_TLORA ; LILYGO T-LoRa V2.1-1.6 ESP32 with SX1276 -D P_LORA_DIO_0=26 ; SX1276 DIO0 interrupt pin -D P_LORA_DIO_1=33 ; SX1276 DIO1 interrupt pin -D P_LORA_NSS=18 ; Chip select - SS pin @@ -367,13 +367,13 @@ build_flags = ${esp32_base.build_flags} -D LORA_TX_POWER=22 -; === LilyGo T3 with SX1276 environments === -[env:LilyGo_T3_sx1276_Repeater] -extends = LilyGo_T3_sx1276 -build_src_filter = ${LilyGo_T3_sx1276.build_src_filter} +<../examples/simple_repeater/main.cpp> +; === LILYGO T-LoRa V2.1-1.6 with SX1276 environments === +[env:LilyGo_TLora_V2_1_1_6_Repeater] +extends = LilyGo_TLora_V2_1_1_6 +build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter} +<../examples/simple_repeater/main.cpp> build_flags = - ${LilyGo_T3_sx1276.build_flags} - -D ADVERT_NAME="\"T3-1276 Repeater\"" + ${LilyGo_TLora_V2_1_1_6.build_flags} + -D ADVERT_NAME="\"TLora-V2.1-1.6 Repeater\"" -D ADVERT_LAT=-37.0 -D ADVERT_LON=145.0 -D ADMIN_PASSWORD="\"password\"" @@ -381,38 +381,38 @@ build_flags = ; -D MESH_DEBUG=1 ; -D CORE_DEBUG_LEVEL=3 -[env:LilyGo_T3_sx1276_terminal_chat] -extends = LilyGo_T3_sx1276 +[env:LilyGo_TLora_V2_1_1_6_terminal_chat] +extends = LilyGo_TLora_V2_1_1_6 build_flags = - ${LilyGo_T3_sx1276.build_flags} + ${LilyGo_TLora_V2_1_1_6.build_flags} -D MAX_CONTACTS=100 -D MAX_GROUP_CHANNELS=1 ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 -build_src_filter = ${LilyGo_T3_sx1276.build_src_filter} +<../examples/simple_secure_chat/main.cpp> +build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter} +<../examples/simple_secure_chat/main.cpp> lib_deps = - ${LilyGo_T3_sx1276.lib_deps} + ${LilyGo_TLora_V2_1_1_6.lib_deps} densaugeo/base64 @ ~1.4.0 -[env:LilyGo_T3_sx1276_companion_radio_usb] -extends = LilyGo_T3_sx1276 +[env:LilyGo_TLora_V2_1_1_6_companion_radio_usb] +extends = LilyGo_TLora_V2_1_1_6 build_flags = - ${LilyGo_T3_sx1276.build_flags} + ${LilyGo_TLora_V2_1_1_6.build_flags} -D MAX_CONTACTS=100 -D MAX_GROUP_CHANNELS=1 ; -D ENABLE_PRIVATE_KEY_IMPORT=1 ; -D ENABLE_PRIVATE_KEY_EXPORT=1 ; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 ; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 -build_src_filter = ${LilyGo_T3_sx1276.build_src_filter} +<../examples/companion_radio/main.cpp> +build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter} +<../examples/companion_radio/main.cpp> lib_deps = - ${LilyGo_T3_sx1276.lib_deps} + ${LilyGo_TLora_V2_1_1_6.lib_deps} densaugeo/base64 @ ~1.4.0 -[env:LilyGo_T3_sx1276_companion_radio_ble] -extends = LilyGo_T3_sx1276 +[env:LilyGo_TLora_V2_1_1_6_companion_radio_ble] +extends = LilyGo_TLora_V2_1_1_6 build_flags = - ${LilyGo_T3_sx1276.build_flags} + ${LilyGo_TLora_V2_1_1_6.build_flags} -D MAX_CONTACTS=100 -D MAX_GROUP_CHANNELS=1 -D BLE_PIN_CODE=123456 @@ -421,17 +421,17 @@ build_flags = ; -D ENABLE_PRIVATE_KEY_EXPORT=1 ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 -build_src_filter = ${LilyGo_T3_sx1276.build_src_filter} + +<../examples/companion_radio/main.cpp> +build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter} + +<../examples/companion_radio/main.cpp> lib_deps = - ${LilyGo_T3_sx1276.lib_deps} + ${LilyGo_TLora_V2_1_1_6.lib_deps} densaugeo/base64 @ ~1.4.0 -[env:LilyGo_T3_sx1276_room_server] -extends = LilyGo_T3_sx1276 -build_src_filter = ${LilyGo_T3_sx1276.build_src_filter} +<../examples/simple_room_server/main.cpp> +[env:LilyGo_TLora_V2_1_1_6_room_server] +extends = LilyGo_TLora_V2_1_1_6 +build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter} +<../examples/simple_room_server/main.cpp> build_flags = - ${LilyGo_T3_sx1276.build_flags} - -D ADVERT_NAME="\"T3-1276 Room\"" + ${LilyGo_TLora_V2_1_1_6.build_flags} + -D ADVERT_NAME="\"TLora-V2.1-1.6 Room\"" -D ADVERT_LAT=-37.0 -D ADVERT_LON=145.0 -D ADMIN_PASSWORD="\"password\"" diff --git a/src/helpers/LilyGoT3S3Board.h b/src/helpers/LilyGoT3S3Board.h deleted file mode 100644 index f7287d211..000000000 --- a/src/helpers/LilyGoT3S3Board.h +++ /dev/null @@ -1,88 +0,0 @@ -#pragma once - -#include -#include "ESP32Board.h" -#include - -// LoRa radio module pins for LilyGo T3S3 -// These need to be defined here for the SX1276 version -#if defined(RADIO_CLASS) && defined(CustomSX1276) - // SX1276 pin definitions specific to LilyGo T3S3 - #define P_LORA_DIO_0 34 // DIO0 interrupt pin (SX1276 uses DIO0) - #define P_LORA_DIO_1 33 // DIO1 interrupt pin - #define P_LORA_NSS 7 // Chip select - #define P_LORA_RESET 8 // Reset pin - #define P_LORA_SCLK 5 // SPI clock - #define P_LORA_MISO 3 // SPI MISO - #define P_LORA_MOSI 6 // SPI MOSI -#endif - -// Base class for LilyGo T-series boards -class LilyGoTBoard : public ESP32Board { -public: - void begin() { - ESP32Board::begin(); - - esp_reset_reason_t reason = esp_reset_reason(); - if (reason == ESP_RST_DEEPSLEEP) { - long wakeup_source = esp_sleep_get_ext1_wakeup_status(); - if (wakeup_source & (1 << P_LORA_DIO_1)) { // received a LoRa packet (while in deep sleep) - startup_reason = BD_STARTUP_RX_PACKET; - } - - rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS); - rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1); - } - } - - void enterDeepSleep(uint32_t secs, int pin_wake_btn = -1) { - esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); - - // Make sure the DIO1 and NSS GPIOs are hold on required levels during deep sleep - rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY); - rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1); - - rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS); - - if (pin_wake_btn < 0) { - esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet - } else { - esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1) | (1L << pin_wake_btn), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet OR wake btn - } - - if (secs > 0) { - esp_sleep_enable_timer_wakeup(secs * 1000000); - } - - // Finally set ESP32 into sleep - esp_deep_sleep_start(); // CPU halts here and never returns! - } - - uint16_t getBattMilliVolts() override { - analogReadResolution(12); - - uint32_t raw = 0; - for (int i = 0; i < 8; i++) { - raw += analogReadMilliVolts(PIN_VBAT_READ); - } - raw = raw / 8; - - return (2 * raw); - } -}; - -// Standard ESP32 version (for T3 board) -class LilyGoT3Board : public LilyGoTBoard { -public: - const char* getManufacturerName() const override { - return "LilyGo T3"; - } -}; - -// For S3 variant -class LilyGoT3S3Board : public LilyGoTBoard { -public: - const char* getManufacturerName() const override { - return "LilyGo T3S3"; - } -}; \ No newline at end of file diff --git a/src/helpers/LilyGoTLoraBoard.h b/src/helpers/LilyGoTLoraBoard.h new file mode 100644 index 000000000..c595740fd --- /dev/null +++ b/src/helpers/LilyGoTLoraBoard.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include "ESP32Board.h" + +// LILYGO T-LoRa V2.1-1.6 board with SX1276 +class LilyGoTLoraBoard : public ESP32Board { +public: + const char* getManufacturerName() const override { + return "LILYGO T-LoRa V2.1-1.6"; + } + + uint16_t getBattMilliVolts() override { + analogReadResolution(12); + + uint32_t raw = 0; + for (int i = 0; i < 8; i++) { + raw += analogReadMilliVolts(PIN_VBAT_READ); + } + raw = raw / 8; + + return (2 * raw); + } +}; \ No newline at end of file From 0fc85b8c59168fb6a766023e9301b3d6c5c67364 Mon Sep 17 00:00:00 2001 From: Alex Wolden Date: Wed, 5 Mar 2025 19:37:22 -0800 Subject: [PATCH 7/7] Revert identity change --- platformio.ini | 2 +- src/Identity.cpp | 65 +++++------------------------------------------- 2 files changed, 7 insertions(+), 60 deletions(-) diff --git a/platformio.ini b/platformio.ini index 099b3d934..87025a51d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -349,9 +349,9 @@ build_flags = -D P_LORA_TX_LED=2 ; LED pin for TX indication -D PIN_VBAT_READ=35 ; Battery voltage reading (analog pin) -D RADIO_CLASS=CustomSX1276 + -D ARDUINO_LOOP_STACK_SIZE=16384 -D WRAPPER_CLASS=CustomSX1276Wrapper -D LORA_TX_POWER=20 - -D USE_ESP32_ENCRYPTION=true ; ============= [LilyGo_T3S3_sx1262] diff --git a/src/Identity.cpp b/src/Identity.cpp index 568f61b11..e7c57d7ef 100644 --- a/src/Identity.cpp +++ b/src/Identity.cpp @@ -3,11 +3,6 @@ #define ED25519_NO_SEED 1 #include -// For weaker ESP32 boards, we use libsodium for cryptographic operations to reduce stack usage -#ifdef USE_ESP32_ENCRYPTION -#include -#endif - namespace mesh { Identity::Identity() { @@ -19,11 +14,7 @@ Identity::Identity(const char* pub_hex) { } bool Identity::verify(const uint8_t* sig, const uint8_t* message, int msg_len) const { - #ifdef USE_ESP32_ENCRYPTION - return crypto_sign_ed25519_verify_detached(sig, message, msg_len, pub_key) == 0; - #else - return ed25519_verify(sig, message, msg_len, pub_key); - #endif + return ed25519_verify(sig, message, msg_len, pub_key); } bool Identity::readFrom(Stream& s) { @@ -41,7 +32,6 @@ void Identity::printTo(Stream& s) const { LocalIdentity::LocalIdentity() { memset(prv_key, 0, sizeof(prv_key)); } - LocalIdentity::LocalIdentity(const char* prv_hex, const char* pub_hex) : Identity(pub_hex) { Utils::fromHex(prv_key, PRV_KEY_SIZE, prv_hex); } @@ -49,21 +39,7 @@ LocalIdentity::LocalIdentity(const char* prv_hex, const char* pub_hex) : Identit LocalIdentity::LocalIdentity(RNG* rng) { uint8_t seed[SEED_SIZE]; rng->random(seed, SEED_SIZE); - - #ifdef USE_ESP32_ENCRYPTION - // Use libsodium for keypair generation on ESP32 to reduce stack usage - // NOTE: Format differences between implementations: - // - The current ed25519 implementation (orlp/ed25519) uses a 64-byte private key format - // - Libsodium also uses a 64-byte format for Ed25519 secret keys, where: - // * First 32 bytes: the actual private key seed - // * Last 32 bytes: the corresponding public key - - // Generate keypair using libsodium with the provided seed - // This avoids the deep stack usage of the default implementation - crypto_sign_ed25519_seed_keypair(pub_key, prv_key, seed); - #else - ed25519_create_keypair(pub_key, prv_key, seed); - #endif + ed25519_create_keypair(pub_key, prv_key, seed); } bool LocalIdentity::readFrom(Stream& s) { @@ -101,46 +77,17 @@ void LocalIdentity::readFrom(const uint8_t* src, size_t len) { memcpy(pub_key, &src[PRV_KEY_SIZE], PUB_KEY_SIZE); } else if (len == PRV_KEY_SIZE) { memcpy(prv_key, src, PRV_KEY_SIZE); - - #ifdef USE_ESP32_ENCRYPTION - // In libsodium, the private key already contains the public key in its last 32 bytes - // We can just extract it directly, avoiding the expensive derivation calculation - memcpy(pub_key, prv_key + 32, 32); - #else - // now need to re-calculate the pub_key - ed25519_derive_pub(pub_key, prv_key); - #endif + // now need to re-calculate the pub_key + ed25519_derive_pub(pub_key, prv_key); } } void LocalIdentity::sign(uint8_t* sig, const uint8_t* message, int msg_len) const { - #ifdef USE_ESP32_ENCRYPTION - crypto_sign_ed25519_detached(sig, NULL, message, msg_len, prv_key); - #else - ed25519_sign(sig, message, msg_len, pub_key, prv_key); - #endif + ed25519_sign(sig, message, msg_len, pub_key, prv_key); } void LocalIdentity::calcSharedSecret(uint8_t* secret, const uint8_t* other_pub_key) { - #ifdef USE_ESP32_ENCRYPTION - // NOTE: To calculate a shared secret with Ed25519 keys and libsodium, we need to: - // Convert the Ed25519 keys to Curve25519 (X25519) format - // Perform the key exchange using the converted keys - // - // The default implementation handles this conversion internally, - // but with libsodium we need to do these steps explicitly. - unsigned char x25519_pk[crypto_scalarmult_curve25519_BYTES]; - unsigned char x25519_sk[crypto_scalarmult_curve25519_BYTES]; - - // Convert Ed25519 keys to Curve25519 keys for ECDH - crypto_sign_ed25519_pk_to_curve25519(x25519_pk, other_pub_key); - crypto_sign_ed25519_sk_to_curve25519(x25519_sk, prv_key); - - // Calculate shared secret using X25519 - crypto_scalarmult_curve25519(secret, x25519_sk, x25519_pk); - #else - ed25519_key_exchange(secret, other_pub_key, prv_key); - #endif + ed25519_key_exchange(secret, other_pub_key, prv_key); } } \ No newline at end of file