lpcware

SD Card, FAT32, SD Card hot plug with uClinux on LPC1788

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by Vladimir Khusainov on Tue Feb 14 02:24:28 MST 2012
Here is a sample session showing how to use SD Card with the LPC1788 running Emcraft Systems' port of uClinux.

Highlights of this demo are as follows:

- A FAT32 filesystem is mounted on the SD Card allowing to easily move data between the LPC1788 target and a Windows PC.
- "Hot-plug" is supported, that is, it is possible to insert and remove an SD Card on a running system.
- The SD Card driver makes use of a DMA driver achieving a decent 4 MBytes/sec rate on files in a FAT32 filesystem.

The initial set-up is an Embedded Artists' LPC1788 development board with an SD Card with a FAT32 filesystem inserted into the SD Card slot.

Boot the Linux kernel and see the kernel log messages:

<code>
[...]
mmci-pl18x dev:mmc0: DMA buffer: phy:a0d20000, virt:a0d20000
mmci-pl18x dev:mmc0: mmc0: MMCI rev 0 cfg 00 at 0x00000000400c0000 irq 29,-1
[...]
mmc0: host does not support reading read-only switch. assuming write-enable.
mmc0: new SDHC card at address b368
mmcblk0: mmc0:b368 SD 3.84 GiB
mmcblk0: p1
[...]
</code>

Mount the FAT32 file system on the first partion of the SD Card:

<code>
~ # mount /dev/mmcblk0p1 /mnt/
~ # cat /proc/mounts
rootfs / rootfs rw 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,relatime 0 0
none /dev/pts devpts rw,relatime,mode=600,ptmxmode=000 0 0 /dev/mmcblk0p1 /mnt vfat rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0 0
</code>

Measure the data transfer rate from the SD card by reading a large file from the FAT32 file system:

<code>
~ # cd /mnt/Music/
/mnt/Music # date ; cat FinamFM-VelvetUG.mp3 > /dev/null ; date ; ls -l FinamFM-VelvetUG.mp3
Thu Jan 1 00:00:15 UTC 1970
Thu Jan 1 00:00:28 UTC 1970
-rwxr-xr-x 1 root root 52060058 Oct 31 2011 FinamFM-VelvetUG.mp3
</code>

Copy an executable to the FAT32 and them run it from there:

<code>
~ # cp /bin/busybox /mnt/
~ # /mnt/busybox echo hello1
hello1
</code>

Not, let's test the hot-plug functionality. Bring the target down and remove the SD Card from the slot. Boot the kernel and see the kernel messages.

<code>
[...]
mmci-pl18x dev:mmc0: DMA buffer: phy:a0d20000, virt:a0d20000
mmci-pl18x dev:mmc0: mmc0: MMCI rev 0 cfg 00 at 0x00000000400c0000 irq 29,-1
[...]
</code>

Let's check the list of block devices currently present in the system. No mmcblk0[...] devices are present since there is no SD card inserted into the slot.

<code>
~ # ls -l /sys/block/
~ #
</code>

Insert an SD card with a FAT32 file system into the slot and wait for the Linux kernel to detect it:

<code>
~ # mmc0: host does not support reading read-only switch. assuming write-enable.
mmc0: new SDHC card at address b368
mmcblk0: mmc0:b368 SD 3.84 GiB
mmcblk0: p1
</code>

See the list of block devices currently present in the system:

<code>
~ # ls -l /sys/block/
lrwxrwxrwx 1 root root 0 Jan 1 00:04 mmcblk0 -> ../devices/dev:mmc0/mmc_host/mmc0/mmc0:b368/block/mmcblk0
~ #
</code>

Mount the FAT32 file system on the SD Card:

<code>
~ # mount /dev/mmcblk0p1 /mnt/
~ # /mnt/busybox echo hello2
hello2
</code>

Unmount the file system:

<code>
~ # umount /dev/mmcblk0p1
~ #
</code>

Remove the SD card from the slot. The kernel will detect the card removal:

<code>
~ # mmc0: card b368 removed
</code>

Outcomes