前言
许多场合下我们需要掉电保存一些参数,使用EEPROM会增加成本且开发麻烦。利用内部FLASH模拟EEPROM可在节约硬件成本的情况下通过简单的算法很方便地实现同样的功能。
一、为什么需要使用磨损均衡算法
1、EEPROM按字节擦写数据,内部FLASH只能按页擦除,按半字写入; 2、FLASH 单个存储位只能从1变为0,而不能从0变成1; 3、擦除过程就是把所有的存储位都写1; 4、FLASH 擦写次数是有限,一般是 10W 次; 以上FLASH特效决定需要一种比较高效算法,总不能每次更改数值就擦除一次。 本损均衡算法支持存储多个参数,最大限度降低FLASH的擦除次数。
二、算法均衡说明
1、存储结构
1、每个存储单元占用4字节:键(1byte)+值(2byte)+校验和(1byte); 2、键(1byte)除了用来区分参数,也用来分辨存储单元是否为空,所以键值最大为254。
2、读写方案
1、开机遍历FLASH中所有记录,将已有参数读取到数组中备用(存在最后位置的记录为最新记录),若改参数缺失则使用默认参数,同时获得下个可用存储单元的位置; 2、每次更新参数使先修改数组中的参数值,再在FLASH中创建一条最新记录,同时更新下个可用存储单元的位置; 3、若当前页中存储单元用尽,先擦除页,再从起始位置开始新建所有参数的记录; 4、每次读取某个参数使只需到数组中查找即可。
三、算法实现代码
1.头文件
#ifndef __FLASH_H__
#define __FLASH_H__
#include "stdint.h"
#define D_PAGE_SIZE (uint32_t)FLASH_PAGE_SIZE
#define D_PAGE_ADDR(PageNum) (FLASH_BASE + (D_PAGE_SIZE * (PageNum)))
#define D_FLASH_START_ADDR D_PAGE_ADDR(20)
#define D_PARA_NUM 10
void FlashInitPara(uint16_t aDefVal[D_PARA_NUM]);
uint8_t FlashReadPara(uint8_t Key,uint16_t *pVal);
void FlashWritePara(uint8_t Key,uint16_t Val);
#endif
2.源文件
#include "flash.h"
#include "stm32f1xx_hal.h"
#include "debug.h"
extern void FLASH_PageErase(uint32_t PageAddress);
#define D_PARA_KAY_NULL 0XFF
#pragma pack(1)
typedef struct
{
uint8_t Key;
uint16_t Val;
uint8_t Sum;
} ST_DATA_PACK, *PST_DATA_PACK;
#pragma pack()
typedef struct
{
uint8_t Exist;
uint16_t Val;
} ST_PARA_INFO, *PST_PARA_INFO;
static ST_PARA_INFO s_aParaTbl[D_PARA_NUM] = {0};
static uint16_t s_NewWriteIdx = 0;
static void FlashEaserPage(void);
static void CreateRecord(uint8_t Key);
static uint8_t CalcCheckSum(const uint8_t *pData, uint32_t Len);
void FlashInitPara(uint16_t aDefVal[D_PARA_NUM])
{
for(uint16_t i = 0; i < D_PAGE_SIZE / sizeof(ST_DATA_PACK); i++)
{
uint32_t ReadAddr = D_FLASH_START_ADDR + sizeof(ST_DATA_PACK) * i;
PST_DATA_PACK pstPack = (PST_DATA_PACK)ReadAddr;
if(pstPack->Key != D_PARA_KAY_NULL)
{
uint8_t Sum = CalcCheckSum((uint8_t *)pstPack,3);
if(pstPack->Sum == Sum)
{
if(pstPack->Key < D_PARA_NUM)
{
s_aParaTbl[pstPack->Key].Exist = 1;
s_aParaTbl[pstPack->Key].Val = pstPack->Val;
}
}
}
else
{
s_NewWriteIdx = i;
DebugPrintf("init s_NewWriteIdx = %d\n", s_NewWriteIdx);
break;
}
}
for(uint16_t i = 0; i < D_PARA_NUM; i++)
{
if(!s_aParaTbl[i].Exist)
{
s_aParaTbl[i].Exist = 1;
s_aParaTbl[i].Val = aDefVal[i];
CreateRecord(i);
}
else
{
DebugPrintf("init key:%d,Val:0x%x\n", i, s_aParaTbl[i].Val);
}
}
}
static void FlashEaserPage(void)
{
DebugPrintf("%s\n", __func__);
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
FLASH_PageErase(D_FLASH_START_ADDR);
FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
HAL_FLASH_Lock();
}
static void CreateRecord(uint8_t Key)
{
ST_DATA_PACK stPack = {0};
if(s_NewWriteIdx == D_PAGE_SIZE / sizeof(ST_DATA_PACK))
{
FlashEaserPage();
s_NewWriteIdx = 0;
HAL_FLASH_Unlock();
for(uint16_t j = 0; j < D_PARA_NUM; j++)
{
if(s_aParaTbl[j].Exist)
{
stPack.Key = j;
stPack.Val = s_aParaTbl[j].Val;
stPack.Sum = CalcCheckSum((uint8_t *)&stPack,3);
uint32_t Addr = D_FLASH_START_ADDR + sizeof(ST_DATA_PACK) * j;
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Addr, *(uint32_t *)&stPack);
s_NewWriteIdx += 1;
DebugPrintf("s_NewWriteIdx = %d\n", s_NewWriteIdx);
}
}
HAL_FLASH_Lock();
}
else
{
stPack.Key = Key;
stPack.Val = s_aParaTbl[Key].Val;
stPack.Sum = CalcCheckSum((uint8_t *)&stPack,3);
uint32_t Addr = D_FLASH_START_ADDR + sizeof(ST_DATA_PACK) * s_NewWriteIdx;
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Addr, *(uint32_t *)&stPack);
HAL_FLASH_Lock();
s_NewWriteIdx += 1;
DebugPrintf("s_NewWriteIdx = %d\n", s_NewWriteIdx);
}
}
uint8_t FlashReadPara(uint8_t Key, uint16_t *pVal)
{
if(Key < D_PARA_NUM)
{
for(uint16_t i = 0; i < D_PARA_NUM; i++)
{
if((i == Key) && (s_aParaTbl[i].Exist))
{
*pVal = s_aParaTbl[i].Val;
return 1;
}
}
}
return 0;
}
void FlashWritePara(uint8_t Key, uint16_t Val)
{
if(Key < D_PARA_NUM)
{
for(uint16_t i = 0; i < D_PARA_NUM; i++)
{
if(i == Key)
{
s_aParaTbl[i].Val = Val;
s_aParaTbl[i].Exist = 1;
CreateRecord(Key);
break;
}
}
}
}
static uint8_t CalcCheckSum(const uint8_t *pData, uint32_t Len)
{
uint32_t Sum = 0;
const uint8_t *pDataEnd = pData + Len;
while(pData < pDataEnd)
{
Sum += *pData++;
}
return (Sum & 0xFF);
}
void FlashTest(void)
{
uint16_t aDefPara[D_PARA_NUM] = {0x0000, 0x1111, 0x2222, 0x3333, 0x4444,
0x5555, 0x6666, 0x7777, 0x8888, 0x9999};
FlashInitPara(aDefPara);
for(uint16_t i = 0; i < 100; i++)
{
uint16_t Key = i % D_PARA_NUM;
if(Key % 2 == 0)
{
continue;
}
uint16_t Val = 0;
if(FlashReadPara(Key, &Val) > 0)
{
FlashWritePara(Key, Val + 1);
if(FlashReadPara(Key, &Val) > 0)
{
DebugPrintf("after i:%d,key:%d,Val:0x%x\n", i, Key, Val);
}
else
{
DebugPrintf("i = %d,read fail\n", i);
}
}
}
}
总结
以上代码已在STM32F103上多次验证,但仍可能有bug,如有发现请评论留言,谢谢!
|