/* * Copyright 2025 Google LLC * * SPDX-License-Identifier: Apache-2.0 */ #include #include #include #include #include #include #include #include #include LOG_MODULE_REGISTER(cdc_acm_bridge, LOG_LEVEL_INF); const struct device *const uart_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart); static struct usbd_context *sample_usbd; #define DEVICE_DT_GET_COMMA(node_id) DEVICE_DT_GET(node_id), const struct device *uart_bridges[] = { DT_FOREACH_STATUS_OKAY(zephyr_uart_bridge, DEVICE_DT_GET_COMMA) }; static void sample_msg_cb(struct usbd_context *const ctx, const struct usbd_msg *msg) { LOG_INF("USBD message: %s", usbd_msg_type_string(msg->type)); if (usbd_can_detect_vbus(ctx)) { if (msg->type == USBD_MSG_VBUS_READY) { if (usbd_enable(ctx)) { LOG_ERR("Failed to enable device support"); } } if (msg->type == USBD_MSG_VBUS_REMOVED) { if (usbd_disable(ctx)) { LOG_ERR("Failed to disable device support"); } } } if (msg->type == USBD_MSG_CDC_ACM_LINE_CODING || msg->type == USBD_MSG_CDC_ACM_CONTROL_LINE_STATE) { for (uint8_t i = 0; i < ARRAY_SIZE(uart_bridges); i++) { /* update all bridges, non valid combinations are * skipped automatically. */ uart_bridge_settings_update(msg->dev, uart_bridges[i]); } } } int main(void) { int err; sample_usbd = sample_usbd_init_device(sample_msg_cb); if (sample_usbd == NULL) { LOG_ERR("Failed to initialize USB device"); return -ENODEV; } if (!usbd_can_detect_vbus(sample_usbd)) { err = usbd_enable(sample_usbd); if (err) { LOG_ERR("Failed to enable device support"); return err; } } LOG_INF("USB device support enabled"); k_sleep(K_FOREVER); return 0; }