diff --git a/boards/nordic/nrf54lm20dk/nrf54lm20dk_nrf54lm20a_cpuapp.yaml b/boards/nordic/nrf54lm20dk/nrf54lm20dk_nrf54lm20a_cpuapp.yaml index 765845fa27f..e5a2b89087d 100644 --- a/boards/nordic/nrf54lm20dk/nrf54lm20dk_nrf54lm20a_cpuapp.yaml +++ b/boards/nordic/nrf54lm20dk/nrf54lm20dk_nrf54lm20a_cpuapp.yaml @@ -20,4 +20,5 @@ supported: - i2s - pwm - spi + - usbd - watchdog diff --git a/samples/subsys/usb/cdc_acm/README.rst b/samples/subsys/usb/cdc_acm/README.rst index 21ce853ae08..db95373b253 100644 --- a/samples/subsys/usb/cdc_acm/README.rst +++ b/samples/subsys/usb/cdc_acm/README.rst @@ -1,8 +1,8 @@ .. zephyr:code-sample:: usb-cdc-acm - :name: USB CDC-ACM - :relevant-api: usbd_api _usb_device_core_api uart_interface + :name: USB CDC ACM UART sample + :relevant-api: usbd_api uart_interface - Use USB CDC-ACM driver to implement a serial port echo. + Use USB CDC ACM UART driver to implement a serial port echo. Overview ******** diff --git a/samples/subsys/usb/cdc_acm/prj.conf b/samples/subsys/usb/cdc_acm/prj.conf index 55e4fe9217f..e0012027ec6 100644 --- a/samples/subsys/usb/cdc_acm/prj.conf +++ b/samples/subsys/usb/cdc_acm/prj.conf @@ -1,11 +1,14 @@ +CONFIG_USB_DEVICE_STACK_NEXT=y + CONFIG_STDOUT_CONSOLE=y -CONFIG_USB_DEVICE_STACK=y -CONFIG_USB_DEVICE_PRODUCT="Zephyr CDC ACM sample" -CONFIG_USB_DEVICE_PID=0x0001 -CONFIG_LOG=y -CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y -CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y CONFIG_SERIAL=y -CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_UART_LINE_CTRL=y -CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n +CONFIG_USBD_CDC_ACM_CLASS=y + +CONFIG_LOG=y +CONFIG_USBD_LOG_LEVEL_ERR=y +CONFIG_UDC_DRIVER_LOG_LEVEL_ERR=y +CONFIG_USBD_CDC_ACM_LOG_LEVEL_ERR=y + +CONFIG_SAMPLE_USBD_PID=0x0001 +CONFIG_SAMPLE_USBD_PRODUCT="USBD CDC ACM sample" diff --git a/samples/subsys/usb/cdc_acm/sample.yaml b/samples/subsys/usb/cdc_acm/sample.yaml index 875bfb335b5..57ae22f0df3 100644 --- a/samples/subsys/usb/cdc_acm/sample.yaml +++ b/samples/subsys/usb/cdc_acm/sample.yaml @@ -1,22 +1,13 @@ sample: name: CDC ACM USB tests: - sample.usb.cdc-acm: - depends_on: usb_device - tags: usb - arch_exclude: posix - harness: console - harness_config: - type: one_line - regex: - - "Wait for DTR" sample.usb_device_next.cdc-acm: depends_on: usbd tags: usb - extra_args: CONF_FILE="usbd_next_prj.conf" integration_platforms: - nrf52840dk/nrf52840 - nrf54h20dk/nrf54h20/cpuapp + - nrf54lm20dk/nrf54lm20a/cpuapp - frdm_k64f - stm32f723e_disco - nucleo_f413zh @@ -34,7 +25,6 @@ tests: depends_on: usbd tags: usb extra_args: - - CONF_FILE="usbd_next_prj.conf" - DCONFIG_USBD_CDC_ACM_WORKQUEUE=y integration_platforms: - frdm_k64f @@ -43,10 +33,3 @@ tests: type: one_line regex: - "Wait for DTR" - sample.usb.cdc-acm.buildonly: - depends_on: usb_device - tags: usb - arch_allow: posix - build_only: true - integration_platforms: - - native_sim diff --git a/samples/subsys/usb/cdc_acm/src/main.c b/samples/subsys/usb/cdc_acm/src/main.c index ab1cbdc6a4c..c80d253966b 100644 --- a/samples/subsys/usb/cdc_acm/src/main.c +++ b/samples/subsys/usb/cdc_acm/src/main.c @@ -4,14 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -/** - * @file - * @brief Sample echo app for CDC ACM class - * - * Sample app for USB CDC ACM class driver. The received data is echoed back - * to the serial port. - */ - #include #include @@ -21,7 +13,6 @@ #include #include -#include #include #include LOG_MODULE_REGISTER(cdc_acm_echo, LOG_LEVEL_INF); @@ -48,7 +39,6 @@ static inline void print_baudrate(const struct device *dev) } } -#if defined(CONFIG_USB_DEVICE_STACK_NEXT) static struct usbd_context *sample_usbd; K_SEM_DEFINE(dtr_sem, 0, 1); @@ -106,7 +96,6 @@ static int enable_usb_device_next(void) return 0; } -#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */ static void interrupt_handler(const struct device *dev, void *user_data) { @@ -178,37 +167,16 @@ int main(void) return 0; } -#if defined(CONFIG_USB_DEVICE_STACK_NEXT) - ret = enable_usb_device_next(); -#else - ret = usb_enable(NULL); -#endif - + ret = enable_usb_device_next(); if (ret != 0) { - LOG_ERR("Failed to enable USB"); + LOG_ERR("Failed to enable USB device support"); return 0; } ring_buf_init(&ringbuf, sizeof(ring_buffer), ring_buffer); LOG_INF("Wait for DTR"); - -#if defined(CONFIG_USB_DEVICE_STACK_NEXT) k_sem_take(&dtr_sem, K_FOREVER); -#else - while (true) { - uint32_t dtr = 0U; - - uart_line_ctrl_get(uart_dev, UART_LINE_CTRL_DTR, &dtr); - if (dtr) { - break; - } else { - /* Give CPU resources to low priority threads. */ - k_sleep(K_MSEC(100)); - } - } -#endif - LOG_INF("DTR set"); /* They are optional, we use them to test the interrupt endpoint */ @@ -225,11 +193,7 @@ int main(void) /* Wait 100ms for the host to do all settings */ k_msleep(100); -#ifndef CONFIG_USB_DEVICE_STACK_NEXT - print_baudrate(uart_dev); -#endif uart_irq_callback_set(uart_dev, interrupt_handler); - /* Enable rx interrupts */ uart_irq_rx_enable(uart_dev); diff --git a/samples/subsys/usb/cdc_acm/usbd_next_prj.conf b/samples/subsys/usb/cdc_acm/usbd_next_prj.conf deleted file mode 100644 index f08928438b7..00000000000 --- a/samples/subsys/usb/cdc_acm/usbd_next_prj.conf +++ /dev/null @@ -1,14 +0,0 @@ -CONFIG_USB_DEVICE_STACK_NEXT=y - -CONFIG_STDOUT_CONSOLE=y -CONFIG_SERIAL=y -CONFIG_UART_LINE_CTRL=y -CONFIG_USBD_CDC_ACM_CLASS=y - -CONFIG_LOG=y -CONFIG_USBD_LOG_LEVEL_WRN=y -CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y -CONFIG_USBD_CDC_ACM_LOG_LEVEL_ERR=y - -CONFIG_SAMPLE_USBD_PID=0x0001 -CONFIG_SAMPLE_USBD_PRODUCT="USBD CDC ACM sample" diff --git a/samples/subsys/usb/hid-keyboard/sample.yaml b/samples/subsys/usb/hid-keyboard/sample.yaml index 6161ff90c2c..01349157d14 100644 --- a/samples/subsys/usb/hid-keyboard/sample.yaml +++ b/samples/subsys/usb/hid-keyboard/sample.yaml @@ -1,14 +1,21 @@ sample: name: USB HID keyboard sample common: - harness: button filter: dt_alias_exists("sw0") and dt_alias_exists("led0") depends_on: - usbd - gpio + tags: usb + timeout: 15 + harness: console + harness_config: + type: one_line + regex: + - "HID keyboard sample is initialized" integration_platforms: - nrf52840dk/nrf52840 - nrf54h20dk/nrf54h20/cpuapp + - nrf54lm20dk/nrf54lm20a/cpuapp - frdm_k64f - stm32f723e_disco - nucleo_f413zh @@ -17,17 +24,13 @@ common: - samd21_xpro - same54_xpro tests: - sample.usbd.hid-keyboard: - tags: usb + sample.usbd.hid-keyboard: {} sample.usbd.hid-keyboard.out-report: - tags: usb extra_args: - EXTRA_DTC_OVERLAY_FILE="out_report.overlay" sample.usbd.hid-keyboard.large-report: - tags: usb extra_args: - EXTRA_DTC_OVERLAY_FILE="large_in_report.overlay" sample.usbd.hid-keyboard.large-out-report: - tags: usb extra_args: - EXTRA_DTC_OVERLAY_FILE="large_out_report.overlay" diff --git a/samples/subsys/usb/hid-mouse/README.rst b/samples/subsys/usb/hid-mouse/README.rst index e2f099f98f4..0c1d908f05b 100644 --- a/samples/subsys/usb/hid-mouse/README.rst +++ b/samples/subsys/usb/hid-mouse/README.rst @@ -1,6 +1,6 @@ .. zephyr:code-sample:: usb-hid-mouse :name: USB HID mouse - :relevant-api: _usb_device_core_api usb_hid_device_api input_interface + :relevant-api: usbd_api usbd_hid_device input_interface Implement a basic HID mouse device. diff --git a/samples/subsys/usb/hid-mouse/usbd_next.overlay b/samples/subsys/usb/hid-mouse/app.overlay similarity index 100% rename from samples/subsys/usb/hid-mouse/usbd_next.overlay rename to samples/subsys/usb/hid-mouse/app.overlay diff --git a/samples/subsys/usb/hid-mouse/prj.conf b/samples/subsys/usb/hid-mouse/prj.conf index b797a7382e8..9d5ea1fc142 100644 --- a/samples/subsys/usb/hid-mouse/prj.conf +++ b/samples/subsys/usb/hid-mouse/prj.conf @@ -1,13 +1,11 @@ -CONFIG_USB_DEVICE_STACK=y -CONFIG_USB_DEVICE_HID=y -CONFIG_USB_DEVICE_PRODUCT="Zephyr HID mouse sample" -CONFIG_USB_DEVICE_PID=0x0007 -CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n +CONFIG_USB_DEVICE_STACK_NEXT=y +CONFIG_USBD_HID_SUPPORT=y CONFIG_LOG=y -CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y -CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y +CONFIG_USBD_LOG_LEVEL_INF=y +CONFIG_USBD_HID_LOG_LEVEL_WRN=y +CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y +CONFIG_SAMPLE_USBD_PID=0x0007 CONFIG_GPIO=y CONFIG_INPUT=y -CONFIG_INPUT_MODE_SYNCHRONOUS=y diff --git a/samples/subsys/usb/hid-mouse/sample.yaml b/samples/subsys/usb/hid-mouse/sample.yaml index 132c71c928b..d420c5418a0 100644 --- a/samples/subsys/usb/hid-mouse/sample.yaml +++ b/samples/subsys/usb/hid-mouse/sample.yaml @@ -5,25 +5,24 @@ common: filter: dt_alias_exists("sw0") and dt_alias_exists("led0") depends_on: - gpio + tags: usb tests: - sample.usb.hid-mouse: - depends_on: - - usb_device - platform_exclude: - - frdm_mcxn947/mcxn947/cpu0 - tags: usb sample.usb_device_next.hid-mouse: depends_on: - usbd + timeout: 15 + harness: console + harness_config: + type: multi_line + regex: + - "HS bNumConfigurations 1" + - "FS bNumConfigurations 1" integration_platforms: - nrf52840dk/nrf52840 - nrf54h20dk/nrf54h20/cpuapp + - nrf54lm20dk/nrf54lm20a/cpuapp - frdm_k64f - stm32f723e_disco - nucleo_f413zh - mimxrt1060_evk/mimxrt1062/qspi - max32690evkit/max32690/m4 - extra_args: - - CONF_FILE="usbd_next_prj.conf" - - EXTRA_DTC_OVERLAY_FILE="usbd_next.overlay" - tags: usb diff --git a/samples/subsys/usb/hid-mouse/src/main.c b/samples/subsys/usb/hid-mouse/src/main.c index 4e579832cf9..967591b2a71 100644 --- a/samples/subsys/usb/hid-mouse/src/main.c +++ b/samples/subsys/usb/hid-mouse/src/main.c @@ -15,16 +15,14 @@ #include #include -#include #include -#include +#include #include LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios); static const uint8_t hid_report_desc[] = HID_MOUSE_REPORT_DESC(2); -static enum usb_dc_status_code usb_status; #define MOUSE_BTN_LEFT 0 #define MOUSE_BTN_RIGHT 1 @@ -38,22 +36,7 @@ enum mouse_report_idx { }; K_MSGQ_DEFINE(mouse_msgq, MOUSE_REPORT_COUNT, 2, 1); -static K_SEM_DEFINE(ep_write_sem, 0, 1); - -static inline void status_cb(enum usb_dc_status_code status, const uint8_t *param) -{ - usb_status = status; -} - -static ALWAYS_INLINE void rwup_if_suspended(void) -{ - if (IS_ENABLED(CONFIG_USB_DEVICE_REMOTE_WAKEUP)) { - if (usb_status == USB_DC_SUSPEND) { - usb_wakeup_request(); - return; - } - } -} +static bool mouse_ready; static void input_cb(struct input_event *evt, void *user_data) { @@ -63,11 +46,9 @@ static void input_cb(struct input_event *evt, void *user_data) switch (evt->code) { case INPUT_KEY_0: - rwup_if_suspended(); WRITE_BIT(tmp[MOUSE_BTN_REPORT_IDX], MOUSE_BTN_LEFT, evt->value); break; case INPUT_KEY_1: - rwup_if_suspended(); WRITE_BIT(tmp[MOUSE_BTN_REPORT_IDX], MOUSE_BTN_RIGHT, evt->value); break; case INPUT_KEY_2: @@ -99,42 +80,30 @@ static void input_cb(struct input_event *evt, void *user_data) INPUT_CALLBACK_DEFINE(NULL, input_cb, NULL); -#if defined(CONFIG_USB_DEVICE_STACK_NEXT) -static int enable_usb_device_next(void) +static void mouse_iface_ready(const struct device *dev, const bool ready) { - struct usbd_context *sample_usbd; - int err; - - sample_usbd = sample_usbd_init_device(NULL); - if (sample_usbd == NULL) { - LOG_ERR("Failed to initialize USB device"); - return -ENODEV; - } - - err = usbd_enable(sample_usbd); - if (err) { - LOG_ERR("Failed to enable device support"); - return err; - } - - LOG_DBG("USB device support enabled"); - - return 0; + LOG_INF("HID device %s interface is %s", + dev->name, ready ? "ready" : "not ready"); + mouse_ready = ready; } -#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */ -static void int_in_ready_cb(const struct device *dev) +static int mouse_get_report(const struct device *dev, + const uint8_t type, const uint8_t id, const uint16_t len, + uint8_t *const buf) { - ARG_UNUSED(dev); - k_sem_give(&ep_write_sem); + LOG_WRN("Get Report not implemented, Type %u ID %u", type, id); + + return 0; } -static const struct hid_ops ops = { - .int_in_ready = int_in_ready_cb, +struct hid_device_ops mouse_ops = { + .iface_ready = mouse_iface_ready, + .get_report = mouse_get_report, }; int main(void) { + struct usbd_context *sample_usbd; const struct device *hid_dev; int ret; @@ -143,51 +112,58 @@ int main(void) return 0; } -#if defined(CONFIG_USB_DEVICE_STACK_NEXT) - hid_dev = DEVICE_DT_GET_ONE(zephyr_hid_device); -#else - hid_dev = device_get_binding("HID_0"); -#endif - if (hid_dev == NULL) { - LOG_ERR("Cannot get USB HID Device"); - return 0; - } - ret = gpio_pin_configure_dt(&led0, GPIO_OUTPUT); - if (ret < 0) { + if (ret != 0) { LOG_ERR("Failed to configure the LED pin, error: %d", ret); return 0; } - usb_hid_register_device(hid_dev, - hid_report_desc, sizeof(hid_report_desc), - &ops); + hid_dev = DEVICE_DT_GET_ONE(zephyr_hid_device); + if (!device_is_ready(hid_dev)) { + LOG_ERR("HID Device is not ready"); + return -EIO; + } - usb_hid_init(hid_dev); + ret = hid_device_register(hid_dev, + hid_report_desc, sizeof(hid_report_desc), + &mouse_ops); + if (ret != 0) { + LOG_ERR("Failed to register HID Device, %d", ret); + return ret; + } -#if defined(CONFIG_USB_DEVICE_STACK_NEXT) - ret = enable_usb_device_next(); -#else - ret = usb_enable(status_cb); -#endif + sample_usbd = sample_usbd_init_device(NULL); + if (sample_usbd == NULL) { + LOG_ERR("Failed to initialize USB device"); + return -ENODEV; + } + + ret = usbd_enable(sample_usbd); if (ret != 0) { - LOG_ERR("Failed to enable USB"); - return 0; + LOG_ERR("Failed to enable device support"); + return ret; } + LOG_DBG("USB device support enabled"); + while (true) { UDC_STATIC_BUF_DEFINE(report, MOUSE_REPORT_COUNT); k_msgq_get(&mouse_msgq, &report, K_FOREVER); - ret = hid_int_ep_write(hid_dev, report, MOUSE_REPORT_COUNT, NULL); + if (!mouse_ready) { + LOG_INF("USB HID device is not ready"); + continue; + } + + ret = hid_device_submit_report(hid_dev, MOUSE_REPORT_COUNT, report); if (ret) { - LOG_ERR("HID write error, %d", ret); + LOG_ERR("HID submit report error, %d", ret); } else { - k_sem_take(&ep_write_sem, K_FOREVER); /* Toggle LED on sent report */ (void)gpio_pin_toggle(led0.port, led0.pin); } } + return 0; } diff --git a/samples/subsys/usb/hid-mouse/usbd_next_prj.conf b/samples/subsys/usb/hid-mouse/usbd_next_prj.conf deleted file mode 100644 index 9c8894b2126..00000000000 --- a/samples/subsys/usb/hid-mouse/usbd_next_prj.conf +++ /dev/null @@ -1,11 +0,0 @@ -CONFIG_USB_DEVICE_STACK_NEXT=y -CONFIG_USBD_HID_SUPPORT=y - -CONFIG_LOG=y -CONFIG_USBD_LOG_LEVEL_WRN=y -CONFIG_USBD_HID_LOG_LEVEL_WRN=y -CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y -CONFIG_SAMPLE_USBD_PID=0x0007 - -CONFIG_GPIO=y -CONFIG_INPUT=y diff --git a/samples/subsys/usb/legacy/cdc_acm/CMakeLists.txt b/samples/subsys/usb/legacy/cdc_acm/CMakeLists.txt new file mode 100644 index 00000000000..c97b4990766 --- /dev/null +++ b/samples/subsys/usb/legacy/cdc_acm/CMakeLists.txt @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(cdc_acm) + +include(${ZEPHYR_BASE}/samples/subsys/usb/common/common.cmake) +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/subsys/usb/legacy/cdc_acm/README.rst b/samples/subsys/usb/legacy/cdc_acm/README.rst new file mode 100644 index 00000000000..1e7a4d15300 --- /dev/null +++ b/samples/subsys/usb/legacy/cdc_acm/README.rst @@ -0,0 +1,102 @@ +.. zephyr:code-sample:: legacy-usb-cdc-acm + :name: Legacy USB CDC ACM UART sample + :relevant-api: _usb_device_core_api uart_interface + + Use USB CDC ACM UART driver to implement a serial port echo. + +Overview +******** + +This sample app demonstrates use of a USB Communication Device Class (CDC) +Abstract Control Model (ACM) driver provided by the Zephyr project. +Received data from the serial port is echoed back to the same port +provided by this driver. + +.. note:: + This samples demonstrate deprecated :ref:`usb_device_stack`. + +This sample can be found under :zephyr_file:`samples/subsys/usb/legacy/cdc_acm` in the +Zephyr project tree. + +Requirements +************ + +This project requires an USB device driver, which is available for multiple +boards supported in Zephyr. + +Building and Running +******************** + +Reel Board +=========== + +To see the console output of the app, open a serial port emulator and +attach it to the USB to TTL Serial cable. Build and flash the project: + +.. zephyr-app-commands:: + :zephyr-app: samples/subsys/usb/legacy/cdc_acm + :board: reel_board + :goals: flash + :compact: + +Running +======= + +Plug the board into a host device, for example, a PC running Linux. +The board will be detected as shown by the Linux dmesg command: + +.. code-block:: console + + usb 9-1: new full-speed USB device number 112 using uhci_hcd + usb 9-1: New USB device found, idVendor=8086, idProduct=f8a1 + usb 9-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3 + usb 9-1: Product: CDC-ACM + usb 9-1: Manufacturer: Intel + usb 9-1: SerialNumber: 00.01 + cdc_acm 9-1:1.0: ttyACM1: USB ACM device + +The app prints on serial output (UART1), used for the console: + +.. code-block:: console + + Wait for DTR + +Open a serial port emulator, for example minicom +and attach it to detected CDC ACM device: + +.. code-block:: console + + minicom --device /dev/ttyACM1 + +The app should respond on serial output with: + +.. code-block:: console + + DTR set, start test + Baudrate detected: 115200 + +And on ttyACM device, provided by zephyr USB device stack: + +.. code-block:: console + + Send characters to the UART device + Characters read: + +The characters entered in serial port emulator will be echoed back. + +Troubleshooting +=============== + +If the ModemManager runs on your operating system, it will try +to access the CDC ACM device and maybe you can see several characters +including "AT" on the terminal attached to the CDC ACM device. +You can add or extend the udev rule for your board to inform +ModemManager to skip the CDC ACM device. +For this example, it would look like this: + +.. code-block:: none + + ATTRS{idVendor}=="8086" ATTRS{idProduct}=="f8a1", ENV{ID_MM_DEVICE_IGNORE}="1" + +You can use +``/lib/udev/rules.d/77-mm-usb-device-blacklist.rules`` as reference. diff --git a/samples/subsys/usb/legacy/cdc_acm/app.overlay b/samples/subsys/usb/legacy/cdc_acm/app.overlay new file mode 100644 index 00000000000..c50d0fd9d69 --- /dev/null +++ b/samples/subsys/usb/legacy/cdc_acm/app.overlay @@ -0,0 +1,11 @@ +/* + * Copyright (c) 2021 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +&zephyr_udc0 { + cdc_acm_uart0 { + compatible = "zephyr,cdc-acm-uart"; + }; +}; diff --git a/samples/subsys/usb/legacy/cdc_acm/prj.conf b/samples/subsys/usb/legacy/cdc_acm/prj.conf new file mode 100644 index 00000000000..55e4fe9217f --- /dev/null +++ b/samples/subsys/usb/legacy/cdc_acm/prj.conf @@ -0,0 +1,11 @@ +CONFIG_STDOUT_CONSOLE=y +CONFIG_USB_DEVICE_STACK=y +CONFIG_USB_DEVICE_PRODUCT="Zephyr CDC ACM sample" +CONFIG_USB_DEVICE_PID=0x0001 +CONFIG_LOG=y +CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y +CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y +CONFIG_SERIAL=y +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_UART_LINE_CTRL=y +CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n diff --git a/samples/subsys/usb/legacy/cdc_acm/sample.yaml b/samples/subsys/usb/legacy/cdc_acm/sample.yaml new file mode 100644 index 00000000000..b61deab71ff --- /dev/null +++ b/samples/subsys/usb/legacy/cdc_acm/sample.yaml @@ -0,0 +1,7 @@ +sample: + name: CDC ACM USB +tests: + sample.usb.legacy.cdc-acm: + depends_on: usb_device + tags: usb + build_only: true diff --git a/samples/subsys/usb/legacy/cdc_acm/src/main.c b/samples/subsys/usb/legacy/cdc_acm/src/main.c new file mode 100644 index 00000000000..d06161f0f5c --- /dev/null +++ b/samples/subsys/usb/legacy/cdc_acm/src/main.c @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2019 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +LOG_MODULE_REGISTER(cdc_acm_echo, LOG_LEVEL_INF); + +const struct device *const uart_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart); + +#define RING_BUF_SIZE 1024 +uint8_t ring_buffer[RING_BUF_SIZE]; + +struct ring_buf ringbuf; + +static bool rx_throttled; + +static inline void print_baudrate(const struct device *dev) +{ + uint32_t baudrate; + int ret; + + ret = uart_line_ctrl_get(dev, UART_LINE_CTRL_BAUD_RATE, &baudrate); + if (ret) { + LOG_WRN("Failed to get baudrate, ret code %d", ret); + } else { + LOG_INF("Baudrate %u", baudrate); + } +} + +static void interrupt_handler(const struct device *dev, void *user_data) +{ + ARG_UNUSED(user_data); + + while (uart_irq_update(dev) && uart_irq_is_pending(dev)) { + if (!rx_throttled && uart_irq_rx_ready(dev)) { + int recv_len, rb_len; + uint8_t buffer[64]; + size_t len = MIN(ring_buf_space_get(&ringbuf), + sizeof(buffer)); + + if (len == 0) { + /* Throttle because ring buffer is full */ + uart_irq_rx_disable(dev); + rx_throttled = true; + continue; + } + + recv_len = uart_fifo_read(dev, buffer, len); + if (recv_len < 0) { + LOG_ERR("Failed to read UART FIFO"); + recv_len = 0; + }; + + rb_len = ring_buf_put(&ringbuf, buffer, recv_len); + if (rb_len < recv_len) { + LOG_ERR("Drop %u bytes", recv_len - rb_len); + } + + LOG_DBG("tty fifo -> ringbuf %d bytes", rb_len); + if (rb_len) { + uart_irq_tx_enable(dev); + } + } + + if (uart_irq_tx_ready(dev)) { + uint8_t buffer[64]; + int rb_len, send_len; + + rb_len = ring_buf_get(&ringbuf, buffer, sizeof(buffer)); + if (!rb_len) { + LOG_DBG("Ring buffer empty, disable TX IRQ"); + uart_irq_tx_disable(dev); + continue; + } + + if (rx_throttled) { + uart_irq_rx_enable(dev); + rx_throttled = false; + } + + send_len = uart_fifo_fill(dev, buffer, rb_len); + if (send_len < rb_len) { + LOG_ERR("Drop %d bytes", rb_len - send_len); + } + + LOG_DBG("ringbuf -> tty fifo %d bytes", send_len); + } + } +} + +int main(void) +{ + int ret; + + if (!device_is_ready(uart_dev)) { + LOG_ERR("CDC ACM device not ready"); + return 0; + } + + ret = usb_enable(NULL); + if (ret != 0) { + LOG_ERR("Failed to enable USB"); + return 0; + } + + ring_buf_init(&ringbuf, sizeof(ring_buffer), ring_buffer); + + LOG_INF("Wait for DTR"); + + while (true) { + uint32_t dtr = 0U; + + uart_line_ctrl_get(uart_dev, UART_LINE_CTRL_DTR, &dtr); + if (dtr) { + break; + } + + k_sleep(K_MSEC(100)); + } + + LOG_INF("DTR set"); + + /* They are optional, we use them to test the interrupt endpoint */ + ret = uart_line_ctrl_set(uart_dev, UART_LINE_CTRL_DCD, 1); + if (ret) { + LOG_WRN("Failed to set DCD, ret code %d", ret); + } + + ret = uart_line_ctrl_set(uart_dev, UART_LINE_CTRL_DSR, 1); + if (ret) { + LOG_WRN("Failed to set DSR, ret code %d", ret); + } + + /* Wait 100ms for the host to do all settings */ + k_msleep(100); + + print_baudrate(uart_dev); + + uart_irq_callback_set(uart_dev, interrupt_handler); + /* Enable rx interrupts */ + uart_irq_rx_enable(uart_dev); + + return 0; +} diff --git a/samples/subsys/usb/legacy/hid-mouse/CMakeLists.txt b/samples/subsys/usb/legacy/hid-mouse/CMakeLists.txt new file mode 100644 index 00000000000..d2217c00a47 --- /dev/null +++ b/samples/subsys/usb/legacy/hid-mouse/CMakeLists.txt @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(hid-mouse) + +include(${ZEPHYR_BASE}/samples/subsys/usb/common/common.cmake) +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/subsys/usb/legacy/hid-mouse/README.rst b/samples/subsys/usb/legacy/hid-mouse/README.rst new file mode 100644 index 00000000000..aa82e23804a --- /dev/null +++ b/samples/subsys/usb/legacy/hid-mouse/README.rst @@ -0,0 +1,114 @@ +.. zephyr:code-sample:: legacy-usb-hid-mouse + :name: Legacy USB HID mouse + :relevant-api: _usb_device_core_api usb_hid_device_api input_interface + + Implement a basic HID mouse device. + +Overview +******** + +This sample app demonstrates use of a USB Human Interface Device (HID) driver +by the Zephyr project. This very simple driver enumerates a board with a button +into a mouse that has a left mouse button and optionally (depending on +the number of buttons on the board) a right mouse button, X-axis movement, +and Y-axis movement. +If the USB peripheral driver supports remote wakeup feature, wakeup request +will be performed on every button click if the bus is in suspended state. +This sample can be found under :zephyr_file:`samples/subsys/usb/legacy/hid-mouse` in the +Zephyr project tree. + +.. note:: + This samples demonstrate deprecated :ref:`usb_device_stack`. + +Requirements +************ + +This project requires an USB device driver and uses the :ref:`input` API. +There must be a :dtcompatible:`gpio-keys` group of buttons or keys defined at +the board level that can generate input events, otherwise the example will build +but not work as expected. + +The key mapping in the sample is as follows: + +- ``INPUT_KEY_0``: left button +- ``INPUT_KEY_1``: right button +- ``INPUT_KEY_2``: move the mouse along the x-axis +- ``INPUT_KEY_3``: move the mouse along the y-axis + +An LED must also be configured via the ``led0`` devicetree alias. You may also +see this error if you try to build this sample for an unsupported board: + +.. code-block:: none + + Unsupported board: led0 devicetree alias is not defined + +Building and Running +******************** + +This sample can be built for multiple boards, in this example we will build it +for the nucleo_f070rb board: + +.. zephyr-app-commands:: + :zephyr-app: samples/subsys/usb/legacy/hid-mouse + :board: nucleo_f070rb + :goals: build + :compact: + +After you have built and flashed the sample app image to your board, plug the +board into a host device, for example, a PC running Linux. +The board will be detected as shown by the Linux dmesg command: + +.. code-block:: console + + dmesg | tail -10 + usb 2-2: new full-speed USB device number 2 using at91_ohci + usb 2-2: New USB device found, idVendor=2fe3, idProduct=0007, bcdDevice= 2.03 + usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3 + usb 2-2: Product: Zephyr HID mouse sample + usb 2-2: Manufacturer: ZEPHYR + usb 2-2: SerialNumber: 86FE679A598AC47A + input: ZEPHYR Zephyr HID mouse sample as /devices/soc0/ahb/600000.ohci/usb2/2-2/2-2:1.0/0003:2FE3:0100.0001/input/input0 + hid-generic 0003:2FE3:0100.0001: input: USB HID v1.10 Mouse [ZEPHYR Zephyr HID mouse sample] on usb-at91-2/input0 + +You can also monitor mouse events by using the standard Linux ``evtest`` command +(see the `Ubuntu evtest man page`_ for more information about this tool): + +.. _Ubuntu evtest man page: + http://manpages.ubuntu.com/manpages/trusty/man1/evtest.1.html + +.. code-block:: console + + sudo evtest /dev/input/event0 + Input driver version is 1.0.1 + Input device ID: bus 0x3 vendor 0x2fe3 product 0x7 version 0x110 + Input device name: "ZEPHYR Zephyr HID mouse sample" + Supported events: + Event type 0 (EV_SYN) + Event type 1 (EV_KEY) + Event code 272 (BTN_LEFT) + Event code 273 (BTN_RIGHT) + Event code 274 (BTN_MIDDLE) + Event type 2 (EV_REL) + Event code 0 (REL_X) + Event code 1 (REL_Y) + Event code 8 (REL_WHEEL) + Event type 4 (EV_MSC) + Event code 4 (MSC_SCAN) + Properties: + Testing ... (interrupt to exit) + +When you press the button on your board, it will act as if the left +mouse button was pressed, and this information will be displayed +by ``evtest``: + +.. code-block:: console + + Event: time 1167609663.618515, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90001 + Event: time 1167609663.618515, type 1 (EV_KEY), code 272 (BTN_LEFT), value 1 + Event: time 1167609663.618515, -------------- SYN_REPORT ------------ + Event: time 1167609663.730510, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90001 + Event: time 1167609663.730510, type 1 (EV_KEY), code 272 (BTN_LEFT), value 0 + Event: time 1167609663.730510, -------------- SYN_REPORT ------------ + +If your board has more than one button, they will act as right mouse button, +X-axis movement, and Y-axis movement. diff --git a/samples/subsys/usb/legacy/hid-mouse/prj.conf b/samples/subsys/usb/legacy/hid-mouse/prj.conf new file mode 100644 index 00000000000..b797a7382e8 --- /dev/null +++ b/samples/subsys/usb/legacy/hid-mouse/prj.conf @@ -0,0 +1,13 @@ +CONFIG_USB_DEVICE_STACK=y +CONFIG_USB_DEVICE_HID=y +CONFIG_USB_DEVICE_PRODUCT="Zephyr HID mouse sample" +CONFIG_USB_DEVICE_PID=0x0007 +CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n + +CONFIG_LOG=y +CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y +CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y + +CONFIG_GPIO=y +CONFIG_INPUT=y +CONFIG_INPUT_MODE_SYNCHRONOUS=y diff --git a/samples/subsys/usb/legacy/hid-mouse/sample.yaml b/samples/subsys/usb/legacy/hid-mouse/sample.yaml new file mode 100644 index 00000000000..4dc2da90798 --- /dev/null +++ b/samples/subsys/usb/legacy/hid-mouse/sample.yaml @@ -0,0 +1,15 @@ +sample: + name: USB HID mouse sample +common: + harness: button + filter: dt_alias_exists("sw0") and dt_alias_exists("led0") + depends_on: + - gpio +tests: + sample.usb.legacy.hid-mouse: + depends_on: + - usb_device + build_only: true + platform_exclude: + - frdm_mcxn947/mcxn947/cpu0 + tags: usb diff --git a/samples/subsys/usb/legacy/hid-mouse/src/main.c b/samples/subsys/usb/legacy/hid-mouse/src/main.c new file mode 100644 index 00000000000..e668ce64758 --- /dev/null +++ b/samples/subsys/usb/legacy/hid-mouse/src/main.c @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2018 qianfan Zhao + * Copyright (c) 2018, 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include +LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); + +static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios); +static const uint8_t hid_report_desc[] = HID_MOUSE_REPORT_DESC(2); +static enum usb_dc_status_code usb_status; + +#define MOUSE_BTN_LEFT 0 +#define MOUSE_BTN_RIGHT 1 + +enum mouse_report_idx { + MOUSE_BTN_REPORT_IDX = 0, + MOUSE_X_REPORT_IDX = 1, + MOUSE_Y_REPORT_IDX = 2, + MOUSE_WHEEL_REPORT_IDX = 3, + MOUSE_REPORT_COUNT = 4, +}; + +K_MSGQ_DEFINE(mouse_msgq, MOUSE_REPORT_COUNT, 2, 1); +static K_SEM_DEFINE(ep_write_sem, 0, 1); + +static inline void status_cb(enum usb_dc_status_code status, const uint8_t *param) +{ + usb_status = status; +} + +static ALWAYS_INLINE void rwup_if_suspended(void) +{ + if (IS_ENABLED(CONFIG_USB_DEVICE_REMOTE_WAKEUP)) { + if (usb_status == USB_DC_SUSPEND) { + usb_wakeup_request(); + return; + } + } +} + +static void input_cb(struct input_event *evt, void *user_data) +{ + static uint8_t tmp[MOUSE_REPORT_COUNT]; + + ARG_UNUSED(user_data); + + switch (evt->code) { + case INPUT_KEY_0: + rwup_if_suspended(); + WRITE_BIT(tmp[MOUSE_BTN_REPORT_IDX], MOUSE_BTN_LEFT, evt->value); + break; + case INPUT_KEY_1: + rwup_if_suspended(); + WRITE_BIT(tmp[MOUSE_BTN_REPORT_IDX], MOUSE_BTN_RIGHT, evt->value); + break; + case INPUT_KEY_2: + if (evt->value) { + tmp[MOUSE_X_REPORT_IDX] += 10U; + } + + break; + case INPUT_KEY_3: + if (evt->value) { + tmp[MOUSE_Y_REPORT_IDX] += 10U; + } + + break; + default: + LOG_INF("Unrecognized input code %u value %d", + evt->code, evt->value); + return; + } + + if (k_msgq_put(&mouse_msgq, tmp, K_NO_WAIT) != 0) { + LOG_ERR("Failed to put new input event"); + } + + tmp[MOUSE_X_REPORT_IDX] = 0U; + tmp[MOUSE_Y_REPORT_IDX] = 0U; + +} + +INPUT_CALLBACK_DEFINE(NULL, input_cb, NULL); + +static void int_in_ready_cb(const struct device *dev) +{ + ARG_UNUSED(dev); + k_sem_give(&ep_write_sem); +} + +static const struct hid_ops ops = { + .int_in_ready = int_in_ready_cb, +}; + +int main(void) +{ + const struct device *hid_dev; + int ret; + + if (!gpio_is_ready_dt(&led0)) { + LOG_ERR("LED device %s is not ready", led0.port->name); + return 0; + } + + hid_dev = device_get_binding("HID_0"); + if (hid_dev == NULL) { + LOG_ERR("Cannot get USB HID Device"); + return 0; + } + + ret = gpio_pin_configure_dt(&led0, GPIO_OUTPUT); + if (ret < 0) { + LOG_ERR("Failed to configure the LED pin, error: %d", ret); + return 0; + } + + usb_hid_register_device(hid_dev, + hid_report_desc, sizeof(hid_report_desc), + &ops); + + usb_hid_init(hid_dev); + + ret = usb_enable(status_cb); + if (ret != 0) { + LOG_ERR("Failed to enable USB"); + return 0; + } + + while (true) { + uint8_t __aligned(sizeof(void *)) report[MOUSE_REPORT_COUNT]; + + k_msgq_get(&mouse_msgq, &report, K_FOREVER); + + ret = hid_int_ep_write(hid_dev, report, MOUSE_REPORT_COUNT, NULL); + if (ret) { + LOG_ERR("HID write error, %d", ret); + } else { + k_sem_take(&ep_write_sem, K_FOREVER); + /* Toggle LED on sent report */ + (void)gpio_pin_toggle(led0.port, led0.pin); + } + } + return 0; +} diff --git a/samples/subsys/usb/legacy/mass/CMakeLists.txt b/samples/subsys/usb/legacy/mass/CMakeLists.txt new file mode 100644 index 00000000000..f80c6eb9afa --- /dev/null +++ b/samples/subsys/usb/legacy/mass/CMakeLists.txt @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(mass) + +include(${ZEPHYR_BASE}/samples/subsys/usb/common/common.cmake) +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/subsys/usb/legacy/mass/README.rst b/samples/subsys/usb/legacy/mass/README.rst new file mode 100644 index 00000000000..8e3ae5319df --- /dev/null +++ b/samples/subsys/usb/legacy/mass/README.rst @@ -0,0 +1,32 @@ +.. zephyr:code-sample:: legacy-usb-mass + :name: Legacy USB Mass Storage + :relevant-api: _usb_device_core_api + + Expose board's RAM as a USB disk using USB Mass Storage driver. + +Overview +******** + +This sample app demonstrates use of a USB Mass Storage driver by the Zephyr +project. This very simple driver enumerates a board with RAM disk into an USB +disk. This sample can be found under +:zephyr_file:`samples/subsys/usb/legacy/mass` in the Zephyr project tree. + +.. note:: + This samples demonstrate deprecated :ref:`usb_device_stack`. + +Requirements +************ + +This project requires a USB device driver, and either 96KiB of RAM or a FLASH device. + +Building and Running +******************** + +The configurations selects RAM-based disk without any file system. + +.. zephyr-app-commands:: + :zephyr-app: samples/subsys/usb/legacy/mass + :board: reel_board + :goals: build + :compact: diff --git a/samples/subsys/usb/legacy/mass/app.overlay b/samples/subsys/usb/legacy/mass/app.overlay new file mode 100644 index 00000000000..ff09a0f8ca7 --- /dev/null +++ b/samples/subsys/usb/legacy/mass/app.overlay @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + ramdisk0 { + compatible = "zephyr,ram-disk"; + disk-name = "RAM"; + sector-size = <512>; + sector-count = <192>; + }; +}; diff --git a/samples/subsys/usb/legacy/mass/prj.conf b/samples/subsys/usb/legacy/mass/prj.conf new file mode 100644 index 00000000000..0954afcd0ee --- /dev/null +++ b/samples/subsys/usb/legacy/mass/prj.conf @@ -0,0 +1,14 @@ +CONFIG_STDOUT_CONSOLE=y + +#USB related configs +CONFIG_USB_DEVICE_STACK=y +CONFIG_USB_DEVICE_PRODUCT="Zephyr MSC sample" +CONFIG_USB_DEVICE_PID=0x0008 +CONFIG_LOG=y +CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y +CONFIG_USB_MASS_STORAGE=y +CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y +CONFIG_USB_MASS_STORAGE_LOG_LEVEL_ERR=y +CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n + +CONFIG_MAIN_STACK_SIZE=1536 diff --git a/samples/subsys/usb/legacy/mass/sample.yaml b/samples/subsys/usb/legacy/mass/sample.yaml new file mode 100644 index 00000000000..f4d1bb2f96d --- /dev/null +++ b/samples/subsys/usb/legacy/mass/sample.yaml @@ -0,0 +1,10 @@ +sample: + name: Mass Storage +tests: + sample.usb.legacy.mass_ram_none: + min_ram: 128 + depends_on: usb_device + build_only: true + arch_exclude: posix + tags: + - usb diff --git a/samples/subsys/usb/legacy/mass/src/main.c b/samples/subsys/usb/legacy/mass/src/main.c new file mode 100644 index 00000000000..52dfa00294e --- /dev/null +++ b/samples/subsys/usb/legacy/mass/src/main.c @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2016 Intel Corporation. + * Copyright (c) 2019-2020 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include +LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); + +int main(void) +{ + int ret; + + ret = usb_enable(NULL); + if (ret != 0) { + LOG_ERR("Failed to enable USB"); + return 0; + } + + LOG_INF("The device is put in USB mass storage mode"); + + return 0; +} diff --git a/samples/subsys/usb/mass/README.rst b/samples/subsys/usb/mass/README.rst index b68f57497b2..910c34e7c1e 100644 --- a/samples/subsys/usb/mass/README.rst +++ b/samples/subsys/usb/mass/README.rst @@ -1,6 +1,6 @@ .. zephyr:code-sample:: usb-mass :name: USB Mass Storage - :relevant-api: usbd_api usbd_msc_device _usb_device_core_api file_system_api + :relevant-api: usbd_api usbd_msc_device file_system_api Expose board's RAM or FLASH as a USB disk using USB Mass Storage driver. diff --git a/samples/subsys/usb/mass/boards/rpi_pico_rp2040_w.overlay b/samples/subsys/usb/mass/boards/rpi_pico_rp2040_w.overlay new file mode 100644 index 00000000000..759ce77a4f8 --- /dev/null +++ b/samples/subsys/usb/mass/boards/rpi_pico_rp2040_w.overlay @@ -0,0 +1 @@ +#include "rpi_pico.overlay" diff --git a/samples/subsys/usb/mass/prj.conf b/samples/subsys/usb/mass/prj.conf index 0954afcd0ee..dd9d784070d 100644 --- a/samples/subsys/usb/mass/prj.conf +++ b/samples/subsys/usb/mass/prj.conf @@ -1,14 +1,15 @@ +CONFIG_USB_DEVICE_STACK_NEXT=y + CONFIG_STDOUT_CONSOLE=y +CONFIG_SERIAL=y +CONFIG_UART_LINE_CTRL=y +CONFIG_USBD_MSC_CLASS=y +CONFIG_USBD_MSC_LUNS_PER_INSTANCE=3 -#USB related configs -CONFIG_USB_DEVICE_STACK=y -CONFIG_USB_DEVICE_PRODUCT="Zephyr MSC sample" -CONFIG_USB_DEVICE_PID=0x0008 CONFIG_LOG=y -CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y -CONFIG_USB_MASS_STORAGE=y -CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y -CONFIG_USB_MASS_STORAGE_LOG_LEVEL_ERR=y -CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n +CONFIG_USBD_LOG_LEVEL_WRN=y +CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y -CONFIG_MAIN_STACK_SIZE=1536 +CONFIG_SAMPLE_USBD_PID=0x0008 +CONFIG_SAMPLE_USBD_PRODUCT="USBD MSC sample" +CONFIG_MAIN_STACK_SIZE=2048 diff --git a/samples/subsys/usb/mass/sample.yaml b/samples/subsys/usb/mass/sample.yaml index 4dbf2126798..2065a35ad30 100644 --- a/samples/subsys/usb/mass/sample.yaml +++ b/samples/subsys/usb/mass/sample.yaml @@ -1,30 +1,13 @@ sample: name: Mass Storage tests: - sample.usb.mass_ram_none: - min_ram: 128 - depends_on: usb_device - arch_exclude: posix - extra_args: - - EXTRA_DTC_OVERLAY_FILE="ramdisk.overlay" - extra_configs: - - CONFIG_LOG_DEFAULT_LEVEL=3 - tags: - - msd - - usb - harness: console - harness_config: - type: multi_line - ordered: true - regex: - - "No file system selected" - - "The device is put in USB mass storage mode." - sample.usb_device_next.mass_ram_none: + sample.usbd.mass_ram_none: min_ram: 128 depends_on: usbd integration_platforms: - nrf52840dk/nrf52840 - nrf54h20dk/nrf54h20/cpuapp + - nrf54lm20dk/nrf54lm20a/cpuapp - frdm_k64f - stm32f723e_disco - nucleo_f413zh @@ -32,12 +15,8 @@ tests: - mimxrt1060_evk/mimxrt1062/qspi - max32690evkit/max32690/m4 extra_args: - - CONF_FILE="usbd_next_prj.conf" - EXTRA_DTC_OVERLAY_FILE="ramdisk.overlay" - extra_configs: - - CONFIG_LOG_DEFAULT_LEVEL=3 tags: - - msd - usb harness: console harness_config: @@ -45,43 +24,23 @@ tests: ordered: true regex: - "No file system selected" - - "The device is put in USB mass storage mode." - sample.usb.mass_ram_fat: + - "The device is put in USB mass storage mode" + sample.usbd.mass_ram_fat: min_ram: 128 - depends_on: usb_device - arch_exclude: posix + depends_on: usbd + integration_platforms: + - nrf52840dk/nrf52840 + - nrf54h20dk/nrf54h20/cpuapp + - frdm_k64f + - stm32f723e_disco + - nucleo_f413zh + - mimxrt685_evk/mimxrt685s/cm33 + - mimxrt1060_evk/mimxrt1062/qspi extra_args: - EXTRA_DTC_OVERLAY_FILE="ramdisk.overlay" extra_configs: - - CONFIG_LOG_DEFAULT_LEVEL=3 - CONFIG_APP_MSC_STORAGE_RAM=y tags: - - msd - - usb - harness: console - harness_config: - type: multi_line - ordered: true - regex: - - "End of files" - - "The device is put in USB mass storage mode." - sample.usb.mass_flash_fatfs: - min_ram: 32 - modules: - - fatfs - depends_on: usb_device - filter: dt_compat_enabled("nordic,qspi-nor") - platform_allow: - - nrf52840dk/nrf52840 - - nrf5340dk/nrf5340/cpuapp - - adafruit_feather_nrf52840/nrf52840/sense - integration_platforms: - - nrf52840dk/nrf52840 - extra_configs: - - CONFIG_LOG_DEFAULT_LEVEL=3 - - CONFIG_APP_MSC_STORAGE_FLASH_FATFS=y - tags: - - msd - usb harness: console harness_config: @@ -89,8 +48,8 @@ tests: ordered: true regex: - "End of files" - - "The device is put in USB mass storage mode." - sample.usb_device_next.mass_flash_fatfs: + - "The device is put in USB mass storage mode" + sample.usbd.mass_flash_fatfs: min_ram: 32 modules: - fatfs @@ -102,43 +61,18 @@ tests: - adafruit_feather_nrf52840/nrf52840/sense integration_platforms: - nrf52840dk/nrf52840 - extra_args: CONF_FILE="usbd_next_prj.conf" extra_configs: - - CONFIG_LOG_DEFAULT_LEVEL=3 - CONFIG_APP_MSC_STORAGE_FLASH_FATFS=y tags: - - msd - - usb - harness: console - harness_config: - type: multi_line - ordered: true - regex: - - "End of files" - - "The device is put in USB mass storage mode." - sample.usb.mass_sdhc_fatfs: - min_ram: 32 - filter: dt_compat_enabled("zephyr,sdmmc-disk") - modules: - - fatfs - depends_on: - - usb_device - - sdhc - extra_configs: - - CONFIG_LOG_DEFAULT_LEVEL=3 - - CONFIG_APP_MSC_STORAGE_SDCARD=y - tags: - - msd - usb harness: console harness_config: - fixture: fixture_sdcard type: multi_line ordered: true regex: - "End of files" - - "The device is put in USB mass storage mode." - sample.usb_device_next.mass_sdhc_fatfs: + - "The device is put in USB mass storage mode" + sample.usbd.mass_sdhc_fatfs: min_ram: 32 filter: dt_compat_enabled("zephyr,sdmmc-disk") modules: @@ -147,11 +81,8 @@ tests: - usbd - sdhc extra_configs: - - CONFIG_LOG_DEFAULT_LEVEL=3 - CONFIG_APP_MSC_STORAGE_SDCARD=y - extra_args: CONF_FILE="usbd_next_prj.conf" tags: - - msd - usb harness: console harness_config: @@ -160,12 +91,12 @@ tests: ordered: true regex: - "End of files" - - "The device is put in USB mass storage mode." - sample.usb.mass_flash_littlefs: + - "The device is put in USB mass storage mode" + sample.usbd.mass_flash_littlefs: modules: - littlefs min_ram: 32 - depends_on: usb_device + depends_on: usbd filter: dt_compat_enabled("nordic,qspi-nor") platform_allow: - nrf52840dk/nrf52840 @@ -174,10 +105,8 @@ tests: integration_platforms: - nrf52840dk/nrf52840 extra_configs: - - CONFIG_LOG_DEFAULT_LEVEL=3 - CONFIG_APP_MSC_STORAGE_FLASH_LITTLEFS=y tags: - - msd - usb harness: console harness_config: @@ -185,4 +114,4 @@ tests: ordered: true regex: - "End of files" - - "The device is put in USB mass storage mode." + - "The device is put in USB mass storage mode" diff --git a/samples/subsys/usb/mass/src/main.c b/samples/subsys/usb/mass/src/main.c index df90560d835..59a770a5154 100644 --- a/samples/subsys/usb/mass/src/main.c +++ b/samples/subsys/usb/mass/src/main.c @@ -9,13 +9,12 @@ #include #include -#include #include #include #include #include -LOG_MODULE_REGISTER(main); +LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); #if CONFIG_DISK_DRIVER_FLASH #include @@ -41,7 +40,6 @@ FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(storage); static struct fs_mount_t fs_mnt; -#if defined(CONFIG_USB_DEVICE_STACK_NEXT) static struct usbd_context *sample_usbd; #if CONFIG_DISK_DRIVER_RAM @@ -56,28 +54,6 @@ USBD_DEFINE_MSC_LUN(nand, "NAND", "Zephyr", "FlashDisk", "0.00"); USBD_DEFINE_MSC_LUN(sd, "SD", "Zephyr", "SD", "0.00"); #endif -static int enable_usb_device_next(void) -{ - int err; - - sample_usbd = sample_usbd_init_device(NULL); - if (sample_usbd == NULL) { - LOG_ERR("Failed to initialize USB device"); - return -ENODEV; - } - - err = usbd_enable(sample_usbd); - if (err) { - LOG_ERR("Failed to enable device support"); - return err; - } - - LOG_DBG("USB device support enabled"); - - return 0; -} -#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */ - static int setup_flash(struct fs_mount_t *mnt) { int rc = 0; @@ -215,16 +191,24 @@ int main(void) setup_disk(); -#if defined(CONFIG_USB_DEVICE_STACK_NEXT) - ret = enable_usb_device_next(); -#else - ret = usb_enable(NULL); -#endif + sample_usbd = sample_usbd_init_device(NULL); + if (sample_usbd == NULL) { + LOG_ERR("Failed to initialize USB device"); + return -ENODEV; + } + + ret = usbd_enable(sample_usbd); + if (ret) { + LOG_ERR("Failed to enable device support"); + return ret; + } + if (ret != 0) { LOG_ERR("Failed to enable USB"); return 0; } - LOG_INF("The device is put in USB mass storage mode.\n"); + LOG_INF("The device is put in USB mass storage mode"); + return 0; } diff --git a/samples/subsys/usb/mass/usbd_next_prj.conf b/samples/subsys/usb/mass/usbd_next_prj.conf deleted file mode 100644 index dd9d784070d..00000000000 --- a/samples/subsys/usb/mass/usbd_next_prj.conf +++ /dev/null @@ -1,15 +0,0 @@ -CONFIG_USB_DEVICE_STACK_NEXT=y - -CONFIG_STDOUT_CONSOLE=y -CONFIG_SERIAL=y -CONFIG_UART_LINE_CTRL=y -CONFIG_USBD_MSC_CLASS=y -CONFIG_USBD_MSC_LUNS_PER_INSTANCE=3 - -CONFIG_LOG=y -CONFIG_USBD_LOG_LEVEL_WRN=y -CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y - -CONFIG_SAMPLE_USBD_PID=0x0008 -CONFIG_SAMPLE_USBD_PRODUCT="USBD MSC sample" -CONFIG_MAIN_STACK_SIZE=2048