You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2026/10/21 17:54:56 UTC
(mynewt-core) 14/15: apps/usb_net: Add USB network example
This is an automated email from the ASF dual-hosted git repository.
kasjer pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 91119dd5032bebfc5bcb313994f011f7a3f87740
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Tue Oct 13 13:02:44 2026 +0200
apps/usb_net: Add USB network example
Example application exposing USB network function (CDC-NCM by
default, CDC-ECM or RNDIS with USBD_NET) as lwIP interface through
hw/usb/tinyusb/net_lwip. The host gets an address from the device
DHCP server on USB. When the target enables Ethernet (ETH_0) both
interfaces run in the same lwIP stack, Ethernet keeps the default
route. Echo, discard and chargen services (inet_def_service) are
reachable on all interfaces; shell commands lsif and ip show
interface state.
VID/PID and strings are left to the target.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Jerzy Kasenberg <je...@codecoup.pl>
---
apps/usb_net/pkg.yml | 43 +++++++++++++++++++++++++++++++++++
apps/usb_net/src/main.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++
apps/usb_net/syscfg.yml | 43 +++++++++++++++++++++++++++++++++++
3 files changed, 146 insertions(+)
diff --git a/apps/usb_net/pkg.yml b/apps/usb_net/pkg.yml
new file mode 100644
index 000000000..91dcda9b0
--- /dev/null
+++ b/apps/usb_net/pkg.yml
@@ -0,0 +1,43 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: apps/usb_net
+pkg.type: app
+pkg.description: >
+ USB network example. Device is reachable from USB host over CDC-NCM
+ (or CDC-ECM/RNDIS), and over Ethernet when the target enables it,
+ both served by one lwIP stack. Echo, discard and chargen services
+ run on all interfaces.
+pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+ - usb
+ - network
+ - ncm
+ - lwip
+
+pkg.deps:
+ - "@apache-mynewt-core/kernel/os"
+ - "@apache-mynewt-core/sys/console"
+ - "@apache-mynewt-core/sys/log"
+ - "@apache-mynewt-core/sys/shell"
+ - "@apache-mynewt-core/net/ip/lwip_mn"
+ - "@apache-mynewt-core/net/ip/inet_def_service"
+ - "@apache-mynewt-core/hw/usb/tinyusb"
+ - "@apache-mynewt-core/hw/usb/tinyusb/net_lwip"
diff --git a/apps/usb_net/src/main.c b/apps/usb_net/src/main.c
new file mode 100644
index 000000000..9d7836c20
--- /dev/null
+++ b/apps/usb_net/src/main.c
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*
+ * USB network example.
+ *
+ * USB network function (NCM by default) is added to lwIP by
+ * hw/usb/tinyusb/net_lwip with static address USBD_NET_IP_ADDR and a DHCP
+ * server for the host. If the target enables Ethernet (ETH_0) it runs in
+ * the same lwIP stack, gets its address from DHCP and stays the default
+ * route. Echo (7), discard (9) and chargen (19) TCP/UDP services are
+ * reachable on both interfaces. Shell commands "lsif" and "ip" show
+ * interface state.
+ */
+
+#include <os/mynewt.h>
+#include <console/console.h>
+#include <inet_def_service/inet_def_service.h>
+
+#include <lwip/netif.h>
+#include <lwip/ip4_addr.h>
+#include <net_lwip/net_lwip.h>
+
+int
+mynewt_main(int argc, char **argv)
+{
+ struct netif *nif;
+ char addr[IP4ADDR_STRLEN_MAX];
+
+ sysinit();
+
+ inet_def_service_init(os_eventq_dflt_get());
+
+ nif = usb_net_lwip_netif();
+ ip4addr_ntoa_r(netif_ip4_addr(nif), addr, sizeof(addr));
+ console_printf("usb_net: USB interface %c%c%u at %s\n", nif->name[0],
+ nif->name[1], nif->num, addr);
+
+ while (1) {
+ os_eventq_run(os_eventq_dflt_get());
+ }
+
+ return 0;
+}
diff --git a/apps/usb_net/syscfg.yml b/apps/usb_net/syscfg.yml
new file mode 100644
index 000000000..fc2d5999c
--- /dev/null
+++ b/apps/usb_net/syscfg.yml
@@ -0,0 +1,43 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+# USBD_VID, USBD_PID, USBD_VENDOR_STRING and USBD_PRODUCT_STRING are left
+# to the target. Ethernet is enabled by the target (ETH_0: 1) on boards
+# that have it.
+syscfg.vals:
+ CONSOLE_IMPLEMENTATION: full
+ LOG_IMPLEMENTATION: full
+ SHELL_TASK: 1
+
+ USBD_STD_DESCRIPTORS: 1
+ # Network function uses Interface Association Descriptor.
+ USBD_COMPOSITE_DEVICE: 1
+ USBD_NET: NCM
+ USBD_STACK_SIZE: 256
+
+ # lwIP heap is shared between tasks.
+ BASELIBC_THREAD_SAFE_HEAP_ALLOCATION: 1
+ # Ethernet DHCP client, DHCP server on USB, inet_def_service (3)
+ # and DNS client.
+ LWIP_NUM_UDP_PCB: 8
+
+ # Received TCP data is copied to msys mbufs. lwIP can deliver whole
+ # receive window (TCP_WND, 3 * MSS) at once, msys must be able to hold
+ # it or received data can't be passed to socket.
+ MSYS_1_BLOCK_COUNT: 32