stm32flasheeprom断电保护数值存储程序Word下载.docx
《stm32flasheeprom断电保护数值存储程序Word下载.docx》由会员分享,可在线阅读,更多相关《stm32flasheeprom断电保护数值存储程序Word下载.docx(24页珍藏版)》请在冰豆网上搜索。
![stm32flasheeprom断电保护数值存储程序Word下载.docx](https://file1.bdocx.com/fileroot1/2022-11/28/aa6515dd-5f5d-4f06-8d1c-134dd31437e0/aa6515dd-5f5d-4f06-8d1c-134dd31437e01.gif)
h2>
<
center>
&
copy;
COPYRIGHT2009STMicroelectronics<
/center>
/h2>
*/
/*Definetopreventrecursiveinclusion-------------------------------------*/
#ifndef__EEPROM_H
#define__EEPROM_H
/*Includes------------------------------------------------------------------*/
#include"
stm32f10x.h"
/*Exportedconstants--------------------------------------------------------*/
/*DefinetheSTM32F10XxxFlashpagesizedependingontheusedSTM32device*/
#ifdefined(STM32F10X_LD)||defined(STM32F10X_MD)
#definePAGE_SIZE(uint16_t)0x400/*Pagesize=1KByte*/
#elifdefined(STM32F10X_HD)||defined(STM32F10X_CL)
#definePAGE_SIZE(uint16_t)0x800/*Pagesize=2KByte*/
#endif
/*EEPROMstartaddressinFlash*/
#defineEEPROM_START_ADDRESS((uint32_t)0x08010000)/*EEPROMemulationstartaddress:
after64KByteofusedFlashmemory*/
/*Pages0and1baseandendaddresses*/
#definePAGE0_BASE_ADDRESS((uint32_t)(EEPROM_START_ADDRESS+0x000))
#definePAGE0_END_ADDRESS((uint32_t)(EEPROM_START_ADDRESS+(PAGE_SIZE-1)))
#definePAGE1_BASE_ADDRESS((uint32_t)(EEPROM_START_ADDRESS+PAGE_SIZE))
#definePAGE1_END_ADDRESS((uint32_t)(EEPROM_START_ADDRESS+(2*PAGE_SIZE-1)))
/*UsedFlashpagesforEEPROMemulation*/
#definePAGE0((uint16_t)0x0000)
#definePAGE1((uint16_t)0x0001)
/*Novalidpagedefine*/
#defineNO_VALID_PAGE((uint16_t)0x00AB)
/*Pagestatusdefinitions*/
#defineERASED((uint16_t)0xFFFF)/*PAGEisempty*/
#defineRECEIVE_DATA((uint16_t)0xEEEE)/*PAGEismarkedtoreceivedata*/
#defineVALID_PAGE((uint16_t)0x0000)/*PAGEcontainingvaliddata*/
/*Validpagesinreadandwritedefines*/
#defineREAD_FROM_VALID_PAGE((uint8_t)0x00)
#defineWRITE_IN_VALID_PAGE((uint8_t)0x01)
/*Pagefulldefine*/
#definePAGE_FULL((uint8_t)0x80)
/*Variables'
number*/
#defineNumbOfVar((uint8_t)0x03)
/*Exportedtypes------------------------------------------------------------*/
/*Exportedmacro------------------------------------------------------------*/
/*Exportedfunctions-------------------------------------------------------*/
uint16_tEE_Init(void);
uint16_tEE_ReadVariable(uint16_tVirtAddress,uint16_t*Data);
uint16_tEE_WriteVariable(uint16_tVirtAddress,uint16_tData);
#endif/*__EEPROM_H*/
/*******************(C)COPYRIGHT2009STMicroelectronics*****ENDOFFILE****/
/*************eeprom.c*************/
*@fileEEPROM_Emulation/src/eeprom.c
*@briefThisfileprovidesalltheEEPROMemulationfirmwarefunctions.
*/
/**@addtogroupEEPROM_Emulation
*@{
eeprom.h"
/*Privatetypedef-----------------------------------------------------------*/
/*Privatedefine------------------------------------------------------------*/
/*Privatemacro-------------------------------------------------------------*/
/*Privatevariables---------------------------------------------------------*/
/*Globalvariableusedtostorevariablevalueinreadsequence*/
uint16_tDataVar=0;
uint32_tCurWrAddress;
/*Virtualaddressdefinedbytheuser:
0xFFFFvalueisprohibited*/
uint16_tVirtAddVarTab[NumbOfVar];
/*Privatefunctionprototypes-----------------------------------------------*/
/*Privatefunctions---------------------------------------------------------*/
staticFLASH_StatusEE_Format(void);
staticuint16_tEE_FindValidPage(uint8_tOperation);
staticuint16_tEE_VerifyPageFullWriteVariable(uint16_tVirtAddress,uint16_tData);
staticuint16_tEE_PageTransfer(uint16_tVirtAddress,uint16_tData);
//初始化写地址,减少每次读写时查询时间
uint16_tInitCurrWrAddress(void)
{
FLASH_StatusFlashStatus=FLASH_COMPLETE;
uint16_tValidPage=PAGE0;
//uint32_tAddress;
uint32_tPageEndAddress;
/*GetvalidPageforwriteoperation*/
ValidPage=EE_FindValidPage(WRITE_IN_VALID_PAGE);
/*Checkifthereisnovalidpage*/
if(ValidPage==NO_VALID_PAGE)
{
CurWrAddress=(uint32_t)(EEPROM_START_ADDRESS+(uint32_t)(ValidPage*PAGE_SIZE));
returnNO_VALID_PAGE;
}
/*GetthevalidPagestartAddress*/
//Address=(uint32_t)(EEPROM_START_ADDRESS+(uint32_t)(ValidPage*PAGE_SIZE));
/*GetthevalidPageendAddress*/
PageEndAddress=(uint32_t)((EEPROM_START_ADDRESS-2)+(uint32_t)((1+ValidPage)*PAGE_SIZE));
/*Checkeachactivepageaddressstartingfrombegining*/
while(CurWrAddress<
PageEndAddress)
/*VerifyifAddressandAddress+2contentsare0xFFFFFFFF*/
if((*(__IOuint32_t*)CurWrAddress)==0xFFFFFFFF)
/*Setvariablevirtualaddress*/
FlashStatus=FLASH_COMPLETE;
/*Returnprogramoperationstatus*/
returnFlashStatus;
else
/*Nextaddresslocation*/
CurWrAddress=CurWrAddress+4;
/*ReturnPAGE_FULLincasethevalidpageisfull*/
returnPAGE_FULL;
}
*@briefRestorethepagestoaknowngoodstateincaseofpage'
sstatus
*corruptionafterapowerloss.
*@paramNone.
*@retval-Flasherrorcode:
onwriteFlasherror
*-FLASH_COMPLETE:
onsuccess
uint16_t__EE_Init(void)
uint16_tPageStatus0=6,PageStatus1=6;
uint16_tVarIdx=0;
uint16_tEepromStatus=0,ReadStatus=0;
int16_tx=-1;
uint16_tFlashStatus;
/*GetPage0status*/
PageStatus0=(*(__IOuint16_t*)PAGE0_BASE_ADDRESS);
/*GetPage1status*/
PageStatus1=(*(__IOuint16_t*)PAGE1_BASE_ADDRESS);
/*Checkforinvalidheaderstatesandrepairifnecessary*/
switch(PageStatus0)
caseERASED:
if(PageStatus1==VALID_PAGE)/*Page0erased,Page1valid*/
/*ErasePage0*/
FlashStatus=FLASH_ErasePage(PAGE0_BASE_ADDRESS);
/*Iferaseoperationwasfailed,aFlasherrorcodeisreturned*/
if(FlashStatus!
=FLASH_COMPLETE)
elseif(PageStatus1==RECEIVE_DATA)/*Page0erased,Page1receive*/
/*MarkPage1asvalid*/
FlashStatus=FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS,VALID_PAGE);
/*Ifprogramoperationwasfailed,aFlasherrorcodeisreturned*/
else/*FirstEEPROMaccess(Page0&
1areerased)orinvalidstate->
formatEEPROM*/
/*ErasebothPage0andPage1andsetPage0asvalidpage*/
FlashStatus=EE_Format();
/*Iferase/programoperationwasfailed,aFlasherrorcodeisreturned*/
break;
caseRECEIVE_DATA:
if(PageStatus1==VALID_PAGE)/*Page0receive,Page1valid*/
/*TransferdatafromPage1toPage0*/
for(VarIdx=0;
VarIdx<
NumbOfVar;
VarIdx++)
if((*(__IOuint16_t*)(PAGE0_BASE_ADDRESS+6))==VirtAddVarTab[VarIdx])
x=VarIdx;
if(VarIdx!
=x)
/*Readthelastvariables'
updates*/
ReadStatus=EE_ReadVariable(VirtAddVarTab[VarIdx],&
DataVar);
/*Incasevariablecorrespondingtothevirtualaddresswasfound*/
if(ReadStatus!
=0x1)
/*TransferthevariabletothePage0*/
EepromStatus=EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx],DataVar);
if(EepromStatus!
returnEepromStatus;
/*MarkPage0asvalid*/
FlashStatus=FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS,VALID_PAGE);
/*ErasePage1*/
FlashStatus=FLASH_ErasePage(PAGE1_BASE_ADDRESS);
elseif(PageStatus1==ERASED)/*Page0receive,Page1erased*/
FlashStatus=FLA