Micro SD card library

I really like the idea of having extra memory because I am currently struggling with limited SRAM on the Spark Core. However the proposed solution of using some of the IO pins to interface to the external memory is not feasible for me because they are already in use.

Could it be possible to add an extra memory chip on top the flash chip on the Spark Core itself, e.g. the one proposed by @timb: Fujitsu MB85RS1MT (FRAM)?

I know it is not nice solution but maybe it is doable and helps me for the short term. I don’t have experience with the SPI interface so I wonder how to do the address selection (CS) of the MB85RS1MT in this case.

Thanks,
Henk

I’m working on the MicroSD shield with FRAM footprint for users to add their own external flash.

How does that sound? :smile:

CS can be output pin to basically select/not select that particular SPI device.

By default, with only one SPI device, the SS is used (which is A2 for :spark: core).

With additional devices, all you need is to hook up CS on a different pin and define it in your code.

Hope this helps!

@kennethlimcp, does your solution requires connection towards other IO pins of the Spark Core?
If that is the case then it is not feasible for me because i only have one IO pin left over.

Thanks,
Henk

mrokopraakli:

  1. How is your microSD implemented - breakout?
  2. Do you have any other SPI deviced connected?
  3. Did you check the wiring of the SPI lines A2, A3, A4, A5?
  4. How are you powering the microSD? Anything else powered by that supply?

If you post your code on github, I can to a quick test to see if it runs on my testbed. :smile:

nika8991 & kennethlimcp, timb is working on an shield that has both FRAM and microSD:

However, nika8991, multiple SPI devices need separate chip select lines no matter what. Perhaps you need to consider using an I2C I/O expander to regain some pins. :smile:

@peekay123,

i guess he has too much on his plate? I’m just gonna go ahead and make mine as a learning project for me to make an actual working PCB that i can really use on the :spark: core

Mine’s gonna be MicroSD + 2-3 slots for FRAM.

I’m actually working on the list of stuff i need to buy as a batch and at the same time fabricate the prototype board with some in-house facilities :smile:

Targeting to have it working by end of April latest! :slight_smile:

Sounds great kennethlimcp. I look forward to seeing that! :smile:

My SPI lines are connected correctly and power is stable. But I fond some interesting problem from my side.
When you’ll use too long filename thenSD library is unable to read/write on SD card. For example: “myawesomeofflinedata.log” failes, but when you’ll short name “my.log” then it works like a charm. If your’re unable to reproduction situation then I can send a code example privately.

Also, is it possible to get free size of SD card? Currently is possible to get whole SD card size.

Thanks

Hi @markopraakli

I don’t know about this library but on Arduino, the standard SD library only works with the old DOS standard 8.3 files names. There is another library that extends that called LongNames.

1 Like

Oh DOS filenames! That brings me way back! :slight_smile:

marko​praakli, bko is correct. The SD library only handles DOS 8.3 filenames. Part of the reason for this is that Microsoft is known to hold the patent on long filenames so libraries typically avoid potential conflicts by not including support for them. Support for long file names also consumes more resources. :smile:

Patent on long file names… slaps forehead Really??? What is this society I’m living in…

Microsoft is making a tidy profit from licensing old DOS patents to Android manufacturers! :open_mouth:

1 Like

I hesitate to ask this here as it seems this thread has gotten more into the new shield, but I am having some issues with the library that BDub ported. ( I am flashing through the webIDE, btw)

First off, I can’t seem to change the SS to any other Analog. I have tried A0 and A1 and the both give me initialization errors.
When I do use A2, however, I am unable to do any reads or writes. I can init the card, print out info from it and even list all of the files, but file open seems to just fail. I have tried two cards to no avail. Has anyone seriously used this lib on the spark and if so…how?

Try Serial.begin(A0)

I think there’s a little issue with the readwrite example but i haven’t dugged further.

Speedtest should work fine.

Actually i can’t remember which worked and which didn’t but CardInfo is definitely ok

I initially ported all of the examples and tested them all, and then @peekay123 added the soft SPI implementation. It should play well along side the hard SPI but maybe there is a problem with it. Try to go back to the last commit and see if it works any better please :smile:

Whelp, never mind. I should get some sleep. Copypasta issue ignoring that the examples use different classes for the file objects(File and SdFile). Also, forgot to set the SS pin as output manually before trying to init SPI with that as SS. Thanks for the help guys!

1 Like

BDub, I tested the library extensively with both software and hardware SPI and I found no problems whatsoever. When used in hardware SPI mode, it functions as if no software SPI code was added. I am perplexed at billprozac’s problem with the SS line.

kennethlimcp, I think you meant SD.begin(A0), not Serial.begin(A0)!

billprozac, which example program are you running? The examples are using the default pin settings for the SPI, including using A2 as SS. I will be adding a new speed test example that allows the selection of either software or hardware SPI and the pins associated with each. In the meantime, let’s try to figure out what is causing problems in your situation.

2 Likes

Looks like he just didn’t define the pinMode(A0,OUTPUT); I’m not sure why the A2 didn’t work though. The Spark code sets that output up for you. All is well now I guess though :wink: Thanks for the reply peekay!

Sorry for all of the confusion. I started with the SDInfo which worked fine, so I added the file read and write from the example, not noticing that the File objects from the read/write use File, and the root file object in the info example uses SdFile. Once I realized this, I created a File object from the SdFile and it all works.

Regarding the SS issue above, I think I clearly documented the insane idiocy on my part of not setting the A0 pin to output, thus not allowing the SDCard the use that Analog as SS. It might be worth adding a comment to the examples that shows using some non-hw SS for those of us that are a little denser :wink:

I basically combined the Info and read/write example into the following:

/*
 SD card test 

  This example shows how use the utility libraries on which the'
  SD library is based in order to get info about your SD card.
  Very useful for testing a card when you're not sure whether its working or not.

  The circuit:
   * SD card attached to SPI bus as follows:
   Refer to "libraries/SdFat/Sd2Card_config.h" 

  created  28 Mar 2011
  by Limor Fried 
  modified 16 Mar 2011
  by Tom Igoe
  modified for Maple(STM32 micros)/libmaple
  17 Mar 2012
  by dinau
*/
// include the SD library:

#include "application.h"

#include "SD.h"
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

// change this to match your SD shield or module;
const int chipSelect = A0;  // Using a pin other than the hardware SS

void error(const char* str)
{
  Serial.print("error: ");
  Serial.println(str);
  if (card.errorCode()) {
    Serial.print("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.print(',');
    Serial.println(card.errorData(), HEX);
  }
  while(1) {
    SPARK_WLAN_Loop();
  };
}

void setup()
{
  Serial.begin(115200);
  //Init new SS pin
  pinMode(chipSelect, OUTPUT);
  while (!Serial.available()) SPARK_WLAN_Loop();
  while (Serial.available()) Serial.read();
  Serial.print("\nInitializing SD card...");

  if (!card.init(SPI_FULL_SPEED)) error("card.init failed");

  // initialize a FAT volume
  if (!volume.init(&card)) error("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the   card");

  Serial.print("Type is FAT");
  Serial.println(volume.fatType(), DEC);
  uint32_t volumesize;
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                         // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);

  if (!root.openRoot(&volume)) error("openRoot failed"); 
}

void loop(void) {
  uint32_t t;
  double r;

  Serial.println("Type any character to start");
  while (!Serial.available()) SPARK_WLAN_Loop();
  while (Serial.available()) Serial.read();
  if (!file.open(&root, "boo.txt", O_CREAT | O_TRUNC | O_RDWR)) {
    error("open failed");
  }

  File myFile(file, "boo.txt");
  if (myFile){  
    Serial.print("Writing to boo.txt...");
    myFile.println("w00t!");
    Serial.println("done.");
    Serial.println("Reading boo.txt:");
    myFile.seek(0);
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
  }  
  file.close();
}
2 Likes