From d6fc4bfd9befcd176f08535ea0b3f926b269975a Mon Sep 17 00:00:00 2001 From: coffee Date: Sat, 20 Dec 2025 22:11:04 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=AE=8C=E5=96=84FlashMem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/HFlashMem.h | 13 ++++--------- src/HFlashMem.c | 13 ++++++++----- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/include/HFlashMem.h b/include/HFlashMem.h index 923a5c5..b34784d 100644 --- a/include/HFlashMem.h +++ b/include/HFlashMem.h @@ -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映射内存大小 diff --git a/src/HFlashMem.c b/src/HFlashMem.c index c9a8b6c..09358c8 100644 --- a/src/HFlashMem.c +++ b/src/HFlashMem.c @@ -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) {