1. 完善FlashMem

This commit is contained in:
coffee 2025-12-20 22:11:04 +08:00
parent 6663ede808
commit d6fc4bfd9b
2 changed files with 12 additions and 14 deletions

View File

@ -16,17 +16,12 @@
#define HFLASH_TIMER_ID 0
#endif
// Flash内存大小, 需要4k倍数
#ifndef HFLASH_MEM_SIZE
#define HFLASH_MEM_SIZE (4096 * 3)
#endif
///< 以下IO指令必须实现
enum eHFlashMemIO
{
kHFlashMemIOErase, ///< 请求该地址擦除, 异步非阻塞(addr, cmd)
kHFlashMemIOCheckErase, ///< 检查是否已擦除完成(cmd, uint8_t *status) status设置为0:未擦除, 1:已擦除
kHFlashMemIOSyncErase, ///< 请求该地址擦除, 同步阻塞到完成(addr, cmd)
kHFlashMemIOErase, ///< 请求该地址擦除, 异步非阻塞(addr, cmd, NULL)
kHFlashMemIOCheckErase, ///< 检查是否已擦除完成(0, cmd, uint8_t *status) status设置为0:未擦除, 1:已擦除
kHFlashMemIOSyncErase, ///< 请求该地址擦除, 同步阻塞到完成(addr, cmd, NULL)
};
///< 内存缓存页, 配置需和 (memSize / 4096) 大小一致
@ -44,7 +39,7 @@ typedef struct HFlashMemOpts
{
uint8_t (*read)(uint32_t addr, void *buf, uint32_t len); ///< 读取Flash
uint8_t (*write)(uint32_t addr, const void *buf, uint32_t len); ///< 写入Flash
uint8_t (*ioctl)(uint32_t aadr, uint32_t cmd, void *arg); ///< 控制Flash
uint8_t (*ioctl)(uint32_t aadr, uint32_t cmd, void *arg); ///< 控制Flash, 需要外部实现以上eHFlashMemIO指令
uint8_t *mem; ///< Flash映射内存, 需要4k倍数
HFlashMemCache *cache; ///< 内存缓存
uint16_t memSize; ///< Flash映射内存大小

View File

@ -96,15 +96,18 @@ static void CheckSyncCache()
}
uint16_t needErase = sInfo.useNum;
for (int32_t i = sInfo.useNum - 1; i >= 0; --i) {
uint8_t heat = MAX_HEAT;
for (uint16_t i = 0; i < sInfo.useNum; ++i) {
// 检查哪个页的异步擦除, 现在擦除完成
if (sOpts->cache[i].eraseStatus == kEraseStart) {
sOpts->cache[i].eraseStatus = kWaitWrite;
}
if (needErase == sInfo.useNum) {
// 寻找需要擦除的页
if (sOpts->cache[i].isDirty && sOpts->cache[i].eraseStatus == kEraseWait) {
// 寻找需要擦除的页
if (sOpts->cache[i].isDirty && sOpts->cache[i].eraseStatus == kEraseWait) {
// 查找最低热度的优先擦除
if (sOpts->cache[i].heat < heat) {
heat = sOpts->cache[i].heat;
needErase = i;
}
}
@ -145,7 +148,7 @@ static void StartSyncCache()
sCheckTimer = HTIMER_INVALID;
}
sCheckTimer = HTimerAdd(HFLASH_TIMER_ID, 1, CheckSyncCache, kHTimerLoop);
sCheckTimer = HTimerAdd(HFLASH_TIMER_ID, 0, CheckSyncCache, kHTimerLoop);
// 如果已经在擦除中, 就不需要立刻通知擦除
if (sInfo.needWaitErase) {