Using USB Flash with the USB High Speed Interfaces on the i.MX RT1060 Print

 

This application note explains how to use a USB Flash device with the USB High Speed (HS) interfaces of the i.MX RT1060 microcontroller running uCLinux. The chip has Universal Serial Bus 2.0 Integrated PHY which contains two integrated USB 2.0 PHY macrocells capable of connecting to USB host/device systems at the USB low-, full-, and high-speed rates.


Hardware Platform

The hardware platform is the NXP i.MX RT1060 EVK board.

This demo assumes that a Micro-B to USB 2.0 A Female cable is plugged into the J9 or J10 USB interface connector on the NXP i.MX RT1060 EVK board 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.


Logging Data onto USB Flash

On power-up or reset, U-Boot loads the Linux and Device Tree images from the SD Card to the SDRAM and passes control to the kernel entry point:

U-Boot 2017.09-imxrt-2.5.2 (Nov 22 2018 - 01:47:42 +0300) CPU: i.MX RT106x at 600MHz Model: NXP i.RT1060 EVK DRAM: 32 MiB MMC: FSL_SDHC: 0 reading uboot.env Video: 480x272x24 In: serial@40184000 Out: serial@40184000 Err: serial@40184000 Net: eth0: ethernet@402D8000 reading splash-rt1050-series_24.bmp 391736 bytes read in 109 ms (3.4 MiB/s) reading mxrt106x-evk.ini 102 bytes read in 12 ms (7.8 KiB/s) fatexec: 'setenv ipaddr 192.168.1.141' fatexec: 'setenv netmask 255.255.255.0' Hit any key to stop autoboot: 0 reading rootfs.uImage 7749634 bytes read in 1772 ms (4.2 MiB/s) ## Booting kernel from Legacy Image at 80007fc0 ... Image Name: Linux-4.5.0 Image Type: ARM Linux Multi-File Image (uncompressed) Data Size: 7749570 Bytes = 7.4 MiB Load Address: 80008000 Entry Point: 80008001 Contents: Image 0: 7739904 Bytes = 7.4 MiB Image 1: 9654 Bytes = 9.4 KiB Verifying Checksum ... OK ## Flattened Device Tree from multi component Image at 80007FC0 Booting using the fdt at 0x80769a0c Loading Multi-File Image ... OK Loading Device Tree to 81e77000, end 81e7c5b5 ... OK Starting kernel ...

The kernel proceeds to boot-up, initializing the configured I/O interfaces and sub-systems:

Booting Linux on physical CPU 0x0 Linux version 4.5.0-00417-g1e71634a15d7-dirty (yur@linux-7d45) (gcc version 4.7.4 20130508 (prerelease) (20170818-165657- build on build.emcraft by build) ) #153 Wed Oct 18 21:58:40 MSK 2017 CPU: ARMv7-M [411fc271] revision 1 (ARMv7M), cr=00000000 CPU: WBA data cache, WBA instruction cache Machine model: NXP IMXRT1060 board debug: ignoring loglevel setting. On node 0 totalpages: 8192 free_area_init_node: node 0, pgdat 80759320, node_mem_map 81ec0000 Normal zone: 64 pages used for memmap Normal zone: 0 pages reserved Normal zone: 8192 pages, LIFO batch:0 pcpu-alloc: s0 r0 d32768 u32768 alloc=1*32768 pcpu-alloc: [0] 0 Built 1 zonelists in Zone order, mobility grouping off. Total pages: 8128 Kernel command line: console=ttyLP0,115200 consoleblank=0 ignore_loglevel ip=192.168.1.141:192.168.1.65::255.255.255.0::eth0:off PID hash table entries: 128 (order: -3, 512 bytes) Dentry cache hash table entries: 4096 (order: 2, 16384 bytes) Inode-cache hash table entries: 2048 (order: 1, 8192 bytes) Memory: 23792K/32768K available (2188K kernel code, 118K rwdata, 360K rodata, 4824K init, 95K bss, 8976K reserved, 0K cma-reserved) Virtual kernel memory layout: vector : 0x00000000 - 0x00001000 ( 4 kB) fixmap : 0xffc00000 - 0xfff00000 (3072 kB) vmalloc : 0x00000000 - 0xffffffff (4095 MB) lowmem : 0x80000000 - 0x82000000 ( 32 MB) .text : 0x80008000 - 0x8028531c (2549 kB) .init : 0x80286000 - 0x8073c000 (4824 kB) .data : 0x8073c000 - 0x80759a00 ( 119 kB) .bss : 0x80759a00 - 0x807719f8 ( 96 kB) NR_IRQS:16 nr_irqs:16 16 sched_clock: 32 bits at 75MHz, resolution 13ns, wraps every 28633115129ns clocksource: vf-pit: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 25483472618 ns Calibrating delay loop... 1196.85 BogoMIPS (lpj=5984256) pid_max: default: 4096 minimum: 301 Mount-cache hash table entries: 1024 (order: 0, 4096 bytes) Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes) devtmpfs: initialized clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns pinctrl core: initialized pinctrl subsystem NET: Registered protocol family 16 imxrt105x-pinctrl 401f8000.iomuxc: initialized IMX pinctrl driver SCSI subsystem initialized usbcore: registered new interface driver usbfs usbcore: registered new interface driver hub usbcore: registered new device driver usb pps_core: LinuxPPS API ver. 1 registered pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti < This e-mail address is being protected from spambots. You need JavaScript enabled to view it > PTP clock support registered clocksource: Switched to clocksource vf-pit NET: Registered protocol family 2 TCP established hash table entries: 1024 (order: 0, 4096 bytes) TCP bind hash table entries: 1024 (order: 0, 4096 bytes) TCP: Hash tables configured (established 1024 bind 1024) UDP hash table entries: 256 (order: 0, 4096 bytes) UDP-Lite hash table entries: 256 (order: 0, 4096 bytes) RPC: Registered named UNIX socket transport module. RPC: Registered udp transport module. RPC: Registered tcp transport module. RPC: Registered tcp NFSv4.1 backchannel transport module. futex hash table entries: 16 (order: -5, 192 bytes) fuse init (API version 7.24) Block layer SCSI generic (bsg) driver version 0.4 loaded (major 252) io scheduler noop registered io scheduler cfq registered (default) mxsfb 402b8000.lcdif: initialized 40184000.serial: ttyLP0 at MMIO 0x40184000 (irq = 44, base_baud = 375000) is a FSL_LPUART console [ttyLP0] enabled Serial: VF610 driver libphy: fec_enet_mii_bus: probed

The USB controller and USB mass storage device driver are initialized. Note that only one USB controller of two is inialized here. It is USB OTG2 (with J10 (USB HOST) connector). The second USB controller (USB OTG1 with J9 (USB OTG) connector) will be initialized as soon as we connect a cable to the J9 connector.

ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver ehci-mxc: Freescale On-Chip EHCI Host driver usbcore: registered new interface driver uas usbcore: registered new interface driver usb-storage ci_hdrc ci_hdrc.1: EHCI Host Controller ci_hdrc ci_hdrc.1: new USB bus registered, assigned bus number 1 ci_hdrc ci_hdrc.1: USB 2.0 started, EHCI 1.00 hub 1-0:1.0: USB hub found hub 1-0:1.0: 1 port detected g_serial gadget: Gadget Serial v2.4 g_serial gadget: g_serial ready i2c /dev entries driver ft5x0x_ts 0-0038: resolution: 479x271 ft5x0x_ts 0-0038: Operating withoug nReset pin (-2) input: ft5x0x_ts as /devices/virtual/input/input0 [FTS] Firmware version = 0x12 [FTS] report rate is 90Hz. [FTS] touch threshold is 92. i2c i2c-0: LPI2C adapter registered sdhci: Secure Digital Host Controller Interface driver sdhci: Copyright(c) Pierre Ossman sdhci-pltfm: SDHCI platform and OF driver helper sdhci-esdhc-imx 402c0000.esdhc: could not get default state /soc/aips-bus@40000000/esdhc@402c0000: voltage-ranges unspecified mmc0: SDHCI controller on 402c0000.esdhc [402c0000.esdhc] using DMA usbcore: registered new interface driver usbhid usbhid: USB HID core driver NET: Registered protocol family 10 sit: IPv6 over IPv4 tunneling driver input: gpio-keys as /devices/platform/gpio-keys/input/input1 Freeing unused kernel memory: 4824K (80286000 - 8073c000) init started: BusyBox v1.24.2 (2017-10-18 21:56:31 MSK) mmc0: host does not support reading read-only switch, assuming write-enable mmc0: new high speed SD card at address b368 mmcblk0: mmc0:b368 00000 1.86 GiB mmcblk0: p1 FAT-fs (mmcblk0p1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck. random: dropbear urandom read with 5 bits of entropy available / # Micrel KSZ8081 or KSZ8091 402d8000.etherne:02: attached PHY driver [Micrel KSZ8081 or KSZ8091] (mii_bus:phy_addr=402d8000.etherne:02, irq=-1) IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready fec 402d8000.ethernet eth0: Link is Up - 100Mbps/Full - flow control rx/tx IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready / #


USB OTG 1 Controller (USB OTG)

Connect just a Micro-B to USB 2.0 A Female cable to J9 connector. Observe the i.MX has determined its OTG role correctly:

ci_hdrc ci_hdrc.0: EHCI Host Controller
ci_hdrc ci_hdrc.0: new USB bus registered, assigned bus number 2
ci_hdrc ci_hdrc.0: USB 2.0 started, EHCI 1.00
hub 2-0:1.0: USB hub found
hub 2-0:1.0: 1 port detected

Connect USB Flash device to the cable. Observe it is detected and configured:

usb 2-1: new high-speed USB device number 2 using ci_hdrc
usb-storage 2-1:1.0: USB Mass Storage device detected
scsi host0: usb-storage 2-1:1.0
scsi 0:0:0:0: Direct-Access SanDisk Cruzer Edge 1.26 PQ: 0 ANSI: 5
sd 0:0:0:0: [sda] 7821312 512-byte logical blocks: (4.00 GB/3.73 GiB)
sd 0:0:0:0: [sda] Write Protect is off
sd 0:0:0:0: [sda] Mode Sense: 43 00 00 00
sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
sda: sda1
sd 0:0:0:0: [sda] Attached SCSI removable disk

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 4GBytes disk partitioned to have a single empty FAT32 partition:

/ # fdisk -l /dev/sda Disk /dev/sda: 4004 MB, 4004511744 bytes 116 heads, 51 sectors/track, 1322 cylinders Units = cylinders of 5916 * 512 = 3028992 bytes Device Boot Start End Blocks Id System /dev/sda1 1 1322 3910450+ b Win95 FAT32

Let's mount the FAT32 file system. As expected, it is empty at this point:

/ # mount /dev/sda1 /mnt/usbflash
/ # ls -l /mnt/usbflash/
/ #

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/usbflash/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:

/ # cat /mnt/usbflash/data.log Thu Jan 1 00:14:02 UTC 1970 Thu Jan 1 00:14:03 UTC 1970 Thu Jan 1 00:14:04 UTC 1970 Thu Jan 1 00:14:05 UTC 1970 Thu Jan 1 00:14:06 UTC 1970 Thu Jan 1 00:14:07 UTC 1970 Thu Jan 1 00:14:08 UTC 1970 Thu Jan 1 00:14:09 UTC 1970 Thu Jan 1 00:14:10 UTC 1970 Thu Jan 1 00:14:11 UTC 1970 Thu Jan 1 00:14:12 UTC 1970 Thu Jan 1 00:14:13 UTC 1970 Thu Jan 1 00:14:14 UTC 1970 / #

Now, let's unmount the USB Flash and unplug the device from the USB cable:

/ # umount /mnt/usbflash
/ # usb 2-1: USB disconnect, device number 2

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 2-1: new high-speed USB device number 4 using ci_hdrc usb-storage 2-1:1.0: USB Mass Storage device detected scsi host2: usb-storage 2-1:1.0 scsi 2:0:0:0: Direct-Access SanDisk Cruzer Edge 1.26 PQ: 0 ANSI: 5 sd 2:0:0:0: [sda] 7821312 512-byte logical blocks: (4.00 GB/3.73 GiB) sd 2:0:0:0: [sda] Write Protect is off sd 2:0:0:0: [sda] Mode Sense: 43 00 00 00 sd 2:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA sda: sda1 sd 2:0:0:0: [sda] Attached SCSI removable disk / # mount /dev/sda1 /mnt/usbflash / # ls -lt /mnt/usbflash -rwxr-xr-x 1 root root 377 Jan 1 1980 data.log

USB OTG 2 Controller (USB HOST)

Connect a Micro-B to USB 2.0 A Female cable to the J10 connector. Repeat the same steps as with USB OTG 1 above:

usb 1-1: new high-speed USB device number 2 using ci_hdrc usb-storage 1-1:1.0: USB Mass Storage device detected scsi host3: usb-storage 1-1:1.0 scsi 3:0:0:0: Direct-Access SanDisk Cruzer Edge 1.26 PQ: 0 ANSI: 5 sd 3:0:0:0: [sda] 7821312 512-byte logical blocks: (4.00 GB/3.73 GiB) sd 3:0:0:0: [sda] Write Protect is off sd 3:0:0:0: [sda] Mode Sense: 43 00 00 00 sd 3:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA sda: sda1 sd 3:0:0:0: [sda] Attached SCSI removable disk / # mount /dev/sda1 /mnt/usbflash/ / # while true; do date >> /mnt/usbflash/data.log; sleep 1; done ^C / # cat /mnt/usbflash/data.log Thu Jan 1 00:18:19 UTC 1970 Thu Jan 1 00:18:20 UTC 1970 Thu Jan 1 00:18:21 UTC 1970 Thu Jan 1 00:18:22 UTC 1970 / # umount /mnt/usbflash/ / # usb 1-1: USB disconnect, device number 2 usb 1-1: new high-speed USB device number 3 using ci_hdrc usb-storage 1-1:1.0: USB Mass Storage device detected scsi host4: usb-storage 1-1:1.0 scsi 4:0:0:0: Direct-Access SanDisk Cruzer Edge 1.26 PQ: 0 ANSI: 5 sd 4:0:0:0: [sda] 7821312 512-byte logical blocks: (4.00 GB/3.73 GiB) sd 4:0:0:0: [sda] Write Protect is off sd 4:0:0:0: [sda] Mode Sense: 43 00 00 00 sd 4:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA sda: sda1 sd 4:0:0:0: [sda] Attached SCSI removable disk / # mount /dev/sda1 /mnt/usbflash/ / # ls -l /mnt/usbflash/ -rwxr-xr-x 1 root root 116 Jan 1 1980 data.log / # umount /mnt/usbflash/

Read / Write Performance

Write and read throughputs to the above 4GB USB Flash are measured using the dd command as described below. To make experiments more clean we format USB Flash (in PC) before testing with each next USB OTG x controller.


USB OTG 1 Controller

Connect USB Flash device to the J9 connector, mount it, then measure:

ci_hdrc ci_hdrc.0: EHCI Host Controller ci_hdrc ci_hdrc.0: new USB bus registered, assigned bus number 2 ci_hdrc ci_hdrc.0: USB 2.0 started, EHCI 1.00 hub 2-0:1.0: USB hub found hub 2-0:1.0: 1 port detected usb 2-1: new high-speed USB device number 3 using ci_hdrc usb-storage 2-1:1.0: USB Mass Storage device detected scsi host6: usb-storage 2-1:1.0 scsi 6:0:0:0: Direct-Access SanDisk Cruzer Edge 1.26 PQ: 0 ANSI: 5 sd 6:0:0:0: [sda] 7821312 512-byte logical blocks: (4.00 GB/3.73 GiB) sd 6:0:0:0: [sda] Write Protect is off sd 6:0:0:0: [sda] Mode Sense: 43 00 00 00 sd 6:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA sda: sda1 sd 6:0:0:0: [sda] Attached SCSI removable disk / # mount /dev/sda1 /mnt/usbflash/

Write throughput:

/ # dd if=/dev/zero of=/mnt/usbflash/10m bs=1M count=10 10+0 records in 10+0 records out 10485760 bytes (10.0MB) copied, 0.848279 seconds, 11.8MB/s / # umount /mnt/usbflash / # mount /dev/sda1 /mnt/usbflash

Read throughput:

/ # dd if=/mnt/usbflash/10m of=/dev/null bs=1M count=10 10+0 records in 10+0 records out 10485760 bytes (10.0MB) copied, 0.549217 seconds, 18.2MB/s / # umount /mnt/usbflash usb 2-1: USB disconnect, device number 3 ci_hdrc ci_hdrc.0: USB bus 2 deregistered


USB OTG 2 Controller

Connect USB Flash device to the J10 connector, mount it, then measure:

usb 1-1: new high-speed USB device number 4 using ci_hdrc usb-storage 1-1:1.0: USB Mass Storage device detected scsi host7: usb-storage 1-1:1.0 scsi 7:0:0:0: Direct-Access SanDisk Cruzer Edge 1.26 PQ: 0 ANSI: 5 sd 7:0:0:0: [sda] 7821312 512-byte logical blocks: (4.00 GB/3.73 GiB) sd 7:0:0:0: [sda] Write Protect is off sd 7:0:0:0: [sda] Mode Sense: 43 00 00 00 sd 7:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA sda: sda1 sd 7:0:0:0: [sda] Attached SCSI removable disk / # mount /dev/sda1 /mnt/usbflash/

Write throughput:

/ # dd if=/dev/zero of=/mnt/usbflash/10m bs=1M count=10 10+0 records in 10+0 records out 10485760 bytes (10.0MB) copied, 0.828554 seconds, 12.1MB/s / # umount /mnt/usbflash / # mount /dev/sda1 /mnt/usbflash

Read throughput:

/ # dd if=/mnt/usbflash/10m of=/dev/null bs=1M count=10 10+0 records in 10+0 records out 10485760 bytes (10.0MB) copied, 0.544694 seconds, 18.4MB/s / # umount /mnt/usbflash usb 1-1: USB disconnect, device number 4


Data Synchronization Considerations

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/usbflash / # 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,mode=0755) devpts on /dev/pts type devpts (rw,relatime,gid=5,mode=620,ptmxmode=000) /dev/mmcblk0p1 on /mnt/sdcard type vfat (rw,relatime,fmask=0022,dmask=0022, codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro) /dev/sda1 on /mnt/usbflash type vfat (rw,sync,relatime,fmask=0022,dmask=0022, codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)

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.