A flash read-write method and electronic device

By adding flag bytes and variables to manage the read and write order of Flash memory units in DTOF applications, the performance and lifespan issues of Flash memory in microcontrollers are solved, achieving efficient and reliable data management and extended lifespan.

CN120045136BActive Publication Date: 2025-11-25SHENZHEN ADAPS PHOTONICS TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510137541.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-02-07
Publication Date
2025-11-25
Estimated Expiration
2045-02-07

AI Technical Summary

Technical Problem

In existing technologies, when a microcontroller saves data to Flash memory in a DTOF application scenario, the file system method occupies storage space and consumes processor resources, while the direct access method needs to consider the Flash lifespan, leading to performance and lifespan issues.

Method used

By adding flag bytes to the data and setting variables in memory, the read and write order of Flash storage units is dynamically managed, and a non-destructive data deletion method is used to reduce the number of Flash erase/write cycles.

Benefits of technology

Effectively manage the data read and write process of Flash memory, extend the lifespan of Flash memory, improve data management efficiency and system reliability, and avoid data loss and system crashes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120045136B_ABST
    Figure CN120045136B_ABST
Patent Text Reader

Abstract

The application relates to the technical field of data storage, in particular to a flash read-write method and an electronic device. The method comprises the following steps: adding a mark byte to original data in advance to form target data, wherein the mark byte comprises a first mark bit and a second mark bit, the first mark bit represents whether the target data has been written into a flash storage unit, and the second mark bit represents whether the target data should be deleted from the flash storage unit; setting a first variable and a second variable in the memory in advance, the first variable saves a flash unit number to be written next time, and the second variable saves a flash unit number to be read next time; when the target data is to be written, the first mark bit of the target data in the memory is modified, the second mark bit remains unchanged, the target data is written into a flash storage unit corresponding to the first variable, and the unit number saved by the first variable in the memory is increased by 1.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application relates to the technical field of data storage, in particular to a flash read-write method and an electronic device. BACKGROUND

[0002] In some DTOF application scenarios (offline log transmission, offline ranging result transmission) in which a single-chip microcomputer serves as a controller, data generated at irregular or regular intervals during DTOF operation needs to be saved to a flash memory in the single-chip microcomputer or externally. Generally, there are two ways to save data to the flash memory in the single-chip microcomputer, one is based on a file system, and the other is direct access to the storage device.

[0003] The file system method needs to occupy a certain amount of storage space to store file system metadata (such as directory structure and file allocation table) and cached data, which may become a problem for single-chip microcomputers with limited storage space. In addition, the file system also needs to consume certain processor resources to perform file operations, which may affect the overall performance of the single-chip microcomputer. The read-write operations of the file system are usually much more complex than direct access to the storage device, because they need to handle file opening, closing, reading, writing, positioning and other operations, which may introduce additional delays and overheads, thereby affecting the real-time performance and response speed of the single-chip microcomputer. However, the direct access to the storage device method needs to consider the number of write-erase times of the flash, as well as the page size and erase block size of the flash, in order to avoid the frequent writing of DTOF data leading to the shortening of the service life of the flash.

[0004] Therefore, how to design a flash read-write method that can effectively manage the data read-write process of the flash memory to prolong the service life of the flash is a technical problem to be solved at present. SUMMARY

[0005] The application aims to overcome the above technical problems and provides a flash read-write method and an electronic device, which can effectively manage the data read-write process of the flash memory to prolong the service life of the flash.

[0006] In a first aspect, an embodiment of the application discloses a flash read-write method, which adopts the following scheme:

[0007] A flash read-write method comprises the following steps:

[0008] Pre-add a flag byte to the original data to form target data, wherein the flag byte includes a first flag bit and a second flag bit, the first flag bit represents whether the target data has been written into a flash memory unit, and the second flag bit represents whether the target data should be deleted from the flash memory unit; a first variable and a second variable are pre-set in the memory, the first variable saves a flash unit number to be written next time, and the second variable saves a flash unit number to be read next time;

[0009] When the target data is to be written, the first flag bit of the target data in the memory is modified, the second flag bit is kept unchanged, the target data is written into the flash memory unit corresponding to the first variable, and the unit number saved in the first variable in the memory is increased by 1;

[0010] Or, the target data is read from the flash memory unit corresponding to the second variable, the second flag bit of the target data in the flash is modified, the first flag bit is kept unchanged, and the unit number saved in the second variable in the memory is increased by 1.

[0011] By adopting the above technical scheme, the data read-write process of the flash memory can be effectively managed. First, by adding a flag byte to the original data, the state of the data (already written or to be deleted) can be clearly identified, which enables the system to quickly judge the state of the data when reading the data, thereby avoiding misoperation and improving the efficiency and accuracy of data management. Secondly, by setting the first variable and the second variable in the memory, the read-write order of the flash memory unit is dynamically managed, which can ensure that the data is processed in sequence and avoid unnecessary repeated writing operation, thereby improving the reliability and efficiency of data management. In addition, the first flag bit and the second flag bit of the target data are modified respectively, which can accurately control the writing and deleting operation of the data without affecting other states, and when deleting, only the flag bit needs to be modified to logically delete the data without physically erasing the data in the flash memory. This non-destructive deletion method reduces the erase-write times of the flash memory and can further prolong the service life of the flash.

[0012] Optionally, the unit number is specifically: based on a preset byte length, the flash space is divided according to the address from low to high to obtain N storage units; the first storage unit to the Nth storage unit correspond to unit number 1 to unit number N; when the unit number saved in the first variable IN is N, after the addition operation, the unit number saved in the first variable is 1; when the unit number saved in the second variable OUT is N, after the addition operation, the unit number saved in the second variable is 1.

[0013] By adopting the above technical solution, it can be ensured that when the flash memory unit is recycled, data loss or incorrect writing will not be caused due to variable exceeding the range. Specifically, the flash space is divided according to the address from low to high direction, and each memory unit is assigned a unique number, so that the data management is more orderly and efficient. When the cell number saved by the first variable reaches the maximum value N, it is automatically reset to 1, ensuring that the writing operation is always within the effective range, avoiding the problem of out-of-range. Similarly, when the cell number saved by the second variable reaches the maximum value N, it is also automatically reset to 1, ensuring that the reading operation is also within the effective range, preventing reading of incorrect data.

[0014] Optionally, the flag byte is 8 bits, when the data in the flash memory unit is erased, the flag byte is 1111 1111; the first flag bit corresponds to the lowest bit, and the second flag bit corresponds to the second lowest bit; when the target data has been written into the flash memory unit, the flag byte is 1111 1110, and when the target data should be deleted from the flash memory unit, the flag byte is 1111 1100.

[0015] By adopting the above technical solution, the state of the flash memory unit can be accurately identified, and the correct writing and deletion of data can be ensured. Specifically, when the target data has been written into the flash memory unit, the flag byte becomes 1111 1110, which clearly indicates that the unit has been used. When the target data should be deleted from the flash memory unit, the flag byte becomes 1111 1100, which clearly marks that the data of the unit needs to be removed. These specific flag bit settings help to improve the reliability and data management efficiency of the system.

[0016] Optionally, after the cell number saved by the first variable in the memory is incremented by 1, it further comprises: judging whether the cell number saved by the updated first variable is equal to the cell number saved by the second variable; if yes, deleting the storage unit data corresponding to the cell number saved by the second variable, or discarding the target data to be stored.

[0017] By adopting the above technical solution, the overflow problem of the flash memory unit can be effectively avoided. When the cell number saved by the first variable is incremented by 1 and is the same as the cell number saved by the second variable, it means that the flash storage space is full. At this time, the system will automatically delete the storage unit data corresponding to the second variable, or give up the newly written target data, thereby ensuring the stability and reliability of the system, and preventing data loss or system crash due to insufficient storage space.

[0018] Optionally, when power failure occurs, the cell numbers saved by the first variable and the second variable set in the memory are deleted.

[0019] By adopting the above technical solution, when power failure occurs, the unit numbers saved by the first variable and the second variable in the memory are deleted, thereby avoiding the problem of data confusion caused by accidental power failure. The solution ensures that the system can correctly recover the state after restarting, thereby improving the stability and reliability of the system.

[0020] Optionally, when restarting, the flag byte in the target data stored in the flash is judged to set the unit numbers saved by the first variable and the second variable according to the values of the first flag bit and the second flag bit of the flag byte.

[0021] By adopting the above technical solution, the unit numbers of the first variable and the second variable in the memory can be accurately recovered after the system restarts, thereby avoiding the problems of data loss and repeated writing. Specifically, when restarting, the flag byte of the target data stored in the flash is judged to identify which data is valid and which data needs to be deleted or ignored. The unit numbers saved by the first variable and the second variable are set according to the values of the first flag bit and the second flag bit of the flag byte, thereby ensuring the continuity and consistency of the system and reducing the risk of data confusion caused by power failure. This mechanism improves the reliability and stability of the system, especially in the case of frequent power failure or accidental restart, thereby ensuring the consistency and integrity of the data.

[0022] Optionally, when the first flag bit is 0 and the second flag bit is 1, it indicates that the flash storage unit is a valid unit; when the first flag bit and the second flag bit are other values, it indicates that the flash storage unit is an invalid unit; and the setting of the unit numbers saved by the first variable and the second variable according to the values of the first flag bit and the second flag bit of the flag byte specifically includes setting the unit numbers saved by the first variable and the second variable according to the searched valid units or invalid units.

[0023] By adopting the above technical solution, the data state in the flash memory can be effectively managed, and the read-write position can be accurately recovered during the restart process. Specifically, when the first flag bit is 0 and the second flag bit is 1, it indicates that the flash storage unit is a valid unit, i.e., the data in the unit can be normally read and used. When the first flag bit and the second flag bit are other values, it indicates that the flash storage unit is an invalid unit, i.e., the data in the unit has been marked for deletion or should not be used again. When the system restarts, the flag byte of each storage unit is detected to quickly determine which units are valid and which units are invalid, thereby correctly setting the first variable and the second variable in the memory and avoiding data loss or incorrect reading. These measures improve the reliability and stability of the system and reduce problems caused by improper data management.

[0024] Optionally, the setting of the unit number saved by the first variable and the unit number saved by the second variable according to the searched valid unit or invalid unit specifically comprises: if there is no valid unit in all storage units in the flash, setting the unit number saved by the first variable as 1 and setting the unit number saved by the second variable as 1; if all storage units in the flash are valid units, setting the unit number saved by the first variable as 1 and setting the unit number saved by the second variable as N.

[0025] By adopting the above technical solution, the state of the flash storage can be quickly recovered after the system restarts, and the problem of data loss caused by power failure can be avoided. Specifically, when all storage units in the flash are invalid units, the first variable and the second variable are initialized as 1, ensuring that the system can start writing new data in order from the beginning; and when all storage units are valid units, the first variable is set as 1 and the second variable is set as N, ensuring that the reading operation ends at the last valid unit, thereby maintaining the consistency and integrity of the data.

[0026] Optionally, the setting of the unit number saved by the first variable and the unit number saved by the second variable according to the searched valid unit or invalid unit specifically comprises:

[0027] Starting from the storage unit with unit number 1, searching for the first valid unit, recording the unit number A_1 of the valid unit, and starting searching from the unit number A_1:

[0028] If no invalid unit is found, setting the unit number saved by the first variable as 1 and setting the unit number saved by the second variable as A_1;

[0029] If an invalid unit is found, recording the unit number B of the invalid unit, starting searching from the unit number B, if no valid unit is found, setting the unit number saved by the first variable as B and setting the unit number saved by the second variable as A_1; if a valid unit is found, ending the search, recording the unit number A_2 of the valid unit, setting the unit number saved by the first variable as B, and setting the unit number saved by the second variable as A_.

[0030] By adopting the technical scheme, the data in the flash memory can be effectively managed, and the read-write state can be correctly restored after restart. Specifically, when searching from the storage unit with unit number 1 to the first valid unit (unit number is A_1), if no invalid unit is found subsequently, the first variable and the second variable are set to 1 and A_1 respectively, so that the starting position of next writing and reading is reasonable. If an invalid unit (unit number is B) is found when searching from A_1, further searching is performed: if no new valid unit is found, the first variable is set to B, and the second variable is set to A_1, so that the invalid unit is not repeatedly used. If a new valid unit (unit number is A_2) is found, the first variable is set to B, and the second variable is set to A_2, so that the intermediate invalid unit is skipped, and the storage efficiency is improved. Overall, the scheme optimizes the data management and recovery process of the flash memory, reduces the influence of invalid data, and improves the reliability and performance of the system.

[0031] In a second aspect, another embodiment of the present application discloses an electronic device, which adopts the following scheme:

[0032] An electronic device, comprising: a flash memory, a memory and a CPU;

[0033] The flash memory is configured to store the target data.

[0034] The memory is configured to save the first variable, the second variable and the target data.

[0035] The CPU is configured to execute the read-write method of the flash.

[0036] By adopting the technical scheme, the data read-write process of the flash memory can be effectively managed. By adding the flag byte to the original data and setting the corresponding variable in the memory, the accurate control of the data state is realized. Especially in the case of power failure, the read-write position can be accurately restored after the system restarts, and the problems of data loss and repeated writing are avoided. In addition, through the state judgment of the flag byte, the valid data and the data to be deleted can be quickly identified, and the reliability and efficiency of the system are improved.

[0037] In summary, the present application has at least one of the following beneficial technical effects:

[0038] 1. By adding the flag byte to the original data in advance and setting the first variable and the second variable in the memory, an efficient data management mechanism is realized, the number of flash erasures is effectively reduced, and the service life of the flash is prolonged.

[0039] 2. In writing or reading target data, the consistency and integrity of the data are ensured by modifying the first flag bit and the second flag bit of the flag byte, and the reliability and stability of the system are improved;

[0040] 3. When power recovery, the first variable and the second variable in the memory are reset according to the flag byte state of the target data stored in the flash, the system can normally operate in abnormal conditions, and the fault tolerance of the system is enhanced. BRIEF DESCRIPTION OF DRAWINGS

[0041] The above and other objects, features and advantages of the present application will become more apparent from the following detailed description when taken in conjunction with the accompanying drawings in which like reference characters refer to like parts throughout the figures, and in which:

[0042] Figure 1 A flowchart of a read-write method of a flash according to an embodiment of the present application is disclosed;

[0043] Figure 2 A schematic diagram of target data according to an embodiment of the present application is disclosed;

[0044] Figure 3 A schematic diagram of a first flag bit and a second flag bit according to an embodiment of the present application is disclosed;

[0045] Figure 4 A schematic diagram of a flash space forming a ring type queue according to an embodiment of the present application is disclosed;

[0046] Figure 5 A schematic diagram of an initial state of a flag byte, data writing and data reading according to an embodiment of the present application is disclosed;

[0047] Figure 6 A flowchart of resetting the first variable and the second variable after restarting according to an embodiment of the present application is disclosed;

[0048] Figure 7 A schematic diagram of an electronic device according to another embodiment of the present application is disclosed. DETAILED DESCRIPTION

[0049] Embodiments of the present application will be described in more detail by referring to the drawings. Although the embodiments of the present application are shown in the drawings, it should be understood that the present application can be implemented in various forms and should not be limited by the embodiments described herein. Rather, these embodiments are provided so that the present application is more thorough and complete, and the scope of the present application is fully conveyed to those skilled in the art.

[0050] The terminology used in this application is for the purpose of describing particular embodiments only and is not intended to be limiting. As used in this application and the appended claims, the singular forms "a," "an" and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It will be further understood that the terms "and / or," as used herein, refers to and encompasses any and all possible combinations of one or more of the associated listed items.

[0051] It should be understood that, although the terms "first," "second," etc. can be used herein to describe various information, these terms are not intended to denote a particular order or hierarchy. These terms are used only to distinguish one from another. For example, a first information can be termed a second information, and, similarly, a second information can be termed a first information, without departing from the scope of the present application. Therefore, the use of the terms "first," "second," etc., herein will not be taken to imply that the features so described are the only features of their kind, but are merely one or more features of their kind. In the description of the application, the meaning of "a plurality" is two or more, unless explicitly stated otherwise.

[0052] The technical solutions of the embodiments of the application will be described below in detail with reference to the drawings.

[0053]

First Embodiment

[0054] The read-write method for flash disclosed in the first embodiment of the application is used to save each data generated by DTOF (Direct Time Of Flight) into the flash memory in time, so as to avoid data loss caused by sudden power failure of the single-chip microcomputer.

[0055] Referring to Figure 1 , the read-write method for flash comprises the following steps:

[0056] S10, a flag byte is added to the original data in advance to form target data, wherein the flag byte comprises a first flag bit and a second flag bit, the first flag bit represents whether the target data has been written into the flash memory unit, and the second flag bit represents whether the target data should be deleted from the flash memory unit; a first variable IN and a second variable OUT are set in the memory in advance, the first variable IN saves the flash unit number of the next writing, and the second variable OUT saves the flash unit number of the next reading.

[0057] Specifically, the original data is fixed-length or indefinite-length data, and the target data is formed after the flag byte is added, as shown in Figure 2 The figure shows a schematic diagram of the target data. In the embodiment, the length of the original data is not limited. The first flag bit and the second flag bit are two bits in the flag byte, but the positions of the bits in the byte are not limited.

[0058] For example, refer to Figure 3 When the flag byte is 1 byte, the corresponding flag byte is 8 bits, the first flag bit A is defined to correspond to the lowest bit, and the second flag bit B is defined to correspond to the next lowest bit. The first flag bit A and the second flag bit B represent the state of the target data stored in the flash memory unit with "0" and "1". When the first flag bit is 1, it indicates that the target data has not been written into the flash memory unit, and when the first flag bit is 0, it indicates that the target data has been written into the flash memory unit. When the second flag bit B is 1, it indicates that the target data should not be deleted from the flash memory unit, and when the second flag bit B is 0, it indicates that the target data should be deleted from the flash memory unit. In this way, based on the flag byte added in the original data, the write and delete states of the data can be effectively managed to reduce unnecessary erase operations.

[0059] For example, refer to Figure 4 In this embodiment, the flash memory unit number is specifically: based on a preset byte length, the flash space is divided in the direction from low to high address, and each storage unit is assigned a unique number to obtain N storage units. The first storage unit to the Nth storage unit corresponds to unit number 1 to unit number N.

[0060] When the first variable IN currently holds the unit number N (i.e., reaches the maximum value), after the add 1 operation, the unit number held by the first variable IN is automatically reset to 1 to ensure that the write operation is always performed within the valid range, avoiding the problem of out-of-bounds. Similarly, when the second variable OUT currently holds the unit number N (i.e., reaches the maximum value), after the add 1 operation, the unit number held by the second variable OUT is also automatically reset to 1 to ensure that the read operation is also performed within the valid range, preventing the reading of incorrect data.

[0061] S20, when the target data is to be written, modifying the first flag bit of the target data in the memory, keeping the second flag bit unchanged, writing the target data into the flash memory unit corresponding to the first variable IN, and adding 1 to the unit number held by the first variable IN in the memory;

[0062] For example, with 8 bits as the flag byte, when the data in the flash memory unit is erased, the flag byte is 11111111 (0xFF, 0x is the meaning of hexadecimal), which is the initial state of the flag byte. The first flag bit corresponds to the lowest bit, and the second flag bit corresponds to the next lowest bit. When the target data is to be written, the first flag bit of the target data in the memory is modified, and the second flag bit is kept unchanged. The corresponding flag byte is 11111110 (0xFE), refer to Figure 5 .

[0063] Further, after the cell number saved in the first variable IN in the memory is increased by 1 in step S20, in order to avoid the overflow problem of the flash memory cell, in the embodiment, further includes:

[0064] S21, judging whether the cell number saved in the first variable IN after being increased by 1 is equal to the cell number saved in the second variable OUT.

[0065] Wherein, when the cell number saved in the first variable IN after being increased by 1 is equal to the cell number saved in the second variable OUT, it means that the flash memory space is full.

[0066] S22, if yes, deleting the memory cell data corresponding to the cell number saved in the second variable OUT, or discarding the target data to be stored.

[0067] Wherein, when it is determined that the flash memory space is full, the system automatically deletes the memory cell data corresponding to the second variable OUT, or gives up the target data to be newly written, so as to ensure the stability and reliability of the system, and prevent data loss or system crash caused by insufficient memory space.

[0068] S30, reading out the target data from the flash memory cell corresponding to the second variable OUT, modifying the second flag bit of the target data in the flash, keeping the first flag bit unchanged, increasing the cell number saved in the second variable OUT in the memory by 1.

[0069] Referring to Figure 4 When the target data is to be read out, the second flag bit of the target data in the flash is modified, the first flag bit is kept unchanged, and the corresponding flag byte is 1111 1100 (0xFC), referring to Figure 5 .

[0070] Further, when the device is powered off, the cell numbers saved in the first variable IN and the second variable OUT in the memory are deleted.

[0071] Correspondingly, when restarted, by reacquiring the cell numbers saved in the first variable IN and the second variable OUT, the continuity and consistency of the system can be ensured. Since the flag byte is stored in the target data in advance, the cell numbers of the first variable IN and the second variable OUT can be reset by the following step S40, referring to Figure 6 , specifically as follows:

[0072] S40, judging the flag byte in the target data stored in the flash, so as to set the cell numbers saved in the first variable IN and the second variable OUT according to the values of the first flag bit and the second flag bit of the flag byte.

[0073] Wherein, the first flag bit is 0 and the second flag bit is 1, indicating that the flash storage unit is a valid unit; the first flag bit and the second flag bit are other values, i.e. the first flag bit is 1 and the second flag bit is 0, or the first flag bit is 1 and the second flag bit is 1, or the first flag bit is 0 and the second flag bit is 0, indicating that the flash storage unit is an invalid unit. In this embodiment, by detecting the flag byte of each storage unit, it can be quickly determined which unit is valid and which unit is invalid, so as to correctly set the first variable IN and the second variable OUT in the memory, and find the next position to be stored or read out in the flash storage space.

[0074] The step S40 sets the unit number saved by the first variable IN and the second variable OUT according to the valid unit or invalid unit searched, and specifically includes:

[0075] S41, if there is no valid unit in all storage units in the flash, the unit number saved by the first variable IN is set to 1, and the unit number saved by the second variable OUT is set to 1;

[0076] If there is no valid unit in all storage units in the flash, it means that the flash is empty, and the first variable IN and the second variable OUT are initialized to 1, so that new data can be written into the flash from the first address, and data can be read from the first address.

[0077] S42, if all storage units in the flash are valid units, the unit number saved by the first variable IN is set to 1, and the unit number saved by the second variable OUT is set to N.

[0078] If all storage units in the flash are valid units, it means that the flash is full, the first variable IN is set to 1, and the second variable OUT is set to N, so that new data can be written into the flash from the first address, and data can be read from the Nth address.

[0079] Further, referring to FIG. 6, the step S40 further specifically includes:

[0080] S43, searching from the storage unit with unit number 1, recording the unit number A_1 of the first valid unit found, and searching from the unit number A_1:

[0081] S44, if no invalid unit is found, the unit number saved by the first variable IN is set to 1, and the unit number saved by the second variable OUT is set to A_1;

[0082] If no invalid cell is found after the first valid cell (cell number is A_1) is found by searching from the cell number 1, the first variable IN and the second variable OUT are set to 1 and A_1 respectively, ensuring that the start position of the next write and read is reasonable.

[0083] S45, if an invalid cell is found, record the cell number B of the invalid cell and search from the cell number B;

[0084] S46, if no valid cell is found, set the cell number B saved by the first variable IN and set the cell number A_1 saved by the second variable OUT;

[0085] S47, if a valid cell is found, record the cell number A_2 of the valid cell, set the cell number B saved by the first variable IN and set the cell number A_2 saved by the second variable OUT.

[0086] If an invalid cell (cell number is B) is found by searching from A_1, further search is performed:

[0087] If no new valid cell is found, set the first variable IN to B and the second variable OUT to A_1, avoiding the repeated use of invalid cells. If a new valid cell (cell number is A_2) is found, set the first variable IN to B and the second variable OUT to A_2, skipping the intermediate invalid cells and improving storage efficiency.

[0088] In this way, the cell numbers saved by the first variable IN and the second variable OUT are restored by steps S41-S47, maintaining the consistency and integrity of the data and improving the reliability and performance of the system.

[0089] To sum up, the application can identify the state of data (written or to be deleted) by adding a mark byte to the original data, which enables the system to quickly determine the state of data when reading data, thereby avoiding misoperation and improving the efficiency and accuracy of data management. Secondly, by setting the first variable IN and the second variable OUT in the memory, the read-write sequence of the flash memory unit is dynamically managed, which can ensure that data is processed in sequence and avoid unnecessary repeated writing operation, thereby improving the reliability and efficiency of data management. In addition, the first flag bit and the second flag bit of the target data are modified respectively, which can accurately control the writing and deleting operation of data without affecting other states. When deleting, only the flag bit needs to be modified to logically delete the data without physically erasing the data in the flash memory. This non-destructive deletion method reduces the erase-write times of the flash memory and can further prolong the service life of the flash. When power recovery, the first variable IN and the second variable OUT in the memory are reset according to the mark byte state of the target data stored in the flash, which ensures that the system can normally operate under abnormal conditions without frequent modification of the flash.

[0090] The second embodiment

[0091] Referring to Figure 7 In the second embodiment of the application, an electronic device is disclosed, which comprises a flash memory, a memory and a CPU; the flash memory is used for storing target data; the memory is used for saving the first variable, the second variable and the target data; and the CPU is used for the read-write method of the flash in the above embodiment. For details, please refer to the above embodiment, which will not be described in detail here.

[0092] The electronic device provided in the embodiment has the same technical effects as the method for reading and writing the flash in the first embodiment.

[0093] In addition, it can be understood that the above-mentioned various embodiments are only exemplary descriptions of the application, and the technical solutions of each embodiment can be arbitrarily combined and used without conflict in technical features, contradiction in structure and violation of the purpose of the application.

[0094] In the several embodiments of the application, it should be understood that the disclosed method and device can be implemented in other ways.

[0095] It should be pointed out finally that the above embodiments are only used to illustrate the technical solutions of the present application, but not to limit the same; and although the present application has been described in detail with reference to the foregoing embodiments, it should be appreciated by those skilled in the art that the technical solutions recorded in the foregoing embodiments can be modified, or some technical features thereof can be replaced equivalently; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application.

Claims

1. A method for reading and writing flash memory, characterized in that, include: The original data is pre-added with flag bytes to form target data. The flag bytes include a first flag bit and a second flag bit. The first flag bit indicates whether the target data has been written to the flash storage unit, and the second flag bit indicates whether the target data should be deleted from the flash storage unit. A first variable and a second variable are pre-set in memory. The first variable stores the flash cell number to be written next, and the second variable stores the flash cell number to be read next. When the target data is to be written, the first flag bit of the target data in memory is modified, the second flag bit is kept unchanged, the target data is written to the flash storage unit corresponding to the first variable, and the unit number of the first variable in memory is incremented by 1. The target data is read from the flash storage unit corresponding to the second variable, the second flag bit of the target data in the flash is modified, the first flag bit is kept unchanged, and the unit number where the second variable is stored in memory is incremented by 1; The flag byte is 8 bits. When the data in the flash storage unit is erased, the flag byte is 11111111. The first flag bit corresponds to the least significant bit, and the second flag bit corresponds to the second least significant bit. When the target data has been written to the flash storage unit, the flag byte is 1111 1110; when the target data should be deleted from the flash storage unit, the flag byte is 1111 1100.

2. The method according to claim 1, characterized in that, The unit numbering is specifically as follows: based on a preset byte length, the flash space is divided into N storage units according to the address from low to high; the first storage unit to the Nth storage unit correspond to unit number 1 to unit number N; When the cell number currently stored in the first variable IN is N, after the increment operation, the cell number stored in the first variable will be 1. When the cell number currently stored in the second variable OUT is N, after the increment operation, the cell number stored in the second variable becomes 1.

3. The method according to claim 1, characterized in that, After incrementing the cell number stored in the first variable in memory by 1, the method further includes: Determine whether the updated cell number stored in the first variable is equal to the cell number stored in the second variable; If so, delete the storage unit data corresponding to the unit number saved by the second variable, or discard the target data to be stored.

4. The method according to claim 2, characterized in that, When power is lost, the cell numbers stored in the first and second variables set in memory are deleted.

5. The method according to claim 4, characterized in that, Upon restart, the flag byte in the target data stored in flash is determined, and the unit number stored in the first variable and the second variable is set according to the values ​​of the first flag bit and the second flag bit of the flag byte.

6. The method according to claim 5, characterized in that, When the first flag bit is 0 and the second flag bit is 1, it indicates that the flash memory unit is a valid unit; when the first flag bit and the second flag bit are other values, it indicates that the flash memory unit is an invalid unit. The step of setting the unit number stored in the first variable and the second variable according to the values ​​of the first flag bit and the second flag bit of the flag byte specifically includes: setting the unit number stored in the first variable and the second variable according to the searched valid unit or invalid unit.

7. The method according to claim 6, characterized in that, The step of setting the unit number stored in the first variable and the second variable based on the searched valid or invalid units specifically includes: If there are no valid cells in all storage cells in the flash, then the cell number stored in the first variable is set to 1, and the cell number stored in the second variable is also set to 1. If all storage cells in the flash memory are valid cells, then the cell number stored in the first variable is set to 1, and the cell number stored in the second variable is set to N.

8. The method according to claim 6, characterized in that, The step of setting the unit number stored in the first variable and the second variable based on the searched valid or invalid units specifically includes: The search begins with memory cell number 1. Upon finding the first valid cell, its cell number A_1 is recorded. The search then continues from cell number A_1. If no invalid cell is found, the cell number stored in the first variable is set to 1, and the cell number stored in the second variable is set to A_1; If an invalid cell is found, the cell number B of the invalid cell is recorded. The search starts from cell number B. If no valid cell is found, the cell number stored in the first variable is set to B, and the cell number stored in the second variable is set to A_1. If a valid cell is found, the search ends, the cell number A_2 of the valid cell is recorded, the cell number stored in the first variable is set to B, and the cell number stored in the second variable is set to A_2.

9. An electronic device, characterized in that, include: Flash memory, RAM, and CPU; The flash memory is used to store the target data; The memory is used to store the first variable, the second variable, and the target data; The CPU is configured to execute a method for reading and writing flash memory as described in any one of claims 1 to 8.

Citation Information

Patent Citations

  • Environment variable writing method, storage medium and electronic device

    CN110764704A

  • Power failure data storage method, single-chip microcomputer and computer readable storage medium

    CN112817527A