diff --git a/Kconfig.rtt b/Kconfig.rtt index da302913..2d35b4ca 100644 --- a/Kconfig.rtt +++ b/Kconfig.rtt @@ -135,6 +135,11 @@ if RT_USING_CHERRYUSB prompt "Enable usb dfu device" default n + config RT_CHERRYUSB_DEVICE_CDC_ACM_CHARDEV + bool + prompt "Enable chardev for cdc acm device" + default n + choice prompt "Select usb device template" default RT_CHERRYUSB_DEVICE_TEMPLATE_NONE @@ -176,6 +181,8 @@ if RT_USING_CHERRYUSB bool "winusbv2_hid" config RT_CHERRYUSB_DEVICE_TEMPLATE_ADB bool "adb" + config RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM_CHARDEV + bool "cdc_acm_chardev" endchoice config CONFIG_USBDEV_MSC_BLOCK_DEV_NAME diff --git a/Kconfig.rttpkg b/Kconfig.rttpkg index 06b3ef97..63614327 100644 --- a/Kconfig.rttpkg +++ b/Kconfig.rttpkg @@ -134,6 +134,11 @@ if PKG_USING_CHERRYUSB prompt "Enable usb dfu device" default n + config PKG_CHERRYUSB_DEVICE_CDC_ACM_CHARDEV + bool + prompt "Enable chardev for cdc acm device" + default n + choice prompt "Select usb device template" default PKG_CHERRYUSB_DEVICE_TEMPLATE_NONE @@ -175,6 +180,8 @@ if PKG_USING_CHERRYUSB bool "winusbv2_hid" config PKG_CHERRYUSB_DEVICE_TEMPLATE_ADB bool "adb" + config PKG_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM_CHARDEV + bool "cdc_acm_chardev" endchoice config CONFIG_USBDEV_MSC_BLOCK_DEV_NAME diff --git a/SConscript b/SConscript index 3e8b07ca..3eb72443 100644 --- a/SConscript +++ b/SConscript @@ -131,12 +131,13 @@ if GetDepend(['PKG_CHERRYUSB_DEVICE']): src += Glob('class/adb/usbd_adb.c') src += Glob('platform/rtthread/usbd_adb_shell.c') + if GetDepend(['PKG_CHERRYUSB_DEVICE_CDC_ACM_CHARDEV']): + src += Glob('platform/rtthread/usbd_serial.c') + if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM']): src += Glob('demo/cdc_acm_template.c') - if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_MSC']): + if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_MSC']) or GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_MSC_BLKDEV']): src += Glob('demo/msc_ram_template.c') - if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_MSC_BLKDEV']): - src += Glob('platform/rtthread/usbd_msc_blkdev.c') if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_HID_MOUSE']): src += Glob('demo/hid_mouse_template.c') if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_HID_KEYBOARD']): @@ -167,6 +168,8 @@ if GetDepend(['PKG_CHERRYUSB_DEVICE']): src += Glob('demo/winusb2.0_hid_template.c') if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_ADB']): src += Glob('demo/adb/usbd_adb_template.c') + if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM_CHARDEV']): + src += Glob('demo/cdc_acm_rttchardev_template.c') # USB HOST if GetDepend(['PKG_CHERRYUSB_HOST']): diff --git a/demo/cdc_acm_rttchardev_template.c b/demo/cdc_acm_rttchardev_template.c new file mode 100644 index 00000000..f5fec3f0 --- /dev/null +++ b/demo/cdc_acm_rttchardev_template.c @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2025, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "usbd_core.h" +#include "usbd_cdc_acm.h" + +/*!< endpoint address */ +#define CDC_IN_EP 0x81 +#define CDC_OUT_EP 0x02 +#define CDC_INT_EP 0x83 + +#define USBD_VID 0xFFFF +#define USBD_PID 0xFFFF +#define USBD_MAX_POWER 100 +#define USBD_LANGID_STRING 1033 + +/*!< config descriptor size */ +#define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN) + +#ifdef CONFIG_USB_HS +#define CDC_MAX_MPS 512 +#else +#define CDC_MAX_MPS 64 +#endif + +#ifdef CONFIG_USBDEV_ADVANCE_DESC +static const uint8_t device_descriptor[] = { + USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01) +}; + +static const uint8_t config_descriptor[] = { + USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), + CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02) +}; + +static const uint8_t device_quality_descriptor[] = { + /////////////////////////////////////// + /// device qualifier descriptor + /////////////////////////////////////// + 0x0a, + USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x00, + 0x00, +}; + +static const char *string_descriptors[] = { + (const char[]){ 0x09, 0x04 }, /* Langid */ + "CherryUSB", /* Manufacturer */ + "CherryUSB CDC DEMO", /* Product */ + "2022123456", /* Serial Number */ +}; + +static const uint8_t *device_descriptor_callback(uint8_t speed) +{ + return device_descriptor; +} + +static const uint8_t *config_descriptor_callback(uint8_t speed) +{ + return config_descriptor; +} + +static const uint8_t *device_quality_descriptor_callback(uint8_t speed) +{ + return device_quality_descriptor; +} + +static const char *string_descriptor_callback(uint8_t speed, uint8_t index) +{ + if (index > 3) { + return NULL; + } + return string_descriptors[index]; +} + +const struct usb_descriptor cdc_descriptor = { + .device_descriptor_callback = device_descriptor_callback, + .config_descriptor_callback = config_descriptor_callback, + .device_quality_descriptor_callback = device_quality_descriptor_callback, + .string_descriptor_callback = string_descriptor_callback +}; +#else +/*!< global descriptor */ +static const uint8_t cdc_descriptor[] = { + USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01), + USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), + CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02), + /////////////////////////////////////// + /// string0 descriptor + /////////////////////////////////////// + USB_LANGID_INIT(USBD_LANGID_STRING), + /////////////////////////////////////// + /// string1 descriptor + /////////////////////////////////////// + 0x14, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + 'C', 0x00, /* wcChar0 */ + 'h', 0x00, /* wcChar1 */ + 'e', 0x00, /* wcChar2 */ + 'r', 0x00, /* wcChar3 */ + 'r', 0x00, /* wcChar4 */ + 'y', 0x00, /* wcChar5 */ + 'U', 0x00, /* wcChar6 */ + 'S', 0x00, /* wcChar7 */ + 'B', 0x00, /* wcChar8 */ + /////////////////////////////////////// + /// string2 descriptor + /////////////////////////////////////// + 0x26, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + 'C', 0x00, /* wcChar0 */ + 'h', 0x00, /* wcChar1 */ + 'e', 0x00, /* wcChar2 */ + 'r', 0x00, /* wcChar3 */ + 'r', 0x00, /* wcChar4 */ + 'y', 0x00, /* wcChar5 */ + 'U', 0x00, /* wcChar6 */ + 'S', 0x00, /* wcChar7 */ + 'B', 0x00, /* wcChar8 */ + ' ', 0x00, /* wcChar9 */ + 'C', 0x00, /* wcChar10 */ + 'D', 0x00, /* wcChar11 */ + 'C', 0x00, /* wcChar12 */ + ' ', 0x00, /* wcChar13 */ + 'D', 0x00, /* wcChar14 */ + 'E', 0x00, /* wcChar15 */ + 'M', 0x00, /* wcChar16 */ + 'O', 0x00, /* wcChar17 */ + /////////////////////////////////////// + /// string3 descriptor + /////////////////////////////////////// + 0x16, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + '2', 0x00, /* wcChar0 */ + '0', 0x00, /* wcChar1 */ + '2', 0x00, /* wcChar2 */ + '2', 0x00, /* wcChar3 */ + '1', 0x00, /* wcChar4 */ + '2', 0x00, /* wcChar5 */ + '3', 0x00, /* wcChar6 */ + '4', 0x00, /* wcChar7 */ + '5', 0x00, /* wcChar8 */ + '6', 0x00, /* wcChar9 */ +#ifdef CONFIG_USB_HS + /////////////////////////////////////// + /// device qualifier descriptor + /////////////////////////////////////// + 0x0a, + USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x00, + 0x00, +#endif + 0x00 +}; +#endif + +static void usbd_event_handler(uint8_t busid, uint8_t event) +{ + switch (event) { + case USBD_EVENT_RESET: + break; + case USBD_EVENT_CONNECTED: + break; + case USBD_EVENT_DISCONNECTED: + break; + case USBD_EVENT_RESUME: + break; + case USBD_EVENT_SUSPEND: + break; + case USBD_EVENT_CONFIGURED: + + break; + case USBD_EVENT_SET_REMOTE_WAKEUP: + break; + case USBD_EVENT_CLR_REMOTE_WAKEUP: + break; + + default: + break; + } +} + +extern void usbd_cdc_acm_serial_init(uint8_t busid, uint8_t in_ep, uint8_t out_ep); + +void cdc_acm_chardev_init(uint8_t busid, uintptr_t reg_base) +{ +#ifdef CONFIG_USBDEV_ADVANCE_DESC + usbd_desc_register(busid, &cdc_descriptor); +#else + usbd_desc_register(busid, cdc_descriptor); +#endif + usbd_cdc_acm_serial_init(busid, CDC_IN_EP, CDC_OUT_EP); + usbd_initialize(busid, reg_base, usbd_event_handler); +} \ No newline at end of file diff --git a/platform/rtthread/usbd_serial.c b/platform/rtthread/usbd_serial.c new file mode 100644 index 00000000..5ce62209 --- /dev/null +++ b/platform/rtthread/usbd_serial.c @@ -0,0 +1,272 @@ +/* + * Copyright (c) 2025, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include + +#include "usbd_core.h" +#include "usbd_cdc_acm.h" + +#define DEV_FORMAT_CDC_ACM "usb-acm%d" + +#ifndef CONFIG_USBDEV_MAX_CDC_ACM_CLASS +#define CONFIG_USBDEV_MAX_CDC_ACM_CLASS (4) +#endif + +#ifndef CONFIG_USBDEV_SERIAL_RX_BUFSIZE +#define CONFIG_USBDEV_SERIAL_RX_BUFSIZE (2048) +#endif + +struct usbd_serial { + struct rt_device parent; + uint8_t busid; + uint8_t in_ep; + uint8_t out_ep; + struct usbd_interface intf_ctrl; + struct usbd_interface intf_data; + usb_osal_sem_t tx_done; + uint8_t minor; + char name[32]; + struct rt_ringbuffer rx_rb; + rt_uint8_t rx_rb_buffer[CONFIG_USBDEV_SERIAL_RX_BUFSIZE]; +}; + +static uint32_t g_devinuse = 0; + +static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_usbd_serial_cdc_acm_rx_buf[CONFIG_USBDEV_MAX_CDC_ACM_CLASS][USB_ALIGN_UP(512, CONFIG_USB_ALIGN_SIZE)]; + +static struct usbd_serial g_usbd_serial_cdc_acm[CONFIG_USBDEV_MAX_CDC_ACM_CLASS]; + +static struct usbd_serial *usbd_serial_alloc(void) +{ + uint8_t devno; + struct usbd_serial *serial; + + for (devno = 0; devno < CONFIG_USBDEV_MAX_CDC_ACM_CLASS; devno++) { + if ((g_devinuse & (1U << devno)) == 0) { + g_devinuse |= (1U << devno); + + serial = &g_usbd_serial_cdc_acm[devno]; + memset(serial, 0, sizeof(struct usbd_serial)); + serial->minor = devno; + snprintf(serial->name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT_CDC_ACM, serial->minor); + return serial; + } + } + return NULL; +} + +static void usbd_serial_free(struct usbd_serial *serial) +{ + uint8_t devno = serial->minor; + + if (devno < 32) { + g_devinuse &= ~(1U << devno); + } + memset(serial, 0, sizeof(struct usbd_serial)); +} + +static rt_err_t usbd_serial_open(struct rt_device *dev, rt_uint16_t oflag) +{ + struct usbd_serial *serial; + + RT_ASSERT(dev != RT_NULL); + + serial = (struct usbd_serial *)dev; + + if (!usb_device_is_configured(serial->busid)) { + USB_LOG_ERR("USB device is not configured\n"); + return -RT_EPERM; + } + + usbd_ep_start_read(serial->busid, serial->out_ep, + g_usbd_serial_cdc_acm_rx_buf[serial->minor], + usbd_get_ep_mps(serial->busid, serial->out_ep)); + return RT_EOK; +} + +static rt_ssize_t usbd_serial_read(struct rt_device *dev, + rt_off_t pos, + void *buffer, + rt_size_t size) +{ + struct usbd_serial *serial; + + RT_ASSERT(dev != RT_NULL); + + serial = (struct usbd_serial *)dev; + + if (!usb_device_is_configured(serial->busid)) { + return -RT_EPERM; + } + + return rt_ringbuffer_get(&serial->rx_rb, (rt_uint8_t *)buffer, size); +} + +static rt_ssize_t usbd_serial_write(struct rt_device *dev, + rt_off_t pos, + const void *buffer, + rt_size_t size) +{ + struct usbd_serial *serial; + int ret = 0; + rt_uint8_t *align_buf; + + RT_ASSERT(dev != RT_NULL); + + serial = (struct usbd_serial *)dev; + + if (!usb_device_is_configured(serial->busid)) { + return -RT_EPERM; + } + align_buf = (rt_uint8_t *)buffer; + +#ifdef RT_USING_CACHE + if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { + align_buf = rt_malloc_align(size, CONFIG_USB_ALIGN_SIZE); + if (!align_buf) { + USB_LOG_ERR("serial get align buf failed\n"); + return 0; + } + + usb_memcpy(align_buf, buffer, size); + } +#endif + usb_osal_sem_reset(serial->tx_done); + usbd_ep_start_write(serial->busid, serial->in_ep, align_buf, size); + ret = usb_osal_sem_take(serial->tx_done, 3000); + if (ret < 0) { + USB_LOG_ERR("serial write timeout\n"); + ret = -RT_ETIMEOUT; + } else { + ret = size; + } + +#ifdef CONFIG_USB_DCACHE_ENABLE + if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { + rt_free_align(align_buf); + } +#endif + + return ret; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops usbd_serial_ops = { + NULL, + usbd_serial_open, + NULL, + usbd_serial_read, + usbd_serial_write, + NULL +}; +#endif + +rt_err_t usbd_serial_register(struct usbd_serial *serial, + void *data) +{ + rt_err_t ret; + struct rt_device *device; + RT_ASSERT(serial != RT_NULL); + + device = &(serial->parent); + + device->type = RT_Device_Class_Char; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + +#ifdef RT_USING_DEVICE_OPS + device->ops = &usbd_serial_ops; +#else + device->init = NULL; + device->open = usbd_serial_open; + device->close = NULL; + device->read = usbd_serial_read; + device->write = usbd_serial_write; + device->control = NULL; +#endif + device->user_data = data; + + /* register a character device */ + ret = rt_device_register(device, serial->name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE); + +#ifdef RT_USING_POSIX_DEVIO + /* set fops */ + device->fops = NULL; +#endif + rt_ringbuffer_init(&serial->rx_rb, serial->rx_rb_buffer, sizeof(serial->rx_rb_buffer)); + + return ret; +} + +void usbd_cdc_acm_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes) +{ + struct usbd_serial *serial; + + for (uint8_t devno = 0; devno < CONFIG_USBDEV_MAX_CDC_ACM_CLASS; devno++) { + serial = &g_usbd_serial_cdc_acm[devno]; + if (serial->out_ep == ep) { + rt_ringbuffer_put(&serial->rx_rb, g_usbd_serial_cdc_acm_rx_buf[serial->minor], nbytes); + break; + } + } +} + +void usbd_cdc_acm_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes) +{ + struct usbd_serial *serial; + + if ((nbytes % usbd_get_ep_mps(busid, ep)) == 0 && nbytes) { + /* send zlp */ + usbd_ep_start_write(busid, ep, NULL, 0); + } else { + for (uint8_t devno = 0; devno < CONFIG_USBDEV_MAX_CDC_ACM_CLASS; devno++) { + serial = &g_usbd_serial_cdc_acm[devno]; + if ((serial->in_ep == ep) && serial->tx_done) { + usb_osal_sem_give(serial->tx_done); + break; + } + } + } +} + +void usbd_cdc_acm_serial_init(uint8_t busid, uint8_t in_ep, uint8_t out_ep) +{ + struct usbd_serial *serial; + + struct usbd_endpoint cdc_out_ep = { + .ep_addr = out_ep, + .ep_cb = usbd_cdc_acm_bulk_out + }; + + struct usbd_endpoint cdc_in_ep = { + .ep_addr = in_ep, + .ep_cb = usbd_cdc_acm_bulk_in + }; + + serial = usbd_serial_alloc(); + if (serial == NULL) { + USB_LOG_ERR("No more serial device available\n"); + return; + } + + serial->busid = busid; + serial->in_ep = in_ep; + serial->out_ep = out_ep; + serial->tx_done = usb_osal_sem_create(0); + + usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &serial->intf_ctrl)); + usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &serial->intf_data)); + usbd_add_endpoint(busid, &cdc_out_ep); + usbd_add_endpoint(busid, &cdc_in_ep); + + if (usbd_serial_register(serial, NULL) != RT_EOK) { + USB_LOG_ERR("Failed to register serial device\n"); + usbd_serial_free(serial); + return; + } + + USB_LOG_INFO("USB CDC ACM Serial Device %s initialized\n", serial->name); +} \ No newline at end of file