|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdint.h> |
| 8 | + |
| 9 | +#include <zephyr/device.h> |
| 10 | +#include <zephyr/usb/usbd.h> |
| 11 | +#include <zephyr/logging/log.h> |
| 12 | + |
| 13 | +LOG_MODULE_REGISTER(usbd_app_config, CONFIG_LOG_DEFAULT_LEVEL); |
| 14 | + |
| 15 | +USBD_DEVICE_DEFINE(app_usbd, DEVICE_DT_GET(DT_NODELABEL(virtual_udc0)), 0x2fe3, 0x9999); |
| 16 | +USBD_DESC_LANG_DEFINE(app_lang); |
| 17 | +USBD_DESC_MANUFACTURER_DEFINE(app_mfr, "Nordic"); |
| 18 | +USBD_DESC_PRODUCT_DEFINE(app_product, "Virtual UVC device"); |
| 19 | +USBD_DESC_CONFIG_DEFINE(fs_cfg_desc, "FS Configuration"); |
| 20 | +USBD_CONFIGURATION_DEFINE(app_fs_config, 0, 200, &fs_cfg_desc); |
| 21 | + |
| 22 | +/* This is a minimal as possible configuration for the purpose of providing |
| 23 | + * a temporary test bench, and does not support all the configurations of the |
| 24 | + * sample USB config. Notably, only FullSpeed support |
| 25 | + */ |
| 26 | + |
| 27 | +struct usbd_context *app_usbd_init_device(void) |
| 28 | +{ |
| 29 | + int ret; |
| 30 | + |
| 31 | + ret = usbd_add_descriptor(&app_usbd, &app_lang); |
| 32 | + if (ret != 0) { |
| 33 | + LOG_ERR("Failed to initialize language descriptor (%d)", ret); |
| 34 | + return NULL; |
| 35 | + } |
| 36 | + |
| 37 | + ret = usbd_add_descriptor(&app_usbd, &app_mfr); |
| 38 | + if (ret != 0) { |
| 39 | + LOG_ERR("Failed to initialize manufacturer descriptor (%d)", ret); |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + |
| 43 | + ret = usbd_add_descriptor(&app_usbd, &app_product); |
| 44 | + if (ret != 0) { |
| 45 | + LOG_ERR("Failed to initialize product descriptor (%d)", ret); |
| 46 | + return NULL; |
| 47 | + } |
| 48 | + |
| 49 | + ret = usbd_add_configuration(&app_usbd, USBD_SPEED_FS, &app_fs_config); |
| 50 | + if (ret != 0) { |
| 51 | + LOG_ERR("Failed to add Full-Speed configuration"); |
| 52 | + return NULL; |
| 53 | + } |
| 54 | + |
| 55 | + ret = usbd_register_all_classes(&app_usbd, USBD_SPEED_FS, 1, NULL); |
| 56 | + if (ret != 0) { |
| 57 | + LOG_ERR("Failed to add register classes"); |
| 58 | + return NULL; |
| 59 | + } |
| 60 | + |
| 61 | + usbd_device_set_code_triple(&app_usbd, USBD_SPEED_FS, USB_BCC_MISCELLANEOUS, 0x02, 0x01); |
| 62 | + |
| 63 | + usbd_self_powered(&app_usbd, USB_SCD_SELF_POWERED); |
| 64 | + |
| 65 | + ret = usbd_init(&app_usbd); |
| 66 | + if (ret != 0) { |
| 67 | + LOG_ERR("Failed to initialize device support"); |
| 68 | + return NULL; |
| 69 | + } |
| 70 | + |
| 71 | + return &app_usbd; |
| 72 | +} |
0 commit comments