Bad block-based data writing method, reading method, and system thereof
Patent Information
- Application Number
- CN202211732195.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-30
- Publication Date
- 2026-09-15
- Estimated Expiration
- 2042-12-30
AI Technical Summary
[0035] Based on the technical solution of this application, each type of data has multiple consecutive historical backups in the EEPROM. When data is written, a readback and verification method is used to ensure successful data writing. During power-on reading, the latest valid data is read from the EEPROM according to the rules at the time of data writing. In case of abnormal situations, the most recent valid data can be found according to the rules (index and version) at the time of writing.
Smart Images

Figure CN116048870B_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of data processing, and in particular to methods and systems for writing and reading data based on bad blocks. Background Technology
[0002] With the continuous development of science and technology, a wide variety of feature-rich embedded products are increasingly becoming part of our lives. Whether it's the emerging smart home devices or various automotive electronic components, while supporting a wide range of functions, they inevitably need to store some important data. For example, a dashboard alone needs to store key data such as total fuel consumption, trip fuel consumption, average fuel consumption, and instantaneous fuel consumption, as well as various mileage information, driving information, and vehicle model information. The amount of information that needs to be stored is hundreds of types, and the data must not only be diverse but also accurate.
[0003] These data are primarily stored in the EEPROM chip, which is independent of the MCU. When the EEPROM malfunctions, the MCU cannot receive immediate feedback. During the writing process to the EEPROM, abnormal power outages, electrostatic discharge, excessive current, or other faults can occur, resulting in bad blocks in the EEPROM. This leads to data loss or inability to retain data, causing incalculable losses for users. Therefore, this paper focuses on introducing a more secure and reliable storage method to address the issue of bad blocks in EEPROM chips. Summary of the Invention
[0004] The main objective of this invention is to provide a method for writing and reading data based on bad blocks, and a system thereof. After the data is written, the written data is verified according to the check code and the data writing rules, making the data writing more secure.
[0005] Firstly, a method for writing data based on bad blocks is provided, the writing method including:
[0006] According to the preset data writing rules, fill in the version number and serial number corresponding to the data to be written, and save the data to be written;
[0007] Obtain the CRC_A checksum of the data to be written and save the checksum to the MCU's cache;
[0008] Read back the data to be written, and verify the data to be written according to the verification code and the data writing rules;
[0009] If the verification passes, the data writing is complete.
[0010] In an optional implementation, the step of reading back the data to be written and verifying the data to be written according to the checksum and the data writing rules includes:
[0011] The data to be written is first verified;
[0012] If the first verification passes, a second verification based on the checksum is performed on the data to be written.
[0013] If the second verification passes, the data writing is complete.
[0014] Secondly, a method for reading data based on bad blocks is provided, the method including:
[0015] Get the address of the largest version number and serial number in the EEPROM;
[0016] Obtain the data to be read from the largest address;
[0017] According to the preset data reading verification rules, the data to be read is verified.
[0018] If the read verification passes, the data reading is complete.
[0019] In one optional implementation, the read data verification rules include: the sequence number of the address used for rollback is consecutive to the sequence number of the current page, or the version number of the address used for rollback is consecutive to the version number of the current page.
[0020] Thirdly, a writing system for bad block-based data is provided, the writing system comprising:
[0021] The data to be written module is used to fill in the version number and serial number corresponding to the data to be written according to the preset data writing rules, and save the data to be written.
[0022] The verification code caching module is used to obtain the verification code CRC_A of the data to be written and save the verification code to the MCU's cache;
[0023] The verification module is used to read back the data to be written and verify the data to be written according to the verification code and the data writing rules.
[0024] The verification module is used to complete the data writing process if the verification passes.
[0025] In an optional implementation, the step of reading back the data to be written and verifying the data to be written according to the checksum and the data writing rules includes:
[0026] The data to be written is first verified;
[0027] If the first verification passes, a second verification based on the checksum is performed on the data to be written.
[0028] If the second verification passes, the data writing is complete.
[0029] Fourthly, a data reading system based on bad blocks is provided, the reading system comprising:
[0030] The address acquisition module is used to obtain the address with the largest version number and serial number in the EEPROM;
[0031] The data to be read module is used to obtain the data to be read from the largest address;
[0032] The read verification module is used to perform read verification on the data to be read according to preset read data verification rules;
[0033] The read verification module is used to complete the data reading if the read verification passes.
[0034] In one optional implementation, the read data verification rules include: the sequence number of the address used for rollback is consecutive to the sequence number of the current page, or the version number of the address used for rollback is consecutive to the version number of the current page.
[0035] Based on the technical solution of this application, each type of data has multiple consecutive historical backups in the EEPROM. When data is written, a readback and verification method is used to ensure successful data writing. During power-on reading, the latest valid data is read from the EEPROM according to the rules at the time of data writing. In case of abnormal situations, the most recent valid data can be found according to the rules (index and version) at the time of writing. Attached Figure Description
[0036] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments of this application will be briefly introduced below.
[0037] Figure 1 A flowchart illustrating a method for writing data based on bad blocks according to an embodiment of the present invention;
[0038] Figure 2 A flowchart illustrating a method for reading data based on bad blocks according to an embodiment of the present invention;
[0039] Figure 3 A structural diagram of a bad block-based data writing system provided in one embodiment of the present invention;
[0040] Figure 4 This is a structural diagram of a data reading system based on bad blocks provided in one embodiment of the present invention. Detailed Implementation
[0041] The embodiments of this application are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar modules or modules having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain this application, and should not be construed as limiting the invention.
[0042] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the term “comprising” as used in this application means the presence of the stated features, integers, steps, operations, modules, and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, modules, components, and / or groups thereof. It should be understood that when we say a module is “connected” or “coupled” to another module, it can be directly connected or coupled to the other module, or there may be an intermediate module. Furthermore, “connected” or “coupled” as used herein can include wireless connection or wireless coupling. The term “and / or” as used herein includes all or any of the modules and all combinations thereof of one or more associated listed items.
[0043] To make the objectives, technical solutions, and advantages of this application clearer, the implementation of this application will be described in further detail below with reference to the accompanying drawings.
[0044] The technical solutions of this application and how they solve the aforementioned technical problems will be described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.
[0045] like Figure 1 The diagram shows a flowchart of a method for writing data based on bad blocks according to an embodiment of the present invention. The writing method includes:
[0046] Step 101: According to the preset data writing rules, fill in the version number and serial number corresponding to the data to be written, and save the data to be written;
[0047] Step 102: Obtain the CRC_A checksum of the data to be written and save the checksum to the MCU's cache;
[0048] Step 103: Read back the data to be written and verify the data to be written according to the verification code and the data writing rules;
[0049] Step 104: If the verification passes, the data writing is completed.
[0050] In this embodiment of the invention, the preset data writing rules include: the address for the next data write is the current write address plus 1, and the address for data rollback is the current write address minus 1. Data writing typically involves two steps: writing the data to be written to the corresponding location in the EEPROM, and verifying the written data through rollback. Specifically, during data writing, the correct index and version values are filled according to the current write position, and a checksum CRC_A is calculated and stored in the MCU's cache. After the data writing is complete, the data is read back. The read-back data is first verified to determine if the verification is successful. If the verification is successful, it cannot be directly determined to be valid data, because what is read may be historical data stored in a bad block of the EEPROM (bad blocks can be read but not written). Therefore, it is necessary to determine whether the read checksum is the same as the written checksum. If the write fails, the index and version are incremented by 1, and the data is rewritten to the next address.
[0051] The step of reading back the data to be written, and verifying the data to be written according to the checksum and the data writing rules, includes:
[0052] The data to be written is first verified;
[0053] If the first verification passes, a second verification based on the checksum is performed on the data to be written.
[0054] If the second verification passes, the data writing is complete.
[0055] In this embodiment of the invention, according to a preset data writing rule, a version number and serial number corresponding to the data to be written are filled in, and the data to be written is saved; the checksum CRC_A of the data to be written is obtained and saved to the MCU's cache; the data to be written is read back, and the data to be written is verified according to the checksum and the data writing rule; if the verification passes, the data writing is completed. Verifying the written data according to the checksum and the data writing rule after data writing makes data writing more secure.
[0056] like Figure 2 The diagram shows a flowchart of a method for reading data based on bad blocks according to an embodiment of the present invention. The reading method includes:
[0057] Step 201: Obtain the address of the EEPROM with the largest version number and serial number;
[0058] Step 202: Obtain the data to be read from the largest address;
[0059] Step 203: Perform read verification on the data to be read according to the preset read data verification rules;
[0060] Step 204: If the read verification passes, the data reading is completed.
[0061] In this embodiment of the invention, the address corresponding to the largest version number and serial number is usually the location where the data was stored before the last power-off. Therefore, when data is read from the EEPROM, the version of each page is read first to find the largest version value. Then, among all pages with the largest version value, the page with the largest index is found. The data on this page is read and verified. If the read data passes the verification, it is considered valid data. If the verification fails, the data is rolled back to the previous page. The data read back for rollback needs to pass the verification, and the adjacent pages must have consecutive indices or the same version for rollback to be possible. Otherwise, even if the verification passes, the data read back may be historical data stored in bad blocks of the EEPROM.
[0062] The data verification rules include: the sequence number of the address used for rollback is consecutive to the sequence number of the current page, or the version number of the address used for rollback is consecutive to the version number of the current page.
[0063] In this embodiment of the invention, the address with the largest version number and serial number in the EEPROM is obtained; the data to be read is obtained from the largest address; the data to be read is verified according to a preset data verification rule; if the verification passes, the data reading is completed. After the data is read, the read data is verified according to the preset verification rule, making the read data more secure.
[0064] like Figure 3 The diagram shown is a structural diagram of a bad block-based data writing system according to an embodiment of the present invention. The writing system includes:
[0065] The data to be written module 301 is used to fill in the version number and serial number corresponding to the data to be written according to the preset data writing rules, and save the data to be written.
[0066] The verification code caching module 302 is used to obtain the verification code CRC_A of the data to be written and save the verification code to the MCU's cache;
[0067] The verification module 303 is used to read back the data to be written and verify the data to be written according to the verification code and the data writing rules;
[0068] The verification module 304 is used to complete the data writing if the verification passes.
[0069] In this embodiment of the invention, the preset data writing rules include: the address for the next data write is the current write address plus 1, and the address for data rollback is the current write address minus 1. Data writing typically involves two steps: writing the data to be written to the corresponding location in the EEPROM, and verifying the written data through rollback. Specifically, during data writing, the correct index and version values are filled according to the current write position, and a checksum CRC_A is calculated and stored in the MCU's cache. After the data writing is complete, the data is read back. The read-back data is first verified to determine if the verification is successful. If the verification is successful, it cannot be directly determined to be valid data, because what is read may be historical data stored in a bad block of the EEPROM (bad blocks can be read but not written). Therefore, it is necessary to determine whether the read checksum is the same as the written checksum. If the write fails, the index and version are incremented by 1, and the data is rewritten to the next address.
[0070] The step of reading back the data to be written, and verifying the data to be written according to the checksum and the data writing rules, includes:
[0071] The data to be written is first verified;
[0072] If the first verification passes, a second verification based on the checksum is performed on the data to be written.
[0073] If the second verification passes, the data writing is complete.
[0074] In this embodiment of the invention, according to a preset data writing rule, a version number and serial number corresponding to the data to be written are filled in, and the data to be written is saved; the checksum CRC_A of the data to be written is obtained and saved to the MCU's cache; the data to be written is read back, and the data to be written is verified according to the checksum and the data writing rule; if the verification passes, the data writing is completed. Verifying the written data according to the checksum and the data writing rule after data writing makes data writing more secure.
[0075] like Figure 4The diagram shown is a structural diagram of a bad block-based data reading system according to an embodiment of the present invention. The reading system includes:
[0076] Address acquisition module 401 is used to obtain the address with the largest version number and serial number in EEPROM;
[0077] The data to be read acquisition module 402 is used to acquire the data to be read from the largest address;
[0078] The read verification module 403 is used to perform read verification on the data to be read according to the preset read data verification rules;
[0079] The read verification module 404 is used to complete the data reading if the read verification passes.
[0080] In this embodiment of the invention, the address corresponding to the largest version number and serial number is usually the location where the data was stored before the last power-off. Therefore, when data is read from the EEPROM, the version of each page is read first to find the largest version value. Then, among all pages with the largest version value, the page with the largest index is found. The data on this page is read and verified. If the read data passes the verification, it is considered valid data. If the verification fails, the data is rolled back to the previous page. The data read back for rollback needs to pass the verification, and the adjacent pages must have consecutive indices or the same version for rollback to be possible. Otherwise, even if the verification passes, the data read back may be historical data stored in bad blocks of the EEPROM.
[0081] The data verification rules include: the sequence number of the address used for rollback is consecutive to the sequence number of the current page, or the version number of the address used for rollback is consecutive to the version number of the current page.
[0082] In this embodiment of the invention, the address with the largest version number and serial number in the EEPROM is obtained; the data to be read is obtained from the largest address; the data to be read is verified according to a preset data verification rule; if the verification passes, the data reading is completed. After the data is read, the read data is verified according to the preset verification rule, making the read data more secure.
[0083] It should be understood that although the steps in the flowcharts of the accompanying figures are shown sequentially as indicated by the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the accompanying figures may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the sub-steps or stages of other steps.
[0084] The above description is only a partial implementation of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A method for writing data based on bad blocks, characterized in that, The writing method includes: According to the preset data writing rules, the data to be written is saved and written to the corresponding position in the EEPROM. Based on the current writing position, the version number and serial number index corresponding to the data to be written are filled in. Calculate the checksum of the data to be written and save the checksum to the MCU's cache; After the data writing is completed, the data to be written is read back, and the read-back data is verified according to the verification code and the data writing rules to determine whether the data has passed the verification. If the data verification passes and the read verification code is the same as the written verification code, it indicates that the writing is successful and the data writing is completed; otherwise, the writing fails, and the serial number index is incremented by 1, the version number version is also incremented by 1, and the data is rewritten to the next address. The preset data writing rules include: the address of the next data write is the current write address plus 1, and when rolling back data, the address of the rolled-back data is the current write address minus 1.
2. The writing method as described in claim 1, characterized in that, The process of reading back the data to be written, and verifying the read-back data according to the checksum and the data writing rules, includes: The data to be written is first verified; If the first verification passes, a second verification based on the checksum is performed on the data to be written. If the second verification passes, the data writing is complete.
3. A method for reading data based on bad blocks, characterized in that, The reading method includes: Get the address with the largest version number and serial number index in the EEPROM. The address corresponding to the largest version number and serial number index is the location where the data was saved before the last power-off. Retrieving the data to be read from the largest address includes: when reading data from the EEPROM, first reading the version number in each page, finding the largest version number value, and among all pages where the version number is the largest, finding the page with the largest serial number index, and reading the data from that page; According to the preset data reading verification rules, the data to be read is verified. If the read verification passes, the data is considered valid, and the data reading is completed. If the verification fails, the data is rolled back to the previous page. The data read back needs to pass the verification, and the rollback will only proceed if the serial number index is consecutive or the version number is the same between two adjacent pages.
4. The reading method as described in claim 3, characterized in that, The data verification rules include: the sequence number index of the address used for rollback is consecutive to the sequence number index of the current page, or the version number version of the address used for rollback is consecutive to the version number version of the current page.
5. A data writing system based on bad blocks, characterized in that, The writing system includes: The data to be written module is used to save the data to be written, that is, to write the data to be written to the corresponding position in the EEPROM. According to the current writing position, it fills in the version number and serial number index corresponding to the data to be written. The preset data writing rules include: the address of the next data writing is the current writing address plus 1, and when rolling back the data, the address of the rolled-back data is the current writing address minus 1. The check code caching module is used to calculate the check code of the data to be written and save the check code to the MCU's cache; The verification module is used to read back the data to be written after the data writing is completed, and to verify the read-back data to be written according to the verification code and the data writing rules. The verification module is used to determine whether the data has passed verification. If the data passes verification and the read verification code is the same as the written verification code, it indicates that the writing is successful and the data writing is completed; otherwise, the writing fails, and the serial number index is incremented by 1 and the version number version is also incremented by 1, and the data is rewritten to the next address.
6. The writing system as described in claim 5, characterized in that, The process of reading back the data to be written, and verifying the read-back data according to the checksum and the data writing rules, includes: The data to be written is first verified; If the first verification passes, a second verification based on the checksum is performed on the data to be written. If the second verification passes, the data writing is complete.
7. A data reading system based on bad blocks, characterized in that, The reading system includes: The address acquisition module is used to obtain the address with the largest version number and serial number index in the EEPROM. The address corresponding to the largest version number and serial number index is the location where the data was saved before the last power-off. The data to be read module is used to obtain the data to be read from the maximum address, including: when the data is read from the EEPROM, first read the version number in each page, find the maximum version number value, and among all pages where the version number is the maximum value, find the page with the largest serial number index, and read the data of that page; The read verification module is used to perform read verification on the data to be read according to preset read data verification rules; The read verification module is used to complete the data reading if the read verification is successful, i.e., the data is considered valid. If the verification fails, the data is rolled back to the previous page. The data read back needs to pass the verification and the rollback is only performed if the serial number index is consecutive or the version number is the same between two adjacent pages.
8. The reading system as described in claim 7, characterized in that, The data verification rules include: the sequence number index of the address used for rollback is consecutive to the sequence number index of the current page, or the version number version of the address used for rollback is consecutive to the version number version of the current page.
Citation Information
Patent Citations
Flash memory safely writing method and data writing method
CN101567217A
Cache data verification method, device and equipment and storage medium
CN111897846A