First attempt to adding hardware support for NRF52 SPI SD Card (#5561)

* 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 <tgoettgens@gmail.com>
This commit is contained in:
Woutvstk 2025-01-05 21:19:16 +01:00 committed by GitHub
parent 37a03c2ef9
commit 96277ed804
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 46 additions and 12 deletions

View File

@ -14,13 +14,20 @@
#ifdef HAS_SDCARD
#include <SD.h>
#include <SPI.h>
#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
#endif
#endif
}

View File

@ -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;

View File

@ -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");

View File

@ -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

View File

@ -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

View File

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