|
@@ -98,6 +98,8 @@ static bool ejected = false;
|
|
|
static struct disk_lba sector_cache[BLOCKS_PER_SECTOR];
|
|
|
static int sector_cached = -1;
|
|
|
|
|
|
+static volatile int cache_busy = 1;
|
|
|
+
|
|
|
static int sector_cache_load(uint32_t sector)
|
|
|
{
|
|
|
int ret;
|
|
@@ -115,15 +117,24 @@ static int sector_cache_load(uint32_t sector)
|
|
|
sector_cached = sector;
|
|
|
}
|
|
|
|
|
|
+static inline void sector_cache_sched_commit(void)
|
|
|
+{
|
|
|
+ cache_busy = 1;
|
|
|
+}
|
|
|
+
|
|
|
static void sector_cache_commit(void)
|
|
|
{
|
|
|
int ret;
|
|
|
uint32_t sector = sector_cached;
|
|
|
- if (sector * SPI_FLASH_SECTOR_SIZE > DRIVE_FLASH_SIZE)
|
|
|
+ if (sector * SPI_FLASH_SECTOR_SIZE > DRIVE_FLASH_SIZE) {
|
|
|
+ cache_busy = 0;
|
|
|
return;
|
|
|
+ }
|
|
|
//flash_encrypt_write_sector(sector, sector_cache[0].data);
|
|
|
+ flash_sector_erase(sector * SPI_FLASH_SECTOR_SIZE + DRIVE_FLASH_OFFSET);
|
|
|
flash_write(sector * SPI_FLASH_SECTOR_SIZE + DRIVE_FLASH_OFFSET,
|
|
|
sector_cache[0].data, SPI_FLASH_SECTOR_SIZE);
|
|
|
+ cache_busy = 0;
|
|
|
}
|
|
|
|
|
|
struct drive {
|
|
@@ -254,7 +265,8 @@ int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buff
|
|
|
sec_off = lba - (sector * BLOCKS_PER_SECTOR);
|
|
|
|
|
|
if ( sector != sector_cached) {
|
|
|
- sector_cache_load(sector);
|
|
|
+ if (!cache_busy)
|
|
|
+ sector_cache_load(sector);
|
|
|
return 0;
|
|
|
}
|
|
|
memcpy(buffer, sector_cache[sec_off].data + offset, bufsize);
|
|
@@ -291,10 +303,12 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t*
|
|
|
sector = lba / BLOCKS_PER_SECTOR;
|
|
|
sec_off = lba - (sector * BLOCKS_PER_SECTOR);
|
|
|
if ( sector != sector_cached) {
|
|
|
+ while(cache_busy)
|
|
|
+ sleep_ms(1);
|
|
|
sector_cache_load(sector);
|
|
|
}
|
|
|
memcpy(sector_cache[sec_off].data + offset, buffer, bufsize);
|
|
|
- sector_cache_commit();
|
|
|
+ sector_cache_sched_commit();
|
|
|
asm volatile("DMB");
|
|
|
return bufsize;
|
|
|
}
|
|
@@ -380,4 +394,9 @@ void disk_crypto_activate(int status)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void msc_task(void) {
|
|
|
+ if (cache_busy)
|
|
|
+ sector_cache_commit();
|
|
|
+}
|
|
|
+
|
|
|
#endif
|