This application note explains how to configure the STM32F7 as the USB Ethernet Gadget connected to the USB PC over the USB OTG High-Speed link.
The standard Linux Ethernet Gadget driver, with the CDC Ethernet support enabled, is used to implement this functionality. To provide support for Microsoft Windows USB hosts the gadget driver implements a second device configuration with "Remote NDIS" (RNDIS) protocol supported.
In practical embedded applications, the Ethernet Gadget can be used for organizing TCP/IP-based communications with the STM32F7 over the USB physical link.
Configuration and Build
To enable Ethernet Gadget functionality in the rootfs project, do the following:
- Activate the cross development environment:
[yur@ubuntu linux-cortexm-2.1.0]$ . ./ACTIVATE.sh
[yur@ubuntu linux-cortexm-2.1.0]$ cd projects/rootfs
[yur@ubuntu rootfs]$
- Edit the rootfs.dts.STM32F7 file to change the default USB HS interface role: replace the dr_mode property value in the &usb_hs node reference from host to peripheral:
[yur@ubuntu rootfs]$ vi rootfs.dts.STM32F7
...
&usb_hs {
status = "okay";
dr_mode = "peripheral";
pinctrl-names = "default";
...
- Select the appropriate USB Gadget in the Linux configuration menu (go to the Device Drivers -> USB support -> USB Gadget Support -> USB Gadget Drivers menu, and select the Ethernet Gadget (with CDC Ethernet support) variant):
[yur@ubuntu rootfs]$ make kmenuconfig
- Build the project:
[yur@ubuntu rootfs]$ make
Run the resultant rootfs.uImage on the target. Observe the appropriate kernel messages indicating that the USB gadget is started:
...
using random self ethernet address
using random host ethernet address
usb0: HOST MAC 02:5e:48:43:be:47
usb0: MAC da:01:6a:31:91:a3
...
g_ether gadget: Ethernet Gadget, version: Memorial Day 2008
g_ether gadget: g_ether ready
dwc2 40040000.usb: bound driver g_ether
...
/ # ifconfig usb0
usb0 Link encap:Ethernet HWaddr DA:01:6A:31:91:A3
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
The MAC addresses used by default are generated by the driver randomly. You can set up fixed values by adding appropriate variables to the Linux kernel command line in U-Boot:
- g_ether.host_addr=<value> (e.g. g_ether.host_addr=02:00:00:00:02:02) to specify the MAC address of the interface on the Host side;
- g_ether.dev_addr=<value> (e.g. g_ether.dev_addr=04:00:00:00:04:04) to specify the MAC address of the interface on the Gadget side.
Connect to the Linux Host
If the host PC is running Linux, messages like these shown below will be displayed in the STM32F7 console upon connection to the host:
dwc2 40040000.usb: new device is high-speed
dwc2 40040000.usb: new address 5
g_ether gadget: high-speed config #1: CDC Ethernet (ECM)
The host PC will detect the STM32F7 as a CDC Ethernet device:
[yur@ubuntu ~]$ dmesg | tail
...
usb 1-1: new high speed USB device using ehci_hcd and address 3
usb 1-1: configuration #1 chosen from 2 choices
usb0: register 'cdc_ether' at usb-0000:02:03.0-1, CDC Ethernet Device, 02:5e:48:43:be:47
usb0: no IPv6 routers present
[yur@ubuntu ~]$ ifconfig usb0
usb0Link encap:Ethernet HWaddr 02:5e:48:43:be:47
inet6 addr: fe80::5e:48ff:fe43:be47/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Connect to the Windows Host
If the host PC is running Windows, messages like these shown below will be displayed in the STM32F7 console upon connection to the host:
dwc2 40040000.usb: new device is high-speed
dwc2 40040000.usb: new address 4
The RNDIS/Ethernet Gadget driver installation procedure depends on the Windows OS version. Here are the instructions for different versions:
http://webos-internals.org/index.php?title=USBnet_Setup#Microsoft_Windows
After the host driver installation is complete, the STM32F7 will print the following information on the console:
g_ether gadget: high-speed config #2: RNDIS
TCT/IP Communications
Setup an IP address of the USB Ethernet interface on the USB Device (STM32F7) side. Select an address that will not conflict with the other network sub nets in the system (if present):
/ # ifconfig usb0 192.168.2.2
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):
Check communication between STM32F7 and the host over USB with ping:
- on STM32F7:
/ # ping 192.168.2.1
PING 192.168.2.1 (192.168.2.1): 56 data bytes
64 bytes from 192.168.2.1: seq=0 ttl=64 time=5.337 ms
64 bytes from 192.168.2.1: seq=1 ttl=64 time=2.795 ms
64 bytes from 192.168.2.1: seq=2 ttl=64 time=2.827 ms
64 bytes from 192.168.2.1: seq=3 ttl=64 time=2.864 ms
^C
--- 192.168.2.1 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 2.795/3.455/5.337 ms
- on PC:
[yur@ubuntu ~]$ ping 192.168.2.2
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=134 ms
64 bytes from 192.168.2.2: icmp_seq=2 ttl=64 time=1.96 ms
64 bytes from 192.168.2.2: icmp_seq=3 ttl=64 time=2.09 ms
64 bytes from 192.168.2.2: icmp_seq=4 ttl=64 time=2.11 ms
64 bytes from 192.168.2.2: icmp_seq=5 ttl=64 time=2.00 ms
^C
--- 192.168.2.2 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4008ms
rtt min/avg/max/mdev = 1.968/28.529/134.472/52.971 ms
Start the HTTP daemon on the STM32F7:
/ # httpd -h /httpd/html/
Connect to the website on the STM32F7 over USB from the host: