update(platform): make msc host with fs common for dcache

Signed-off-by: sakumisu <1203593632@qq.com>
This commit is contained in:
sakumisu
2025-06-04 15:33:49 +08:00
parent 1d95077161
commit 88cbed9807
2 changed files with 77 additions and 51 deletions

View File

@@ -10,11 +10,6 @@
struct usbh_msc *active_msc_class;
int USB_disk_status(void)
{
return RES_OK;
}
int USB_disk_initialize(void)
{
active_msc_class = (struct usbh_msc *)usbh_find_class_instance("/dev/sda");
@@ -28,14 +23,69 @@ int USB_disk_initialize(void)
return RES_OK;
}
int USB_disk_status(void)
{
return RES_OK;
}
int USB_disk_read(BYTE *buff, LBA_t sector, UINT count)
{
return usbh_msc_scsi_read10(active_msc_class, sector, buff, count);
int ret;
uint8_t *align_buf;
align_buf = (uint8_t *)buff;
#ifdef CONFIG_USB_DCACHE_ENABLE
if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
align_buf = (uint8_t *)memalign(CONFIG_USB_ALIGN_SIZE, count * active_msc_class->blocksize);
if (!align_buf) {
printf("msc get align buf failed\r\n");
return -USB_ERR_NOMEM;
}
}
#endif
ret = usbh_msc_scsi_read10(active_msc_class, sector, align_buf, count);
if (ret < 0) {
ret = RES_ERROR;
} else {
ret = RES_OK;
}
#ifdef CONFIG_USB_DCACHE_ENABLE
if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
usb_memcpy(buff, align_buf, count * active_msc_class->blocksize);
free(align_buf);
}
#endif
return ret;
}
int USB_disk_write(const BYTE *buff, LBA_t sector, UINT count)
{
return usbh_msc_scsi_write10(active_msc_class, sector, buff, count);
int ret;
uint8_t *align_buf;
align_buf = (uint8_t *)buff;
#ifdef CONFIG_USB_DCACHE_ENABLE
if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
align_buf = (uint8_t *)memalign(CONFIG_USB_ALIGN_SIZE, count * active_msc_class->blocksize);
if (!align_buf) {
printf("msc get align buf failed\r\n");
return -USB_ERR_NOMEM;
}
usb_memcpy(align_buf, buff, count * active_msc_class->blocksize);
}
#endif
ret = usbh_msc_scsi_write10(active_msc_class, sector, align_buf, count);
if (ret < 0) {
ret = RES_ERROR;
} else {
ret = RES_OK;
}
#ifdef CONFIG_USB_DCACHE_ENABLE
if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
free(align_buf);
}
#endif
return ret;
}
int USB_disk_ioctl(BYTE cmd, void *buff)

View File

@@ -29,20 +29,11 @@
#endif
#endif
#if defined(BSP_USING_BL61X)
#include "bflb_l1c.h"
void rt_hw_cpu_dcache_ops(int ops, void *addr, int size)
{
if (ops == RT_HW_CACHE_FLUSH) {
bflb_l1c_dcache_clean_range(addr, size);
} else {
bflb_l1c_dcache_invalidate_range(addr, size);
}
}
#ifdef RT_USING_CACHE
#ifndef CONFIG_USB_DCACHE_ENABLE
#error CONFIG_USB_DCACHE_ENABLE must be enabled to use msc disk
#endif
#endif
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t msc_sector[512];
static rt_err_t rt_udisk_init(rt_device_t dev)
{
@@ -60,36 +51,29 @@ static ssize_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void *buffer,
{
struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data;
int ret;
rt_uint8_t *align_buf;
align_buf = (rt_uint8_t *)buffer;
#ifdef RT_USING_CACHE
rt_uint32_t *align_buf;
if ((uint32_t)buffer & (RT_ALIGN_SIZE - 1)) {
align_buf = rt_malloc_align(size * msc_class->blocksize, RT_ALIGN_SIZE);
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 {
align_buf = (rt_uint32_t *)buffer;
}
#endif
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;
}
rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, align_buf, size * msc_class->blocksize);
if ((uint32_t)buffer & (RT_ALIGN_SIZE - 1)) {
rt_memcpy(buffer, align_buf, size * msc_class->blocksize);
#ifdef RT_USING_CACHE
if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) {
usb_memcpy(buffer, align_buf, size * msc_class->blocksize);
rt_free_align(align_buf);
}
#else
ret = usbh_msc_scsi_read10(msc_class, pos, buffer, size);
if (ret < 0) {
rt_kprintf("usb mass_storage read failed\n");
return 0;
}
#endif
return size;
}
@@ -99,37 +83,29 @@ static ssize_t rt_udisk_write(rt_device_t dev, rt_off_t pos, const void *buffer,
{
struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data;
int ret;
rt_uint8_t *align_buf;
align_buf = (rt_uint8_t *)buffer;
#ifdef RT_USING_CACHE
rt_uint32_t *align_buf;
if ((uint32_t)buffer & (RT_ALIGN_SIZE - 1)) {
align_buf = rt_malloc_align(size * msc_class->blocksize, RT_ALIGN_SIZE);
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;
}
rt_memcpy(align_buf, buffer, size * msc_class->blocksize);
} else {
align_buf = (rt_uint32_t *)buffer;
usb_memcpy(align_buf, buffer, size * msc_class->blocksize);
}
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, align_buf, size * msc_class->blocksize);
#endif
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 & (RT_ALIGN_SIZE - 1)) {
#ifdef RT_USING_CACHE
if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) {
rt_free_align(align_buf);
}
#else
ret = usbh_msc_scsi_write10(msc_class, pos, buffer, size);
if (ret < 0) {
rt_kprintf("usb mass_storage write failed\n");
return 0;
}
#endif
return size;