Skip to content

Commit 8f46310

Browse files
committed
update(platform): update dcache for msc host with fs
Signed-off-by: sakumisu <[email protected]>
1 parent 66344d6 commit 8f46310

File tree

2 files changed

+77
-51
lines changed

2 files changed

+77
-51
lines changed

platform/fatfs/usbh_fatfs.c

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010

1111
struct usbh_msc *active_msc_class;
1212

13-
int USB_disk_status(void)
14-
{
15-
return RES_OK;
16-
}
17-
1813
int USB_disk_initialize(void)
1914
{
2015
active_msc_class = (struct usbh_msc *)usbh_find_class_instance("/dev/sda");
@@ -28,14 +23,69 @@ int USB_disk_initialize(void)
2823
return RES_OK;
2924
}
3025

26+
int USB_disk_status(void)
27+
{
28+
return RES_OK;
29+
}
30+
3131
int USB_disk_read(BYTE *buff, LBA_t sector, UINT count)
3232
{
33-
return usbh_msc_scsi_read10(active_msc_class, sector, buff, count);
33+
int ret;
34+
uint8_t *align_buf;
35+
36+
align_buf = (uint8_t *)buff;
37+
#ifdef CONFIG_USB_DCACHE_ENABLE
38+
if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
39+
align_buf = (uint8_t *)memalign(CONFIG_USB_ALIGN_SIZE, count * active_msc_class->blocksize);
40+
if (!align_buf) {
41+
printf("msc get align buf failed\r\n");
42+
return -USB_ERR_NOMEM;
43+
}
44+
}
45+
#endif
46+
ret = usbh_msc_scsi_read10(active_msc_class, sector, align_buf, count);
47+
if (ret < 0) {
48+
ret = RES_ERROR;
49+
} else {
50+
ret = RES_OK;
51+
}
52+
#ifdef CONFIG_USB_DCACHE_ENABLE
53+
if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
54+
usb_memcpy(buff, align_buf, count * active_msc_class->blocksize);
55+
free(align_buf);
56+
}
57+
#endif
58+
return ret;
3459
}
3560

3661
int USB_disk_write(const BYTE *buff, LBA_t sector, UINT count)
3762
{
38-
return usbh_msc_scsi_write10(active_msc_class, sector, buff, count);
63+
int ret;
64+
uint8_t *align_buf;
65+
66+
align_buf = (uint8_t *)buff;
67+
#ifdef CONFIG_USB_DCACHE_ENABLE
68+
if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
69+
align_buf = (uint8_t *)memalign(CONFIG_USB_ALIGN_SIZE, count * active_msc_class->blocksize);
70+
if (!align_buf) {
71+
printf("msc get align buf failed\r\n");
72+
return -USB_ERR_NOMEM;
73+
}
74+
usb_memcpy(align_buf, buff, count * active_msc_class->blocksize);
75+
}
76+
#endif
77+
ret = usbh_msc_scsi_write10(active_msc_class, sector, align_buf, count);
78+
if (ret < 0) {
79+
ret = RES_ERROR;
80+
} else {
81+
ret = RES_OK;
82+
}
83+
#ifdef CONFIG_USB_DCACHE_ENABLE
84+
if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
85+
free(align_buf);
86+
}
87+
#endif
88+
return ret;
3989
}
4090

4191
int USB_disk_ioctl(BYTE cmd, void *buff)

platform/rtthread/usbh_dfs.c

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,11 @@
2929
#endif
3030
#endif
3131

32-
#if defined(BSP_USING_BL61X)
33-
#include "bflb_l1c.h"
34-
35-
void rt_hw_cpu_dcache_ops(int ops, void *addr, int size)
36-
{
37-
if (ops == RT_HW_CACHE_FLUSH) {
38-
bflb_l1c_dcache_clean_range(addr, size);
39-
} else {
40-
bflb_l1c_dcache_invalidate_range(addr, size);
41-
}
42-
}
32+
#ifdef RT_USING_CACHE
33+
#ifndef CONFIG_USB_DCACHE_ENABLE
34+
#error CONFIG_USB_DCACHE_ENABLE must be enabled to use msc disk
35+
#endif
4336
#endif
44-
45-
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t msc_sector[512];
4637

4738
static rt_err_t rt_udisk_init(rt_device_t dev)
4839
{
@@ -60,36 +51,29 @@ static ssize_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void *buffer,
6051
{
6152
struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data;
6253
int ret;
54+
rt_uint8_t *align_buf;
6355

56+
align_buf = (rt_uint8_t *)buffer;
6457
#ifdef RT_USING_CACHE
65-
rt_uint32_t *align_buf;
66-
67-
if ((uint32_t)buffer & (RT_ALIGN_SIZE - 1)) {
68-
align_buf = rt_malloc_align(size * msc_class->blocksize, RT_ALIGN_SIZE);
58+
if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) {
59+
align_buf = rt_malloc_align(size * msc_class->blocksize, CONFIG_USB_ALIGN_SIZE);
6960
if (!align_buf) {
7061
rt_kprintf("msc get align buf failed\n");
7162
return 0;
7263
}
7364
} else {
74-
align_buf = (rt_uint32_t *)buffer;
7565
}
76-
66+
#endif
7767
ret = usbh_msc_scsi_read10(msc_class, pos, (uint8_t *)align_buf, size);
7868
if (ret < 0) {
7969
rt_kprintf("usb mass_storage read failed\n");
8070
return 0;
8171
}
82-
rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, align_buf, size * msc_class->blocksize);
83-
if ((uint32_t)buffer & (RT_ALIGN_SIZE - 1)) {
84-
rt_memcpy(buffer, align_buf, size * msc_class->blocksize);
72+
#ifdef RT_USING_CACHE
73+
if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) {
74+
usb_memcpy(buffer, align_buf, size * msc_class->blocksize);
8575
rt_free_align(align_buf);
8676
}
87-
#else
88-
ret = usbh_msc_scsi_read10(msc_class, pos, buffer, size);
89-
if (ret < 0) {
90-
rt_kprintf("usb mass_storage read failed\n");
91-
return 0;
92-
}
9377
#endif
9478
return size;
9579
}
@@ -99,37 +83,29 @@ static ssize_t rt_udisk_write(rt_device_t dev, rt_off_t pos, const void *buffer,
9983
{
10084
struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data;
10185
int ret;
86+
rt_uint8_t *align_buf;
10287

88+
align_buf = (rt_uint8_t *)buffer;
10389
#ifdef RT_USING_CACHE
104-
rt_uint32_t *align_buf;
105-
106-
if ((uint32_t)buffer & (RT_ALIGN_SIZE - 1)) {
107-
align_buf = rt_malloc_align(size * msc_class->blocksize, RT_ALIGN_SIZE);
90+
if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) {
91+
align_buf = rt_malloc_align(size * msc_class->blocksize, CONFIG_USB_ALIGN_SIZE);
10892
if (!align_buf) {
10993
rt_kprintf("msc get align buf failed\n");
11094
return 0;
11195
}
11296

113-
rt_memcpy(align_buf, buffer, size * msc_class->blocksize);
114-
} else {
115-
align_buf = (rt_uint32_t *)buffer;
97+
usb_memcpy(align_buf, buffer, size * msc_class->blocksize);
11698
}
117-
118-
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, align_buf, size * msc_class->blocksize);
99+
#endif
119100
ret = usbh_msc_scsi_write10(msc_class, pos, (uint8_t *)align_buf, size);
120101
if (ret < 0) {
121102
rt_kprintf("usb mass_storage write failed\n");
122103
return 0;
123104
}
124-
if ((uint32_t)buffer & (RT_ALIGN_SIZE - 1)) {
105+
#ifdef RT_USING_CACHE
106+
if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) {
125107
rt_free_align(align_buf);
126108
}
127-
#else
128-
ret = usbh_msc_scsi_write10(msc_class, pos, buffer, size);
129-
if (ret < 0) {
130-
rt_kprintf("usb mass_storage write failed\n");
131-
return 0;
132-
}
133109
#endif
134110

135111
return size;

0 commit comments

Comments
 (0)