ninja2
Well-known member
I'm using a Sparkfun Micromod Teeny Processor on their Micromod Main Double board, that includes a microSD slot.
The Teensy interfaces with the microSD over SPI #2
The sketch below simply opens a file on the SD, writes a couple of lines, then closes the file. It runs fine but only once. If I restart the program (by the reset button, or sketch re-upload) it always fails at initialisation
The card will then malfunction until either the SD Card is removed & replaced, or it is power cycled on/off.
How can I stop this happening??
The Teensy interfaces with the microSD over SPI #2
The sketch below simply opens a file on the SD, writes a couple of lines, then closes the file. It runs fine but only once. If I restart the program (by the reset button, or sketch re-upload) it always fails at initialisation
Code:
sd.begin(SD_CONFIG)
How can I stop this happening??
Code:
#include <Streaming.h>
#include "SdFat.h"
#define error(s) sd.errorHalt(&Serial, F(s))
const uint8_t SD_CS_PIN = 44;
#define SPI_CLOCK SD_SCK_MHZ(50) // maximum. Reduce if errors ...
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK)
//SdExFat sd; // SD_FAT_TYPE 2 = exFAT
SdFs sd; // SD_FAT_TYPE 3 = FAT16/32/exFAT
FsFile logfile;
char logName[]= "SDtest.csv";
bool SD_OK = false;
void setup() {
//Serial.begin(115200); // not needed teensy
while (!Serial && millis() < 1000);
delay(1000);
Serial << "\n\n=== Micromod_SdFat.b ===\n";
Serial << "* Init SD: ";
if (sd.begin(SD_CONFIG)) {
Serial << "(OK)\n"; SD_OK = true;
}
else {
Serial << "FAIL **HALT**\n";
while(1);
}
// remove any prior file
if (sd.exists(logName)) {
Serial << "* delete file : " << logName << " ";
sd.remove(logName);
Serial << "(OK)\n";
}
Serial << "* open logfle : " << logName << " ";
if (logfile.open(logName, O_RDWR | O_CREAT)) {
Serial << "(OK)\n";}
else {
error("** ERROR ** logName failed to open. *HALT*\n");
while(1);
}
if (logfile) {
logfile << "A,BB,CCC,DDDD\n";
logfile << "2,1234,56,7\n";
}
Serial << "* close logfle: " << logName << " ";
if (logfile.close()) Serial << "(OK)\n";
else Serial << "FAILED to close\n";
Serial << "---- setup() done ---\n";
}
void loop() {}
Last edited: