I’ve been trying to use the SdFat library with the Xiao but I get a bunch a warnings and eventually the program does not work. The SdFat is required since it supports long filenames and is supposed to write to a microSD card connected via SPI. The code is here. This code is tested and works on a regular Arduino Nano. I can’t even see the output on the serial monitor.
C:\Users\xxxx.platformio\packages\framework-arduino-samd-seeed\variants\XIAO_m0/variant.h:219:25: warning: extra tokens at end of #endif directive [-Wendif-labels] #endif SerialUSB
^~~~~~~~~
There are a huge number of changes in 2.2.2 since I decided to use clang-format to force Google style formatting.
I did this to avoid warnings from the static analysis programs Cppcheck and cpplint.
clang-format is aggressive so it may actually cause code to fail. For example clang-format rearranges the order of includes according to the selected style.
Blockquote
Version 2.2.2 of SDfat lib is messed up… roll back to 2.2.0 and it works
Changed the CS pin to D2 Changed the Serial baud to 115200 Changed the SD clock speed to 4MHz via SD_SCK_MHZ(4) Changed the library version to 2.2.0 and then to Adafruit’s fork of the SDFat 2.2.3
I added a test Serial.println("Hello Xiao!"); but that does not show up on the monitor.
EDIT:
I wrote a simple Hello Xiao program to test the serial and that did not show anything on the serial terminal. I still get the same set of warnings when compiling the project.
Ignore my previous comment. I think I have this figured out. I read the documentation listed here. I tried the code listed here with the SD library and everything worked! Also using the Arduino fork of the SdFat library.
I then used the same serial and SD init code with my version and it worked!
I think by default the CS pin is D4.
However, this can be changed to whatever you like in the code. I believe the reference code waits for serial to initialize. Though I cannot figure out why my SD init code did not work earlier but works now.
My version of the working code is below:
#include <Arduino.h>
#include <SPI.h>
#include <SdFat.h>
#define SD_CS_PIN 2
#define LEDPIN 4
#define SERIALBAUD 9600
SdFat SD;
File dataFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(SERIALBAUD);
while (!Serial)
{
;
}
Serial.println("Hello Xiao!");
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS_PIN))
{
Serial.println(" failed!");
while (1)
{
;
}
}
else
{
Serial.println(" done!");
}
String fileName;
fileName = "GPSData_";
fileName += String("19");
fileName += String("01");
fileName += String("2024");
fileName += String("_");
fileName += String("10");
fileName += String("45");
fileName += String("00");
fileName += String(".kml");
Serial.print("File name is: ");
Serial.println(fileName);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// (O_READ | O_WRITE | O_CREAT) erases any previous content and then writes.
// FILE_WRITE is (O_RDWR | O_CREAT | O_AT_END)
dataFile = SD.open(fileName, FILE_WRITE);
// if the file opened okay, write to it:
if (dataFile)
{
Serial.print("Writing to file...");
dataFile.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
dataFile.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
dataFile.println("");
dataFile.println(" <Document>");
dataFile.println(" <name>Views with Time</name>");
dataFile.println(" <open>1</open>");
dataFile.println(" <description>");
dataFile.println(" In Google Earth 20, enable historical imagery and sunlight,");
dataFile.println(" then click on each placemark to fly to that point in time.");
dataFile.println(" </description>");
dataFile.println("");
dataFile.println(" <Placemark>");
dataFile.println(" <name>Sutro Baths in 1946</name>");
dataFile.println(" <gx:TimeStamp>");
dataFile.println(" <when>1946-07-29T05:00:00-08:00</when>");
dataFile.println(" </gx:TimeStamp>");
dataFile.println(" <longitude>-122.518172</longitude>");
dataFile.println(" <latitude>37.778036</latitude>");
dataFile.println(" <altitude>221.0</altitude>");
dataFile.println(" </Placemark>");
dataFile.println("");
dataFile.println(" </Document>");
dataFile.println("</kml>");
dataFile.close();
Serial.println(" done!");
}
else
{
// if the file didn't open, print an error:
Serial.println("Error opening file for writing!!");
}
// re-open the file for reading:
Serial.print("\n\nReading from file: ");
Serial.println(fileName);
Serial.println("");
dataFile = SD.open(fileName, FILE_READ);
if (dataFile)
{
// read from the file until there's nothing else in it:
while (dataFile.available())
{
Serial.write(dataFile.read());
}
// close the file:
dataFile.close();
Serial.println("\n File read!");
}
else
{
// if the file didn't open, print an error:
Serial.println("Error opening file for reading!!");
}
// Close the serial port
Serial.end();
}
void loop()
{
// nothing happens after setup
}