refactor: SD card SPI HAL (#3957)

* refactor: SD card SPI driver

* Removed SPI and DMA StdPeriph drivers

* Allow for disabling SPI DMA support

* Disable SPI sdcard CRC support

* Fix 2MHz timer on NV14

* Shared VS1053b driver

* Fix: SPI2 uses PCLK1

* feat: new generic diskio API

* More generic SD_CARD_PRESENT()

* Fixes

* Flush incoming block on token mismatch

* Scratch buffer test

* Incredible hack to re-sync sd card

* fix: SDIO driver
This commit is contained in:
Raphael Coeffic
2023-09-23 19:31:56 +10:00
committed by GitHub
parent 870fe7eca3
commit 87df8a4f2c
61 changed files with 2697 additions and 2628 deletions
+37 -32
View File
@@ -24,11 +24,6 @@
#include <string.h>
#if defined(SIMU) && !defined(SIMU_DISKIO)
#define __disk_read(...) (RES_OK)
#define __disk_write(...) (RES_OK)
#endif
#if 0 // set to 1 to enable traces
#include "debug.h"
#define TRACE_DISK_CACHE(...) TRACE(__VA_ARGS__)
@@ -47,15 +42,16 @@ DiskCache diskCache;
class DiskCacheBlock
{
public:
public:
DiskCacheBlock();
bool read(BYTE* buff, DWORD sector, UINT count);
DRESULT fill(BYTE drv, BYTE* buff, DWORD sector, UINT count);
DRESULT fill(const diskio_driver_t* drv, BYTE lun, BYTE* buff, DWORD sector,
UINT count);
void free(DWORD sector, UINT count);
void free();
bool empty() const;
private:
private:
uint8_t data[DISK_CACHE_BLOCK_SIZE];
DWORD startSector;
DWORD endSector;
@@ -77,16 +73,17 @@ 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(const diskio_driver_t* drv, BYTE lun, BYTE * buff,
DWORD sector, UINT count)
{
DRESULT res = __disk_read(drv, data, sector, DISK_CACHE_BLOCK_SECTORS);
DRESULT res = drv->read(lun, data, sector, DISK_CACHE_BLOCK_SECTORS);
if (res != RES_OK) {
return res;
}
startSector = sector;
endSector = sector + DISK_CACHE_BLOCK_SECTORS;
memcpy(buff, data, count * BLOCK_SIZE);
TRACE_DISK_CACHE("\tcache %p FILLED from read(%u, %u)", this, (uint32_t)sector, (uint32_t)count);
TRACE_DISK_CACHE("cache %p FILLED from read(%u, %u)", this, (uint32_t)sector, (uint32_t)count);
return RES_OK;
}
@@ -108,13 +105,17 @@ bool DiskCacheBlock::empty() const
return (endSector == 0);
}
DiskCache::DiskCache():
lastBlock(0)
DiskCache::DiskCache() : lastBlock(0), blocks(nullptr), diskDrv(nullptr)
{
stats.noHits = 0;
stats.noMisses = 0;
stats.noWrites = 0;
}
void DiskCache::initialize(const diskio_driver_t* drv)
{
blocks = new DiskCacheBlock[DISK_CACHE_BLOCKS_NUM];
diskDrv = drv;
}
void DiskCache::clear()
@@ -128,24 +129,28 @@ void DiskCache::clear()
}
}
DRESULT DiskCache::read(BYTE drv, BYTE * buff, DWORD sector, UINT count)
uint32_t DiskCache::getSectors(uint8_t lun)
{
// TODO: check if not caching first sectors would improve anything
// if (sector < 1000) {
// ++stats.noMisses;
// return __disk_read(drv, buff, sector, count);
// }
if (sectors == 0) {
diskDrv->ioctl(lun, GET_SECTOR_COUNT, &sectors);
}
return sectors;
}
DRESULT DiskCache::read(BYTE lun, BYTE * buff, DWORD sector, UINT count)
{
// if read is bigger than cache block, then read it directly without using cache
if (count > DISK_CACHE_BLOCK_SECTORS) {
TRACE_DISK_CACHE("\t\t big read(%u, %u)", (uint32_t)sector, (uint32_t)count);
return __disk_read(drv, buff, sector, count);
TRACE_DISK_CACHE("big read(%u, %u)", (uint32_t)sector, (uint32_t)count);
return diskDrv->read(lun, 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 >= sdGetNoSectors()) {
TRACE_DISK_CACHE("\t\t cache would be beyond end of disk %u (%u)", (uint32_t)sector, sdGetNoSectors());
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 >= getSectors(lun)) {
TRACE_DISK_CACHE("cache would be beyond end of disk %u (%u)",
(uint32_t)sector, getSectors(lun));
return diskDrv->read(lun, buff, sector, count);
}
for (int n = 0; n < DISK_CACHE_BLOCKS_NUM; ++n) {
@@ -161,7 +166,7 @@ DRESULT DiskCache::read(BYTE drv, BYTE * buff, DWORD sector, UINT count)
for (int n = 0; n < DISK_CACHE_BLOCKS_NUM; ++n) {
if (blocks[n].empty()) {
TRACE_DISK_CACHE("\t\t using free block");
return blocks[n].fill(drv, buff, sector, count);
return blocks[n].fill(diskDrv, lun, buff, sector, count);
}
}
@@ -171,16 +176,16 @@ DRESULT DiskCache::read(BYTE drv, BYTE * buff, DWORD sector, UINT count)
lastBlock = 0;
}
return blocks[lastBlock].fill(drv, buff, sector, count);
return blocks[lastBlock].fill(diskDrv, lun, buff, sector, count);
}
DRESULT DiskCache::write(BYTE drv, const BYTE* buff, DWORD sector, UINT count)
DRESULT DiskCache::write(BYTE lun, const BYTE* buff, DWORD sector, UINT count)
{
++stats.noWrites;
for(int n = 0; n < DISK_CACHE_BLOCKS_NUM; ++n) {
blocks[n].free(sector, count);
}
return __disk_write(drv, buff, sector, count);
return diskDrv->write(lun, buff, sector, count);
}
const DiskCacheStats & DiskCache::getStats() const
@@ -195,13 +200,13 @@ int DiskCache::getHitRate() const
return (stats.noHits * 1000) / all;
}
DRESULT disk_read(BYTE drv, BYTE * buff, DWORD sector, UINT count)
DRESULT disk_cache_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_cache_write(BYTE drv, const BYTE * buff, DWORD sector, UINT count)
{
return diskCache.write(drv, buff, sector, count);
}