From b1125513f3bbfc857f905826c349d8428a0e82cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Wed, 16 Oct 2024 21:11:24 +0200 Subject: [PATCH 01/20] Use SD card as store&forward memory --- src/graphics/Screen.cpp | 10 +- src/memGet.cpp | 4 + src/mesh/MeshService.cpp | 4 +- src/mesh/MeshService.h | 4 +- src/mesh/NodeDB.cpp | 8 +- src/modules/Modules.cpp | 9 +- src/modules/StoreForwardModule.cpp | 194 ++++++++++++++++++++++------- src/modules/StoreForwardModule.h | 11 ++ 8 files changed, 180 insertions(+), 64 deletions(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index efa3ec78f..f1c2bd873 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -49,6 +49,9 @@ along with this program. If not, see . #include "modules/ExternalNotificationModule.h" #include "modules/TextMessageModule.h" #include "modules/WaypointModule.h" +#if !MESHTASTIC_EXCLUDE_STOREFORWARD +#include "modules/StoreForwardModule.h" +#endif #include "sleep.h" #include "target_specific.h" @@ -58,11 +61,9 @@ along with this program. If not, see . #ifdef ARCH_ESP32 #include "esp_task_wdt.h" -#include "modules/StoreForwardModule.h" #endif #if ARCH_PORTDUINO -#include "modules/StoreForwardModule.h" #include "platform/portduino/PortduinoGlue.h" #endif @@ -2397,9 +2398,9 @@ void DebugInfo::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 display->setColor(WHITE); // Draw the channel name display->drawString(x, y + FONT_HEIGHT_SMALL, channelStr); +#if !MESHTASTIC_EXCLUDE_STOREFORWARD // Draw our hardware ID to assist with bluetooth pairing. Either prefix with Info or S&F Logo if (moduleConfig.store_forward.enabled) { -#ifdef ARCH_ESP32 if (!Throttle::isWithinTimespanMs(storeForwardModule->lastHeartbeat, (storeForwardModule->heartbeatInterval * 1200))) { // no heartbeat, overlap a bit #if (defined(USE_EINK) || defined(ILI9341_DRIVER) || defined(ILI9342_DRIVER) || defined(ST7701_CS) || defined(ST7735_CS) || \ @@ -2426,6 +2427,9 @@ void DebugInfo::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 imgSF); #endif } +#else + // No store and forward, show a exclamation mark + if (false) { #endif } else { // TODO: Raspberry Pi supports more than just the one screen size diff --git a/src/memGet.cpp b/src/memGet.cpp index ef1102f1e..33fb94e1c 100644 --- a/src/memGet.cpp +++ b/src/memGet.cpp @@ -57,6 +57,8 @@ uint32_t MemGet::getFreePsram() { #ifdef ARCH_ESP32 return ESP.getFreePsram(); +#elif defined(HAS_SDCARD) + return SD.totalBytes() - SD.usedBytes(); #elif defined(ARCH_PORTDUINO) return 4194252; #else @@ -73,6 +75,8 @@ uint32_t MemGet::getPsramSize() { #ifdef ARCH_ESP32 return ESP.getPsramSize(); +#elif defined(HAS_SDCARD) + return SD.totalBytes(); #elif defined(ARCH_PORTDUINO) return 4194252; #else diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index d224c05dc..c561b3817 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -284,7 +284,6 @@ void MeshService::sendToPhone(meshtastic_MeshPacket *p) { perhapsDecode(p); -#ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_STOREFORWARD if (moduleConfig.store_forward.enabled && storeForwardModule->isServer() && p->decoded.portnum == meshtastic_PortNum_TEXT_MESSAGE_APP) { @@ -292,7 +291,6 @@ void MeshService::sendToPhone(meshtastic_MeshPacket *p) fromNum++; // Notify observers for packet from radio return; } -#endif #endif if (toPhoneQueue.numFree() == 0) { @@ -418,4 +416,4 @@ uint32_t MeshService::GetTimeSinceMeshPacket(const meshtastic_MeshPacket *mp) delta = 0; return delta; -} +} \ No newline at end of file diff --git a/src/mesh/MeshService.h b/src/mesh/MeshService.h index 1ccca4e6d..52ba60663 100644 --- a/src/mesh/MeshService.h +++ b/src/mesh/MeshService.h @@ -13,11 +13,9 @@ #if defined(ARCH_PORTDUINO) && !HAS_RADIO #include "../platform/portduino/SimRadio.h" #endif -#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) #if !MESHTASTIC_EXCLUDE_STOREFORWARD #include "modules/StoreForwardModule.h" #endif -#endif extern Allocator &queueStatusPool; extern Allocator &mqttClientProxyMessagePool; @@ -165,4 +163,4 @@ class MeshService friend class RoutingModule; }; -extern MeshService *service; +extern MeshService *service; \ No newline at end of file diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 558c5b825..aa7141351 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -21,6 +21,9 @@ #include "mesh-pb-constants.h" #include "meshUtils.h" #include "modules/NeighborInfoModule.h" +// #if !MESHTASTIC_EXCLUDE_STOREFORWARD +// #include "modules/StoreForwardModule.h" +// #endif #include #include #include @@ -32,13 +35,12 @@ #if HAS_WIFI #include "mesh/wifi/WiFiAPClient.h" #endif -#include "modules/StoreForwardModule.h" + #include #include #endif #ifdef ARCH_PORTDUINO -#include "modules/StoreForwardModule.h" #include "platform/portduino/PortduinoGlue.h" #endif @@ -1208,4 +1210,4 @@ void recordCriticalError(meshtastic_CriticalErrorCode code, uint32_t address, co LOG_ERROR("A critical failure occurred, portduino is exiting..."); exit(2); #endif -} +} \ No newline at end of file diff --git a/src/modules/Modules.cpp b/src/modules/Modules.cpp index ad3f0ace4..55b1bdc48 100644 --- a/src/modules/Modules.cpp +++ b/src/modules/Modules.cpp @@ -47,9 +47,6 @@ #endif #if ARCH_PORTDUINO #include "input/LinuxInputImpl.h" -#if !MESHTASTIC_EXCLUDE_STOREFORWARD -#include "modules/StoreForwardModule.h" -#endif #endif #if HAS_TELEMETRY #include "modules/Telemetry/DeviceTelemetry.h" @@ -63,6 +60,9 @@ #if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_POWER_TELEMETRY #include "modules/Telemetry/PowerTelemetry.h" #endif +#if !MESHTASTIC_EXCLUDE_STOREFORWARD +#include "modules/StoreForwardModule.h" +#endif #ifdef ARCH_ESP32 #if defined(USE_SX1280) && !MESHTASTIC_EXCLUDE_AUDIO #include "modules/esp32/AudioModule.h" @@ -70,9 +70,6 @@ #if !MESHTASTIC_EXCLUDE_PAXCOUNTER #include "modules/esp32/PaxcounterModule.h" #endif -#if !MESHTASTIC_EXCLUDE_STOREFORWARD -#include "modules/StoreForwardModule.h" -#endif #endif #if defined(ARCH_ESP32) || defined(ARCH_NRF52) || defined(ARCH_RP2040) #if !MESHTASTIC_EXCLUDE_EXTERNALNOTIFICATION diff --git a/src/modules/StoreForwardModule.cpp b/src/modules/StoreForwardModule.cpp index 039523207..8f76bd047 100644 --- a/src/modules/StoreForwardModule.cpp +++ b/src/modules/StoreForwardModule.cpp @@ -32,7 +32,7 @@ StoreForwardModule *storeForwardModule; int32_t StoreForwardModule::runOnce() { -#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) +#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) || defined(HAS_SDCARD) if (moduleConfig.store_forward.enabled && is_server) { // Send out the message queue. if (this->busy) { @@ -89,6 +89,27 @@ void StoreForwardModule::populatePSRAM() LOG_DEBUG("After PSRAM init: heap %d/%d PSRAM %d/%d", memGet.getFreeHeap(), memGet.getHeapSize(), memGet.getFreePsram(), memGet.getPsramSize()); LOG_DEBUG("numberOfPackets for packetHistory - %u", numberOfPackets); + this->storageType = StorageType::PSRAM; +} + +/** + * if we have an SDCARD, format it for store&forward use + */ +void StoreForwardModule::populateSDCard() +{ +#if defined(HAS_SDCARD) + if (SD.cardType() != CARD_NONE) { + if (!SD.exists("/storeforward")) { + LOG_INFO("Creating StoreForward directory"); + SD.mkdir("/storeforward"); + } + this->storageType = StorageType::SDCARD; + uint32_t numberOfPackets = (this->records ? this->records : (((SD.totalBytes() / 3) * 2) / sizeof(PacketHistoryStruct))); + // only allocate space for one temp copy + this->packetHistory = (PacketHistoryStruct *)malloc(sizeof(PacketHistoryStruct)); + LOG_DEBUG("numberOfPackets for packetHistory - %u", numberOfPackets); + } +#endif } /** @@ -135,11 +156,26 @@ uint32_t StoreForwardModule::getNumAvailablePackets(NodeNum dest, uint32_t last_ lastRequest.emplace(dest, 0); } for (uint32_t i = lastRequest[dest]; i < this->packetHistoryTotalCount; i++) { - if (this->packetHistory[i].time && (this->packetHistory[i].time > last_time)) { - // Client is only interested in packets not from itself and only in broadcast packets or packets towards it. - if (this->packetHistory[i].from != dest && - (this->packetHistory[i].to == NODENUM_BROADCAST || this->packetHistory[i].to == dest)) { - count++; + if (this->storageType == StorageType::PSRAM) { + if (this->packetHistory[i].time && (this->packetHistory[i].time > last_time)) { + // Client is only interested in packets not from itself and only in broadcast packets or packets towards it. + if (this->packetHistory[i].from != dest && + (this->packetHistory[i].to == NODENUM_BROADCAST || this->packetHistory[i].to == dest)) { + count++; + } + } + } else { + auto handler = SD.open("/storeforward/" + String(i), FILE_READ); + if (handler) { + handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); + handler.close(); + if (this->packetHistory[0].time && (this->packetHistory[0].time > last_time)) { + // Client is only interested in packets not from itself and only in broadcast packets or packets towards it. + if (this->packetHistory[0].from != dest && + (this->packetHistory[0].to == NODENUM_BROADCAST || this->packetHistory[0].to == dest)) { + count++; + } + } } } } @@ -187,19 +223,33 @@ void StoreForwardModule::historyAdd(const meshtastic_MeshPacket &mp) const auto &p = mp.decoded; if (this->packetHistoryTotalCount == this->records) { - LOG_WARN("S&F - PSRAM Full. Starting overwrite."); + LOG_WARN("S&F - Storage Full. Starting overwrite."); this->packetHistoryTotalCount = 0; for (auto &i : lastRequest) { i.second = 0; // Clear the last request index for each client device } } - this->packetHistory[this->packetHistoryTotalCount].time = getTime(); - this->packetHistory[this->packetHistoryTotalCount].to = mp.to; - this->packetHistory[this->packetHistoryTotalCount].channel = mp.channel; - this->packetHistory[this->packetHistoryTotalCount].from = getFrom(&mp); - this->packetHistory[this->packetHistoryTotalCount].payload_size = p.payload.size; - memcpy(this->packetHistory[this->packetHistoryTotalCount].payload, p.payload.bytes, meshtastic_Constants_DATA_PAYLOAD_LEN); + if (this->storageType == StorageType::PSRAM) { + this->packetHistory[this->packetHistoryTotalCount].time = getTime(); + this->packetHistory[this->packetHistoryTotalCount].to = mp.to; + this->packetHistory[this->packetHistoryTotalCount].channel = mp.channel; + this->packetHistory[this->packetHistoryTotalCount].from = getFrom(&mp); + this->packetHistory[this->packetHistoryTotalCount].payload_size = p.payload.size; + memcpy(this->packetHistory[this->packetHistoryTotalCount].payload, p.payload.bytes, + meshtastic_Constants_DATA_PAYLOAD_LEN); + } else { + // Save to SDCARD + this->packetHistory[0].time = getTime(); + this->packetHistory[0].to = mp.to; + this->packetHistory[0].channel = mp.channel; + this->packetHistory[0].from = getFrom(&mp); + this->packetHistory[0].payload_size = p.payload.size; + memcpy(this->packetHistory[0].payload, p.payload.bytes, meshtastic_Constants_DATA_PAYLOAD_LEN); + auto handler = SD.open("/storeforward/" + String(this->packetHistoryTotalCount), FILE_WRITE); + handler.write((uint8_t *)&this->packetHistory, sizeof(PacketHistoryStruct)); + handler.close(); + } this->packetHistoryTotalCount++; } @@ -233,46 +283,93 @@ bool StoreForwardModule::sendPayload(NodeNum dest, uint32_t last_time) meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t last_time, bool local) { for (uint32_t i = lastRequest[dest]; i < this->packetHistoryTotalCount; i++) { - if (this->packetHistory[i].time && (this->packetHistory[i].time > last_time)) { - /* Copy the messages that were received by the server in the last msAgo - to the packetHistoryTXQueue structure. - Client not interested in packets from itself and only in broadcast packets or packets towards it. */ - if (this->packetHistory[i].from != dest && - (this->packetHistory[i].to == NODENUM_BROADCAST || this->packetHistory[i].to == dest)) { + if (this->storageType == StorageType::PSRAM) { - meshtastic_MeshPacket *p = allocDataPacket(); + if (this->packetHistory[i].time && (this->packetHistory[i].time > last_time)) { + /* Copy the messages that were received by the server in the last msAgo + to the packetHistoryTXQueue structure. + Client not interested in packets from itself and only in broadcast packets or packets towards it. */ + if (this->packetHistory[i].from != dest && + (this->packetHistory[i].to == NODENUM_BROADCAST || this->packetHistory[i].to == dest)) { - p->to = local ? this->packetHistory[i].to : dest; // PhoneAPI can handle original `to` - p->from = this->packetHistory[i].from; - p->channel = this->packetHistory[i].channel; - p->rx_time = this->packetHistory[i].time; + meshtastic_MeshPacket *p = allocDataPacket(); - // Let's assume that if the server received the S&F request that the client is in range. - // TODO: Make this configurable. - p->want_ack = false; + p->to = local ? this->packetHistory[i].to : dest; // PhoneAPI can handle original `to` + p->from = this->packetHistory[i].from; + p->channel = this->packetHistory[i].channel; + p->rx_time = this->packetHistory[i].time; - if (local) { // PhoneAPI gets normal TEXT_MESSAGE_APP - p->decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP; - memcpy(p->decoded.payload.bytes, this->packetHistory[i].payload, this->packetHistory[i].payload_size); - p->decoded.payload.size = this->packetHistory[i].payload_size; - } else { - meshtastic_StoreAndForward sf = meshtastic_StoreAndForward_init_zero; - sf.which_variant = meshtastic_StoreAndForward_text_tag; - sf.variant.text.size = this->packetHistory[i].payload_size; - memcpy(sf.variant.text.bytes, this->packetHistory[i].payload, this->packetHistory[i].payload_size); - if (this->packetHistory[i].to == NODENUM_BROADCAST) { - sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_BROADCAST; + // Let's assume that if the server received the S&F request that the client is in range. + // TODO: Make this configurable. + p->want_ack = false; + + if (local) { // PhoneAPI gets normal TEXT_MESSAGE_APP + p->decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP; + memcpy(p->decoded.payload.bytes, this->packetHistory[i].payload, this->packetHistory[i].payload_size); + p->decoded.payload.size = this->packetHistory[i].payload_size; } else { - sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_DIRECT; + meshtastic_StoreAndForward sf = meshtastic_StoreAndForward_init_zero; + sf.which_variant = meshtastic_StoreAndForward_text_tag; + sf.variant.text.size = this->packetHistory[i].payload_size; + memcpy(sf.variant.text.bytes, this->packetHistory[i].payload, this->packetHistory[i].payload_size); + if (this->packetHistory[i].to == NODENUM_BROADCAST) { + sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_BROADCAST; + } else { + sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_DIRECT; + } + + p->decoded.payload.size = pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), + &meshtastic_StoreAndForward_msg, &sf); } - p->decoded.payload.size = pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), - &meshtastic_StoreAndForward_msg, &sf); + lastRequest[dest] = i + 1; // Update the last request index for the client device + + return p; } + } + } else { + auto handler = SD.open("/storeforward/" + String(i), FILE_READ); + if (handler) { + handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); + handler.close(); + 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)) { - lastRequest[dest] = i + 1; // Update the last request index for the client device + meshtastic_MeshPacket *p = allocDataPacket(); - return p; + p->to = local ? this->packetHistory[0].to : dest; // PhoneAPI can handle original `to` + p->from = this->packetHistory[0].from; + p->channel = this->packetHistory[0].channel; + p->rx_time = this->packetHistory[0].time; + + // Let's assume that if the server received the S&F request that the client is in range. + p->want_ack = false; + + if (local) { // PhoneAPI gets normal TEXT_MESSAGE_APP + p->decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP; + memcpy(p->decoded.payload.bytes, this->packetHistory[0].payload, this->packetHistory[0].payload_size); + p->decoded.payload.size = this->packetHistory[0].payload_size; + } else { + meshtastic_StoreAndForward sf = meshtastic_StoreAndForward_init_zero; + sf.which_variant = meshtastic_StoreAndForward_text_tag; + sf.variant.text.size = this->packetHistory[0].payload_size; + memcpy(sf.variant.text.bytes, this->packetHistory[0].payload, this->packetHistory[0].payload_size); + if (this->packetHistory[0].to == NODENUM_BROADCAST) { + sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_BROADCAST; + } else { + sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_DIRECT; + } + + p->decoded.payload.size = pb_encode_to_bytes( + p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), &meshtastic_StoreAndForward_msg, &sf); + } + + lastRequest[dest] = i + 1; // Update the last request index for the client device + + return p; + } + } } } } @@ -377,7 +474,7 @@ void StoreForwardModule::statsSend(uint32_t to) */ ProcessMessage StoreForwardModule::handleReceived(const meshtastic_MeshPacket &mp) { -#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) +#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) || defined(HAS_SDCARD) if (moduleConfig.store_forward.enabled) { if ((mp.decoded.portnum == meshtastic_PortNum_TEXT_MESSAGE_APP) && is_server) { @@ -556,7 +653,7 @@ StoreForwardModule::StoreForwardModule() ProtobufModule("StoreForward", meshtastic_PortNum_STORE_FORWARD_APP, &meshtastic_StoreAndForward_msg) { -#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) +#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) || defined(HAS_SDCARD) isPromiscuous = true; // Brown chicken brown cow @@ -601,12 +698,17 @@ StoreForwardModule::StoreForwardModule() this->populatePSRAM(); is_server = true; } else { - LOG_INFO("."); LOG_INFO("S&F: not enough PSRAM free, disabling."); } } else { LOG_INFO("S&F: device doesn't have PSRAM, disabling."); } +#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; +#endif // Client } else { diff --git a/src/modules/StoreForwardModule.h b/src/modules/StoreForwardModule.h index e3273470b..3a5032737 100644 --- a/src/modules/StoreForwardModule.h +++ b/src/modules/StoreForwardModule.h @@ -9,6 +9,10 @@ #include #include +#ifdef HAS_SDCARD +#include +#endif + struct PacketHistoryStruct { uint32_t time; uint32_t to; @@ -18,6 +22,9 @@ struct PacketHistoryStruct { pb_size_t payload_size; }; +// enum for the storage type +enum StorageType { PSRAM, SDCARD }; + class StoreForwardModule : private concurrency::OSThread, public ProtobufModule { bool busy = 0; @@ -80,6 +87,10 @@ class StoreForwardModule : private concurrency::OSThread, public ProtobufModule< private: void populatePSRAM(); + void populateSDCard(); + + // Storage Type + StorageType storageType = PSRAM; // S&F Defaults uint32_t historyReturnMax = 25; // Return maximum of 25 records by default. From 7532052edc4655dae48f61ba07a8a37ce5967993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Wed, 16 Oct 2024 21:11:24 +0200 Subject: [PATCH 02/20] Use SD card as store&forward memory --- src/graphics/Screen.cpp | 10 +- src/memGet.cpp | 4 + src/mesh/MeshService.cpp | 4 +- src/mesh/MeshService.h | 4 +- src/mesh/NodeDB.cpp | 8 +- src/modules/Modules.cpp | 9 +- src/modules/StoreForwardModule.cpp | 194 ++++++++++++++++++++++------- src/modules/StoreForwardModule.h | 11 ++ 8 files changed, 180 insertions(+), 64 deletions(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index efa3ec78f..f1c2bd873 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -49,6 +49,9 @@ along with this program. If not, see . #include "modules/ExternalNotificationModule.h" #include "modules/TextMessageModule.h" #include "modules/WaypointModule.h" +#if !MESHTASTIC_EXCLUDE_STOREFORWARD +#include "modules/StoreForwardModule.h" +#endif #include "sleep.h" #include "target_specific.h" @@ -58,11 +61,9 @@ along with this program. If not, see . #ifdef ARCH_ESP32 #include "esp_task_wdt.h" -#include "modules/StoreForwardModule.h" #endif #if ARCH_PORTDUINO -#include "modules/StoreForwardModule.h" #include "platform/portduino/PortduinoGlue.h" #endif @@ -2397,9 +2398,9 @@ void DebugInfo::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 display->setColor(WHITE); // Draw the channel name display->drawString(x, y + FONT_HEIGHT_SMALL, channelStr); +#if !MESHTASTIC_EXCLUDE_STOREFORWARD // Draw our hardware ID to assist with bluetooth pairing. Either prefix with Info or S&F Logo if (moduleConfig.store_forward.enabled) { -#ifdef ARCH_ESP32 if (!Throttle::isWithinTimespanMs(storeForwardModule->lastHeartbeat, (storeForwardModule->heartbeatInterval * 1200))) { // no heartbeat, overlap a bit #if (defined(USE_EINK) || defined(ILI9341_DRIVER) || defined(ILI9342_DRIVER) || defined(ST7701_CS) || defined(ST7735_CS) || \ @@ -2426,6 +2427,9 @@ void DebugInfo::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 imgSF); #endif } +#else + // No store and forward, show a exclamation mark + if (false) { #endif } else { // TODO: Raspberry Pi supports more than just the one screen size diff --git a/src/memGet.cpp b/src/memGet.cpp index ef1102f1e..33fb94e1c 100644 --- a/src/memGet.cpp +++ b/src/memGet.cpp @@ -57,6 +57,8 @@ uint32_t MemGet::getFreePsram() { #ifdef ARCH_ESP32 return ESP.getFreePsram(); +#elif defined(HAS_SDCARD) + return SD.totalBytes() - SD.usedBytes(); #elif defined(ARCH_PORTDUINO) return 4194252; #else @@ -73,6 +75,8 @@ uint32_t MemGet::getPsramSize() { #ifdef ARCH_ESP32 return ESP.getPsramSize(); +#elif defined(HAS_SDCARD) + return SD.totalBytes(); #elif defined(ARCH_PORTDUINO) return 4194252; #else diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index d224c05dc..c561b3817 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -284,7 +284,6 @@ void MeshService::sendToPhone(meshtastic_MeshPacket *p) { perhapsDecode(p); -#ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_STOREFORWARD if (moduleConfig.store_forward.enabled && storeForwardModule->isServer() && p->decoded.portnum == meshtastic_PortNum_TEXT_MESSAGE_APP) { @@ -292,7 +291,6 @@ void MeshService::sendToPhone(meshtastic_MeshPacket *p) fromNum++; // Notify observers for packet from radio return; } -#endif #endif if (toPhoneQueue.numFree() == 0) { @@ -418,4 +416,4 @@ uint32_t MeshService::GetTimeSinceMeshPacket(const meshtastic_MeshPacket *mp) delta = 0; return delta; -} +} \ No newline at end of file diff --git a/src/mesh/MeshService.h b/src/mesh/MeshService.h index 1ccca4e6d..52ba60663 100644 --- a/src/mesh/MeshService.h +++ b/src/mesh/MeshService.h @@ -13,11 +13,9 @@ #if defined(ARCH_PORTDUINO) && !HAS_RADIO #include "../platform/portduino/SimRadio.h" #endif -#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) #if !MESHTASTIC_EXCLUDE_STOREFORWARD #include "modules/StoreForwardModule.h" #endif -#endif extern Allocator &queueStatusPool; extern Allocator &mqttClientProxyMessagePool; @@ -165,4 +163,4 @@ class MeshService friend class RoutingModule; }; -extern MeshService *service; +extern MeshService *service; \ No newline at end of file diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 87a7ad091..70e4db1a6 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -21,6 +21,9 @@ #include "mesh-pb-constants.h" #include "meshUtils.h" #include "modules/NeighborInfoModule.h" +// #if !MESHTASTIC_EXCLUDE_STOREFORWARD +// #include "modules/StoreForwardModule.h" +// #endif #include #include #include @@ -32,7 +35,7 @@ #if HAS_WIFI #include "mesh/wifi/WiFiAPClient.h" #endif -#include "modules/StoreForwardModule.h" + #include #include #include @@ -42,7 +45,6 @@ #endif #ifdef ARCH_PORTDUINO -#include "modules/StoreForwardModule.h" #include "platform/portduino/PortduinoGlue.h" #endif @@ -1270,4 +1272,4 @@ void recordCriticalError(meshtastic_CriticalErrorCode code, uint32_t address, co LOG_ERROR("A critical failure occurred, portduino is exiting..."); exit(2); #endif -} +} \ No newline at end of file diff --git a/src/modules/Modules.cpp b/src/modules/Modules.cpp index ad3f0ace4..55b1bdc48 100644 --- a/src/modules/Modules.cpp +++ b/src/modules/Modules.cpp @@ -47,9 +47,6 @@ #endif #if ARCH_PORTDUINO #include "input/LinuxInputImpl.h" -#if !MESHTASTIC_EXCLUDE_STOREFORWARD -#include "modules/StoreForwardModule.h" -#endif #endif #if HAS_TELEMETRY #include "modules/Telemetry/DeviceTelemetry.h" @@ -63,6 +60,9 @@ #if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_POWER_TELEMETRY #include "modules/Telemetry/PowerTelemetry.h" #endif +#if !MESHTASTIC_EXCLUDE_STOREFORWARD +#include "modules/StoreForwardModule.h" +#endif #ifdef ARCH_ESP32 #if defined(USE_SX1280) && !MESHTASTIC_EXCLUDE_AUDIO #include "modules/esp32/AudioModule.h" @@ -70,9 +70,6 @@ #if !MESHTASTIC_EXCLUDE_PAXCOUNTER #include "modules/esp32/PaxcounterModule.h" #endif -#if !MESHTASTIC_EXCLUDE_STOREFORWARD -#include "modules/StoreForwardModule.h" -#endif #endif #if defined(ARCH_ESP32) || defined(ARCH_NRF52) || defined(ARCH_RP2040) #if !MESHTASTIC_EXCLUDE_EXTERNALNOTIFICATION diff --git a/src/modules/StoreForwardModule.cpp b/src/modules/StoreForwardModule.cpp index 039523207..8f76bd047 100644 --- a/src/modules/StoreForwardModule.cpp +++ b/src/modules/StoreForwardModule.cpp @@ -32,7 +32,7 @@ StoreForwardModule *storeForwardModule; int32_t StoreForwardModule::runOnce() { -#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) +#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) || defined(HAS_SDCARD) if (moduleConfig.store_forward.enabled && is_server) { // Send out the message queue. if (this->busy) { @@ -89,6 +89,27 @@ void StoreForwardModule::populatePSRAM() LOG_DEBUG("After PSRAM init: heap %d/%d PSRAM %d/%d", memGet.getFreeHeap(), memGet.getHeapSize(), memGet.getFreePsram(), memGet.getPsramSize()); LOG_DEBUG("numberOfPackets for packetHistory - %u", numberOfPackets); + this->storageType = StorageType::PSRAM; +} + +/** + * if we have an SDCARD, format it for store&forward use + */ +void StoreForwardModule::populateSDCard() +{ +#if defined(HAS_SDCARD) + if (SD.cardType() != CARD_NONE) { + if (!SD.exists("/storeforward")) { + LOG_INFO("Creating StoreForward directory"); + SD.mkdir("/storeforward"); + } + this->storageType = StorageType::SDCARD; + uint32_t numberOfPackets = (this->records ? this->records : (((SD.totalBytes() / 3) * 2) / sizeof(PacketHistoryStruct))); + // only allocate space for one temp copy + this->packetHistory = (PacketHistoryStruct *)malloc(sizeof(PacketHistoryStruct)); + LOG_DEBUG("numberOfPackets for packetHistory - %u", numberOfPackets); + } +#endif } /** @@ -135,11 +156,26 @@ uint32_t StoreForwardModule::getNumAvailablePackets(NodeNum dest, uint32_t last_ lastRequest.emplace(dest, 0); } for (uint32_t i = lastRequest[dest]; i < this->packetHistoryTotalCount; i++) { - if (this->packetHistory[i].time && (this->packetHistory[i].time > last_time)) { - // Client is only interested in packets not from itself and only in broadcast packets or packets towards it. - if (this->packetHistory[i].from != dest && - (this->packetHistory[i].to == NODENUM_BROADCAST || this->packetHistory[i].to == dest)) { - count++; + if (this->storageType == StorageType::PSRAM) { + if (this->packetHistory[i].time && (this->packetHistory[i].time > last_time)) { + // Client is only interested in packets not from itself and only in broadcast packets or packets towards it. + if (this->packetHistory[i].from != dest && + (this->packetHistory[i].to == NODENUM_BROADCAST || this->packetHistory[i].to == dest)) { + count++; + } + } + } else { + auto handler = SD.open("/storeforward/" + String(i), FILE_READ); + if (handler) { + handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); + handler.close(); + if (this->packetHistory[0].time && (this->packetHistory[0].time > last_time)) { + // Client is only interested in packets not from itself and only in broadcast packets or packets towards it. + if (this->packetHistory[0].from != dest && + (this->packetHistory[0].to == NODENUM_BROADCAST || this->packetHistory[0].to == dest)) { + count++; + } + } } } } @@ -187,19 +223,33 @@ void StoreForwardModule::historyAdd(const meshtastic_MeshPacket &mp) const auto &p = mp.decoded; if (this->packetHistoryTotalCount == this->records) { - LOG_WARN("S&F - PSRAM Full. Starting overwrite."); + LOG_WARN("S&F - Storage Full. Starting overwrite."); this->packetHistoryTotalCount = 0; for (auto &i : lastRequest) { i.second = 0; // Clear the last request index for each client device } } - this->packetHistory[this->packetHistoryTotalCount].time = getTime(); - this->packetHistory[this->packetHistoryTotalCount].to = mp.to; - this->packetHistory[this->packetHistoryTotalCount].channel = mp.channel; - this->packetHistory[this->packetHistoryTotalCount].from = getFrom(&mp); - this->packetHistory[this->packetHistoryTotalCount].payload_size = p.payload.size; - memcpy(this->packetHistory[this->packetHistoryTotalCount].payload, p.payload.bytes, meshtastic_Constants_DATA_PAYLOAD_LEN); + if (this->storageType == StorageType::PSRAM) { + this->packetHistory[this->packetHistoryTotalCount].time = getTime(); + this->packetHistory[this->packetHistoryTotalCount].to = mp.to; + this->packetHistory[this->packetHistoryTotalCount].channel = mp.channel; + this->packetHistory[this->packetHistoryTotalCount].from = getFrom(&mp); + this->packetHistory[this->packetHistoryTotalCount].payload_size = p.payload.size; + memcpy(this->packetHistory[this->packetHistoryTotalCount].payload, p.payload.bytes, + meshtastic_Constants_DATA_PAYLOAD_LEN); + } else { + // Save to SDCARD + this->packetHistory[0].time = getTime(); + this->packetHistory[0].to = mp.to; + this->packetHistory[0].channel = mp.channel; + this->packetHistory[0].from = getFrom(&mp); + this->packetHistory[0].payload_size = p.payload.size; + memcpy(this->packetHistory[0].payload, p.payload.bytes, meshtastic_Constants_DATA_PAYLOAD_LEN); + auto handler = SD.open("/storeforward/" + String(this->packetHistoryTotalCount), FILE_WRITE); + handler.write((uint8_t *)&this->packetHistory, sizeof(PacketHistoryStruct)); + handler.close(); + } this->packetHistoryTotalCount++; } @@ -233,46 +283,93 @@ bool StoreForwardModule::sendPayload(NodeNum dest, uint32_t last_time) meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t last_time, bool local) { for (uint32_t i = lastRequest[dest]; i < this->packetHistoryTotalCount; i++) { - if (this->packetHistory[i].time && (this->packetHistory[i].time > last_time)) { - /* Copy the messages that were received by the server in the last msAgo - to the packetHistoryTXQueue structure. - Client not interested in packets from itself and only in broadcast packets or packets towards it. */ - if (this->packetHistory[i].from != dest && - (this->packetHistory[i].to == NODENUM_BROADCAST || this->packetHistory[i].to == dest)) { + if (this->storageType == StorageType::PSRAM) { - meshtastic_MeshPacket *p = allocDataPacket(); + if (this->packetHistory[i].time && (this->packetHistory[i].time > last_time)) { + /* Copy the messages that were received by the server in the last msAgo + to the packetHistoryTXQueue structure. + Client not interested in packets from itself and only in broadcast packets or packets towards it. */ + if (this->packetHistory[i].from != dest && + (this->packetHistory[i].to == NODENUM_BROADCAST || this->packetHistory[i].to == dest)) { - p->to = local ? this->packetHistory[i].to : dest; // PhoneAPI can handle original `to` - p->from = this->packetHistory[i].from; - p->channel = this->packetHistory[i].channel; - p->rx_time = this->packetHistory[i].time; + meshtastic_MeshPacket *p = allocDataPacket(); - // Let's assume that if the server received the S&F request that the client is in range. - // TODO: Make this configurable. - p->want_ack = false; + p->to = local ? this->packetHistory[i].to : dest; // PhoneAPI can handle original `to` + p->from = this->packetHistory[i].from; + p->channel = this->packetHistory[i].channel; + p->rx_time = this->packetHistory[i].time; - if (local) { // PhoneAPI gets normal TEXT_MESSAGE_APP - p->decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP; - memcpy(p->decoded.payload.bytes, this->packetHistory[i].payload, this->packetHistory[i].payload_size); - p->decoded.payload.size = this->packetHistory[i].payload_size; - } else { - meshtastic_StoreAndForward sf = meshtastic_StoreAndForward_init_zero; - sf.which_variant = meshtastic_StoreAndForward_text_tag; - sf.variant.text.size = this->packetHistory[i].payload_size; - memcpy(sf.variant.text.bytes, this->packetHistory[i].payload, this->packetHistory[i].payload_size); - if (this->packetHistory[i].to == NODENUM_BROADCAST) { - sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_BROADCAST; + // Let's assume that if the server received the S&F request that the client is in range. + // TODO: Make this configurable. + p->want_ack = false; + + if (local) { // PhoneAPI gets normal TEXT_MESSAGE_APP + p->decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP; + memcpy(p->decoded.payload.bytes, this->packetHistory[i].payload, this->packetHistory[i].payload_size); + p->decoded.payload.size = this->packetHistory[i].payload_size; } else { - sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_DIRECT; + meshtastic_StoreAndForward sf = meshtastic_StoreAndForward_init_zero; + sf.which_variant = meshtastic_StoreAndForward_text_tag; + sf.variant.text.size = this->packetHistory[i].payload_size; + memcpy(sf.variant.text.bytes, this->packetHistory[i].payload, this->packetHistory[i].payload_size); + if (this->packetHistory[i].to == NODENUM_BROADCAST) { + sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_BROADCAST; + } else { + sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_DIRECT; + } + + p->decoded.payload.size = pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), + &meshtastic_StoreAndForward_msg, &sf); } - p->decoded.payload.size = pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), - &meshtastic_StoreAndForward_msg, &sf); + lastRequest[dest] = i + 1; // Update the last request index for the client device + + return p; } + } + } else { + auto handler = SD.open("/storeforward/" + String(i), FILE_READ); + if (handler) { + handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); + handler.close(); + 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)) { - lastRequest[dest] = i + 1; // Update the last request index for the client device + meshtastic_MeshPacket *p = allocDataPacket(); - return p; + p->to = local ? this->packetHistory[0].to : dest; // PhoneAPI can handle original `to` + p->from = this->packetHistory[0].from; + p->channel = this->packetHistory[0].channel; + p->rx_time = this->packetHistory[0].time; + + // Let's assume that if the server received the S&F request that the client is in range. + p->want_ack = false; + + if (local) { // PhoneAPI gets normal TEXT_MESSAGE_APP + p->decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP; + memcpy(p->decoded.payload.bytes, this->packetHistory[0].payload, this->packetHistory[0].payload_size); + p->decoded.payload.size = this->packetHistory[0].payload_size; + } else { + meshtastic_StoreAndForward sf = meshtastic_StoreAndForward_init_zero; + sf.which_variant = meshtastic_StoreAndForward_text_tag; + sf.variant.text.size = this->packetHistory[0].payload_size; + memcpy(sf.variant.text.bytes, this->packetHistory[0].payload, this->packetHistory[0].payload_size); + if (this->packetHistory[0].to == NODENUM_BROADCAST) { + sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_BROADCAST; + } else { + sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_DIRECT; + } + + p->decoded.payload.size = pb_encode_to_bytes( + p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), &meshtastic_StoreAndForward_msg, &sf); + } + + lastRequest[dest] = i + 1; // Update the last request index for the client device + + return p; + } + } } } } @@ -377,7 +474,7 @@ void StoreForwardModule::statsSend(uint32_t to) */ ProcessMessage StoreForwardModule::handleReceived(const meshtastic_MeshPacket &mp) { -#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) +#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) || defined(HAS_SDCARD) if (moduleConfig.store_forward.enabled) { if ((mp.decoded.portnum == meshtastic_PortNum_TEXT_MESSAGE_APP) && is_server) { @@ -556,7 +653,7 @@ StoreForwardModule::StoreForwardModule() ProtobufModule("StoreForward", meshtastic_PortNum_STORE_FORWARD_APP, &meshtastic_StoreAndForward_msg) { -#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) +#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) || defined(HAS_SDCARD) isPromiscuous = true; // Brown chicken brown cow @@ -601,12 +698,17 @@ StoreForwardModule::StoreForwardModule() this->populatePSRAM(); is_server = true; } else { - LOG_INFO("."); LOG_INFO("S&F: not enough PSRAM free, disabling."); } } else { LOG_INFO("S&F: device doesn't have PSRAM, disabling."); } +#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; +#endif // Client } else { diff --git a/src/modules/StoreForwardModule.h b/src/modules/StoreForwardModule.h index e3273470b..3a5032737 100644 --- a/src/modules/StoreForwardModule.h +++ b/src/modules/StoreForwardModule.h @@ -9,6 +9,10 @@ #include #include +#ifdef HAS_SDCARD +#include +#endif + struct PacketHistoryStruct { uint32_t time; uint32_t to; @@ -18,6 +22,9 @@ struct PacketHistoryStruct { pb_size_t payload_size; }; +// enum for the storage type +enum StorageType { PSRAM, SDCARD }; + class StoreForwardModule : private concurrency::OSThread, public ProtobufModule { bool busy = 0; @@ -80,6 +87,10 @@ class StoreForwardModule : private concurrency::OSThread, public ProtobufModule< private: void populatePSRAM(); + void populateSDCard(); + + // Storage Type + StorageType storageType = PSRAM; // S&F Defaults uint32_t historyReturnMax = 25; // Return maximum of 25 records by default. From 840996c7551b069cb225a287eea05021d9a1e7f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sat, 26 Oct 2024 13:41:31 +0200 Subject: [PATCH 03/20] fix build for portduino --- src/graphics/images.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/graphics/images.h b/src/graphics/images.h index 2b0854a33..1d6168c8c 100644 --- a/src/graphics/images.h +++ b/src/graphics/images.h @@ -21,7 +21,7 @@ const uint8_t bluetoothConnectedIcon[36] PROGMEM = {0xfe, 0x01, 0xff, 0x03, 0x03 #endif #if (defined(USE_EINK) || defined(ILI9341_DRIVER) || defined(ILI9342_DRIVER) || defined(ST7701_CS) || defined(ST7735_CS) || \ - defined(ST7789_CS) || defined(USE_ST7789) || defined(HX8357_CS) || ARCH_PORTDUINO) && \ + defined(ST7789_CS) || defined(USE_ST7789) || defined(HX8357_CS)) && \ !defined(DISPLAY_FORCE_SMALL_FONTS) const uint8_t imgQuestionL1[] PROGMEM = {0xff, 0x01, 0x01, 0x32, 0x7b, 0x49, 0x49, 0x6f, 0x26, 0x01, 0x01, 0xff}; const uint8_t imgQuestionL2[] PROGMEM = {0x0f, 0x08, 0x08, 0x08, 0x06, 0x0f, 0x0f, 0x06, 0x08, 0x08, 0x08, 0x0f}; From 94155f170e64c84289acc745a62fc3d1d97bd1bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sat, 26 Oct 2024 13:58:29 +0200 Subject: [PATCH 04/20] fix compilation, double macros resolved --- src/graphics/images.h | 2 +- src/modules/StoreForwardModule.cpp | 28 ++++++++++++++++++++-------- src/modules/StoreForwardModule.h | 4 ++-- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/graphics/images.h b/src/graphics/images.h index 1d6168c8c..5ce40f26a 100644 --- a/src/graphics/images.h +++ b/src/graphics/images.h @@ -21,7 +21,7 @@ const uint8_t bluetoothConnectedIcon[36] PROGMEM = {0xfe, 0x01, 0xff, 0x03, 0x03 #endif #if (defined(USE_EINK) || defined(ILI9341_DRIVER) || defined(ILI9342_DRIVER) || defined(ST7701_CS) || defined(ST7735_CS) || \ - defined(ST7789_CS) || defined(USE_ST7789) || defined(HX8357_CS)) && \ + defined(ST7789_CS) || defined(USE_ST7789) || defined(HX8357_CS)) && \ !defined(DISPLAY_FORCE_SMALL_FONTS) const uint8_t imgQuestionL1[] PROGMEM = {0xff, 0x01, 0x01, 0x32, 0x7b, 0x49, 0x49, 0x6f, 0x26, 0x01, 0x01, 0xff}; const uint8_t imgQuestionL2[] PROGMEM = {0x0f, 0x08, 0x08, 0x08, 0x06, 0x0f, 0x0f, 0x06, 0x08, 0x08, 0x08, 0x0f}; diff --git a/src/modules/StoreForwardModule.cpp b/src/modules/StoreForwardModule.cpp index 8f76bd047..c111d5299 100644 --- a/src/modules/StoreForwardModule.cpp +++ b/src/modules/StoreForwardModule.cpp @@ -89,7 +89,7 @@ void StoreForwardModule::populatePSRAM() LOG_DEBUG("After PSRAM init: heap %d/%d PSRAM %d/%d", memGet.getFreeHeap(), memGet.getHeapSize(), memGet.getFreePsram(), memGet.getPsramSize()); LOG_DEBUG("numberOfPackets for packetHistory - %u", numberOfPackets); - this->storageType = StorageType::PSRAM; + this->storageType = StorageType::ST_PSRAM; } /** @@ -156,7 +156,7 @@ uint32_t StoreForwardModule::getNumAvailablePackets(NodeNum dest, uint32_t last_ lastRequest.emplace(dest, 0); } for (uint32_t i = lastRequest[dest]; i < this->packetHistoryTotalCount; i++) { - if (this->storageType == StorageType::PSRAM) { + if (this->storageType == StorageType::ST_PSRAM) { if (this->packetHistory[i].time && (this->packetHistory[i].time > last_time)) { // Client is only interested in packets not from itself and only in broadcast packets or packets towards it. if (this->packetHistory[i].from != dest && @@ -164,7 +164,8 @@ uint32_t StoreForwardModule::getNumAvailablePackets(NodeNum dest, uint32_t last_ count++; } } - } else { + } else if (this->storageType == StorageType::ST_SDCARD) { +#if defined(HAS_SDCARD) auto handler = SD.open("/storeforward/" + String(i), FILE_READ); if (handler) { handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); @@ -177,6 +178,9 @@ uint32_t StoreForwardModule::getNumAvailablePackets(NodeNum dest, uint32_t last_ } } } +#endif + } else { + LOG_ERROR("S&F: Unknown storage type"); } } return count; @@ -230,7 +234,7 @@ void StoreForwardModule::historyAdd(const meshtastic_MeshPacket &mp) } } - if (this->storageType == StorageType::PSRAM) { + if (this->storageType == StorageType::ST_PSRAM) { this->packetHistory[this->packetHistoryTotalCount].time = getTime(); this->packetHistory[this->packetHistoryTotalCount].to = mp.to; this->packetHistory[this->packetHistoryTotalCount].channel = mp.channel; @@ -238,8 +242,9 @@ void StoreForwardModule::historyAdd(const meshtastic_MeshPacket &mp) this->packetHistory[this->packetHistoryTotalCount].payload_size = p.payload.size; memcpy(this->packetHistory[this->packetHistoryTotalCount].payload, p.payload.bytes, meshtastic_Constants_DATA_PAYLOAD_LEN); - } else { - // Save to SDCARD + } else if (this->storageType == StorageType::ST_SDCARD) { +// Save to SDCARD +#if defined(HAS_SDCARD) this->packetHistory[0].time = getTime(); this->packetHistory[0].to = mp.to; this->packetHistory[0].channel = mp.channel; @@ -249,6 +254,9 @@ void StoreForwardModule::historyAdd(const meshtastic_MeshPacket &mp) auto handler = SD.open("/storeforward/" + String(this->packetHistoryTotalCount), FILE_WRITE); handler.write((uint8_t *)&this->packetHistory, sizeof(PacketHistoryStruct)); handler.close(); +#endif + } else { + LOG_ERROR("S&F: Unknown storage type"); } this->packetHistoryTotalCount++; @@ -283,7 +291,7 @@ bool StoreForwardModule::sendPayload(NodeNum dest, uint32_t last_time) meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t last_time, bool local) { for (uint32_t i = lastRequest[dest]; i < this->packetHistoryTotalCount; i++) { - if (this->storageType == StorageType::PSRAM) { + if (this->storageType == StorageType::ST_PSRAM) { if (this->packetHistory[i].time && (this->packetHistory[i].time > last_time)) { /* Copy the messages that were received by the server in the last msAgo @@ -327,7 +335,8 @@ meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t return p; } } - } else { + } else if (this->storageType == StorageType::ST_SDCARD) { +#if defined(HAS_SDCARD) auto handler = SD.open("/storeforward/" + String(i), FILE_READ); if (handler) { handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); @@ -371,6 +380,9 @@ meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t } } } +#endif + } else { + LOG_ERROR("S&F: Unknown storage type"); } } return nullptr; diff --git a/src/modules/StoreForwardModule.h b/src/modules/StoreForwardModule.h index 3a5032737..5756d9de5 100644 --- a/src/modules/StoreForwardModule.h +++ b/src/modules/StoreForwardModule.h @@ -23,7 +23,7 @@ struct PacketHistoryStruct { }; // enum for the storage type -enum StorageType { PSRAM, SDCARD }; +enum StorageType { ST_PSRAM, ST_SDCARD }; class StoreForwardModule : private concurrency::OSThread, public ProtobufModule { @@ -90,7 +90,7 @@ class StoreForwardModule : private concurrency::OSThread, public ProtobufModule< void populateSDCard(); // Storage Type - StorageType storageType = PSRAM; + StorageType storageType = ST_PSRAM; // S&F Defaults uint32_t historyReturnMax = 25; // Return maximum of 25 records by default. From a13a299b92a4ef8c4fc641addcd5532d07a29368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sat, 26 Oct 2024 14:39:12 +0200 Subject: [PATCH 05/20] next one - fix compilation for Portduino --- src/graphics/Screen.cpp | 2 +- src/graphics/images.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index f1c2bd873..2e401731e 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -2416,7 +2416,7 @@ void DebugInfo::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 #endif } else { #if (defined(USE_EINK) || defined(ILI9341_DRIVER) || defined(ILI9342_DRIVER) || defined(ST7701_CS) || defined(ST7735_CS) || \ - defined(ST7789_CS) || defined(USE_ST7789) || defined(HX8357_CS)) && \ + defined(ST7789_CS) || defined(USE_ST7789) || defined(HX8357_CS) || ARCH_PORTDUINO) && \ !defined(DISPLAY_FORCE_SMALL_FONTS) display->drawFastImage(x + SCREEN_WIDTH - 18 - display->getStringWidth(ourId), y + 3 + FONT_HEIGHT_SMALL, 16, 8, imgSFL1); diff --git a/src/graphics/images.h b/src/graphics/images.h index 5ce40f26a..2b0854a33 100644 --- a/src/graphics/images.h +++ b/src/graphics/images.h @@ -21,7 +21,7 @@ const uint8_t bluetoothConnectedIcon[36] PROGMEM = {0xfe, 0x01, 0xff, 0x03, 0x03 #endif #if (defined(USE_EINK) || defined(ILI9341_DRIVER) || defined(ILI9342_DRIVER) || defined(ST7701_CS) || defined(ST7735_CS) || \ - defined(ST7789_CS) || defined(USE_ST7789) || defined(HX8357_CS)) && \ + defined(ST7789_CS) || defined(USE_ST7789) || defined(HX8357_CS) || ARCH_PORTDUINO) && \ !defined(DISPLAY_FORCE_SMALL_FONTS) const uint8_t imgQuestionL1[] PROGMEM = {0xff, 0x01, 0x01, 0x32, 0x7b, 0x49, 0x49, 0x6f, 0x26, 0x01, 0x01, 0xff}; const uint8_t imgQuestionL2[] PROGMEM = {0x0f, 0x08, 0x08, 0x08, 0x06, 0x0f, 0x0f, 0x06, 0x08, 0x08, 0x08, 0x0f}; From 91933c66f309bbd5d00d9d9c4ab777dc829f81b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 27 Oct 2024 17:31:10 +0100 Subject: [PATCH 06/20] missed one. --- src/modules/StoreForwardModule.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/StoreForwardModule.cpp b/src/modules/StoreForwardModule.cpp index c111d5299..77e0bf1de 100644 --- a/src/modules/StoreForwardModule.cpp +++ b/src/modules/StoreForwardModule.cpp @@ -103,7 +103,7 @@ void StoreForwardModule::populateSDCard() LOG_INFO("Creating StoreForward directory"); SD.mkdir("/storeforward"); } - this->storageType = StorageType::SDCARD; + this->storageType = StorageType::ST_SDCARD; uint32_t numberOfPackets = (this->records ? this->records : (((SD.totalBytes() / 3) * 2) / sizeof(PacketHistoryStruct))); // only allocate space for one temp copy this->packetHistory = (PacketHistoryStruct *)malloc(sizeof(PacketHistoryStruct)); From 96277ed804580058b315a4d2a59f6af24bca538f Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Sun, 5 Jan 2025 21:19:16 +0100 Subject: [PATCH 07/20] First attempt to adding hardware support for NRF52 SPI SD Card (#5561) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * First attempt to adding hardware support for NRF52 SPI SD Card using arduino SD library My first time contributing to an open source project so not very confident in what i'm doing. Changes to FSCommon: initializing SD library for NRF52. Progress: No compile error, but SD card does not get initialized properly yet added ifdef ARCH_ESP32 conditions around esp32 SD library functions memget: added ifdef conditional statements StoreForwardModule.cpp: added ifdef conditional statements Rak4631 platfromIO.ini and variant.h: added arduino-libraries/SD@^1.3.0 library to libdeps defined HAS_SDCARD and SPI pins Arduino SD library. Made changes to library because using namespace SDLIB in header file caused ambiguity problems Not sure this is the right way of adding a library, also, how do i implement changes to the library permanently to the project? Am I going somewhat in the right direction with these changes? Tell me your thoughts, thanks * replaced arduino SD library to custom fork. A "using namespace" statement in the header file was to messy to work around. NRF52 SD card initialisation added * updated library reference added card size and type function to SD library added populateSDCard for NRF52 * Changed NRF52 SD object from SDFilesystem to SD Changed NRF52 SD object from SDFilesystem to SD for more compatibility with esp32 SD library. Some functions are still different but most used open, read, write and close are the same. * Removed duplicate ESP32/NRF52 SD card access code Mainly made changes to the custom arduino SD library to make it compatible with the esp32 SD library already used in the store and forward code. With these compatible function names and return, I removed some duplicate code. * trunk fmt and pin SD library to commit hash * print this out on ESP32 anyway --------- Co-authored-by: Thomas Göttgens --- src/FSCommon.cpp | 28 ++++++++++++++++++++-------- src/memGet.cpp | 4 ++-- src/modules/StoreForwardModule.cpp | 10 +++++++++- src/xmodem.h | 2 +- variants/rak4631/platformio.ini | 1 + variants/rak4631/variant.h | 13 +++++++++++++ 6 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index df46c1941..f9c96931a 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -14,13 +14,20 @@ #ifdef HAS_SDCARD #include #include - #ifdef SDCARD_USE_SPI1 +#ifdef ARCH_ESP32 SPIClass SPI1(HSPI); -#define SDHandler SPI1 +#endif // ARCH_ESP32 +#ifdef ARCH_NRF52 +#define SDCARD_SPI SPI1 +#endif // NRF52 +#define SDHandler SPI1 // only used for esp32 #else -#define SDHandler SPI -#endif +#ifdef ARCH_NRF52 +#define SDCARD_SPI SPI +#endif // NRF52 +#define SDHandler SPI // only used for esp32 +#endif // SDCARD_USE_SPI1 #endif // HAS_SDCARD @@ -46,7 +53,7 @@ void OSFS::writeNBytes(uint16_t address, unsigned int num, const byte *input) input++; } } -#endif +#endif // ARCH_STM32WL bool lfs_assert_failed = false; // Note: we use this global on all platforms, though it can only be set true on nrf52 (in our modified lfs_util.h) @@ -356,9 +363,11 @@ void fsInit() void setupSDCard() { #ifdef HAS_SDCARD +#if (defined(ARCH_ESP32) || defined(ARCH_NRF52)) +#if (defined(ARCH_ESP32)) SDHandler.begin(SPI_SCK, SPI_MISO, SPI_MOSI); - - if (!SD.begin(SDCARD_CS, SDHandler)) { +#endif + if (!SD.begin(SDCARD_CS, SDHandler)) { // param SDHandler only used for esp32 LOG_DEBUG("No SD_MMC card detected"); return; } @@ -381,6 +390,9 @@ void setupSDCard() uint64_t cardSize = SD.cardSize() / (1024 * 1024); LOG_DEBUG("SD Card Size: %lu MB", (uint32_t)cardSize); LOG_DEBUG("Total space: %lu MB", (uint32_t)(SD.totalBytes() / (1024 * 1024))); +#if (defined(ARCH_ESP32)) // not implemented in arduino sd library LOG_DEBUG("Used space: %lu MB", (uint32_t)(SD.usedBytes() / (1024 * 1024))); #endif -} \ No newline at end of file +#endif +#endif +} diff --git a/src/memGet.cpp b/src/memGet.cpp index 33fb94e1c..c7078fac4 100644 --- a/src/memGet.cpp +++ b/src/memGet.cpp @@ -57,7 +57,7 @@ uint32_t MemGet::getFreePsram() { #ifdef ARCH_ESP32 return ESP.getFreePsram(); -#elif defined(HAS_SDCARD) +#elif (defined(HAS_SDCARD) && defined(ARCH_ESP32)) return SD.totalBytes() - SD.usedBytes(); #elif defined(ARCH_PORTDUINO) return 4194252; @@ -75,7 +75,7 @@ uint32_t MemGet::getPsramSize() { #ifdef ARCH_ESP32 return ESP.getPsramSize(); -#elif defined(HAS_SDCARD) +#elif (defined(HAS_SDCARD) && defined(ARCH_ESP32)) return SD.totalBytes(); #elif defined(ARCH_PORTDUINO) return 4194252; diff --git a/src/modules/StoreForwardModule.cpp b/src/modules/StoreForwardModule.cpp index 4a762a1c3..928f11643 100644 --- a/src/modules/StoreForwardModule.cpp +++ b/src/modules/StoreForwardModule.cpp @@ -98,6 +98,7 @@ void StoreForwardModule::populatePSRAM() void StoreForwardModule::populateSDCard() { #if defined(HAS_SDCARD) +#if (defined(ARCH_ESP32) || defined(ARCH_NRF52)) if (SD.cardType() != CARD_NONE) { if (!SD.exists("/storeforward")) { LOG_INFO("Creating StoreForward directory"); @@ -109,7 +110,8 @@ void StoreForwardModule::populateSDCard() this->packetHistory = (PacketHistoryStruct *)malloc(sizeof(PacketHistoryStruct)); LOG_DEBUG("numberOfPackets for packetHistory - %u", numberOfPackets); } -#endif +#endif // ARCH_ESP32 || ARCH_NRF52 +#endif // HAS_SDCARD } /** @@ -166,6 +168,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) auto handler = SD.open("/storeforward/" + String(i), FILE_READ); if (handler) { handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); @@ -178,6 +181,7 @@ uint32_t StoreForwardModule::getNumAvailablePackets(NodeNum dest, uint32_t last_ } } } +#endif #endif } else { LOG_ERROR("S&F: Unknown storage type"); @@ -248,6 +252,7 @@ void StoreForwardModule::historyAdd(const meshtastic_MeshPacket &mp) } else if (this->storageType == StorageType::ST_SDCARD) { // Save to SDCARD #if defined(HAS_SDCARD) +#if defined(ARCH_ESP32) || defined(ARCH_NRF52) this->packetHistory[0].time = getTime(); this->packetHistory[0].to = mp.to; this->packetHistory[0].channel = mp.channel; @@ -260,6 +265,7 @@ void StoreForwardModule::historyAdd(const meshtastic_MeshPacket &mp) auto handler = SD.open("/storeforward/" + String(this->packetHistoryTotalCount), FILE_WRITE); handler.write((uint8_t *)&this->packetHistory, sizeof(PacketHistoryStruct)); handler.close(); +#endif #endif } else { LOG_ERROR("S&F: Unknown storage type"); @@ -346,6 +352,7 @@ 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) auto handler = SD.open("/storeforward/" + String(i), FILE_READ); if (handler) { handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); @@ -389,6 +396,7 @@ meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t } } } +#endif #endif } else { LOG_ERROR("S&F: Unknown storage type"); diff --git a/src/xmodem.h b/src/xmodem.h index 4cfcb43e1..ae1cca7cb 100644 --- a/src/xmodem.h +++ b/src/xmodem.h @@ -62,7 +62,7 @@ class XModemAdapter uint16_t packetno = 0; #if defined(ARCH_NRF52) || defined(ARCH_STM32WL) - File file = File(FSCom); + Adafruit_LittleFS_Namespace::File file = Adafruit_LittleFS_Namespace::File(FSCom); #else File file; #endif diff --git a/variants/rak4631/platformio.ini b/variants/rak4631/platformio.ini index ced93df66..c423264fe 100644 --- a/variants/rak4631/platformio.ini +++ b/variants/rak4631/platformio.ini @@ -20,6 +20,7 @@ lib_deps = https://github.com/RAKWireless/RAK13800-W5100S.git#1.0.2 rakwireless/RAKwireless NCP5623 RGB LED library@^1.0.2 https://github.com/RAKWireless/RAK12034-BMX160.git#dcead07ffa267d3c906e9ca4a1330ab989e957e2 + https://github.com/Woutvstk/arduino_SD.git#909c2a4a14f5b053bb69958709f0c874aa358c38 ;library to acces SD card ; If not set we will default to uploading over serial (first it forces bootloader entry by talking 1200bps to cdcacm) ; Note: as of 6/2013 the serial/bootloader based programming takes approximately 30 seconds diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index bc5541336..f5a60fdee 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -122,6 +122,19 @@ static const uint8_t MOSI = PIN_SPI_MOSI; static const uint8_t MISO = PIN_SPI_MISO; static const uint8_t SCK = PIN_SPI_SCK; +// SD card SPI pin definitions + +#define HAS_SDCARD 1 +#define SDCARD_USE_SPI1 1 + +#ifdef SDCARD_USE_SPI1 +#define SDCARD_SPI SPI1 +#endif +#define SPI_MOSI PIN_SPI1_MOSI +#define SPI_SCK PIN_SPI1_SCK +#define SPI_MISO PIN_SPI1_MISO +#define SDCARD_CS (26) + /* * eink display pins */ 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 08/20] 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 From a6aa84431c3e245c786dc9de5066ad167fb85667 Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Fri, 31 Jan 2025 14:08:57 +0100 Subject: [PATCH 09/20] added quotes against linking issues in platformio.ini issue #5898 (#5966) --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index ea4de4db1..1c51e53b4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,7 +20,7 @@ extra_scripts = bin/platformio-custom.py build_flags = -Wno-missing-field-initializers -Wno-format - -Isrc -Isrc/mesh -Isrc/mesh/generated -Isrc/gps -Isrc/buzz -Wl,-Map,${platformio.build_dir}/output.map + -Isrc -Isrc/mesh -Isrc/mesh/generated -Isrc/gps -Isrc/buzz -Wl,-Map,"${platformio.build_dir}"/output.map -DUSE_THREAD_NAMES -DTINYGPS_OPTION_NO_CUSTOM_FIELDS -DPB_ENABLE_MALLOC=1 From 443922b947109688e140ad025a7ba2441504d0fc Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Tue, 18 Feb 2025 09:08:03 +0100 Subject: [PATCH 10/20] Reorganize spi definitions for use with sd cards ESP32/NRF52 (#6080) Changed variant files of hardware that uses HAS_SDCARD. Reorganised SPIClass definitions in FSCommon --- src/FSCommon.cpp | 27 +++++++++----------------- variants/CDEBYTE_EoRa-S3/variant.h | 2 +- variants/bpi_picow_esp32_s3/variant.h | 2 +- variants/dreamcatcher/variant.h | 2 +- variants/mesh-tab/variant.h | 1 + variants/my_esp32s3_diy_eink/variant.h | 2 +- variants/my_esp32s3_diy_oled/variant.h | 2 +- variants/rak4631/variant.h | 11 +++-------- variants/t-deck/variant.h | 1 + variants/tbeam-s3-core/variant.h | 2 +- variants/tlora_t3s3_epaper/variant.h | 2 +- variants/tlora_t3s3_v1/variant.h | 5 +---- variants/unphone/variant.h | 1 + 13 files changed, 23 insertions(+), 37 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 3b659b67e..de76ce29d 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -16,25 +16,16 @@ #include "SPILock.h" #include #include -#ifndef SDCARD_USE_HSPI // old ESP32 -#ifdef SDCARD_USE_SPI1 -#ifdef ARCH_ESP32 -SPIClass SPI1(HSPI); -#endif // ARCH_ESP32 -#ifdef ARCH_NRF52 -#define SDCARD_SPI SPI1 -#endif // NRF52 -#define SDHandler SPI1 // only used for esp32 -#else -#ifdef ARCH_NRF52 -#define SDCARD_SPI SPI -#endif // NRF52 -#define SDHandler SPI // only used for esp32 -#endif // SDCARD_USE_SPI1 -#else +#if defined(ARCH_ESP32) +#if defined(SDCARD_USE_HSPI) SPIClass SDHandler = SPIClass(HSPI); +#elif defined(SDCARD_USE_VSPI) +SPIClass SDHandler = SPIClass(VSPI); #endif -#endif // HAS_SDCARD +#elif defined(ARCH_NRF52) +#define SDHandler SPI // only used for esp32 +#endif // ESP32/NRF52 +#endif // HAS_SDCARD #if defined(ARCH_STM32WL) @@ -403,4 +394,4 @@ void setupSDCard() #endif #endif #endif - +} diff --git a/variants/CDEBYTE_EoRa-S3/variant.h b/variants/CDEBYTE_EoRa-S3/variant.h index 5da99667b..854996332 100644 --- a/variants/CDEBYTE_EoRa-S3/variant.h +++ b/variants/CDEBYTE_EoRa-S3/variant.h @@ -6,7 +6,7 @@ // SD card - TODO: test, currently untested, copied from T3S3 variant #define HAS_SDCARD -#define SDCARD_USE_SPI1 +#define SDCARD_USE_HSPI // TODO: rename this to make this SD-card specific #define SPI_CS 13 #define SPI_SCK 14 diff --git a/variants/bpi_picow_esp32_s3/variant.h b/variants/bpi_picow_esp32_s3/variant.h index d8d9413d7..fc9064436 100644 --- a/variants/bpi_picow_esp32_s3/variant.h +++ b/variants/bpi_picow_esp32_s3/variant.h @@ -5,7 +5,7 @@ // #define HAS_SCREEN 0 // #define HAS_SDCARD -// #define SDCARD_USE_SPI1 +// #define SDCARD_USE_HSPI #define USE_SSD1306 #define I2C_SDA 12 diff --git a/variants/dreamcatcher/variant.h b/variants/dreamcatcher/variant.h index 7835979e1..1e04a2b26 100644 --- a/variants/dreamcatcher/variant.h +++ b/variants/dreamcatcher/variant.h @@ -70,7 +70,7 @@ #endif #define HAS_SDCARD // Have SPI interface SD card slot -#define SDCARD_USE_SPI1 +#define SDCARD_USE_HSPI #define LORA_RESET 3 #define LORA_SCK 12 diff --git a/variants/mesh-tab/variant.h b/variants/mesh-tab/variant.h index 533c931bc..a912aa3af 100644 --- a/variants/mesh-tab/variant.h +++ b/variants/mesh-tab/variant.h @@ -25,6 +25,7 @@ #define GPS_TX_PIN 17 // #define HAS_SDCARD 1 +// #define SDCARD_USE_HSPI #define SPI_MOSI 13 #define SPI_SCK 12 #define SPI_MISO 11 diff --git a/variants/my_esp32s3_diy_eink/variant.h b/variants/my_esp32s3_diy_eink/variant.h index 024f912dd..c60e2b0ce 100644 --- a/variants/my_esp32s3_diy_eink/variant.h +++ b/variants/my_esp32s3_diy_eink/variant.h @@ -4,7 +4,7 @@ // #define HAS_SCREEN 0 // #define HAS_SDCARD -// #define SDCARD_USE_SPI1 +// #define SDCARD_USE_HSPI // #define USE_SSD1306 diff --git a/variants/my_esp32s3_diy_oled/variant.h b/variants/my_esp32s3_diy_oled/variant.h index 8a3a39003..dcaabe64d 100644 --- a/variants/my_esp32s3_diy_oled/variant.h +++ b/variants/my_esp32s3_diy_oled/variant.h @@ -4,7 +4,7 @@ // #define HAS_SCREEN 0 // #define HAS_SDCARD -// #define SDCARD_USE_SPI1 +// #define SDCARD_USE_HSPI #define USE_SSD1306 diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index 47c403584..9d1373dad 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -121,14 +121,9 @@ static const uint8_t SCK = PIN_SPI_SCK; // SD card SPI pin definitions #define HAS_SDCARD 1 -#define SDCARD_USE_SPI1 1 - -#ifdef SDCARD_USE_SPI1 -#define SDCARD_SPI SPI1 -#endif -#define SPI_MOSI PIN_SPI1_MOSI -#define SPI_SCK PIN_SPI1_SCK -#define SPI_MISO PIN_SPI1_MISO +#define SPI_MOSI PIN_SPI_MOSI +#define SPI_SCK PIN_SPI_SCK +#define SPI_MISO PIN_SPI_MISO #define SDCARD_CS (26) /* diff --git a/variants/t-deck/variant.h b/variants/t-deck/variant.h index 4aeeb7ca8..69b2a7a9f 100644 --- a/variants/t-deck/variant.h +++ b/variants/t-deck/variant.h @@ -37,6 +37,7 @@ // Have SPI interface SD card slot #define HAS_SDCARD 1 +#define SDCARD_USE_HSPI #define SPI_MOSI (41) #define SPI_SCK (40) #define SPI_MISO (38) diff --git a/variants/tbeam-s3-core/variant.h b/variants/tbeam-s3-core/variant.h index cc706459f..fd7649265 100644 --- a/variants/tbeam-s3-core/variant.h +++ b/variants/tbeam-s3-core/variant.h @@ -58,7 +58,7 @@ #define GPS_1PPS_PIN 6 #define HAS_SDCARD // Have SPI interface SD card slot -#define SDCARD_USE_SPI1 +#define SDCARD_USE_HSPI // PCF8563 RTC Module // #define PCF8563_RTC 0x51 //Putting definitions in variant. h does not compile correctly diff --git a/variants/tlora_t3s3_epaper/variant.h b/variants/tlora_t3s3_epaper/variant.h index 732869b20..1dad897cf 100644 --- a/variants/tlora_t3s3_epaper/variant.h +++ b/variants/tlora_t3s3_epaper/variant.h @@ -1,5 +1,5 @@ #define HAS_SDCARD -#define SDCARD_USE_SPI1 +#define SDCARD_USE_HSPI // Display (E-Ink) #define USE_EINK diff --git a/variants/tlora_t3s3_v1/variant.h b/variants/tlora_t3s3_v1/variant.h index babe44a58..06ee0292a 100644 --- a/variants/tlora_t3s3_v1/variant.h +++ b/variants/tlora_t3s3_v1/variant.h @@ -1,5 +1,5 @@ #define HAS_SDCARD -#define SDCARD_USE_SPI1 +#define SDCARD_USE_HSPI #define USE_SSD1306 @@ -74,6 +74,3 @@ #define LR11X0_DIO3_TCXO_VOLTAGE 3.0 #define LR11X0_DIO_AS_RF_SWITCH #endif - -#define HAS_SDCARD // Have SPI interface SD card slot -#define SDCARD_USE_SPI1 \ No newline at end of file diff --git a/variants/unphone/variant.h b/variants/unphone/variant.h index 0a94c5987..b5fdf5631 100644 --- a/variants/unphone/variant.h +++ b/variants/unphone/variant.h @@ -49,6 +49,7 @@ #undef GPS_TX_PIN // #define HAS_SDCARD 1 // causes hang if defined +#define SDCARD_USE_HSPI #define SDCARD_CS 43 #define LED_PIN 13 // the red part of the RGB LED From 5aa4946e6fd249eb68d1af4acfbae1ce19b1ab86 Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Sun, 23 Feb 2025 16:56:22 +0100 Subject: [PATCH 11/20] Revert "Reorganize spi definitions for use with sd cards ESP32/NRF52 (#6080)" This reverts commit 443922b947109688e140ad025a7ba2441504d0fc. --- src/FSCommon.cpp | 29 +++++++++++++++++--------- variants/CDEBYTE_EoRa-S3/variant.h | 2 +- variants/bpi_picow_esp32_s3/variant.h | 2 +- variants/dreamcatcher/variant.h | 2 +- variants/mesh-tab/variant.h | 1 - variants/my_esp32s3_diy_eink/variant.h | 2 +- variants/my_esp32s3_diy_oled/variant.h | 2 +- variants/rak4631/variant.h | 11 +++++++--- variants/t-deck/variant.h | 1 - variants/tbeam-s3-core/variant.h | 2 +- variants/tlora_t3s3_epaper/variant.h | 2 +- variants/tlora_t3s3_v1/variant.h | 5 ++++- variants/unphone/variant.h | 1 - 13 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index de76ce29d..3b659b67e 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -16,16 +16,25 @@ #include "SPILock.h" #include #include -#if defined(ARCH_ESP32) -#if defined(SDCARD_USE_HSPI) -SPIClass SDHandler = SPIClass(HSPI); -#elif defined(SDCARD_USE_VSPI) -SPIClass SDHandler = SPIClass(VSPI); -#endif -#elif defined(ARCH_NRF52) +#ifndef SDCARD_USE_HSPI // old ESP32 +#ifdef SDCARD_USE_SPI1 +#ifdef ARCH_ESP32 +SPIClass SPI1(HSPI); +#endif // ARCH_ESP32 +#ifdef ARCH_NRF52 +#define SDCARD_SPI SPI1 +#endif // NRF52 +#define SDHandler SPI1 // only used for esp32 +#else +#ifdef ARCH_NRF52 +#define SDCARD_SPI SPI +#endif // NRF52 #define SDHandler SPI // only used for esp32 -#endif // ESP32/NRF52 -#endif // HAS_SDCARD +#endif // SDCARD_USE_SPI1 +#else +SPIClass SDHandler = SPIClass(HSPI); +#endif +#endif // HAS_SDCARD #if defined(ARCH_STM32WL) @@ -394,4 +403,4 @@ void setupSDCard() #endif #endif #endif -} + diff --git a/variants/CDEBYTE_EoRa-S3/variant.h b/variants/CDEBYTE_EoRa-S3/variant.h index 854996332..5da99667b 100644 --- a/variants/CDEBYTE_EoRa-S3/variant.h +++ b/variants/CDEBYTE_EoRa-S3/variant.h @@ -6,7 +6,7 @@ // SD card - TODO: test, currently untested, copied from T3S3 variant #define HAS_SDCARD -#define SDCARD_USE_HSPI +#define SDCARD_USE_SPI1 // TODO: rename this to make this SD-card specific #define SPI_CS 13 #define SPI_SCK 14 diff --git a/variants/bpi_picow_esp32_s3/variant.h b/variants/bpi_picow_esp32_s3/variant.h index fc9064436..d8d9413d7 100644 --- a/variants/bpi_picow_esp32_s3/variant.h +++ b/variants/bpi_picow_esp32_s3/variant.h @@ -5,7 +5,7 @@ // #define HAS_SCREEN 0 // #define HAS_SDCARD -// #define SDCARD_USE_HSPI +// #define SDCARD_USE_SPI1 #define USE_SSD1306 #define I2C_SDA 12 diff --git a/variants/dreamcatcher/variant.h b/variants/dreamcatcher/variant.h index 1e04a2b26..7835979e1 100644 --- a/variants/dreamcatcher/variant.h +++ b/variants/dreamcatcher/variant.h @@ -70,7 +70,7 @@ #endif #define HAS_SDCARD // Have SPI interface SD card slot -#define SDCARD_USE_HSPI +#define SDCARD_USE_SPI1 #define LORA_RESET 3 #define LORA_SCK 12 diff --git a/variants/mesh-tab/variant.h b/variants/mesh-tab/variant.h index a912aa3af..533c931bc 100644 --- a/variants/mesh-tab/variant.h +++ b/variants/mesh-tab/variant.h @@ -25,7 +25,6 @@ #define GPS_TX_PIN 17 // #define HAS_SDCARD 1 -// #define SDCARD_USE_HSPI #define SPI_MOSI 13 #define SPI_SCK 12 #define SPI_MISO 11 diff --git a/variants/my_esp32s3_diy_eink/variant.h b/variants/my_esp32s3_diy_eink/variant.h index c60e2b0ce..024f912dd 100644 --- a/variants/my_esp32s3_diy_eink/variant.h +++ b/variants/my_esp32s3_diy_eink/variant.h @@ -4,7 +4,7 @@ // #define HAS_SCREEN 0 // #define HAS_SDCARD -// #define SDCARD_USE_HSPI +// #define SDCARD_USE_SPI1 // #define USE_SSD1306 diff --git a/variants/my_esp32s3_diy_oled/variant.h b/variants/my_esp32s3_diy_oled/variant.h index dcaabe64d..8a3a39003 100644 --- a/variants/my_esp32s3_diy_oled/variant.h +++ b/variants/my_esp32s3_diy_oled/variant.h @@ -4,7 +4,7 @@ // #define HAS_SCREEN 0 // #define HAS_SDCARD -// #define SDCARD_USE_HSPI +// #define SDCARD_USE_SPI1 #define USE_SSD1306 diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index 9d1373dad..47c403584 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -121,9 +121,14 @@ static const uint8_t SCK = PIN_SPI_SCK; // SD card SPI pin definitions #define HAS_SDCARD 1 -#define SPI_MOSI PIN_SPI_MOSI -#define SPI_SCK PIN_SPI_SCK -#define SPI_MISO PIN_SPI_MISO +#define SDCARD_USE_SPI1 1 + +#ifdef SDCARD_USE_SPI1 +#define SDCARD_SPI SPI1 +#endif +#define SPI_MOSI PIN_SPI1_MOSI +#define SPI_SCK PIN_SPI1_SCK +#define SPI_MISO PIN_SPI1_MISO #define SDCARD_CS (26) /* diff --git a/variants/t-deck/variant.h b/variants/t-deck/variant.h index 69b2a7a9f..4aeeb7ca8 100644 --- a/variants/t-deck/variant.h +++ b/variants/t-deck/variant.h @@ -37,7 +37,6 @@ // Have SPI interface SD card slot #define HAS_SDCARD 1 -#define SDCARD_USE_HSPI #define SPI_MOSI (41) #define SPI_SCK (40) #define SPI_MISO (38) diff --git a/variants/tbeam-s3-core/variant.h b/variants/tbeam-s3-core/variant.h index fd7649265..cc706459f 100644 --- a/variants/tbeam-s3-core/variant.h +++ b/variants/tbeam-s3-core/variant.h @@ -58,7 +58,7 @@ #define GPS_1PPS_PIN 6 #define HAS_SDCARD // Have SPI interface SD card slot -#define SDCARD_USE_HSPI +#define SDCARD_USE_SPI1 // PCF8563 RTC Module // #define PCF8563_RTC 0x51 //Putting definitions in variant. h does not compile correctly diff --git a/variants/tlora_t3s3_epaper/variant.h b/variants/tlora_t3s3_epaper/variant.h index 1dad897cf..732869b20 100644 --- a/variants/tlora_t3s3_epaper/variant.h +++ b/variants/tlora_t3s3_epaper/variant.h @@ -1,5 +1,5 @@ #define HAS_SDCARD -#define SDCARD_USE_HSPI +#define SDCARD_USE_SPI1 // Display (E-Ink) #define USE_EINK diff --git a/variants/tlora_t3s3_v1/variant.h b/variants/tlora_t3s3_v1/variant.h index 06ee0292a..babe44a58 100644 --- a/variants/tlora_t3s3_v1/variant.h +++ b/variants/tlora_t3s3_v1/variant.h @@ -1,5 +1,5 @@ #define HAS_SDCARD -#define SDCARD_USE_HSPI +#define SDCARD_USE_SPI1 #define USE_SSD1306 @@ -74,3 +74,6 @@ #define LR11X0_DIO3_TCXO_VOLTAGE 3.0 #define LR11X0_DIO_AS_RF_SWITCH #endif + +#define HAS_SDCARD // Have SPI interface SD card slot +#define SDCARD_USE_SPI1 \ No newline at end of file diff --git a/variants/unphone/variant.h b/variants/unphone/variant.h index b5fdf5631..0a94c5987 100644 --- a/variants/unphone/variant.h +++ b/variants/unphone/variant.h @@ -49,7 +49,6 @@ #undef GPS_TX_PIN // #define HAS_SDCARD 1 // causes hang if defined -#define SDCARD_USE_HSPI #define SDCARD_CS 43 #define LED_PIN 13 // the red part of the RGB LED From 4ec013460661f847d73903f83c4a3c61312d56b6 Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Sun, 23 Feb 2025 16:56:39 +0100 Subject: [PATCH 12/20] Revert "Rak4631 remove spi1 (#6042)" This reverts commit 9b46cb4ef08688a2f424c76d8425561e4f5db844. --- src/detect/einkScan.h | 16 ++++++++-------- variants/rak4631/variant.h | 18 +++++++++++------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/detect/einkScan.h b/src/detect/einkScan.h index 5bc218d00..d20c7b6e5 100644 --- a/src/detect/einkScan.h +++ b/src/detect/einkScan.h @@ -6,28 +6,28 @@ void d_writeCommand(uint8_t c) { - SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); + SPI1.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); if (PIN_EINK_DC >= 0) digitalWrite(PIN_EINK_DC, LOW); if (PIN_EINK_CS >= 0) digitalWrite(PIN_EINK_CS, LOW); - SPI.transfer(c); + SPI1.transfer(c); if (PIN_EINK_CS >= 0) digitalWrite(PIN_EINK_CS, HIGH); if (PIN_EINK_DC >= 0) digitalWrite(PIN_EINK_DC, HIGH); - SPI.endTransaction(); + SPI1.endTransaction(); } void d_writeData(uint8_t d) { - SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); + SPI1.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); if (PIN_EINK_CS >= 0) digitalWrite(PIN_EINK_CS, LOW); - SPI.transfer(d); + SPI1.transfer(d); if (PIN_EINK_CS >= 0) digitalWrite(PIN_EINK_CS, HIGH); - SPI.endTransaction(); + SPI1.endTransaction(); } unsigned long d_waitWhileBusy(uint16_t busy_time) @@ -53,7 +53,7 @@ unsigned long d_waitWhileBusy(uint16_t busy_time) void scanEInkDevice(void) { - SPI.begin(); + SPI1.begin(); d_writeCommand(0x22); d_writeData(0x83); d_writeCommand(0x20); @@ -62,6 +62,6 @@ void scanEInkDevice(void) LOG_DEBUG("EInk display found"); else LOG_DEBUG("EInk display not found"); - SPI.end(); + SPI1.end(); } #endif \ No newline at end of file diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index 47c403584..f5a60fdee 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -107,11 +107,15 @@ static const uint8_t AREF = PIN_AREF; /* * SPI Interfaces */ -#define SPI_INTERFACES_COUNT 1 +#define SPI_INTERFACES_COUNT 2 -#define PIN_SPI_MISO (29) -#define PIN_SPI_MOSI (30) -#define PIN_SPI_SCK (3) +#define PIN_SPI_MISO (45) +#define PIN_SPI_MOSI (44) +#define PIN_SPI_SCK (43) + +#define PIN_SPI1_MISO (29) // (0 + 29) +#define PIN_SPI1_MOSI (30) // (0 + 30) +#define PIN_SPI1_SCK (3) // (0 + 3) static const uint8_t SS = 42; static const uint8_t MOSI = PIN_SPI_MOSI; @@ -139,8 +143,8 @@ static const uint8_t SCK = PIN_SPI_SCK; #define PIN_EINK_BUSY (0 + 4) #define PIN_EINK_DC (0 + 17) #define PIN_EINK_RES (-1) -#define PIN_EINK_SCLK PIN_SPI_SCK -#define PIN_EINK_MOSI PIN_SPI_MOSI // also called SDI +#define PIN_EINK_SCLK (0 + 3) +#define PIN_EINK_MOSI (0 + 30) // also called SDI // #define USE_EINK @@ -268,7 +272,7 @@ SO GPIO 39/TXEN MAY NOT BE DEFINED FOR SUCCESSFUL OPERATION OF THE SX1262 - TG #define PIN_ETHERNET_RESET 21 #define PIN_ETHERNET_SS PIN_EINK_CS -#define ETH_SPI_PORT SPI +#define ETH_SPI_PORT SPI1 #define AQ_SET_PIN 10 #ifdef __cplusplus From b19d358dccc8284ad4f7294193d584c4c64a3e67 Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Sun, 23 Feb 2025 17:23:35 +0100 Subject: [PATCH 13/20] redid the reorganisation of the SPI definitions --- src/FSCommon.cpp | 30 +++++++++++--------------- variants/CDEBYTE_EoRa-S3/variant.h | 2 +- variants/bpi_picow_esp32_s3/variant.h | 2 +- variants/dreamcatcher/variant.h | 2 +- variants/my_esp32s3_diy_eink/variant.h | 2 +- variants/tbeam-s3-core/variant.h | 2 +- variants/tlora_t3s3_epaper/variant.h | 2 +- variants/tlora_t3s3_v1/variant.h | 4 ++-- 8 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 3b659b67e..64490405f 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -16,25 +16,20 @@ #include "SPILock.h" #include #include -#ifndef SDCARD_USE_HSPI // old ESP32 -#ifdef SDCARD_USE_SPI1 -#ifdef ARCH_ESP32 -SPIClass SPI1(HSPI); -#endif // ARCH_ESP32 -#ifdef ARCH_NRF52 -#define SDCARD_SPI SPI1 -#endif // NRF52 -#define SDHandler SPI1 // only used for esp32 -#else -#ifdef ARCH_NRF52 -#define SDCARD_SPI SPI -#endif // NRF52 -#define SDHandler SPI // only used for esp32 -#endif // SDCARD_USE_SPI1 -#else +#if defined(ARCH_ESP32) +#if defined(SDCARD_USE_HSPI) SPIClass SDHandler = SPIClass(HSPI); +#elif defined(SDCARD_USE_VSPI) +SPIClass SDHandler = SPIClass(VSPI); #endif -#endif // HAS_SDCARD +#elif defined(ARCH_NRF52) +#if defined(SDCARD_USE_SPI1) +#define SDHandler SPI1 // only used for esp32, SPI selection for NRF52 happens in variant.h (for now) +#elif defined(SDCARD_USE_SPI) +#define SDHandler SPI // only used for esp32 +#endif //NRF52 SPI or SPI1 +#endif // ESP32/NRF52 +#endif // HAS_SDCARD #if defined(ARCH_STM32WL) @@ -404,3 +399,4 @@ void setupSDCard() #endif #endif +} \ No newline at end of file diff --git a/variants/CDEBYTE_EoRa-S3/variant.h b/variants/CDEBYTE_EoRa-S3/variant.h index 5da99667b..854996332 100644 --- a/variants/CDEBYTE_EoRa-S3/variant.h +++ b/variants/CDEBYTE_EoRa-S3/variant.h @@ -6,7 +6,7 @@ // SD card - TODO: test, currently untested, copied from T3S3 variant #define HAS_SDCARD -#define SDCARD_USE_SPI1 +#define SDCARD_USE_HSPI // TODO: rename this to make this SD-card specific #define SPI_CS 13 #define SPI_SCK 14 diff --git a/variants/bpi_picow_esp32_s3/variant.h b/variants/bpi_picow_esp32_s3/variant.h index d8d9413d7..fc9064436 100644 --- a/variants/bpi_picow_esp32_s3/variant.h +++ b/variants/bpi_picow_esp32_s3/variant.h @@ -5,7 +5,7 @@ // #define HAS_SCREEN 0 // #define HAS_SDCARD -// #define SDCARD_USE_SPI1 +// #define SDCARD_USE_HSPI #define USE_SSD1306 #define I2C_SDA 12 diff --git a/variants/dreamcatcher/variant.h b/variants/dreamcatcher/variant.h index 7835979e1..1e04a2b26 100644 --- a/variants/dreamcatcher/variant.h +++ b/variants/dreamcatcher/variant.h @@ -70,7 +70,7 @@ #endif #define HAS_SDCARD // Have SPI interface SD card slot -#define SDCARD_USE_SPI1 +#define SDCARD_USE_HSPI #define LORA_RESET 3 #define LORA_SCK 12 diff --git a/variants/my_esp32s3_diy_eink/variant.h b/variants/my_esp32s3_diy_eink/variant.h index 024f912dd..c60e2b0ce 100644 --- a/variants/my_esp32s3_diy_eink/variant.h +++ b/variants/my_esp32s3_diy_eink/variant.h @@ -4,7 +4,7 @@ // #define HAS_SCREEN 0 // #define HAS_SDCARD -// #define SDCARD_USE_SPI1 +// #define SDCARD_USE_HSPI // #define USE_SSD1306 diff --git a/variants/tbeam-s3-core/variant.h b/variants/tbeam-s3-core/variant.h index cc706459f..fd7649265 100644 --- a/variants/tbeam-s3-core/variant.h +++ b/variants/tbeam-s3-core/variant.h @@ -58,7 +58,7 @@ #define GPS_1PPS_PIN 6 #define HAS_SDCARD // Have SPI interface SD card slot -#define SDCARD_USE_SPI1 +#define SDCARD_USE_HSPI // PCF8563 RTC Module // #define PCF8563_RTC 0x51 //Putting definitions in variant. h does not compile correctly diff --git a/variants/tlora_t3s3_epaper/variant.h b/variants/tlora_t3s3_epaper/variant.h index 732869b20..1dad897cf 100644 --- a/variants/tlora_t3s3_epaper/variant.h +++ b/variants/tlora_t3s3_epaper/variant.h @@ -1,5 +1,5 @@ #define HAS_SDCARD -#define SDCARD_USE_SPI1 +#define SDCARD_USE_HSPI // Display (E-Ink) #define USE_EINK diff --git a/variants/tlora_t3s3_v1/variant.h b/variants/tlora_t3s3_v1/variant.h index babe44a58..63caaaf12 100644 --- a/variants/tlora_t3s3_v1/variant.h +++ b/variants/tlora_t3s3_v1/variant.h @@ -1,5 +1,5 @@ #define HAS_SDCARD -#define SDCARD_USE_SPI1 +#define SDCARD_USE_HSPI #define USE_SSD1306 @@ -76,4 +76,4 @@ #endif #define HAS_SDCARD // Have SPI interface SD card slot -#define SDCARD_USE_SPI1 \ No newline at end of file +#define SDCARD_USE_HSPI \ No newline at end of file From 95fe4aed047e58b60ad0b20e0c838af825a76409 Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Sun, 23 Feb 2025 18:17:24 +0100 Subject: [PATCH 14/20] trunk fmt --- src/FSCommon.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 64490405f..ef3c4e50b 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -24,10 +24,10 @@ SPIClass SDHandler = SPIClass(VSPI); #endif #elif defined(ARCH_NRF52) #if defined(SDCARD_USE_SPI1) -#define SDHandler SPI1 // only used for esp32, SPI selection for NRF52 happens in variant.h (for now) +#define SDHandler SPI1 // only used for esp32, SPI selection for NRF52 happens in variant.h (for now) #elif defined(SDCARD_USE_SPI) -#define SDHandler SPI // only used for esp32 -#endif //NRF52 SPI or SPI1 +#define SDHandler SPI // only used for esp32 +#endif // NRF52 SPI or SPI1 #endif // ESP32/NRF52 #endif // HAS_SDCARD @@ -398,5 +398,4 @@ void setupSDCard() #endif #endif #endif - } \ No newline at end of file From 372b62aa35074d395173b363dd6c42958edb90e1 Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Mon, 24 Feb 2025 12:45:44 +0100 Subject: [PATCH 15/20] remove second initSPI() (#6140) --- src/main.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8e72f66a3..c1c2c06e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -611,8 +611,7 @@ void setup() i2cScanner.reset(); #endif - // Init our SPI controller (must be before screen and lora) - initSPI(); + // initSPI() must have called at this point (must be before screen and lora) #ifdef HAS_SDCARD setupSDCard(); From db376532ad2f3df863c43467c29668862db0dd6b Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Tue, 25 Feb 2025 15:22:03 +0100 Subject: [PATCH 16/20] Add SPIM3/SPIM2 for SPI/SPI1 selection The SPIM3 is faster (max 32Mhz) than SPIM2 (max 8Mhz) Default is SPI_32MHZ_INTERFACE 0 in SPI library --- variants/rak4631/variant.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index f5a60fdee..d65952636 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -108,11 +108,14 @@ static const uint8_t AREF = PIN_AREF; * SPI Interfaces */ #define SPI_INTERFACES_COUNT 2 +#define SPI_32MHZ_INTERFACE 0 //0: use SPIM3 for SPI and SPIM2 for SPI1; 1: the opposite +//SPI pins for SX1262 #define PIN_SPI_MISO (45) #define PIN_SPI_MOSI (44) #define PIN_SPI_SCK (43) +//SPI1 pins for external(rak4630) spi (incl. SDCard) #define PIN_SPI1_MISO (29) // (0 + 29) #define PIN_SPI1_MOSI (30) // (0 + 30) #define PIN_SPI1_SCK (3) // (0 + 3) From f0ca0b947cd71649d92407a89b9742f463aa7f07 Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Tue, 25 Feb 2025 16:04:59 +0100 Subject: [PATCH 17/20] trunk fmt --- variants/rak4631/variant.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index d65952636..ac8130943 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -108,14 +108,14 @@ static const uint8_t AREF = PIN_AREF; * SPI Interfaces */ #define SPI_INTERFACES_COUNT 2 -#define SPI_32MHZ_INTERFACE 0 //0: use SPIM3 for SPI and SPIM2 for SPI1; 1: the opposite +#define SPI_32MHZ_INTERFACE 0 // 0: use SPIM3 for SPI and SPIM2 for SPI1; 1: the opposite -//SPI pins for SX1262 +// SPI pins for SX1262 #define PIN_SPI_MISO (45) #define PIN_SPI_MOSI (44) #define PIN_SPI_SCK (43) -//SPI1 pins for external(rak4630) spi (incl. SDCard) +// SPI1 pins for external(rak4630) spi (incl. SDCard) #define PIN_SPI1_MISO (29) // (0 + 29) #define PIN_SPI1_MOSI (30) // (0 + 30) #define PIN_SPI1_SCK (3) // (0 + 3) From 7e7792aa51e8f597a67c893bd2db85fb0dcfbf26 Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Sat, 29 Mar 2025 13:15:21 +0100 Subject: [PATCH 18/20] Changed SD library for nrf52 and got S&F working (#6382) - Changed library from deprecated arduino SD library to wrapper of SdFat library - Fixed some bugs in the S&F code for SD card storage Now the S&F functionality works on nrf52 (tested on rak4631 with RAK15002) --- src/FSCommon.cpp | 21 +++++++++++---------- src/modules/Modules.cpp | 2 +- src/modules/StoreForwardModule.cpp | 12 +++++++++--- variants/rak4631/platformio.ini | 2 +- variants/rak4631/variant.h | 6 ++++++ 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 818f9fe31..68afebb3d 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -32,8 +32,7 @@ SPIClass SDHandler = SPIClass(VSPI); #ifndef SD_SPI_FREQUENCY #define SD_SPI_FREQUENCY 4000000U #endif -#endif // HAS_SDCARD - +#endif // HAS_SDCARD #if defined(ARCH_STM32WL) @@ -342,9 +341,11 @@ void setupSDCard() #if (defined(ARCH_ESP32) || defined(ARCH_NRF52)) #if (defined(ARCH_ESP32)) SDHandler.begin(SPI_SCK, SPI_MISO, SPI_MOSI); +#elif (defined(ARCH_NRF52)) + SDHandler.begin(); #endif - if (!SD.begin(SDCARD_CS, SDHandlerr, SD_SPI_FREQUENCY)) { // param SDHandler only used for esp32 + if (!SD.begin(SDCARD_CS, SDHandler, SD_SPI_FREQUENCY)) { LOG_DEBUG("No SD_MMC card detected"); return; } @@ -353,23 +354,23 @@ void setupSDCard() LOG_DEBUG("No SD_MMC card attached"); return; } - LOG_DEBUG("SD_MMC Card Type: "); if (cardType == CARD_MMC) { - LOG_DEBUG("MMC"); + LOG_DEBUG("SD_MMC Card Type: MMC"); } else if (cardType == CARD_SD) { - LOG_DEBUG("SDSC"); + LOG_DEBUG("SD_MMC Card Type: SDSC"); } else if (cardType == CARD_SDHC) { - LOG_DEBUG("SDHC"); + LOG_DEBUG("SD_MMC Card Type: SDHC"); } else { - LOG_DEBUG("UNKNOWN"); + LOG_DEBUG("SD_MMC Card Type: UNKNOWN"); } uint64_t cardSize = SD.cardSize() / (1024 * 1024); LOG_DEBUG("SD Card Size: %lu MB", (uint32_t)cardSize); LOG_DEBUG("Total space: %lu MB", (uint32_t)(SD.totalBytes() / (1024 * 1024))); -#if (defined(ARCH_ESP32)) // not implemented in arduino sd library + LOG_INFO("Now scanning free clusters on SD card"); + delay(100); // let serial print the above statement properly LOG_DEBUG("Used space: %lu MB", (uint32_t)(SD.usedBytes() / (1024 * 1024))); -#endif + #endif #endif } \ No newline at end of file diff --git a/src/modules/Modules.cpp b/src/modules/Modules.cpp index 93b8d9826..8a4ac7722 100644 --- a/src/modules/Modules.cpp +++ b/src/modules/Modules.cpp @@ -219,7 +219,7 @@ void setupModules() paxcounterModule = new PaxcounterModule(); #endif #endif -#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) +#if defined(ARCH_ESP32) || defined(ARCH_PORTDUINO) || defined(HAS_SDCARD) #if !MESHTASTIC_EXCLUDE_STOREFORWARD storeForwardModule = new StoreForwardModule(); #endif diff --git a/src/modules/StoreForwardModule.cpp b/src/modules/StoreForwardModule.cpp index b981f175a..994d08cfc 100644 --- a/src/modules/StoreForwardModule.cpp +++ b/src/modules/StoreForwardModule.cpp @@ -107,6 +107,7 @@ void StoreForwardModule::populateSDCard() } this->storageType = StorageType::ST_SDCARD; uint32_t numberOfPackets = (this->records ? this->records : (((SD.totalBytes() / 3) * 2) / sizeof(PacketHistoryStruct))); + this->records = numberOfPackets; // only allocate space for one temp copy this->packetHistory = (PacketHistoryStruct *)malloc(sizeof(PacketHistoryStruct)); LOG_DEBUG("numberOfPackets for packetHistory - %u", numberOfPackets); @@ -174,9 +175,13 @@ uint32_t StoreForwardModule::getNumAvailablePackets(NodeNum dest, uint32_t last_ spiLock->lock(); auto handler = SD.open("/storeforward/" + String(i), FILE_READ); if (handler) { - handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); + if (handler.read((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)) != + sizeof(PacketHistoryStruct)) { + LOG_ERROR("SD card reading error"); + } handler.close(); if (this->packetHistory[0].time && (this->packetHistory[0].time > last_time)) { + // Client is only interested in packets not from itself and only in broadcast packets or packets towards it. if (this->packetHistory[0].from != dest && (this->packetHistory[0].to == NODENUM_BROADCAST || this->packetHistory[0].to == dest)) { @@ -191,6 +196,7 @@ uint32_t StoreForwardModule::getNumAvailablePackets(NodeNum dest, uint32_t last_ LOG_ERROR("S&F: Unknown storage type"); } } + return count; } @@ -267,8 +273,8 @@ void StoreForwardModule::historyAdd(const meshtastic_MeshPacket &mp) 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)); + auto handler = SD.open("/storeforward/" + String(this->packetHistoryTotalCount), FILE_WRITE, true); + handler.write((uint8_t *)&this->packetHistory[0], sizeof(PacketHistoryStruct)); handler.close(); spiLock->unlock(); #endif diff --git a/variants/rak4631/platformio.ini b/variants/rak4631/platformio.ini index c423264fe..474de8bed 100644 --- a/variants/rak4631/platformio.ini +++ b/variants/rak4631/platformio.ini @@ -20,7 +20,7 @@ lib_deps = https://github.com/RAKWireless/RAK13800-W5100S.git#1.0.2 rakwireless/RAKwireless NCP5623 RGB LED library@^1.0.2 https://github.com/RAKWireless/RAK12034-BMX160.git#dcead07ffa267d3c906e9ca4a1330ab989e957e2 - https://github.com/Woutvstk/arduino_SD.git#909c2a4a14f5b053bb69958709f0c874aa358c38 ;library to acces SD card + https://github.com/Woutvstk/SdFat_wrapper25.git#6f8f48d56c15cbeac753560dfeede4a487f81f4c ; If not set we will default to uploading over serial (first it forces bootloader entry by talking 1200bps to cdcacm) ; Note: as of 6/2013 the serial/bootloader based programming takes approximately 30 seconds diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index ac8130943..67551bfcc 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -138,6 +138,12 @@ static const uint8_t SCK = PIN_SPI_SCK; #define SPI_MISO PIN_SPI1_MISO #define SDCARD_CS (26) +// Some settings for the SdFat library to optimize flash usage +#define SDFAT_FILE_TYPE 1 // only support FAT16/FAT32, not exFAT +#define CHECK_FLASH_PROGRAMMING \ + 0 // this reduces flash usage but may cause higher power usage when sd card is idle TODO:Check if power usage is higher +#define MAINTAIN_FREE_CLUSTER_COUNT 1 // maintain free cluster count + /* * eink display pins */ From 0d19f766e88da5b11c51399e0019a9c52be90da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Mon, 7 Apr 2025 13:00:28 +0200 Subject: [PATCH 19/20] fix build errors --- src/FSCommon.cpp | 27 ++------------------------- src/xmodem.h | 4 +++- variants/unphone/variant.h | 1 - 3 files changed, 5 insertions(+), 27 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 68afebb3d..cfb47970d 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -21,6 +21,8 @@ SPIClass SDHandler = SPIClass(HSPI); #elif defined(SDCARD_USE_VSPI) SPIClass SDHandler = SPIClass(VSPI); +#else +#define SDHandler SPI #endif #elif defined(ARCH_NRF52) #if defined(SDCARD_USE_SPI1) @@ -34,31 +36,6 @@ SPIClass SDHandler = SPIClass(VSPI); #endif #endif // HAS_SDCARD - -#if defined(ARCH_STM32WL) - -uint16_t OSFS::startOfEEPROM = 1; -uint16_t OSFS::endOfEEPROM = 2048; - -// 3) How do I read from the medium? -void OSFS::readNBytes(uint16_t address, unsigned int num, byte *output) -{ - for (uint16_t i = address; i < address + num; i++) { - *output = EEPROM.read(i); - output++; - } -} - -// 4) How to I write to the medium? -void OSFS::writeNBytes(uint16_t address, unsigned int num, const byte *input) -{ - for (uint16_t i = address; i < address + num; i++) { - EEPROM.update(i, *input); - input++; - } -} -#endif // ARCH_STM32WL - /** * @brief Copies a file from one location to another. * diff --git a/src/xmodem.h b/src/xmodem.h index ae1cca7cb..1e7b7a94f 100644 --- a/src/xmodem.h +++ b/src/xmodem.h @@ -61,8 +61,10 @@ class XModemAdapter uint16_t packetno = 0; -#if defined(ARCH_NRF52) || defined(ARCH_STM32WL) +#if defined(ARCH_NRF52) Adafruit_LittleFS_Namespace::File file = Adafruit_LittleFS_Namespace::File(FSCom); +#elif defined(ARCH_STM32WL) + File file = File(FSCom); #else File file; #endif diff --git a/variants/unphone/variant.h b/variants/unphone/variant.h index 49b98ea81..272fec378 100644 --- a/variants/unphone/variant.h +++ b/variants/unphone/variant.h @@ -48,7 +48,6 @@ #undef GPS_RX_PIN #undef GPS_TX_PIN - // #define HAS_SDCARD 1 // causes hang if defined #define SDCARD_USE_HSPI From 6d8bedb02719413107c401aef0b2e6c6a30e5c89 Mon Sep 17 00:00:00 2001 From: Woutvstk <119763111+Woutvstk@users.noreply.github.com> Date: Tue, 8 Apr 2025 15:27:47 +0200 Subject: [PATCH 20/20] Clean up unused macros and outdated comments (#6520) * Updating comments and removing residue * Some more unused macros --- src/FSCommon.cpp | 14 +++++++------- variants/rak4631/variant.h | 6 ------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index cfb47970d..48406ad05 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -26,11 +26,11 @@ SPIClass SDHandler = SPIClass(VSPI); #endif #elif defined(ARCH_NRF52) #if defined(SDCARD_USE_SPI1) -#define SDHandler SPI1 // only used for esp32, SPI selection for NRF52 happens in variant.h (for now) +#define SDHandler SPI1 #elif defined(SDCARD_USE_SPI) -#define SDHandler SPI // only used for esp32 -#endif // NRF52 SPI or SPI1 -#endif // ESP32/NRF52 +#define SDHandler SPI +#endif // NRF52 SPI or SPI1 +#endif // ESP32/NRF52 #ifndef SD_SPI_FREQUENCY #define SD_SPI_FREQUENCY 4000000U #endif @@ -344,9 +344,9 @@ void setupSDCard() uint64_t cardSize = SD.cardSize() / (1024 * 1024); LOG_DEBUG("SD Card Size: %lu MB", (uint32_t)cardSize); LOG_DEBUG("Total space: %lu MB", (uint32_t)(SD.totalBytes() / (1024 * 1024))); - LOG_INFO("Now scanning free clusters on SD card"); - delay(100); // let serial print the above statement properly - LOG_DEBUG("Used space: %lu MB", (uint32_t)(SD.usedBytes() / (1024 * 1024))); + LOG_INFO("Now scanning free clusters on SD card, this may take some time..."); + delay(100); // let serial print the above statement properly + LOG_DEBUG("Used space: %lu MB", (uint32_t)(SD.usedBytes() / (1024 * 1024))); // This might take some time during boot #endif #endif diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index 67551bfcc..f2087c2a5 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -130,12 +130,6 @@ static const uint8_t SCK = PIN_SPI_SCK; #define HAS_SDCARD 1 #define SDCARD_USE_SPI1 1 -#ifdef SDCARD_USE_SPI1 -#define SDCARD_SPI SPI1 -#endif -#define SPI_MOSI PIN_SPI1_MOSI -#define SPI_SCK PIN_SPI1_SCK -#define SPI_MISO PIN_SPI1_MISO #define SDCARD_CS (26) // Some settings for the SdFat library to optimize flash usage