If you have any questions or issues, please Ask a new question, and the NXP support team can address it there.
This article is an example using Zephyr's Networking stack over USB on the MCXN microcontroller family. Although similar steps should also work on other boards with USB support. This article provides 2 examples and patches:
- Single network interface over USB NCM
- Two network interfaces in same app: USB NCM plus Ethernet
USB Background
Zephyr added a new USB Device stack which was called USB Next during development. After the Zephyr v4.2 release, this USB Next stack is the default stack and is recommended for new applications. The original USB stack has been deprecated, see the USB doc. The latest stack is included using the Kconfig CONFIG_USB_DEVICE_STACK_NEXT.
Networking support over USB is supported. According to USB device next follow-up, the USB maintainer wrote "The support for networking functions RNDIS and CDC EEM is not available in the new stack. Instead, the CDC NCM and CDC ECM implementations should be used." And the USB samples page says "USB CDC ECM and USB CDC NCM implementations are covered by the networking samples zperf: Network Traffic Generator or HTTP Server".
To enable networking over USB, these samples include overlay files like usbd_cdc_ncm.overlay and overlay-usbd.conf .
This article builds the HTTP Server sample with USB CDC NCM for the MCXN products.
Example: USB NCM only
The http_server sample using a single network interface through USB.
Requirements
The requirements to use the ncm_mcxn.patch and follow these steps include:
- After the Zephyr v4.2 release and before v4.3 was released, the default USB stack was changed and these samples were updated. This patch was based on commit 67f2464
- The patch supports the following MCXN boards:
Steps for http_server sample
By default, the http_server runs over Ethernet on these boards. The attached patch adds overlays for these boards to that sample to run over USB. Build the sample with CLI using the command below for the FRDM-MCXN947 board. The MCUXpresso extension for VS Code can also be used to build these samples
west build -b frdm_mcxn947//cpu0 samples/net/sockets/http_server/ --pristine
Attach both USB Type-C cables to the FRDM-MCXN947. Flash the http_server app to the board and run.
The USB port J11 will enumerate in the host computer as a network adapter. Assign that adapter a fixed IP address of 192.0.2.2. Open a web browser and browse to 192.0.2.1. The browser will show a web page served from the FRDM-MCXN947 board.
This is the console output printed to the terminal after the web client browses from the server:
*** Booting Zephyr OS build v4.2.0-3840-g62212512547a ***
[00:00:00.001,000] <inf> net_config: Initializing network
[00:00:00.001,000] <inf> net_config: IPv4 address: 192.0.2.1
[00:00:00.101,000] <inf> net_config: IPv6 address: 2001:db8::1
[00:00:00.101,000] <inf> net_config: IPv6 address: 2001:db8::1
[00:00:00.614,000] <inf> cdc_ncm: Enabled cdc_ncm_0
[00:00:00.615,000] <inf> cdc_ncm: New configuration, interface 1 alternate 1
[00:00:00.617,000] <inf> cdc_ncm: New configuration, interface 1 alternate 0
[00:00:00.617,000] <err> usbd_core: UDC error event
[00:00:00.617,000] <err> usbd_core: UDC error event
[00:00:00.638,000] <inf> cdc_ncm: New configuration, interface 1 alternate 1
[00:00:00.738,000] <inf> cdc_ncm: Speed change submitted
[00:00:00.739,000] <inf> cdc_ncm: Speed change sent
[00:00:00.740,000] <inf> cdc_ncm: Connected status submitted
[00:00:00.747,000] <inf> cdc_ncm: Connection status sent
[00:00:00.840,000] <inf> cdc_ncm: Connected status done
[00:04:52.217,000] <dbg> net_http_server_sample: uptime_handler: Uptime handler1
[00:04:53.216,000] <dbg> net_http_server_sample: uptime_handler: Uptime handler1
[00:04:53.641,000] <inf> net_http_server_sample: Accepted websocket connection s
[00:04:53.884,000] <dbg> net_http_server_sample: uptime_handler: Uptime handler1
[00:04:54.883,000] <dbg> net_http_server_sample: uptime_handler: Uptime handler1
uart:~$
Example: USB NCM plus Ethernet
Similar to the example above, but this example leaves the Ethernet device enabled, and modifies the http_server source to enable both networking interfaces.
Requirements
The requirements to use the ncm_plus_ethernet.patch and follow these steps include:
- Like the example above, this patch was based on commit 67f2464
- The patch supports the FRDM-MCXN947 board
- Connect the board to the host computer as described in the previous example. In addition, connect an Ethernet cable to J16 on the board to the host computer.
Networking setup
This example configures 2 network interfaces in the http_server example, both with static IPv4 addresses. The host computer will also connect through two network interfaces: one through USB and one using Ethernet. Using Ubuntu as the host, I found I had to set the two interfaces on separate subnets for Ubuntu to communicate with both interfaces. This is the setup used to test this example using all static IP addresses:
- USB NCM interface:
- http_server eth1 interface with address 192.0.3.1
- host computer enx00005e005301 interface with address 192.0.3.2
- Ethernet interface:
- http_server eth0 interface with address 192.0.2.1
- host computer wired Ethernet interface with address 192.0.2.2
Steps for http_server sample
Build the sample with CLI using the command below for the FRDM-MCXN947 board. The MCUXpresso extension for VS Code can also be used to build these samples
west build -b frdm_mcxn947//cpu0 samples/net/sockets/http_server/ --pristine
Open a web browser and browse to 192.0.2.1. The browser will show a web page served from the Ethernet interface of the FRDM-MCXN947 board. Open a second tab or browser, and browse to 192.0.3.1 for the USB interface. Both web pages are updated using the two interfaces.
This is the console output printed to the terminal after the web client browses from the server:
[00:00:00.050,000] <inf> phy_mii: PHY (0) ID 7C121
[00:00:00.051,000] <inf> eth_nxp_enet_qos_mac: Link is down
*** Booting Zephyr OS build v4.2.0-3649-g4006a48b4040 ***
[00:00:00.052,000] <inf> net_config: Initializing network
[00:00:00.052,000] <inf> net_config: Waiting interface 1 (0x30002190) to be up...
[00:00:00.530,000] <inf> cdc_ncm: Enabled cdc_ncm_0
[00:00:00.531,000] <inf> cdc_ncm: New configuration, interface 1 alternate 1
[00:00:00.533,000] <inf> cdc_ncm: New configuration, interface 1 alternate 0
[00:00:00.533,000] <err> usbd_core: UDC error event
[00:00:00.533,000] <err> usbd_core: UDC error event
[00:00:00.554,000] <inf> cdc_ncm: New configuration, interface 1 alternate 1
[00:00:00.654,000] <inf> cdc_ncm: Speed change submitted
[00:00:00.657,000] <inf> cdc_ncm: Speed change sent
[00:00:00.658,000] <inf> cdc_ncm: Connected status submitted
[00:00:00.665,000] <inf> cdc_ncm: Connection status sent
[00:00:00.758,000] <inf> cdc_ncm: Connected status done
[00:00:02.152,000] <inf> phy_mii: PHY (0) Link speed 100 Mb, full duplex
[00:00:02.152,000] <inf> eth_nxp_enet_qos_mac: Link is up
[00:00:02.153,000] <inf> net_config: Interface 1 (0x30002190) coming up
[00:00:02.153,000] <inf> net_config: IPv4 address: 192.0.2.1
[00:00:02.253,000] <inf> net_config: IPv6 address: 2001:db8::1
[00:00:02.253,000] <inf> net_config: IPv6 address: 2001:db8::1
[00:00:06.548,000] <dbg> net_http_server_sample: uptime_handler: Uptime handler status 1
[00:00:07.650,000] <dbg> net_http_server_sample: uptime_handler: Uptime handler status 1
[00:00:07.652,000] <inf> net_http_server_sample: Accepted websocket connection for net stats
[00:00:08.581,000] <dbg> net_http_server_sample: uptime_handler: Uptime handler status 1
The http_server includes the shell with some networking commands. Enter the command "help" in the debug console to learn more. This is the output from the shell command "net iface":
uart:~$ net iface
Hostname: zephyr
Default interface: 1
Interface eth0 (0x30002190) (Ethernet) [1]
===================================
Link addr : AE:9A:22:0C:E0:F3
MTU : 1500
Flags : AUTO_START,IPv4,IPv6
Device : ethernet (0x10037d08)
Status : oper=UP, admin=UP, carrier=ON
Ethernet capabilities supported:
10 Mbits
100 Mbits
Ethernet PHY device: ethernet-phy@0 (0x10037cc0)
Ethernet link speed: 100 Mbits full-duplex
IPv6 unicast addresses (max 3):
fe80::ac9a:22ff:fe0c:e0f3 autoconf preferred infinite
2001:db8::1 manual preferred infinite
IPv6 multicast addresses (max 4):
ff02::1
ff02::1:ff0c:e0f3
ff02::1:ff00:1
IPv6 prefixes (max 2):
<none>
IPv6 hop limit : 64
IPv6 base reachable time : 30000
IPv6 reachable time : 23045
IPv6 retransmit timer : 0
IPv4 unicast addresses (max 1):
192.0.2.1/255.255.255.0 manual preferred infinite
IPv4 multicast addresses (max 2):
224.0.0.1
IPv4 gateway : 192.0.2.2
Interface eth1 (0x300022b0) (Ethernet) [2]
===================================
Link addr : 02:00:00:F1:34:7C
MTU : 1500
Flags : AUTO_START,IPv4,IPv6
Device : cdc_ncm_eth0 (0x10037ce4)
Status : oper=UP, admin=UP, carrier=ON
Ethernet capabilities supported:
10 Mbits
Ethernet PHY device: <none> (0)
IPv6 unicast addresses (max 3):
fe80::ff:fef1:347c autoconf preferred infinite
IPv6 multicast addresses (max 4):
ff02::1
ff02::1:fff1:347c
IPv6 prefixes (max 2):
<none>
IPv6 hop limit : 64
IPv6 base reachable time : 30000
IPv6 reachable time : 36584
IPv6 retransmit timer : 0
IPv4 unicast addresses (max 1):
192.0.3.1/255.255.255.0 manual preferred infinite
IPv4 multicast addresses (max 2):
224.0.0.1
IPv4 gateway : 0.0.0.0
Return to Zephyr Knowledge Hub