[Wiki]Everything there is to know about SD Card access

Troubleshooting

Something not working? Make sure your Pokitto’s battery is charged. Some cards can be picky about voltage.
Still not working? Try another card. FAT is brittle.

Available APIs

There are three ways to access the SD card from your code. Each has its pros and cons:

- PokittoDisk

Based on PetitFatFs (PFFS).
Pros: The smallest of the three libs.
Cons: Limited to one file at a time, 8-char filenames only.

#include <Pokitto.h>
void readExample(char *buffer, int length){
  pokInitSD();
  if(fileOpen("filename", FILE_MODE_READONLY) == 0)
    fileReadBytes(buffer, length);
  fileClose();
}

- SDFileSystem (SDFS)

Based on FatFs.
Pros: The most feature-complete.
Cons: The slowest, largest of the three. API is problematic. Not available in simulator.
Notes:

  • Use of the C fopen function results in a memory leak. Use the SDFS directly, like in the example below.
#include <SDFileSystem.h>
...
void readExample(char *buffer, int amount){
  auto sdfs = new SDFileSystem(P0_9, P0_8, P0_6, P0_7, "sd", NC, SDFileSystem::SWITCH_NONE, 24000000 );
  auto file = sdfs->open("filename", O_RDONLY);
  if(file){
    file->read(buffer, amount);
    file->close();
  }
}

- File

Also based on FatFs.
Pros: Faster than the other two. Simple API. Works in simulator, hardware and emulator.
Cons: Still under development, but already available in PokittoLib.
Notes:

  • Some SD cards are not supported yet. YMMV.
#include <File>
void readExample(char *buffer, int amount){
  File file;
  if(file.openRO("filename"))
    file.read(buffer, amount);
}

You can use SDFS with File’s API like this:

#include <SDFSFile.h>
void readExample(char *buffer, int amount){
  File file;
  if(file.openRO("filename"))
    file.read(buffer, amount);
}

It will initialize SDFS automatically, just create a File and use it.
Note that you can’t mix the two. Either use File or SDFSFile.

7 Likes