Supporting USB Gadgets in i.MX RT uClinux BSP Print

 

This application note explains how to configure the i.MX RT1050 as the composite USB Gadget providing the USB Ethernet and the Mass storage functions over the USB OTG High-Speed link.

In practical embedded applications, the Ethernet Gadget can be used for organizing TCP/IP-based communications with the i.MX RT1050 over the USB physical link. The Mass Storage Gadget can be used to share data on physical or virtual storage of the IMXRT1050 board to a PC, for example provide access to its SD card.


Configuration and Build

To enable USB Gadgets functionality in the rootfs project, do the following:

  1. Activate the cross development environment:
  2. $ . ./ACTIVATE.sh
    $ cd projects/rootfs

  3. Select the appropriate USB Gadget in the Linux configuration menu (go to the Device Drivers -> USB support -> USB Gadget Support -> USB Gadget functions configurable through configfs menu, and select the RNDIS and Mass storage options):
  4. $ make kmenuconfig

  5. Build the project:
  6. $ make

  7. Run the resultant rootfs.uImage on the target.
  8. Configure the USB Gadgets in the Linux console on the target board:
  9. Mount the configfs File System:
  10. / # mount -t configfs configfs /sys/kernel/config/

  11. Create new composite USB gadget:
  12. / # mkdir /sys/kernel/config/usb_gadget/g1

  13. Add description for new device:
  14. / # cd /sys/kernel/config/usb_gadget/g1
    /sys/kernel/config/usb_gadget/g1 # echo 0x525 > idVendor
    /sys/kernel/config/usb_gadget/g1 # echo 0xa4a7 > idProduct
    /sys/kernel/config/usb_gadget/g1 # mkdir strings/0x409
    /sys/kernel/config/usb_gadget/g1 # echo 00001 > strings/0x409/serialnumber
    /sys/kernel/config/usb_gadget/g1 # echo user > strings/0x409/manufacturer
    /sys/kernel/config/usb_gadget/g1 # echo mygadget > strings/0x409/product

  15. Create configuration for the composite device:
  16. /sys/kernel/config/usb_gadget/g1 # mkdir configs/c.1
    /sys/kernel/config/usb_gadget/g1 # mkdir configs/c.1/strings/0x409
    /sys/kernel/config/usb_gadget/g1 # echo composite > configs/c.1/strings/0x409/configuration
    /sys/kernel/config/usb_gadget/g1 # echo 120 > configs/c.1/MaxPower

  17. Define the mass storage function:
  18. /sys/kernel/config/usb_gadget/g1 # mkdir functions/mass_storage.0
    Mass Storage Function, version: 2009/09/11
    LUN: removable file: (no medium)

  19. Provide whole SD card to mass storage:
  20. /sys/kernel/config/usb_gadget/g1 # echo /dev/mmcblk0 > functions/mass_storage.0/lun.0/file

  21. Define the Ethernet Gadget (RNDIS) function:
  22. /sys/kernel/config/usb_gadget/g1 # mkdir functions/rndis.usb0
    using random self ethernet address
    using random host ethernet address

  23. Link the Gadget functions to the composite device:
  24. /sys/kernel/config/usb_gadget/g1 # ln -s functions/mass_storage.0/ configs/c.1/
    /sys/kernel/config/usb_gadget/g1 # ln -s functions/rndis.usb0 configs/c.1

  25. 14. Enable the USB Gadget on either USB1 J9 (ci_hdrc.0) port:
  26. /sys/kernel/config/usb_gadget/g1 # echo ci_hdrc.1 > UDC


Connect to the Linux Host

The host PC will detect the i.MX RT1170 as 2 devices: RNDIS Ethernet and USB Mass Storage:

$ dmesg | tail -n 24 [11840.525554] usb 2-1: new high-speed USB device number 2 using ehci-pci [11840.810733] usb 2-1: New USB device found, idVendor=0525, idProduct=a4a7, bcdDevice= 5.15 [11840.810773] usb 2-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [11840.810800] usb 2-1: Product: mygadget [11840.810820] usb 2-1: Manufacturer: user [11840.810839] usb 2-1: SerialNumber: 00001 [11840.828070] usb-storage 2-1:1.0: USB Mass Storage device detected [11840.828705] scsi host4: usb-storage 2-1:1.0 [11841.395826] cfg80211: Loading compiled-in X.509 certificates for regulatory database [11841.396272] cfg80211: Loaded X.509 cert 'sforshee: 00b28ddf47aef9cea7' [11841.477143] usbcore: registered new interface driver cdc_ether [11841.535728] rndis_host 2-1:1.1 usb0: register 'rndis_host' at usb-0000:00:0b. 0-1, RNDIS device, fe:cf:a8:d7:4e:14 [11841.536179] usbcore: registered new interface driver rndis_host [11841.627777] usbcore: registered new interface driver rndis_wlan [11841.653113] rndis_host 2-1:1.1 enxfecfa8d74e14: renamed from usb0 [11841.866869] scsi 4:0:0:0: Direct-Access Linux File-Stor Gadget 0515 PQ: 0 ANSI: 2 [11841.870134] sd 4:0:0:0: Attached scsi generic sg3 type 0 [11841.890328] sd 4:0:0:0: Power-on or device reset occurred [11841.909963] sd 4:0:0:0: [sdc] 15564800 512-byte logical blocks: (7.97 GB/7.42 GiB) [11841.921499] sd 4:0:0:0: [sdc] Write Protect is off [11841.921508] sd 4:0:0:0: [sdc] Mode Sense: 0f 00 00 00 [11841.933252] sd 4:0:0:0: [sdc] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA [11842.047093] sdc: sdc1 [11842.055086] sd 4:0:0:0: [sdc] Attached SCSI removable disk


Using the Ethernet Function of the Composite USB Gadget

  1. Setup an IP address of the USB Ethernet interface on the USB Device (i.MX RT1050) side. Select an address that will not conflict with the other network sub nets in the system (if present):
  2. / # ifconfig usb0 192.168.2.2

  3. Setup an IP address of the USB Ethernet interface on the USB host (PC) side (obviously, select the address from the same sub-net as the Gadget):

    Linux host example:

    $ sudo ifconfig enxfebca0601bd5 192.168.2.1

  4. Check communication between i.MX RT1050 and the host over USB with ping:
    • on i.MX RT1050:
    • / # ping 192.168.2.1 -c 3
      PING 192.168.2.1 (192.168.2.1): 56 data bytes
      64 bytes from 192.168.2.1: seq=0 ttl=64 time=3.215 ms
      64 bytes from 192.168.2.1: seq=1 ttl=64 time=2.158 ms
      64 bytes from 192.168.2.1: seq=2 ttl=64 time=2.515 ms

      --- 192.168.2.1 ping statistics ---
      3 packets transmitted, 3 packets received, 0% packet loss
      round-trip min/avg/max = 2.158/2.629/3.215 ms

    • on PC:
    • $ ping 192.168.2.2 -c 3
      PING 192.168.2.2 (192.168.2.2) 56(84) bytes of data.
      64 bytes from 192.168.2.2: icmp_seq=1 ttl=64 time=4.08 ms
      64 bytes from 192.168.2.2: icmp_seq=2 ttl=64 time=3.59 ms
      64 bytes from 192.168.2.2: icmp_seq=3 ttl=64 time=4.63 ms

      --- 192.168.2.2 ping statistics ---
      3 packets transmitted, 3 received, 0% packet loss, time 2004ms
      rtt min/avg/max/mdev = 3.589/4.099/4.627/0.423 ms
      $


Using the Mass Storage Function of the Composite USB Gadget

  1. On the Linux host check the Mass Storage Gadget is automatically mounted:
  2. $ mount | grep "/dev/sd"
    dev/sda3 on / type ext4 (rw,relatime,errors=remount-ro)
    /dev/sda3 on /var/snap/firefox/common/host-hunspell type ext4 (ro,noexec,noatime,errors=remount-ro)
    /dev/sda2 on /boot/efi type vfat (rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)
    /dev/sdb on /media/sasha/RT1050-EVK type vfat (rw,nosuid,nodev,relatime,uid=1001,gid=1001,fmask=0022,dmask=0022,
    codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)
    /dev/sdc1 on /media/sasha/987E-02F8 type vfat
    (rw,nosuid,nodev,relatime,uid=1001,gid=1001,fmask=0022,dmask=0022,
    codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)

  3. Verify that the mounted media contains the boot image:
  4. $ ls /media/sasha/987E-02F8
    rootfs.uImage splash-imxrt-series_24.bmp 'System Volume Information'


Configuration USB Multifunction Composite Gadget via a Script

For convenience the usb_gadget.sh script is included into the rootfs project.

  • To configure USB Gadget as a mass storage device the following command can be used on the target board:
  • / # usb_gadget.sh -m -p 0
    Mass Storage Function, version: 2009/09/11
    LUN: removable file: (no medium)
    USB Gadet config done
    / #

  • To configure USB Gadget as a network device the following command can be used on the target board:
  • / # usb_gadget.sh -r -p 0
    using random self ethernet address
    using random host ethernet address
    usb0: HOST MAC 5a:32:a9:e7:72:85
    usb0: MAC 22:ff:72:5f:e5:64
    USB Gadet config done
    / #

  • To configure USB Gadget as a mass storage and a network devices the following command can be used on the target board:
  • / # usb_gadget.sh -m -r -p 0
    Mass Storage Function, version: 2009/09/11
    LUN: removable file: (no medium)
    using random self ethernet address
    using random host ethernet address
    usb0: HOST MAC 06:b2:3f:33:72:e8
    usb0: MAC a6:b2:16:ae:fa:e1
    USB Gadet config done
    / #

  • To deactivate configured USB composite gadget the following command can be used on the target board:

    / # usb_gadget.sh -d
    USB gadgets deactivated
    / #