From f7feea63f74aee61a283b19cbb6bb5857bd7a82d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 5 Jan 2025 21:34:38 +0100 Subject: [PATCH] Attempt to merge --- src/FSCommon.cpp | 10 +++++++--- src/main.cpp | 4 +++- src/mesh/RF95Interface.cpp | 2 +- src/modules/StoreForwardModule.cpp | 19 +++++++++++++++---- src/modules/StoreForwardModule.h | 1 + variants/tlora_v2_1_16/platformio.ini | 10 ++++++++-- variants/tlora_v2_1_16/variant.h | 13 ++++++++++++- 7 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index a5f3514d3..80ed68600 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -13,8 +13,10 @@ #include "configuration.h" #ifdef HAS_SDCARD +#include "SPILock.h" #include #include +#ifndef SDCARD_USE_HSPI // old ESP32 #ifdef SDCARD_USE_SPI1 #ifdef ARCH_ESP32 SPIClass SPI1(HSPI); @@ -29,7 +31,9 @@ SPIClass SPI1(HSPI); #endif // NRF52 #define SDHandler SPI // only used for esp32 #endif // SDCARD_USE_SPI1 - +#else +SPIClass SDHandler = SPIClass(HSPI); +#endif #endif // HAS_SDCARD #if defined(ARCH_STM32WL) @@ -379,10 +383,10 @@ void fsInit() void setupSDCard() { #ifdef HAS_SDCARD -concurrency::LockGuard g(spiLock); + concurrency::LockGuard g(spiLock); #if (defined(ARCH_ESP32) || defined(ARCH_NRF52)) #if (defined(ARCH_ESP32)) - SDHandler.begin(SPI_SCK, SPI_MISO, SPI_MOSI); + SDHandler.begin(SPI_SCK, SPI_MISO, SPI_MOSI); #endif if (!SD.begin(SDCARD_CS, SDHandler)) { // param SDHandler only used for esp32 LOG_DEBUG("No SD_MMC card detected"); diff --git a/src/main.cpp b/src/main.cpp index c2b20b1c1..b75cc8aa6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -613,6 +613,9 @@ void setup() i2cScanner.reset(); #endif + // Init our SPI controller (must be before screen and lora) + initSPI(); + #ifdef HAS_SDCARD setupSDCard(); #endif @@ -704,7 +707,6 @@ void setup() drv.setMode(DRV2605_MODE_INTTRIG); #endif - // Init our SPI controller (must be before screen and lora) #ifdef ARCH_RP2040 #ifdef HW_SPI1_DEVICE SPI1.setSCK(LORA_SCK); diff --git a/src/mesh/RF95Interface.cpp b/src/mesh/RF95Interface.cpp index 9ef045099..4b92c4951 100644 --- a/src/mesh/RF95Interface.cpp +++ b/src/mesh/RF95Interface.cpp @@ -185,7 +185,7 @@ bool RF95Interface::init() #endif if (res == RADIOLIB_ERR_NONE) - res = lora->setCRC(RADIOLIB_SX126X_LORA_CRC_ON); + res = lora->setCRC(true); if (res == RADIOLIB_ERR_NONE) startReceive(); // start receiving diff --git a/src/modules/StoreForwardModule.cpp b/src/modules/StoreForwardModule.cpp index 928f11643..b981f175a 100644 --- a/src/modules/StoreForwardModule.cpp +++ b/src/modules/StoreForwardModule.cpp @@ -99,6 +99,7 @@ void StoreForwardModule::populateSDCard() { #if defined(HAS_SDCARD) #if (defined(ARCH_ESP32) || defined(ARCH_NRF52)) + spiLock->lock(); if (SD.cardType() != CARD_NONE) { if (!SD.exists("/storeforward")) { LOG_INFO("Creating StoreForward directory"); @@ -110,6 +111,7 @@ void StoreForwardModule::populateSDCard() this->packetHistory = (PacketHistoryStruct *)malloc(sizeof(PacketHistoryStruct)); LOG_DEBUG("numberOfPackets for packetHistory - %u", numberOfPackets); } + spiLock->unlock(); #endif // ARCH_ESP32 || ARCH_NRF52 #endif // HAS_SDCARD } @@ -169,6 +171,7 @@ uint32_t StoreForwardModule::getNumAvailablePackets(NodeNum dest, uint32_t last_ } else if (this->storageType == StorageType::ST_SDCARD) { #if defined(HAS_SDCARD) #if defined(ARCH_ESP32) || defined(ARCH_NRF52) + spiLock->lock(); auto handler = SD.open("/storeforward/" + String(i), FILE_READ); if (handler) { handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); @@ -181,6 +184,7 @@ uint32_t StoreForwardModule::getNumAvailablePackets(NodeNum dest, uint32_t last_ } } } + spiLock->unlock(); #endif #endif } else { @@ -262,9 +266,11 @@ void StoreForwardModule::historyAdd(const meshtastic_MeshPacket &mp) this->packetHistory[0].emoji = (bool)p.emoji; this->packetHistory[0].payload_size = p.payload.size; memcpy(this->packetHistory[0].payload, p.payload.bytes, meshtastic_Constants_DATA_PAYLOAD_LEN); + spiLock->lock(); auto handler = SD.open("/storeforward/" + String(this->packetHistoryTotalCount), FILE_WRITE); handler.write((uint8_t *)&this->packetHistory, sizeof(PacketHistoryStruct)); handler.close(); + spiLock->unlock(); #endif #endif } else { @@ -353,10 +359,12 @@ meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t } else if (this->storageType == StorageType::ST_SDCARD) { #if defined(HAS_SDCARD) #if defined(ARCH_ESP32) || defined(ARCH_NRF52) + spiLock->lock(); auto handler = SD.open("/storeforward/" + String(i), FILE_READ); if (handler) { handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); handler.close(); + spiLock->unlock(); if (this->packetHistory[0].time && (this->packetHistory[0].time > last_time)) { if (this->packetHistory[0].from != dest && (this->packetHistory[0].to == NODENUM_BROADCAST || this->packetHistory[0].to == dest)) { @@ -395,6 +403,8 @@ meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t return p; } } + } else { + spiLock->unlock(); } #endif #endif @@ -735,11 +745,12 @@ StoreForwardModule::StoreForwardModule() } #ifdef HAS_SDCARD // If we have an SDCARD, format it for store&forward use - this->populateSDCard(); - LOG_INFO("S&F: SDCARD initialized"); - is_server = true; + if (SD.cardType() != CARD_NONE) { + this->populateSDCard(); + LOG_INFO("S&F: SDCARD initialized"); + is_server = true; + } #endif - // Client } else { is_client = true; diff --git a/src/modules/StoreForwardModule.h b/src/modules/StoreForwardModule.h index 649e1cfaa..1e65c1cb0 100644 --- a/src/modules/StoreForwardModule.h +++ b/src/modules/StoreForwardModule.h @@ -10,6 +10,7 @@ #include #ifdef HAS_SDCARD +#include "SPILock.h" #include #endif diff --git a/variants/tlora_v2_1_16/platformio.ini b/variants/tlora_v2_1_16/platformio.ini index 351f71676..722bf9d59 100644 --- a/variants/tlora_v2_1_16/platformio.ini +++ b/variants/tlora_v2_1_16/platformio.ini @@ -3,5 +3,11 @@ extends = esp32_base board = ttgo-lora32-v21 board_check = true build_flags = - ${esp32_base.build_flags} -D TLORA_V2_1_16 -I variants/tlora_v2_1_16 - -DGPS_POWER_TOGGLE ; comment this line to disable triple press function on the user button to turn off gps entirely. \ No newline at end of file + ${esp32_base.build_flags} + -D TLORA_V2_1_16 + -I variants/tlora_v2_1_16 + -DGPS_POWER_TOGGLE ; comment this line to disable triple press function on the user button to turn off gps entirely. + -DRADIOLIB_EXCLUDE_SX128X=1 + -DRADIOLIB_EXCLUDE_SX126X=1 + -DRADIOLIB_EXCLUDE_LR11X0=1 + \ No newline at end of file diff --git a/variants/tlora_v2_1_16/variant.h b/variants/tlora_v2_1_16/variant.h index 48c069ab7..9607620ea 100644 --- a/variants/tlora_v2_1_16/variant.h +++ b/variants/tlora_v2_1_16/variant.h @@ -22,4 +22,15 @@ #define LORA_DIO1 33 // https://www.thethingsnetwork.org/forum/t/big-esp32-sx127x-topic-part-3/18436 #endif -#define LORA_DIO2 32 // Not really used \ No newline at end of file +#define LORA_DIO2 32 // Not really used + +/* + * Use SD Card for Store and Forward + */ +#define HAS_SDCARD +#define SDCARD_USE_HSPI +#define SPI_MOSI 15 +#define SPI_MISO 2 +#define SPI_SCK 14 +#define SPI_CS 13 +#define SDCARD_CS SPI_CS \ No newline at end of file