Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from
Jan 5, 2025
28 changes: 20 additions & 8 deletions src/FSCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions src/memGet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion src/modules/StoreForwardModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
}

/**
Expand Down Expand Up @@ -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));
Expand All @@ -178,6 +181,7 @@ uint32_t StoreForwardModule::getNumAvailablePackets(NodeNum dest, uint32_t last_
}
}
}
#endif
#endif
} else {
LOG_ERROR("S&F: Unknown storage type");
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -389,6 +396,7 @@ meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t
}
}
}
#endif
#endif
} else {
LOG_ERROR("S&F: Unknown storage type");
Expand Down
2 changes: 1 addition & 1 deletion src/xmodem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions variants/rak4631/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions variants/rak4631/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down