Method for reading and writing data in Flash memory
By dividing units in Flash memory and optimizing write and erase operations using check bits, the timing inaccurate and misjudgment damage caused by frequent writes is solved, which improves the read and write speed and extends the service life.
Patent Information
- Application Number
- CN202111604778.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-24
- Publication Date
- 2024-12-31
- Estimated Expiration
- 2041-12-24
AI Technical Summary
The data reading and writing methods of existing Flash memory have problems such as frequent writing, inaccurate timing, abnormal communication and misjudgment damage, which affects system stability and is wasted service life.
By dividing the Flash memory into several units and setting up a check bit in each unit, the data storage situation is judged based on the check bit data, the write and erase operations are optimized, and the write error count threshold is used to determine whether data is required. Erase and issue a service life detection reminder.
It improves the read and write speed of Flash memory, avoids misjudgment damage and waste of service life, and extends the service life of the memory.
Smart Images

Figure CN114461537B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of memory technology, and in particular to a method for reading and writing data in a Flash memory. Background Art
[0002] At present, the commonly used memories of single-chip microcomputers are EEPROM and Flash. Among them, the cost of built-in EEPROM is relatively high, and the external EEPROM requires the addition of peripheral circuits. Compared with the use of EEPROM, the use of built-in Flash memory can effectively solve the cost problem. The storage method of Flash memory is usually to erase first and then write. Since the single erase time is long, frequent erase operations will cause timing inaccuracy and communication abnormalities, thereby affecting the stability of the entire system.
[0003] In the related art, the patent document with publication number CN112286452A discloses a STM32 single-chip FLASH memory storage and reading algorithm. The scheme is to divide the Flash into several small units, set the first unit as the initial address, read the data in each unit from the initial address in sequence, and perform storage, read and write operations only when all the data in the unit is empty. If each unit is full, the erase operation is performed. Although this scheme effectively reduces the number of erases and writes, it is necessary to check the data content in each small unit each time data is written. As the number of units with stored data increases, the time for the scheme to check the empty unit will also increase accordingly, which is not conducive to the fast reading and writing of the Flash memory.
[0004] In the prior art, the patent document with publication number CN105426317B also provides a storage method and a reading method for storing user data in Flash. Before storing a new data packet, the storage block a is first checked for "empty" by area. After finding the first "empty" area whose first address is closest to the first address of the storage block a, the packaged data packet is started to be written. After the data writing action is completed, the written data is verified to see whether it has been written successfully. If it has not been written successfully, the writing action is performed again; if the writing action is executed M times and still fails, the next "empty" area is searched, and the above writing operation is performed again. If X areas cannot be written successfully, it is considered that the storage block a is damaged and the storage operation is exited. This scheme directly determines that the Flash memory is damaged if X areas cannot be written successfully, which may result in misjudgment operation. In fact, during the use of the Flash memory, there are situations where the reading and writing failures occur several times after a certain number of erasures. However, after several reading and writing failures, the Flash memory can be read and written successfully again after being erased again. The above scheme directly determines that the Flash memory is damaged. In the long run, the available storage area will become less and less, resulting in a waste of the service life of the Flash memory. Summary of the invention
[0005] In order to overcome the problems existing in the related art, the present application provides a data reading and writing method of a Flash memory, which can improve the reading and writing speed of the Flash memory and avoid wasting its service life.
[0006] The present application provides a method for reading and writing data in a Flash memory, comprising:
[0007] Get the data to be stored;
[0008] Read the check bit data of the mth unit, where m is an integer greater than or equal to zero;
[0009] Determine the data storage status of the mth unit according to the check bit data of the mth unit, if there is no stored data in the mth unit, determine whether the data to be stored is written successfully after the data to be stored is written into the mth unit, if so, add one to m and clear the write error count before executing the step of obtaining the data to be stored;
[0010] If not, the write error count is increased by one, and it is determined whether the write error count is greater than or equal to the first write error count threshold. If so, the Flash memory is erased and m and the write error count are cleared to zero, and then the data storage program is terminated and a service life detection reminder is issued.
[0011] In one implementation, after determining whether the write error count is greater than or equal to a first write error count threshold, the method further includes:
[0012] If the write error count is less than the first write error count threshold, determine whether the write error count is greater than or equal to the second write error count threshold, and if so, read the data from the Flash memory and then erase the data, and after clearing m, execute the step of writing the data to be stored in the mth unit;
[0013] The first write error count threshold is greater than the second write error count threshold.
[0014] In one implementation, determining the data storage status of the m-th unit according to the check bit data of the m-th unit includes:
[0015] If the check bit data of the m-th unit is a null value, then there is no stored data in the m-th unit;
[0016] If the check bit data of the m-th unit is check information, then the m-th unit contains storage data.
[0017] In one implementation manner, after determining the data storage status of the m-th unit according to the check bit data of the m-th unit, the method includes:
[0018] If the mth unit contains stored data, then the step of reading the check bit data of the mth unit is performed after adding one to the m.
[0019] In one implementation, before reading the check bit data of the mth unit, the method includes:
[0020] The Flash memory is divided into n units according to a preset byte length, where n is a positive integer greater than one.
[0021] A full load flag is provided in the Flash memory. When the check bit data of the nth unit is not a null value, the state of the full load flag is recorded as full load, otherwise the state of the full load flag is recorded as not full load.
[0022] In one implementation, erasing data from the Flash memory includes:
[0023] Erasing starts from the starting area of the Flash memory, and after erasing is completed, the state of the full load flag is updated to non-full load, and the data erasing program is exited;
[0024] During the erasing process, if a system function instruction is obtained, the full load mark is maintained unchanged, and the system function instruction is executed after exiting the data erasing program, and the state of the full load mark is re-detected after the execution of the system function instruction is completed.
[0025] In one embodiment, the data reading and writing method of the Flash memory further includes:
[0026] When a data erasing instruction is obtained, the state of the full load mark is detected, and when the state of the full load mark is full, an erasing function is called once, and after the erasing is completed, the state of the full load mark is updated to non-full, and the data erasing program is exited;
[0027] During the erasing process, if a system function instruction is obtained, the full load mark is maintained unchanged, and the system function instruction is executed after exiting the data erasing program, and the state of the full load mark is re-detected after the execution of the system function instruction is completed.
[0028] In one implementation, reading data from the Flash memory includes:
[0029] Clearing m to zero;
[0030] Reading the check bit data of the mth unit;
[0031] Determining the data storage status of the mth unit according to the check bit data of the mth unit, if the mth unit contains stored data, reading the data stored in the mth unit and adding 1 to m, and then executing the step of reading the check bit data of the mth unit;
[0032] If there is no stored data in the mth unit, it is determined whether m is greater than zero. If so, the data reading procedure is terminated.
[0033] In one implementation, after determining whether m is greater than zero, the following steps are performed:
[0034] If m is equal to zero, a prompt indicating that the Flash memory is empty is issued and the data reading procedure is terminated.
[0035] The technical solution provided by this application may have the following beneficial effects:
[0036] The solution provided by the present application divides the Flash memory into several units, and a check bit is set in each unit. The data storage status of the mth unit is judged according to the check bit data. When it is determined that the current unit, that is, the mth unit, has no stored data, the action of writing the data to be stored in the mth unit is executed, and m is increased by one to make the pointer point to the next unit of the current unit in the Flash memory, so that when the data storage operation is performed next time, there is no need to start from the initial unit to check the unit empty, and the new unit can be directly reached for empty check and data storage operation, which saves a lot of unit empty check time; and in the solution described in the present application, in the current unit to be stored, the data is written into the mth unit, and the m is increased by one to make the pointer point to the next unit of the current unit in the Flash memory, so that when the data storage operation is performed next time, there is no need to start from the initial unit to check the unit empty from the beginning, and the new unit can be directly reached for empty check and data storage operation, which saves a lot of unit empty check time. When the storage data fails to be written, the write error count is increased by one to count the number of times the current data to be stored fails to be written. When the write error count of the current data to be stored is greater than or equal to the first write error count threshold, the Flash memory is erased and m and the write error count are cleared. At the same time, the data storage program is terminated and a service life detection reminder is issued to remind the operator to detect the service life of the Flash memory to identify whether the cause of the write failure is damage caused by the use time of the Flash memory exceeding its service life, or whether it is just a read-write BUG that occurs during the use of the Flash memory, so as to prevent waste caused by misjudgment of damage.
[0037] It should be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] The above and other objects, features and advantages of the present application will become more apparent through a more detailed description of exemplary embodiments of the present application in conjunction with the accompanying drawings, wherein the same reference numerals generally represent the same components in the exemplary embodiments of the present application.
[0039] Figure 1It is a flowchart of a method for reading and writing data in a Flash memory shown in an embodiment of the present application;
[0040] Figure 2 It is a flowchart of a method for reading data from a Flash memory according to an embodiment of the present application;
[0041] Figure 3 It is a flowchart of a method for erasing data from a Flash memory shown in an embodiment of the present application. DETAILED DESCRIPTION
[0042] The preferred embodiments of the present application will be described in more detail below with reference to the accompanying drawings. Although the preferred embodiments of the present application are shown in the accompanying 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. 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.
[0043] The terms used in this application are for the purpose of describing specific embodiments only and are not intended to limit this application. The singular forms of "a", "said" and "the" used in this application and the appended claims are also intended to include plural forms unless the context clearly indicates other meanings. 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 associated listed items.
[0044] It should be understood that although the terms "first", "second", "third", etc. may be used in this application to describe various information, this information should not be limited to these terms. These terms are only used to distinguish the same type of information 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, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features. In the description of this application, the meaning of "multiple" is two or more, unless otherwise clearly and specifically defined.
[0045] The solution provided in the prior art reads the data in each cell from the initial address in sequence, and performs storage, read and write operations only when all the data in the cell is empty, and performs the erase operation only when each cell is full. Although this solution effectively reduces the number of erases and writes, it is necessary to check the data content in each small cell each time data is written. As the number of cells with stored data increases, the time for this solution to check for empty cells will also increase accordingly, which is not conducive to the fast reading and writing of Flash memory.
[0046] Embodiment 1
[0047] The prior art also provides another solution, which directly determines that the Flash memory is damaged when X areas cannot be written successfully, which may result in misjudgment operation. In fact, during the use of the Flash memory, there may be a situation where the reading and writing fails several times after a certain number of erasures. However, after several reading and writing failures, the Flash memory can be read and written successfully again after being erased again. The above solution directly determines that the Flash memory is damaged. In the long run, the available storage area will become less and less, resulting in a waste of the service life of the Flash memory.
[0048] In view of the above problems, an embodiment of the present application provides a method for reading and writing data in a Flash memory.
[0049] The technical solution of the embodiments of the present application is described in detail below with reference to the accompanying drawings.
[0050] Figure 1 It is a flowchart of a method for reading and writing data in a Flash memory according to an embodiment of the present application.
[0051] See also Figure 1 , the data storage method of the Flash memory comprises:
[0052] 101. Obtain data to be stored;
[0053] In the embodiment of the present application, when data to be stored is generated, it means that there is a data storage demand, that is, the data storage program is started and the data to be stored is obtained.
[0054] Furthermore, the FLASH memory currently used for data storage on the market has continuous block area addresses, and usually stores data in bytes and erases data in blocks. In the embodiment of the present application, since the data written each time has a fixed byte length, before storing the data to be stored, the Flash memory is divided into n units according to the preset byte length, where n is a positive integer greater than one.
[0055] In actual application, the preset byte length can be set according to actual needs and is not limited here.
[0056] In actual applications, after the Flash memory is divided once according to a preset byte length, if the storage length of the divided unit does not need to be changed, the Flash memory can be erased and stored repeatedly without performing the division action again; if the storage length of the unit needs to be adjusted, the Flash memory can be divided again according to another preset byte length after the Flash memory is erased.
[0057] Furthermore, a full load flag is provided in the Flash memory. When the check bit data of the nth unit is not a null value, the state of the full load flag is recorded as full load, otherwise the state of the full load flag is recorded as not full load.
[0058] 102. Read the check bit data of the mth unit;
[0059] Here, m is an integer greater than or equal to zero.
[0060] In an embodiment of the present application, a check bit is added to the stored data. When the mth unit contains stored data, the check bit data of the mth unit is read, and the check information of the data in the mth unit can be read; when there is no stored data in the mth unit, the result of reading the check bit data of the mth unit is a null value.
[0061] 103. Determine whether the mth unit contains stored data according to the check bit data of the mth unit;
[0062] When the mth unit contains stored data, execute step 104 and return to step 102;
[0063] When there is no stored data in the mth unit, step 105 and step 106 are executed in sequence.
[0064] Exemplarily, the process of determining whether the mth unit contains stored data according to the check bit data of the mth unit is as follows:
[0065] If the check bit data of the m-th unit is a null value, then there is no stored data in the m-th unit;
[0066] If the check bit data of the m-th unit is check information, then the m-th unit contains storage data.
[0067] In actual application, by reading the check bit data of the mth unit, it is possible to know whether there is data in the mth unit. Assuming the check information is 0xA5, when the check bit data of the mth unit is 0xA5, it means that there is data in the unit. Correspondingly, when the check bit data of the mth unit is 0xFF, it means that the unit is empty.
[0068] It should be noted that the above description of the verification information is only an example given in the embodiment of the present application. In actual application, the specific content of the verification information can be set according to needs.
[0069] 104. Increase m by one;
[0070] When it is determined that the current unit contains storage data, the value of m is increased by one, so that the pointer can point to the next unit of the current unit in the Flash memory, that is, the mth unit is changed to the next unit of the current unit, and so on. The storage position is moved backward one by one from the starting unit according to the divided units, thereby avoiding skipping of units that have not performed write operations.
[0071] 105. Write the data to be stored in the mth unit;
[0072] 106. Determine whether the data to be stored is written successfully;
[0073] If yes, execute step 107 and then return to step 101;
[0074] If not, execute step 108 and step 109.
[0075] 107. Increase m by one and clear the error count;
[0076] When the data to be stored is written successfully, m is increased by one to make the pointer point to the next unit of the current unit in the Flash memory. Therefore, when data storage is needed again, the next unit of the last data storage position can be directly checked for empty space without starting from the starting position, saving a lot of unit empty space checking time; and the write error count is cleared to facilitate recounting the number of write failures of the current data storage operation.
[0077] 108. Increase the error count by one;
[0078] When the data to be stored fails to be written, the write error count is increased by one. When the write error count reaches a certain value, it indicates that there is a problem with the Flash memory. Different write error counts correspond to different Flash memory problems. Different processing methods are executed according to different write error counts.
[0079] For example, when the current write error count is 0, the Flash memory write failure may be sporadic, and the write operation can be successfully performed again. At this time, the write error count is updated to 1, and the write operation in step 105 is performed again.
[0080] 109. Determine whether the write error count is greater than or equal to a first write error count threshold;
[0081] If yes, execute step 110 and step 111;
[0082] If not, execute step 112.
[0083] In actual application, the first write error count threshold can be set according to actual conditions. For example, the first write error count threshold is set to 3. When the write error count is greater than or equal to 3, it means that the Flash memory frequently fails to write during the current data storage. It is necessary to detect whether the Flash memory has been used for a period of time exceeding its service life and whether it needs to be replaced.
[0084] 110. Erase the data in the Flash memory and clear m and the write error count;
[0085] When the write error count is greater than or equal to the first write error count threshold, it indicates that the Flash memory frequently fails to write during the current data storage. Therefore, the Flash memory needs to be erased and m and the write error count are cleared.
[0086] If it is detected that the usage time of the Flash memory has exceeded its specified service life, the Flash memory is replaced; if the usage time of the Flash memory has not reached its specified service life, the initialized Flash memory is put into use again, that is, the Flash memory is erased, m and the write error count are cleared, and the data to be stored is written from the starting unit when data storage is required next time.
[0087] 111. End the data storage program and issue a reminder for service life detection;
[0088] In an embodiment of the present application, the service life detection reminder can be given in the form of voice broadcast, indicator light or text message. For example, the text prompt information of the service life detection is displayed on the interactive terminal of the system, or an LED light is used. When the system issues a service life detection reminder, the LED light lights up to remind the user to check the usage time of the Flash memory.
[0089] 112. Determine whether the write error count is greater than or equal to a second write error count threshold.
[0090] If yes, execute step 113 and then return to step 105;
[0091] If not, return to step 105.
[0092] In an embodiment of the present application, the first write error count threshold is greater than the second write error count threshold; when the write error count is greater than or equal to the second write error count threshold, it indicates that the failure to write to the Flash memory may be due to a conflict in the operation of components inside the system chip, resulting in the level output being at a critical value of 1 and 0. At this time, the Flash memory is read and then erased, and m is cleared, that is, it is redirected to the starting unit of the Flash memory, and the data to be stored is rewritten to the starting unit.
[0093] 113. After reading the data from the Flash memory, erase the data and clear m to zero.
[0094] After the data originally stored in the Flash memory is read, the data in the Flash memory is erased to prevent the stored data from being lost, and the pointer is re-pointed to the starting unit of the Flash memory to perform a write operation.
[0095] The solution provided by the present application divides the Flash memory into several units, and a check bit is set in each unit. The data storage status of the mth unit is judged according to the check bit data. When it is determined that the current unit, that is, the mth unit, has no stored data, the action of writing the data to be stored in the mth unit is executed, and m is increased by one to make the pointer point to the next unit of the current unit in the Flash memory, so that when the data storage operation is performed next time, there is no need to start from the initial unit to check the unit empty, and the new unit can be directly reached for empty check and data storage operation, which saves a lot of unit empty check time; and in the solution described in the present application, in the current unit to be stored, the data is written into the mth unit, and the m is increased by one to make the pointer point to the next unit of the current unit in the Flash memory, so that when the data storage operation is performed next time, there is no need to start from the initial unit to check the unit empty from the beginning, and the new unit can be directly reached for empty check and data storage operation, which saves a lot of unit empty check time. When the storage data fails to be written, the write error count is increased by one to count the number of times the current data to be stored fails to be written. When the write error count of the current data to be stored is greater than or equal to the first write error count threshold, the Flash memory is erased and m and the write error count are cleared. At the same time, the data storage program is terminated and a service life detection reminder is issued to remind the operator to detect the service life of the Flash memory to identify whether the cause of the write failure is damage caused by the use time of the Flash memory exceeding its service life, or whether it is just a read-write BUG that occurs during the use of the Flash memory, so as to prevent waste caused by misjudgment of damage.
[0096] Embodiment 2
[0097] Based on the above Flash memory reading and writing method, the embodiment of the present application provides a Flash memory data reading method for data reading in step 113 .
[0098] The technical solution of the embodiments of the present application is described in detail below with reference to the accompanying drawings.
[0099] Figure 2 It is a flowchart of a method for reading data from a Flash memory according to an embodiment of the present application.
[0100] See also Figure 2 , the data reading method of the Flash memory comprises:
[0101] 201. Clear m to zero;
[0102] In the embodiment of the present application, m=0 is set so that the pointer in the system points to the starting unit of the Flash memory, that is, data reading starts from the starting unit of the Flash memory.
[0103] 202. Read the check bit data of the mth unit;
[0104] In the embodiment of the present application, step 202 is consistent with step 102 in the above-mentioned embodiment 1 and will not be repeated here.
[0105] 203. Determine whether the mth unit contains stored data according to the check bit data of the mth unit.
[0106] If the mth unit contains stored data, then execute step 204 and then return to execute step 202;
[0107] If there is no stored data in the mth unit, step 205 is executed.
[0108] In the embodiment of the present application, step 203 is consistent with step 103 in the above-mentioned embodiment 1 and will not be repeated here.
[0109] 204. Read the data stored in the mth unit and increase m by one;
[0110] In an embodiment of the present application, when it is determined that the mth unit contains stored data, the data stored in the unit is read, and m is increased by one so that the pointer points to the next unit, thereby reading the data stored in the Flash memory unit by unit, effectively preventing data from being missed during the data reading process.
[0111] 205. Determine whether m is greater than zero.
[0112] If yes, execute step 207;
[0113] If not, then execute step 206 and step 207 in sequence.
[0114] In the embodiment of the present application, when it is determined that there is no stored data in the mth unit, if m is greater than 0, it means that the current pointer does not point to the starting unit, that is, there is already data that has been successfully read in the data reading process. Since in the embodiment of the present application, in the data writing process, writing is performed unit by unit starting from the starting unit, therefore, when the pointer does not point to the starting unit and the unit currently pointed to is empty, it means that the storage data of the Flash memory has been read, and step 207 is executed, that is, the data reading program ends.
[0115] 206. Sending a prompt indicating that the Flash memory is empty;
[0116] In an embodiment of the present application, when it is determined that there is no stored data in the mth unit, if m is equal to 0, it means that the current pointer points to the starting unit, which means that the starting unit of the Flash memory is empty, which means that there is no stored data in the Flash memory, and a prompt is issued that the Flash memory is empty.
[0117] 207. End the data reading program.
[0118] The embodiment of the present application provides a method for reading data from a Flash memory based on the method for reading and writing data from the Flash memory of the first embodiment. When a data reading operation is required, m is set to zero so that a pointer points to a starting unit, and the storage data in the Flash memory is read from the starting unit, and the data storage status of the mth unit is determined by the check bit data. When the unit containing the storage data is read, m is increased by one to make the pointer point to the next unit. Since the data writing process starts from the starting unit and is written to each unit one by one, when it is determined that there is no storage data in the mth unit, it means that the storage data in the Flash memory has been read, which effectively prevents data from being missed during the data reading process.
[0119] By judging the value of m when the unit has no stored data, it can be determined whether the unit without stored data is the starting unit, and thus it can be determined whether the current Flash memory is an empty memory.
[0120] Embodiment 3
[0121] In the prior art, Flash memory usually stores data in bytes and erases data in blocks, and each time data is stored, data is erased before being written. Since the erasing time is long, if there are frequent write operations, the entire data storage and erasing time will be correspondingly prolonged, which will cause timing inaccuracy and communication abnormalities in the system, thereby affecting the stability of the entire system.
[0122] In view of the problems existing in the prior art, based on the data reading and writing method of the Flash memory shown in the above embodiment 1, the embodiment of the present application proposes a data erasing method of the Flash memory.
[0123] The technical solution of the embodiments of the present application is described in detail below with reference to the accompanying drawings.
[0124] Figure 3 It is a flowchart of a method for erasing data from a Flash memory shown in an embodiment of the present application.
[0125] See also Figure 3 , the data erasing method of the Flash memory comprises:
[0126] 301. Obtaining a data erasure instruction;
[0127] 302. Detect the state of the full load mark;
[0128] In an embodiment of the present application, a full load flag is provided in the Flash memory. When the check bit data of the nth unit is not a null value, the state of the full load flag is recorded as full load, otherwise the state of the full load flag is recorded as not full load.
[0129] 303. When the state of the full load flag is full, call the erase function once;
[0130] In step 303, if the system function instruction is obtained, the full load mark is maintained unchanged, and the system function instruction is executed after the data erasing program is exited, and step 302 is executed again after the execution of the system function instruction is completed.
[0131] In the embodiment of the present application, the execution time of the data erase operation of the Flash memory is relatively long. During the erasing process, if a system function instruction is generated, the data erase operation is suspended. Since the data erase operation is not completed at this time, the full load mark remains unchanged. During the suspension of the data erase operation, the system function instruction is executed to give priority to completing other functional logics of the system to avoid affecting the system timing, communication and other functions due to a single erase time being too long, which is beneficial to improving the stability of the system; after the execution of the system function instruction is completed, the status of the full load mark is re-detected.
[0132] In the embodiment of the present application, the above-mentioned system function instructions are other instructions except the data erasing instruction.
[0133] 304. When the erasure is completed, the state of the full load flag is updated to not full load;
[0134] When the erasing is completed, the state of the full load flag is updated to non-full load, indicating that the storage data of the Flash memory has been erased, the Flash memory has been cleared, and can be used to write data from the starting unit.
[0135] 305. Exit the data erasure program.
[0136] The data erasing method provided in the embodiment of the present application does not require other system programs to wait for the erasing to be completed during the erasing time. The system can pause the erasing during the data erasing process to execute other functional logics. In this way, the time for data erasing is split up, which is equivalent to the waiting time required for erasing being used to execute other functional logics of the system, thereby avoiding the impact of a single erasing time that is too long on system timing, communication and other functions, and is beneficial to improving the stability of the system.
[0137] Embodiment 4
[0138] The embodiment of the present application designs the data erasing process of step 110 and / or step 113 in the above-mentioned embodiment 1.
[0139] The data erasure process includes:
[0140] Erasing starts from the starting area of the Flash memory, and after erasing is completed, the state of the full load flag is updated to non-full load, and the data erasing program is exited;
[0141] During the erasing process, if a system function instruction is obtained, the full load mark is maintained unchanged, and the system function instruction is executed after exiting the data erasing program, and the state of the full load mark is re-detected after the execution of the system function instruction is completed.
[0142] In the embodiment of the present application, the execution time of the data erase operation of the Flash memory is relatively long. During the erasing process, if a system function instruction is generated, the data erase operation is suspended. Since the data erase operation is not completed at this time, the full load mark remains unchanged. During the suspension of the data erase operation, the system function instruction is executed to give priority to completing other functional logics of the system to avoid affecting the system timing, communication and other functions due to a single erase time being too long, which is beneficial to improving the stability of the system; after the execution of the system function instruction is completed, the status of the full load mark is re-detected.
[0143] This operation actually places the erase function in the main loop and divides the time for erasing data. The erase function is executed each time the main loop is executed. When there is a lot of data in the Flash memory, the erase time is longer, and the erase pause time is used to execute other system function logics. After completing the system function instructions, it returns to the main loop.
[0144] In the embodiment of the present application, the above-mentioned system function instructions are other instructions except the data erasing instruction.
[0145] The data erasing method provided in the embodiment of the present application does not require other system programs to wait for the erasing to be completed during the erasing time. The system can pause the erasing during the data erasing process to execute other functional logics. In this way, the time for data erasing is split up, which is equivalent to the waiting time required for erasing being used to execute other functional logics of the system, thereby avoiding the impact of a single erasing time that is too long on system timing, communication and other functions, and is beneficial to improving the stability of the system.
[0146] Embodiment 5
[0147] Corresponding to the aforementioned application function implementation method embodiment, the present application also provides a Flash memory and corresponding embodiments.
[0148] The Flash memory, whose storage area is divided into n units, wherein n is a positive integer greater than one, stores data based on the data storage method of the Flash memory as described in the first embodiment, reads data based on the data reading method of the Flash memory as described in the second embodiment, and erases data based on the methods of the third and fourth embodiments.
[0149] Regarding the device in the above embodiment, the specific manner in which each module performs the operation has been described in detail in the embodiment of the method, and will not be elaborated again here.
[0150] The scheme of the present application has been described in detail above with reference to the accompanying drawings. In the above embodiments, the description of each embodiment has its own emphasis. For the part that is not described in detail in a certain embodiment, refer to the relevant description of other embodiments. Those skilled in the art should also know that the actions and modules involved in the description are not necessarily required for the present application. In addition, it can be understood that the steps in the method of the embodiment of the present application can be adjusted in order, merged and deleted according to actual needs, and the modules in the device of the embodiment of the present application can be merged, divided and deleted according to actual needs.
[0151] In addition, the method according to the present application may also be implemented as a computer program or a computer program product, which includes computer program code instructions for executing some or all of the steps in the above method of the present application.
[0152] Alternatively, the present application can also be implemented as a non-temporary machine-readable storage medium (or computer-readable storage medium, or machine-readable storage medium) on which executable code (or computer program, or computer instruction code) is stored. When the executable code (or computer program, or computer instruction code) is executed by a processor of an electronic device (or electronic device, server, etc.), the processor executes part or all of the steps of the above-mentioned method according to the present application.
[0153] Those skilled in the art will further appreciate that the various illustrative logical blocks, modules, circuits, and algorithm steps described in connection with the application herein may be implemented as electronic hardware, computer software, or combinations of both.
[0154] The flow chart and block diagram in the accompanying drawings show the possible architecture, function and operation of the system and method according to multiple embodiments of the present application. In this regard, each square box in the flow chart or block diagram can represent a part of a module, a program segment or a code, and the part of the module, the program segment or the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two continuous square boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or the flow chart, and the combination of the square boxes in the block diagram and / or the flow chart can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0155] The embodiments of the present application have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and changes will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The selection of terms used herein is intended to best explain the principles of the embodiments, practical applications, or improvements to the technology in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.
Claims
1. A method for reading and writing data in a Flash memory, characterized in that: include: Get the data to be stored; Read the check bit data of the mth unit, where m is an integer greater than or equal to zero; Determine the data storage status of the mth unit according to the check bit data of the mth unit, if there is no stored data in the mth unit, determine whether the data to be stored is written successfully after the data to be stored is written into the mth unit, if so, add one to m and clear the write error count before executing the step of obtaining the data to be stored; If not, the write error count is incremented by one, and then it is determined whether the write error count is greater than or equal to a first write error count threshold. If so, the Flash memory is erased and m and the write error count are cleared to zero, and then the data storage program is terminated and a service life detection reminder is issued; After determining whether the write error count is greater than or equal to a first write error count threshold, the method further comprises: If the write error count is less than the first write error count threshold, determine whether the write error count is greater than or equal to the second write error count threshold, and if so, read the data from the Flash memory and then erase the data, and after clearing m, execute the step of writing the data to be stored in the mth unit; The first write error count threshold is greater than the second write error count threshold.
2. The method for reading and writing data in a Flash memory according to claim 1, wherein: The determining the data storage status of the mth unit according to the check bit data of the mth unit includes: If the check bit data of the m-th unit is a null value, then there is no stored data in the m-th unit; If the check bit data of the m-th unit is check information, then the m-th unit contains storage data.
3. The method for reading and writing data in a Flash memory according to claim 2, characterized in that: After determining the data storage status of the mth unit according to the check bit data of the mth unit, the method includes: If the mth unit contains stored data, then the step of reading the check bit data of the mth unit is performed after adding one to the m.
4. The method for reading and writing data of a Flash memory according to claim 1, characterized in that: Before reading the check bit data of the mth unit, the method includes: The Flash memory is divided into n units according to a preset byte length, where n is a positive integer greater than one.
5. The method for reading and writing data in a Flash memory according to claim 4, characterized in that: A full load flag is provided in the Flash memory. When the check bit data of the nth unit is not a null value, the state of the full load flag is recorded as full load, otherwise the state of the full load flag is recorded as not full load.
6. The method for reading and writing data in a Flash memory according to claim 5, characterized in that: Erasing data from the Flash memory includes: Erasing starts from the starting area of the Flash memory, and after erasing is completed, the state of the full load flag is updated to non-full load, and the data erasing program is exited; During the erasing process, if a system function instruction is obtained, the full load mark is maintained unchanged, and the system function instruction is executed after exiting the data erasing program, and the state of the full load mark is re-detected after the execution of the system function instruction is completed.
7. The method for reading and writing data in a Flash memory according to claim 5, characterized in that: Also includes: When a data erasing instruction is obtained, the state of the full load mark is detected, and when the state of the full load mark is full, an erasing function is called once, and after the erasing is completed, the state of the full load mark is updated to non-full, and the data erasing program is exited; During the erasing process, if a system function instruction is obtained, the full load mark is maintained unchanged, and the system function instruction is executed after exiting the data erasing program, and the state of the full load mark is re-detected after the execution of the system function instruction is completed.
8. The method for reading and writing data in a Flash memory according to claim 1, wherein: The step of reading data from the Flash memory comprises: Clearing m to zero; Reading the check bit data of the mth unit; Determining the data storage status of the mth unit according to the check bit data of the mth unit, if the mth unit contains stored data, reading the data stored in the mth unit and adding 1 to m, and then executing the step of reading the check bit data of the mth unit; If there is no stored data in the mth unit, it is determined whether m is greater than zero. If so, the data reading procedure is terminated.
9. The method for reading and writing data in a Flash memory according to claim 8, characterized in that: After determining whether m is greater than zero, the method further comprises: If m is equal to zero, a prompt indicating that the Flash memory is empty is issued and the data reading procedure is terminated.
