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] 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 */