I’m trying to test the SD card functionality on a custom PCB I made for the B404X. I’m an electrical engineer and have been designing boards for 9 years. I copied the SD card pinout/schematic of the B Series SoM development board exactly, and have triple checked that it is the same.
I’ve been trying for days to get SDFat to work through the Web IDE, but none of the example codes worked. I’ve tried to use the secondary SPI1, as suggested in the Dev Board datasheet. The error I get is: SD errorCode: 0X20,0XFF
I’ve tried different SD cards. I’ve tried SPI_HALF_SPEED, but that creates a different error. I’ve set SD_SPI_Configuration to 1. I’ve tried SdFat SD(&SPI1). I’ve done a manual digitalRead of the CD pin and see it change when I insert/remove the SD card.
Any insights would be appreciated. Thank you
Here is the main code I’ve been working with:
#include <SdFat.h>
SYSTEM_THREAD(ENABLED);
SerialLogHandler logHandler;
// Set up the SDFat object
//SdFat SD(&SPI1);
#define SD_SPI_CONFIGURATION 1
SdFat sd;
// Define the pin numbers
const uint8_t chipSelect = D5; // CS Pin
const uint8_t cardDetect = D6; // CD Pin
//SD inserted, cardDetect = 0
//No SD inserted, cardDetect = 1
void setup() {
// Initialize serial communication
Serial.begin(9600);
//while (!Serial); // Wait for serial port to connect - required for native USB port only
delay(5000);
Serial.println("Starting...");
pinMode(cardDetect, INPUT);
// Check if SD card is inserted
if (digitalRead(cardDetect)) {
Serial.println("No SD card detected. Insert SD card and restart the device.");
return;
}
Serial.println("SD Card Detected");
// Initialize the SD card
if (!sd.begin(chipSelect, SPI_FULL_SPEED)) {
sd.initErrorHalt();
return;
}
Serial.println("SD Card Initialized");
// Open a new file
File myFile = sd.open("test.txt", FILE_WRITE);
// If the file opened okay, write to it
if (myFile) {
Serial.println("Writing to test.txt...");
myFile.println("Testing 1, 2, 3.");
myFile.close();
Serial.println("Done.");
} else {
// If the file didn't open, print an error
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}