Skip to content

Commit 90c80bf

Browse files
committed
define CFG_TUD_MSC_EP_BUFSIZE 4096
1 parent c8908cc commit 90c80bf

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

src/msc_disk.c

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*
2424
*/
2525

26+
#include <stdlib.h>
2627
#include "hardware/flash.h"
2728
#include "hardware/sync.h"
2829
#include "bsp/board.h"
@@ -33,6 +34,12 @@
3334
#include "../include/msc_disk.h"
3435
#include <mrubyc.h>
3536

37+
int FLASH_disk_read(void *buff, int32_t sector, int count);
38+
int FLASH_disk_write(const void *buff, int32_t sector, int count);
39+
40+
#define DISK_READ(buff, sector, count) FLASH_disk_read(buff, sector, count)
41+
#define DISK_WRITE(buff, sector, count) FLASH_disk_write(buff, sector, count)
42+
3643
// whether host does safe-eject
3744
static bool ejected = false;
3845

@@ -112,8 +119,21 @@ tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint
112119
(void) lun;
113120
// out of ramdisk
114121
if ( lba >= SECTOR_COUNT ) return -1;
115-
memcpy(buffer, (void *)(FLASH_MMAP_ADDR + lba * SECTOR_SIZE + offset), bufsize);
116-
return bufsize;
122+
123+
/*
124+
* In order to avoid (0 < offset) and (bufsize != SECTOR_SIZE),
125+
* CFG_TUD_MSC_EP_BUFSIZE has to be equal to SECTOR_SIZE.
126+
*/
127+
if (0 < offset || bufsize != SECTOR_SIZE) {
128+
console_printf("Failed in tud_msc_read10_cb()\n");
129+
console_printf("offset: %d, bufsize: %d\n", offset, bufsize);
130+
tud_task();
131+
abort();
132+
}
133+
134+
//memcpy(buffer, (void *)(FLASH_MMAP_ADDR + lba * SECTOR_SIZE), SECTOR_SIZE);
135+
DISK_READ(buffer, lba, 1);
136+
return SECTOR_SIZE;
117137
}
118138

119139
bool
@@ -131,22 +151,27 @@ tud_msc_is_writable_cb (uint8_t lun)
131151
// Process data in buffer to disk's storage and return number of written bytes
132152
int32_t
133153
tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize)
134-
/*
135-
* TODO: Cache coherency problem can be avoided if bufsize == SECTOR_SIZE(4094)
136-
*/
137154
{
138155
(void) lun;
139156
// out of ramdisk
140157
if ( lba >= SECTOR_COUNT ) return -1;
141-
uint32_t ints = save_and_disable_interrupts();
142-
if (offset == 0) {
143-
flash_range_erase(FLASH_TARGET_OFFSET + lba * SECTOR_SIZE, SECTOR_SIZE);
158+
159+
/*
160+
* In order to avoid (0 < offset) and (bufsize != SECTOR_SIZE),
161+
* CFG_TUD_MSC_EP_BUFSIZE has to be equal to SECTOR_SIZE.
162+
*/
163+
if (0 < offset || bufsize != SECTOR_SIZE) {
164+
console_printf("Failed in tud_msc_write10_cb()\n");
165+
console_printf("offset: %d, bufsize: %d\n", offset, bufsize);
166+
tud_task();
167+
abort();
144168
}
145-
flash_range_program(FLASH_TARGET_OFFSET + lba * SECTOR_SIZE + offset, buffer, bufsize);
146-
restore_interrupts(ints);
147-
return bufsize;
169+
170+
DISK_WRITE(buffer, lba, 1);
171+
return SECTOR_SIZE;
148172
}
149173

174+
150175
// Callback invoked when received an SCSI command not in built-in list below
151176
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
152177
// - READ10 and WRITE10 has their own callbacks

src/usb_cdc.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "../tinyusb/tusb_config.h"
21
#include "tusb.h"
32

43
#include <mrubyc.h>

tinyusb/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105

106106
// It is normaly 512, but we use 4096 to avoid cache coherency problem
107107
// on writing flash
108-
#define CFG_TUD_MSC_EP_BUFSIZE 512
108+
#define CFG_TUD_MSC_EP_BUFSIZE 4096
109109

110110
// CDC FIFO size of TX and RX
111111
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)

0 commit comments

Comments
 (0)