Using SD Card in Linux |
The STM32F7 SD Card device driver is linux/drivers/mmc/host/mmci.c. To enable the SD Card support in the kernel, you must enable the CONFIG_MMC_ARMMMCI build time option in the kernel configuration. To do so, start the kernel configuration GUI (make kmenuconfig from the project directory) and proceed to enable the following item: Device Drivers -> MMC/SD/SDIO card support. Then enter the MMC/SD/SDIO card support menu and enable the following options: Then, return to the top level menu, enter the System Type -> STM32 I/O interfaces menu, and check the Enable STM32 SD Card interface and Use DMA for the SD Card Interface options: The most typical use of SD Card in embedded applications is a removable storage device (disk) that can be easily taken from the embedded target to a PC or notebook. In such contexts, SD Card installed to the embedded target is typically already formatted with an MS-DOS file system. The Linux kernel must be specially configured to allow mounting the MS-DOS file system. To perform the configuration, start the kernel configuration interface, proceed to File systems -> DOS/FAT/NT Filesystems and enable the following kernel options: The two additional kernel configuration options that, depending on your application needs, may be needed to mount an MS-DOS file system on SD Card are illustrated below: and: In the target file system, there must be appropriate device nodes to access the SD Card using standard Linux tools and utilities. Add the following lines to your <project>.initramfs file to create the device nodes for the SD Card: nod /dev/mmcblk0 0600 0 0 b 179 0 On the target, you will require the mount utility in order to mount an MS-DOS file system on the SD Card. In uClinux, mount is available as part of the multi-call busybox utility. Typically, mount will already be enabled in your busybox configuration, but in case it is not, run the busybox configuration GUI (make bmenuconfig from the project directory), proceed to Linux System Utilities and enable the following item: You will also need to add a symlink for mount to the target file system. This is done by adding the following line to the <project>.initramfs file (if the line is not already there): slink /bin/mount busybox 777 0 0 Having updated your project configuration as described above, build the bootable Linux image (<project>.uImage) by running make from the project directory. Install the newly built uImage file to the Flash on the target as described in Installing Linux Images to Flash or load it to RAM via Ethernet as described in Loading Linux Images via Ethernet and TFTP. Insert a pre-formatted card with an MS-DOS file system to the SD Card slot on the Discovery board. When you boot the uImage on the STM32F7, 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: ...... 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 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 Write throughput to an 8GB Kingston SDHC card class 10 is measured to be the following: ~ # dd if=/dev/zero of=/mnt/10m bs=1M count=10 Read throughput using the same SDHC card is as follows: ~ # umount /mnt Ok, you have validated that we can write data to the SD Card. Now you remove the card the 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 STM32F7. Messages similar to the ones shown below should appear on the Linux console: mmc0: new SD card at address aaaa Mount the MS-DOS file system and verify the content of the previously created log file: ~ # mount /dev/mmcblk0p1 /mnt |