diff --git a/radio/src/disk_cache.cpp b/radio/src/disk_cache.cpp index 4933a2d9f..53bcdb858 100644 --- a/radio/src/disk_cache.cpp +++ b/radio/src/disk_cache.cpp @@ -43,7 +43,7 @@ DiskCacheBlock::DiskCacheBlock() { } -bool DiskCacheBlock::read(BYTE* buff, DWORD sector, UINT count) +bool DiskCacheBlock::read(BYTE * buff, DWORD sector, UINT count) { if (sector >= startSector && (sector+count) <= endSector) { TRACE_DISK_CACHE("\tcache read(%u, %u) from %p", (uint32_t)sector, (uint32_t)count, this); @@ -53,9 +53,8 @@ bool DiskCacheBlock::read(BYTE* buff, DWORD sector, UINT count) return false; } -DRESULT DiskCacheBlock::fill(BYTE drv, BYTE* buff, DWORD sector, UINT count) +DRESULT DiskCacheBlock::fill(BYTE drv, BYTE * buff, DWORD sector, UINT count) { - // TODO: check if trying to read beyond the end of disk DRESULT res = __disk_read(drv, data, sector, DISK_CACHE_BLOCK_SECTORS); if (res != RES_OK) { return res; @@ -88,7 +87,7 @@ DiskCache::DiskCache() blocks = new DiskCacheBlock[DISK_CACHE_BLOCKS_NUM]; } -DRESULT DiskCache::read(BYTE drv, BYTE* buff, DWORD sector, UINT count) +DRESULT DiskCache::read(BYTE drv, BYTE * buff, DWORD sector, UINT count) { // TODO: check if not caching first sectors would improve anything // if (sector < 1000) { @@ -101,8 +100,14 @@ DRESULT DiskCache::read(BYTE drv, BYTE* buff, DWORD sector, UINT count) TRACE_DISK_CACHE("\t\t big read(%u, %u)", this, (uint32_t)sector, (uint32_t)count); return __disk_read(drv, buff, sector, count); } + + // if block + cache block size is beyond the end of the disk, then read it directly without using cache + if (sector+DISK_CACHE_BLOCK_SECTORS > SDCardInfo.CardCapacity / SDCardInfo.CardBlockSize) { + TRACE_DISK_CACHE("\t\t cache would be beyond end of disk(%u)", this, (uint32_t)sector); + return __disk_read(drv, buff, sector, count); + } - for(int n=0; n < DISK_CACHE_BLOCKS_NUM; ++n) { + for (int n=0; n= DISK_CACHE_BLOCKS_NUM) { lastBlock = 0; } + return blocks[lastBlock].fill(drv, buff, sector, count); } @@ -147,13 +153,13 @@ int DiskCache::getHitRate() const return (stats.noHits * 1000) / all; } -DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, UINT count) +DRESULT disk_read(BYTE drv, BYTE * buff, DWORD sector, UINT count) { return diskCache.read(drv, buff, sector, count); } -DRESULT disk_write (BYTE drv, const BYTE *buff, DWORD sector, UINT count) +DRESULT disk_write(BYTE drv, const BYTE * buff, DWORD sector, UINT count) { return diskCache.write(drv, buff, sector, count); } diff --git a/radio/src/targets/common/arm/stm32/usbd_storage_msd.cpp b/radio/src/targets/common/arm/stm32/usbd_storage_msd.cpp index 6f4c71fea..3e07ff7a8 100644 --- a/radio/src/targets/common/arm/stm32/usbd_storage_msd.cpp +++ b/radio/src/targets/common/arm/stm32/usbd_storage_msd.cpp @@ -21,6 +21,7 @@ /* Includes ------------------------------------------------------------------*/ #include "opentx.h" #include "FatFs/diskio.h" +#include "sdio_sd.h" #if defined(__cplusplus) && !defined(SIMU) extern "C" { @@ -38,8 +39,7 @@ enum MassstorageLuns { }; /* USB Mass storage Standard Inquiry Data */ -const unsigned char STORAGE_Inquirydata[] = {//36 - +const unsigned char STORAGE_Inquirydata[] = { //36 /* LUN 0 */ 0x00, 0x80, diff --git a/radio/src/thirdparty/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_scsi.c b/radio/src/thirdparty/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_scsi.c index 1a7ccf6bc..b7f1d020b 100644 --- a/radio/src/thirdparty/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_scsi.c +++ b/radio/src/thirdparty/STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/src/usbd_msc_scsi.c @@ -666,7 +666,7 @@ static int8_t SCSI_ProcessRead (uint8_t lun) { uint32_t len; - len = MIN(SCSI_blk_len , MSC_MEDIA_PACKET); + len = MIN(SCSI_blk_len , MSC_MEDIA_PACKET); if( USBD_STORAGE_fops->Read(lun , MSC_BOT_Data,