Cannot get Artemis ATP SPI to work to a disk

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);
        }
}

DEV-13743 is designed to connect to 5 volt systems, the redboard turbo and artemist atp are both 3.3 volt systems so it won’t work. BOB-00544 is what you’re after for a 3.3 volt system.

I guess I read this

"The SparkFun Shifting microSD Breakout is quite similar to the SparkFun microSD Transflash Breakout, but with the additional feature of being 5.0V tolerant for ease of use. No more discrete level shifting is required! "

as meaning this should work with both 3.3 and 5V systems. I bought it specifically because I want to use it with either Arduino Mega, ATP, or Turbo.

Is this correct that it does not work with 3.3V systems? Is there another disk that works with both?

I have not had any luck using DEV-13743 with a 3.3 volt system, I have however had luck with BOB-00544 on 3.3 volt systems. Apparently the logic level converter on 13743 needs 5 volt data to work.

Just for the fun, I obtained a DEV-13743 MicroSD Breakout. I have connected to an Artemis ATP. For me, it worked without problems ( library V2.2.0) and connected as follows:

MicroSD    ATP
 VCC        3V3 
 D2/CS      23  (defined by PIN_MICROSD_CHIP_SELECT)
 D1         MOSI / COPI
 SCK        SCK
 D0         MISO / CIPO
 CD         27  (defined by PIN_MICROSD_CD_SELECT)
 GND        GND

The microSD is formatted for FAT32.

Then used a sketch I had shared on another post, although this is updated. It has a simple menu to read and write the card.

LongNameTest.zip (5.46 KB)