|Using USB Flash with the USB High Speed Interface on the LPC4357|
This application note explains how to use a USB Flash device with the USB High Speed (HS) interface of the Embedded Artists LPC4357 Dev Kit running uCLinux.
The hardware platform is the Embedded Artists LPC4357 Dev Kit. This demo assumes that a mini-USB cable is plugged into the USB0 interface connector J32 on the baseboard and that a pre-formatted USB Flash disk with an FAT32 partition is plugged into the USB 2.0 A Female connector of the above USB cable.
Note: The Linux image and the sample project have been built and validated in context of the Emcraft Systems Release 1.14.0. If you are using a different release, some porting changes may be needed.
When you boot the Linux image to the target, there will be the following messages on the system console indicating that the USB High-Speed controller and USB mass storage device driver have been initialized:
The following messages indicate the USB Flash has been detected and configured:
usb 1-1: new high speed USB device using lpc43xx-ehci and address 2
At this point, the USB Flash is accessible as a disk. The following command is used to examine the disk, which is detected as a 8 GBytes disk partitioned to have a single empty FAT32 partition:
~ # fdisk -l /dev/sda
Let's mount the FAT32 file system. As expected, it is empty at this point:
~ # mount /dev/sda1 /mnt
Let's "harvest" some data and store what is collected into a file on the USB Flash disk. In this demo, we emulate a data stream by taking a snapshot of the system time each second:
~ # while true; do date >> /mnt/data.log; sleep 1; done
Having let the "data harvesting" run for a few seconds, let's interrupt it (by pressing ^-C) and take a look at what data we have collected:
Now, let's unmount the USB Flash and unplug the device from the USB connector:
~ # umount /mnt/
At this point, the USB Flash device can be taken to a PC for further data processing. Just plug in the USB Flash into a USB port on your PC and the PC software will be able to mount the device as a FAT32 file system.
Note that the format of Windows and Unix text files differs slightly. In Windows, lines end with both the line feed and carriage return ASCII characters, but Unix uses only a line feed. As a consequence, some Windows applications will not show the line breaks in Unix-format files. Assuming that data is stored in a text file (vs a binary file) and Windows is a data processing host, Linux data harvesting applications should take care of the difference by adding a carriage return character to data logs.
Note further that you can hot plug your USB Flash device on the running system at any time:
~ # usb 1-1: new high speed USB device using lpc43xx-ehci and address 3
Write throughput to the above USB Flash is measured to be as follows:
~ # dd if=/dev/zero of=/mnt/10m bs=1M count=10
Read throughput using the same USB Flash device is as follows:
~ # umount /mnt
It is important to understand that VFAT supports write-back in Linux, which means that file changes do not go to the physical media straight away and instead are cached in memory and go to the Flash at a later time. This helps to reduce amount to I/O to the physical Flash, resulting in a better performance overall.
The write-back creates a certain issue for embedded devices however. If the power to the device is shut down unexpectedly, or the USB Flash is unplugged without a proper unmount or sync, some of latest file changes may be lost.
As it is typical with Linux, the issue can be handled in many ways. Data synchronization can be ensured on a per-file, per-subtree, per-filesystem or system-wide basis. Synchronization can be transparent for the user or may require issuing an explicit API call or a shell command.
The most obvious solution is to mount the file system in synchronous mode (note the -o sync parameter in the call below):
~ # mount -o sync /dev/sda1 /mnt
When the file system is mounted for synchronous operation, Linux guarantees that data is written to the physical media before any write() returns to a calling application. The tradeoff is that written data is no longer cached in memory, which reduces the write performance substantially.