USB OTG¶
Configuring the USB OTG port under Linux¶
By default, the USB OTG controller usbotg1 is configured with
dr_mode = "otg" in the device tree. It can operate as a host or a
peripheral. To select a fixed mode, change dr_mode to "host" or
"peripheral".
Follow the Build the Linux kernel from source code
or Customizing the Linux kernel
guide and edit the &usbotg1 node in:
For example, to select host mode, change only the dr_mode property,
keeping the other properties and endpoints in the existing node:
To select peripheral mode instead, use dr_mode = "peripheral".
If you build the kernel manually from source code, rebuild the device trees and copy the updated DTBs to your SD card or eMMC.
You can read the current dr_mode value on a running target with:
Using the USB OTG port under Linux¶
As host¶
As peripheral¶
In order to use the board as a peripheral, an appropriate module needs to be loaded. For example, there is an Ethernet gadget module called g_ether, a mass storage gadget called g_mass_storage, and a serial gadget called g_serial.
Examples¶
Connect a type-C plug to Standard-A plug cable between the board and a host PC, and run the following examples on the target board:
Mass Storage Device¶
The following uses the g_mass_storage module to expose the root partition on eMMC to a PC while booting from recovery SD card:
The partition should be loaded on the PC as a mass storage device.
USB Debug Console¶
The following uses the g_serial module to spawn a new debug console over USB:
On target:
echo g_serial > /etc/modules-load.d/g_serial.conf # Load g_serial module on boot
sudo systemctl enable [email protected] # Spawn a new tty session on ttyGS0
reboot # Reboot to take effect
On host:
Ethernet Device¶
The following uses g_ether to establish an Ethernet link with a host computer over USB.
First, connect a USB cable from a host computer to the USB OTG port of the target. Then run the following commands:
On the target:
Load the g_ether module, assigning the host MAC address
02:00:00:00:00:01.
Note: you may exclude the host_addr argument. It is used here
because the host computer will rename usb0 to
enx020000000001.
modprobe g_ether host_addr=02:00:00:00:00:01
nmcli dev set usb0 managed no
ip addr flush usb0; ip addr add 192.168.10.2/24 dev usb0; ip link set usb0 up
On the host computer:
Tell NetworkManager not to manage this interface. Skip this step if NetworkManager is not running on the host.
Assign a static IP address of 192.168.10.1.
nmcli dev set enx020000000001 managed no
sudo ip addr flush enx020000000001; sudo ip addr add 192.168.10.1/24 dev enx020000000001; sudo ip link set enx020000000001 up
Ping the target from the host:
PING 192.168.10.2 (192.168.10.2) 56(84) bytes of data.
64 bytes from 192.168.10.2: icmp_seq=1 ttl=64 time=0.931 ms
64 bytes from 192.168.10.2: icmp_seq=2 ttl=64 time=0.822 ms
64 bytes from 192.168.10.2: icmp_seq=3 ttl=64 time=0.777 ms
--- 192.168.10.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2032ms
rtt min/avg/max/mdev = 0.777/0.843/0.931/0.064 ms
As OTG¶
With OTG, the board can be either a host or a peripheral. In order to use it as a host and connect a peripheral (e.g. a USB flash drive) to it, either use a peripheral with a type-C connector or use a type-C to type-A adapter. In order to use it as a peripheral and connect it to a PC, a cable with a type-C plug on one end and a Standard-A plug on the other is needed.