Skip to content

Commit 4c0a72b

Browse files
jukkarnashif
authored andcommitted
samples: net: echo-server: Add USB CDC NCM support
Add support for USB cdc_ncm to echo-server application. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 85b9bd4 commit 4c0a72b

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed

samples/net/sockets/echo_server/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@ target_sources_ifdef(CONFIG_NET_UDP app PRIVATE src/udp.c)
2323
target_sources_ifdef(CONFIG_NET_TCP app PRIVATE src/tcp.c)
2424
target_sources_ifdef(CONFIG_NET_VLAN app PRIVATE src/vlan.c)
2525
target_sources_ifdef(CONFIG_NET_L2_IPIP app PRIVATE src/tunnel.c)
26-
target_sources_ifdef(CONFIG_USB_DEVICE_STACK app PRIVATE src/usb.c)
26+
27+
if (CONFIG_USB_DEVICE_STACK)
28+
target_sources(app PRIVATE src/usb.c)
29+
endif()
30+
31+
if (CONFIG_USB_DEVICE_STACK_NEXT)
32+
target_sources(app PRIVATE src/usb.c)
33+
include(${ZEPHYR_BASE}/samples/subsys/usb/common/common.cmake)
34+
endif()
2735

2836
include(${ZEPHYR_BASE}/samples/net/common/common.cmake)
2937

samples/net/sockets/echo_server/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,11 @@ config NET_SAMPLE_HTTPS_SERVER_SERVICE_PORT
104104
default 443
105105
depends on NET_SAMPLE_HTTPS_SERVICE
106106

107+
if USB_DEVICE_STACK_NEXT
108+
# Source common USB sample options used to initialize new experimental USB
109+
# device stack. The scope of these options is limited to USB samples in project
110+
# tree, you cannot use them in your own application.
111+
source "samples/subsys/usb/common/Kconfig.sample_usbd"
112+
endif
113+
107114
source "Kconfig.zephyr"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CONFIG_USB_DEVICE_STACK_NEXT=y
2+
3+
CONFIG_LOG=y
4+
CONFIG_USBD_LOG_LEVEL_WRN=y
5+
CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y
6+
7+
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048

samples/net/sockets/echo_server/src/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static inline bool is_tunnel(struct net_if *iface)
107107
}
108108
#endif /* CONFIG_NET_L2_IPIP */
109109

110-
#if defined(CONFIG_USB_DEVICE_STACK)
110+
#if defined(CONFIG_USB_DEVICE_STACK) || defined(CONFIG_USB_DEVICE_STACK_NEXT)
111111
int init_usb(void);
112112
#else
113113
static inline int init_usb(void)

samples/net/sockets/echo_server/src/usb.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,46 @@ LOG_MODULE_DECLARE(net_echo_server_sample, LOG_LEVEL_DBG);
1010
#include <zephyr/usb/usb_device.h>
1111
#include <zephyr/net/net_config.h>
1212

13+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
14+
#include <sample_usbd.h>
15+
16+
static struct usbd_context *sample_usbd;
17+
18+
static int enable_usb_device_next(void)
19+
{
20+
int err;
21+
22+
sample_usbd = sample_usbd_init_device(NULL);
23+
if (sample_usbd == NULL) {
24+
return -ENODEV;
25+
}
26+
27+
err = usbd_enable(sample_usbd);
28+
if (err) {
29+
return err;
30+
}
31+
32+
return 0;
33+
}
34+
#endif /* CONFIG_USB_DEVICE_STACK_NEXT */
35+
1336
int init_usb(void)
1437
{
38+
#if defined(CONFIG_USB_DEVICE_STACK)
1539
int ret;
1640

1741
ret = usb_enable(NULL);
1842
if (ret != 0) {
1943
LOG_ERR("Cannot enable USB (%d)", ret);
2044
return ret;
2145
}
46+
#endif /* CONFIG_USB_DEVICE_STACK */
47+
48+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
49+
if (enable_usb_device_next()) {
50+
return 0;
51+
}
52+
#endif /* CONFIG_USB_DEVICE_STACK_NEXT */
2253

2354
(void)net_config_init_app(NULL, "Initializing network");
2455

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
cdc_ncm_eth0: cdc_ncm_eth0 {
9+
compatible = "zephyr,cdc-ncm-ethernet";
10+
remote-mac-address = "00005E005301";
11+
};
12+
};

0 commit comments

Comments
 (0)