cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
s002wjh
Explorer
Explorer
9,496 Views
Registered: ‎07-30-2009

write to SD card

is there a guide/example on how to create a user app to write/read from SD card for reference board like 702/706 under petalinux/baremetal

0 Kudos
3 Replies
rfs613
Scholar
Scholar
9,287 Views
Registered: ‎05-28-2013

The SD card normally contains a filesystem (like FAT16 or FAT32) which you must mount in order to access it. For example:

mount /dev/mmcblk0p1 /mnt

Now the contents of the SD card is accessible under /mnt. This operation is normally done as part of the boot-up process. It can also be done automatically upon insertion of a SD card using mdev or udev.

 

Anyhow, your question was about reading/writing to the SD from application code. To do that, you just use normal I/O functions like open(), read(), write(). If you are data has lots of small pieces, you might want to use buffered IO functions: fopen(), fread(), fwrite() instead.

FILE *fh;
int len;

/* Assuming your SD card is mounted under /mnt */
fh = fopen("/mnt/somefile", "rw");
if (!fh) {
    perror("fopen");
    exit(1);
}

fwrite("test", 4, 1, fh);

If you do not want a filesystem on the SD card, it is possible to open /dev/mmcblk0 directly, and treat it just like one very large memory block.

 

0 Kudos
s002wjh
Explorer
Explorer
9,255 Views
Registered: ‎07-30-2009

anyway to automatically mount the SD,  is it done under kernel -config?

0 Kudos
rfs613
Scholar
Scholar
9,225 Views
Registered: ‎05-28-2013

There are a number of ways it can be automatically mounted. The exact steps depend somewhat on which flavour of linux you are using (eg. PetaLinux, versus Ubuntu, or some other flavour).

If the SD card is always inserted, then the simplest option is to add a line to /etc/fstab. This file is used during boot to control what gets mounted, and in what order. https://en.wikipedia.org/wiki/Fstab

If the SD card is removable, it gets a bit more complicated. The basic infrastructure for this is called "hotplug". When the card is inserted or removed, the kernel will run the hotplug program, which then performs the mount command. http://linux.die.net/man/8/hotplug

Hotplug is fairly large and complex, designed for desktop PC environments. There is a smaller alternative called "mdev" that is commonly used on embedded systems. We're using it successfully on our Zynq-based designs. http://git.busybox.net/busybox/plain/docs/mdev.txt

Hope that at least points you in the right direction.

0 Kudos