chipKIT® Development Platform

Inspired by Arduino™

SD Multiple Files

Created Fri, 17 Aug 2012 15:32:43 +0000 by mikes


mikes

Fri, 17 Aug 2012 15:32:43 +0000

Is there a way to have more than one file open on a SD at one time?

I want to be able to open one file and while making some alterations write it's contents to another file.


rasmadrak

Tue, 21 Aug 2012 23:24:57 +0000

Not with the standard SD library. Newer releases of the library supports this, but unfortunately they're not yet ported to Chipkit.


mikes

Tue, 04 Sep 2012 15:19:05 +0000

Ok I tried to port the new Arduino library to chipkit https://github.com/mskoczen/chipKIT32-MAX/tree/sd-1.0/hardware/pic32/libraries/SD It does work for multiple files but, it is slow running code like below takes about 7.5 seconds on a 378 byte file. That speed is acceptable for my purpose but I would not mind if it went a little faster, any suggestions appreciated.

File mainfile;
  File tempfile;
  mainfile = SD.open("FILE.TXT");
  tempfile = SD.open("FILE2.TXT", FILE_WRITE);
  char ch;
  if (mainfile) {
    while (mainfile.available()){//copy file
      ch=mainfile.read();
      tempfile.write(ch); //read one character out of file write to other
    }
  }
  mainfile.close();
  tempfile.close();

rasmadrak

Tue, 04 Sep 2012 18:07:53 +0000

Great initiative and cool!

A couple of things (depending on the library defaults):

  1. Use buffers and write full sectors if possible, since each write defaults to cause a sync of the card, unless the FILE_WRITE has been fixed so a manual sync is required.
  2. The read() only fetches a single byte. There's a deeper function that can be called to read a full 512-byte sector in one go. Much faster.
  3. I don't know what has been changed in this library, but make sure the card doesn't get deselected and reinitialized as the files are swapped. I imagine that could take a while.
  4. Are you using software bitbang or hardware SPI?

There are probably more things to check. (I guess you should check if tempfile exists as well before writing to it?)


mikes

Tue, 04 Sep 2012 21:40:23 +0000

Reading several chars then Writing them reduced the time to .3 seconds for the same file.

char n;
  File mainfile;
  File tempfile;
  mainfile = SD.open(origname);
  tempfile = SD.open(copyname, FILE_WRITE);
  char ch[32];
  if (mainfile) {
    while (mainfile.available()){
      for(n=0;n<32;n++)
        ch[n]=mainfile.read();
      for(n=0;n<32;n++)
        tempfile.write(ch[n]);
    }
  }
  mainfile.close();
  tempfile.close();

rasmadrak

Thu, 06 Sep 2012 11:42:52 +0000

You should be able to further improve by using the function that read/write calls directly, but with a larger array, i:e

Instead of: for (i < 32) read()

do: buf = read(32)

Same for writing.

The function resides in SdFat.h and looks as follows:

int16_t read(void) {      //&lt;----regular read()
    uint8_t b;
    return read(&amp;b, 1) == 1 ? b : -1;    //&lt;--- does in fact point to a better function that reads multiple values in one go.
  }

  int16_t read(void* buf, uint16_t nbyte);   //&lt;---- needs to be accessible via the wrapper or made public (bad).

Jacob Christ

Sun, 28 Apr 2013 22:33:47 +0000

Just and update on this topic. The Arduino 1.0 SD features (that mikes back ported to 0023) to support multiple files open has been pulled into MPIDE and will be in the next release.

Jacob