|
| 1 | +/* |
| 2 | + * Copyright (c) 2023-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 | + |
| 12 | +#include <zephyr/logging/log.h> |
| 13 | +LOG_MODULE_REGISTER(usbd_app_config); |
| 14 | + |
| 15 | +#define ZEPHYR_PROJECT_USB_VID 0x2fe3 |
| 16 | +#define APP_USBD_PID 0x9999 |
| 17 | + |
| 18 | +/* doc device instantiation start */ |
| 19 | +USBD_DEVICE_DEFINE(app_usbd, |
| 20 | + DEVICE_DT_GET(DT_NODELABEL(virtual_udc0)), |
| 21 | + ZEPHYR_PROJECT_USB_VID, APP_USBD_PID); |
| 22 | +/* doc device instantiation end */ |
| 23 | + |
| 24 | +/* doc string instantiation start */ |
| 25 | +USBD_DESC_LANG_DEFINE(app_lang); |
| 26 | +USBD_DESC_MANUFACTURER_DEFINE(app_mfr, "Nordic"); |
| 27 | +USBD_DESC_PRODUCT_DEFINE(app_product, "Virtual USB device"); |
| 28 | + |
| 29 | +/* doc string instantiation end */ |
| 30 | + |
| 31 | +USBD_DESC_CONFIG_DEFINE(fs_cfg_desc, "FS Configuration"); |
| 32 | + |
| 33 | +/* doc configuration instantiation start */ |
| 34 | +static const uint8_t attributes = 0; |
| 35 | +USBD_CONFIGURATION_DEFINE(app_fs_config, attributes, 200, &fs_cfg_desc); |
| 36 | +/* doc configuration instantiation end */ |
| 37 | + |
| 38 | +struct usbd_context *app_usbd_init_device(void) |
| 39 | +{ |
| 40 | + int err; |
| 41 | + |
| 42 | + /* doc add string descriptor start */ |
| 43 | + err = usbd_add_descriptor(&app_usbd, &app_lang); |
| 44 | + if (err) { |
| 45 | + LOG_ERR("Failed to initialize language descriptor (%d)", err); |
| 46 | + return NULL; |
| 47 | + } |
| 48 | + |
| 49 | + err = usbd_add_descriptor(&app_usbd, &app_mfr); |
| 50 | + if (err) { |
| 51 | + LOG_ERR("Failed to initialize manufacturer descriptor (%d)", err); |
| 52 | + return NULL; |
| 53 | + } |
| 54 | + |
| 55 | + err = usbd_add_descriptor(&app_usbd, &app_product); |
| 56 | + if (err) { |
| 57 | + LOG_ERR("Failed to initialize product descriptor (%d)", err); |
| 58 | + return NULL; |
| 59 | + } |
| 60 | + /* doc add string descriptor end */ |
| 61 | + |
| 62 | + /* doc configuration register start */ |
| 63 | + err = usbd_add_configuration(&app_usbd, USBD_SPEED_FS, |
| 64 | + &app_fs_config); |
| 65 | + if (err) { |
| 66 | + LOG_ERR("Failed to add Full-Speed configuration"); |
| 67 | + return NULL; |
| 68 | + } |
| 69 | + /* doc configuration register end */ |
| 70 | + |
| 71 | + /* doc functions register start */ |
| 72 | + err = usbd_register_all_classes(&app_usbd, USBD_SPEED_FS, 1, NULL); |
| 73 | + if (err) { |
| 74 | + LOG_ERR("Failed to add register classes"); |
| 75 | + return NULL; |
| 76 | + } |
| 77 | + /* doc functions register end */ |
| 78 | + |
| 79 | + /* Always use class code information from Interface Descriptors */ |
| 80 | + if (IS_ENABLED(CONFIG_USBD_CDC_ACM_CLASS) || |
| 81 | + IS_ENABLED(CONFIG_USBD_CDC_ECM_CLASS) || |
| 82 | + IS_ENABLED(CONFIG_USBD_CDC_NCM_CLASS) || |
| 83 | + IS_ENABLED(CONFIG_USBD_MIDI2_CLASS) || |
| 84 | + IS_ENABLED(CONFIG_USBD_AUDIO2_CLASS) || |
| 85 | + IS_ENABLED(CONFIG_USBD_VIDEO_CLASS)) { |
| 86 | + /* |
| 87 | + * Class with multiple interfaces have an Interface |
| 88 | + * Association Descriptor available, use an appropriate triple |
| 89 | + * to indicate it. |
| 90 | + */ |
| 91 | + usbd_device_set_code_triple(&app_usbd, USBD_SPEED_FS, |
| 92 | + USB_BCC_MISCELLANEOUS, 0x02, 0x01); |
| 93 | + } else { |
| 94 | + usbd_device_set_code_triple(&app_usbd, USBD_SPEED_FS, 0, 0, 0); |
| 95 | + } |
| 96 | + |
| 97 | + usbd_self_powered(&app_usbd, attributes & USB_SCD_SELF_POWERED); |
| 98 | + |
| 99 | + /* doc device init start */ |
| 100 | + err = usbd_init(&app_usbd); |
| 101 | + if (err) { |
| 102 | + LOG_ERR("Failed to initialize device support"); |
| 103 | + return NULL; |
| 104 | + } |
| 105 | + /* doc device init end */ |
| 106 | + |
| 107 | + return &app_usbd; |
| 108 | +} |
0 commit comments