update SD spi access

This commit is contained in:
elmo128 2023-09-22 01:34:28 +02:00
parent bfb01da2c8
commit 4e4c24f404
5 changed files with 122 additions and 89 deletions

View File

@ -11,6 +11,7 @@
#include "mining.h" #include "mining.h"
#include "monitor.h" #include "monitor.h"
#include "drivers/displays/display.h" #include "drivers/displays/display.h"
#include "drivers/storage/SDCard.h"
//3 seconds WDT //3 seconds WDT
#define WDT_TIMEOUT 3 #define WDT_TIMEOUT 3
@ -25,9 +26,10 @@
OneButton button2(PIN_BUTTON_2); OneButton button2(PIN_BUTTON_2);
#endif #endif
extern monitor_data mMonitor; extern monitor_data mMonitor;
SDCard SDCrd = SDCard();
/**********************⚡ GLOBAL Vars *******************************/ /**********************⚡ GLOBAL Vars *******************************/
unsigned long start = millis(); unsigned long start = millis();

View File

@ -27,8 +27,8 @@
// (default SPI unit provided by <SPI.h>) // (default SPI unit provided by <SPI.h>)
// setup SPI pins. // setup SPI pins.
#define SDSPI_CS 13 #define SDSPI_CS 13
// The following pins will be ignored, if a tft display is set up. (!defined NO_DISPLAY) // The following pins can be retreived from the TFT_eSPI lib,
// check display settings to find the appropriate lines. // if a display that is using it is activated.
#define SDSPI_CLK 14 #define SDSPI_CLK 14
#define SDSPI_MOSI 15 #define SDSPI_MOSI 15
#define SDSPI_MISO 2 #define SDSPI_MISO 2

View File

@ -1,4 +1,3 @@
#include <FS.h> #include <FS.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <WiFi.h> #include <WiFi.h>
@ -7,6 +6,7 @@
#include "nvMemory.h" #include "nvMemory.h"
#include "..\devices\device.h" #include "..\devices\device.h"
#include "SDCard.h" #include "SDCard.h"
//#include "..\lib\TFT_eSPI\User_Setup_Select.h"
#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4)
#include <SD_MMC.h> #include <SD_MMC.h>
@ -17,19 +17,43 @@
#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) || defined (BUILD_SDSPI) #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) || defined (BUILD_SDSPI)
SDCard::SDCard() /// @param int ID only relevant in SPI mode, if you want to use a non standard SPI Unit.
SDCard::SDCard(int ID):cardInitialized_(false),cardBusy_(false)
{ {
#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4)
iSD_ = &SD_MMC; iSD_ = &SD_MMC;
#elif defined (BUILD_SDSPI) #elif defined (BUILD_SDSPI)
if(ID>=0)
{
ispi_ = new SPIClass(ID);
newInstance_ = true;
}
else
{
ispi_ = &SPI; ispi_ = &SPI;
newInstance_ = false;
}
iSD_ = &SD; iSD_ = &SD;
#endif // interface type #endif // interface type
initSDcard();
} }
SDCard::~SDCard() SDCard::~SDCard()
{ {
unmount(); iSD_->end();
if(newInstance_)
{
ispi_->end();
delete ispi_;
}
Serial.println("SDCard: Unmounted");
}
/// @brief Check if the card is accessed right now.
/// @return true if active
bool SDCard::cardBusy()
{
return !cardBusy_;
} }
/// @brief Transfer settings from config file on a SD card to the device. /// @brief Transfer settings from config file on a SD card to the device.
@ -54,7 +78,7 @@ bool SDCard::loadConfigFile(TSettings* Settings)
// Load existing configuration file // Load existing configuration file
// Read configuration from FS json // Read configuration from FS json
if (initSDcard()) if (cardAvailable())
{ {
if (iSD_->exists(JSON_CONFIG_FILE)) if (iSD_->exists(JSON_CONFIG_FILE))
{ {
@ -62,15 +86,16 @@ bool SDCard::loadConfigFile(TSettings* Settings)
File configFile = iSD_->open(JSON_CONFIG_FILE, "r"); File configFile = iSD_->open(JSON_CONFIG_FILE, "r");
if (configFile) if (configFile)
{ {
cardBusy_ = true;
StaticJsonDocument<512> json; StaticJsonDocument<512> json;
DeserializationError error = deserializeJson(json, configFile); DeserializationError error = deserializeJson(json, configFile);
configFile.close(); configFile.close();
cardBusy_ = false;
Serial.println("SDCard: Loading config file"); Serial.println("SDCard: Loading config file");
serializeJsonPretty(json, Serial);
Serial.print('\n');
unmount();
if (!error) if (!error)
{ {
serializeJsonPretty(json, Serial);
Serial.print('\n');
Settings->WifiSSID = json[JSON_KEY_SSID] | Settings->WifiSSID; Settings->WifiSSID = json[JSON_KEY_SSID] | Settings->WifiSSID;
Settings->WifiPW = json[JSON_KEY_PASW] | Settings->WifiPW; Settings->WifiPW = json[JSON_KEY_PASW] | Settings->WifiPW;
Settings->PoolAddress = json[JSON_KEY_POOLURL] | Settings->PoolAddress; Settings->PoolAddress = json[JSON_KEY_POOLURL] | Settings->PoolAddress;
@ -98,82 +123,71 @@ bool SDCard::loadConfigFile(TSettings* Settings)
{ {
Serial.println("SDCard: No config file available!"); Serial.println("SDCard: No config file available!");
} }
unmount();
} }
return false; return false;
} }
void SDCard::unmount() /// @brief Check if a SD card is inserted.
/// @return true if inserted.
bool SDCard::cardAvailable()
{ {
iSD_->end(); if (cardInitialized_)
#ifdef BUILD_SDSPI_SETUP
ispi_->end();
#endif // BUILD_SDSPI_SETUP
Serial.println("SDCard: Unmounted");
}
bool SDCard::initSDcard()
{ {
if (iSD_->cardType() != CARD_NONE) if (iSD_->cardType() != CARD_NONE)
{ {
Serial.println("SDCard: Already mounted."); Serial.println("SDCard: Inserted.");
return true;
}
Serial.println("SDCard: Mounting card.");
bool cardInitialized = false;
#if defined (BUILD_SDMMC_4)
if (iSD_->cardType() == CARD_NONE)
{
iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3);
cardInitialized = iSD_->begin("/sd", false);
Serial.println("SDCard: 4-Bit Mode.");
}
#elif defined (BUILD_SDMMC_1)
#warning SDMMC : 1 - bit mode is not always working. If you experience issues, try other modes.
if (iSD_->cardType() == CARD_NONE)
{
iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0);
cardInitialized = iSD_->begin("/sd", true);
Serial.println("SDCard: 1-Bit Mode.");
}
#elif defined (BUILD_SDSPI)
if (iSD_->cardType() == CARD_NONE)
{
#ifdef BUILD_SDSPI_SETUP
ispi_->end();
ispi_->begin(SDSPI_CLK, SDSPI_MISO, SDSPI_MOSI, SDSPI_CS);
#endif //BUILD_SDSPI_SETUP
cardInitialized = iSD_->begin(SDSPI_CS, *ispi_);
Serial.println("SDCard: SPI mode.");
}
#else
Serial.println("SDCard: interface not available.");
return false;
#endif // dataPinsDefined
if (cardInitialized)
{
if (iSD_->cardType() != CARD_NONE)
{
Serial.println("SDCard: Mounted.");
return true; return true;
} }
else else
{ {
Serial.println("SDCard: Mounting failed."); Serial.println("SDCard: Not inserted.");
iSD_->end();
} }
} }
else
{
Serial.println("SDCard: Interface not initialized.");
}
return false; return false;
} }
/// @brief Init SD card interface. Normaly not required, as this is called by the constructor.
/// @return true on success
bool SDCard::initSDcard()
{
if (!cardAvailable())
{
Serial.println("SDCard: init SD card interface.");
#if defined (BUILD_SDMMC_4)
iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3);
cardInitialized_ = iSD_->begin("/sd", false);
Serial.println("SDCard: 4-Bit Mode.");
}
#elif defined (BUILD_SDMMC_1)
#warning SDMMC : 1 - bit mode is not always working. If you experience issues, try other modes.
iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0);
cardInitialized_ = iSD_->begin("/sd", true);
Serial.println("SDCard: 1-Bit Mode.");
}
#elif defined (BUILD_SDSPI)
ispi_->begin(SDSPI_CLK, SDSPI_MISO, SDSPI_MOSI, SDSPI_CS);
cardInitialized_ = iSD_->begin(SDSPI_CS, *ispi_);
Serial.println("SDCard: SPI mode.");
}
#else
Serial.println("SDCard: interface not defined.");
return false;
#endif // dataPinsDefined
cardAvailable();
return cardInitialized_;
}
#else #else
SDCard::SDCard() {} SDCard::SDCard(int ID) {}
SDCard::~SDCard() {} SDCard::~SDCard() {}
void SDCard::SD2nvMemory(nvMemory* nvMem, TSettings* Settings) {}; void SDCard::SD2nvMemory(nvMemory* nvMem, TSettings* Settings) {};
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() {} bool SDCard::cardAvailable() { return false; }
#endif //BUILD_SDMMC #endif //BUILD_SDMMC

View File

@ -24,31 +24,47 @@
#undef BUILD_SDMMC_1 #undef BUILD_SDMMC_1
#undef BUILD_SDMMC_4 #undef BUILD_SDMMC_4
#define BUILD_SDSPI #define BUILD_SDSPI
#if defined (SDSPI_CLK) && defined (SDSPI_MOSI) && defined (SDSPI_MISO) && defined (NO_DISPLAY)
#define BUILD_SDSPI_SETUP
#endif // SPIPINS
#include <SPI.h> #include <SPI.h>
#include <SD.h> #include <SD.h>
#include "..\lib\TFT_eSPI\User_Setup_Select.h"
#ifndef NO_DISPLAY
#if !defined(SDSPI_CLK) && defined(TFT_CLK)
#define SDSPI_CLK TFT_CLK
#endif // SDSPI_CLK
#if !defined(SDSPI_MOSI) && defined(TFT_MOSI)
#define SDSPI_MOSI TFT_MOSI
#endif // SDSPI_MOSI
#if !defined(SDSPI_MISO) && defined(TFT_MISO)
#define SDSPI_MISO TFT_MISO
#endif // SDSPI_MISO
#elif !defined(SDSPI_CLK) || !defined(SDSPI_MOSI) || !defined(SDSPI_MISO)
#error: Please define SDSPI pins!
#endif // NO_DISPLAY
#warning SD card support in SPI mode enabled! #warning SD card support in SPI mode enabled!
#endif #endif
// Handles the transfer of settings from sd card to nv memory (wifi credentials are handled by wifimanager)
class SDCard class SDCard
{ {
public: public:
SDCard(); SDCard(int ID=-1);
~SDCard(); ~SDCard();
void SD2nvMemory(nvMemory* nvMem, TSettings* Settings); void SD2nvMemory(nvMemory* nvMem, TSettings* Settings);
bool loadConfigFile(TSettings* Settings); bool loadConfigFile(TSettings* Settings);
bool cardAvailable();
bool cardBusy();
private: private:
bool initSDcard(); bool initSDcard();
void unmount(); bool cardInitialized_;
bool cardBusy_;
#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) #if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4)
fs::SDMMCFS* iSD_; fs::SDMMCFS* iSD_;
#elif defined (BUILD_SDSPI) #elif defined (BUILD_SDSPI)
SPIClass* ispi_; SPIClass* ispi_;
fs::SDFS* iSD_; fs::SDFS* iSD_;
bool newInstance_;
#endif #endif
}; };

View File

@ -27,6 +27,8 @@ extern monitor_data mMonitor;
nvMemory nvMem; nvMemory nvMem;
extern SDCard SDCrd;
void saveConfigCallback() void saveConfigCallback()
// Callback notifying us of the need to save configuration // Callback notifying us of the need to save configuration
{ {
@ -87,7 +89,6 @@ void init_WifiManager()
if (!nvMem.loadConfig(&Settings)) if (!nvMem.loadConfig(&Settings))
{ {
//No config file on internal flash. //No config file on internal flash.
SDCard SDCrd;
if (SDCrd.loadConfigFile(&Settings)) if (SDCrd.loadConfigFile(&Settings))
{ {
//Config file on SD card. //Config file on SD card.