Flash read-write method and electronic equipment
By adding flag bytes to the original data and setting variables in memory, dynamically managing the read and write process of Flash memory, the problem of shortening the Flash life in DTOF application scenarios is solved, efficient and reliable data management is achieved, and the service life of Flash is extended.
Patent Information
- Application Number
- CN202510137541.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-07
- Publication Date
- 2025-05-27
- Estimated Expiration
- 2045-02-07
AI Technical Summary
In DTOF application scenarios, microcontrollers need to frequently save data to Flash memory. In the prior art, file system methods occupy storage space and consume processor resources. Direct access to memory requires the number of write and erases of Flash, resulting in a shortening of Flash life.
Using a method of reading and writing to flash, by adding flag bytes to the original data in advance and setting the first variable and the second variable in memory, the read and write order of the flash memory unit is dynamically managed to ensure that the data is processed in sequence and avoid unnecessary repeated write operations.
Effectively manage the data reading and writing process of Flash memory, reduce the number of Flash erasing times, extend the service life of Flash, improve the efficiency and reliability of data management, and ensure that the system can operate normally in abnormal situations.
Smart Images

Figure CN120045136A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of data storage, and in particular, to a method for reading and writing flash and an electronic device. Background Art
[0002] In some DTOF application scenarios (offline log supplementary transmission, offline ranging result supplementary transmission) where a single-chip microcomputer is used as a controller, it is necessary to save the data generated irregularly or regularly during the DTOF working process to the internal or external Flash memory of the single-chip microcomputer. Among them, there are generally two ways to save data from the single-chip microcomputer to the flash, one is based on the file system, and the other is to directly access the memory.
[0003] Among them, the file system method requires a certain amount of storage space to store the metadata of the file system (such as directory structure, file allocation table, etc.) and cache data. For a single-chip microcomputer with limited storage space, this may become a problem. In addition, the file system also needs to consume a certain amount of processor resources to execute file operations, which may affect the overall performance of the single-chip microcomputer. The read and write operations of the file system are usually much more complex than directly accessing the storage device because they need to handle various operations such as opening, closing, reading, writing, and positioning of files, and these operations may introduce additional delays and overheads, thus affecting the real-time performance and response speed of the single-chip microcomputer. However, for the direct access memory method, it is necessary to consider the write and erase times of the Flash, as well as the page size and erase block size of the Flash, to avoid shortening the Flash life due to frequent writing of DTOF data.
[0004] Therefore, how to design a method for reading and writing flash that can effectively manage the data reading and writing process of the Flash memory to extend the service life of the Flash is a technical problem that needs to be solved urgently at present. Summary of the Invention
[0005] The purpose of this application is to overcome the above technical problems and provide a method for reading and writing flash and an electronic device, which can effectively manage the data reading and writing process of the Flash memory to extend the service life of the Flash.
[0006] In the first aspect, an embodiment of this application discloses a method for reading and writing flash, and the following scheme is adopted: A method for reading and writing flash, comprising: Add flag bytes to the original data in advance to form target data, where 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; Set a first variable and a second variable in the memory in advance. The first variable stores the flash cell number to be written next time, and the second variable stores the flash cell number to be read next time; When writing the target data, modify the first flag bit of the target data in the memory, keep the second flag bit unchanged, write the target data into the flash storage unit corresponding to the first variable, and increment the cell number stored in the first variable in the memory by 1; Or, read the target data from the flash storage unit corresponding to the second variable, modify the second flag bit of the target data in the flash, keep the first flag bit unchanged, and increment the cell number stored in the second variable in the memory by 1.
[0007] By adopting the above technical solutions, the data reading and writing process of the Flash memory can be effectively managed. First, by adding flag bytes to the original data, the status of the data (written or to be deleted) can be clearly identified, which enables the system to quickly judge the status of the data when reading the data, thus avoiding misoperations and improving the efficiency and accuracy of data management. Second, by setting the first variable and the second variable in the memory to dynamically manage the reading and writing order of the flash cells, the data can be processed in sequence, avoiding unnecessary repeated writing operations, and improving the reliability and efficiency of data management. In addition, by modifying the first flag bit and the second flag bit of the target data respectively, the writing and deletion operations of the data can be precisely controlled without affecting other statuses. When performing deletion, 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 number of erase and write operations of the flash memory and can further extend the service life of the Flash.
[0008] Optionally, the cell number is specifically: Based on a preset byte length, divide the flash space in the direction from low address to high address to obtain N storage cells; The first storage cell to the Nth storage cell correspond to cell numbers 1 to cell 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 is 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 is 1.
[0009] By adopting the above technical solution, it can be ensured that when the flash storage unit is recycled, data loss or incorrect writing will not occur due to variable out-of-range. Specifically: the flash space is divided in the direction from low to high address, and each storage unit is assigned a unique number, making data management more orderly and efficient. When the unit number saved by the first variable reaches the maximum value N, it is automatically reset to 1, ensuring that the write operation is always within the valid range and avoiding out-of-bounds problems. Similarly, when the unit number saved by the second variable reaches the maximum value N, it is also automatically reset to 1, ensuring that the read operation is also within the valid range and preventing incorrect data from being read.
[0010] Optionally, the flag byte is 8 bits. When the data in the flash storage 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 storage unit, the flag byte is 1111 1110, and when the target data should be deleted from the flash storage unit, the flag byte is 1111 1100.
[0011] By adopting the above technical solution, the state of the flash storage unit can be accurately identified, ensuring the correct writing and deletion of data. Specifically: when the target data has been written into the flash storage unit, the flag byte becomes 1111 1110, clearly indicating that the unit has been used. When the target data should be deleted from the flash storage unit, the flag byte becomes 1111 1100, clearly marking that the data in the unit needs to be removed. These specific flag bit settings help improve the reliability of the system and data management efficiency.
[0012] Optionally, after incrementing by 1 the unit number saved by the first variable in the memory, it further includes: determining whether the updated unit number saved by the first variable is equal to the unit number saved by the second variable; if so, deleting the storage unit data corresponding to the unit number saved by the second variable, or discarding the target data to be stored.
[0013] By adopting the above technical solution, the overflow problem of the flash storage unit can be effectively avoided. When the unit number saved by the first variable after incrementing by 1 is the same as the unit number saved by the second variable, it indicates 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 abandon the newly written target data, thereby ensuring the stability and reliability of the system and preventing data loss or system crash caused by insufficient storage space.
[0014] Optionally, when power is off, the unit numbers saved by the first variable and the second variable set in the memory are deleted.
[0015] By adopting the above technical solution, when power is lost, the unit numbers saved in the first variable and the second variable set in the memory are deleted, thus avoiding the problem of data chaos caused by accidental power-off. This solution ensures that the system can correctly restore its state after restart, improving the stability and reliability of the system.
[0016] Optionally, when restarting, the flag byte in the target data stored in the flash is judged to set the unit numbers saved 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.
[0017] By adopting the above technical solution, it can ensure that the unit numbers of the first variable and the second variable in the memory are accurately restored after the system restarts, avoiding data loss and duplicate writing problems. Specifically: when restarting, by judging the flag byte of the target data stored in the flash, it can be identified which data is valid and which needs to be deleted or ignored. Setting the unit numbers saved 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 ensures the continuity and consistency of the system, reducing the risk of data chaos caused by power-off. This mechanism improves the reliability and stability of the system, especially in the case of frequent power-off or accidental restart, ensuring data consistency and integrity.
[0018] Optionally, when the first flag bit is 0 and the second flag bit is 1, it indicates that this flash storage unit is a valid unit; when the first flag bit and the second flag bit are other values, it indicates that this flash storage unit is an invalid unit; setting the unit numbers saved 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 numbers saved in the first variable and the second variable according to the searched valid unit or invalid unit.
[0019] By adopting the above technical solution, it can effectively manage the data state in the Flash memory and ensure accurate restoration of the read / write position during restart. Specifically: when the first flag bit is 0 and the second flag bit is 1, it indicates that this Flash storage unit is a valid unit, that is, the data in this unit can be normally read and used. When the first flag bit and the second flag bit are other values, it indicates that this Flash storage unit is an invalid unit, that is, the data in this unit has been marked as deleted or should no longer be used. When the system restarts, by detecting the flag byte of each storage unit, it can quickly determine which units are valid and which units are invalid, so as to correctly set the first variable and the second variable in the memory, avoiding data loss or incorrect reading. These measures improve the reliability and stability of the system and reduce problems caused by improper data management.
[0020] Optionally, setting the cell numbers saved in the first variable and the second variable according to the searched valid cells or invalid cells specifically includes: If there are no valid cells among all the storage cells in the flash, set the cell number saved in the first variable to 1 and set the cell number saved in the second variable to 1; If all the storage cells in the flash are valid cells, set the cell number saved in the first variable to 1 and set the cell number saved in the second variable to N.
[0021] By adopting the above technical solution, the state of flash storage can be quickly restored after the system restarts, avoiding the problem of data loss caused by power failure. Specifically, when there are no valid cells among all the storage cells in the flash, initialize the first variable and the second variable to 1 to ensure that the system can write new data orderly from the beginning; when all the storage cells are valid cells, set the first variable to 1 and the second variable to N to ensure that the read operation ends at the last valid cell, thereby maintaining the consistency and integrity of the data.
[0022] Optionally, setting the cell numbers saved in the first variable and the second variable according to the searched valid cells or invalid cells specifically includes: Start searching from the storage cell with cell number 1, search for the first valid cell, record the cell number A_1 of the valid cell, and start searching from cell number A_1: If no invalid cell is found, set the cell number saved in the first variable to 1 and set the cell number saved in the second variable to A_1; If an invalid cell is found, record the cell number B of the invalid cell, start searching from cell number B. If no valid cell is found, set the cell number saved in the first variable to B and set the cell number saved in the second variable to A_1; If a valid cell is found, end the search, record the cell number A_2 of the valid cell, then set the cell number saved in the first variable to B and set the cell number saved in the second variable to A_.
[0023] By adopting the above technical solutions, it is possible to effectively manage the data in the flash memory and ensure the correct restoration of the read / write state after a restart. Specifically: When searching from the storage unit numbered 1 to find the first valid unit (unit number is A_1), if no invalid unit is found subsequently, the first variable and the second variable are respectively set to 1 and A_1, ensuring a reasonable starting position for the next write and read. If an invalid unit (unit number is B) is found when searching starting from A_1, further search is carried out: If no new valid unit is found, the first variable is set to B and the second variable is set to A_1, avoiding the repeated use of invalid units. 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, skipping the intermediate invalid units and improving the storage efficiency. Generally, this solution optimizes the data management and restoration process of the flash memory, reduces the impact of invalid data, and improves the reliability and performance of the system.
[0024] In a second aspect, another embodiment of the present application discloses an electronic device, adopting the following solution: An electronic device includes: a flash memory, a memory, and a CPU; The flash memory is used to store the target data; The memory is used to save the first variable, the second variable, and the target data; The CPU is used to execute the above-mentioned method for reading and writing the flash.
[0025] By adopting the above technical solutions, it is possible to effectively manage the data read / write process of the flash memory. By adding flag bytes to the original data and setting corresponding variables in the memory, precise control of the data state is achieved. Especially in the case of power failure, the read / write position can be accurately restored after the system restarts, avoiding data loss and repeated writing problems. In addition, by judging the state of the flag bytes, valid and data to be deleted can be quickly identified, improving the reliability and efficiency of the system.
[0026] In summary, the present application includes at least one of the following beneficial technical effects: 1. By pre-adding flag bytes to the original data and setting the first variable and the second variable in the memory, an efficient data management mechanism is realized, effectively reducing the number of erasures of the Flash and prolonging the service life of the Flash; 2. When writing or reading the target data, by modifying the first flag bit and the second flag bit of the flag byte, the consistency and integrity of the data are ensured, improving the reliability and stability of the system; 3. When power is restored after a power failure, the first and second variables in the memory are reset according to the status of the flag bytes of the target data stored in the flash memory, ensuring that the system can operate normally even in abnormal situations and enhancing the fault tolerance of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] The above and other objects, features, and advantages of the present application will become more apparent by describing the exemplary embodiments of the present application in more detail in conjunction with the accompanying drawings, wherein, in the exemplary embodiments of the present application, the same reference numerals generally represent the same components.
[0028] Figure 1 Schematic flowchart of a method for reading and writing flash in an embodiment of the present application; Figure 2 Schematic diagram of the composition of target data disclosed in an embodiment of the present application; Figure 3 Schematic diagram of the first flag bit and the second flag bit disclosed in an embodiment of the present application; Figure 4 Schematic diagram of forming a circular queue in the flash space disclosed in an embodiment of the present application; Figure 5 Schematic diagram of the initial state, data writing, and data reading states of the flag byte disclosed in an embodiment of the present application; Figure 6 Schematic flowchart of setting the first and second variables after restart disclosed in an embodiment of the present application; Figure 7 Schematic diagram of the structure of an electronic device disclosed in another embodiment of the present application. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0029] Embodiments of the present application will be described in more detail below with reference to the accompanying 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 set forth herein. On the contrary, these embodiments are provided to make the present application more thorough and complete, and to fully convey the scope of the present application to those skilled in the art.
[0030] The terms used in the present application are for the purpose of describing specific embodiments only and are not intended to limit the present application. The singular forms "a" and "the" used in the present application and the appended claims are also intended to include the plural forms unless the context clearly dictates otherwise. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more of the associated listed items.
[0031] It should be understood that although the terms "first", "second", etc. may be used in this application to describe various information, such information should not be limited to these terms. These terms are only used to distinguish information of the same type from each other. For example, without departing from the scope of this application, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Thus, features defined with "first" and "second" may explicitly or implicitly include one or more of such features. In the description of this application, the meaning of "a plurality" is two or more, unless otherwise specifically defined.
[0032] The technical solutions of the embodiments of this application will be described in detail below with reference to the accompanying drawings.
[0033]
First Embodiment
[0034] See Figure 1 , a method for reading and writing flash includes the following steps: S10. Add a flag byte to the original data in advance to form target data, where the flag byte includes a first flag bit and a second flag bit. The first flag bit indicates whether the target data has been written into the flash storage unit, and the second flag bit indicates whether the target data should be deleted from the flash storage unit; set a first variable IN and a second variable OUT in the memory in advance. The first variable IN stores the flash cell number to be written next time, and the second variable OUT stores the flash cell number to be read out next time.
[0035] Specifically, the original data is fixed-length or variable-length data. After adding the flag byte, it forms target data, as Figure 2 shown in the schematic diagram of the target data composition. In this 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 position in the byte is not limited.
[0036] For example, see Figure 3, when the flag byte is 1 byte, the corresponding flag byte is 8 bits. Define the first flag bit A corresponding to the lowest bit, and define the second flag bit B corresponding to the second 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 cell 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 cell. When it is 0, it indicates that the target data has been written into the flash memory cell. When the second flag bit B is 1, it indicates that the target data should not be deleted from the flash memory cell. When it is 0, it indicates that the target data should be deleted from the flash memory cell. Thus, based on the flag byte added to the original data, the write and delete states of the data can be effectively managed to reduce unnecessary erase operations.
[0037] See Figure 4 , in this embodiment, the specific numbering of the flash memory cells is as follows: based on the preset byte length, the flash space is divided in the direction from the lowest address to the highest address, and each storage cell is assigned a unique number to obtain N storage cells. The first storage cell to the Nth storage cell correspond to cell number 1 to cell number N.
[0038] Among them, when the cell number currently saved by the first variable IN is N (that is, when it reaches the maximum value), after the increment operation, the cell number saved by the first variable IN is automatically reset to 1 to ensure that the write operation is always within the valid range and avoid out-of-bounds problems. Similarly, when the cell number currently saved by the second variable OUT is N (that is, when it reaches the maximum value), after the increment operation, the cell number saved by the second variable OUT is also automatically reset to 1 to ensure that the read operation is also within the valid range and prevent reading incorrect data.
[0039] S20. When writing the target data, modify the first flag bit of the target data in the memory, keep the second flag bit unchanged, write the target data into the flash memory cell corresponding to the first variable IN, and increment the cell number saved by the first variable IN in the memory; Among them, taking the flag byte as 8 bits as an example, when the data in the flash memory cell is erased, the flag byte is 11111111 (0xFF, where 0x means hexadecimal). As the initial state of the marker byte, the first flag bit corresponds to the lowest bit, and the second flag bit corresponds to the second lowest bit. When writing the target data, modify the first flag bit of the target data in the memory and keep the second flag bit unchanged, then the corresponding flag byte is 1111 1110 (0xFE), see Figure 5 .
[0040] Further, after incrementing by 1 the cell number saved in the first variable IN in memory in step S20, in order to avoid the overflow problem of flash storage cells, in this embodiment, it further includes: S21. Determine whether the updated cell number saved in the first variable IN is equal to the cell number saved in the second variable OUT; Among them, when the cell number saved in the first variable IN after incrementing by 1 is the same as the cell number saved in the second variable OUT, it indicates that the flash storage space is full.
[0041] S22. If so, delete the stored cell data corresponding to the cell number saved in the second variable OUT, or discard the target data to be stored.
[0042] Among them, when it is determined that the flash storage space is full, the system automatically deletes the stored cell data corresponding to the second variable OUT, or abandons the newly written target data, so as to ensure the stability and reliability of the system and prevent data loss or system crash caused by insufficient storage space.
[0043] S30. Read the target data from the flash storage cell corresponding to the second variable OUT, modify the second flag bit of the target data in the flash, keep the first flag bit unchanged, and increment by 1 the cell number saved in the second variable OUT in memory.
[0044] See Figure 4 , when reading the target data, modify the second flag bit of the target data in the flash, keep the first flag bit unchanged, and the corresponding flag byte is 1111 1100 (0xFC), see Figure 5 .
[0045] Further, when the device loses power, the cell numbers saved in the first variable IN and the second variable OUT set in the memory are deleted.
[0046] Correspondingly, when restarting, by re-obtaining 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 pre-stored in the target data, the cell numbers of the first variable IN and the second variable OUT can be reset through the following step S40, see Figure 6 , specifically as follows: S40. Judge the flag byte in the target data stored in the flash, and 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.
[0047] Among them, when the first flag bit is 0 and the second flag bit is 1, it indicates that the flash memory cell is a valid cell; when the first flag bit and the second flag bit are other values, that is, 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, it indicates that the flash memory cell is an invalid cell. Thus, in this embodiment, by detecting the flag byte of each memory cell, it is possible to quickly determine which cells are valid and which cells are invalid, so as to correctly set the first variable IN and the second variable OUT in the memory, and re-find the next position in the flash memory space to be written or read.
[0048] This step S40 sets the cell numbers saved in the first variable IN and the second variable OUT according to the valid cells or invalid cells searched, specifically including: S41. If there are no valid cells among all the memory cells in the flash, set the cell number saved in the first variable IN to 1, and set the cell number saved in the second variable OUT to 1; If there are no valid cells among all the memory cells in the flash, it means the flash is empty. Initialize the first variable IN and the second variable OUT to 1, and new data can be written to the flash from the first address, and data can also be read from the first address.
[0049] S42. If all the memory cells in the flash are valid cells, set the cell number saved in the first variable IN to 1, and set the cell number saved in the second variable OUT to N.
[0050] If all the memory cells in the flash are valid cells, it means the flash is full. Set the first variable IN to 1 and the second variable OUT to N, and new data can be written to the flash from the first address, and data can be read from the Nth address.
[0051] Furthermore, referring to Figure 6, step S40 also specifically includes: S43. Start searching from the memory cell with cell number 1, search for the first valid cell, record the cell number A_1 of the valid cell, and start searching from cell number A_1: S44. If no invalid cells are found, set the cell number saved in the first variable IN to 1, and set the cell number saved in the second variable OUT to A_1; Among them, when starting from the memory cell with cell number 1 and searching for the first valid cell (the cell number is A_1), if no invalid cells are found subsequently, set the first variable IN and the second variable OUT to 1 and A_1 respectively, ensuring that the starting positions for the next write and read are reasonable.
[0052] S45. If an invalid cell is found, record the cell number B of the invalid cell and start searching from cell number B; S46. If no valid cell is found, set the cell number saved in the first variable IN to B, and set the cell number saved in the second variable OUT to A_1; S47. If a valid cell is found, record the cell number A_2 of the valid cell, then set the cell number saved in the first variable IN to B, and set the cell number saved in the second variable OUT to A_2.
[0053] Among them, if an invalid cell (cell number is B) is found when searching starts from A_1, then further search: If no new valid cell is found, set the first variable IN to B and the second variable OUT to A_1, avoiding the reuse 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, which can improve the storage efficiency.
[0054] In this way, steps S41 - S47 implement the restoration process of the cell numbers saved in the first variable IN and the second variable OUT, maintaining the consistency and integrity of the data, and improving the reliability and performance of the system.
[0055] In summary, by adding a flag byte to the original data, the present invention can clearly identify the status of the data (written or to be deleted), which enables the system to quickly determine the status of the data when reading the data, thereby avoiding misoperations and improving the efficiency and accuracy of data management. Secondly, by setting the first variable IN and the second variable OUT in the memory to dynamically manage the read - write order of the flash memory cells, it can ensure that the data is processed in order, avoid unnecessary repeated write operations, and improve the reliability and efficiency of data management. In addition, by modifying the first flag bit and the second flag bit of the target data respectively, it can precisely control the write and delete operations of the data without affecting other states. And when performing deletion, 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 number of erase - write operations of the flash memory and can further extend the service life of the Flash. When power is restored after a power failure, the first variable IN and the second variable OUT in the memory are reset according to the status of the flag byte of the target data stored in the flash memory, ensuring that the system can also operate normally in abnormal situations without frequently modifying the flash.
[0056]
Second Embodiment
[0057] The technical effect of the electronic device provided in this embodiment during actual application is the same as that of the method of reading and writing to the flash in the first embodiment.
[0058] In addition, it can be understood that the foregoing embodiments are only exemplary descriptions of the present invention. On the premise that the technical features do not conflict, the structures are not contradictory, and the invention purpose of the present invention is not violated, the technical solutions of each embodiment can be arbitrarily combined and used in combination.
[0059] In several embodiments provided by the present invention, it should be understood that the disclosed methods and devices can be implemented in other ways.
[0060] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions recorded in the foregoing embodiments, or perform equivalent replacements for some of the technical features; 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 each embodiment of the present invention.
Claims
1. A method for reading and writing flash, characterized in that: include: Pre-adding 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 indicates whether the target data has been written into the flash storage unit, and the second flag bit indicates whether the target data should be deleted from the flash storage unit; Pre-setting a first variable and a second variable in the memory, wherein the first variable stores the flash unit number to be written next time, and the second variable stores the flash unit number to be read next time; 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 storage unit corresponding to the first variable, and the unit number stored in the first variable in the memory is increased by 1; Or, the target data is read out 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 1 is added to the unit number stored in the second variable in the memory.
2. The method according to claim 1, characterized in that: The unit numbering is specifically as follows: based on the preset byte length, the flash space is divided in the direction of address from low to high to obtain N storage units; the first storage unit to the Nth storage unit correspond to unit numbers 1 to unit numbers N; When the unit number currently stored in the first variable IN is N, after the addition operation, the unit number stored in the first variable is 1; When the unit number currently stored in the second variable OUT is N, after the addition operation, the unit number stored in the second variable is 1.
3. The method according to claim 1, characterized in that: The flag byte is 8 bits. When the data in the flash storage 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 storage unit, the flag byte is 1111 1110, and when the target data should be deleted from the flash storage unit, the flag byte is 1111 1100.
4. The method according to claim 1, characterized in that: After the unit number of the first variable stored in the memory is increased by 1, the method further includes: Determining whether the updated unit number stored in the first variable is equal to the unit number stored in the second variable; If so, delete the storage unit data corresponding to the unit number stored in the second variable, or discard the target data to be stored.
5. The method according to claim 2, characterized in that: When power is lost, the unit numbers saved by the first variable and the second variable set in the memory are deleted.
6. The method according to claim 5, characterized in that: 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.
7. The method according to claim 6, characterized in that: 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; The step of setting the unit numbers 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 numbers stored in the first variable and the second variable according to the searched valid unit or invalid unit.
8. The method according to claim 7, characterized in that: The step of setting the unit numbers stored in the first variable and the second variable according to the searched valid unit or invalid unit specifically includes: If there is no valid unit in all storage units in the flash, the unit number stored in the first variable is set to 1, and the unit number stored in the second variable is set to 1; If all storage units in the flash are valid units, the unit number stored in the first variable is set to 1, and the unit number stored in the second variable is set to N.
9. The method according to claim 7, characterized in that: The step of setting the unit numbers stored in the first variable and the second variable according to the searched valid unit or invalid unit specifically includes: Start searching from the storage unit with unit number 1, search for the first valid unit, record the unit number A_1 of the valid unit, and start searching from unit 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, and the search is started from the 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 is terminated, 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.
10. 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 used to execute a method for reading and writing flash as described in any one of claims 1 to 9.
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
Flash memory and wear leveling method thereof
US20240265964A1