Help going from SD.h to SdFat.h

Hi. I'm trying to move from the SD.h library to the SdFat.h library and I'm having trouble getting started. I'm using a custom board that is based on an M0. It has a built in SD card which shares the SPI bus with a flash memory device. The following code works:

#include "SD.h"
//#include "SdFat.h"
//SdFat32 sd;
//File32 file;

#define Serial SerialUSB 

#define SDselect           46  // (PA21) Chip select for SD card. Make this pin low to activate the SD card for SPI communication
#define SDon               45  // (PA06) Provides power to the SD via a high side mosfet. Make this pin low to power on the SD card
#define FlashCS            44  // (PA05) Chip select for flash memory. Make this pin low to activate the flash memory for SPI communication

// Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur.
#define SPI_CLOCK SD_SCK_MHZ(4)

void setup() {

  pinMode(SDselect, OUTPUT);          // Chip select pin for SD card must be an output
  digitalWrite(SDselect, LOW);        // turns on the SD card.
  pinMode(SDon, OUTPUT);          // Chip select pin for SD card must be an output
  digitalWrite(SDon, LOW);        // turns on the SD card.
  pinMode(FlashCS, OUTPUT);          // Chip select pin for SD card must be an output
  digitalWrite(FlashCS, HIGH);        
  delay(20);
  
  Serial.begin(9600);

  // Wait for USB Serial
  while (!Serial) {
    yield();
  }
  Serial.println("initialize...");

  // Initialize the SD.
  if (!SD.begin(SDselect)) {
    Serial.println("initialization failed.");
  } else {
    Serial.println("Initialization OK");
  }

  Serial.println("Done");
}

void loop() {
}

But with this code initialization fails.

//#include "SD.h"
#include "SdFat.h"
SdFat32 sd;
File32 file;

#define Serial SerialUSB 

#define SDselect           46  // (PA21) Chip select for SD card. Make this pin low to activate the SD card for SPI communication
#define SDon               45  // (PA06) Provides power to the SD via a high side mosfet. Make this pin low to power on the SD card
#define FlashCS            44  // (PA05) Chip select for flash memory. Make this pin low to activate the flash memory for SPI communication

// Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur.
#define SPI_CLOCK SD_SCK_MHZ(4)

void setup() {

  pinMode(SDselect, OUTPUT);          // Chip select pin for SD card must be an output
  digitalWrite(SDselect, LOW);        // turns on the SD card.
  pinMode(SDon, OUTPUT);          // Chip select pin for SD card must be an output
  digitalWrite(SDon, LOW);        // turns on the SD card.
  pinMode(FlashCS, OUTPUT);          // Chip select pin for SD card must be an output
  digitalWrite(FlashCS, HIGH);        
  delay(20);
  
  Serial.begin(9600);

  // Wait for USB Serial
  while (!Serial) {
    yield();
  }
  Serial.println("initialize...");

  // Initialize the SD.
  if (!sd.begin(SDselect)) {
    Serial.println("initialization failed.");
  } else {
    Serial.println("Initialization OK");
  }
  
  Serial.println("Done");
}

void loop() {
}

Note that the only differences are the lines commented at the top, and changing the case of "SD.begin" to "sd.begin." What am I leaving out? Any ideas?

The best approach is to first test your hardware with an example taken directly from the SDFat library, rather than your own code.

Modify the example only as required to accommodate pin changes, etc.

Yep, that's what I did first. The code in the post is modified from the "readCsvFile" example. I just pared it down. after it failed on initialization.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.