Connecting the dots: A practical guide for USB OTG on embedded platforms

Connecting the dots: A practical guide for USB OTG on embedded platforms

USB On-The-GO (OTG) support on Linux 2.6 was initially developed by Texas Instruments for OMAP 16xx and 17xx series processors. Other OTG systems should work in similar ways, but the hardware level details could be very different.

Systems need specialized hardware support to implement OTG, notably including a special Mini-AB jack and associated transceiver to support.

USB OTG can be act as "HOST" or "GADGET"

Dual-Role operation: They can act either as a host, using the standard Linux-USB host side driver stack, or as a peripheral, using this framework. To do that, the system software relies on small additions to those programming interfaces, and on a new internal component (here called an "OTG Controller") affecting which driver stack connects to the OTG port.

USB OTG as a Host
USB OTG as Gadgets

The above images explain what type of device can be connected via a USB-OTG port. When OTG behaves as HOST, we can connect devices, which may be input or output devices. When it behaves as a gadget, it mostly uses to communicate between the host PC and the target device.

How to bring up a USB OTG port in an embedded platform

As described in the below image, there are 5 lines connected between the OTG port and the embedded platform.

  • USB_OTG_VBUS --> This is the power line, which is mostly 5 volts required for this USB port. This can be fed via power regulators or a direct 5V supply.

  • D+ D- --> These two are the data lines.

  • USB_OTG_ID --> This is a very important pin, as this pin determines what the current role of the USB OTG port is. If the USB_OTG_ID pin is grounded (connected to the ground), the device assumes the peripheral role. In this role, the device acts as a typical USB device. If the USB_OTG_ID pin is floating, the device can initially act as a host. However, if the device detects that it is connected to another USB device that is supposed to act as the host (e.g., a USB OTG adapter or another OTG-capable device), it can dynamically switch its role to that of a peripheral.

  • GND --> This is a typical ground pin.

During the bringup of this USB port, it is very simple; we have to just provide the proper power supply and configure the MUX related to the USB OTG port.

For example dts enrty for USB OTG in NXP:

Serial data transfer using USB-OTG Port

The gadget serial driver is a Linux USB gadget driver, a USB device side driver. It runs on a Linux system that has USB device side hardware. The gadget serial driver talks over USB to either a CDC ACM driver or a generic USB serial driver running on a host PC. On the device-side Linux system, the gadget serial driver looks like a serial device.

  1. Load the serial driver in Linux based device platform

    # modprobe g_serial

  2. Connect the target to your machine via the Micro USB cable. Linux will create a device node called /dev/ttyGS0 on the target side and a device node called /dev/ttyACM0 on the host side.

  3. Verify dev node in Target

    # ls -l /dev/ttyGS0

    crw-rw---- 1 root dialout 235, 0 May 2 07:57 /dev/ttyGS0

  4. Verify dev node in Host

    $ ls -l /dev/ttyACM0

    crw-rw---- 1 root dialout 166, 0 May 30 12:35 /dev/ttyACM0

  5. Perform settings in host using stty

    $ sudo stty -F /dev/ttyACM0 raw -echo -echoe -echok -echoctl -echoke

  6. Perform settings in target using stty

    # stty -F /dev/ttyGS0 raw -echo -echoe -echok -echoctl -echoke

  7. Read the contents arriving at /dev/ttyACM0

    $ sudo cat /dev/ttyACM0

  8. Write the text in target /dev/ttyGS0 that will be transfer to host /dev/ttyACM0

    # echo "testing Micro usb" > /dev/ttyGS0

  9. Unload the USB Gadget Serial driver

    # modprobe -r g_serial

How to Use USB Gadgets for File storage

The g_mass_storage driver allows a target device to appear as a USB Mass Storage device to a host system. It allows you to choose either a block device (such as a hard drive, MTD partition, or flash card) or a backing file to act as the backing storage for this device.

  1. Enable the USB Gadget mass Storage

    # modprobe g_mass_storage file=/dev/mmcblk1p3

  2. Connect the target to your machine via the Micro USB cable. you can see the /dev/mmcblk1p3(device root file system partition) mounted in host system

    $ ls /media/user/root/

    bin boot dev etc home lib lib64 lost+found media mnt opt proc run sbin sys tmp unit_tests usr var

  3. Test the file transfer from host system to target device

    $ sudo cp -a usb_otg_test.txt /media/$USER/root/home/root/

  4. Verify the file copied in /home/root directory of target device

    # ls -l usb_otg_test.txt

    -rw-rw-r-- 1 1000 1000 861 May 30 2023 usb_otg_test.txt

  5. Unload the USB Gadget mass Storage

    # modprobe -r g_mass_storage

How to use USB Gadgets Ethernet

An Ethernet controller can add lots of complexity and cost to an embedded board, and consume a large amount of PCB real-estate. Most consumer embedded systems omit an Ethernet port completely, as do some development boards. The Linux USB gadget subsystem provides a way to leverage existing hardware (many devices have a USB device port) to use utilities such as NFS, SSH, and FTP. This is accomplished by using the g_ether driver. It creates an interface called usb0 on the target. Once the USB cable is connected to the host, it creates an interface known as usb0 on the host machine.

  1. Load the USB Gadgets Ethernet driver

    # modprobe g_ether dev_addr=12:34:56:78:9a:bc host_addr=12:34:56:78:9a:bd

  2. Connect the target to the host using a USB-OTG cable.

  3. On the target, bring up the usb0 interface using ifconfig.

    # ifconfig usb0 192.168.0.100 netmask 255.255.255.0

  4. configure the Linux host system

    # ifconfig usb0 192.168.0.101 netmask 255.255.255.0

  5. For all intents and purposes, the usb0 interface can now be treated just like any other network interface.

  6. Ping the Host system from target device

    # ping 192.168.0.101

  7. Ping the Target device from host device

    # ping 192.168.0.100

The above details talk about the usage and bringup of USB OTG ports in embedded platforms. Hopping this article will help you get a better idea of the USB-OTG port on embedded platforms.

References:

https://www.kernel.org/doc/Documentation/usb/mass-storage.txt

https://www.kernel.org/doc/Documentation/usb/gadget_serial.rst

https://github.com/torvalds/linux/blob/master/arch/arm64/boot/dts/freescale/imx8mp-evk.dts

https://linuxlink.timesys.com/docs/wiki/engineering/HOWTO_Use_USB_Gadget_Ethernet

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics