Using SD Card in Linux Print


This application note explains how to use SD Card in Linux on the Emcraft Systems STM32H7 SOM soldered onto the STM32H7-BSB Rev 4A baseboard. Support for the SD Card is enabled in the standard rootfs project available from the Emcraft software distribution and installed on each module shipped by Emcraft.

Insert a pre-formatted card with an MS-DOS file system to the SD Card slot on the development baseboard. When you boot the uImage on the STM32H7, there should be messages similar to the ones shown below. In the below example, Linux has detected an SD Card with a single partition on it:

[ 3.010346] mmci-pl18x 52007000.mmc: Got CD GPIO [ 3.017170] mmci-pl18x 52007000.mmc: mmc0: PL180 manf 53 rev1 at 0x52007000 irq 42,0 (pio) [ 3.070152] mmci-pl18x 48022400.mmc: mmc1: PL180 manf 53 rev1 at 0x48022400 irq 44,0 (pio) [ 3.112321] mmc0: new high speed SDHC card at address aaaa [ 3.131100] mmcblk0: mmc0:aaaa SC16G 14.8 GiB [ 3.167430] mmcblk0: p1

At this point you are ready to mount the MS-DOS file system on the SD Card. This is done as follows:

/ # mount /dev/mmcblk0p1 /mnt

Check that the file system has indeed been mounted (refer to the last line in the below output):

/ # mount rootfs on / type rootfs (rw) proc on /proc type proc (rw,relatime) sysfs on /sys type sysfs (rw,relatime) devtmpfs on /dev type devtmpfs (rw,nosuid,relatime) tmpfs on /tmp type tmpfs (rw,nosuid,relatime,mode=777) devpts on /dev/pts type devpts (rw,relatime,gid=5,mode=620,ptmxmode=000) /dev/mmcblk0p1 on /mnt type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437, iocharset=iso8859-1,shortname=mixed,errors=remount-ro)

Now you can write something to the SD Card. In the below example, we store the current date and time to a log file, although in real-life applications you will probably want to do something more meaningful:

~ # date > /mnt/log.file

Verify the written content by reading the log file back:

/ # cat /mnt/log.file
Thu Jan 1 00:04:16 UTC 1970
/ #

Write throughput is measured to be the following:

/ # mount -o remount,sync /mnt
/ # dd if=/dev/zero of=/mnt/10m bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10.0MB) copied, 12.891522 seconds, 794.3KB/s
/ #

Read throughput is as follows:

/ # echo 3 > /proc/sys/vm/drop_caches
[ 528.302583] hush (84): drop_caches: 3
/ # dd if=/mnt/10m of=/dev/null bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10.0MB) copied, 0.822218 seconds, 12.2MB/s
/ #

Ok, you have validated that we can write data to the SD Card. Now you remove the card from the embedded target. Unmount the file system and then extract the card from the SD Card slot:

/ # umount /mnt
/ #

At this point, you can take the card to your PC or notebook and process data stored in the MS-DOS filesystem. As a final step in this application note, validate hot insertion of SD Card on the target. Insert the card back into the SD Card slot on the baseboard with Linux up and running on the STM32H7. Messages similar to the ones shown below should appear on the Linux console:

[ 333.112321] mmc0: new high speed SDHC card at address aaaa [ 333.131100] mmcblk0: mmc0:aaaa SC16G 14.8 GiB [ 333.167430] mmcblk0: p1

Mount the MS-DOS file system and verify the content of the previously created log file:

/ # mount /dev/mmcblk0p1 /mnt / # ls -lt /mnt -rwxr-xr-x 1 root root 10485760 Jan 1 1980 10m -rwxr-xr-x 1 root root 29 Jan 1 1980 log.file / # cat /mnt/log.file Thu Jan 1 00:04:16 UTC 1970 / #