eeprom in flash
introduce
This code implements the simple flash simulation eeprom function. You can call the eeprom interface at the top level by simply implementing the underlying flash operation interface. Suitable for projects with less eeprom data storage
principle
The flash space provided to eeprom is divided into two partitions, each partition can contain multiple erase pages, which are erased uniformly during erase. Which partition is currently used is determined by the beginning USED_MARK. The actual data and the eeprom virtual address form a programming unit. When writing, the new data is written in sequence, and when reading, the last value is the valid value. When one partition is full, data is copied to another partition.
Transplant instructions
- Copy eeprom_in_flash.c and eeprom_in_flash.h to the project
- Implement the underlying calling interface of EE_ErasePart, EE_ProgramWord, and EE_ReadWord. You can refer to the eeprom_port example
- Modify the macro definition in eeprom_in_flash.h, configure flash address size and other related parameters
- EEPROM_NUM_MAX: The maximum number of eeprom data, the unit is the number of 16 bit data. The optional range is limited by the minimum partition size and must be less than (EEPROM_PART_SIZE_min/4-1)
- EEPROM_PART0_SIZE/EEPROM_PART1_SIZE: The size of two partitions, which can be of different sizes. Each partition can contain multiple erased pages, which is an integer multiple of the minimum erased page.
- EEPROM_START_ADDRESS: Used to simulate the flash start address of eeprom
Instructions for use
//参数:eeprom初始化值,NULL则无初始值
int EEPROM_Init ( void * default_data );
int EEPROM_Format ( void * default_data );
//参数Address:eeprom的地址,一个地址保存16bit数据,范围0 - (EEPROM_NUM_MAX-1)
//参数length:读写buf的长度,为16bit数据的个数
uint16_t EEPROM_Read ( uint16_t Address );
int EEPROM_Write ( uint16_t Address , uint16_t Data );
int EEPROM_Read_Buf ( uint16_t Address , uint16_t * buf , uint16_t length );
int EEPROM_Write_Buf ( uint16_t Address , uint16_t * buf , uint16_t length );
//参数addr:eeprom存储空间的地址,单位byte,与上面eeprom的参数呈2倍关系,地址空间不可重复,必须2字节对齐,范围0 - (EEPROM_NUM_MAX-1)*2
//参数length:读写buf的长度,单位字节长度,必须2字节对齐
int Config_Read_Buf ( uint16_t addr , void * buf , uint16_t length );
int Config_Write_Buf ( uint16_t addr , void * buf , uint16_t length );
Limitations of use
- The minimum programming unit is 32bit or below
- There are at least two erasable pages for eeprom to use
- The maximum amount of eeprom stored is less than one-quarter of the available flash space
- The clear value after erasing is 0xff
Features
- Very low resource share, suitable for small microcontroller projects
- Support flash erasing programming wear balance
- Supports safe power outage at any time , with high reliability
- All data is buffered in memory, and the read and write speed is fast
- The update program does not affect the stored content. It can be updated incrementally or you can choose not to use old data.
- Supports log printing interface for easy debugging
- Supports 8-bit microcontroller
- Data correctness verification is not supported
Things to note
- When calling eeprom read and write interface, pay attention to the address and length range, and do not cover different data address ranges.
- Flash address programming interface EE_ProgramWord. If 32-bit writes to atomic operations, you must first write 16 bits lower and then write 16 bits higher and 16 bits higher.