本文主要是介绍多扇区轮询存储提高FLASH使用寿命,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/********************************************************************************* @brief flash 多扇区轮询存储(用于提高flash使用寿命)******************************************************************************1.数组首位UserData[第一位] 和 UserData[最后一位] 作为数据标识,用户不可使用!*******************************************************************************/
#include "flash_helper.h"#define HelperDBG 0flash_helper_typedef FlashHelperStruct;
uint8_t CurrentSector=0;
uint8_t PreviousSector=0;//读 datalen必须为2的整数倍
void FlashHelperReadDataEx(uint32_t addr,uint8_t *rdata, uint16_t datalen)
{STMFLASH_ReadBytes(addr,rdata,datalen);
}
//写 datalen必须为2的整数倍
void FlashHelperWriteDataEx(uint32_t addr,uint8_t *wdata, uint16_t datalen)
{STMFLASH_WriteBytesNoErase(addr,wdata,datalen);
}
//擦除扇区
void FlashHelperErasePageEx(uint32_t EraseAddress )
{FLASH_Unlock(); //解锁 FLASH_ErasePage(EraseAddress);FLASH_Lock(); //上锁
}//初始化缓存区,获取最近一次存储的数据
void FlashHelperInit(uint32_t Addr_startSector)
{uint8_t ErrCnt=0;uint8_t NextIndex=0;uint8_t RdataBuf[UserDataLen];//读取上次的数据-缓存uint8_t Headerdata[SectorNumMax*2];//扇区头字节uint8_t Taildata[SectorNumMax*2]; //扇区尾字节uint16_t MaxSaveSize; //一个分区最多存几个字节数据 uint16_t SectorMaxTimes; // 一个分区最多存几次数据 uint16_t i,k;FlashHelperStruct.UserData[0] = 0xAA;FlashHelperStruct.UserData[UserDataLen-1] = 0x55; //先写入首尾固定字节数据SectorMaxTimes = (uint16_t)(SECTOR_SIZE/UserDataLen); //计算每个扇区最多可以存几次MaxSaveSize = SectorMaxTimes*UserDataLen; //计算每个扇区最多可以存几个字节的数据#if 0 //擦除全部扇区:仅测试用for(k=0;k<SectorNumMax;k++) {FlashHelperErasePageEx(Addr_startSector + SECTOR_SIZE * k); }#endiffor(k=0;k<SectorNumMax;k++) //读取每个扇区的头尾字节数据{FlashHelperStruct.SectorStartAddr[k] = Addr_startSector + SECTOR_SIZE * k ;FlashHelperReadDataEx(FlashHelperStruct.SectorStartAddr[k],&Headerdata[k*2],2);//读扇区头字节FlashHelperReadDataEx(FlashHelperStruct.SectorStartAddr[k]+(MaxSaveSize-2),&Taildata[k*2],2); //读扇区尾字节#if HelperDBGLogOutD("Headerdata[%d]=%02x,%02x ,%02x,%02x\r\n",k,Headerdata[k*2],Headerdata[k*2+1],Taildata[k*2],Taildata[k*2+1]);#endif}for(k=0;k<SectorNumMax;k++) //获取当前数据应当存储的所在扇区位置 并获取上一次数据{if((Headerdata[k*2]==0xAA) && (Taildata[k*2+1] == 0xFF)) {for(i=0;i< SectorMaxTimes;i++){FlashHelperStruct.CurrentOptionAddr = FlashHelperStruct.SectorStartAddr[k]+i*UserDataLen;FlashHelperReadDataEx(FlashHelperStruct.CurrentOptionAddr,RdataBuf,UserDataLen);if(RdataBuf[0]==0xFF && RdataBuf[UserDataLen-1]==0xFF){break;}memcpy(FlashHelperStruct.UserData,RdataBuf,UserDataLen);//听说memcpy比for效率高//for(j=0;j<UserDataLen;j++){FlashHelperStruct.UserData[j] = RdataBuf[j];} } break; //找到了正确位置 提前退出循环}else if(Headerdata[k*2]==0xAA && Taildata[k*2+1] == 0x55)// 扇区正好存满?{NextIndex = k+1;if(NextIndex==SectorNumMax) NextIndex = 0;if((Headerdata[NextIndex*2]==0xAA) && (Taildata[NextIndex*2+1] == 0xFF)) //检查到下一个扇区有数据{for(i=0;i< SectorMaxTimes;i++){FlashHelperStruct.CurrentOptionAddr = FlashHelperStruct.SectorStartAddr[NextIndex]+i*UserDataLen;FlashHelperReadDataEx(FlashHelperStruct.CurrentOptionAddr,RdataBuf,UserDataLen);if(RdataBuf[0]==0xFF && RdataBuf[UserDataLen-1]==0xFF){break;}memcpy(FlashHelperStruct.UserData,RdataBuf,UserDataLen);// }}else // 当前扇区刚好存满{FlashHelperReadDataEx(FlashHelperStruct.SectorStartAddr[k]+(SectorMaxTimes-1)*UserDataLen,RdataBuf,UserDataLen);memcpy(FlashHelperStruct.UserData,RdataBuf,UserDataLen);FlashHelperStruct.CurrentOptionAddr = FlashHelperStruct.SectorStartAddr[NextIndex]; //把下一个扇区的起始地址作为当前存储地址} break; //找到了正确位置 提前退出循环}else //读出来里面啥也没有{ErrCnt++;}}if(ErrCnt==SectorNumMax) //用到的扇区都 没有有效信息{FlashHelperStruct.CurrentOptionAddr = FlashHelperStruct.SectorStartAddr[0]; //从第一个扇区开始存for(k=0;k<SectorNumMax;k++){FlashHelperErasePageEx(FlashHelperStruct.SectorStartAddr[k]); //擦除所有扇区#if HelperDBGLogOutD("no data FlashHelperErasePageEx \r\n" ); #endif}}CurrentSector = (uint32_t)((FlashHelperStruct.CurrentOptionAddr - HelperStartAddress) / SECTOR_SIZE);PreviousSector = CurrentSector;#if HelperDBGLogOutD("CurrentOptionAddr = %04x\r\n" ,FlashHelperStruct.CurrentOptionAddr);#endif
}//更新数据存储
void FlashHelperUpdate(void)
{uint8_t NewCurrentSector = 0;if(FlashHelperStruct.CurrentOptionAddr < HelperStartAddress ) return;if(FlashHelperStruct.CurrentOptionAddr > (HelperStartAddress +SECTOR_SIZE*SectorNumMax)) return;// 判断计算当前处于哪个扇区CurrentSector = (uint32_t)((FlashHelperStruct.CurrentOptionAddr - HelperStartAddress) / SECTOR_SIZE);#if HelperDBGLogOutD("CurrentAddr = %04x,CurrentSector = %d \r\n" ,FlashHelperStruct.CurrentOptionAddr,CurrentSector); #endifif(CurrentSector>=SectorNumMax){NewCurrentSector = 0;//扇区超出范围 回到起点PreviousSector = NewCurrentSector;FlashHelperStruct.CurrentOptionAddr = FlashHelperStruct.SectorStartAddr[NewCurrentSector];FlashHelperWriteDataEx(FlashHelperStruct.CurrentOptionAddr,FlashHelperStruct.UserData,UserDataLen); //储存数据FlashHelperErasePageEx(FlashHelperStruct.SectorStartAddr[CurrentSector-1]); //存完后擦除上一个扇区 }else //判断扇区存满没有{if(PreviousSector!=CurrentSector){FlashHelperStruct.CurrentOptionAddr = FlashHelperStruct.SectorStartAddr[CurrentSector];//存储到下一个扇区FlashHelperWriteDataEx(FlashHelperStruct.CurrentOptionAddr,FlashHelperStruct.UserData,UserDataLen); //储存数据FlashHelperErasePageEx(FlashHelperStruct.SectorStartAddr[PreviousSector]); //存完后擦除上一个扇区PreviousSector = CurrentSector;}else{FlashHelperWriteDataEx(FlashHelperStruct.CurrentOptionAddr,FlashHelperStruct.UserData,UserDataLen);} }FlashHelperStruct.CurrentOptionAddr += (UserDataLen);//存完后地址自增
}//调用完刷新函数以后需要立即调用此函数,才是读出的刚刚存入的真实数据
uint8_t FlashHelperGetRecentData(uint8_t *buff)
{uint8_t ret = 0;uint8_t rdata[UserDataLen];FlashHelperReadDataEx(FlashHelperStruct.CurrentOptionAddr-UserDataLen,rdata,UserDataLen);memcpy(buff,rdata,UserDataLen);return ret;
}
#ifndef FLASH_HELPER_H_
#define FLASH_HELPER_H_#include <stdio.h>
#include "main.h"#define UserDataLen 16 //单次存储数据的数组长度,包含头尾和校验字节
#define SectorNumMax 5 //总共几块分区
#define HelperStartAddress 0x08020000 //数据存储起始扇区地址typedef struct flash_helper
{uint8_t UserData[UserDataLen];/*需要存储的数据缓存在此数组*/uint32_t SectorStartAddr[SectorNumMax];/*保存数据用的每个分区的首地址*/uint32_t CurrentOptionAddr;/*记录当前操作的地址*/} flash_helper_typedef;extern flash_helper_typedef FlashHelperStruct;void FlashHelperUpdate(void);
void FlashHelperInit(uint32_t Addr_startSector);
uint8_t FlashHelperGetRecentData(uint8_t *buff);#endif
本文代码基于此文进行修改优化
原文点这里
https://www.cnblogs.com/yangfengwu/p/13616968.html?ivk_sa=1026860b
这篇关于多扇区轮询存储提高FLASH使用寿命的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!