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

 

This application note explains how to use a USB Flash device with the USB High Speed (HS) interfaces of the i.MX RT1024 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 RT1024 EVK board.

This demo assumes that a Micro-B to USB 2.0 A Female cable is plugged into the J9 USB interface connector on the NXP i.MX RT1024 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 SPL 2022.04 (Jul 04 2023 - 14:46:31 +0000) Trying to boot from MMC1 U-Boot 2022.04 (Jul 04 2023 - 14:46:31 +0000) DRAM: 32 MiB Core: 60 devices, 13 uclasses, devicetree: separate MMC: FSL_SDHC: 0 Loading Environment from MMC... OK In: serial@40184000 Out: serial@40184000 Err: serial@40184000 Net: eth0: ethernet@402D8000 Hit any key to stop autoboot: 0 6940777 bytes read in 635 ms (10.4 MiB/s) ## Booting kernel from Legacy Image at 80007fc0 ... Image Name: Linux-5.15.71 Image Type: ARM Linux Multi-File Image (uncompressed) Data Size: 6940713 Bytes = 6.6 MiB Load Address: 80008000 Entry Point: 80008001 Contents: Image 0: 6934720 Bytes = 6.6 MiB Image 1: 5981 Bytes = 5.8 KiB Verifying Checksum ... OK ## Flattened Device Tree from multi component Image at 80007FC0 Booting using the fdt at 0x806a50cc Loading Multi-File Image Loading Device Tree to 81d83000, end 81d8775c ... 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 5.15.71 (sasha@workbench.emcraft.com) (arm-none-eabi-gcc (GNU Arm Embedded Toolchain 10.3-2021.10) 10.3.1 20210824 (release), GNU ld (GNU Arm Embedded Toolchain 10.3-2021.10) 2.36.1.20210621) #2 Tue Jul 4 14:44:18 UTC 2023 CPU: ARMv7-M [411fc271] revision 1 (ARMv7M), cr=00000000 CPU: PIPT / VIPT nonaliasing data cache, PIPT instruction cache OF: fdt: Machine model: NXP IMXRT1024 EVK board Reserved memory: created DMA memory pool at 0x81f00000, size 1 MiB OF: reserved mem: initialized node dmapool@81f00000, compatible id shared-dma-pool Zone ranges: Normal [mem 0x0000000080000000-0x0000000081ffffff] Movable zone start for each node Early memory node ranges node 0: [mem 0x0000000080000000-0x0000000081efffff] node 0: [mem 0x0000000081f00000-0x0000000081ffffff] Initmem setup node 0 [mem 0x0000000080000000-0x0000000081ffffff] Built 1 zonelists, mobility grouping off. Total pages: 8128 Kernel command line: clk_ignore_unused ip=192.168.88.89:192.168.88.170: 192.168.88.1:255.255.0.0::eth0:off Unknown kernel command line parameters "ip=192.168.88.89:192.168.88.170: 192.168.88.1:255.255.0.0::eth0:off", will be passed to user space. Dentry cache hash table entries: 4096 (order: 2, 16384 bytes, linear) Inode-cache hash table entries: 2048 (order: 1, 8192 bytes, linear) mem auto-init: stack:off, heap alloc:off, heap free:off Memory: 24532K/32768K available (3266K kernel code, 180K rwdata, 860K rodata, 2264K init, 97K bss, 8236K reserved, 0K cma-reserved) NR_IRQS: 16, nr_irqs: 16, preallocated irqs: 16 Switching to timer-based delay loop, resolution 333ns sched_clock: 32 bits at 3000kHz, resolution 333ns, wraps every 715827882841ns clocksource: mxc_timer1: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 637086815595 ns Calibrating delay loop (skipped), value calculated using timer frequency.. 6.00 BogoMIPS (lpj=30000) pid_max: default: 4096 minimum: 301 Mount-cache hash table entries: 1024 (order: 0, 4096 bytes, linear) Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes, linear) devtmpfs: initialized DMA: default coherent area is set clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns futex hash table entries: 16 (order: -5, 192 bytes, linear) pinctrl core: initialized pinctrl subsystem NET: Registered PF_NETLINK/PF_ROUTE protocol family imxrt1020-pinctrl 401f8000.pinctrl: initialized IMX pinctrl driver imxrt1020-pinctrl 400a8000.pinctrl-snvs: 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 <giometti@linux.it> PTP clock support registered Bluetooth: Core ver 2.22 NET: Registered PF_BLUETOOTH protocol family Bluetooth: HCI device and connection manager initialized Bluetooth: HCI socket layer initialized Bluetooth: L2CAP socket layer initialized Bluetooth: SCO socket layer initialized clocksource: Switched to clocksource mxc_timer1 NET: Registered PF_INET protocol family IP idents hash table entries: 2048 (order: 2, 16384 bytes, linear) tcp_listen_portaddr_hash hash table entries: 512 (order: 0, 4096 bytes, linear) Table-perturb hash table entries: 65536 (order: 6, 262144 bytes, linear) TCP established hash table entries: 1024 (order: 0, 4096 bytes, linear) TCP bind hash table entries: 1024 (order: 0, 4096 bytes, linear) TCP: Hash tables configured (established 1024 bind 1024) UDP hash table entries: 256 (order: 0, 4096 bytes, linear) UDP-Lite hash table entries: 256 (order: 0, 4096 bytes, linear) NET: Registered PF_UNIX/PF_LOCAL protocol family 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. Bus freq driver module loaded Initialise system trusted keyrings workingset: timestamp_bits=30 max_order=13 bucket_order=0 fuse: init (API version 7.34) Key type asymmetric registered Asymmetric key parser 'x509' registered Block layer SCSI generic (bsg) driver version 0.4 loaded (major 251) io scheduler mq-deadline registered io scheduler kyber registered 40184000.serial: ttyLP0 at MMIO 0x40184010 (irq = 20, base_baud = 1250000) is a FSL_LPUART fsl-lpuart 40184000.serial: Serial: Console lpuart rounded baud ratefrom 208333 to 115200 printk: console [ttyLP0] enabled PPP generic driver version 2.4.2 PPP BSD Compression module registered PPP Deflate Compression module registered usbcore: registered new interface driver rt2800usb ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver usbcore: registered new interface driver cdc_acm cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters usbcore: registered new interface driver uas usbcore: registered new interface driver usb-storage udc-core: couldn't find an available UDC - added [g_serial] to list of pending drivers i2c_dev: i2c /dev entries driver usbcore: registered new interface driver btusb sdhci: Secure Digital Host Controller Interface driver sdhci: Copyright(c) Pierre Ossman sdhci-pltfm: SDHCI platform and OF driver helper usbcore: registered new interface driver usbhid usbhid: USB HID core driver NET: Registered PF_PACKET protocol family mmc0 bounce up to 128 segments into one, max segment size 65536 bytes Bluetooth: RFCOMM TTY layer initialized Bluetooth: RFCOMM socket layer initialized Bluetooth: RFCOMM ver 1.11 Loading compiled-in X.509 certificates ci_hdrc ci_hdrc.0: EHCI Host Controller ci_hdrc ci_hdrc.0: new USB bus registered, assigned bus number 1 mmc0: SDHCI controller on 402c0000.mmc [402c0000.mmc] using DMA ci_hdrc ci_hdrc.0: USB 2.0 started, EHCI 1.00 hub 1-0:1.0: USB hub found hub 1-0:1.0: 1 port detected input: gpio-keys as /devices/platform/gpio-keys/input/input0 cfg80211: Loading compiled-in X.509 certificates for regulatory database cfg80211: Loaded X.509 cert 'sforshee: 00b28ddf47aef9cea7' clk: Not disabling unused clocks platform regulatory.0: Direct firmware load for regulatory.db failed with error -2 cfg80211: failed to load regulatory.db Freeing unused kernel image (initmem) memory: 2264K This architecture does not have kernel memory protection. Run /init as init process init started: BusyBox v1.24.2 (2023-07-04 14:44:03 UTC) [69] Jan 01 00:00:01 Running in background mmc0: host does not support reading read-only switch, assuming write-enable / # mmc0: new SDHC card at address 0001 mmcblk0: mmc0:0001 00000 7.37 GiB mmcblk0: p1 Micrel KSZ8081 or KSZ8091 402d8000.ethernet-1:02: attached PHY driver (mii_bus:phy_addr=402d8000.ethernet-1:02, irq=POLL) fec 402d8000.ethernet eth0: Link is Up - 100Mbps/Full - flow control off

USB OTG 1 Controller (USB OTG)

Connect just a Micro-B to USB 2.0 A Female cable to J9 connector.

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

usb 1-1: new full-speed USB device number 2 using ci_hdrc
usb 1-1: not running at top speed; connect to a high speed hub
usb-storage 1-1:1.0: USB Mass Storage device detected
scsi host0: usb-storage 1-1:1.0
scsi 0:0:0:0: Direct-Access Generic Flash Disk 8.07 PQ: 0 ANSI: 2
sd 0:0:0:0: [sda] 8228864 512-byte logical blocks: (4.21 GB/3.92 GiB)
sd 0:0:0:0: [sda] Write Protect is off
sd 0:0:0:0: [sda] No Caching mode page found
sd 0:0:0:0: [sda] Assuming drive cache: write through
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: 4213 MB, 4213178368 bytes 255 heads, 63 sectors/track, 512 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 513 4113408 c Win95 FAT32 (LBA) Partition 1 has different physical/logical endings: phys=(511, 254, 63) logical=(512, 56, 56)

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

/ # mount /dev/sda1 /mnt/usbflash
/ # ls -l /mnt/usbflash/
drwxr-xr-x 2 root root 4096 May 25 2023 System Volume Information
/ #

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 drwxr-xr-x 2 root root 4096 May 25 2023 System Volume Information / #

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:03:35 UTC 1970 Thu Jan 1 00:03:36 UTC 1970 Thu Jan 1 00:03:37 UTC 1970 Thu Jan 1 00:03:38 UTC 1970 Thu Jan 1 00:03:39 UTC 1970 Thu Jan 1 00:03:40 UTC 1970 Thu Jan 1 00:03:41 UTC 1970 Thu Jan 1 00:03:42 UTC 1970 / #

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

/ # umount /mnt/usbflash

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.

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

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 full-speed USB device number 3 using ci_hdrc usb 1-1: not running at top speed; connect to a high speed hub usb-storage 1-1:1.0: USB Mass Storage device detected scsi host0: usb-storage 1-1:1.0 scsi 0:0:0:0: Direct-Access Generic Flash Disk 8.07 PQ: 0 ANSI: 2 sd 0:0:0:0: [sda] 8228864 512-byte logical blocks: (4.21 GB/3.92 GiB) sd 0:0:0:0: [sda] Write Protect is off sd 0:0:0:0: [sda] No Caching mode page found sd 0:0:0:0: [sda] Assuming drive cache: write through sda: sda1 sd 0:0:0:0: [sda] Attached SCSI removable disk/ # mount /dev/sda1 /mnt/usbflash / # ls -l /mnt/usbflash drwxr-xr-x 2 root root 4096 May 25 2023 System Volume Information -rwxr-xr-x 1 root root 232 Jan 1 1980 data.log


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 none 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) devpts on /dev/pts type devpts (rw,relatime,gid=5,mode=620,ptmxmode=000) /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.