Hi all,
Having a bit of an issue with SDFat on an Electron. Setup is:
- Particle Dev
- FW 0.7.0
- Adafruit 5V MicroSD Breakout on breadboard
- SD_FAT_VERSION 20170302
I’ve used the sample programs, and TryMeFirst.cpp works just fine, as does SDInfo.cpp. When I try to run very similar code, it chokes out every time, and returns SD errorCode 0x4,0xFF. The SD reader is the only SPI device currently, although I’m running other sensors on I2C. I know that breadboards can be an issue, so I’m running the SPI_SIXTEENTH_SPEED modifier. I’ve been trying a few things, so apologies if the code is a bit messy. What really has me confused is I’m not really doing anything, just opening the file, which works fine in TryMeFirst.cpp, so I figure I’m missing something obvious… Any thoughts?
Thanks,
Peter
#pragma once
/*
Cryo_MicroSD.h
Wrapper for SDFat library.
Used for logging to SPI SD card, initially at 20s intervals.
Originally heavily based on SDCardLogHandlerRK.
*/
#include <string>
#include "SdFat.h"
class Cryo_MicroSD {
public:
Cryo_MicroSD ();
virtual ~Cryo_MicroSD ();
void Begin(std::string logFileName);
void LogToSD(std::string stringToLog);
private:
/* data */
std::string _logFileName;
std::string _stringToLog;
int _SDCSPin = SS;
SdFat spiSDCard;
bool lastBeginResult = false; //!< Last result from sd.begin().
//Will be false if there's not a valid SD card inserted.
unsigned long cardCheckPeriod = 10000;
File currentLogFile;
unsigned long lastCardCheck = 0; //millis() value at last time checked for SD card
//related to cardCheckPeriod
int TryOpeningCard(int SDCSPin = SS);
};
#include "Cryo_MicroSD.h"
#include <string>
#include "SdFat.h"
#define SDCARD_DEBUG_LEVEL 2
// Don't change these, just change the debugging level above
// Note: must use Serial.printlnf here, not Log.info, as these are called from the log handler itself!
#if SDCARD_DEBUG_LEVEL >= 1
#define DEBUG_NORMAL(x) Serial.printlnf x
#define SPI_SPEED SPI_SIXTEENTH_SPEED //Apparently full speed can have issues on breadboards.
#else
#define DEBUG_NORMAL(x)
#define SPI_SPEED SPI_FULL_SPEED
#endif
#if SDCARD_DEBUG_LEVEL >= 2
#define DEBUG_HIGH(x) Serial.printlnf x
#else
#define DEBUG_HIGH(x)
#endif
Cryo_MicroSD::Cryo_MicroSD(){
}
Cryo_MicroSD::~Cryo_MicroSD(){
}
void Cryo_MicroSD::Begin(std::string logFileName){
_logFileName = logFileName;
TryOpeningCard();
}
int Cryo_MicroSD::TryOpeningCard(int SDCSPin){
_SDCSPin = SDCSPin;
pinMode(_SDCSPin, OUTPUT);
lastBeginResult = spiSDCard.begin(_SDCSPin, SPI_SPEED);
if(!lastBeginResult)
DEBUG_NORMAL(("Begin failed."));
return lastBeginResult;
}
void Cryo_MicroSD::LogToSD(std::string stringToLog){
_stringToLog = stringToLog;
if(!lastBeginResult){
TryOpeningCard();
}
//char logFileNameCStr[11] = "201811.csv";
//strcpy(logFileNameCStr, _logFileName.c_str());
//Serial.println(logFileNameCStr);
if(!currentLogFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)){
DEBUG_NORMAL(("Failed to open log file."));
Serial.println(digitalRead(_SDCSPin));
spiSDCard.errorPrint(&Serial);
}
currentLogFile.println(_stringToLog.c_str());
currentLogFile.close();
}