NVM area data backup mechanism

Previous NVM area mainly explained handling precautions, this article explains supplement for data backup method mentioned in Part NVM area.
The main characteristics of the NVM area to be written to non-volatile, can be permanently stored data, generally used to store infrequently modified data, this function is similar to FLASH. Writing data to the NVM region can be divided into three steps: the first step, the original data in the target sector is read out to the RAM; a second step, erasing the target sector data NVM; a third step, the new data and writes the old data in RAM to the sector. Based on the above characteristics of the write operation can be seen, if a write operation NVM area of the second step or the third step when the chip loses power, it will cause NVM area of the original data is lost, and the new data is written to fail, showing NVM area data disorder phenomenon. Although this is only a small probability event, but for the stability and robustness of the product should also consider adding data backup mechanism.
There are two basic scenarios for data backup, respectively, for small amounts of data and large amount of data scenarios. Describes the current small amount of data backup scheme, which requires each update data is less than a sector size minus 1, the sector size of 256 bytes of the present embodiment, updating the maximum amount of data to 255 bytes. (Note: different types of chip NVM sector size vary, you can contact Lingke technical confirmation). The idea is to realize: selecting two regions from the NVM region, the last byte of each record region of the region number of operations. The decision to sign zone data update which should be used. Operate in cycles two regions, a region of operation, when the region 2 acts as a backup role, whereas the same reason.
The first write data
Step: The NVM address region is defined as 0x0000 ~ 0x00FF Block1, will be defined as 0x0200 ~ 0x02FF Block2.
Block1 0x0000 #define
#define Block2 0x0200
Step two: Defining a 256-byte array databuf and a flag variable cnt, cnt assignment and 0.
char DATABUF unsigned [256];
Unsigned char CNT = 0;
The third step: the target data (data to be written) databuf copied to the array, and the copy cnt + 1 to the position databuf + 255.
databuf [255] = cnt + 1 ;
Step IV: WriteNVM calling function writes data to databuf Block1, length of 256 bytes is written. + WriteNVM function call 255 to the position of the write cnt Block2, a length of 1 byte is written;
WriteNVM (Block1, DATABUF, 256);
WriteNVM (Block2 + 255, & cnt, 1);
Update Data
Step: Read functions are invoked ReadNVM Block1 Block2 flag and taking the (last byte).
FLAG1 char unsigned, FLAG2;
ReadNVM (+ Block1 255, & flag1,1);
ReadNVM (Block2 + 255, & flag2,1);
second step: determining the size of the two flags of Block select small values Block update flag . The target data (data to be written) databuf copied to the array, and the bit flag added to 2 copies of the position databuf + 255.
IF (FLAG1> FLAG2)
{
CNT = 2 + FLAG2;
DATABUF [255] = CNT;
WriteNVM (Block2, DATABUF, 256); }
the else {
CNT FLAG1 = +2;
DATABUF [255] = CNT;
WriteNVM (Block1, DATABUF , 256);
}
Read data
step: reading Block1 Block2 of data and flags.
ReadNVM (+ Block1 255, & flag1,1);
ReadNVM (Block2 + 255, & flag2,1);
second step: determination flag bit size, the larger the value of Block new data value within a small Block old data.
IF (FLAG1> FLAG2)
{
ReadNVM (Block1, DATABUF, 255);
} the else {
ReadNVM (Block2, DATABUF, 255);
}

Guess you like

Origin blog.51cto.com/13520299/2427963