2323 *
2424 */
2525
26+ #include <stdlib.h>
2627#include "hardware/flash.h"
2728#include "hardware/sync.h"
2829#include "bsp/board.h"
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
3744static 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
119139bool
@@ -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
132152int32_t
133153tud_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
0 commit comments