The present disclosure relates to data storage, and particular to a method and a device for saving data.
Currently, a variety of embedded devices are popular in industrial applications and consumer electronics. Usually, in an embedded device, besides the volatile RAM (Random Access Memory), e.g., SRAM, DRAM, SRAM and the like, for caching the parameters required during the operation of the processor/controller, there is a Non-Volatile RAM (NVRAM), such as EEPROM so as to save data/parameters permanently upon power-off. In order to reduce the cost of the embedded product, it has been proposed to emulate the NVRAM by using a Flash memory, such as a NOR Flash, so that all the data in the volatile RAM can be backup in the NOR flash rather than in the NVRAM, such as EEPROM.
Although there are many solutions to emulate NVRAM, such solutions have common disadvantages, for example, it is difficult for them to emulate the NVRAM with a big size due to the complex logic or unreliable performance.
Further, the existing solution can not be adapted to the requirements on the NVRAM emulation with different sizes and/or to the different types of Flash memory flexibly, either due to the complex logic for writing/reading or to the high risk introduced to the product.
Moreover, the existing solution either takes no consideration of balancing the wear level of respective sectors of the Flash memory or uses a complicated algorithm to roughly equalize the erasing of the Flash sectors.
According to one aspect of the present disclosure, it is provided a method for backing up data stored in a portion of a volatile memory, comprising: allocating, in a Flash memory, two memory zones with equal size alternately working as active memory zone and backup memory zone, wherein each memory zone comprises a basic data region, a header region and an update data region; a size of the basic data region is equal to a size of the portion of the volatile memory; when switching between the two memory zones for working as active memory zone, storing a copy of the data in the portion of the volatile memory into the basic data region of the active memory zone and storing integrity data for the data stored in the basic data region in the header region; and if there is a change to data in the portion of the volatile memory, storing the change in the update data region of the active memory zone.
According to another aspect of the present disclosure, there is provided a device for saving data in a portion of a volatile RAM, wherein the device comprises: a flash memory; the volatile RAM; and a processor that is used for allocating, in the Flash memory, two memory zones with equal size alternately working as active memory zone and backup memory zone, wherein each memory zone comprises a basic data region, a header region and an update data region; a size of the basic data region is equal to a size of the portion of the volatile RAM; storing, during a switch between the two memory zones for working as active memory zone, a copy of the data in the portion of the volatile RAM in the active memory zone and storing CRC codes for the data stored in the basic data region in the header region; and if there is a change to data in the portion of the volatile RAM, storing the change in the update data region of the active memory zone.
According to another aspect of the present disclosure, there is provided a computer readable medium storing program code instructions which cause a computer to implement the method for saving data from a volatile RAM as described above.
These and other aspects, features and advantages of the present disclosure will become apparent from the following detailed description of exemplary embodiments, which is to be read in connection with the accompanying drawings.
The present disclosure may be better understood when reading the description as follows with reference to the following accompanying figures, in which:
To illustrate the technical solutions of the present disclosure clearly and fully, hereinafter, detailed description will be made to the embodiments of the present disclosure with reference to the accompanying drawings. It should be appreciated that those skilled in the art will be able to devise various arrangements that, although are not explicitly described or illustrated herein, embody an inventive concept of the present disclosure and thus are intended to fall into the scope of the present disclosure.
In the present disclosure, it is proposed a solution for backing up the data stored or existing in a volatile RAM according to the present principle, which can be applied to emulate a NVRAM by using a Flash memory, so as to reduce the cost of NVRAM without introducing complex logic.
According to an inventive concept of the present principle, a Flash memory is adopted to emulate a NVRAM such as EEPROM so as to save the data stored in a volatile RAM permanently upon the power-off. In particular, the Flash memory is divided into at least two memory zones, for example, memory zone A, memory zone B and memory zone C with equal size, wherein one of the memory zones A, B, C can be selected as an active memory zone, others are set as backup memory zones; the active memory zone is switched among the memory zones A, B, and C by turns.
Optionally, as illustrated in
Optionally, the basic data region has a size equal to the size of the volatile RAM (i.e., the size of the NVRAM to be emulated), and maintains a static image of the volatile RAM. When the active memory zone is switched, all the data in the volatile RAM is copied to the basic data region of the memory zone which is selected as the active memory zone.
Optionally, the header region keeps CRC (Cyclic Redundancy Check) codes and metadata for the data saved in the basic data region, wherein the CRC codes are used to keep the integrity of the data saved in the basic data region and in the header (the basic data region and the header generally traverse 2 or more sectors in the flash), and the metadata records information on the performance of the memory zone, for example, the metadata can be used for recording the number of times for which the sectors of memory zone have been erased. In an example, the size of the header region is 32 bytes. Naturally, the size of the header region can be adaptive according to the actual project requirements.
Optionally, the update data region keeps a dynamic image of the volatile RAM; in other words, after a memory zone is selected as a current active memory zone, if there is a change to the data in the volatile RAM, the data subjected to the change (i.e. the data describing the change) is recorded in the update data region of the current active memory zone. Herein, in an example, when a user makes a change, the device may write data or value of parameter(s) to the RAM or change data in the RAM. The device checks if the new data or value is the same as the old value in the RAM. If yes, there is no change. If not, the new data or value is written to the RAM, and a data record describing the new data is stored in the update data region. Below is an example of data structure of the data record.
Optionally, the size of the update data region can be determined and set based on the project requirements, such as, the number of the backup memory zones, the number of times for which the sector of the Flash memory can be erased, the frequency for which the data in RAM is modified, the size of the data to be modified at a time, or the like.
As illustrated in
Optionally, the offset field indicates the address offset from a basic address of the volatile RAM, the size field indicates the size of data which is modified at a time, the data field indicates the modified data, and the CRC field is used to keep the integrity of the modified data.
Dependent on the actual situation, the data structure utilized in the update data region can further include a padding field, and the padding field is intended to adapt to different types of Flash. For example, most of Flash only supports four-bytes writing, if the size of the data to be written into the Flash is not the multiple of the four-bytes, the padding data in the padding field can be used to align the end of the data with the four-byte border. In such a case, the padding field can be 0 to 3 bytes. The utilization of the padding field improves the compatibility of the present principle with the different types of Flash. Optionally, data 0xFF can be used as the padding data in the padding field.
Optionally, the data structure can include ‘N’ field and ‘V’ field, and such two fields indicates fixed values for char ‘N’ and ‘V’. They help the API to quickly determine whether the subsequent data record is valid. If the data in the fields “N” or “V” is incorrect, it can be directly determined that the subsequent data record is invalid without resorting the CRC field in the data structure to judge the validation of the data record.
According to one aspect of the present principle, it is provided a method for saving data from a volatile RAM. As illustrated in
Hereinafter, a case in which the Flash memory is divided into two memory zones, i.e., the memory zone A and the memory zone B, is taken as an example for illustrating the present principle. The case in which the Flash is divided into more than two memory zones is similar to the case of two memory zones, and the difference is mainly in that more than one memory zone can be used as the backup memory zones, thus the details thereof being omitted for the purpose of concise and clarity.
For the two memory zones, all the sectors in one memory zone need to be erased together, and the data in one memory zone should be kept integrate before erasing the other one, thus ensuring the safety and integrity of the data. In such a way, it avoids the bugs due to the erasing of data crosses Flash sector border, and thus keeps the logic to be implemented clear and easy; further, only one of the two memory zones is activated as the active memory zone, and the other one is inactive, and both memory zones back up for each other.
Further, for the reading/writing operation for the data in the RAM, the present principle uses one mutex in API, thus avoiding the issues due to the internal thread and semaphores normally used in the known solution, such as complex logic, the unreliable data storage, and the like.
As mentioned above, compared to the known embedded device using a NVRAM, such as EEPROM, to save the data permanently upon power-off, the solution of the present principle does not use a physical NVRAM but uses partial space in Flash, such as NOR Flash, to emulate the NVRAM to reduce the cost.
According to the method illustrated in
For the purpose of simplifying the description, assuming that at the very beginning, original data, e.g., parameters are stored in the memory Zone A, and the memory zone A is selected as the active memory zone; during an initialization of the RAM, the parameters are loaded from the memory zone A to the RAM. After that, upon detection of a change to data in the RAM, the changed data is recorded in the memory zone A until there is not enough free space to record the data; then, the active memory zone is switched from the memory zone A to the memory zone B, that is, the memory zone B is activated as the active memory zone, and all the data in the RAM at this point are copied to the memory zone B as the static mapping of the RAM, and the memory zone B begins to record the subsequent change to the data of the RAM as the dynamic mapping of the RAM, while the memory zone A is erased. A similar process is conducted if the memory zone B has no enough free space for recording the changed data from the RAM.
By using the memory zone to record the changed data (e.g. by adding a data record describing the change in the update data field) upon detection of a change to the data in the RAM instead of all the data in the RAM, the storage space in the memory zone can be saved and the recording speed can be improved; meanwhile, by using the static mapping of the RAM and the dynamic mapping of the RAM, all the current data in the RAM is saved in the Flash memory.
Further, the memory zone A and memory zone B are backup for each other to save the data from the RAM alternatively, so that all the sectors in the Flash memory will have the equal erasing opportunity, and the wearing level is balanced among all the sectors without complex logic, thus extending the lifespan of the Flash.
As mentioned above, each of memory zones has three regions, i.e., a basic data region, a header region and an update data region, wherein the size of the basic data region is equal the size of the RAM, and the header region keeps CRC (Cyclic Redundancy Check) codes and metadata for the data saved in the basic data region.
Optionally, as illustrated in
As discussed above, the CRC field in the data structure of the update data region can be used to determine whether the data record in the update data region is correct. Further, the fields ‘N’ and ‘V’ can also be used to assist determining whether the scanned data record is correct, that is, if the data in the fields “N” or “V” is incorrect, it can be directly determined that the scanned data record is invalid without checking CRC any more.
For the process of reading from/writing into the RAM, the present principle proposes creating only one mutex in API to avoid the conflict and ensure operation safety during multi-task access. Only if the mutex is obtained, it is possible for API to read/write data from/into the RAM.
Optionally, in order to reduce the number of times for which the sectors of the Flash are erased, the principle proposes that after the API writes a data block into the RAM, the API scans the data block to determine whether the data block has an identical portion to the previous data block at the same location in the RAM. In particular, the API scans the data block from the beginning of the data block to determine a starting position at which data is modified, and scans the data block from the ending of the data block to determine an ending position at which data is modified; then, only a data portion between the starting position and the ending position needs to be written to the update data region of the active memory zone. In such a way, the space of the update date region can be saved, and thus the number of times for which the memory zone is erased can be reduced, extending the lifespan of the Flash.
Although the respective data process are illustrated in
Optionally, in order to adapt to emulate a variety of NVRAM with different sizes by different types of Flash more flexibly, the setting parameters, such as the parameters concerning the size of the NVRAM to be emulated, the size of the update data region of the memory zone, the starting address of the basic data region of the memory zone in the Flash, the sector size of the Flash and the like, can be defined as macro parameters so as to improve the flexibility and compatibility of the application of the present principle.
According to the present principle, a Flash memory such as NOR Flash, can be adopted to emulate the NVRAM such as EEPROM with simple and reliable logic, and it is possible to remove the NVRAM from an embedded device, thus reducing the product cost.
Moreover, according to the present principle, two memory zones of the FLASH are used for data integrity, so that all the data in the RAM can be saved into the two memory zones alternatively, and the sectors of the Flash would have the equal erasing opportunity, thus extending the lifespan of the Flash.
Further, the solution according to the present principle can easily support the requirement for different NVRAM sizes, and it only needs to adapt the size of the Flash to the size of the NVRAM to be emulated.
Further, according to the present principle, it is possible to analyze the life cycles of the emulated NVRAM quantitatively. For example, for each data record in the update data region of the memory zone, each of the offset field and the size field occupies 3 bytes, each of “N” and “V” fields 1 bytes, and the padding field 3 bytes at most, and the CRC field 4 bytes; if 32K NVRAM needs to be emulated, and assuming that max length of the data record to be written to the update data region at a time is N, the number of the data records which can be accommodated in the update data region will be 32K/(15+N) data records; in consideration of one-time data copy from the volatile RAM to the basic region of the memory zone upon the switching of the active memory zone, before one memory zone is erased, the memory zone can be allowed to be written up to the number of times (32K/(15+N)+1); assuming the sector of the Flash allows to be erased 100,000 times, two memory zones will be allowed to be written up to the number of times (32K/(15+N)+1)*2*100,000. In other words, the life cycles of the emulated NVRAM can be analyzed quantitatively.
Moreover, for the power-down exception, the solution according to the present principle can decrease the possibility of the data lost as much as possible. For example, for an operation of writing a data record into the update data region of the active memory zone, if power-down occurs during the writing operation, only the data record being written might be lost upon an initialization of the RAM since the CRC of the data record might be incorrect, no other data records missing. For the case in which the power-down occurs before or after the writing operation, no data record is lost.
For an operation of switching the active memory operation, if the power-down occurs before switching sector, no data record is lost; if the power-down occurs during the operation of copying data from the volatile RAM into the basic data region of the backup memory zone, the CRC for basic data in the backup memory zone might be incorrect, and the backup memory zone will be erased upon an initialization of the RAM, while the data in the active memory zone will be loaded into the volatile RAM, and thus the last data record which triggers the switching of the memory zone will be dropped, and no other data is lost; if the power-down occurs after copying the data from the volatile RAM to the basic data region of the backup memory zone and before erasing another memory zone, the CRCs for both memory zones are correct, the memory zone having a larger free space in the update data region is selected as the active memory zone upon an initialization of the RAM, and the full memory zone will be erased, no data is lost; if the power-down occurs during the operation of erasing the full memory zone, the CRC for the full memory zone might be incorrect, it will be erased upon an initialization of the RAM, while the CRC for another memory zone is correct, and no data is lost; for the last case, if the power-down occurs after erasing the full memory zone, no data lost.
It can be seen that the solution of principle can endure the power-down exception and reduce the possibility of the data lost as much as possible without complex logic.
According to another aspect of the present principle, there is provided a device for saving data from a volatile RAM into a Flash memory. As illustrated in
According to an embodiment of the present principle, the device as described above can be used to emulate a NVRAM such as EEPROM, wherein the size of each of two memory zone is larger the size of NVRAM to be emulated.
Optionally, the controller is further configured to select one of two memory zones as an active memory zone, and to switch the active memory zone among the two memory zones.
Optionally, the data loader is further configured to copy the data from the volatile RAM to the active memory zone until the active memory zone has no enough free space to accommodate the data.
Optionally, the controller is configured to allocate in each of the two memory zones, a basic data region, a header region and an update data region, wherein the size of the basic data region is equal to the size of the NVRAM to be emulated.
Optionally, the data loader is configured to load all the data from the RAM into the basic data region of a backup memory zone upon the switching of the active memory zone.
Optionally, the data loader is further configured to only load the data subjected to a change from the RAM into the update data region of the active memory zone if the data in the volatile RAM is subjected to a change.
Optionally, the controller is further configured to select a backup memory zone as a new active memory zone if the current active memory zone has no enough free space in the update data region to accommodate the changed data.
Optionally, the controller is configured to select the memory zone whose basic data region has correct CRC codes and whose update data region has a maximum free space as the active memory zone upon the selection of the active memory zone.
Optionally, the data loader is configured to load the data saved in the basic data region of the active memory zone into the RAM, and update the data loaded into the RAM in consideration of the data saved in the update data region of the active memory zone upon an initialization of the volatile RAM.
According to another aspect of the present principle, it is provided a computer program product downloadable from a communication network and/or recorded on a medium readable by computer and/or executable by a processor, comprising program code instructions for implementing the steps of a method as aforementioned.
According to one aspect of the present principle, it is provided Non-transitory computer-readable medium comprising a computer program product recorded thereon and capable of being run by a processor, including program code instructions for implementing the steps of a method as aforementioned.
As readily conceived by one of ordinary skill in the art, the teachings of the present disclosure may be implemented in various forms of hardware, software, firmware, special purpose processors, or combinations thereof.
Optionally, the above method can be applied to save data in a portion of the RAM instead of the whole RAM. It is because we don't need to save all data of the RAM into Non-volatile memory in some situations.
Optionally, the teachings of the present disclosure are implemented as a combination of hardware and software. Moreover, the software may be implemented as an application program tangibly embodied on a program storage unit. The application program may be uploaded to, and executed by, a machine comprising any suitable architecture. Preferably, the machine is implemented on a computer platform having hardware such as one or more central processing units (“CPU”), a random access memory (“RAM”), and input/output (“I/O”) interfaces. The computer platform may also include an operating system and microinstruction code. The various processes and functions described herein may be either part of the microinstruction code or part of the application program, or any combination thereof, which may be executed by a CPU. In addition, various other peripheral units may be connected to the computer platform such as an additional data storage unit and a printing unit.
The embodiments of the invention being thus described, it will be obvious that all the embodiments may be varied in many ways. Such variations should not be regarded as a departure from the scope of the invention, and all such modifications as would be obvious to those skilled in the art are intended to be included within the scope of the following claims.
Number | Date | Country | Kind |
---|---|---|---|
PCT/CN2015/099643 | Dec 2015 | CN | national |
This application claims the benefit under 35 U.S.C. § 365 of International Application no. PCT/CN2015/099643, filed, Dec. 30, 2015.