Skip to content
Adan edited this page Feb 5, 2022 · 8 revisions

SD card access

Writing to SD is supported from Orcus 1.3.0

At present, reading and writing files from FAT12/16/32 filesystems is supported. This has been tested with cards which implement various different versions of the applicable standards, including a 16M MMC, 32M SD (1.x), a 512M SD (2.x), 2G SD (2.x), 16G SDHC and 32G SDXC.

I would strongly recommend you use libfat to access SD cards, but it is possible to access them directly if you wish.

Using libfat

First, in your Makefile, add -lfat to LIBS before Orcus. For example:

LIBS := -lfat -lorcus

Then, in your code, you'll need to add the following:

#include <fat.h>

The rest is standard libfat as used with other devkitPro projects. You need to call fatInitDefault() before attempting file operations, which will return true if the SD card was mounted successfully, or false otherwise. If you want to unmount the card (e.g. to allow people to switch to a different card), you'll need to call fatUnmount("sd").

You can now use standard C functions from newlib to work with the filesystem (such as fopen, fclose, fread, fwrite, opendir). The root of the SD card is mapped as / or alternatively sd:/.

FILE* fp = fopen("sd:/a/file.txt");
...
fclose(fp);

As with any other C program, argv[0] will contain the o2x file path/name so you can determine which directory your code is running from if you wish to load assets.

Direct access

If for some reason you want to access the card directly, you need to call sdInit first, which will return 0 on success. Then you can use sdReadBlocks and sdWriteBlocks as documented in the Orcus API reference.

Clone this wiki locally