Skip to content

Commit

Permalink
enable the block cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed Jan 22, 2025
1 parent a40edfa commit 196718e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions firmware/controllers/thread_priority.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

// USB mass storage
#define MSD_THD_PRIO LOWPRIO + 20
#define MSD_CACHE_PRIO MSD_THD_PRIO - 1

// Lua interpreter must be lowest priority, as the user's code may get stuck in an infinite loop
#define PRIO_LUA LOWPRIO + 10
20 changes: 14 additions & 6 deletions firmware/hw_layer/mass_storage/mass_storage_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "mass_storage_init.h"
#include "mass_storage_device.h"
#include "null_device.h"
#include "block_cache.h"

#if HAL_USE_USB_MSD

Expand Down Expand Up @@ -40,7 +41,10 @@

// One block buffer per LUN
static NO_CACHE uint8_t blkbufIni[MMCSD_BLOCK_SIZE];
static SDMMC_MEMORY uint8_t blkbufSdmmc[MMCSD_BLOCK_SIZE];
static struct {
uint8_t sdmmc[MMCSD_BLOCK_SIZE];
uint8_t prefetch[MMCSD_BLOCK_SIZE];
} sdBuffers SDMMC_MEMORY;

static CCM_OPTIONAL MassStorageController msd(usb_driver);

Expand Down Expand Up @@ -72,8 +76,12 @@ static const scsi_inquiry_response_t sdCardInquiry = {
{'v',CH_KERNEL_MAJOR+'0','.',CH_KERNEL_MINOR+'0'}
};

static BlockCache sdReadPrefetch(sdBuffers.prefetch);

void attachMsdSdCard(BaseBlockDevice* blkdev) {
msd.attachLun(1, blkdev, blkbufSdmmc, &sdCardInquiry, nullptr);
// Start the prefetcher
sdReadPrefetch.start(blkdev);
msd.attachLun(1, reinterpret_cast<BaseBlockDevice*>(&sdReadPrefetch), sdBuffers.sdmmc, &sdCardInquiry, nullptr);

#if EFI_TUNER_STUDIO
// SD MSD attached, enable indicator in TS
Expand Down Expand Up @@ -114,9 +122,9 @@ void initUsbMsd() {
// SRAM, but excluded from the cache
#ifdef STM32H7XX
{
void* base = &blkbufSdmmc;
static_assert(sizeof(blkbufSdmmc) == 512);
uint32_t size = MPU_RASR_SIZE_512;
void* base = &sdBuffers;
static_assert(sizeof(sdBuffers) == 1024);
uint32_t size = MPU_RASR_SIZE_1K;

mpuConfigureRegion(MPU_REGION_4,
base,
Expand All @@ -137,7 +145,7 @@ void initUsbMsd() {
msd.attachLun(0, getRamdiskDevice(), blkbufIni, &iniDriveInquiry, nullptr);

// attach a null device in place of the SD card for now - the SD thread may replace it later
msd.attachLun(1, (BaseBlockDevice*)&ND1, blkbufSdmmc, &sdCardInquiry, nullptr);
msd.attachLun(1, (BaseBlockDevice*)&ND1, sdBuffers.sdmmc, &sdCardInquiry, nullptr);

// start the mass storage thread
msd.start();
Expand Down

0 comments on commit 196718e

Please sign in to comment.