1、h2© COPYRIGHT 2009 STMicroelectronics/h2 */* Define to prevent recursive inclusion -*/#ifndef _EEPROM_H#define _EEPROM_H/* Includes -*/#include stm32f10x.h/* Exported constants -*/* Define the STM32F10Xxx Flash page size depending on the used STM32 device */#if defined (STM32F10X_LD) | defined
2、(STM32F10X_MD) #define PAGE_SIZE (uint16_t)0x400 /* Page size = 1KByte */#elif defined (STM32F10X_HD) | defined (STM32F10X_CL) #define PAGE_SIZE (uint16_t)0x800 /* Page size = 2KByte */#endif/* EEPROM start address in Flash */#define EEPROM_START_ADDRESS (uint32_t)0x08010000) /* EEPROM emulation sta
3、rt address: after 64KByte of used Flash memory */* Pages 0 and 1 base and end addresses */#define PAGE0_BASE_ADDRESS (uint32_t)(EEPROM_START_ADDRESS + 0x000)#define PAGE0_END_ADDRESS (uint32_t)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1)#define PAGE1_BASE_ADDRESS (uint32_t)(EEPROM_START_ADDRESS + PAGE_SI
4、ZE)#define PAGE1_END_ADDRESS (uint32_t)(EEPROM_START_ADDRESS + (2 * PAGE_SIZE - 1)/* Used Flash pages for EEPROM emulation */#define PAGE0 (uint16_t)0x0000)#define PAGE1 (uint16_t)0x0001)/* No valid page define */#define NO_VALID_PAGE (uint16_t)0x00AB)/* Page status definitions */#define ERASED (uin
5、t16_t)0xFFFF) /* PAGE is empty */#define RECEIVE_DATA (uint16_t)0xEEEE) /* PAGE is marked to receive data */#define VALID_PAGE (uint16_t)0x0000) /* PAGE containing valid data */* Valid pages in read and write defines */#define READ_FROM_VALID_PAGE (uint8_t)0x00)#define WRITE_IN_VALID_PAGE (uint8_t)0
6、x01)/* Page full define */#define PAGE_FULL (uint8_t)0x80)/* Variables number */#define NumbOfVar (uint8_t)0x03)/* Exported types -*/* Exported macro -*/* Exported functions - */uint16_t EE_Init(void);uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);uint16_t EE_WriteVariable(uint16_t V
7、irtAddress, uint16_t Data);#endif /* _EEPROM_H */* (C) COPYRIGHT 2009 STMicroelectronics *END OF FILE*/*eeprom.c*/ * file EEPROM_Emulation/src/eeprom.c * brief This file provides all the EEPROM emulation firmware functions. */ /* addtogroup EEPROM_Emulation * eeprom.h/* Private typedef -*/* Private
8、define -*/* Private macro -*/* Private variables -*/* Global variable used to store variable value in read sequence */uint16_t DataVar = 0;uint32_t CurWrAddress;/* Virtual address defined by the user: 0xFFFF value is prohibited */uint16_t VirtAddVarTabNumbOfVar;/* Private function prototypes -*/* Pr
9、ivate functions -*/static FLASH_Status EE_Format(void);static uint16_t EE_FindValidPage(uint8_t Operation);static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data);static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data);/初始化写地址,减少每次读写时查询时间uint16_t InitCurrWrAd
10、dress(void) FLASH_Status FlashStatus = FLASH_COMPLETE; uint16_t ValidPage = PAGE0; /uint32_t Address; uint32_t PageEndAddress; /* Get valid Page for write operation */ ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE); /* Check if there is no valid page */ if (ValidPage = NO_VALID_PAGE) CurWrAddress
11、 = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE); return NO_VALID_PAGE; /* Get the valid Page start Address */ /Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE); /* Get the valid Page end Address */ PageEndAddress = (uint32_t)(EEPROM_START_ADDRESS -
12、 2) + (uint32_t)(1 + ValidPage) * PAGE_SIZE); /* Check each active page address starting from begining */ while (CurWrAddress format EEPROM */ /* Erase both Page0 and Page1 and set Page0 as valid page */ FlashStatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is retur
13、ned */ break; case RECEIVE_DATA: if (PageStatus1 = VALID_PAGE) /* Page0 receive, Page1 valid */ /* Transfer data from Page1 to Page0 */ for (VarIdx = 0; VarIdx NumbOfVar; VarIdx+) if ( *(_IO uint16_t*)(PAGE0_BASE_ADDRESS + 6) = VirtAddVarTabVarIdx) x = VarIdx; if (VarIdx != x) /* Read the last varia
14、bles updates */ ReadStatus = EE_ReadVariable(VirtAddVarTabVarIdx, &DataVar); /* In case variable corresponding to the virtual address was found */ if (ReadStatus != 0x1) /* Transfer the variable to the Page0 */ EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTabVarIdx, DataVar); if (EepromStatus ! return EepromStatus; /* Mark Page0 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE); /* Erase Page1 */ FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS); else if (PageStatus1 = ERASED) /* Page0 receive, Page1 erased */ FlashStatus = FLA
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1