From 42f4e4276258df0d235839ab53a087ec4c1ca396 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Thu, 12 Mar 2026 22:13:22 +0800 Subject: refactor(platform/rtthread): add rt prefix for file name Signed-off-by: sakumisu <1203593632@qq.com> --- SConscript | 14 +- platform/rtthread/rt_usb_check.c | 35 +++ platform/rtthread/rt_usb_msh.c | 53 +++++ platform/rtthread/rt_usbd_adb.c | 156 ++++++++++++ platform/rtthread/rt_usbd_serial.c | 276 ++++++++++++++++++++++ platform/rtthread/rt_usbh_lwip.c | 469 +++++++++++++++++++++++++++++++++++++ platform/rtthread/rt_usbh_msc.c | 191 +++++++++++++++ platform/rtthread/rt_usbh_serial.c | 215 +++++++++++++++++ platform/rtthread/usb_check.c | 35 --- platform/rtthread/usb_msh.c | 53 ----- platform/rtthread/usbd_adb_shell.c | 156 ------------ platform/rtthread/usbd_serial.c | 276 ---------------------- platform/rtthread/usbh_dfs.c | 191 --------------- platform/rtthread/usbh_lwip.c | 469 ------------------------------------- platform/rtthread/usbh_rtserial.c | 215 ----------------- 15 files changed, 1402 insertions(+), 1402 deletions(-) create mode 100644 platform/rtthread/rt_usb_check.c create mode 100644 platform/rtthread/rt_usb_msh.c create mode 100644 platform/rtthread/rt_usbd_adb.c create mode 100644 platform/rtthread/rt_usbd_serial.c create mode 100644 platform/rtthread/rt_usbh_lwip.c create mode 100644 platform/rtthread/rt_usbh_msc.c create mode 100644 platform/rtthread/rt_usbh_serial.c delete mode 100644 platform/rtthread/usb_check.c delete mode 100644 platform/rtthread/usb_msh.c delete mode 100644 platform/rtthread/usbd_adb_shell.c delete mode 100644 platform/rtthread/usbd_serial.c delete mode 100644 platform/rtthread/usbh_dfs.c delete mode 100644 platform/rtthread/usbh_lwip.c delete mode 100644 platform/rtthread/usbh_rtserial.c diff --git a/SConscript b/SConscript index 17f80e9e..b92df75e 100644 --- a/SConscript +++ b/SConscript @@ -142,10 +142,10 @@ if GetDepend(['PKG_CHERRYUSB_DEVICE']): src += Glob('class/vendor/display/usbd_display.c') if GetDepend(['PKG_CHERRYUSB_DEVICE_ADB']): src += Glob('class/adb/usbd_adb.c') - src += Glob('platform/rtthread/usbd_adb_shell.c') + src += Glob('platform/rtthread/rt_usbd_adb.c') if GetDepend(['PKG_CHERRYUSB_DEVICE_CDC_ACM_CHARDEV']): - src += Glob('platform/rtthread/usbd_serial.c') + src += Glob('platform/rtthread/rt_usbd_serial.c') if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM']): src += Glob('demo/cdc_acm_template.c') @@ -335,20 +335,20 @@ if GetDepend(['PKG_CHERRYUSB_HOST']): or GetDepend(['PKG_CHERRYUSB_HOST_PL2303']) \ or GetDepend(['PKG_CHERRYUSB_HOST_GSM']): src += Glob('class/serial/usbh_serial.c') - src += Glob('platform/rtthread/usbh_rtserial.c') + src += Glob('platform/rtthread/rt_usbh_serial.c') if GetDepend('RT_USING_DFS') and GetDepend(['PKG_CHERRYUSB_HOST_MSC']): - src += Glob('platform/rtthread/usbh_dfs.c') + src += Glob('platform/rtthread/rt_usbh_msc.c') if GetDepend('PKG_CHERRYUSB_HOST_CDC_ECM') \ or GetDepend('PKG_CHERRYUSB_HOST_CDC_RNDIS') \ or GetDepend('PKG_CHERRYUSB_HOST_CDC_NCM') \ or GetDepend('PKG_CHERRYUSB_HOST_ASIX') \ or GetDepend('PKG_CHERRYUSB_HOST_RTL8152'): - src += Glob('platform/rtthread/usbh_lwip.c') + src += Glob('platform/rtthread/rt_usbh_lwip.c') -src += Glob('platform/rtthread/usb_msh.c') -src += Glob('platform/rtthread/usb_check.c') +src += Glob('platform/rtthread/rt_usb_msh.c') +src += Glob('platform/rtthread/rt_usb_check.c') group = DefineGroup('CherryUSB', src, depend = ['PKG_USING_CHERRYUSB'], LIBS = LIBS, LIBPATH=LIBPATH, CPPPATH = path, CPPDEFINES = CPPDEFINES) diff --git a/platform/rtthread/rt_usb_check.c b/platform/rtthread/rt_usb_check.c new file mode 100644 index 00000000..154d89f0 --- /dev/null +++ b/platform/rtthread/rt_usb_check.c @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 ~ 2025, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "rtthread.h" +#include "usb_config.h" + +#if defined(PKG_CHERRYUSB_HOST) || defined(RT_CHERRYUSB_HOST) + +#ifndef RT_USING_TIMER_SOFT +#error must enable RT_USING_TIMER_SOFT to support timer callback in thread +#endif + +#if RT_TIMER_THREAD_STACK_SIZE < 1024 +#error "RT_TIMER_THREAD_STACK_SIZE must be >= 1024" +#endif +#endif + +#if defined(ARCH_ARM_CORTEX_M7) || \ + defined(ARCH_ARM_CORTEX_A) || \ + defined(ARCH_RISCV64) || \ + defined(SOC_HPM6200) || defined(SOC_HPM6300) || defined(SOC_HPM6700) || defined(SOC_HPM6800) || \ + defined(SOC_HPM6E00) || defined(SOC_HPM6P00) || \ + defined(BSP_USING_BL61X) || defined(BSP_USING_BL808) +#ifndef RT_USING_CACHE +#error RT_USING_CACHE must be enabled in this chip +#endif +#endif + +#ifdef RT_USING_CACHE +#ifndef CONFIG_USB_DCACHE_ENABLE +#error CONFIG_USB_DCACHE_ENABLE must be enabled if you do not config nocache ram +#endif +#endif diff --git a/platform/rtthread/rt_usb_msh.c b/platform/rtthread/rt_usb_msh.c new file mode 100644 index 00000000..8dd61164 --- /dev/null +++ b/platform/rtthread/rt_usb_msh.c @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "rtthread.h" + +#if defined(PKG_CHERRYUSB_HOST) || defined(RT_CHERRYUSB_HOST) + +#include "usbh_core.h" + +int usbh_init(int argc, char **argv) +{ + uint8_t busid; + uint32_t reg_base; + + if (argc < 3) { + USB_LOG_ERR("please input correct command: usbh_init \r\n"); + return -1; + } + + busid = atoi(argv[1]); + reg_base = strtoll(argv[2], NULL, 16); + usbh_initialize(busid, reg_base, NULL); + + return 0; +} + +int usbh_deinit(int argc, char **argv) +{ + uint8_t busid; + + if (argc < 2) { + USB_LOG_ERR("please input correct command: usbh_deinit \r\n"); + return -1; + } + + busid = atoi(argv[1]); + usbh_deinitialize(busid); + + return 0; +} + +MSH_CMD_EXPORT(usbh_init, init usb host); +MSH_CMD_EXPORT(usbh_deinit, deinit usb host); +MSH_CMD_EXPORT(lsusb, ls usb devices); + +#ifdef CONFIG_USBHOST_SERIAL +#include "usbh_serial.h" +MSH_CMD_EXPORT(usbh_serial, usbh_serial test); +#endif + +#endif diff --git a/platform/rtthread/rt_usbd_adb.c b/platform/rtthread/rt_usbd_adb.c new file mode 100644 index 00000000..4ab7f59a --- /dev/null +++ b/platform/rtthread/rt_usbd_adb.c @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2025, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include + +#include "usbd_core.h" +#include "usbd_adb.h" + +#ifndef CONFIG_USBDEV_SHELL_RX_BUFSIZE +#define CONFIG_USBDEV_SHELL_RX_BUFSIZE (2048) +#endif + +struct usbd_adb_shell { + struct rt_device parent; + usb_osal_sem_t tx_done; + struct rt_ringbuffer rx_rb; + rt_uint8_t rx_rb_buffer[CONFIG_USBDEV_SHELL_RX_BUFSIZE]; +} g_usbd_adb_shell; + +void usbd_adb_notify_shell_read(uint8_t *data, uint32_t len) +{ + rt_ringbuffer_put(&g_usbd_adb_shell.rx_rb, data, len); + + if (g_usbd_adb_shell.parent.rx_indicate) { + g_usbd_adb_shell.parent.rx_indicate(&g_usbd_adb_shell.parent, len); + } +} + +void usbd_adb_notify_write_done(void) +{ + if (g_usbd_adb_shell.tx_done) { + usb_osal_sem_give(g_usbd_adb_shell.tx_done); + } +} + +static rt_err_t usbd_adb_shell_open(struct rt_device *dev, rt_uint16_t oflag) +{ + while (!usb_device_is_configured(0)) { + rt_thread_mdelay(10); + } + return RT_EOK; +} + +static rt_err_t usbd_adb_shell_close(struct rt_device *dev) +{ + if (g_usbd_adb_shell.tx_done) { + usb_osal_sem_give(g_usbd_adb_shell.tx_done); + } + + return RT_EOK; +} + +static rt_ssize_t usbd_adb_shell_read(struct rt_device *dev, + rt_off_t pos, + void *buffer, + rt_size_t size) +{ + return rt_ringbuffer_get(&g_usbd_adb_shell.rx_rb, (rt_uint8_t *)buffer, size); +} + +static rt_ssize_t usbd_adb_shell_write(struct rt_device *dev, + rt_off_t pos, + const void *buffer, + rt_size_t size) +{ + int ret = 0; + + RT_ASSERT(dev != RT_NULL); + + if (!usb_device_is_configured(0)) { + return size; + } + + if (usbd_adb_can_write() && size) { + usb_osal_sem_reset(g_usbd_adb_shell.tx_done); + usbd_abd_write(ADB_SHELL_LOALID, buffer, size); + usb_osal_sem_take(g_usbd_adb_shell.tx_done, 0xffffffff); + } + + return size; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops usbd_adb_shell_ops = { + NULL, + usbd_adb_shell_open, + usbd_adb_shell_close, + usbd_adb_shell_read, + usbd_adb_shell_write, + NULL +}; +#endif + +void usbd_adb_shell_init(uint8_t in_ep, uint8_t out_ep) +{ + rt_err_t ret; + struct rt_device *device; + + device = &(g_usbd_adb_shell.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_adb_shell_ops; +#else + device->init = NULL; + device->open = usbd_adb_shell_open; + device->close = usbd_adb_shell_close; + device->read = usbd_adb_shell_read; + device->write = usbd_adb_shell_write; + device->control = NULL; +#endif + device->user_data = NULL; + + /* register a character device */ + ret = rt_device_register(device, "adb-sh", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE); + +#ifdef RT_USING_POSIX_DEVIO + /* set fops */ + device->fops = NULL; +#endif + + g_usbd_adb_shell.tx_done = usb_osal_sem_create(0); + rt_ringbuffer_init(&g_usbd_adb_shell.rx_rb, g_usbd_adb_shell.rx_rb_buffer, sizeof(g_usbd_adb_shell.rx_rb_buffer)); +} + +static int adb_enter(int argc, char **argv) +{ + (void)argc; + (void)argv; + + finsh_set_device("adb-sh"); + rt_console_set_device("adb-sh"); + + return 0; +} +MSH_CMD_EXPORT(adb_enter, adb_enter); + +static int adb_exit(int argc, char **argv) +{ + (void)argc; + (void)argv; + + usbd_adb_close(ADB_SHELL_LOALID); + + finsh_set_device(RT_CONSOLE_DEVICE_NAME); + rt_console_set_device(RT_CONSOLE_DEVICE_NAME); + + return 0; +} +MSH_CMD_EXPORT(adb_exit, adb_exit); diff --git a/platform/rtthread/rt_usbd_serial.c b/platform/rtthread/rt_usbd_serial.c new file mode 100644 index 00000000..41fb01af --- /dev/null +++ b/platform/rtthread/rt_usbd_serial.c @@ -0,0 +1,276 @@ +/* + * 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]; + usb_ringbuffer_t rx_rb; + 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; + + while(!usb_device_is_configured(serial->busid)) { + rt_thread_mdelay(10); + } + + 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 usb_ringbuffer_read(&serial->rx_rb, (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; + + if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { + align_buf = rt_malloc_align(USB_ALIGN_UP(size, CONFIG_USB_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); + } + + 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; + } + + if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { + rt_free_align(align_buf); + } + + return ret; +} + +static 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) { + usb_ringbuffer_write(&serial->rx_rb, g_usbd_serial_cdc_acm_rx_buf[serial->minor], nbytes); + 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)); + + if (serial->parent.rx_indicate) { + serial->parent.rx_indicate(&serial->parent, nbytes); + } + break; + } + } +} + +static 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; + } + } + } +} + +#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 + +static 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_INT_RX | RT_DEVICE_FLAG_REMOVABLE); + +#ifdef RT_USING_POSIX_DEVIO + /* set fops */ + device->fops = NULL; +#endif + + return ret; +} + +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); + + usb_ringbuffer_init(&serial->rx_rb, serial->rx_rb_buffer, sizeof(serial->rx_rb_buffer)); + + 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); +} diff --git a/platform/rtthread/rt_usbh_lwip.c b/platform/rtthread/rt_usbh_lwip.c new file mode 100644 index 00000000..292e5e3c --- /dev/null +++ b/platform/rtthread/rt_usbh_lwip.c @@ -0,0 +1,469 @@ +/* + * Copyright (c) 2024, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "netif/etharp.h" +#include "lwip/netif.h" +#include "lwip/pbuf.h" +#include "lwip/tcpip.h" +#if LWIP_DHCP +#include "lwip/dhcp.h" +#include "lwip/prot/dhcp.h" +#endif + +#include +#include +#include + +#include "usbh_core.h" + +#include "lwip/opt.h" + +#ifndef RT_USING_LWIP212 +#error must enable RT_USING_LWIP212 +#endif + +#ifndef LWIP_NO_RX_THREAD +#warning suggest you to enable LWIP_NO_RX_THREAD, we do not use rtthread eth rx thread +#endif + +#ifndef LWIP_NO_TX_THREAD +#warning suggest you to enable LWIP_NO_TX_THREAD, we do not use rtthread eth tx thread +#endif + +#if LWIP_TCPIP_CORE_LOCKING_INPUT != 1 +#warning suggest you to set LWIP_TCPIP_CORE_LOCKING_INPUT to 1 for better performance, usb handles eth input with own thread +#endif + +#if LWIP_TCPIP_CORE_LOCKING != 1 +#error must set LWIP_TCPIP_CORE_LOCKING to 1 +#endif + +#if PBUF_POOL_BUFSIZE < 1600 +#error PBUF_POOL_BUFSIZE must be larger than 1600 +#endif + +#if RT_LWIP_TCPTHREAD_STACKSIZE < 2048 +#error RT_LWIP_TCPTHREAD_STACKSIZE must be >= 2048 +#endif + +#if !defined(CONFIG_USBHOST_PLATFORM_CDC_ECM) && \ + !defined(CONFIG_USBHOST_PLATFORM_CDC_RNDIS) && \ + !defined(CONFIG_USBHOST_PLATFORM_CDC_NCM) && \ + !defined(CONFIG_USBHOST_PLATFORM_ASIX) && \ + !defined(CONFIG_USBHOST_PLATFORM_RTL8152) +#error "Please enable at least one USB Ethernet platform in usb_config.h or Kconfig" +#endif + +// #define CONFIG_USBHOST_PLATFORM_CDC_ECM +// #define CONFIG_USBHOST_PLATFORM_CDC_RNDIS +// #define CONFIG_USBHOST_PLATFORM_CDC_NCM +// #define CONFIG_USBHOST_PLATFORM_ASIX +// #define CONFIG_USBHOST_PLATFORM_RTL8152 + +void usbh_lwip_eth_output_common(struct pbuf *p, uint8_t *buf) +{ + struct pbuf *q; + uint8_t *buffer; + + buffer = buf; + for (q = p; q != NULL; q = q->next) { + usb_memcpy(buffer, q->payload, q->len); + buffer += q->len; + } +} + +void usbh_lwip_eth_input_common(struct netif *netif, uint8_t *buf, uint32_t len) +{ +#if LWIP_TCPIP_CORE_LOCKING_INPUT + pbuf_type type = PBUF_REF; +#else + pbuf_type type = PBUF_POOL; +#endif + err_t err; + struct pbuf *p; + + p = pbuf_alloc(PBUF_RAW, len, type); + if (p != NULL) { +#if LWIP_TCPIP_CORE_LOCKING_INPUT + p->payload = buf; +#else + usb_memcpy(p->payload, buf, len); +#endif + err = netif->input(p, netif); + if (err != ERR_OK) { + pbuf_free(p); + } + } else { + USB_LOG_ERR("No memory to alloc pbuf\r\n"); + } +} + +#ifdef CONFIG_USBHOST_PLATFORM_CDC_ECM +#include "usbh_cdc_ecm.h" + +static struct eth_device g_cdc_ecm_dev; + +static rt_err_t rt_usbh_cdc_ecm_control(rt_device_t dev, int cmd, void *args) +{ + struct usbh_cdc_ecm *cdc_ecm_class = (struct usbh_cdc_ecm *)dev->user_data; + + switch (cmd) { + case NIOCTL_GADDR: + + /* get mac address */ + if (args) + rt_memcpy(args, cdc_ecm_class->mac, 6); + else + return -RT_ERROR; + + break; + + default: + break; + } + + return RT_EOK; +} + +static rt_err_t rt_usbh_cdc_ecm_eth_tx(rt_device_t dev, struct pbuf *p) +{ + int ret; + (void)dev; + + usbh_lwip_eth_output_common(p, usbh_cdc_ecm_get_eth_txbuf()); + ret = usbh_cdc_ecm_eth_output(p->tot_len); + if (ret < 0) { + return -RT_ERROR; + } else { + return RT_EOK; + } +} + +void usbh_cdc_ecm_eth_input(uint8_t *buf, uint32_t buflen) +{ + usbh_lwip_eth_input_common(g_cdc_ecm_dev.netif, buf, buflen); +} + +void usbh_cdc_ecm_run(struct usbh_cdc_ecm *cdc_ecm_class) +{ + memset(&g_cdc_ecm_dev, 0, sizeof(struct eth_device)); + + g_cdc_ecm_dev.parent.control = rt_usbh_cdc_ecm_control; + g_cdc_ecm_dev.eth_rx = NULL; + g_cdc_ecm_dev.eth_tx = rt_usbh_cdc_ecm_eth_tx; + g_cdc_ecm_dev.parent.user_data = cdc_ecm_class; + + eth_device_init(&g_cdc_ecm_dev, "u0"); + eth_device_linkchange(&g_cdc_ecm_dev, RT_TRUE); + + usb_osal_thread_create("usbh_cdc_ecm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ecm_rx_thread, NULL); +} + +void usbh_cdc_ecm_stop(struct usbh_cdc_ecm *cdc_ecm_class) +{ + (void)cdc_ecm_class; + + eth_device_deinit(&g_cdc_ecm_dev); +} +#endif + +#ifdef CONFIG_USBHOST_PLATFORM_CDC_RNDIS +#include "usbh_rndis.h" + +static struct eth_device g_rndis_dev; + +static rt_timer_t keep_timer = RT_NULL; + +static void rndis_dev_keepalive_timeout(void *parameter) +{ + struct usbh_rndis *rndis_class = (struct usbh_rndis *)parameter; + usbh_rndis_keepalive(rndis_class); +} + +static void timer_init(struct usbh_rndis *rndis_class) +{ + keep_timer = rt_timer_create("keep", + rndis_dev_keepalive_timeout, + rndis_class, + 5000, + RT_TIMER_FLAG_PERIODIC | + RT_TIMER_FLAG_SOFT_TIMER); + + rt_timer_start(keep_timer); +} + +static rt_err_t rt_usbh_rndis_control(rt_device_t dev, int cmd, void *args) +{ + struct usbh_rndis *rndis_class = (struct usbh_rndis *)dev->user_data; + + switch (cmd) { + case NIOCTL_GADDR: + + /* get mac address */ + if (args) + rt_memcpy(args, rndis_class->mac, 6); + else + return -RT_ERROR; + + break; + + default: + break; + } + + return RT_EOK; +} + +static rt_err_t rt_usbh_rndis_eth_tx(rt_device_t dev, struct pbuf *p) +{ + int ret; + (void)dev; + + usbh_lwip_eth_output_common(p, usbh_rndis_get_eth_txbuf()); + ret = usbh_rndis_eth_output(p->tot_len); + if (ret < 0) { + return -RT_ERROR; + } else { + return RT_EOK; + } +} + +void usbh_rndis_eth_input(uint8_t *buf, uint32_t buflen) +{ + usbh_lwip_eth_input_common(g_rndis_dev.netif, buf, buflen); +} + +void usbh_rndis_run(struct usbh_rndis *rndis_class) +{ + memset(&g_rndis_dev, 0, sizeof(struct eth_device)); + + g_rndis_dev.parent.control = rt_usbh_rndis_control; + g_rndis_dev.eth_rx = NULL; + g_rndis_dev.eth_tx = rt_usbh_rndis_eth_tx; + g_rndis_dev.parent.user_data = rndis_class; + + eth_device_init(&g_rndis_dev, "u2"); + eth_device_linkchange(&g_rndis_dev, RT_TRUE); + + usb_osal_thread_create("usbh_rndis_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rndis_rx_thread, NULL); + //timer_init(rndis_class); +} + +void usbh_rndis_stop(struct usbh_rndis *rndis_class) +{ + (void)rndis_class; + + eth_device_deinit(&g_rndis_dev); + // rt_timer_stop(keep_timer); + // rt_timer_delete(keep_timer); +} +#endif + +#ifdef CONFIG_USBHOST_PLATFORM_CDC_NCM +#include "usbh_cdc_ncm.h" + +static struct eth_device g_cdc_ncm_dev; + +static rt_err_t rt_usbh_cdc_ncm_control(rt_device_t dev, int cmd, void *args) +{ + struct usbh_cdc_ncm *cdc_ncm_class = (struct usbh_cdc_ncm *)dev->user_data; + + switch (cmd) { + case NIOCTL_GADDR: + + /* get mac address */ + if (args) + rt_memcpy(args, cdc_ncm_class->mac, 6); + else + return -RT_ERROR; + + break; + + default: + break; + } + + return RT_EOK; +} + +static rt_err_t rt_usbh_cdc_ncm_eth_tx(rt_device_t dev, struct pbuf *p) +{ + int ret; + (void)dev; + + usbh_lwip_eth_output_common(p, usbh_cdc_ncm_get_eth_txbuf()); + ret = usbh_cdc_ncm_eth_output(p->tot_len); + if (ret < 0) { + return -RT_ERROR; + } else { + return RT_EOK; + } +} + +void usbh_cdc_ncm_eth_input(uint8_t *buf, uint32_t buflen) +{ + usbh_lwip_eth_input_common(g_cdc_ncm_dev.netif, buf, buflen); +} + +void usbh_cdc_ncm_run(struct usbh_cdc_ncm *cdc_ncm_class) +{ + memset(&g_cdc_ncm_dev, 0, sizeof(struct eth_device)); + + g_cdc_ncm_dev.parent.control = rt_usbh_cdc_ncm_control; + g_cdc_ncm_dev.eth_rx = NULL; + g_cdc_ncm_dev.eth_tx = rt_usbh_cdc_ncm_eth_tx; + g_cdc_ncm_dev.parent.user_data = cdc_ncm_class; + + eth_device_init(&g_cdc_ncm_dev, "u1"); + eth_device_linkchange(&g_cdc_ncm_dev, RT_TRUE); + + usb_osal_thread_create("usbh_cdc_ncm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ncm_rx_thread, NULL); +} + +void usbh_cdc_ncm_stop(struct usbh_cdc_ncm *cdc_ncm_class) +{ + (void)cdc_ncm_class; + + eth_device_deinit(&g_cdc_ncm_dev); +} +#endif + +#ifdef CONFIG_USBHOST_PLATFORM_ASIX +#include "usbh_asix.h" + +static struct eth_device g_asix_dev; + +static rt_err_t rt_usbh_asix_control(rt_device_t dev, int cmd, void *args) +{ + struct usbh_asix *asix_class = (struct usbh_asix *)dev->user_data; + + switch (cmd) { + case NIOCTL_GADDR: + + /* get mac address */ + if (args) + rt_memcpy(args, asix_class->mac, 6); + else + return -RT_ERROR; + + break; + + default: + break; + } + + return RT_EOK; +} + +static rt_err_t rt_usbh_asix_eth_tx(rt_device_t dev, struct pbuf *p) +{ + int ret; + (void)dev; + + usbh_lwip_eth_output_common(p, usbh_asix_get_eth_txbuf()); + ret = usbh_asix_eth_output(p->tot_len); + if (ret < 0) { + return -RT_ERROR; + } else { + return RT_EOK; + } +} + +void usbh_asix_eth_input(uint8_t *buf, uint32_t buflen) +{ + usbh_lwip_eth_input_common(g_asix_dev.netif, buf, buflen); +} + +void usbh_asix_run(struct usbh_asix *asix_class) +{ + memset(&g_asix_dev, 0, sizeof(struct eth_device)); + + g_asix_dev.parent.control = rt_usbh_asix_control; + g_asix_dev.eth_rx = NULL; + g_asix_dev.eth_tx = rt_usbh_asix_eth_tx; + g_asix_dev.parent.user_data = asix_class; + + eth_device_init(&g_asix_dev, "u3"); + eth_device_linkchange(&g_asix_dev, RT_TRUE); + + usb_osal_thread_create("usbh_asix_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_asix_rx_thread, NULL); +} + +void usbh_asix_stop(struct usbh_asix *asix_class) +{ + (void)asix_class; + + eth_device_deinit(&g_asix_dev); +} +#endif + +#ifdef CONFIG_USBHOST_PLATFORM_RTL8152 +#include "usbh_rtl8152.h" + +static struct eth_device g_rtl8152_dev; + +static rt_err_t rt_usbh_rtl8152_control(rt_device_t dev, int cmd, void *args) +{ + struct usbh_rtl8152 *rtl8152_class = (struct usbh_rtl8152 *)dev->user_data; + + switch (cmd) { + case NIOCTL_GADDR: + + /* get mac address */ + if (args) + rt_memcpy(args, rtl8152_class->mac, 6); + else + return -RT_ERROR; + + break; + + default: + break; + } + + return RT_EOK; +} + +static rt_err_t rt_usbh_rtl8152_eth_tx(rt_device_t dev, struct pbuf *p) +{ + int ret; + (void)dev; + + usbh_lwip_eth_output_common(p, usbh_rtl8152_get_eth_txbuf()); + ret = usbh_rtl8152_eth_output(p->tot_len); + if (ret < 0) { + return -RT_ERROR; + } else { + return RT_EOK; + } +} + +void usbh_rtl8152_eth_input(uint8_t *buf, uint32_t buflen) +{ + usbh_lwip_eth_input_common(g_rtl8152_dev.netif, buf, buflen); +} + +void usbh_rtl8152_run(struct usbh_rtl8152 *rtl8152_class) +{ + memset(&g_rtl8152_dev, 0, sizeof(struct eth_device)); + + g_rtl8152_dev.parent.control = rt_usbh_rtl8152_control; + g_rtl8152_dev.eth_rx = NULL; + g_rtl8152_dev.eth_tx = rt_usbh_rtl8152_eth_tx; + g_rtl8152_dev.parent.user_data = rtl8152_class; + + eth_device_init(&g_rtl8152_dev, "u4"); + eth_device_linkchange(&g_rtl8152_dev, RT_TRUE); + + usb_osal_thread_create("usbh_rtl8152_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rtl8152_rx_thread, NULL); +} + +void usbh_rtl8152_stop(struct usbh_rtl8152 *rtl8152_class) +{ + (void)rtl8152_class; + + eth_device_deinit(&g_rtl8152_dev); +} +#endif diff --git a/platform/rtthread/rt_usbh_msc.c b/platform/rtthread/rt_usbh_msc.c new file mode 100644 index 00000000..77846665 --- /dev/null +++ b/platform/rtthread/rt_usbh_msc.c @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2024, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "usbh_core.h" +#include "usbh_msc.h" + +#include "rtthread.h" +#include + +#define DEV_FORMAT "/dev/sd%c" + +#ifndef RT_USING_DFS_ELMFAT +#error "RT_USING_DFS_ELMFAT must be enabled to use USB mass storage device" +#endif + +#ifndef CONFIG_USB_DFS_MOUNT_POINT +#define CONFIG_USB_DFS_MOUNT_POINT "/" +#endif + +static rt_err_t rt_udisk_init(rt_device_t dev) +{ + struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data; + + if (usbh_msc_scsi_init(msc_class) < 0) { + return -RT_ERROR; + } + + return RT_EOK; +} + +static rt_ssize_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void *buffer, + rt_size_t size) +{ + struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data; + int ret; + rt_uint8_t *align_buf; + + align_buf = (rt_uint8_t *)buffer; + + if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { + align_buf = rt_malloc_align(size * msc_class->blocksize, CONFIG_USB_ALIGN_SIZE); + if (!align_buf) { + rt_kprintf("msc get align buf failed\n"); + return 0; + } + } else { + } + + ret = usbh_msc_scsi_read10(msc_class, pos, (uint8_t *)align_buf, size); + if (ret < 0) { + rt_kprintf("usb mass_storage read failed\n"); + return 0; + } + + if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { + usb_memcpy(buffer, align_buf, size * msc_class->blocksize); + rt_free_align(align_buf); + } + + return size; +} + +static rt_ssize_t rt_udisk_write(rt_device_t dev, rt_off_t pos, const void *buffer, + rt_size_t size) +{ + struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data; + int ret; + rt_uint8_t *align_buf; + + align_buf = (rt_uint8_t *)buffer; + + if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { + align_buf = rt_malloc_align(size * msc_class->blocksize, CONFIG_USB_ALIGN_SIZE); + if (!align_buf) { + rt_kprintf("msc get align buf failed\n"); + return 0; + } + + usb_memcpy(align_buf, buffer, size * msc_class->blocksize); + } + + ret = usbh_msc_scsi_write10(msc_class, pos, (uint8_t *)align_buf, size); + if (ret < 0) { + rt_kprintf("usb mass_storage write failed\n"); + return 0; + } + + if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { + rt_free_align(align_buf); + } + + return size; +} + +static rt_err_t rt_udisk_control(rt_device_t dev, int cmd, void *args) +{ + /* check parameter */ + RT_ASSERT(dev != RT_NULL); + struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data; + + if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) { + struct rt_device_blk_geometry *geometry; + + geometry = (struct rt_device_blk_geometry *)args; + if (geometry == RT_NULL) + return -RT_ERROR; + + geometry->bytes_per_sector = msc_class->blocksize; + geometry->block_size = msc_class->blocksize; + geometry->sector_count = msc_class->blocknum; + } + + return RT_EOK; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops udisk_device_ops = { + rt_udisk_init, + RT_NULL, + RT_NULL, + rt_udisk_read, + rt_udisk_write, + rt_udisk_control +}; +#endif + +static void usbh_msc_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV) +{ + struct usbh_msc *msc_class = (struct usbh_msc *)CONFIG_USB_OSAL_THREAD_GET_ARGV; + char name[CONFIG_USBHOST_DEV_NAMELEN]; + char mount_point[CONFIG_USBHOST_DEV_NAMELEN]; + int ret; + + snprintf(name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar); + snprintf(mount_point, CONFIG_USBHOST_DEV_NAMELEN, CONFIG_USB_DFS_MOUNT_POINT, msc_class->sdchar); + + ret = dfs_mount(name, mount_point, "elm", 0, 0); + if (ret == 0) { + rt_kprintf("udisk: %s mount successfully\n", name); + } else { + rt_kprintf("udisk: %s mount failed, ret = %d\n", name, ret); + } + + usb_osal_thread_delete(NULL); +} + +void usbh_msc_run(struct usbh_msc *msc_class) +{ + struct rt_device *dev; + char name[CONFIG_USBHOST_DEV_NAMELEN]; + + dev = rt_malloc(sizeof(struct rt_device)); + memset(dev, 0, sizeof(struct rt_device)); + + snprintf(name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar); + + dev->type = RT_Device_Class_Block; +#ifdef RT_USING_DEVICE_OPS + dev->ops = &udisk_device_ops; +#else + dev->init = rt_udisk_init; + dev->read = rt_udisk_read; + dev->write = rt_udisk_write; + dev->control = rt_udisk_control; +#endif + dev->user_data = msc_class; + + rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); + + usb_osal_thread_create("usbh_msc", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_msc_thread, msc_class); +} + +void usbh_msc_stop(struct usbh_msc *msc_class) +{ + struct rt_device *dev; + + char name[CONFIG_USBHOST_DEV_NAMELEN]; + char mount_point[CONFIG_USBHOST_DEV_NAMELEN]; + + snprintf(name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar); + snprintf(mount_point, CONFIG_USBHOST_DEV_NAMELEN, CONFIG_USB_DFS_MOUNT_POINT, msc_class->sdchar); + + dfs_unmount(mount_point); + dev = rt_device_find(name); + if (dev) { + rt_device_unregister(dev); + rt_free(dev); + } +} diff --git a/platform/rtthread/rt_usbh_serial.c b/platform/rtthread/rt_usbh_serial.c new file mode 100644 index 00000000..ab16f325 --- /dev/null +++ b/platform/rtthread/rt_usbh_serial.c @@ -0,0 +1,215 @@ +/* + * Copyright (c) 2025, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include + +#include "usbh_core.h" +#include "usbh_serial.h" + +static rt_err_t rt_usbh_serial_open(struct rt_device *dev, rt_uint16_t oflag) +{ + struct usbh_serial *serial; + + RT_ASSERT(dev != RT_NULL && dev->user_data != RT_NULL); + + serial = (struct usbh_serial *)dev->user_data; + + serial = usbh_serial_open(serial->hport->config.intf[serial->intf].devname, USBH_SERIAL_O_RDWR | USBH_SERIAL_O_NONBLOCK); + if (serial == RT_NULL) { + USB_LOG_ERR("serial open failed\n"); + return -RT_ERROR; + } + + struct usbh_serial_termios termios; + + memset(&termios, 0, sizeof(termios)); + termios.baudrate = 115200; + termios.stopbits = 0; + termios.parity = 0; + termios.databits = 8; + termios.rtscts = false; + termios.rx_timeout = 0; + + usbh_serial_control(serial, USBH_SERIAL_CMD_SET_ATTR, &termios); + + return RT_EOK; +} + +static rt_err_t rt_usbh_serial_close(struct rt_device *dev) +{ + struct usbh_serial *serial; + + RT_ASSERT(dev != RT_NULL && dev->user_data != RT_NULL); + + serial = (struct usbh_serial *)dev->user_data; + + usbh_serial_close(serial); + + return RT_EOK; +} + +static rt_ssize_t rt_usbh_serial_read(struct rt_device *dev, + rt_off_t pos, + void *buffer, + rt_size_t size) +{ + struct usbh_serial *serial; + + RT_ASSERT(dev != RT_NULL && dev->user_data != RT_NULL); + + serial = (struct usbh_serial *)dev->user_data; + + return usbh_serial_read(serial, buffer, size); +} + +static rt_ssize_t rt_usbh_serial_write(struct rt_device *dev, + rt_off_t pos, + const void *buffer, + rt_size_t size) +{ + struct usbh_serial *serial; + int ret = 0; + rt_uint8_t *align_buf; + + RT_ASSERT(dev != RT_NULL && dev->user_data != RT_NULL); + + serial = (struct usbh_serial *)dev->user_data; + + align_buf = (rt_uint8_t *)buffer; + + if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { + align_buf = rt_malloc_align(USB_ALIGN_UP(size, CONFIG_USB_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); + } + + ret = usbh_serial_write(serial, align_buf, size); + + if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { + rt_free_align(align_buf); + } + + return ret; +} + +static rt_err_t rt_usbh_serial_control(struct rt_device *dev, + int cmd, + void *args) +{ + struct usbh_serial *serial; + struct serial_configure *config; + int ret = -RT_EINVAL; + + RT_ASSERT(dev != RT_NULL && dev->user_data != RT_NULL); + + serial = (struct usbh_serial *)dev->user_data; + + switch (cmd) { + case RT_DEVICE_CTRL_CONFIG: { + config = (struct serial_configure *)args; + + struct usbh_serial_termios termios; + + memset(&termios, 0, sizeof(termios)); + termios.baudrate = config->baud_rate; + termios.stopbits = 0; + termios.parity = config->parity; + termios.databits = config->data_bits; + termios.rtscts = false; + termios.rx_timeout = 0; + + usbh_serial_control(serial, USBH_SERIAL_CMD_SET_ATTR, &termios); + } break; + + default: + break; + } + + return ret; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops usbh_serial_ops = { + NULL, + rt_usbh_serial_open, + rt_usbh_serial_close, + rt_usbh_serial_read, + rt_usbh_serial_write, + rt_usbh_serial_control +}; +#endif + +rt_err_t usbh_serial_register(struct usbh_serial *serial) +{ + rt_err_t ret; + struct rt_device *device; + RT_ASSERT(serial != RT_NULL); + + device = rt_malloc(sizeof(struct rt_device)); + if (device == RT_NULL) { + USB_LOG_ERR("serial device malloc failed\n"); + return -RT_ENOMEM; + } + memset(device, 0, sizeof(struct rt_device)); + + device->type = RT_Device_Class_Char; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + +#ifdef RT_USING_DEVICE_OPS + device->ops = &usbh_serial_ops; +#else + device->init = NULL; + device->open = rt_usbh_serial_open; + device->close = rt_usbh_serial_close; + device->read = rt_usbh_serial_read; + device->write = rt_usbh_serial_write; + device->control = rt_usbh_serial_control; +#endif + device->user_data = serial; + serial->user_data = device; + + /* skip /dev/ to avoid BAD file */ + const char *dev_name = serial->hport->config.intf[serial->intf].devname; + if (strncmp(dev_name, "/dev/", 5) == 0) { + dev_name += 5; + } + + /* register a character device */ + ret = rt_device_register(device, dev_name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_REMOVABLE); + +#ifdef RT_USING_POSIX_DEVIO + /* set fops */ + device->fops = &usbh_serial_fops; +#endif + return ret; +} + +void usbh_serial_unregister(struct usbh_serial *serial) +{ + struct rt_device *device; + + RT_ASSERT(serial != NULL && serial->user_data != NULL); + + device = (struct rt_device *)serial->user_data; + + rt_device_unregister(device); + rt_free(device); +} + +void usbh_serial_run(struct usbh_serial *serial) +{ + usbh_serial_register(serial); +} + +void usbh_serial_stop(struct usbh_serial *serial) +{ + usbh_serial_unregister(serial); +} \ No newline at end of file diff --git a/platform/rtthread/usb_check.c b/platform/rtthread/usb_check.c deleted file mode 100644 index 154d89f0..00000000 --- a/platform/rtthread/usb_check.c +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2022 ~ 2025, sakumisu - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "rtthread.h" -#include "usb_config.h" - -#if defined(PKG_CHERRYUSB_HOST) || defined(RT_CHERRYUSB_HOST) - -#ifndef RT_USING_TIMER_SOFT -#error must enable RT_USING_TIMER_SOFT to support timer callback in thread -#endif - -#if RT_TIMER_THREAD_STACK_SIZE < 1024 -#error "RT_TIMER_THREAD_STACK_SIZE must be >= 1024" -#endif -#endif - -#if defined(ARCH_ARM_CORTEX_M7) || \ - defined(ARCH_ARM_CORTEX_A) || \ - defined(ARCH_RISCV64) || \ - defined(SOC_HPM6200) || defined(SOC_HPM6300) || defined(SOC_HPM6700) || defined(SOC_HPM6800) || \ - defined(SOC_HPM6E00) || defined(SOC_HPM6P00) || \ - defined(BSP_USING_BL61X) || defined(BSP_USING_BL808) -#ifndef RT_USING_CACHE -#error RT_USING_CACHE must be enabled in this chip -#endif -#endif - -#ifdef RT_USING_CACHE -#ifndef CONFIG_USB_DCACHE_ENABLE -#error CONFIG_USB_DCACHE_ENABLE must be enabled if you do not config nocache ram -#endif -#endif diff --git a/platform/rtthread/usb_msh.c b/platform/rtthread/usb_msh.c deleted file mode 100644 index 8dd61164..00000000 --- a/platform/rtthread/usb_msh.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2024, sakumisu - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "rtthread.h" - -#if defined(PKG_CHERRYUSB_HOST) || defined(RT_CHERRYUSB_HOST) - -#include "usbh_core.h" - -int usbh_init(int argc, char **argv) -{ - uint8_t busid; - uint32_t reg_base; - - if (argc < 3) { - USB_LOG_ERR("please input correct command: usbh_init \r\n"); - return -1; - } - - busid = atoi(argv[1]); - reg_base = strtoll(argv[2], NULL, 16); - usbh_initialize(busid, reg_base, NULL); - - return 0; -} - -int usbh_deinit(int argc, char **argv) -{ - uint8_t busid; - - if (argc < 2) { - USB_LOG_ERR("please input correct command: usbh_deinit \r\n"); - return -1; - } - - busid = atoi(argv[1]); - usbh_deinitialize(busid); - - return 0; -} - -MSH_CMD_EXPORT(usbh_init, init usb host); -MSH_CMD_EXPORT(usbh_deinit, deinit usb host); -MSH_CMD_EXPORT(lsusb, ls usb devices); - -#ifdef CONFIG_USBHOST_SERIAL -#include "usbh_serial.h" -MSH_CMD_EXPORT(usbh_serial, usbh_serial test); -#endif - -#endif diff --git a/platform/rtthread/usbd_adb_shell.c b/platform/rtthread/usbd_adb_shell.c deleted file mode 100644 index 4ab7f59a..00000000 --- a/platform/rtthread/usbd_adb_shell.c +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright (c) 2025, sakumisu - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include -#include - -#include "usbd_core.h" -#include "usbd_adb.h" - -#ifndef CONFIG_USBDEV_SHELL_RX_BUFSIZE -#define CONFIG_USBDEV_SHELL_RX_BUFSIZE (2048) -#endif - -struct usbd_adb_shell { - struct rt_device parent; - usb_osal_sem_t tx_done; - struct rt_ringbuffer rx_rb; - rt_uint8_t rx_rb_buffer[CONFIG_USBDEV_SHELL_RX_BUFSIZE]; -} g_usbd_adb_shell; - -void usbd_adb_notify_shell_read(uint8_t *data, uint32_t len) -{ - rt_ringbuffer_put(&g_usbd_adb_shell.rx_rb, data, len); - - if (g_usbd_adb_shell.parent.rx_indicate) { - g_usbd_adb_shell.parent.rx_indicate(&g_usbd_adb_shell.parent, len); - } -} - -void usbd_adb_notify_write_done(void) -{ - if (g_usbd_adb_shell.tx_done) { - usb_osal_sem_give(g_usbd_adb_shell.tx_done); - } -} - -static rt_err_t usbd_adb_shell_open(struct rt_device *dev, rt_uint16_t oflag) -{ - while (!usb_device_is_configured(0)) { - rt_thread_mdelay(10); - } - return RT_EOK; -} - -static rt_err_t usbd_adb_shell_close(struct rt_device *dev) -{ - if (g_usbd_adb_shell.tx_done) { - usb_osal_sem_give(g_usbd_adb_shell.tx_done); - } - - return RT_EOK; -} - -static rt_ssize_t usbd_adb_shell_read(struct rt_device *dev, - rt_off_t pos, - void *buffer, - rt_size_t size) -{ - return rt_ringbuffer_get(&g_usbd_adb_shell.rx_rb, (rt_uint8_t *)buffer, size); -} - -static rt_ssize_t usbd_adb_shell_write(struct rt_device *dev, - rt_off_t pos, - const void *buffer, - rt_size_t size) -{ - int ret = 0; - - RT_ASSERT(dev != RT_NULL); - - if (!usb_device_is_configured(0)) { - return size; - } - - if (usbd_adb_can_write() && size) { - usb_osal_sem_reset(g_usbd_adb_shell.tx_done); - usbd_abd_write(ADB_SHELL_LOALID, buffer, size); - usb_osal_sem_take(g_usbd_adb_shell.tx_done, 0xffffffff); - } - - return size; -} - -#ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops usbd_adb_shell_ops = { - NULL, - usbd_adb_shell_open, - usbd_adb_shell_close, - usbd_adb_shell_read, - usbd_adb_shell_write, - NULL -}; -#endif - -void usbd_adb_shell_init(uint8_t in_ep, uint8_t out_ep) -{ - rt_err_t ret; - struct rt_device *device; - - device = &(g_usbd_adb_shell.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_adb_shell_ops; -#else - device->init = NULL; - device->open = usbd_adb_shell_open; - device->close = usbd_adb_shell_close; - device->read = usbd_adb_shell_read; - device->write = usbd_adb_shell_write; - device->control = NULL; -#endif - device->user_data = NULL; - - /* register a character device */ - ret = rt_device_register(device, "adb-sh", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE); - -#ifdef RT_USING_POSIX_DEVIO - /* set fops */ - device->fops = NULL; -#endif - - g_usbd_adb_shell.tx_done = usb_osal_sem_create(0); - rt_ringbuffer_init(&g_usbd_adb_shell.rx_rb, g_usbd_adb_shell.rx_rb_buffer, sizeof(g_usbd_adb_shell.rx_rb_buffer)); -} - -static int adb_enter(int argc, char **argv) -{ - (void)argc; - (void)argv; - - finsh_set_device("adb-sh"); - rt_console_set_device("adb-sh"); - - return 0; -} -MSH_CMD_EXPORT(adb_enter, adb_enter); - -static int adb_exit(int argc, char **argv) -{ - (void)argc; - (void)argv; - - usbd_adb_close(ADB_SHELL_LOALID); - - finsh_set_device(RT_CONSOLE_DEVICE_NAME); - rt_console_set_device(RT_CONSOLE_DEVICE_NAME); - - return 0; -} -MSH_CMD_EXPORT(adb_exit, adb_exit); diff --git a/platform/rtthread/usbd_serial.c b/platform/rtthread/usbd_serial.c deleted file mode 100644 index 41fb01af..00000000 --- a/platform/rtthread/usbd_serial.c +++ /dev/null @@ -1,276 +0,0 @@ -/* - * 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]; - usb_ringbuffer_t rx_rb; - 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; - - while(!usb_device_is_configured(serial->busid)) { - rt_thread_mdelay(10); - } - - 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 usb_ringbuffer_read(&serial->rx_rb, (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; - - if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { - align_buf = rt_malloc_align(USB_ALIGN_UP(size, CONFIG_USB_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); - } - - 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; - } - - if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { - rt_free_align(align_buf); - } - - return ret; -} - -static 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) { - usb_ringbuffer_write(&serial->rx_rb, g_usbd_serial_cdc_acm_rx_buf[serial->minor], nbytes); - 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)); - - if (serial->parent.rx_indicate) { - serial->parent.rx_indicate(&serial->parent, nbytes); - } - break; - } - } -} - -static 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; - } - } - } -} - -#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 - -static 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_INT_RX | RT_DEVICE_FLAG_REMOVABLE); - -#ifdef RT_USING_POSIX_DEVIO - /* set fops */ - device->fops = NULL; -#endif - - return ret; -} - -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); - - usb_ringbuffer_init(&serial->rx_rb, serial->rx_rb_buffer, sizeof(serial->rx_rb_buffer)); - - 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); -} diff --git a/platform/rtthread/usbh_dfs.c b/platform/rtthread/usbh_dfs.c deleted file mode 100644 index 77846665..00000000 --- a/platform/rtthread/usbh_dfs.c +++ /dev/null @@ -1,191 +0,0 @@ -/* - * Copyright (c) 2024, sakumisu - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "usbh_core.h" -#include "usbh_msc.h" - -#include "rtthread.h" -#include - -#define DEV_FORMAT "/dev/sd%c" - -#ifndef RT_USING_DFS_ELMFAT -#error "RT_USING_DFS_ELMFAT must be enabled to use USB mass storage device" -#endif - -#ifndef CONFIG_USB_DFS_MOUNT_POINT -#define CONFIG_USB_DFS_MOUNT_POINT "/" -#endif - -static rt_err_t rt_udisk_init(rt_device_t dev) -{ - struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data; - - if (usbh_msc_scsi_init(msc_class) < 0) { - return -RT_ERROR; - } - - return RT_EOK; -} - -static rt_ssize_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void *buffer, - rt_size_t size) -{ - struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data; - int ret; - rt_uint8_t *align_buf; - - align_buf = (rt_uint8_t *)buffer; - - if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { - align_buf = rt_malloc_align(size * msc_class->blocksize, CONFIG_USB_ALIGN_SIZE); - if (!align_buf) { - rt_kprintf("msc get align buf failed\n"); - return 0; - } - } else { - } - - ret = usbh_msc_scsi_read10(msc_class, pos, (uint8_t *)align_buf, size); - if (ret < 0) { - rt_kprintf("usb mass_storage read failed\n"); - return 0; - } - - if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { - usb_memcpy(buffer, align_buf, size * msc_class->blocksize); - rt_free_align(align_buf); - } - - return size; -} - -static rt_ssize_t rt_udisk_write(rt_device_t dev, rt_off_t pos, const void *buffer, - rt_size_t size) -{ - struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data; - int ret; - rt_uint8_t *align_buf; - - align_buf = (rt_uint8_t *)buffer; - - if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { - align_buf = rt_malloc_align(size * msc_class->blocksize, CONFIG_USB_ALIGN_SIZE); - if (!align_buf) { - rt_kprintf("msc get align buf failed\n"); - return 0; - } - - usb_memcpy(align_buf, buffer, size * msc_class->blocksize); - } - - ret = usbh_msc_scsi_write10(msc_class, pos, (uint8_t *)align_buf, size); - if (ret < 0) { - rt_kprintf("usb mass_storage write failed\n"); - return 0; - } - - if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { - rt_free_align(align_buf); - } - - return size; -} - -static rt_err_t rt_udisk_control(rt_device_t dev, int cmd, void *args) -{ - /* check parameter */ - RT_ASSERT(dev != RT_NULL); - struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data; - - if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) { - struct rt_device_blk_geometry *geometry; - - geometry = (struct rt_device_blk_geometry *)args; - if (geometry == RT_NULL) - return -RT_ERROR; - - geometry->bytes_per_sector = msc_class->blocksize; - geometry->block_size = msc_class->blocksize; - geometry->sector_count = msc_class->blocknum; - } - - return RT_EOK; -} - -#ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops udisk_device_ops = { - rt_udisk_init, - RT_NULL, - RT_NULL, - rt_udisk_read, - rt_udisk_write, - rt_udisk_control -}; -#endif - -static void usbh_msc_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV) -{ - struct usbh_msc *msc_class = (struct usbh_msc *)CONFIG_USB_OSAL_THREAD_GET_ARGV; - char name[CONFIG_USBHOST_DEV_NAMELEN]; - char mount_point[CONFIG_USBHOST_DEV_NAMELEN]; - int ret; - - snprintf(name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar); - snprintf(mount_point, CONFIG_USBHOST_DEV_NAMELEN, CONFIG_USB_DFS_MOUNT_POINT, msc_class->sdchar); - - ret = dfs_mount(name, mount_point, "elm", 0, 0); - if (ret == 0) { - rt_kprintf("udisk: %s mount successfully\n", name); - } else { - rt_kprintf("udisk: %s mount failed, ret = %d\n", name, ret); - } - - usb_osal_thread_delete(NULL); -} - -void usbh_msc_run(struct usbh_msc *msc_class) -{ - struct rt_device *dev; - char name[CONFIG_USBHOST_DEV_NAMELEN]; - - dev = rt_malloc(sizeof(struct rt_device)); - memset(dev, 0, sizeof(struct rt_device)); - - snprintf(name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar); - - dev->type = RT_Device_Class_Block; -#ifdef RT_USING_DEVICE_OPS - dev->ops = &udisk_device_ops; -#else - dev->init = rt_udisk_init; - dev->read = rt_udisk_read; - dev->write = rt_udisk_write; - dev->control = rt_udisk_control; -#endif - dev->user_data = msc_class; - - rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); - - usb_osal_thread_create("usbh_msc", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_msc_thread, msc_class); -} - -void usbh_msc_stop(struct usbh_msc *msc_class) -{ - struct rt_device *dev; - - char name[CONFIG_USBHOST_DEV_NAMELEN]; - char mount_point[CONFIG_USBHOST_DEV_NAMELEN]; - - snprintf(name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar); - snprintf(mount_point, CONFIG_USBHOST_DEV_NAMELEN, CONFIG_USB_DFS_MOUNT_POINT, msc_class->sdchar); - - dfs_unmount(mount_point); - dev = rt_device_find(name); - if (dev) { - rt_device_unregister(dev); - rt_free(dev); - } -} diff --git a/platform/rtthread/usbh_lwip.c b/platform/rtthread/usbh_lwip.c deleted file mode 100644 index 292e5e3c..00000000 --- a/platform/rtthread/usbh_lwip.c +++ /dev/null @@ -1,469 +0,0 @@ -/* - * Copyright (c) 2024, sakumisu - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "netif/etharp.h" -#include "lwip/netif.h" -#include "lwip/pbuf.h" -#include "lwip/tcpip.h" -#if LWIP_DHCP -#include "lwip/dhcp.h" -#include "lwip/prot/dhcp.h" -#endif - -#include -#include -#include - -#include "usbh_core.h" - -#include "lwip/opt.h" - -#ifndef RT_USING_LWIP212 -#error must enable RT_USING_LWIP212 -#endif - -#ifndef LWIP_NO_RX_THREAD -#warning suggest you to enable LWIP_NO_RX_THREAD, we do not use rtthread eth rx thread -#endif - -#ifndef LWIP_NO_TX_THREAD -#warning suggest you to enable LWIP_NO_TX_THREAD, we do not use rtthread eth tx thread -#endif - -#if LWIP_TCPIP_CORE_LOCKING_INPUT != 1 -#warning suggest you to set LWIP_TCPIP_CORE_LOCKING_INPUT to 1 for better performance, usb handles eth input with own thread -#endif - -#if LWIP_TCPIP_CORE_LOCKING != 1 -#error must set LWIP_TCPIP_CORE_LOCKING to 1 -#endif - -#if PBUF_POOL_BUFSIZE < 1600 -#error PBUF_POOL_BUFSIZE must be larger than 1600 -#endif - -#if RT_LWIP_TCPTHREAD_STACKSIZE < 2048 -#error RT_LWIP_TCPTHREAD_STACKSIZE must be >= 2048 -#endif - -#if !defined(CONFIG_USBHOST_PLATFORM_CDC_ECM) && \ - !defined(CONFIG_USBHOST_PLATFORM_CDC_RNDIS) && \ - !defined(CONFIG_USBHOST_PLATFORM_CDC_NCM) && \ - !defined(CONFIG_USBHOST_PLATFORM_ASIX) && \ - !defined(CONFIG_USBHOST_PLATFORM_RTL8152) -#error "Please enable at least one USB Ethernet platform in usb_config.h or Kconfig" -#endif - -// #define CONFIG_USBHOST_PLATFORM_CDC_ECM -// #define CONFIG_USBHOST_PLATFORM_CDC_RNDIS -// #define CONFIG_USBHOST_PLATFORM_CDC_NCM -// #define CONFIG_USBHOST_PLATFORM_ASIX -// #define CONFIG_USBHOST_PLATFORM_RTL8152 - -void usbh_lwip_eth_output_common(struct pbuf *p, uint8_t *buf) -{ - struct pbuf *q; - uint8_t *buffer; - - buffer = buf; - for (q = p; q != NULL; q = q->next) { - usb_memcpy(buffer, q->payload, q->len); - buffer += q->len; - } -} - -void usbh_lwip_eth_input_common(struct netif *netif, uint8_t *buf, uint32_t len) -{ -#if LWIP_TCPIP_CORE_LOCKING_INPUT - pbuf_type type = PBUF_REF; -#else - pbuf_type type = PBUF_POOL; -#endif - err_t err; - struct pbuf *p; - - p = pbuf_alloc(PBUF_RAW, len, type); - if (p != NULL) { -#if LWIP_TCPIP_CORE_LOCKING_INPUT - p->payload = buf; -#else - usb_memcpy(p->payload, buf, len); -#endif - err = netif->input(p, netif); - if (err != ERR_OK) { - pbuf_free(p); - } - } else { - USB_LOG_ERR("No memory to alloc pbuf\r\n"); - } -} - -#ifdef CONFIG_USBHOST_PLATFORM_CDC_ECM -#include "usbh_cdc_ecm.h" - -static struct eth_device g_cdc_ecm_dev; - -static rt_err_t rt_usbh_cdc_ecm_control(rt_device_t dev, int cmd, void *args) -{ - struct usbh_cdc_ecm *cdc_ecm_class = (struct usbh_cdc_ecm *)dev->user_data; - - switch (cmd) { - case NIOCTL_GADDR: - - /* get mac address */ - if (args) - rt_memcpy(args, cdc_ecm_class->mac, 6); - else - return -RT_ERROR; - - break; - - default: - break; - } - - return RT_EOK; -} - -static rt_err_t rt_usbh_cdc_ecm_eth_tx(rt_device_t dev, struct pbuf *p) -{ - int ret; - (void)dev; - - usbh_lwip_eth_output_common(p, usbh_cdc_ecm_get_eth_txbuf()); - ret = usbh_cdc_ecm_eth_output(p->tot_len); - if (ret < 0) { - return -RT_ERROR; - } else { - return RT_EOK; - } -} - -void usbh_cdc_ecm_eth_input(uint8_t *buf, uint32_t buflen) -{ - usbh_lwip_eth_input_common(g_cdc_ecm_dev.netif, buf, buflen); -} - -void usbh_cdc_ecm_run(struct usbh_cdc_ecm *cdc_ecm_class) -{ - memset(&g_cdc_ecm_dev, 0, sizeof(struct eth_device)); - - g_cdc_ecm_dev.parent.control = rt_usbh_cdc_ecm_control; - g_cdc_ecm_dev.eth_rx = NULL; - g_cdc_ecm_dev.eth_tx = rt_usbh_cdc_ecm_eth_tx; - g_cdc_ecm_dev.parent.user_data = cdc_ecm_class; - - eth_device_init(&g_cdc_ecm_dev, "u0"); - eth_device_linkchange(&g_cdc_ecm_dev, RT_TRUE); - - usb_osal_thread_create("usbh_cdc_ecm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ecm_rx_thread, NULL); -} - -void usbh_cdc_ecm_stop(struct usbh_cdc_ecm *cdc_ecm_class) -{ - (void)cdc_ecm_class; - - eth_device_deinit(&g_cdc_ecm_dev); -} -#endif - -#ifdef CONFIG_USBHOST_PLATFORM_CDC_RNDIS -#include "usbh_rndis.h" - -static struct eth_device g_rndis_dev; - -static rt_timer_t keep_timer = RT_NULL; - -static void rndis_dev_keepalive_timeout(void *parameter) -{ - struct usbh_rndis *rndis_class = (struct usbh_rndis *)parameter; - usbh_rndis_keepalive(rndis_class); -} - -static void timer_init(struct usbh_rndis *rndis_class) -{ - keep_timer = rt_timer_create("keep", - rndis_dev_keepalive_timeout, - rndis_class, - 5000, - RT_TIMER_FLAG_PERIODIC | - RT_TIMER_FLAG_SOFT_TIMER); - - rt_timer_start(keep_timer); -} - -static rt_err_t rt_usbh_rndis_control(rt_device_t dev, int cmd, void *args) -{ - struct usbh_rndis *rndis_class = (struct usbh_rndis *)dev->user_data; - - switch (cmd) { - case NIOCTL_GADDR: - - /* get mac address */ - if (args) - rt_memcpy(args, rndis_class->mac, 6); - else - return -RT_ERROR; - - break; - - default: - break; - } - - return RT_EOK; -} - -static rt_err_t rt_usbh_rndis_eth_tx(rt_device_t dev, struct pbuf *p) -{ - int ret; - (void)dev; - - usbh_lwip_eth_output_common(p, usbh_rndis_get_eth_txbuf()); - ret = usbh_rndis_eth_output(p->tot_len); - if (ret < 0) { - return -RT_ERROR; - } else { - return RT_EOK; - } -} - -void usbh_rndis_eth_input(uint8_t *buf, uint32_t buflen) -{ - usbh_lwip_eth_input_common(g_rndis_dev.netif, buf, buflen); -} - -void usbh_rndis_run(struct usbh_rndis *rndis_class) -{ - memset(&g_rndis_dev, 0, sizeof(struct eth_device)); - - g_rndis_dev.parent.control = rt_usbh_rndis_control; - g_rndis_dev.eth_rx = NULL; - g_rndis_dev.eth_tx = rt_usbh_rndis_eth_tx; - g_rndis_dev.parent.user_data = rndis_class; - - eth_device_init(&g_rndis_dev, "u2"); - eth_device_linkchange(&g_rndis_dev, RT_TRUE); - - usb_osal_thread_create("usbh_rndis_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rndis_rx_thread, NULL); - //timer_init(rndis_class); -} - -void usbh_rndis_stop(struct usbh_rndis *rndis_class) -{ - (void)rndis_class; - - eth_device_deinit(&g_rndis_dev); - // rt_timer_stop(keep_timer); - // rt_timer_delete(keep_timer); -} -#endif - -#ifdef CONFIG_USBHOST_PLATFORM_CDC_NCM -#include "usbh_cdc_ncm.h" - -static struct eth_device g_cdc_ncm_dev; - -static rt_err_t rt_usbh_cdc_ncm_control(rt_device_t dev, int cmd, void *args) -{ - struct usbh_cdc_ncm *cdc_ncm_class = (struct usbh_cdc_ncm *)dev->user_data; - - switch (cmd) { - case NIOCTL_GADDR: - - /* get mac address */ - if (args) - rt_memcpy(args, cdc_ncm_class->mac, 6); - else - return -RT_ERROR; - - break; - - default: - break; - } - - return RT_EOK; -} - -static rt_err_t rt_usbh_cdc_ncm_eth_tx(rt_device_t dev, struct pbuf *p) -{ - int ret; - (void)dev; - - usbh_lwip_eth_output_common(p, usbh_cdc_ncm_get_eth_txbuf()); - ret = usbh_cdc_ncm_eth_output(p->tot_len); - if (ret < 0) { - return -RT_ERROR; - } else { - return RT_EOK; - } -} - -void usbh_cdc_ncm_eth_input(uint8_t *buf, uint32_t buflen) -{ - usbh_lwip_eth_input_common(g_cdc_ncm_dev.netif, buf, buflen); -} - -void usbh_cdc_ncm_run(struct usbh_cdc_ncm *cdc_ncm_class) -{ - memset(&g_cdc_ncm_dev, 0, sizeof(struct eth_device)); - - g_cdc_ncm_dev.parent.control = rt_usbh_cdc_ncm_control; - g_cdc_ncm_dev.eth_rx = NULL; - g_cdc_ncm_dev.eth_tx = rt_usbh_cdc_ncm_eth_tx; - g_cdc_ncm_dev.parent.user_data = cdc_ncm_class; - - eth_device_init(&g_cdc_ncm_dev, "u1"); - eth_device_linkchange(&g_cdc_ncm_dev, RT_TRUE); - - usb_osal_thread_create("usbh_cdc_ncm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ncm_rx_thread, NULL); -} - -void usbh_cdc_ncm_stop(struct usbh_cdc_ncm *cdc_ncm_class) -{ - (void)cdc_ncm_class; - - eth_device_deinit(&g_cdc_ncm_dev); -} -#endif - -#ifdef CONFIG_USBHOST_PLATFORM_ASIX -#include "usbh_asix.h" - -static struct eth_device g_asix_dev; - -static rt_err_t rt_usbh_asix_control(rt_device_t dev, int cmd, void *args) -{ - struct usbh_asix *asix_class = (struct usbh_asix *)dev->user_data; - - switch (cmd) { - case NIOCTL_GADDR: - - /* get mac address */ - if (args) - rt_memcpy(args, asix_class->mac, 6); - else - return -RT_ERROR; - - break; - - default: - break; - } - - return RT_EOK; -} - -static rt_err_t rt_usbh_asix_eth_tx(rt_device_t dev, struct pbuf *p) -{ - int ret; - (void)dev; - - usbh_lwip_eth_output_common(p, usbh_asix_get_eth_txbuf()); - ret = usbh_asix_eth_output(p->tot_len); - if (ret < 0) { - return -RT_ERROR; - } else { - return RT_EOK; - } -} - -void usbh_asix_eth_input(uint8_t *buf, uint32_t buflen) -{ - usbh_lwip_eth_input_common(g_asix_dev.netif, buf, buflen); -} - -void usbh_asix_run(struct usbh_asix *asix_class) -{ - memset(&g_asix_dev, 0, sizeof(struct eth_device)); - - g_asix_dev.parent.control = rt_usbh_asix_control; - g_asix_dev.eth_rx = NULL; - g_asix_dev.eth_tx = rt_usbh_asix_eth_tx; - g_asix_dev.parent.user_data = asix_class; - - eth_device_init(&g_asix_dev, "u3"); - eth_device_linkchange(&g_asix_dev, RT_TRUE); - - usb_osal_thread_create("usbh_asix_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_asix_rx_thread, NULL); -} - -void usbh_asix_stop(struct usbh_asix *asix_class) -{ - (void)asix_class; - - eth_device_deinit(&g_asix_dev); -} -#endif - -#ifdef CONFIG_USBHOST_PLATFORM_RTL8152 -#include "usbh_rtl8152.h" - -static struct eth_device g_rtl8152_dev; - -static rt_err_t rt_usbh_rtl8152_control(rt_device_t dev, int cmd, void *args) -{ - struct usbh_rtl8152 *rtl8152_class = (struct usbh_rtl8152 *)dev->user_data; - - switch (cmd) { - case NIOCTL_GADDR: - - /* get mac address */ - if (args) - rt_memcpy(args, rtl8152_class->mac, 6); - else - return -RT_ERROR; - - break; - - default: - break; - } - - return RT_EOK; -} - -static rt_err_t rt_usbh_rtl8152_eth_tx(rt_device_t dev, struct pbuf *p) -{ - int ret; - (void)dev; - - usbh_lwip_eth_output_common(p, usbh_rtl8152_get_eth_txbuf()); - ret = usbh_rtl8152_eth_output(p->tot_len); - if (ret < 0) { - return -RT_ERROR; - } else { - return RT_EOK; - } -} - -void usbh_rtl8152_eth_input(uint8_t *buf, uint32_t buflen) -{ - usbh_lwip_eth_input_common(g_rtl8152_dev.netif, buf, buflen); -} - -void usbh_rtl8152_run(struct usbh_rtl8152 *rtl8152_class) -{ - memset(&g_rtl8152_dev, 0, sizeof(struct eth_device)); - - g_rtl8152_dev.parent.control = rt_usbh_rtl8152_control; - g_rtl8152_dev.eth_rx = NULL; - g_rtl8152_dev.eth_tx = rt_usbh_rtl8152_eth_tx; - g_rtl8152_dev.parent.user_data = rtl8152_class; - - eth_device_init(&g_rtl8152_dev, "u4"); - eth_device_linkchange(&g_rtl8152_dev, RT_TRUE); - - usb_osal_thread_create("usbh_rtl8152_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rtl8152_rx_thread, NULL); -} - -void usbh_rtl8152_stop(struct usbh_rtl8152 *rtl8152_class) -{ - (void)rtl8152_class; - - eth_device_deinit(&g_rtl8152_dev); -} -#endif diff --git a/platform/rtthread/usbh_rtserial.c b/platform/rtthread/usbh_rtserial.c deleted file mode 100644 index ab16f325..00000000 --- a/platform/rtthread/usbh_rtserial.c +++ /dev/null @@ -1,215 +0,0 @@ -/* - * Copyright (c) 2025, sakumisu - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include -#include - -#include "usbh_core.h" -#include "usbh_serial.h" - -static rt_err_t rt_usbh_serial_open(struct rt_device *dev, rt_uint16_t oflag) -{ - struct usbh_serial *serial; - - RT_ASSERT(dev != RT_NULL && dev->user_data != RT_NULL); - - serial = (struct usbh_serial *)dev->user_data; - - serial = usbh_serial_open(serial->hport->config.intf[serial->intf].devname, USBH_SERIAL_O_RDWR | USBH_SERIAL_O_NONBLOCK); - if (serial == RT_NULL) { - USB_LOG_ERR("serial open failed\n"); - return -RT_ERROR; - } - - struct usbh_serial_termios termios; - - memset(&termios, 0, sizeof(termios)); - termios.baudrate = 115200; - termios.stopbits = 0; - termios.parity = 0; - termios.databits = 8; - termios.rtscts = false; - termios.rx_timeout = 0; - - usbh_serial_control(serial, USBH_SERIAL_CMD_SET_ATTR, &termios); - - return RT_EOK; -} - -static rt_err_t rt_usbh_serial_close(struct rt_device *dev) -{ - struct usbh_serial *serial; - - RT_ASSERT(dev != RT_NULL && dev->user_data != RT_NULL); - - serial = (struct usbh_serial *)dev->user_data; - - usbh_serial_close(serial); - - return RT_EOK; -} - -static rt_ssize_t rt_usbh_serial_read(struct rt_device *dev, - rt_off_t pos, - void *buffer, - rt_size_t size) -{ - struct usbh_serial *serial; - - RT_ASSERT(dev != RT_NULL && dev->user_data != RT_NULL); - - serial = (struct usbh_serial *)dev->user_data; - - return usbh_serial_read(serial, buffer, size); -} - -static rt_ssize_t rt_usbh_serial_write(struct rt_device *dev, - rt_off_t pos, - const void *buffer, - rt_size_t size) -{ - struct usbh_serial *serial; - int ret = 0; - rt_uint8_t *align_buf; - - RT_ASSERT(dev != RT_NULL && dev->user_data != RT_NULL); - - serial = (struct usbh_serial *)dev->user_data; - - align_buf = (rt_uint8_t *)buffer; - - if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { - align_buf = rt_malloc_align(USB_ALIGN_UP(size, CONFIG_USB_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); - } - - ret = usbh_serial_write(serial, align_buf, size); - - if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) { - rt_free_align(align_buf); - } - - return ret; -} - -static rt_err_t rt_usbh_serial_control(struct rt_device *dev, - int cmd, - void *args) -{ - struct usbh_serial *serial; - struct serial_configure *config; - int ret = -RT_EINVAL; - - RT_ASSERT(dev != RT_NULL && dev->user_data != RT_NULL); - - serial = (struct usbh_serial *)dev->user_data; - - switch (cmd) { - case RT_DEVICE_CTRL_CONFIG: { - config = (struct serial_configure *)args; - - struct usbh_serial_termios termios; - - memset(&termios, 0, sizeof(termios)); - termios.baudrate = config->baud_rate; - termios.stopbits = 0; - termios.parity = config->parity; - termios.databits = config->data_bits; - termios.rtscts = false; - termios.rx_timeout = 0; - - usbh_serial_control(serial, USBH_SERIAL_CMD_SET_ATTR, &termios); - } break; - - default: - break; - } - - return ret; -} - -#ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops usbh_serial_ops = { - NULL, - rt_usbh_serial_open, - rt_usbh_serial_close, - rt_usbh_serial_read, - rt_usbh_serial_write, - rt_usbh_serial_control -}; -#endif - -rt_err_t usbh_serial_register(struct usbh_serial *serial) -{ - rt_err_t ret; - struct rt_device *device; - RT_ASSERT(serial != RT_NULL); - - device = rt_malloc(sizeof(struct rt_device)); - if (device == RT_NULL) { - USB_LOG_ERR("serial device malloc failed\n"); - return -RT_ENOMEM; - } - memset(device, 0, sizeof(struct rt_device)); - - device->type = RT_Device_Class_Char; - device->rx_indicate = RT_NULL; - device->tx_complete = RT_NULL; - -#ifdef RT_USING_DEVICE_OPS - device->ops = &usbh_serial_ops; -#else - device->init = NULL; - device->open = rt_usbh_serial_open; - device->close = rt_usbh_serial_close; - device->read = rt_usbh_serial_read; - device->write = rt_usbh_serial_write; - device->control = rt_usbh_serial_control; -#endif - device->user_data = serial; - serial->user_data = device; - - /* skip /dev/ to avoid BAD file */ - const char *dev_name = serial->hport->config.intf[serial->intf].devname; - if (strncmp(dev_name, "/dev/", 5) == 0) { - dev_name += 5; - } - - /* register a character device */ - ret = rt_device_register(device, dev_name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_REMOVABLE); - -#ifdef RT_USING_POSIX_DEVIO - /* set fops */ - device->fops = &usbh_serial_fops; -#endif - return ret; -} - -void usbh_serial_unregister(struct usbh_serial *serial) -{ - struct rt_device *device; - - RT_ASSERT(serial != NULL && serial->user_data != NULL); - - device = (struct rt_device *)serial->user_data; - - rt_device_unregister(device); - rt_free(device); -} - -void usbh_serial_run(struct usbh_serial *serial) -{ - usbh_serial_register(serial); -} - -void usbh_serial_stop(struct usbh_serial *serial) -{ - usbh_serial_unregister(serial); -} \ No newline at end of file -- cgit v1.3.1