Skip to content

Commit

Permalink
samples: usb: use common sample USBD initialization
Browse files Browse the repository at this point in the history
Use the common USBD sample initialization helper where new USB device
support has already been added.

Signed-off-by: Johann Fischer <[email protected]>
  • Loading branch information
jfischer-no authored and carlescufi committed Dec 13, 2023
1 parent b00a998 commit 9566419
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 153 deletions.
1 change: 1 addition & 0 deletions samples/subsys/usb/cdc_acm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ 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})
9 changes: 9 additions & 0 deletions samples/subsys/usb/cdc_acm/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2023 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

# Source common USB sample options used to initialize new experimental USB
# device stack. The scope of these options is limited to USB samples in project
# tree, you cannot use them in your own application.
source "samples/subsys/usb/common/Kconfig.sample_usbd"

source "Kconfig.zephyr"
61 changes: 8 additions & 53 deletions samples/subsys/usb/cdc_acm/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* to the serial port.
*/

#include <sample_usbd.h>

#include <stdio.h>
#include <string.h>
#include <zephyr/device.h>
Expand All @@ -32,66 +34,19 @@ struct ring_buf ringbuf;
static bool rx_throttled;

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
USBD_CONFIGURATION_DEFINE(config_1,
USB_SCD_SELF_POWERED,
200);

USBD_DESC_LANG_DEFINE(sample_lang);
USBD_DESC_MANUFACTURER_DEFINE(sample_mfr, "ZEPHYR");
USBD_DESC_PRODUCT_DEFINE(sample_product, "Zephyr USBD CDC ACM");
USBD_DESC_SERIAL_NUMBER_DEFINE(sample_sn, "0123456789ABCDEF");

USBD_DEVICE_DEFINE(sample_usbd,
DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
0x2fe3, 0x0001);
static struct usbd_contex *sample_usbd;

static int enable_usb_device_next(void)
{
int err;

err = usbd_add_descriptor(&sample_usbd, &sample_lang);
if (err) {
LOG_ERR("Failed to initialize language descriptor (%d)", err);
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_mfr);
if (err) {
LOG_ERR("Failed to initialize manufacturer descriptor (%d)", err);
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_product);
if (err) {
LOG_ERR("Failed to initialize product descriptor (%d)", err);
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_sn);
if (err) {
LOG_ERR("Failed to initialize SN descriptor (%d)", err);
return err;
}

err = usbd_add_configuration(&sample_usbd, &config_1);
if (err) {
LOG_ERR("Failed to add configuration (%d)", err);
return err;
}

err = usbd_register_class(&sample_usbd, "cdc_acm_0", 1);
if (err) {
LOG_ERR("Failed to register CDC ACM class (%d)", err);
return err;
}

err = usbd_init(&sample_usbd);
if (err) {
LOG_ERR("Failed to initialize device support");
return err;
sample_usbd = sample_usbd_init_device();
if (sample_usbd == NULL) {
LOG_ERR("Failed to initialize USB device");
return -ENODEV;
}

err = usbd_enable(&sample_usbd);
err = usbd_enable(sample_usbd);
if (err) {
LOG_ERR("Failed to enable device support");
return err;
Expand Down
3 changes: 3 additions & 0 deletions samples/subsys/usb/cdc_acm/usbd_next_prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ CONFIG_USBD_CDC_ACM_CLASS=y
CONFIG_LOG=y
CONFIG_USBD_LOG_LEVEL_WRN=y
CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y

CONFIG_SAMPLE_USBD_PID=0x0001
CONFIG_SAMPLE_USBD_PRODUCT="USBD CDC ACM sample"
1 change: 1 addition & 0 deletions samples/subsys/usb/console/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(console)

include(${ZEPHYR_BASE}/samples/subsys/usb/common/common.cmake)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
9 changes: 9 additions & 0 deletions samples/subsys/usb/console/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2023 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

# Source common USB sample options used to initialize new experimental USB
# device stack. The scope of these options is limited to USB samples in project
# tree, you cannot use them in your own application.
source "samples/subsys/usb/common/Kconfig.sample_usbd"

source "Kconfig.zephyr"
53 changes: 7 additions & 46 deletions samples/subsys/usb/console/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <sample_usbd.h>

#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/usb/usb_device.h>
Expand All @@ -14,59 +16,18 @@ BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_console), zephyr_cdc_acm_uart),
"Console device is not ACM CDC UART device");

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
USBD_CONFIGURATION_DEFINE(config_1,
USB_SCD_SELF_POWERED,
200);

USBD_DESC_LANG_DEFINE(sample_lang);
USBD_DESC_MANUFACTURER_DEFINE(sample_mfr, "ZEPHYR");
USBD_DESC_PRODUCT_DEFINE(sample_product, "Zephyr USBD ACM console");
USBD_DESC_SERIAL_NUMBER_DEFINE(sample_sn, "0123456789ABCDEF");

USBD_DEVICE_DEFINE(sample_usbd,
DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
0x2fe3, 0x0001);
static struct usbd_contex *sample_usbd;

static int enable_usb_device_next(void)
{
int err;

err = usbd_add_descriptor(&sample_usbd, &sample_lang);
if (err) {
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_mfr);
if (err) {
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_product);
if (err) {
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_sn);
if (err) {
return err;
}

err = usbd_add_configuration(&sample_usbd, &config_1);
if (err) {
return err;
}

err = usbd_register_class(&sample_usbd, "cdc_acm_0", 1);
if (err) {
return err;
}

err = usbd_init(&sample_usbd);
if (err) {
return err;
sample_usbd = sample_usbd_init_device();
if (sample_usbd == NULL) {
return -ENODEV;
}

err = usbd_enable(&sample_usbd);
err = usbd_enable(sample_usbd);
if (err) {
return err;
}
Expand Down
3 changes: 3 additions & 0 deletions samples/subsys/usb/console/usbd_next_prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ CONFIG_USBD_CDC_ACM_CLASS=y
CONFIG_LOG=y
CONFIG_USBD_LOG_LEVEL_WRN=y
CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y

CONFIG_SAMPLE_USBD_PID=0x0004
CONFIG_SAMPLE_USBD_PRODUCT="USBD console sample"
1 change: 1 addition & 0 deletions samples/subsys/usb/mass/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ if((NOT CONFIG_DISK_DRIVER_FLASH) AND
message( FATAL_ERROR "No disk access settings detected." )
endif()

include(${ZEPHYR_BASE}/samples/subsys/usb/common/common.cmake)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

Expand Down
9 changes: 9 additions & 0 deletions samples/subsys/usb/mass/Kconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (c) 2019-2020 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

menu "MSC sample options"

config APP_WIPE_STORAGE
bool "Option to clear the flash area before mounting"
help
Expand Down Expand Up @@ -65,4 +67,11 @@ endif # NORDIC_QSPI_NOR

endif # DISK_DRIVER_FLASH

endmenu

# Source common USB sample options used to initialize new experimental USB
# device stack. The scope of these options is limited to USB samples in project
# tree, you cannot use them in your own application.
source "samples/subsys/usb/common/Kconfig.sample_usbd"

source "Kconfig.zephyr"
62 changes: 8 additions & 54 deletions samples/subsys/usb/mass/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <sample_usbd.h>

#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/usb/usb_device.h>
Expand Down Expand Up @@ -34,19 +36,7 @@ FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(storage);
static struct fs_mount_t fs_mnt;

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
USBD_CONFIGURATION_DEFINE(config_1,
USB_SCD_SELF_POWERED,
200);

USBD_DESC_LANG_DEFINE(sample_lang);
USBD_DESC_MANUFACTURER_DEFINE(sample_mfr, "ZEPHYR");
USBD_DESC_PRODUCT_DEFINE(sample_product, "Zephyr USBD MSC");
USBD_DESC_SERIAL_NUMBER_DEFINE(sample_sn, "0123456789ABCDEF");


USBD_DEVICE_DEFINE(sample_usbd,
DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
0x2fe3, 0x0008);
static struct usbd_contex *sample_usbd;

#if CONFIG_DISK_DRIVER_RAM
USBD_DEFINE_MSC_LUN(RAM, "Zephyr", "RAMDisk", "0.00");
Expand All @@ -64,49 +54,13 @@ static int enable_usb_device_next(void)
{
int err;

err = usbd_add_descriptor(&sample_usbd, &sample_lang);
if (err) {
LOG_ERR("Failed to initialize language descriptor (%d)", err);
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_mfr);
if (err) {
LOG_ERR("Failed to initialize manufacturer descriptor (%d)", err);
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_product);
if (err) {
LOG_ERR("Failed to initialize product descriptor (%d)", err);
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_sn);
if (err) {
LOG_ERR("Failed to initialize SN descriptor (%d)", err);
return err;
}

err = usbd_add_configuration(&sample_usbd, &config_1);
if (err) {
LOG_ERR("Failed to add configuration (%d)", err);
return err;
}

err = usbd_register_class(&sample_usbd, "msc_0", 1);
if (err) {
LOG_ERR("Failed to register MSC class (%d)", err);
return err;
}

err = usbd_init(&sample_usbd);
if (err) {
LOG_ERR("Failed to initialize device support");
return err;
sample_usbd = sample_usbd_init_device();
if (sample_usbd == NULL) {
LOG_ERR("Failed to initialize USB device");
return -ENODEV;
}

err = usbd_enable(&sample_usbd);
err = usbd_enable(sample_usbd);
if (err) {
LOG_ERR("Failed to enable device support");
return err;
Expand Down
2 changes: 2 additions & 0 deletions samples/subsys/usb/mass/usbd_next_prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ 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=1536

0 comments on commit 9566419

Please sign in to comment.