|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdint.h> |
| 8 | + |
| 9 | +#include <zephyr/init.h> |
| 10 | +#include <zephyr/kernel.h> |
| 11 | +#include <zephyr/device.h> |
| 12 | +#include <zephyr/usb/usbd.h> |
| 13 | + |
| 14 | +#include <zephyr/logging/log.h> |
| 15 | +LOG_MODULE_REGISTER(cdc_acm_serial, LOG_LEVEL_DBG); |
| 16 | + |
| 17 | +/* |
| 18 | + * This is intended for use with cdc-acm-snippet or as a default serial backend |
| 19 | + * only in applications where no other USB features are required, configured, |
| 20 | + * and enabled. This code only registers the first CDC-ACM instance. |
| 21 | + */ |
| 22 | + |
| 23 | +USBD_DEVICE_DEFINE(cdc_acm_serial, |
| 24 | + DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)), |
| 25 | + CONFIG_CDC_ACM_SERIAL_VID, CONFIG_CDC_ACM_SERIAL_PID); |
| 26 | + |
| 27 | +USBD_DESC_LANG_DEFINE(cdc_acm_serial_lang); |
| 28 | +USBD_DESC_MANUFACTURER_DEFINE(cdc_acm_serial_mfr, CONFIG_CDC_ACM_SERIAL_MANUFACTURER_STRING); |
| 29 | +USBD_DESC_PRODUCT_DEFINE(cdc_acm_serial_product, CONFIG_CDC_ACM_SERIAL_PRODUCT_STRING); |
| 30 | +USBD_DESC_SERIAL_NUMBER_DEFINE(cdc_acm_serial_sn); |
| 31 | + |
| 32 | +USBD_DESC_CONFIG_DEFINE(fs_cfg_desc, "FS Configuration"); |
| 33 | +USBD_DESC_CONFIG_DEFINE(hs_cfg_desc, "HS Configuration"); |
| 34 | + |
| 35 | +static const uint8_t attributes = IS_ENABLED(CONFIG_CDC_ACM_SERIAL_SELF_POWERED) ? |
| 36 | + USB_SCD_SELF_POWERED : 0; |
| 37 | + |
| 38 | +USBD_CONFIGURATION_DEFINE(cdc_acm_serial_fs_config, |
| 39 | + attributes, |
| 40 | + CONFIG_CDC_ACM_SERIAL_MAX_POWER, &fs_cfg_desc); |
| 41 | + |
| 42 | +USBD_CONFIGURATION_DEFINE(cdc_acm_serial_hs_config, |
| 43 | + attributes, |
| 44 | + CONFIG_CDC_ACM_SERIAL_MAX_POWER, &hs_cfg_desc); |
| 45 | + |
| 46 | + |
| 47 | +static int register_cdc_acm_0(struct usbd_context *const uds_ctx, |
| 48 | + const enum usbd_speed speed) |
| 49 | +{ |
| 50 | + struct usbd_config_node *cfg_nd; |
| 51 | + int err; |
| 52 | + |
| 53 | + if (speed == USBD_SPEED_HS) { |
| 54 | + cfg_nd = &cdc_acm_serial_hs_config; |
| 55 | + } else { |
| 56 | + cfg_nd = &cdc_acm_serial_fs_config; |
| 57 | + } |
| 58 | + |
| 59 | + err = usbd_add_configuration(uds_ctx, speed, cfg_nd); |
| 60 | + if (err) { |
| 61 | + LOG_ERR("Failed to add configuration"); |
| 62 | + return err; |
| 63 | + } |
| 64 | + |
| 65 | + err = usbd_register_class(&cdc_acm_serial, "cdc_acm_0", speed, 1); |
| 66 | + if (err) { |
| 67 | + LOG_ERR("Failed to register classes"); |
| 68 | + return err; |
| 69 | + } |
| 70 | + |
| 71 | + return usbd_device_set_code_triple(uds_ctx, speed, |
| 72 | + USB_BCC_MISCELLANEOUS, 0x02, 0x01); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +static int cdc_acm_serial_init_device(void) |
| 77 | +{ |
| 78 | + int err; |
| 79 | + |
| 80 | + err = usbd_add_descriptor(&cdc_acm_serial, &cdc_acm_serial_lang); |
| 81 | + if (err) { |
| 82 | + LOG_ERR("Failed to initialize language descriptor (%d)", err); |
| 83 | + return err; |
| 84 | + } |
| 85 | + |
| 86 | + err = usbd_add_descriptor(&cdc_acm_serial, &cdc_acm_serial_mfr); |
| 87 | + if (err) { |
| 88 | + LOG_ERR("Failed to initialize manufacturer descriptor (%d)", err); |
| 89 | + return err; |
| 90 | + } |
| 91 | + |
| 92 | + err = usbd_add_descriptor(&cdc_acm_serial, &cdc_acm_serial_product); |
| 93 | + if (err) { |
| 94 | + LOG_ERR("Failed to initialize product descriptor (%d)", err); |
| 95 | + return err; |
| 96 | + } |
| 97 | + |
| 98 | + err = usbd_add_descriptor(&cdc_acm_serial, &cdc_acm_serial_sn); |
| 99 | + if (err) { |
| 100 | + LOG_ERR("Failed to initialize SN descriptor (%d)", err); |
| 101 | + return err; |
| 102 | + } |
| 103 | + |
| 104 | + if (usbd_caps_speed(&cdc_acm_serial) == USBD_SPEED_HS) { |
| 105 | + err = register_cdc_acm_0(&cdc_acm_serial, USBD_SPEED_HS); |
| 106 | + if (err) { |
| 107 | + return err; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + err = register_cdc_acm_0(&cdc_acm_serial, USBD_SPEED_FS); |
| 112 | + if (err) { |
| 113 | + return err; |
| 114 | + } |
| 115 | + |
| 116 | + err = usbd_init(&cdc_acm_serial); |
| 117 | + if (err) { |
| 118 | + LOG_ERR("Failed to initialize device support"); |
| 119 | + return err; |
| 120 | + } |
| 121 | + |
| 122 | + err = usbd_enable(&cdc_acm_serial); |
| 123 | + if (err) { |
| 124 | + LOG_ERR("Failed to enable device support"); |
| 125 | + return err; |
| 126 | + } |
| 127 | + |
| 128 | + return 0; |
| 129 | +} |
| 130 | + |
| 131 | +SYS_INIT(cdc_acm_serial_init_device, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); |
0 commit comments