update debug messages, prepare implementation of SPI mode

This commit is contained in:
elmo128 2023-09-15 12:42:41 +02:00
parent 1beeea2a25
commit 59fbade347

View File

@ -1,6 +1,19 @@
#ifndef _SDCARD_H_ #ifndef _SDCARD_H_
#define _SDCARD_H_ #define _SDCARD_H_
#if defined (SDMMC_D0) && defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3)
#define BUILD_SDMMC_4
#include <SD_MMC.h>
#elif defined (SDMMC_D0) && !(defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3))
#define BUILD_SDMMC_1
#include <SD_MMC.h>
#else
#warning SD card support disabled!
#endif
#include <FS.h>
#include <ArduinoJson.h>
#include "..\devices\device.h" #include "..\devices\device.h"
#include "storage.h" #include "storage.h"
#include "nvMemory.h" #include "nvMemory.h"
@ -15,27 +28,31 @@ public:
bool loadConfigFile(TSettings* Settings); bool loadConfigFile(TSettings* Settings);
private: private:
bool initSDcard(); bool initSDcard();
void unmount();
bool cardInitialized_; #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4)
fs::SDMMCFS* iSD_;
#elif defined (BUILD_SDSPI)
fs::SDFS* iSD_;
#error You chose to run the sd card in SPI mode. This is not implemented yet.
#endif
}; };
#ifdef BUILD_SDMMC #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) || defined (BUILD_SDSPI)
#include <FS.h>
#include <SD_MMC.h>
#include <ArduinoJson.h>
SDCard::SDCard() SDCard::SDCard()
{ {
cardInitialized_ = false; #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4)
iSD_ = &SD_MMC;
#elif defined (BUILD_SDSPI)
#error You chose to run the sd card in SPI mode. This is not implemented yet.
#endif
} }
SDCard::~SDCard() SDCard::~SDCard()
{ {
if (cardInitialized_) unmount();
SD_MMC.end();
} }
void SDCard::SD2nvMemory(nvMemory* nvMem) void SDCard::SD2nvMemory(nvMemory* nvMem)
@ -54,26 +71,24 @@ bool SDCard::loadConfigFile(TSettings* Settings)
{ {
// Load existing configuration file // Load existing configuration file
// Read configuration from FS json // Read configuration from FS json
Serial.println("SDCard: Mounting File System...");
if (initSDcard()) if (initSDcard())
{ {
if (SD_MMC.exists(JSON_CONFIG_FILE)) if (iSD_->exists(JSON_CONFIG_FILE))
{ {
// The file exists, reading and loading // The file exists, reading and loading
Serial.println("SDCard: Reading config file"); File configFile = iSD_->open(JSON_CONFIG_FILE, "r");
File configFile = SD_MMC.open(JSON_CONFIG_FILE, "r");
if (configFile) if (configFile)
{ {
Serial.println("SDCard: Opened configuration file");
StaticJsonDocument<512> json; StaticJsonDocument<512> json;
DeserializationError error = deserializeJson(json, configFile); DeserializationError error = deserializeJson(json, configFile);
configFile.close(); configFile.close();
Serial.println("SDCard: Loading config file");
serializeJsonPretty(json, Serial); serializeJsonPretty(json, Serial);
Serial.print('\n'); Serial.print('\n');
unmount();
if (!error) if (!error)
{ {
Serial.println("SDCard: Parsing JSON");
strcpy(Settings->WifiSSID, json[JSON_KEY_SSID] | Settings->WifiSSID); strcpy(Settings->WifiSSID, json[JSON_KEY_SSID] | Settings->WifiSSID);
strcpy(Settings->WifiPW, json[JSON_KEY_PASW] | Settings->WifiPW); strcpy(Settings->WifiPW, json[JSON_KEY_PASW] | Settings->WifiPW);
strcpy(Settings->PoolAddress, json[JSON_KEY_POOLURL] | Settings->PoolAddress); strcpy(Settings->PoolAddress, json[JSON_KEY_POOLURL] | Settings->PoolAddress);
@ -82,62 +97,79 @@ bool SDCard::loadConfigFile(TSettings* Settings)
Settings->PoolPort = json[JSON_KEY_POOLPORT].as<int>(); Settings->PoolPort = json[JSON_KEY_POOLPORT].as<int>();
if (json.containsKey(JSON_KEY_TIMEZONE)) if (json.containsKey(JSON_KEY_TIMEZONE))
Settings->Timezone = json[JSON_KEY_TIMEZONE].as<int>(); Settings->Timezone = json[JSON_KEY_TIMEZONE].as<int>();
SD_MMC.end();
return true; return true;
} }
else else
{ {
// Error loading JSON data // Error loading JSON data
Serial.println("SDCard: Failed to load json config"); Serial.println("SDCard: Error parsing config file!");
} }
} }
else
{
Serial.println("SDCard: Error opening config file!");
}
} }
else else
{ {
Serial.println("SDCard: No config file available!"); Serial.println("SDCard: No config file available!");
} }
SD_MMC.end(); unmount();
} }
return false; return false;
} }
void SDCard::unmount()
{
iSD_->end();
Serial.println("SDCard: Unmounted");
}
bool SDCard::initSDcard() bool SDCard::initSDcard()
{ {
if((cardInitialized_)&&(SD_MMC.cardType() != CARD_NONE)) if(iSD_->cardType() != CARD_NONE)
{ {
Serial.println("SDCard: Already mounted."); Serial.println("SDCard: Already mounted.");
return cardInitialized_; return true;
} }
Serial.println("SDCard: Mounting card.");
bool oneBitMode = true;
#if defined (SDMMC_D0) && defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3) bool cardInitialized = false;
if (SD_MMC.cardType() == CARD_NONE) #if defined (BUILD_SDMMC_4)
if (iSD_->cardType() == CARD_NONE)
{ {
SD_MMC.setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3); iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3);
oneBitMode = false;
Serial.println("SDCard: 4-Bit Mode."); Serial.println("SDCard: 4-Bit Mode.");
cardInitialized = iSD_->begin("/sd", false);
} }
#elif defined (SDMMC_D0) && !(defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3)) #elif defined (BUILD_SDMMC_1)
if (SD_MMC.cardType() == CARD_NONE) #warning SDMMC: 1-bit mode is not always working. If you experience issues, try other modes.
if (iSD_->cardType() == CARD_NONE)
{ {
SD_MMC.setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0);
Serial.println("SDCard: 1-Bit Mode."); Serial.println("SDCard: 1-Bit Mode.");
cardInitialized = iSD_->begin("/sd", true);
} }
#elif defined (BUILD_SDSPI)
#error You chose to run the sd card in SPI mode. This is not implemented yet.
#else #else
Serial.println("SDCard: interface not available."); Serial.println("SDCard: interface not available.");
return false; return false;
#endif // dataPinsDefined #endif // dataPinsDefined
cardInitialized_ = SD_MMC.begin("/sdcard", oneBitMode); if (cardInitialized)
if ((cardInitialized_) && (SD_MMC.cardType() != CARD_NONE))
{ {
Serial.println("SDCard: Card mounted."); if(iSD_->cardType() != CARD_NONE)
return true; {
} Serial.println("SDCard: Mounted.");
else return true;
{ }
Serial.println("SDCard: No card found."); else
return false; {
Serial.println("SDCard: Mounting failed.");
iSD_->end();
}
} }
return false;
} }
#else #else
@ -147,6 +179,7 @@ SDCard::~SDCard() {}
void SDCard::SD2nvMemory(nvMemory* nvMem) {}; void SDCard::SD2nvMemory(nvMemory* nvMem) {};
bool SDCard::loadConfigFile(TSettings* Settings) { return false; } bool SDCard::loadConfigFile(TSettings* Settings) { return false; }
bool SDCard::initSDcard() { return false; } bool SDCard::initSDcard() { return false; }
void unmount() {}
#endif //BUILD_SDMMC #endif //BUILD_SDMMC