summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsakumisu <[email protected]>2025-06-04 15:33:49 +0800
committersakumisu <[email protected]>2025-06-04 15:33:49 +0800
commit88cbed98070304eef0e09c50983518a2b0cd6733 (patch)
tree5df0d10587c57d68d0198e0a2597f2520f7d6668
parent1d950771616a2c78e0b00b48967e261bbfc9043f (diff)
update(platform): make msc host with fs common for dcache
Signed-off-by: sakumisu <[email protected]>
-rw-r--r--platform/fatfs/usbh_fatfs.c64
-rw-r--r--platform/rtthread/usbh_dfs.c64
2 files changed, 77 insertions, 51 deletions
diff --git a/platform/fatfs/usbh_fatfs.c b/platform/fatfs/usbh_fatfs.c
index 5e164a7b..ad1b04ca 100644
--- a/platform/fatfs/usbh_fatfs.c
+++ b/platform/fatfs/usbh_fatfs.c
@@ -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)
diff --git a/platform/rtthread/usbh_dfs.c b/platform/rtthread/usbh_dfs.c
index 128d2345..00f5a033 100644
--- a/platform/rtthread/usbh_dfs.c
+++ b/platform/rtthread/usbh_dfs.c
@@ -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;