I am trying to connect an sd disk (Sparkfun 13743) to the Artemis ATP using the SdFat.h library. Of course, code that runs on Arduino and Redboard Turbo does not work:(
I have tried to paste code from Example06-SPI to get this to work, but the call to sd.begin always fails.
The connections are made using 3.3V, GND, and the 3 SPI pins on the upper left of the board labeled SCK, MISO, and MOSI. I have tried a variety of chip select pins.
I suspect the sd.begin is failing because I am not using the “correct” chip select pin. What pin should be used for the chip select in SPI? I have tried pins 0(SS), 4, 8, 9, 11, 28, 35 with no success.
In the post
https://user-images.githubusercontent.c … 8889d0.png
an example is shown that appears to use pin 28. Unfortunately, the poster did not include the code he used to make it successful.
In 2019 liquid.soulder posted that the Arduino SD examples should work with the ATP. This appears to not be true; I tried running cardInfo, but it does not compile. There are conflicting definitions of the File open parameters (O_TRUNC, for example).
I am running Sparkfun package Apollo3 version 2.2.0, Arduino IDE 1.8.5.
Here is a simplified form of the code I am trying to use. Commenting around line 14 selects which sd library I am trying.
-
Attempting to compile with the standard SD.h does not compile. I get the conflicts between open parameters.
-
Compiling with library SdFat for FAT16/32 compiles, but fails to initialize sd.begin(). The code tries chipSelect of {0,4,8,9,10,28,35} without success.
-
Compiling with SdFat on a Mega or a Redboard Turbo works fine, using chipSelect = SS.
So, I am completely unable to make SPI work to a simple microSD on an ATP. Hopefully someone can see what I am doing wrong here.
/*
* MicroSD with Level Shifting sparkfun product 13743
* A simple file logger that allows the user to write to a file on the µSD card
*
*/
#include <Wire.h> // Is this needed for SPI?
#include <SPI.h>
//#include <SD.h>
#include <SdFat.h>
//#define Serial SerialUSB // Turbo
//for using SD.h
//File fd;
//using SdFat.h FAT16/FAT32
SdFat sd;
SdFile fd;
//using SdFat.h exFat
//SdExFat sd;
//ExFile fd;
char fileName[50] = "demoFile.txt";
bool alreadyBegan = false; // sd.begin() misbehaves if not first call
#define debug(a,b) {Serial.print(a);Serial.println(b);}
void setup()
{
Serial.begin(115200);
while (!Serial);
Wire.begin(); // not needed?
#ifdef __SD_H__
debug("Using SD.h files","");
#else // __SD_H__
debug("Using SdFat.h files","");
#endif // __SD_H__
// Note: To satisfy the AVR SPI gods the SD library takes care of setting
// SS_PIN as an output. We don't need to.
initializeCard();
}
int count = 0;
void loop()
{
if (count < 20) // just write 20 lines to the file
{
char s[30] = "test longer line";
count++;
flushBuffer(s);
}
delay(1000);
}
void initializeCard(void)
{
Serial.println(F("Initializing SD card..."));
// step through various chipSelect pin candidates
// looking for one that works
uint8_t chipSelect[7]= {0,4,8,9,10,28,35};
int i = 0;
bool sdInitOK = false;
while (!sdInitOK)
{
// begin() returns failure even if it worked if it's not the first call.
if (!sd.begin(chipSelect[i]) && !alreadyBegan) // begin uses half-speed...
{
debug("sd.begin Initialization failed for CS ", chipSelect[i]);
i++;
}
else
{
alreadyBegan = true;
debug("sd.begin Init succeeded! for chipSelect ", chipSelect[i]);
sdInitOK = true;
}
if (i > 6) sdInitOK = true;
}
Serial.println(F("Initialization done."));
Serial.print(fileName);
if (sd.exists(fileName))
{
Serial.println(F(" remove existing file."));
if (sd.remove(fileName))
{
Serial.println("Remove successful");
}
else
{
Serial.println("Remove failed");
}
}
Serial.println(F("Creating empty file."));
#ifdef __SD_H__
fd = SD.open(fileName, FILE_WRITE);
#else
fd.open(fileName, O_WRONLY | O_CREAT | O_EXCL);
#endif
if (!fd.open(fileName, O_WRONLY | O_CREAT | O_EXCL))
{
Serial.println(F("Creation failed"));
}
else
{
Serial.println(F("Creation ok"));
fd.close();
}
}
void flushBuffer(char *s)
{
#ifdef __SD_H__
fd = SD.open(fileName, FILE_WRITE);
#else
fd.open(fileName, O_WRONLY | O_APPEND); // O_RDWR | O_CREAT);
#endif
if (fd)
{
fd.println(s);
fd.close();
}
else
{
Serial.print("flushBuffer: Could not open file "); Serial.println(fileName);
}
}