Storage medium and data reading and writing method and device thereof

By dividing the index storage area and the data storage area in the storage medium, and determining the last historical storage location using the data index flag bits, the problem of low position determination efficiency in the storage medium in the prior art is solved, and fast and efficient data reading and rewriting are achieved.

CN119937925APending Publication Date: 2025-05-06IF NEW ENERGY TECH (WUXI) CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411998754.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-31
Publication Date
2025-05-06

AI Technical Summary

Technical Problem

The prior art is less efficient in determining the historical last storage location in a storage medium, especially in the presence of blank areas and head and tail data in a circular storage medium, resulting in increased search complexity.

Method used

By dividing the index storage area and the data storage area in the storage space, the historical last storage location is determined using the location of the data index flag bit and the stored value. The data index flag bits in the index storage area correspond one by one to the storage sub-region in the data storage area, and the storage value indicates the storage status of the storage sub-region.

Benefits of technology

It realizes the rapid determination of the last storage location of history, and improves the efficiency of data reading and rewriting in the storage medium.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119937925A_ABST
    Figure CN119937925A_ABST
Patent Text Reader

Abstract

The invention discloses a storage medium and a data reading and writing method and device thereof. The method comprises the following steps: determining a last historical storage position in a data storage area according to the position of each data index flag bit in the index storage area and a storage value on each data index flag bit; wherein the data index flag bits of the index storage area are in one-to-one correspondence with the storage sub-areas in the data storage area, and the storage values on the data index flag bits are used for indicating the storage states of the storage sub-areas corresponding to the data index flag bits; and performing corresponding data writing or data reading according to the last historical storage position. According to the application, the index storage area is divided in the storage space, and the historical final storage position is determined according to the related information of the data index flag bit representing the storage state of each storage area in the index storage area, so that rapid determination of the historical final storage position is realized; and thus, the efficiency of reading and continuing writing the latest data in the storage medium is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a storage medium and a data reading and writing method and device thereof. Background Art

[0002] In the era of rapid data development, when writing and reading data to storage media, especially non-volatile memory such as FLASH memory, in many scenarios, it is necessary to start writing data from the location where the data was last stored, and start reading data from the location where the data was last stored. For example, in embedded systems, in many scenarios, it is necessary to store and read the historical data of the device, and at this time, the storage medium of the device is required to meet the above conditions.

[0003] In the related art, after powering on, the search for the last storage location before the last shutdown is generally performed by sequential search or binary search. The latest stored data is determined by sequentially searching the stored data in the storage medium, and the search efficiency is low. Although the binary search is more efficient than the sequential search to a certain extent, for a storage medium with circular storage, there will be a blank area in the middle of the storage medium, and data will exist at the beginning and end. In this case, when the blank area is found by binary search, it is still necessary to use a sequential search, which increases the complexity of the search. Summary of the invention

[0004] The present application provides a storage medium and a data reading and writing method and device thereof, so as to solve the problem of low efficiency in determining the last historical storage position in the storage medium.

[0005] According to one aspect of the present application, a method for reading and writing data from a storage medium is provided, wherein the storage medium includes at least one storage space, and the storage space is divided into at least an index storage area and a data storage area;

[0006] The method includes:

[0007] Determine the historical last storage position in the data storage area according to the position of each data index flag bit in the index storage area and the storage value on each data index flag bit; wherein the data index flag bits in the index storage area correspond one-to-one to the storage sub-areas in the data storage area, and the storage value on the data index flag bit is used to indicate the storage state of the storage sub-area corresponding to the data index flag bit;

[0008] The corresponding data is written or read according to the last historical storage location.

[0009] According to another aspect of the present application, an electronic device is provided, the electronic device comprising:

[0010] at least one processor; and

[0011] a memory communicatively connected to at least one processor; wherein,

[0012] The memory stores a computer program that can be executed by at least one processor, and the computer program is executed by at least one processor so that the at least one processor can execute the data reading and writing method of any embodiment of the present application.

[0013] According to another aspect of the present application, a storage medium is provided, wherein the storage medium stores computer instructions, and the computer instructions are used to implement the data reading and writing method of any embodiment of the present application when executed by a processor.

[0014] The technical solution of the embodiment of the present application divides the storage space into index storage areas, determines the historical last storage position according to the relevant information of the data index flag bits representing the storage status of each storage area in the index storage area, and then processes the storage medium accordingly according to the historical last storage position, thereby achieving rapid determination of the historical last storage position and improving the efficiency of reading and continuing to write data in the storage medium.

[0015] It should be understood that the contents described in this section are not intended to mark the key or important features of the embodiments of the present application, nor are they intended to limit the scope of the present application. Other features of the present application will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0017] Figure 1 is a flow chart of a method for reading and writing data from a storage medium provided in an embodiment of the present application;

[0018] Figure 2 is a flowchart of another method for reading and writing data from a storage medium provided in an embodiment of the present application;

[0019] Figure 3 is a flowchart of another method for reading and writing data from a storage medium provided in an embodiment of the present application;

[0020] Figure 4 is a flowchart of another method for reading and writing data from a storage medium provided in an embodiment of the present application;

[0021] Figure 5is a flow chart of a method for writing data into a storage medium according to an embodiment of the present application;

[0022] Figure 6 It is a schematic diagram of the division of storage space;

[0023] Figure 7 It is a flow chart for determining the last historical storage location when the storage medium system is powered on and initialized;

[0024] Figure 8 is a flow chart of a method for reading data from a storage medium provided in an embodiment of the present application;

[0025] Fig. 9 It is a structural schematic diagram of an electronic device that implements the data reading and writing method of the storage medium of an embodiment of the present application. DETAILED DESCRIPTION

[0026] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.

[0027] It should be noted that the terms "candidate", "target", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0028] Figure 1 A flowchart of a method for reading and writing data from a storage medium is provided for an embodiment of the present application. This embodiment can be applicable to situations where data is quickly continued to be written or read from a storage medium. The method can be executed by a data reading and writing device for the storage medium. The data reading and writing device for the storage medium can be implemented in the form of hardware and / or software, and the data reading and writing device for the storage medium can be configured in the storage medium.

[0029] The storage medium includes at least one storage space, and the storage space is divided into at least an index storage area and a data storage area. Among them, the storage medium refers to the carrier of data storage, which can be, for example, an external FLASH in an embedded device, that is, an external memory of the MCU, an internal FLASH, that is, an internal memory of the MCU, an internal or external EEPROM or other storage device, etc. The embodiment of the present application takes the storage medium as FLASH as an example. The storage space is a space for storing different types of data by dividing the storage medium according to storage needs, for example, dividing the storage medium into multiple storage spaces, each storage space corresponding to storing one type of data. Exemplarily, when the storage medium is FLASH, the storage space corresponds to a storage block block.

[0030] The storage space is further divided into at least an index storage area and a data storage area. The data storage area is used to store written data. The data storage area is further divided into multiple storage sub-areas, each of which is used to store single-write data. The index storage area is used to store index identifiers indicating the storage status of each storage sub-area in the data storage. Multiple data index flags are set in the index storage area, and each data index flag corresponds to the data storage status of a storage sub-area. For example, if the storage status of the target storage sub-area is stored, the storage value on the data index flag corresponding to the target storage sub-area is a first value. If the storage status of the target storage sub-area is not stored, the storage value on the data index flag corresponding to the target storage sub-area is a second value. The first value is different from the second value. By identifying the storage value on the data index flag, the storage status of the corresponding storage sub-area can be determined.

[0031] For example, for FLASH storage media, FLASH is usually composed of storage blocks. One FLASH is divided into N1 blocks, where N1 is an integer multiple of 2; one block is composed of N2 storage sectors, where N2 is usually a power of 2 (e.g., 16); one sector is composed of N3 storage pages, where N3 can be 16; one page is composed of N4 bytes (char), where N4 can be 256, that is, one page usually has a storage area of ​​256 bytes. Table 1 shows the storage composition of FLASH.

[0032] Table 1

[0033]

[0034] Exemplarily, the storage space corresponds to a storage block, and the index storage area and the data storage area correspond to storage sectors. For example, the index storage area corresponds to at least one sector in the block, and the remaining sectors in the block are data storage areas.

[0035] The data index flag can be set in the index storage area according to actual needs. For example, the corresponding data index flag can be obtained by dividing the index storage area according to the number of divided storage sub-areas. The data index flag can be a bit in a byte, or at least two bits, or one or more bytes. In the embodiment of the present application, the specific representation of the data index flag is not limited.

[0036] The size of the data storage area can be set according to the target data size to be stored, but the number of data index flags in the index storage area should be at least not less than the number of storage sub-areas in the data storage area, so that each storage sub-area can have a corresponding data index flag to indicate it.

[0037] like Figure 1 As shown, the method includes:

[0038] S110 , determining the last historical storage position in the data storage area according to the position of each data index flag bit in the index storage area and the storage value of each data index flag bit.

[0039] The data index flags in the index storage area correspond one-to-one to the storage sub-areas in the data storage area, and the storage value on the data index flag is used to indicate the storage state of the storage sub-area corresponding to the data index flag.

[0040] A mapping relationship between the data index flags and each storage sub-area is established according to the order of the data index flags in the index storage area and the order of the storage sub-areas in the data storage area, that is, the first data index flag corresponds to the first storage sub-area, the second data index flag corresponds to the second storage sub-area, and if the first data index flag is located in front of the second data index flag in the index storage area, then the first storage sub-area is also located in front of the second storage sub-area in the data storage area. For example, the corresponding relationship is determined in sequence according to the order of the data index flags and the order of the storage sub-areas, that is, the first data index flag in the index storage area corresponds to the first storage sub-area in the data storage area, the second data index flag in the index storage area corresponds to the second storage sub-area in the data storage area, and so on.

[0041] Different storage values ​​on each data index flag are used to represent different storage states of corresponding storage sub-areas. The setting of different storage values ​​can be determined according to the properties of the storage medium itself. For example, in the initial state of Flash, it is 1. When data is written, 1 is modified to 0. Then, when the data index flag is 1 bit, if the storage value on the data index flag is the initial value 1, it means that the storage state of the storage sub-area corresponding to the data index flag is that no data is stored; if the storage value on the data index flag is 0, it means that the storage state of the storage sub-area corresponding to the data index flag is that data has been stored. When the data index flag is at least two bits, different storage states are represented according to different values ​​on the data index flag. For example, if the storage values ​​on the data index flag are all the initial value 11, it means that the storage state of the storage sub-area corresponding to the data index flag is that no data is stored; if the storage value on the data index flag is 00 or 01 or 10, it means that the storage state of the storage sub-area corresponding to the data index flag is that data has been stored.

[0042] The storage sub-area is used to store data written once in the data storage area. For example, when writing historical data of an electronic device into the Flash, the storage address of each historical data corresponds to a storage sub-area, that is, one storage sub-area is used to store one historical data.

[0043] When writing data in the data storage area, sequential storage is performed in the storage sub-areas, that is, for the initialized data storage area, if there is data to be written, it is first written to the first storage sub-area at the first position, and if there is still data to be written later, it is sequentially written to the storage sub-areas behind the first storage sub-area. When writing data in a storage sub-area in the data storage area, the storage value on the corresponding data index flag is modified to the numerical value corresponding to the stored data. Since the corresponding relationship between the data index flag and each storage sub-area is established according to the order of the data index flag in the index storage area and the order of the storage sub-area in the data storage area, and the storage sub-areas are sequentially stored in sequence, the data index flag in the index storage area is also sequentially modified to the numerical value corresponding to the stored data.

[0044] The storage values ​​on each data index flag in the index storage area are read sequentially, and the storage status in the corresponding storage sub-area is determined according to the read storage values. If it is determined in the sequential reading process that the storage status of the first storage sub-area is that no data is stored, the previous storage sub-area of ​​the storage sub-area is determined to be the historical last storage position, and the specific information of the historical last storage position is determined according to the position of the data index flag corresponding to the previous storage sub-area and the corresponding relationship between the data index flag and the storage sub-area.

[0045] In a feasible embodiment, S110 includes:

[0046] Determine a target data index flag bit according to a stored value; wherein the stored value of the target data index flag bit is not a first stored value, and the stored value of a data index flag bit next to the target data index flag bit is a first stored value; and the first stored value indicates that no data is stored in a storage sub-area corresponding to the data index flag bit;

[0047] The historical last storage position is determined according to the position of the target data index flag in the index storage area and the storage value.

[0048] When the data index flag is a bit in the index storage area, the value of the data index flag includes a first storage value 1 and a second storage value 0. The first storage value 1 indicates that no data is stored in the storage sub-area corresponding to the data index flag, and the second storage value 0 indicates that data has been stored in the storage sub-area corresponding to the data index flag.

[0049] Since the storage sub-areas in the data storage area are stored sequentially, the data index flag corresponding to the storage sub-area that has stored data is 0. When determining the last storage position in history, it is necessary to determine the first bit in the index storage area whose storage value is 1, that is, all the previous bits of this bit are 0, and the storage sub-areas corresponding to all the previous bits have stored data. Then the previous bit corresponding to this bit is the target data index flag, that is, the storage sub-area corresponding to the target data index flag is the last storage area in history.

[0050] Specifically, based on the position of the target data index flag in the index storage area and the correspondence between each data index flag and the storage sub-area in the index storage area, the position of the storage sub-area corresponding to the target data index flag in the data storage area is determined, and then the storage position of the storage sub-area corresponding to the target data index flag is determined based on the position as the last historical storage position.

[0051] Exemplarily, the data index flag is a bit in the index storage area, and the data index flag and the storage sub-area are stored in relative position order. The serial number of the target data index flag in the index storage area is determined, and the serial number indicates the bit position of the target data index flag in the index storage area. The serial number is the serial number of the storage sub-area corresponding to the target data index flag in the data storage area, that is, it indicates the storage sub-area corresponding to the target data index flag in the data storage area. The historical last storage position is determined according to the division address of the index storage area and the data storage area, and the division address range and serial number of the storage sub-area. For example, the historical last storage position (address) = the allocated first address + (serial number - 1) * the data length of each storage sub-area, wherein the allocated first address is the first address of the data storage area, and is determined according to the pre-divided addresses of the index storage area and the data storage area.

[0052] This embodiment can locate the historical last storage position in the data storage area by determining the storage value of each data index flag and the relative relationship of the position. There is no need to read and identify the data storage area, only the index storage area needs to be read and identified, which improves the efficiency and accuracy of determining the historical last storage position.

[0053] S120 , write or read corresponding data according to the last historical storage location.

[0054] When writing data, it is necessary to sequentially write to the subsequent storage sub-areas of the last storage location in history, so as to realize sequential writing of data in the data storage area. When reading data, it is necessary to read in reverse order from the last storage location in history, so as to realize sequential reading from the storage time of the stored data from near to far. For example, after the electronic device is turned on or restarted after power failure, the address process of the latest writing is often lost. At this time, the method of this embodiment can be used to quickly locate the last storage location in history, so as to continue to write new data or read the stored data.

[0055] The technical solution of the embodiment of the present application divides the storage space into index storage areas, determines the historical last storage position according to the relevant information of the data index flag bits representing the storage status of each storage area in the index storage area, and then processes the storage medium accordingly according to the historical last storage position, thereby achieving rapid determination of the historical last storage position and improving the efficiency of reading and continuing to write the latest data in the storage medium.

[0056] Figure 2 This is a flowchart of another method for reading and writing data from a storage medium provided in an embodiment of the present application. This embodiment further refines the data index mark of the index storage area in the above embodiment.

[0057] A plurality of continuous data index flag bits in the index storage area correspond to a plurality of continuous storage sub-areas in the data storage area, and the storage values ​​of the plurality of continuous data index flag bits can be converted into a data index flag.

[0058] In order to improve the efficiency of determining the last historical storage position, it is necessary to improve the search efficiency of the storage value and position of each data index flag in the index storage area. Therefore, in this embodiment, the corresponding range between the data index flag and the storage sub-area is expanded, and a data index flag is determined according to the expanded range. The data index flag is used to represent the storage status of the storage sub-area after the expanded range.

[0059] Specifically, a correspondence is established between a plurality of consecutive data index flags in the index storage area and a plurality of consecutive storage sub-areas in the data storage area. The corresponding number can be adjusted according to the actual situation of the storage medium. For example, in Flash, the data index flag is 1 bit. Since a byte contains 8 bits, in order to facilitate data reading from the index storage area, a correspondence is established between 8 consecutive data index flags in the index storage area and 8 consecutive storage sub-areas in the data storage area. If the data index flag is 2 bits, a correspondence can be established between 4 consecutive data index flags in the index storage area and 4 consecutive storage sub-areas in the data storage area, and so on.

[0060] In order to facilitate the reading of the storage values ​​of multiple consecutive data index flags, the storage values ​​of multiple consecutive data index flags are converted into a data index flag. The specific conversion method can be determined according to the storage type of the storage value of the data index flag before the conversion, and the readability and distinguishability of the data index flag after the conversion. For example, a binary storage method is used in Flash, and the storage values ​​on the data index flag are 0 and 1. In order to facilitate reading, the storage values ​​of multiple consecutive data index flags are converted into hexadecimal values ​​to represent the data index flag. For the convenience of description, the conversion of binary to hexadecimal is used as an example. Of course, other conversion methods are also within the protection scope of the embodiments of the present application.

[0061] After converting the storage values ​​of multiple consecutive data index flags, different data index flags represent the storage states of multiple consecutive storage sub-areas corresponding to the multiple consecutive data index flags, and different values ​​of the data index flags can be determined according to actual conditions.

[0062] Exemplarily, for the case where the data index flag bit is 1 bit in the Flash, a correspondence between 8 consecutive data index flag bits in the index storage area and 8 consecutive storage sub-areas in the data storage area is established, wherein the data index flag includes: a first data index flag indicating that no data is stored in the consecutive multiple storage sub-areas corresponding to the consecutive multiple data index flag bits, a second data index flag indicating that data is stored in the first storage sub-area of ​​the consecutive multiple storage sub-areas corresponding to the consecutive multiple data index flag bits, a third data index flag indicating that data is stored in the first two storage sub-areas of the consecutive multiple storage sub-areas corresponding to the consecutive multiple data index flag bits, and a fourth data index flag indicating that data is stored in the consecutive multiple storage sub-areas corresponding to the consecutive multiple data index flag bits. The first three storage sub-areas in the continuous storage area have stored data, the fifth data index mark indicates that the first four storage sub-areas in the continuous storage sub-areas corresponding to the continuous multiple data index mark bits have stored data, the sixth data index mark indicates that the first five storage sub-areas in the continuous storage sub-areas corresponding to the continuous multiple data index mark bits have stored data, the seventh data index mark indicates that the first six storage sub-areas in the continuous storage sub-areas corresponding to the continuous multiple data index mark bits have stored data, the eighth data index mark indicates that the first seven storage sub-areas in the continuous eight storage sub-areas corresponding to the continuous multiple data index mark bits have stored data, and the ninth data index mark indicates that data are stored in all the continuous storage sub-areas corresponding to the continuous multiple data index mark bits.

[0063] Exemplarily, the storage value of 8 consecutive data index flags in 1 byte is 11111111, which is converted to the first data index flag as 0xFF, which corresponds to no data stored in the eight consecutive storage sub-areas corresponding to the eight consecutive data index flags; the storage value of 8 consecutive data index flags is 01111111, which is converted to the second data index flag as 0x7F, which corresponds to data stored in the first storage sub-area of ​​the eight consecutive storage sub-areas corresponding to the eight consecutive data index flags; the storage value of 8 consecutive data index flags is 0 0111111, converted to the third data index flag bit is 0x3F, which corresponds to the first two storage sub-areas of the eight consecutive storage sub-areas corresponding to the eight consecutive data index flag bits, which have stored data; the storage value of the eight consecutive data index flag bits is 00011111, which is converted to the fourth data index flag bit is 0x1F, which corresponds to the first three storage sub-areas of the eight consecutive storage sub-areas corresponding to the eight consecutive data index flag bits, which has stored data; the storage value of the eight consecutive data index flag bits is 00001111, which is converted to the fifth data index flag bit is 0x0F, corresponding to the first four storage sub-areas of the eight consecutive storage sub-areas corresponding to the eight consecutive data index flags, there are stored data; the storage value of the eight consecutive data index flags is 00000111, which is converted to the sixth data index flag as 0x07, corresponding to the first five storage sub-areas of the eight consecutive storage sub-areas corresponding to the eight consecutive data index flags, there are stored data; the storage value of the eight consecutive data index flags is 00000011, which is converted to the sixth data index flag as 0x03, corresponding to the first five storage sub-areas of the eight consecutive storage sub-areas corresponding to the eight consecutive data index flags. The first six of the eight consecutive storage sub-areas corresponding to the flag bits have stored data; the storage value of the eight consecutive data index flag bits is 00000001, which is converted to the sixth data index flag bit as 0x01, which corresponds to the storage of data in the first seven of the eight consecutive storage sub-areas corresponding to the eight consecutive data index flag bits; the storage value of the eight consecutive data index flag bits is 00000000, which is converted to the sixth data index flag bit as 0x00, which corresponds to the storage of data in the eight consecutive storage sub-areas corresponding to the eight consecutive data index flag bits. It can be seen that by reading the storage values ​​of multiple consecutive data index flag bits, the data storage status of multiple consecutive storage sub-areas can be determined at the same time, so the last storage position in history can be determined with higher efficiency.

[0064] like Figure 2 As shown, the method includes:

[0065] S210, determining a target data index flag according to the data index flag.

[0066] Among them, the data index flag converted from the storage value of at least the target data index flag is not the first data index flag, and the data index flag converted from the storage value of the next data index flag of at least the target data index flag is the first data index flag; wherein the first data index flag indicates that no data is stored in the consecutive multiple storage sub-areas corresponding to the consecutive multiple data index flags.

[0067] According to the correspondence between the continuous multiple data index flags and the continuous multiple storage sub-areas, the continuous number of data index flags is determined, the corresponding data index flags are read from the index storage area in sequence according to the continuous number, the value of each data index flag read is determined respectively, and the storage state of the continuous multiple storage sub-areas corresponding to the numerical index flag is determined according to the value of each data index flag. If the storage state of the continuous multiple storage sub-areas corresponding to the read target data index flag is not that no data is stored, that is, the target data index flag is not the first data index flag, and the storage state of the continuous multiple storage sub-areas corresponding to the next data index flag of the target data index flag is that no data is stored, that is, the next data index flag of the target data index flag is the first data index flag, then it is determined that the target data index flag is included in the continuous multiple data index flags corresponding to the target data index flag.

[0068] According to the relationship between the specific value of the target data index flag and the storage status of the storage sub-area corresponding to the corresponding multiple consecutive data index flags, the target data index flag is determined from the multiple consecutive data index flags corresponding to the target data index flag. Exemplarily, based on the above example, for the case where the data index flag is 1 bit in Flash, a corresponding relationship between 8 consecutive data index flags in the index storage area and 8 consecutive storage sub-areas in the data storage area is established. If the target data index flag is 0x7F, the first data index flag in the eight consecutive data index flags corresponding to the target data index flag is determined to be the target data index flag; if the target data index flag is 0x3F, the second data index flag in the eight consecutive data index flags corresponding to the target data index flag is determined to be the target data index flag; if the target data index flag is 0x1F, the third data index flag in the eight consecutive data index flags corresponding to the target data index flag is determined to be the target data index flag; if the target data index flag is 0x0F, the corresponding data index flag is determined to be The fourth data index flag bit among the eight consecutive data index flag bits corresponding to the data index flag is the target data index flag bit; if the target data index flag is 0x07, the fifth data index flag bit among the eight consecutive data index flag bits corresponding to the target data index flag is determined to be the target data index flag bit; if the target data index flag is 0x03, the sixth data index flag bit among the eight consecutive data index flag bits corresponding to the target data index flag is determined to be the target data index flag bit; if the target data index flag is 0x01, the seventh data index flag bit among the eight consecutive data index flag bits corresponding to the target data index flag is determined to be the target data index flag bit; if the target data index flag is 0x00, the eighth data index flag bit among the eight consecutive data index flag bits corresponding to the target data index flag is determined to be the target data index flag bit. The case where other consecutive multiple data index flag bits correspond to consecutive multiple storage sub-areas can be obtained by analogy analysis based on specific numerical values, which will not be repeated here.

[0069] S220, determining the last historical storage position according to the position of the target data index flag in the index storage area and the data index flag.

[0070] According to the position of the target data index flag in the index storage area and the correspondence between each data index flag and the storage sub-area in the index storage area, the position of the storage sub-area corresponding to the target data index flag in the data storage area is determined, and then the storage position of the storage sub-area corresponding to the target data index flag is determined based on the position as the last historical storage position.

[0071] Exemplarily, for the case where the data index flag bit in Flash is 1 bit, a correspondence between 8 consecutive data index flag bits in the index storage area and 8 consecutive storage sub-areas in the data storage area is established, and multiple consecutive data index flag bits and multiple consecutive storage sub-areas are stored in relative position order, and the first serial number of the target data index flag in all data index flags is determined, the first serial number represents the number of the aforementioned eight consecutive storage sub-areas included in the target data index flag bit, and then the second serial number of the storage sub-area corresponding to the target data index flag bit in the multiple consecutive storage sub-areas is determined according to the value of the target data index flag, the second serial number represents the serial number of the storage sub-area corresponding to the target data index flag bit in the current eight consecutive storage sub-areas corresponding to the target data index flag, and the target serial number of the storage sub-area corresponding to the target data index flag bit in the data storage area is determined according to the first serial number and the second serial number, that is, it is expressed as the number of the storage sub-area corresponding to the target data index flag bit in the data storage area, and the historical last storage position is determined according to the division address of the index storage area and the data storage area, as well as the division address range of the storage sub-area and the target serial number. For example, the historical last storage location (address) = allocated first address + (target serial number - 1) * data length of each storage sub-area, where the allocated first address is the first address of the data storage area, which is determined based on the pre-divided addresses of the index storage area and the data storage area. Among them, the target serial number = the first serial number * 8 + the storage sub-area serial number corresponding to the second serial number, the storage sub-area position information corresponding to the second serial number is determined according to the specific value of the target data index mark, if the target data index mark is 0x7F, the storage sub-area serial number corresponding to the second serial number is 1, if the target data index mark is 0x3F, the storage sub-area serial number corresponding to the second serial number is 2, if the target data index mark is 0x1F, the storage sub-area serial number corresponding to the second serial number is 3, if the target data index mark is 0x0F, the storage sub-area serial number corresponding to the second serial number is 4, if the target data index mark is 0x07, the storage sub-area serial number corresponding to the second serial number is 5, if the target data index mark is 0x03, the storage sub-area serial number corresponding to the second serial number is 6, if the target data index mark is 0x01, the storage sub-area serial number corresponding to the second serial number is 7, if the target data index mark is 0x00, the storage sub-area serial number corresponding to the second serial number is 8.

[0072] S230: Write or read corresponding data according to the last historical storage location.

[0073] In a feasible embodiment, writing corresponding data according to the last historical storage location includes:

[0074] The current storage position of the data to be stored is determined according to the last historical storage position, the storage value on the data index flag corresponding to the current storage position in the index storage area is modified, and the data to be stored is written into the current storage position.

[0075] The first address of the storage sub-area after the last historical storage position is determined as the current storage position of the data to be stored. Before writing the data to be stored into the current storage position, the storage value on the data index flag corresponding to the current storage position in the index storage area is modified.

[0076] Exemplarily, based on the above example, the data to be stored includes at least two historical data, each piece of historical data is written into a storage sub-area, and the current storage position of the data to be stored is determined according to the last historical storage position, the current storage position corresponds to the first storage sub-area, and before the first piece of historical data in the data to be stored is written into the first storage sub-area, the storage value on the data index flag in the data index area corresponding to the first storage sub-area is modified, and after the modification is completed, the first piece of historical data is written into the first storage sub-area; and so on, before the second piece of historical data in the data to be stored is written into the second storage sub-area, the storage value on the data index flag in the data index area corresponding to the second storage sub-area is modified, and after the modification is completed, the second piece of historical data is written into the second storage sub-area, until all pieces of historical data in the data to be stored are written into the storage sub-area, and the writing of the data to be stored is completed.

[0077] Since the number of bytes required to be written to modify the storage value of the data index flag is very small, for example, when the data index flag corresponds to one bit, only one bit needs to be modified, but each piece of historical data is written with multiple bytes, that is, the time to write historical data is much longer than the time to modify the storage value of the data index flag. Therefore, in order to prevent unexpected power outages when storing historical data, first modify the corresponding data index flag in the index storage area, and then write the corresponding historical data. Even if a power outage occurs during the storage process, after powering on and restarting, the last historical storage location written last can still be quickly located, and at most only one piece of historical data will be invalid, which improves the stability of data writing.

[0078] Optionally, if power is lost during the writing process, the electronic device containing the storage medium is restarted, and the historical last storage position and the stored data are determined based on the position of each data index flag and the storage value on each data index flag; the current storage position is determined based on the historical last storage position; and the data to be stored continues to be stored based on the stored data and the current storage position.

[0079] If power is lost during the writing process of the data to be stored, the electronic device is restarted, and the storage value of each data index flag in the index storage area is read first, and the historical last storage position is determined according to the position of each data index flag and the storage value on each data index flag, and the stored data in the data to be stored is determined according to the historical last storage position and the initial storage position of the last storage of the data to be stored, and the first address of the next storage sub-area is determined as the current storage position according to the historical last storage position, and the remaining storage data in the data to be stored is determined according to the stored data, and the remaining storage data is written sequentially and cyclically from the current storage position to continue storage.

[0080] The scheme of the embodiment of the present application improves the efficiency of determining the historical last storage position by establishing a correspondence between multiple consecutive data index flags in the index storage area and multiple consecutive storage sub-areas in the data storage area, and determining the historical last storage position based on the data index flag converted according to the storage values ​​of the multiple consecutive data index flags.

[0081] Figure 3 This is a flowchart of another method for reading and writing data from a storage medium provided in an embodiment of the present application. This embodiment further refines the steps in the above embodiment by writing corresponding data according to the last historical storage location.

[0082] The index storage area at least includes a first index storage area and a second index storage area; the data index flags at the same position in the first index storage area and the second index storage area correspond to the same storage sub-area in the data storage area.

[0083] When the storage medium is divided, two index storage areas are divided, including a first index storage area and a second index storage area. The first index storage area and the second index storage area are of the same size, and both represent the storage status of each storage sub-area in the same data storage area. Moreover, the storage status represented by the storage value of the data index flag located at the same position in the first index storage area and the second index storage area is the same storage sub-area. That is, the corresponding relationship between each data index identification bit and the storage sub-area in the first index storage area is the same as that in the second index storage area. Exemplarily, the storage value of the first data index flag in the first index storage area represents the storage status of the first storage sub-area in the data storage area, and the storage value of the second data index flag in the first index storage area represents the storage status of the second storage sub-area in the data storage area, and so on; similarly, the storage value of the first data index flag in the second index storage area represents the storage status of the first storage sub-area in the data storage area, and the storage value of the second data index flag in the second index storage area represents the storage status of the second storage sub-area in the data storage area, and so on. For another example, a correspondence is established between multiple consecutive data index flags in the first index storage area and multiple consecutive storage sub-areas in the data storage area, and according to the same correspondence, a correspondence is established between multiple consecutive data index flags in the second index storage area and multiple consecutive storage sub-areas in the data storage area.

[0084] It can be considered that the two index storage areas are index backup areas for each other. When the data storage area stores the last storage sub-area, circular storage is required, that is, sequential storage is started again from the first storage sub-area in the data storage area. When re-storing, another index storage area is used to index and determine the storage status corresponding to each storage sub-area during the re-circulation process.

[0085] like Figure 3 As shown, the method includes:

[0086] S310, determining the last historical storage position in the data storage area according to the position of each data index flag in the index storage area and the storage value of each data index flag.

[0087] The data index flags in the index storage area correspond one-to-one to the storage sub-areas in the data storage area, and the storage value on the data index flag is used to indicate the storage state of the storage sub-area corresponding to the data index flag.

[0088] S320: In response to determining that the data storage area is full, erasing a data index flag bit of another index storage area corresponding to the current index storage area.

[0089] If the current index storage area is the first index storage area, the other index storage area is the second index storage area; or, if the current index storage area is the second index storage area, the other index storage area is the first index storage area.

[0090] If the last storage sub-area stored in the data storage area is determined during the storage process of the data to be stored according to the historical last storage position, and the data to be stored still includes unwritten data, then it is determined that the data storage area is full. The current index storage area is the storage area where the data index flag corresponding to the storage state of each storage sub-area in the data storage area before it is not full is located. If the current index storage area is the first index storage area, the other index storage area is the second index storage area; if the current index storage area is the second index storage area, the other index storage area is the first index storage area.

[0091] Specifically, if the current index storage area is the first index storage area, when storing to the last storage sub-area of ​​the data storage area, the storage value of the data index flag corresponding to the last storage sub-area in the first index storage area is modified, and the corresponding data is written into the last storage sub-area; then the data index flag of the second index storage area is erased, and before writing the subsequent data to be stored starting from the first storage sub-area of ​​the data storage area, the storage value of the data index flag corresponding to the first storage sub-area in the second index storage area is modified.

[0092] Since the data to be stored is stored in the data storage area in a sequential cyclic storage manner, when the data storage area has stored a full round, the next round of sequential storage from the beginning is started. Before this, the data index flag of the other index storage area opposite to the current index storage area is erased first. The data index flag of the other index storage area opposite to the current index storage area may be in an initial state before erasing, or may represent the storage state of each storage sub-area in the previous round of data storage area of ​​the current round.

[0093] S330, erasing at least the storage sector where the first address of the data storage area is located.

[0094] The storage sector is the smallest erasing unit in the storage medium. For example, if the storage medium is Flash, the storage sector is sector.

[0095] Since the data storage area is full and a new round of storage needs to be started, it is necessary to erase the data storage area to store in the erased area. Since the storage sector is the smallest erase unit, the erased object is at least one storage sector in the data storage area including the first address. In order to improve the storage efficiency of the data to be stored, the number of storage sectors is determined according to the size of the remaining stored data, and the continuous storage sectors including the first address are erased according to the number of storage sectors; or, according to the preset erase number, the corresponding number of continuous storage sectors including the first address in the data storage area are erased to avoid the subsequent multiple erase actions in the storage process affecting the storage efficiency. Among them, a storage sector includes multiple storage sub-areas, each storage sub-area is used to store a piece of historical data, and generally the size of a piece of historical data does not exceed 1 page, such as 64 bytes or 128 bytes.

[0096] In a feasible embodiment, the first index storage area and the second index storage area are further provided with a regional index flag respectively, and the regional index flag is used to indicate the validity of data in the index storage area where it is located;

[0097] Before erasing the storage sector where the first address of the data storage area is located, the method further includes:

[0098] The regional index flag of the current index storage area is set to invalid, and the regional index flag of another index storage area is set to valid.

[0099] Among them, the regional index flag is used to characterize whether the storage value on the data index flag bit in the index storage area where it is located indicates the storage status of the corresponding storage sub-area. For example, if the regional index flag of another index storage area is invalid, it means that when the storage sub-areas in the data storage area are currently stored, the storage value of the data index flag bit in another index storage area is modified; if the regional index flag of another index storage area is valid, it means that when the storage sub-areas in the data storage area are currently stored, the storage value of the data index flag bit in the index storage area is modified.

[0100] Exemplarily, the regional index flag is located at the first position of the first index storage area and the second index storage area for easy reading; the specific representation of the regional index flag bit can be set according to actual conditions and is not limited here. For example, it can be represented by one bit or at least two bits. Different values ​​on the bit to represent valid or invalid can be set according to actual conditions and is not limited here.

[0101] Specifically, in response to determining that the data storage area is full, after erasing the data index flag of another index storage area opposite to the current index storage area, the area index flag of the current index storage area is set to invalid, and the area index flag of another index storage area is set to valid.

[0102] Exemplarily, the regional index flag of another index storage area is first set to be valid, and then the regional index flag of the current index storage area is set to be invalid, to prevent the regional index flag in the index storage area from being lost due to power failure during the modification process.

[0103] S340, write a new storage value into the data index flag corresponding to the storage sub-area where the first address is located in another index storage area.

[0104] After erasing the corresponding storage sector, before executing data writing, the storage value on the data index flag bit corresponding to the storage sub-area where the first address is located in another index storage area is modified.

[0105] Exemplarily, the storage sub-area where the first address is located is the first storage sub-area, and the first storage sub-area corresponds to the first data index flag in the index storage area. Before writing data, the value of the bit corresponding to the first data index flag in another index storage area is modified to 0.

[0106] S350, writing the data to be stored into the storage sub-area where the first address is located.

[0107] After the corresponding data index flag is modified in another index storage area, the action of writing the data to be stored into the storage sub-area where the first address is located is executed.

[0108] Exemplarily, in the process of modifying the data index flag in another index storage area, only the regional index flag of the current index storage area corresponding to the other index storage area is modified, and the data index flag in the current index storage area is not erased, so as to avoid power failure in the middle, resulting in the inability to trace back to the last historical storage position before it is full.

[0109] The solution of the embodiment of the present application sets up two index storage areas. When the data storage area is full, writing continues to be performed on another index storage area corresponding to the current index storage area, thereby avoiding the loss of storage value of the data index flag caused by erasing the current index storage area when writing new data, thereby improving the stability of data storage positioning.

[0110] Figure 4 This is a flowchart of another method for reading and writing data on a storage medium provided in an embodiment of the present application. This embodiment further refines the steps in the above embodiment by performing corresponding data reading according to the historical last storage position.

[0111] The first index storage area and the second index storage area are also respectively provided with a regional index flag, and the regional index flag is used to indicate the validity of the data in the index storage area where it is located.

[0112] like Figure 4 As shown, the method includes:

[0113] S410, read the regional index flag of another index storage area corresponding to the current index storage area.

[0114] If the current index storage area is the first index storage area, the other index storage area is the second index storage area; or, if the current index storage area is the second index storage area, the other index storage area is the first index storage area.

[0115] The regional index flags of two index storage areas in the storage medium are read and judged. Since the regional index flag is located at the first position of the index storage area, the storage value of the regional index flag is directly read from the corresponding position of the first position. According to the pre-established correspondence between the storage value of the regional index flag and the valid flag and the invalid flag, it is determined whether the other index storage area is in an invalid state or a valid state.

[0116] S420, determining the last historical storage position in the data storage area according to the position of each data index flag bit in the target index storage area and the storage value of each data index flag bit.

[0117] The target index storage area is a storage area with a valid regional index flag. The data index flag of the index storage area corresponds to the storage sub-area in the data storage area one by one, and the storage value on the data index flag is used to indicate the storage state of the storage sub-area corresponding to the data index flag.

[0118] S430, in response to the region index flag of another index storage area being an invalid flag, reading the data in each storage sub-area in reverse order from the last historical storage position until the storage sub-area where the first address of the data storage area is located is read and the data reading is completed. Or,

[0119] If the area index flag of another index storage area is an invalid flag, it means that the data storage area is not fully stored when reading data. Specifically, the historical last storage position in the data storage area is determined according to the position of each data index flag bit in the current index storage area and the storage value on each data index flag bit, and the data in each storage sub-area is read in reverse order from the historical last storage position, so that the stored data is read in sequence from the nearest to the farthest according to the storage time, and the data reading is completed after the storage sub-area where the first address of the data storage area is located is read.

[0120] S440, in response to the regional index flag of another index storage area being a valid flag, reading the data in each storage sub-area in reverse order starting from the last historical storage position, and after reading the storage sub-area where the first address of the data storage area is located, continue to read the data in each storage sub-area in reverse order starting from the last storage sub-area of ​​the data storage area until a blank storage sub-area or the storage sub-area where the last historical storage position is located is read.

[0121] If the area index flag of another index storage area is a valid flag, it means that the data storage area is reading data when the storage is full for one round. Specifically, the historical last storage position in the data storage area is determined according to the position of each data index flag bit in another index storage area and the storage value on each data index flag bit, and the data in each storage sub-area is read in reverse order from the historical last storage position, so as to realize the reading of the stored data from near to far according to the storage time, and after reading the storage sub-area where the first address of the data storage area is located, the data in each storage sub-area is read in reverse order from the last storage sub-area of ​​the data storage area. This is because the current situation is that the data storage area is full for one round, and there is a situation where the storage sector at the beginning of the data storage area is erased and rewritten. In this case, if the reading requirement is still not met after reading from the historical last position to the first storage sub-area, the data before erasure is continued to be read until a blank storage sub-area or a storage sub-area where the historical last storage position is located is read, and all the data in the data storage area is read.

[0122] The solution of the embodiment of the present application determines the data reading order by first reading the regional index marks of the two index storage areas when reading data, avoids the phenomenon that the data reading order is wrong when the data storage area performs circular storage, and improves data reading efficiency and accuracy.

[0123] Figure 5 A flowchart of a method for writing data to a storage medium provided in an embodiment of the present application. In this embodiment, the storage medium is Flash as an example, and the Flash is divided into at least one storage block block corresponding to the storage space, such as Figure 6 The figure shows a schematic diagram of the division of storage space, in which the storage block block is divided into two index storage areas and one data storage area, the storage area corresponds to the sector, and in the index storage area, a data index flag corresponds to a bit in a byte, and eight consecutive data index flags in the index storage area correspond to eight consecutive storage sub-areas in the data storage area, and the storage values ​​of multiple consecutive data index flags are converted into a data index flag by the binary to hexadecimal method.

[0124] In the embodiment of the present application, considering that the internal FLASH of the MCU is written with at least 4 bytes at a time and the external FLASH is written with at least 1 byte at a time, here, for the simplicity of program operation, at least 4 bytes are written to the FLASH at a time. By defining a community, the data index flag in the index storage area is read, and the community is defined as follows:

[0125] typedef union{

[0126] unsigned char flagU8[4];

[0127] unsigned int flagU32;

[0128] }FLASH_FLAG;

[0129] FLASH_FLAG g_historySaveFlag[N];

[0130] The size of N is related to the size of the data storage area. Assuming that the data storage area can store X pieces of historical data, N is X / 32. If X is a multiple of 32, then N=X / 32; otherwise, N=X / 32 (rounded)+1.

[0131] Two storage areas (sector n0 is used as the first index storage area, and sector n1 is used as the second index storage area) are used as index storage areas. The storage data in the index storage area includes a region index flag (8 bytes) and a data index flag; wherein,

[0132] If the eight bytes corresponding to the regional index mark position of the index storage area are all 0xFF, it means that there is no valid data index mark in the index storage area; if the eight bytes corresponding to the regional index mark position of the index storage area are all 0xFE, it means that there is a valid data index mark in the index storage area. Figure 6 As shown, the first index area is a valid index area, and the second index area is an invalid index area.

[0133] The data index flag of each byte in the index storage area corresponds to the storage status of the 8 storage sub-areas. There are 9 situations for the read value of the data index flag of each byte: 0xFF means that there is no data in the corresponding 8 storage sub-areas; 0x7F means that the first storage sub-area of ​​the corresponding 8 storage sub-areas has stored data; 0x3F means that the first two storage sub-areas of the corresponding 8 storage sub-areas have stored data; 0x1F means that the first three storage sub-areas of the corresponding 8 storage sub-areas have stored data; 0x0F means that the first four storage sub-areas of the corresponding 8 storage sub-areas have stored data; 0x07 means that the first five storage sub-areas of the corresponding 8 storage sub-areas have stored data; 0x03 means that the first six storage sub-areas of the corresponding 8 storage sub-areas have stored data; 0x01 means that the first seven storage sub-areas of the corresponding 8 storage sub-areas have stored data; 0x00 means that all the corresponding 8 storage sub-areas have stored data. Figure 6 As shown, the third data index mark is 0x00, indicating that no data is stored in the eight storage sub-areas corresponding to the data index mark. The historical last storage position is determined according to the position and specific value of the second data index mark.

[0134] In the embodiment of the present application, the data index flag in the index storage area is represented by the community g_historySaveFlag[x1].flagU8[y1] defined above. When the first historical data is stored in the first storage sub-area corresponding to the data storage area, g_historySaveFlag[0].flagU8[0]=0x7F; when the second historical data is stored in the second storage sub-area corresponding to the data storage area, g_historySaveFlag[0].flagU8[0]=0x3F; ...; when the eighth historical data is stored in the eighth storage sub-area corresponding to the data storage area, g_historySaveFlag[0].flagU8[0]=0x00; when the ninth historical data is stored in the eighth storage sub-area corresponding to the data storage area, g_historySaveFlag[0].flagU8[0]=0x01; When the 32nd historical data is stored in the 32nd storage sub-area corresponding to the data storage area, g_historySaveFlag[0].flagU8[3]=0x00; when the 33rd historical data is stored in the 33rd storage sub-area corresponding to the data storage area, g_historySaveFlag[1].flagU8[0]=0x7F; ...; when the 64th historical data is stored in the 64th storage sub-area corresponding to the data storage area, g_historySaveFlag[1].flagU8[3]=0x00; ...; and so on.

[0135] Assuming that the size of the index storage area sector is 4096 bytes, and each bit corresponds to a data storage sub-area, the data index mark of an index storage area can correspond to (4096-8)*8 historical data, among which 8 bytes in 4096-8 need to be allocated to the regional index mark.

[0136] After the storage medium is powered on and initialized, the regional index flags of the first index storage area and the second index storage area are read first to confirm whether there is valid data. If there is valid data, just read the data index flag in the valid index storage area first, find two consecutive bytes, the first byte is not 0xFF, and the second byte is 0xFF. The value of the data index flag g_historySaveFlag[x1].flagU8[y1] that is not 0xFF in the two adjacent bytes determines the rough position x1*32+y1*8 of the last historical storage position, and then uses g_historySave The specific value of Flag[x1].flagU8[y1] determines the specific number of storage items at the last power failure (if g_historySaveFlag[x1].flagU8[y1]=0x7F on this byte, the serial number corresponding to the last historical storage position is x1*32+y1*8+1; if it is 0x3F, the serial number corresponding to the last historical storage position is x1*32+y1*8+2; ... if it is 0000, the serial number corresponding to the last historical storage position is x1*32+y1*8+8), and finally the last historical storage position is determined according to the serial number corresponding to the last historical storage position.

[0137] By means of the above method, when the storage medium is powered on and initialized, the last historical storage position and the current storage position can be quickly calculated.

[0138] like Figure 7 The flowchart shown is for determining the last historical storage location when the storage medium system is powered on and initialized.

[0139] First, read the first eight bytes of the first index storage area sector n0 and the first eight bytes of the second index storage area sector n1. The first eight bytes are the regional index mark of each index storage area; if the eight bytes in the regional index mark in any index storage area are 0xFE, it means that the valid data index mark is in the area, and the area is determined to be the current index storage area; if the first eight bytes of sector n0 and sector n1 are not 0xFE, it means that there is no valid historical data, and the valid historical data returned is 0, and the last historical storage address is the first address of the data storage area.

[0140] After the current index storage area is determined from the first index storage area and the second index storage area, the data index flag g_historySaveFlag[x1].flagU8[y1] is read from the position of each data index flag bit in the current index storage area in the order of the storage address.

[0141] The last historical storage location is determined according to the value of the data index flag g_historySaveFlag[x1].flagU8[y1]. Specifically, if the value of g_historySaveFlag[x1].flagU8[y1] is 0x00, the rough location x1*32+y1*8 is determined according to the location of the data index flag g_historySaveFlag[x1], and then the last historical storage location is determined according to the specific value of the data index flag.

[0142] For example, if the specific value of the data index flag g_historySaveFlag[x1].flagU8[y1] is 0xFF, the serial number of the last storage location in history is x1*32+y1*8; if the specific value of g_historySaveFlag[x1].flagU8[y1] is 0x7F, the serial number of the last storage location in history is x1*32+y1*8+1; if the specific value of g_historySaveFlag[x1].flagU8[y1] is 0x3F, the serial number of the last storage location in history is x1*32+y1*8+2; if the specific value of g_historySaveFlag[x1].flagU8[y1] is 0x1F, the serial number of the last storage location in history is x1*32+y1*8+3; if the specific value of g_historySaveFlag[x1].flagU8[y1] is 0x 0F, the serial number of the last historical storage position is x1*32+y1*8+4; if the specific value of g_historySaveFlag[x1].flagU8[y1] is 0x07, the serial number of the last historical storage position is x1*32+y1*8+5; if the specific value of g_historySaveFlag[x1].flagU8[y1] is 0x03, the serial number of the last historical storage position is x1*32+y1*8+6; if the specific value of g_historySaveFlag[x1].flagU8[y1] is 0x01, the serial number of the last historical storage position is x1*32+y1*8+7; finally, the last historical storage position (address) = the allocated first address corresponding to the data storage area + (the serial number of the last historical storage position - 1) * the data length of each storage sub-area, where the data length of each storage sub-area is the data length of each historical data.

[0143] As shown in Table 2, it is a correspondence table between the storage values ​​of the eight data index flag bits (bit0-bit7) corresponding to the data index flag g_historySaveFlag[x1].flagU8[y1] and the last historical storage position.

[0144] Table 2

[0145]

[0146]

[0147] like Figure 5 As shown, the data writing method of the storage medium includes:

[0148] First, determine the serial number (Index0+1) of the last historical storage location determined above and the total number of storage entries X of the data to be stored;

[0149] If (Index0+1) is less than or equal to X, indicating that the data storage area is not currently full, then modify the storage value on the data index flag corresponding to the current storage position in the current index storage area. Specifically, determine the modified new data index flag according to the current value of the data index flag g_historySaveFlag[x1].flagU8[y1] corresponding to the last storage position in history), and then write the data to be stored into the current storage position;

[0150] If (Index0+1) is greater than X, it means that all data storage areas are full and data needs to be stored again from the beginning. Then erase another index storage area sector ny, and then write the first 8 bytes of the index storage area to 0xFE, that is, change the regional index flag to a valid flag, and then write the first 8 bytes of the current index storage area sector nx to 0X00. By first modifying the valid flag and then changing the old regional index flag to an invalid flag, the regional index flag is prevented from being lost when power is lost. Among them, the current index storage area is represented by sector nx, and the other index storage area is represented by sector ny. If nx is n0, ny is n1; if nx is n1, ny is n0.

[0151] At least erasing the storage sector where the first address of the data storage area is located;

[0152] In another index storage area, a new storage value is written to the data index flag corresponding to the storage sub-area where the first address is located, that is, the first data index flag g_historySaveFlag[x1].flagU8[y1] (x1=0, y1=0) of another index storage area sector ny is changed to 0x7F.

[0153] The data to be stored is written into the storage sub-area where the first address is located, and the last valid historical data returned is the first one (Index0=1). The last historical data stored is the allocated first address of the data storage area (address0=allocated first address).

[0154] Continue to write the corresponding storage sub-area according to the data to be stored, and during the writing process, determine the next data index flag value to be written according to the value of the current data index flag g_historySaveFlag[x1].flagU8[y1].

[0155] For example, if the current data index flag g_historySaveFlag[x1].flagU8[y1] is 0x7F, the next data index flag value g_historySaveFlag[x1].flagU8[y1] to be written is 0x3F; if the current data index flag g_historySaveFlag[x1].flagU8[y1] is 0x3F, the next data index flag value g_historySaveFlag[x1].flagU8[y1] to be written is 0x1F; if the current data index flag g_historySaveFlag[x1].flagU8[y1] is 0x3F, the next data index flag value g_historySaveFlag[x1].flagU8[y1] to be written is 0x1F; g[x1].flagU8[y1] is 0x1F, then the next data index flag value to be written g_historySaveFlag[x1].flagU8[y1] is 0x0F; if the current data index flag g_historySaveFlag[x1].flagU8[y1] is 0x0F, then the next data index flag value to be written g_historySaveFlag[x1].flagU8[y1] is 0x07; if the current data index flag g_historySaveFlag[x1].flagU8[y1] is 0x07, then the next data index flag value to be written g_historySaveFlag[x1].flagU8[y1] is 0x07 The data index flag value g_historySaveFlag[x1].flagU8[y1] is 0x03; if the current data index flag g_historySaveFlag[x1].flagU8[y1] is 0x03, the next data index flag value to be written g_historySaveFlag[x1].flagU8[y1] is 0x01; if the current data index flag g_historySaveFlag[x1].flagU8[y1] is 0x01, the next data index flag value to be written g_historySaveFlag[x1].flagU8[y1] is 0x01 agU8[y1] is 0x00; if the current data index flag g_historySaveFlag[x1].flagU8[y1] is 0x00 and y1<3, the next data index flag value to be written g_historySaveFlag[x1].flagU8[y1+1] is 0x7F; if the current data index flag g_historySaveFlag[x1].flagU8[y1] is 0x00 and y1=3, the next data index flag value to be written g_historySaveFlag[x1+1].flagU8[0] is 0x7F.

[0156] The scheme of the embodiment of the present application realizes rapid search of the last historical storage position by means of index when writing data; and when writing data, first modifies the storage value of the corresponding data index flag, and then writes the corresponding data to be stored, so as to ensure that power failure when writing Flash will not affect the continued writing of subsequent data; when modifying the area index flag, first writes the valid flag, and then erases the invalid flag, so as to avoid flag writing errors due to interference or power failure when modifying the area index flag. The present application realizes rapid data writing as well as writing security and stability through the above.

[0157] Figure 8 This is a flow chart of a method for reading data from a storage medium provided in an embodiment of the present application. This embodiment performs data reading based on the above data writing embodiment. The relevant basic settings are not repeated here. Please refer to the above embodiment. Figure 8 As shown, the method includes:

[0158] Read the regional index mark of another index storage area sector ny, that is, the first 8 bits of data of sector ny. If they are all 0x00, it means that the historical data has been fully stored in the data storage area. At this time, the number of historical data items X0 that can be read is: X-(the size of the data storage area divided by the data length of a historical data item-Index0); where X is the total number of historical data items that can be stored in the data storage area.

[0159] If the region index mark of another mark index region sector ny is not 0x00, it means that the historical data has not been stored for a full round in the data storage area, and the number of historical data items X0 that can be read is: Index0; wherein Index0 is the serial number of the historical last storage position obtained according to the above embodiment.

[0160] When X0 is 0, the historical data reading is completed and the process is exited;

[0161] If X0>0 and index0=0, then index0=X, so as to read from the end of the data storage area in reverse order;

[0162] If X0>0 and index0>0, index0 remains unchanged;

[0163] First read the historical data of the storage area address0 corresponding to index0 (= the allocated first address of the data storage area + (Index0-1) * the data length of each historical data);

[0164] Determine whether the historical data is verified correctly. If the verification is correct, it means that the historical data is credible and is uploaded to the user;

[0165] If the verification fails, the historical data is considered unreliable and discarded, and the reading continues;

[0166] X0=X0-1, index0=index0-1, until X0 is 0, it returns that the historical data reading is completed and exits the process.

[0167] Each packet of historical data is composed of: data flag + real-time time (serial number) + data to be stored + checksum (real-time time (serial number) + CRC checksum or other checksum methods of data to be stored). The length of each packet of historical data is preferably 2 to the power of n (such as 32, 64, 128, 256, etc.) or an integer multiple of the minimum storage page, which is determined according to the size of the storage sub-area. If the length is less than the size of the corresponding storage sub-area, the data to be stored can be padded with 0xFF. The data flag is used to indicate the validity of the packet data. When the packet data is read, the data is first determined based on the data flag to determine whether the data is valid, avoiding the complex workload caused by the need to check each packet data, and improving the data reading efficiency. If the data flag of the packet data is valid, the checksum value is determined based on the stored data and the checksum method, and compared with the checksum value in the data packet. If they are consistent, it means that the packet data is credible and can be uploaded to the user for reading. If they are inconsistent, it means that the packet data is untrustworthy and the packet data is discarded.

[0168] The solution of the embodiment of the present application realizes rapid search for the last historical storage position by means of indexes when reading data, thereby improving the efficiency of data reading; and further improves the reading accuracy when performing circular storage in the data storage area by judging the regional index marks of different index storage areas.

[0169] The acquisition, storage, use, and processing of data in the technical solution of this application comply with the relevant provisions of national laws and regulations and do not violate public order and good morals.

[0170] According to an embodiment of the present disclosure, the present disclosure also provides an electronic device, a readable storage medium and a computer program product.

[0171] Fig. 9A schematic diagram of an electronic device 10 that can be used to implement an embodiment of the present application is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices (such as helmets, glasses, watches, etc.) and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present application described and / or required herein.

[0172] like Fig. 9 As shown, the electronic device 10 includes at least one processor 11, and a memory connected to the at least one processor 11, such as a read-only memory (ROM) 12, a random access memory (RAM) 13, etc., wherein the memory stores a computer program that can be executed by at least one processor, and the processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 to the random access memory (RAM) 13. In the RAM 13, various programs and data required for the operation of the electronic device 10 can also be stored. The processor 11, the ROM 12, and the RAM 13 are connected to each other through a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.

[0173] A number of components in the electronic device 10 are connected to the I / O interface 15, including: an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.

[0174] The processor 11 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as a method for reading and writing data on a storage medium.

[0175] In some embodiments, the data reading and writing method of the storage medium can be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as a storage unit 18. In some embodiments, part or all of the computer program can be loaded and / or installed on the electronic device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps in the data reading and writing method of the storage medium described above can be performed. Alternatively, in other embodiments, the processor 11 can be configured to execute the data reading and writing method of the storage medium in any other appropriate manner (for example, by means of firmware).

[0176] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific reference products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.

[0177] The computer programs for implementing the methods of the present application may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that when the computer programs are executed by the processor, the functions / operations specified in the flow charts and / or block diagrams are implemented. The computer programs may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.

[0178] In the context of the present application, a computer readable storage medium may be a tangible medium that may contain or store a computer program for use by or in conjunction with an instruction execution system, device or equipment. A computer readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, a computer readable storage medium may be a machine readable signal medium. A more specific example of a machine readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0179] To provide interaction with a user, the systems and techniques described herein may be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and a pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices may also be used to provide interaction with the user; for example, the feedback provided to the user may be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form (including acoustic input, voice input, or tactile input).

[0180] The systems and techniques described herein may be implemented in a computing system that includes a backend component (e.g., as a data server), or a computing system that includes a switch component (e.g., an application server), or a computing system that includes a frontend component (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with an implementation of the systems and techniques described herein), or a computing system that includes any combination of such backend components, switch components, or frontend components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.

[0181] A computing system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The client and server relationship is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services.

[0182] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this application can be executed in parallel, sequentially or in different orders, as long as the expected results of the technical solution of this application can be achieved, and this document is not limited here.

[0183] The above specific implementations do not constitute a limitation on the protection scope of this application. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of this application should be included in the protection scope of this application.

Claims

1. A method for reading and writing data in a storage medium, characterized in that: The storage medium includes at least one storage space, and the storage space is divided into at least an index storage area and a data storage area; The method comprises: Determine the historical last storage position in the data storage area according to the position of each data index flag bit in the index storage area and the storage value on each data index flag bit; wherein the data index flag bits in the index storage area correspond one-to-one to the storage sub-areas in the data storage area, and the storage value on the data index flag bit is used to indicate the storage state of the storage sub-area corresponding to the data index flag bit; Corresponding data writing or data reading is performed according to the historical last storage location.

2. The method according to claim 1, characterized in that in, A plurality of continuous data index flag bits in the index storage area correspond to a plurality of continuous storage sub-areas in the data storage area, and the storage values ​​of the plurality of continuous data index flag bits are converted into a data index flag.

3. The method according to claim 2, characterized in that Determining the historical last storage position in the data storage area according to the position of each data index flag bit in the index storage area and the storage value on each data index flag bit includes: Determine a target data index flag bit according to the data index flag; wherein the data index flag converted from a storage value including at least the target data index flag bit is not the first data index flag, and the data index flag converted from a storage value including at least the next data index flag bit of the target data index flag bit is the first data index flag; wherein the first data index flag indicates that no data is stored in a plurality of consecutive storage sub-areas corresponding to the plurality of consecutive data index flag bits; The historical last storage position is determined according to the position of the target data index flag in the index storage area and the data index flag.

4. The method according to claim 1, characterized in that: The writing of corresponding data according to the historical last storage position includes: The current storage position of the data to be stored is determined according to the historical last storage position, the storage value on the data index flag corresponding to the current storage position in the index storage area is modified, and the data to be stored is written into the current storage position.

5. The method according to any one of claims 1 to 4, characterized in that: in, The index storage area at least includes a first index storage area and a second index storage area; the data index flags at the same position in the first index storage area and the second index storage area correspond to the same storage sub-area in the data storage area.

6. The method according to claim 5, characterized in that The writing of corresponding data according to the historical last storage position includes: In response to determining that the data storage area is full, erasing the data index flag of another index storage area opposite to the current index storage area; wherein, if the current index storage area is the first index storage area, the another index storage area is the second index storage area, or, if the current index storage area is the second index storage area, the another index storage area is the first index storage area; At least erasing the storage sector where the first address of the data storage area is located; Writing a new storage value into the data index flag corresponding to the storage sub-area where the first address is located in the other index storage area; The data to be stored is written into the storage sub-area where the first address is located.

7. The method according to claim 6, characterized in that The first index storage area and the second index storage area are also respectively provided with a regional index flag, and the regional index flag is used to indicate the validity of the data in the index storage area where it is located; Before erasing the storage sector where the first address of the data storage area is located, the method further includes: The area index flag of the current index storage area is set to be invalid, and the area index flag of another index storage area is set to be valid.

8. The method according to claim 5, characterized in that The first index storage area and the second index storage area are also respectively provided with a regional index flag, and the regional index flag is used to indicate the validity of the data in the index storage area where it is located; The reading of corresponding data according to the historical last storage position includes: Reading a regional index flag of another index storage area relative to the current index storage area; wherein, if the current index storage area is the first index storage area, the other index storage area is the second index storage area, or, if the current index storage area is the second index storage area, the other index storage area is the first index storage area; In response to the region index flag of the other index storage area being an invalid flag, reading data in each storage sub-area in reverse order starting from the last historical storage position until the storage sub-area where the first address of the data storage area is located is read and the data reading is completed; or, In response to the area index flag of the other index storage area being a valid flag, the data in each storage sub-area is read in reverse order starting from the last historical storage position. After reading the storage sub-area where the first address of the data storage area is located, the data in each storage sub-area is read in reverse order starting from the last storage sub-area of ​​the data storage area until a blank storage sub-area or the storage sub-area where the last historical storage position is located is read.

9. A storage medium, characterized in that: The storage medium stores computer executable instructions, and when the computer executable instructions are executed by a processor, the data reading and writing method according to any one of claims 1 to 8 is executed.

10. An electronic device, characterized in that: include: at least one processor; as well as A storage medium in communication with the at least one processor; wherein, The storage medium stores computer executable instructions that can be executed by the at least one processor. When the computer executable instructions are executed by the at least one processor, the at least one processor can execute the data reading and writing method according to any one of claims 1 to 8.

Citation Information

Cited By

  • Axial projection wind speed measuring method and system of wind turbine unit

    CN121878262A