[components][drivers][usb] add the missing file and fix build error for cherryusb

This commit is contained in:
Evlers
2026-06-02 17:41:34 +08:00
committed by Rbb666
parent c6eb181a1f
commit 2e4f500b14
11 changed files with 1468 additions and 3 deletions
+1
View File
@@ -63,6 +63,7 @@ vdso.lds
# cherryusb libraries
!components/drivers/usb/cherryusb/port/pusb2/*.a
!components/drivers/usb/cherryusb/port/xhci/phytium/*.a
!components/drivers/usb/cherryusb/platform/rtthread
# stm32cubemx
**/CubeMX_Config/Drivers/
+2 -3
View File
@@ -142,12 +142,12 @@ if GetDepend(['RT_CHERRYUSB_DEVICE']):
src += Glob('class/vendor/display/usbd_display.c')
if GetDepend(['RT_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(['RT_CHERRYUSB_DEVICE_CDC_ACM_CHARDEV']):
src += Glob('platform/rtthread/rt_usbd_serial.c')
if GetDepend(['PKG_CHERRYUSB_DEVICE_MSC_BLKDEV']):
if GetDepend(['RT_CHERRYUSB_DEVICE_MSC_BLKDEV']):
src += Glob('platform/rtthread/rt_usbd_msc.c')
if GetDepend(['RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM']):
src += Glob('demo/cdc_acm_template.c')
@@ -355,4 +355,3 @@ src += Glob('platform/rtthread/rt_usb_check.c')
group = DefineGroup('UsbStack', src, depend = ['RT_USING_CHERRYUSB'], LIBS = LIBS, LIBPATH=LIBPATH, CPPPATH = path, CPPDEFINES = CPPDEFINES)
Return('group')
@@ -143,6 +143,7 @@ void usbd_adb_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
break;
case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
{
char *support_feature = "device::"
"ro.product.name=cherryadb;"
"ro.product.model=cherrysh;"
@@ -158,6 +159,7 @@ void usbd_adb_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
adb_send_msg(&tx_packet);
adb_client.writable = false;
}
break;
case A_OPEN: /* OPEN(local-id, 0, "destination") */
rx_packet.payload[rx_packet.msg.data_length] = '\0';
@@ -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
@@ -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 <busid> <reg_base>\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 <busid>\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
@@ -0,0 +1,156 @@
/*
* Copyright (c) 2025, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <rtthread.h>
#include <rtdevice.h>
#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);
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2026, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "usbd_core.h"
#include "usbd_msc.h"
#include <rtthread.h>
#include <rtdevice.h>
#ifndef CONFIG_USBDEV_MSC_THREAD
#error "Please enable CONFIG_USBDEV_MSC_THREAD, move msc read & write from isr to thread"
#endif
#ifndef CONFIG_USBDEV_MSC_BLOCK_DEV_NAME
#define CONFIG_USBDEV_MSC_BLOCK_DEV_NAME "sd0"
#endif
static rt_device_t blk_dev = RT_NULL;
static struct rt_device_blk_geometry geometry = { 0 };
static struct usbd_interface msc_intf;
void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint32_t *block_size)
{
rt_device_control(blk_dev, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
*block_num = geometry.sector_count;
*block_size = geometry.bytes_per_sector;
}
int usbd_msc_sector_read(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
{
rt_device_read(blk_dev, sector, buffer, length / geometry.bytes_per_sector);
return 0;
}
int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
{
rt_device_write(blk_dev, sector, buffer, length / geometry.bytes_per_sector);
return 0;
}
void usbd_msc_blkdev_init(uint8_t busid, uint8_t in_ep, uint8_t out_ep)
{
rt_err_t res;
blk_dev = rt_device_find(CONFIG_USBDEV_MSC_BLOCK_DEV_NAME);
RT_ASSERT(blk_dev);
res = rt_device_open(blk_dev, RT_DEVICE_OFLAG_RDWR);
RT_ASSERT(res == RT_EOK);
usbd_add_interface(busid, usbd_msc_init_intf(busid, &msc_intf, out_ep, in_ep));
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -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 <dfs_fs.h>
#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);
}
}
File diff suppressed because it is too large Load Diff