/* STM32 Blue Pill - SD Card Test with SdFat.h (Simulation 10.2) - API Corrected */
#include <SdFat.h>
#include <SPI.h>
const int chipSelectPin = PA4;
SdFs sd;
FsFile myFile;
#define SD_SPI_SPEED SD_SCK_MHZ(4) // Start with a moderate speed for simulation
void setup() {
Serial.begin(115200);
Serial.println("\nInitializing SD card (Corrected SdFat) for Sim 10.2...");
if (!sd.begin(chipSelectPin, SD_SPI_SPEED)) {
Serial.println("************************************************************");
Serial.println("ERROR: SD Card initialization failed!");
Serial.print("SdFat Error Code: 0x"); Serial.print(sd.sdErrorCode(), HEX); // Get general error code
Serial.print(", Error Data: 0x"); Serial.println(sd.sdErrorData(), HEX); // Get specific error data
// sd.printSdError(&Serial); // This function should still work and provide a text description
Serial.println("Please check wiring, SD card presence in Wokwi, and libraries.txt (needs 'SdFat').");
Serial.println("************************************************************");
while (1);
}
Serial.println("SD Card initialized successfully with SdFat.h.");
// Optional: Print volume information
Serial.println("\n--- Volume Information ---");
Serial.print("Volume Type: ");
switch (sd.fatType()) {
case FAT_TYPE_FAT12: Serial.println("FAT12"); break;
case FAT_TYPE_FAT16: Serial.println("FAT16"); break;
case FAT_TYPE_FAT32: Serial.println("FAT32"); break;
case FAT_TYPE_EXFAT: Serial.println("exFAT"); break;
default: Serial.println("Unknown"); break;
}
uint32_t volumeSizeMB = sd.vol()->clusterCount() * sd.vol()->sectorsPerCluster() * 512 / (1024 * 1024);
Serial.print("Volume Size: "); Serial.print(volumeSizeMB); Serial.println(" MB");
Serial.println("--- End Volume Information ---");
Serial.println("\n--- Root Directory Listing ---");
// sd.ls(&Serial, LS_R | LS_DATE | LS_SIZE); // Recursive with details
if (!sd.ls(&Serial, LS_SIZE)) { // List with size, non-recursive for root
Serial.println("sd.ls() failed or root directory is empty/unreadable.");
}
Serial.println("--- End of Listing ---");
Serial.println("\nAttempting to open 'test.txt' from SD card...");
if (!myFile.open("test.txt", O_RDONLY)) { // O_RDONLY for read-only
Serial.println("************************************************************");
Serial.print("ERROR: Failed to open 'test.txt'!");
Serial.print(" SdFat Error Code after open attempt: 0x"); Serial.println(sd.sdErrorCode(), HEX);
// sd.printSdError(&Serial); // Can also print the text error here
if (!sd.exists("test.txt")) {
Serial.println(" -> File 'test.txt' does not exist on SD card root (sd.exists confirms).");
} else {
Serial.println(" -> File 'test.txt' exists (sd.exists confirms), but could not be opened.");
}
Serial.println("************************************************************");
return;
}
Serial.println("'test.txt' opened successfully. File contents:");
Serial.println("-------------------------------------------");
int data_byte;
while ((data_byte = myFile.read()) >= 0) {
Serial.write((char)data_byte);
}
Serial.println("\n-------------------------------------------");
Serial.println("Finished reading from 'test.txt'.");
myFile.close();
Serial.println("'test.txt' closed.");
Serial.println("\nEnd of SD Card Test Sim 10.2 with SdFat.h.");
}
void loop() {
delay(1000);
}