Please enable javascript, or click here to visit my ecommerce web site powered by Shopify.
Jump to: navigation, search

Rock3/dev/usb-gadget

< Rock3‎ | dev
    ROCK 3 >  Development >  USB Gadget Support

USB Gadget Support

USB NET RNDIS

Hardware Connection
ROCK 3C <---> ROCK 5B (Debian11)
Type-A OTG port <---> Type-A 3.0 Host port
Software Configuration

Step1: Install essential tools

root@rock-3c:~# apt update -y && apt install -y net-tools iperf

Step2: Enable USB OTG function (Do this only once)

root@rock-3c:~# rsetup
┌──────────────────────────────────┤ RSETUP ├──────────────────────────────────┐
│ Please select an option below:                                               │
│                                                                              │
│                             System Maintaince                                │
│                             Hardware                                         │
│                             Overlays                                         │
│                             Connectivity                                     │
│                             User Settings                                    │
│                             Localization                                     │
│                             About                                            │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                     <Ok>                         <Cancel>                    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Select Manage overlays

┌──────────────────────────────────┤ RSETUP ├──────────────────────────────────┐
│ Configure Device Tree Overlay                                                │
│                                                                              │
│                        Manage overlays                                       │
│                        View overlay info                                     │
│                        Install overlay from source                           │
│                        Reset overlays                                        │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                     <Ok>                         <Cancel>                    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Select Set OTG port to OTG mode

┌──────────────────────────────────┤ RSETUP ├──────────────────────────────────┐
│ Please select overlays:                                                      │
│                                                                              │
│    [ ] Enable PWM14-M0                                                       │
│    [ ] Enable PWM14-M1                                                   ▒   │
│    [ ] Enable PWM15-M1                                                   ▒   │
│    [ ] Enable PWM8-M0                                                    ▒   │
│    [ ] Enable PWM9-M0                                                    ▒   │
│    [ ] Enable Radxa 5-inch Touchscreen                                   ▒   │
│    [ ] Enable Raspberry Pi 7-inch Touchscreen                            ▒   │
│    [ ] Enable Raspberry Pi Camera v1.3                                   ▒   │
│    [ ] Enable Raspberry Pi Camera v2                                     ▒   │
│    [ ] Enable UART2-M0                                                   ▒   │
│    [ ] Enable UART3-M0                                                   ▒   │
│    [ ] Enable UART4-M1                                                   ▒   │
│    [ ] Enable UART5-M1                                                   ▒   │
│    [ ] Enable UART9-M1                                                   ▒   │
│    [ ] Enable spidev on SPI3-M1 over CS0                                     │
│    [*] Set OTG port to OTG mode                                              │
│                                                                              │
│                                                                              │
│                     <Ok>                         <Cancel>                    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Select Ok' to confirm.

┌──────────────────────────────────┤ RSETUP ├──────────────────────────────────┐
│                                                                              │
│ Selected overlays will be enabled at next boot.                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                    <Ok>                                      │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Click Esc to exit the window.

Reboot ROCK 3C.

root@rock-3c:~# reboot

Step3: Load kernel module

root@rock-3c:~# modprobe g_ether
root@rock-3c:~# dmesg -c
[  108.290807] using random self ethernet address
[  108.290820] using random host ethernet address
[  108.292182] usb0: HOST MAC 9a:ec:b8:df:56:60
[  108.292322] usb0: MAC 52:4c:1e:5e:dd:aa
[  108.292382] using random self ethernet address
[  108.292391] using random host ethernet address
[  108.292504] g_ether gadget: Ethernet Gadget, version: Memorial Day 2008
[  108.292513] g_ether gadget: g_ether ready
[  108.403372] dwc3 fcc00000.dwc3: device reset
[  108.530261] dwc3 fcc00000.dwc3: device reset
[  108.685170] g_ether gadget: high-speed config #2: RNDIS

Step 4: Bring up the usb0 device

root@rock-3c:~# ifconfig usb0 up

Now you should have usb0 on ROCK 3C enabled.

Step 5: Check usb0 network card on ROCK 5B.

On ROCK 5B Debian11, the dmesg gives something like below:

root@rock-5b:~# dmesg -c
[  334.470377] usb 2-1: new high-speed USB device number 2 using ehci-platform
[  334.620061] usb 2-1: New USB device found, idVendor=0525, idProduct=a4a2, bcdDevice= 4.19
[  334.620088] usb 2-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[  334.620107] usb 2-1: Product: RNDIS/Ethernet Gadget
[  334.620125] usb 2-1: Manufacturer: Linux 4.19.193-1-rk356x with dwc3-gadget
[  334.634014] rndis_host 2-1:2.0 usb0: register 'rndis_host' at usb-fc880000.usb-1, RNDIS device, c2:e5:aa:9d:8f:e0

usb0 is the "USB network card" for ROCK 3C and ROCK 5B

Step 6: Setup USB Network

On ROCK 3C

root@rock-3c:~# IP=10.0.1.1
root@rock-3c:~# HOST_IP=10.0.1.1
root@rock-3c:~# ifconfig usb0 $IP netmask 255.255.255.0
root@rock-3c:~# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 usb0

On ROCK 5B

root@rock-5b:~# HOST_IP=10.0.1.2
root@rock-5b:~# ifconfig usb0 $HOST_IP netmask 255.255.255.0

Step 7: Test network via ping

on ROCK 5B

root@rock-5b:~# ifconfig usb0
usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.1.2  netmask 255.255.255.0  broadcast 10.0.1.255
        ether c2:e5:aa:9d:8f:e0  txqueuelen 1000  (Ethernet)
        RX packets 37  bytes 2464 (2.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 145  bytes 28585 (27.9 KiB)
        TX errors 58  dropped 0 overruns 0  carrier 0  collisions 0

On ROCK 3C, ping 'ROCK 5B'

root@rock-3c:~# ping 10.0.1.2
PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data.
64 bytes from 10.0.1.2: icmp_seq=1 ttl=64 time=0.805 ms
64 bytes from 10.0.1.2: icmp_seq=2 ttl=64 time=0.612 ms
64 bytes from 10.0.1.2: icmp_seq=3 ttl=64 time=0.489 ms
64 bytes from 10.0.1.2: icmp_seq=4 ttl=64 time=0.495 ms
64 bytes from 10.0.1.2: icmp_seq=5 ttl=64 time=0.407 ms
64 bytes from 10.0.1.2: icmp_seq=6 ttl=64 time=0.357 ms
64 bytes from 10.0.1.2: icmp_seq=7 ttl=64 time=0.314 ms
64 bytes from 10.0.1.2: icmp_seq=8 ttl=64 time=0.493 ms
64 bytes from 10.0.1.2: icmp_seq=9 ttl=64 time=0.469 ms

Step 8: Test network throughput via 'iperf3'

on ROCK 5B

root@rock-5b:~# iperf3 -s
-----------------------------------------------------------
Server listening on 5201
-----------------------------------------------------------
Accepted connection from 10.0.1.1, port 32814
[  5] local 10.0.1.2 port 5201 connected to 10.0.1.1 port 32816
[ ID] Interval           Transfer     Bitrate
[  5]   0.00-1.00   sec  26.4 MBytes   222 Mbits/sec                  
[  5]   1.00-2.00   sec  30.2 MBytes   253 Mbits/sec                  
[  5]   2.00-3.00   sec  30.1 MBytes   253 Mbits/sec                  
[  5]   3.00-4.00   sec  30.2 MBytes   253 Mbits/sec                  
[  5]   4.00-5.00   sec  30.0 MBytes   252 Mbits/sec                  
[  5]   5.00-6.00   sec  30.2 MBytes   253 Mbits/sec                  
[  5]   6.00-7.00   sec  29.7 MBytes   249 Mbits/sec                  
[  5]   7.00-8.00   sec  30.1 MBytes   253 Mbits/sec                  
[  5]   8.00-9.00   sec  30.2 MBytes   253 Mbits/sec                  
[  5]   9.00-10.00  sec  30.2 MBytes   253 Mbits/sec                  
[  5]  10.00-10.04  sec  1.05 MBytes   246 Mbits/sec                  
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bitrate
[  5]   0.00-10.04  sec   298 MBytes   249 Mbits/sec                  receiver
-----------------------------------------------------------
Server listening on 5201
-----------------------------------------------------------

on ROCK 3C

root@rock-3c:~# iperf3 -c 10.0.1.2
Connecting to host 10.0.1.2, port 5201
[  5] local 10.0.1.1 port 32816 connected to 10.0.1.2 port 5201
[ ID] Interval           Transfer     Bitrate         Retr  Cwnd
[  5]   0.00-1.00   sec  28.5 MBytes   239 Mbits/sec    0    238 KBytes       
[  5]   1.00-2.00   sec  30.3 MBytes   254 Mbits/sec    0    238 KBytes       
[  5]   2.00-3.00   sec  29.8 MBytes   250 Mbits/sec    0    238 KBytes       
[  5]   3.00-4.00   sec  30.3 MBytes   254 Mbits/sec    0    238 KBytes       
[  5]   4.00-5.00   sec  30.3 MBytes   254 Mbits/sec    0    259 KBytes       
[  5]   5.00-6.00   sec  30.2 MBytes   253 Mbits/sec    0    259 KBytes       
[  5]   6.00-7.00   sec  29.9 MBytes   251 Mbits/sec    0    283 KBytes       
[  5]   7.00-8.00   sec  29.6 MBytes   249 Mbits/sec    0    296 KBytes       
[  5]   8.00-9.00   sec  30.4 MBytes   255 Mbits/sec    0    296 KBytes       
[  5]   9.00-10.00  sec  30.4 MBytes   255 Mbits/sec    0    416 KBytes       
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bitrate         Retr
[  5]   0.00-10.00  sec   300 MBytes   252 Mbits/sec    0             sender
[  5]   0.00-10.04  sec   298 MBytes   249 Mbits/sec                  receiver

iperf Done.