Embedded chip flash power-down prevention method, device and system
The embedded chip Flash anti-power-loss method, which calculates data offset and performs cyclic redundancy check, solves the problems of memory resource waste and short Flash lifespan, and achieves efficient data update and backup under the condition of limited RAM resources.
Patent Information
- Application Number
- CN202211650125.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-21
- Publication Date
- 2026-11-27
- Estimated Expiration
- 2042-12-21
AI Technical Summary
Existing methods for protecting embedded chips from power loss result in wasted memory resources, especially when RAM resources are scarce, making it impossible to perform large-scale updates and backups.
An embedded chip Flash anti-power-loss method is adopted. By calculating the data offset, the original data segment and the updated data segment are distinguished, and cyclic redundancy check is performed. Data blocks are erased only when there is valid data in the backup area, reducing the number of erasures. The relationship between data blocks and the backup area can be flexibly set, and data blocks are written one by one.
It solves the problem of wasted memory resources in traditional methods, improves the lifespan of Flash memory, and enables large-scale data updates and backups even with limited RAM resources.
Smart Images

Figure CN115826869B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of data storage technology, and specifically to a method, device, and system for preventing power loss in embedded chip Flash memory. Background Technology
[0002] Most existing embedded chips use Flash memory as their storage medium. Flash memory is characterized by page erasure, meaning that when modifying the address data already written to it, the entire page of data must be erased before the new data and the original data are written back together. If voltage instability occurs during Flash write operations, there is a high probability of data corruption. Therefore, it is essential to incorporate power-loss protection into embedded chips to ensure that the original data is not lost.
[0003] In related technologies, power loss protection is typically achieved through methods such as using one data area and one backup area, or alternating between the two areas. However, this setup means that even small amounts of updated data can occupy large blocks of sectors, and the RAM space used to store the original data is also relatively large, making it impossible to perform large-scale updates and backups on some chips with limited RAM resources.
[0004] Therefore, the relevant technologies suffer from the technical problem of wasted memory resources in power-down protection. Summary of the Invention
[0005] In view of this, the purpose of the present invention is to provide a method, device and system for preventing power loss of embedded chip Flash, so as to overcome the technical problem of memory resource waste in the current embedded chip Flash power loss protection.
[0006] To achieve the above objectives, the present invention adopts the following technical solution:
[0007] On one hand, an embedded chip Flash anti-power-loss method is applied to the data writing process of a target chip, the target chip including at least two data blocks and a backup area, wherein data is written sequentially into the data blocks; the method includes:
[0008] The data block corresponding to the address of the data block to be written to the target data is found in the registry and is denoted as the original data block; wherein, the original data block stores the original data, the address flag and the check flag;
[0009] Based on the original data block, the offset of the data update is calculated according to the target written data to obtain the starting offset position and the corresponding ending offset position. The starting offset position is then stored in the flag position of the address flag to obtain the updated data segment and the original data segment that remains unchanged. The data content in the updated data segment is the target written data.
[0010] Perform cyclic redundancy check on the original data segment, the updated data segment, and the address flag, and write the final cyclic redundancy check value into the check flag bit of the check flag.
[0011] When the offset of the starting offset position and the offset of the corresponding ending offset position are both aligned with the write mode of the target chip, the data in the original data segment is copied into memory and copied to the corresponding address position in the new data block; and the updated data segment, the updated address flag, and the updated check flag are written into the corresponding positions in the new data block to obtain a new data block that has been written.
[0012] Optional, also includes:
[0013] When either the offset of the starting offset position or the offset of the corresponding ending offset position is not aligned with the write mode of the target chip, the updated data segment is padded with the original data segment adjacent to the updated data segment.
[0014] The step of padding the updated data segment with the original data segment adjacent to the updated data segment includes:
[0015] According to the order of the original data segment and the updated data segment, the data corresponding to the maximum alignable write length of the original data segment that is placed before the updated data segment is determined as a single original data segment, and the remaining data segments in the original data segment other than the single original data segment are taken as the remaining data segments.
[0016] The remaining data segment and a portion of the data in the updated data segment are combined to form a data segment aligned with the write mode of the target chip, which is then used as a single data segment; and the data corresponding to the maximum alignable write length of the remaining data after removing the portion of data in the updated data segment is also used as a single data segment.
[0017] The remaining data after removing the partial data and the data corresponding to the single data segment from the updated data segment, together with the partial data of the original data segment after the updated data segment, forms the data corresponding to the minimum alignable write length, which is used as the single data segment.
[0018] The data within the single original data segment is copied into memory and then copied to the corresponding address location in the new data block; and the data within the single data segment, the updated address flag, and the updated check flag are sequentially written into the new data block.
[0019] Optionally, before the step of searching the registry for the data block corresponding to the address of the data block to be written to the target data, the method further includes:
[0020] The registry is created as a two-dimensional data registry; the two-dimensional data registry registers user-preset macro variable control parameters related to Flash files; wherein, the preset macro variable control parameters related to Flash files include: the starting position of the data file, the size of the data file, the number of data blocks, and the data backup area.
[0021] Optionally, the step of finding the data block corresponding to the address of the data block to be written based on the registry includes:
[0022] Determine whether the data block address corresponding to the target written data and the length of the target written data are valid;
[0023] If the conditions are met, determine whether there is valid data in the backup area;
[0024] When there is no valid data in the backup area, the address of the data block to be written to the target data is determined, and the data block is checked for free space according to the writing order of the data blocks. When the data block has free space, the data block is used as the new data block, and the data block corresponding to the address of the data block to be written to the target data is searched in the registry.
[0025] Optional, also includes:
[0026] When there is no free space in the data block, the backup area is used as the new data block, and the data block corresponding to the address of the data block to be written to the target data is found based on the registry.
[0027] Optional, also includes:
[0028] When the backup area contains valid data, it is determined whether the initial address of the backup area is the same as the starting address of the target data to be written.
[0029] If they are the same, erase all the data blocks, update the data in the backup area to the first data block in the target chip, and disable the flag bit of the backup area;
[0030] If they are different, the data in the backup area will be restored to the data block corresponding to the data in the backup area.
[0031] Optionally, restoring the data in the backup area to the data block corresponding to the data in the backup area includes:
[0032] The address flag of the backup area data is read to determine whether there is free space in the data block; when there is free space in the data block, the backup area data is written to the data block corresponding to the free space by memory copying; when there is no free space in the data block, all the data blocks are erased and the backup area data is written to the first data block.
[0033] Invalidate the backup area.
[0034] On another front, an embedded chip Flash power-loss prevention method is applied to the data reading process of a target chip, wherein the target chip includes at least two data blocks and a backup area, and data is sequentially written into the data blocks; the method includes:
[0035] When valid data exists in the backup area, the starting address of the backup area is determined as the starting address of the data to be read, and data of a preset length is read according to a preset offset.
[0036] When there is no valid data in the backup area, the last valid data block is searched through the registry, and data of the preset length is read from the last valid data block according to the preset offset.
[0037] On another front, an embedded chip Flash power-off protection device includes a processor and a memory, wherein the processor is connected to the memory.
[0038] The processor is used to call and execute the program stored in the memory;
[0039] The memory is used to store the program, which is at least used to execute the embedded chip Flash anti-power-loss method described in any of the above-mentioned methods.
[0040] On another front, an embedded chip Flash power-loss prevention system includes: the aforementioned embedded chip Flash power-loss prevention device and an embedded chip; wherein, the embedded chip includes: N data blocks and a backup area; the data blocks and the backup area are different sectors; the size of the data block is such that the entire sector is divided into N blocks, or N sectors are combined into one data block; wherein, N is a natural number greater than or equal to 2.
[0041] The technical solution provided by this invention has at least the following beneficial effects:
[0042] By automatically calculating the offset of the currently written data, the start and end offset positions are obtained, thus distinguishing between the original data segment and the updated data segment. After performing cyclic redundancy check (C16), based on the alignment method, when both the start and end offset positions are aligned with the write method of the target chip, the data in the original data segment is copied into memory to the corresponding address position in the new data block; and the updated data segment, the updated address flag, and the updated check flag are written to the corresponding positions in the new data block, resulting in a new data block with complete writing. Therefore, the technical solution of this invention eliminates the need for a dual-data-area backup scheme. The data size, the number of data blocks, and the correspondence between backup areas can be flexibly set. Both original and updated data can be written into the new data block. Since different data blocks are written sequentially according to their arrangement order, data can be written block by block, updating only the content of one data block without copying the entire data. This solves the problem of resource waste caused by the need to occupy an equivalent amount of RAM when updating and backing up large amounts of data in traditional methods. In addition, a backup area is set up. Data blocks will only be erased when there is valid data in the backup area, which reduces the number of erasures and solves the problem of low Flash lifespan in existing power-loss prevention technologies. Attached Figure Description
[0043] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0044] Figure 1 This is a schematic diagram of a data update in the prior art provided in an embodiment of the present invention;
[0045] Figure 2 This is a schematic diagram of the storage structure of a target chip according to an embodiment of the present invention;
[0046] Figure 3 A schematic diagram of the storage structure of a target chip provided in another embodiment of the present invention;
[0047] Figure 4 This is a flowchart illustrating an embedded chip Flash anti-power-loss method according to an embodiment of the present invention.
[0048] Figure 5 This is a schematic diagram illustrating a data update according to an embodiment of the present invention;
[0049] Figure 6This is a schematic diagram illustrating a data update according to another embodiment of the present invention;
[0050] Figure 7 This is a schematic diagram illustrating a data update according to another embodiment of the present invention;
[0051] Figure 8 This is a flowchart illustrating an embedded chip Flash anti-power-loss method according to an embodiment of the present invention.
[0052] Figure 9 This is a schematic diagram of the structure of an embedded chip Flash anti-power-loss device provided in an embodiment of the present invention. Detailed Implementation
[0053] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be described in detail below. Obviously, the described embodiments are merely some embodiments of this invention, and not all embodiments. Based on the embodiments of this invention, all other implementation methods obtained by those skilled in the art without creative effort are within the scope of protection of this invention.
[0054] As described in the background section, power loss protection is typically achieved through methods such as using one data area and one backup area, or alternating between the two areas. However, this setup results in large blocks of sectors being occupied even when the amount of data being updated is small, and the RAM space used to store the original data is also relatively large, making it impossible to perform large-scale updates and backups on some chips with limited RAM resources.
[0055] Therefore, the relevant technologies suffer from the technical problem of wasted memory resources in power-down protection.
[0056] Figure 1 This is a schematic diagram of a data update in the prior art provided in an embodiment of the present invention, see reference. Figure 1 The current data update method involves reading the contents of the original data block into a large RAM array, then inserting the data that needs to be changed into the position that needs modification. After that, the combined data is verified and written into a new data block. This method consumes a large amount of RAM.
[0057] The current data update method has several drawbacks. It requires a significant amount of RAM for large-scale data updates, limiting its application on chips with limited RAM. Furthermore, proper stack space configuration is necessary to prevent stack overflow. These issues restrict the method's versatility.
[0058] Based on this, embodiments of the present invention provide a method for preventing power loss in embedded chip Flash memory.
[0059] The embedded chip Flash anti-power-loss method provided by the present invention can be applied to the data writing process of the target chip, wherein the target chip is an embedded Flash chip, and the target chip includes at least two data blocks and a backup area.
[0060] Figure 2 This is a schematic diagram of the storage structure of a target chip according to an embodiment of the present invention. Figure 3 This is a schematic diagram of the storage structure of a target chip provided in another embodiment of the present invention.
[0061] The target chip of this invention is an embedded chip, whose storage area may include: N data blocks and a backup area; the data blocks and the backup area are different sectors; the size of the data block is the entire sector divided into N blocks (see [reference]). Figure 2 Alternatively, N sectors can be combined into one data block (see [reference]). Figure 3 ); where N is a natural number greater than or equal to 2.
[0062] Data storage employs a contiguous storage method, starting from the first data block. For example, after the storage space of the first data area (i.e., the first data block) is exhausted, data is stored sequentially in the second, third, and so on, until the last data area contains valid data. Then, the latest updated data is written to the backup area. Once the latest updated data is written to the backup area, the data area is erased, and the updated data is written again from the first block. Simultaneously, the data in the backup area is invalidated, and a new sequential data block write operation begins.
[0063] It is understood that, using the storage structure provided in this invention, developers can rationally allocate Flash data space based on the amount and type of actual storage parameters, without wasting Flash space. Furthermore, the data storage area is only erased once useful data exists in the backup area, significantly increasing the Flash write / erase lifespan requirements for frequent data storage while achieving power-loss protection. All data storage includes CRC checks to ensure accurate retrieval of the latest, legitimate data.
[0064] Figure 4 This is a flowchart illustrating an embedded chip Flash anti-power-loss method according to an embodiment of the present invention. (See attached diagram.) Figure 4 This embodiment may include the following steps:
[0065] Step S41: Based on the registry, find the data block corresponding to the address waddr of the data block to be written to the target data, and denot it as the original data block; wherein, the original data block stores the original data, address flag and check flag.
[0066] Specifically, any data to be written can be defined as the target data. When writing data, the write address, i.e., the address where the data is to be written, needs to be entered. The data block corresponding to the write address can be found according to a pre-set registry, and is recorded as the original data block. The original data block stores the original data (original data content), address flag (i.e., file address), checksum flag, etc.
[0067] In some embodiments, before searching the registry for the data block corresponding to the address of the data block to be written to the target data, the method further includes:
[0068] Create a registry, which is a two-dimensional data registry; the two-dimensional data registry registers the macro variable control parameters related to the Flash file that are preset by the user; among them, the preset macro variable control parameters related to the Flash file include: the starting position of the data file, the size of the data file, the number of data blocks, and the data backup area.
[0069] For example, macro variables such as "starting position of data file", "data file size", "number of data blocks", and "data backup area" can be pre-designed for the Flash space being operated on. If multiple types of data require frequent write operations, N related macro variables can be set.
[0070] Because the data blocks are multiple blocks and located in different sectors from the backup area, simultaneous failures are impossible. This ensures that even if power is lost during the Flash write process, previous valid data can still be found, achieving power-loss protection. The size of the data blocks can be such that the entire sector is divided into N blocks, or N sectors can be combined into one block, minimizing Flash resource waste. Furthermore, the data sector is only erased when valid data exists in the backup area, and the bit-manipulation capability of Flash significantly extends the storage lifespan.
[0071] Create a two-dimensional array for use in the registry. Register the macro variable parameters defined in the previous step into the registry during system startup.
[0072] In some embodiments, finding the data block corresponding to the address of the data block to be written based on the registry includes:
[0073] Determine whether the data block address corresponding to the target data and the length of the target data are valid;
[0074] If the conditions are met, determine whether valid data exists in the backup area;
[0075] When there is no valid data in the backup area, determine the address of the data block to be written to the target data, check whether there is free space in the data block according to the writing order of the data block, and if there is free space in the data block, treat the data block as a new data block, and search for the data block corresponding to the address of the data block to be written to the target data based on the registry.
[0076] For example, the validity of the data block address corresponding to the target data to be written and the length of the target data to be written can be checked first. If both are valid, the existence of valid data in the backup area can be checked before writing the data.
[0077] When there is no valid data in the backup area, it can be determined whether there is free space in the data block. If there is free space in the data block, the data can be written to the corresponding data block, that is, the data block is used as a new data block; the data block corresponding to the address of the data block to be written to the target is found based on the registry.
[0078] In some embodiments, it also includes:
[0079] When there is no free space in the data block, the backup area is used as the new data block, and the corresponding data block address is found in the registry based on the data block address that the target data needs to be written to.
[0080] For example, when there is no free space in the data block, new data can be written to the backup area, that is, the backup area can be used as a new data block.
[0081] In some embodiments, it also includes:
[0082] When valid data exists in the backup area, determine whether the initial address (addr flag) of the backup area is the same as the starting address of the target data to be written.
[0083] If they are the same, erase all data blocks, update the data in the backup area to the first data block in the target chip, and invalidate the flag bit of the backup area;
[0084] If they are different, the data in the backup area will be restored to the data block corresponding to the data in the backup area.
[0085] For example, when valid data exists in the backup area, it may be necessary to determine whether the initial address of the backup area, i.e., the addr in the flag, is the same as the starting address of the addr of the target data being written. If the addresses are different, it means that the data in the current backup area is from another flash file, and the backup area data needs to be restored to the file at the corresponding address. Conversely, if the backup area contains the latest data of the current flash file (target data being written), the data space is erased, the data is updated to the first block of the data space, and then the backup area flag is invalidated. Compared with the traditional method of restoring the backup area as soon as data is available, this can extend the lifespan of the backup area.
[0086] In some embodiments, restoring data from the backup area to data blocks corresponding to the data in the backup area includes:
[0087] Read the address flag of the backup area data to determine if there is any free space in the data block; if there is free space in the data block, write the backup area data to the data block corresponding to the free space using memory copy; if there is no free space in the data block, erase all data blocks and write the backup area data to the first data block.
[0088] Invalidate the backup partition.
[0089] For example, when checking the backup area data and detecting that data needs to be restored, the address flag (addr flag) of the backup area data can be read first. Then, it is checked whether there are empty data blocks (or whether there is free space in the data blocks). If there are empty data blocks, the data in the backup area is written to the empty data blocks using a memory copy method. If there are no empty data blocks, the entire file storage area is erased and the restored data is written to the first block. Finally, the backup area representation needs to be invalidated.
[0090] Step S42: Based on the original data block, calculate the data update offset according to the target written data, obtain the starting offset position and the corresponding ending offset position, and store the starting offset position in the flag position of the address flag to obtain the updated data segment and the original data segment that does not change; wherein, the data content in the updated data segment is the target written data.
[0091] Step S43: Perform cyclic redundancy check on the original data segment, the updated data segment, and the address flag, and write the final cyclic redundancy check value into the check flag bit.
[0092] Cyclic redundancy check can be CRC16.
[0093] Figure 5 This is a schematic diagram illustrating a data update according to an embodiment of the present invention. (See attached diagram.) Figure 5 Offset is the starting offset of this data update, End is the ending offset, Old1 is the unchanged data before Offset, and Old2 is the unchanged data after End. Addr is the file's starting address used during data recovery. The NewCrc16 value of crc16(Old1+Newdata+Old2+Addr, filesize-2) is the latest checksum data. Similarly, Old1+Newdata+Old2+Addr+NewCrc16 is the final content to be written to the new data block.
[0094] Step S44: When the offset of the starting offset position and the offset of the corresponding ending offset position are both aligned with the write mode of the target chip, the data in the original data segment is copied into memory and copied to the corresponding address position in the new data block; and the updated data segment, the updated address flag, and the updated check flag are written into the corresponding positions in the new data block to obtain a new data block that has been written.
[0095] It is understandable that by automatically calculating the offset of the currently written data, the start and end offset positions are obtained, thus distinguishing between the original data segment and the updated data segment. After performing cyclic redundancy check (C16), based on the alignment method, when both the offset of the start offset position and the corresponding offset of the end offset position are aligned with the write method of the target chip, the data in the original data segment is copied into memory and copied to the corresponding address position in the new data block; and the updated data segment, the updated address flag, and the updated check flag are written into the corresponding positions in the new data block, resulting in a new data block that has been written. Therefore, by adopting the technical solution of this invention, the dual-data-area backup scheme is no longer used. The data size, the number of data blocks, and the correspondence between backup areas can be flexibly set. The original data and the updated data can be written into the new data block. Since different data blocks are written sequentially according to the arrangement order, data can be written block by block, updating only the content of one data block without copying the entire data. This solves the problem that traditional large-scale data update backups require the same amount of RAM, leading to resource waste. In addition, a backup area is set up. Data blocks will only be erased when there is valid data in the backup area, which reduces the number of erasures and solves the problem of low Flash lifespan in existing power-loss prevention technologies.
[0096] Flash memory typically uses three minimum write units: single byte, half-word (2 bytes), and word (4 bytes). This means that each written data must meet the alignment requirements outlined above. If the target data to be written is currently word-aligned in the Flash memory, but the current offset is not word-aligned, the write will fail. Therefore, this invention defines a 4-byte array to handle the alignment operation.
[0097] Figure 6 This is a schematic diagram illustrating a data update according to another embodiment of the present invention, see reference. Figure 6 When both Offset and End addresses are aligned with the current write mode, no padding operation is needed. The corresponding Old1 and Old2 are directly copied to the new data block using memory copy, and then Newdata, Addr, and NewCrc16 are written to the specified address. Even if the original data volume is very large, no large amount of RAM is involved in the operation throughout the process.
[0098] In some embodiments, it also includes:
[0099] If either the offset of the starting offset position or the offset of the corresponding ending offset position is not aligned with the write mode of the target chip, the updated data segment is padded with the original data segment adjacent to the updated data segment.
[0100] The updated data segment is padded with the original data segment adjacent to it, including:
[0101] According to the order of the original data segment and the updated data segment, the data corresponding to the maximum alignable write length of the original data segment that is placed before the updated data segment is determined as a single original data segment, and the remaining data segments in the original data segment other than the single original data segment are taken as the remaining data segments.
[0102] The remaining data segment and a portion of the data in the updated data segment are combined to form a data segment aligned with the write mode of the target chip, which is then used as a single data segment; and the data corresponding to the maximum alignable write length of the remaining data after removing a portion of the data in the updated data segment is used as a single data segment.
[0103] After removing some data and the data corresponding to the single data segment from the updated data segment, the remaining data and some data from the original data segment after the updated data segment are combined to form the data corresponding to the minimum alignable write length, which is used as the single data segment.
[0104] The data within the single original data segment is copied into memory and then copied to the corresponding address location in the new data block; and the data within the single data segment, the updated address flag, and the updated check flag are sequentially written into the new data block.
[0105] Figure 7 This is a schematic diagram illustrating a data update according to another embodiment of the present invention.
[0106] Assuming the current Flash is being written as a word, with Offset = 14 and End = 26, neither is word aligned. NewCrc16 is calculated using crc16(Old1 + Newdata + Old2 + Addr, filesize - 2). A characteristic of Flash is that bit operations writing to different addresses do not reduce its lifespan. Since this invention does not utilize large RAM spaces for computation, multiple memory copies and Flash writes are required.
[0107] When writing, simply ensure the data length is modulo 4 and equal to 0 to indicate that the lengths are aligned. See [reference needed]. Figure 7D1 is the maximum alignable write length of the Old1 data (data before offset 12); D2 is the word length padding data (4-byte buf) consisting of the Old1-D1 data and the Newdata data; D3 is the maximum alignable write length of the remaining data after removing the padding data from D2; D4 is the minimum alignable length (4-byte buf) consisting of the remaining Newdata data and the Old2 data; D5 is the remaining data, which must be aligned because the parameter settings require the entire file to be aligned.
[0108] Among them, memcopy is a memory copy function that directly copies the contents of one Flash memory to another; writeflash is to write data in RAM (buf) to a specified Flash space; because multiple write operations are required, the 4-byte buf used by D2 and D4 has the same parameter, and Addr+NewCrc16 will occupy 8 bytes of RAM. The entire update process will not allocate a large amount of RAM resources. For example, even resource-constrained 51 series MCUs can achieve large-scale data update operations.
[0109] Based on a general inventive concept, the present invention also provides an embedded chip Flash anti-power-loss method, applied to the data reading process of the target chip, the target chip including at least two data blocks and a backup area, wherein data is written to the data blocks sequentially.
[0110] Figure 8 This is a flowchart illustrating an embedded chip Flash anti-power-loss method according to an embodiment of the present invention, applied to the data reading process of the target chip. (See attached diagram.) Figure 8 The method provided in this embodiment of the invention may include the following steps:
[0111] Step S81: When there is valid data in the backup area, the starting address of the backup area is determined as the starting address of the data to be read, and data of a preset length is read according to the preset offset.
[0112] Step S82: When there is no valid data in the backup area, the last valid data block is searched through the registry, and data of a preset length is read from the last valid data block according to the preset offset.
[0113] Specifically, when reading data, the system first checks if there is valid data in the backup area. If valid data exists, the starting address of the backup area is used as the starting address of the data, and data of a specified length is read from the specified offset. If there is no valid data in the backup area, the system searches for the last valid data block using a two-dimensional array of registry information, and reads the corresponding number of bytes from the specified position of that block. The range of data read cannot exceed the defined file size.
[0114] Understandably, by determining whether there is valid data in the backup area, data of a preset length is read according to the set offset, thus realizing the reading of written data.
[0115] In this invention, some functions are illustrated with examples:
[0116] Control parameter description:
[0117] DataAddr: The starting address of the data parameter (must be a sector-aligned starting address);
[0118] FileSize: The size of the space occupied by each data block;
[0119] FileBlock: How many blocks of FileSize data are there (the value of FileBlock * FileSize must be a multiple of the number of sectors);
[0120] BackAddr: The sector address used for backup (the size must be a multiple of the sector size and greater than or equal to FileSize, and cannot overlap with the data storage space).
[0121] Registry:
[0122] The code defines a two-dimensional array of length u16 RegInfo[x][7] to store parameters related to the power failure prevention zone. x controls the number of entries in the registry, which customers can modify according to their actual needs.
[0123] RegInfo[x][0]: The high 16 bits of DataAddr;
[0124] RegInfo[x][1]: The lower 16 bits of DataAddr;
[0125] RegInfo[x][2]: FileSize;
[0126] RegInfo[x][3]: Number of FileBlocks;
[0127] RegInfo[x][4]: The high 16 bits of BackAddr data;
[0128] RegInfo[x][5]: The lower 16 bits of BackAddr;
[0129] Note: FileSize*FileBlock should be an integer multiple of the number of sectors.
[0130] Application example (assuming a sector is 512 bytes):
[0131] / *************Step 1: Design Macro Variables*************************** /
[0132] #define ParamData_Size 0x40 / / File size
[0133] #define ParamData_Block 0x08 / / 8 blocks are used to store ParamData file data
[0134] #define GC_Addr_ParamData 0x000 / / Starting address of the data file
[0135] #define GC_Addr_DataBackAddr 0x200 / / Backup area address
[0136] #define RunTime_Size 0x10 / / Stores the runtime, written once per minute.
[0137] #define RunTime_Block 0x80 / / 128 blocks of storage space
[0138] #define GC_Addr_RunTime 0x400 / / Starting address of the data file
[0139] #define GC_Addr_RunTimeBackAddr 0xC00 / / Backup area address
[0140] / ***********************step2: Initialize the Registry*************************** /
[0141] Reg_RegInfo(RegInfo,GC_Addr_ParamData,ParamData_Size,ParamData_Block,GC_Add r_DataBackAddr); / / Register ParamData file information
[0142] Reg_RegInfo(RegInfo+1, GC_Addr_RunTime, RunTime_Size, RunTime_Block, GC_Addr_RunTimeBackAddr); / / Register RunTime file information / *********************step3: Read / Write Operations ************************** /
[0143] Write_Data(u32 addr,u8*src,u16 wlen);
[0144] / / This function is written to prevent power loss; parts 3, 4, and 5 of the technical solution are implemented in this function.
[0145] Read_Data(u32 addr,u8*databuf,u16 rlen);
[0146] This function is for reading in a power-loss protected manner; the six parts of the technical solution are implemented in this function.
[0147] / ***************************End.********************************** /
[0148] Assuming the chip's Flash memory has a lifespan of 100,000 cycles, the Runtime parameter needs to be written once per minute, which is far from meeting the requirements even with two data blocks used alternately. However, after using this solution, the Runtime parameter not only achieves power-loss protection but also significantly extends its lifespan to 128 * 100,000 cycles = 12.8 million cycles.
[0149] Based on a general inventive concept, embodiments of the present invention also provide an embedded chip Flash anti-power-loss device.
[0150] Figure 9 This is a schematic diagram of an embedded chip Flash anti-power-loss device according to an embodiment of the present invention, used to implement the embedded chip Flash anti-power-loss method described in the above embodiment, and used for data writing. (See attached diagram.) Figure 9 The device in this embodiment includes a processor 91 and a memory 92, with the processor 91 connected to the memory 92. The processor 91 is used to call and execute a program stored in the memory 92; the memory 92 is used to store the program, which is at least used to execute the embedded chip Flash anti-power-loss method described in the above embodiment, for writing data.
[0151] The specific implementation scheme of the embedded chip Flash anti-power-loss device provided in this application embodiment can refer to the implementation scheme of the embedded chip Flash anti-power-loss method in any of the above embodiments, and will not be repeated here.
[0152] Based on a general inventive concept, embodiments of the present invention also provide an embedded chip Flash anti-power-loss system.
[0153] The embedded chip Flash power-loss prevention system provided by the present invention includes: the embedded chip Flash power-loss prevention device and the embedded chip described above; wherein, the embedded chip includes: N data blocks and a backup area; the data blocks and the backup area are different sectors; the size of the data block is the entire sector divided into N blocks, or N sectors combined into one data block; wherein, N is a natural number greater than or equal to 2.
[0154] It is understood that the same or similar parts in the above embodiments can be referred to each other, and the contents not described in detail in some embodiments can be referred to the same or similar contents in other embodiments.
[0155] It should be noted that in the description of this invention, the terms "first," "second," etc., are used for descriptive purposes only and should not be construed as indicating or implying relative importance. Furthermore, in the description of this invention, unless otherwise stated, "a plurality of" means at least two.
[0156] Any process or method description in the flowchart or otherwise herein can be understood as representing a module, segment, or portion of code comprising one or more executable instructions for implementing a particular logical function or process, and the scope of the preferred embodiments of the invention includes additional implementations in which functions may be performed not in the order shown or discussed, including substantially simultaneously or in reverse order depending on the functions involved, as will be understood by those skilled in the art to which embodiments of the invention pertain.
[0157] It should be understood that various parts of the present invention can be implemented in hardware, software, firmware, or a combination thereof. In the above embodiments, multiple steps or methods can be implemented in software or firmware stored in memory and executed by a suitable instruction execution system. For example, if implemented in hardware, as in another embodiment, it can be implemented using any one or a combination of the following techniques known in the art: discrete logic circuits having logic gates for implementing logical functions on data signals, application-specific integrated circuits (ASICs) having suitable combinational logic gates, programmable gate arrays (PGAs), field-programmable gate arrays (FPGAs), etc.
[0158] Those skilled in the art will understand that all or part of the steps of the methods in the above embodiments can be implemented by a program instructing related hardware. The program can be stored in a computer-readable storage medium, and when executed, the program includes one or a combination of the steps of the method embodiments.
[0159] Furthermore, the functional units in the various embodiments of the present invention can be integrated into a processing module, or each unit can exist physically separately, or two or more units can be integrated into a module. The integrated module can be implemented in hardware or as a software functional module. If the integrated module is implemented as a software functional module and sold or used as an independent product, it can also be stored in a computer-readable storage medium.
[0160] The storage media mentioned above can be read-only memory, disk, or optical disk, etc.
[0161] In the description of this specification, references to terms such as "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., indicate that a specific feature, structure, material, or characteristic described in connection with that embodiment or example is included in at least one embodiment or example of the invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples.
[0162] Although embodiments of the present invention have been shown and described above, it is understood that the above embodiments are exemplary and should not be construed as limiting the present invention. Those skilled in the art can make changes, modifications, substitutions and variations to the above embodiments within the scope of the present invention.
Claims
1. A method for preventing power loss in embedded chip Flash memory, characterized in that, A data writing process applied to a target chip, the target chip comprising at least two data blocks and a backup area, wherein data is sequentially written to the data blocks; the method includes: The data block corresponding to the address of the data block to be written to the target data is found in the registry and is denoted as the original data block; wherein, the original data block stores the original data, the address flag and the check flag; Based on the original data block, the offset of the data update is calculated according to the target written data to obtain the starting offset position and the corresponding ending offset position. The starting offset position is then stored in the flag position of the address flag to obtain the updated data segment and the original data segment that remains unchanged. The data content in the updated data segment is the target written data. Perform cyclic redundancy check on the original data segment, the updated data segment, and the address flag, and write the final cyclic redundancy check value into the check flag bit of the check flag. When the offset of the starting offset position and the offset of the corresponding ending offset position are both aligned with the write mode of the target chip, the data in the original data segment is copied into memory and copied to the corresponding address position in the new data block; and the updated data segment, the updated address flag, and the updated check flag are written into the corresponding positions in the new data block to obtain a new data block that has been written. When either the offset of the starting offset position or the offset of the corresponding ending offset position is not aligned with the write mode of the target chip, the updated data segment is padded with the original data segment adjacent to the updated data segment. The step of padding the updated data segment with the original data segment adjacent to the updated data segment includes: According to the order of the original data segment and the updated data segment, the data corresponding to the maximum alignable write length of the original data segment that is placed before the updated data segment is determined as a single original data segment, and the remaining data segments in the original data segment other than the single original data segment are taken as the remaining data segments. The remaining data segment and a portion of the data in the updated data segment are combined to form a data segment aligned with the write mode of the target chip, which is then used as a single data segment; and the data corresponding to the maximum alignable write length of the remaining data after removing the portion of data in the updated data segment is also used as a single data segment. The remaining data after removing the partial data and the data corresponding to the single data segment from the updated data segment, together with the partial data of the original data segment after the updated data segment, forms the data corresponding to the minimum alignable write length, which is used as the single data segment. The data within the single original data segment is copied into memory and then copied to the corresponding address location in the new data block; and the data within the single data segment, the updated address flag, and the updated check flag are sequentially written into the new data block.
2. The method according to claim 1, characterized in that, Before the step of searching the registry for the data block corresponding to the address of the data block to be written to the target data, the method further includes: The registry is created as a two-dimensional data registry; the two-dimensional data registry registers user-preset macro variable control parameters related to Flash files; wherein, the preset macro variable control parameters related to Flash files include: the starting position of the data file, the size of the data file, the number of data blocks, and the data backup area.
3. The method according to claim 1, characterized in that, The process of finding the data block corresponding to the address of the data block to be written based on the registry includes: Determine whether the data block address corresponding to the target written data and the length of the target written data are valid; When both the data block address corresponding to the target data to be written and the length of the target data to be written are valid, it is determined whether there is valid data in the backup area; When there is no valid data in the backup area, the address of the data block to be written to the target data is determined, and the data block is checked for free space according to the writing order of the data blocks. When the data block has free space, the data block is used as the new data block, and the data block corresponding to the address of the data block to be written to the target data is searched in the registry.
4. The method according to claim 3, characterized in that, Also includes: When there is no free space in the data block, the backup area is used as the new data block, and the data block corresponding to the address of the data block to be written to the target data is found based on the registry.
5. The method according to claim 3, characterized in that, Also includes: When valid data exists in the backup area, it is determined whether the initial address of the backup area is the same as the starting address of the target data to be written. If they are the same, erase all the data blocks, update the data in the backup area to the first data block in the target chip, and disable the flag bit of the backup area; If they are different, the data in the backup area will be restored to the data block corresponding to the data in the backup area.
6. The method according to claim 5, characterized in that, The step of restoring the data in the backup area to the data block corresponding to the data in the backup area includes: The address flag of the backup area data is read to determine whether there is free space in the data block; when there is free space in the data block, the backup area data is written to the data block corresponding to the free space by memory copying; when there is no free space in the data block, all the data blocks are erased and the backup area data is written to the first data block. Invalidate the backup area.
7. A method for preventing power loss in embedded chip Flash memory, characterized in that, A data reading process applied to a target chip, wherein the target chip includes at least two data blocks and a backup area, wherein data is written sequentially into the data blocks; The target chip's data is written using the method described in any one of claims 1-6; the method includes: When valid data exists in the backup area, the starting address of the backup area is determined as the starting address of the data to be read, and data of a preset length is read according to a preset offset. When there is no valid data in the backup area, the last valid data block is searched through the registry, and data of the preset length is read from the last valid data block according to the preset offset.
8. An embedded chip Flash anti-power-loss device, characterized in that, It includes a processor and a memory, wherein the processor is connected to the memory: The processor is used to call and execute the program stored in the memory; The memory is used to store the program, which is at least used to execute the embedded chip Flash anti-power-loss method according to any one of claims 1-6.
9. An embedded chip Flash anti-power-loss system, characterized in that, include: The embedded chip Flash anti-power-loss device and embedded chip according to claim 8; wherein, the embedded chip includes: N data blocks and a backup area; the data blocks and the backup area are different sectors; the size of the data block is the entire sector divided into N blocks, or N sectors combined into one data block; wherein, N is a natural number greater than or equal to 2.
Citation Information
Patent Citations
Nonvolatile memory management method and device
CN104090732A
Recovery method and system for flash data corrupted due to power failure
CN106227625A
Anti-power-down storage method of external flash memory chip and external flash memory chip
CN114530169A
Device with flash memory and method for writing / erasing / updating data in flash memory thereof
US20220155974A1
