A method for quickly accessing SPI Flash data
Through the combination of index tables and data areas, the life and speed problems of SPI Flash in frequent data access are solved, and the effects of fast access and life extension are achieved.
Patent Information
- Application Number
- CN202310085981.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-02-02
- Publication Date
- 2025-09-02
- Estimated Expiration
- 2043-02-02
AI Technical Summary
In the prior art, SPI Flash has a large storage capacity and low price, but its read and write speed is slow and its lifespan is limited, making it difficult to reconcile the problem of reducing memory life and slow execution speed when the file system frequently accesses data.
Using a combination of index table and data area, the index table is used to locate data locations, and the data area is used to store data. Data access is managed through the status, address offset and number of sectors of the index table attribute items and data index items, reducing the number of erases and access time.
It effectively reduces the number of sector erases of SPI Flash, improves data access speed, extends memory life, and does not rely on standard file systems, shortens data access time.
Smart Images

Figure CN116089322B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of data processing, and in particular relates to a method for quickly accessing SPI Flash data. Background Art
[0002] File systems are commonly used to implement data access, management, and other functions in embedded systems. Common file systems, such as FatFs (a general-purpose FAT file system) and LittleFs (a small, secure file system designed for microcontrollers), are based on non-volatile physical storage media. Primary non-volatile memories include EEPROM (Electrically Erasable Programmable Read-Only Memory) and SPI Flash (Serial Peripheral Interface Flash).
[0003] Traditional EEPROMs are characterized by the ability to randomly access and modify any byte without the need for an erase operation. However, their storage capacity is limited by cost, rarely exceeding 512KB. In embedded systems that require large amounts of data storage while maintaining tight cost controls, SPI Flash is often chosen. Its features include large storage capacity and low price; a limited read / write cycle lifespan of typically 100,000 cycles; and slow read / write speeds. Writing to Flash requires changing data bits from 1 to 0; otherwise, an erase operation is required. Erasing is performed in sectors (4KB in size), resulting in all data being 0xFF. A typical erase time is 50ms.
[0004] The aforementioned characteristics of SPI Flash present numerous challenges for file system usage. For example, FatFs operations require sector erasure each time, significantly reducing the memory lifespan in applications with frequent data access. While LittleFs uses a balancing algorithm to reduce erase times, its method of traversing the entire storage address range results in slower execution as more data is stored. This conflict between application requirements and physical characteristics is difficult to reconcile. Therefore, the present invention proposes a data access method based on SPI Flash. Summary of the Invention
[0005] In order to overcome the problems in the prior art, the present invention provides a method for quickly accessing SPI Flash data.
[0006] The technical solution of the present invention to solve the above technical problems is as follows:
[0007] A method for quickly accessing SPI Flash data includes an index table and a data area, wherein the index table is used to locate the position of each type of data, and the data area is used to store SPI Flash data;
[0008] The index table includes index table attribute items and data index items, the index table attribute items and data index items use the same data structure, and the index table attribute items and data index items include status, address offset, and the number of sectors required for the corresponding data area;
[0009] The first sector of the data area is a data area index table for each type of data. Each piece of data corresponds to an index and stores key information of the data, including entry number, status, and absolute physical address of data storage.
[0010] Furthermore, the status of the index table attribute item and the data index item uses the magic number 0x9824f8e9 to indicate whether the data type already exists; the number of sectors is calculated based on the required data size; and the address offset is the absolute physical address of the Flash.
[0011] Furthermore, after the system is powered on, the index table is created and updated according to each data type number, including the following steps:
[0012] Step 101: Read the content of the data index item from the index table according to the number n;
[0013] Step 102: Determine whether the data index item status already exists; if so, execute step 103; if not, execute steps 1a to 1d and then execute step 103;
[0014] Step 1a: Create a new data index item numbered n;
[0015] Step 1b: Read the entire content of the index table;
[0016] Step 1c: Determine whether the index table attribute item state already exists. If not, assign a value to the index table attribute item. If yes, assign a value to the data index item.
[0017] Step 1d: Calculate the address offset of the attribute item in the index table;
[0018] Step 103: Update the index table.
[0019] Furthermore, when writing data, the following steps are included:
[0020] Step 201: writing data;
[0021] Step 202: The data area index table is retrieved to find the index entry with the status of "used" 0xFFFFFFF7, and the storage address is obtained to start storing data;
[0022] Step 203: Determine whether the storage address is the first address of the sector. If so, erase the sector before storing. If not, store directly without erasing.
[0023] Step 204: After storage is completed, the status value of the current data area index table is modified to be deletable 0xFFFFFFF6, and a new index entry with a status value of 0xFFFFFFF7 is added to the data area index table;
[0024] Step 205: Update the data area index table.
[0025] Furthermore, when reading out all the stored data, the following steps are included:
[0026] Step 301: Search the data area index table to find the index item with the status value 0xFFFFFFF7, and obtain the stored data item number n;
[0027] Step 302: Calculate the offset address of each piece of data;
[0028] Step 303: After reading a piece of data, the data entry number n-1 has been stored;
[0029] Step 304: If n=0, the reading ends.
[0030] Compared with the prior art, the present invention has the following technical effects:
[0031] The present invention combines an index table with data partitioning, effectively reducing the number of sector erases and enabling rapid location of data storage. The present invention's index table is used to locate the location of each type of data, while the index table in the data zone is used to locate the location of each piece of data within that type of data, enabling rapid location of data storage. The present invention abstracts data into file operations without relying on a standard file system, shortening data access time and reducing the lifespan of the SPI Flash. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 A schematic diagram of the data storage structure of the present invention;
[0033] Figure 2 Schematic diagram of the index table data structure of the present invention;
[0034] Figure 3 Schematic diagram of the creation and update process of the index table of the present invention;
[0035] Figure 4 Schematic diagram of the data structure of the data area of the present invention;
[0036] Figure 5 This is a schematic diagram of the data area index table update process of the present invention;
[0037] Figure 6 Flowchart for reading out all data that has been stored for the present invention. DETAILED DESCRIPTION
[0038] The principles and features of the present invention are described below with reference to the accompanying drawings. The examples given are only used to explain the present invention and are not used to limit the scope of the present invention.
[0039] like Figure 1 As shown, an embodiment of the present invention provides a method for fast access to SPI Flash data, including an index table and a data area, wherein the index table is used to locate the position of each type of data, and the data area is used to store the SPI Flash data; the index table includes index table attribute items and data index items, and the index table attribute items and the data index items use the same data structure, and the index table attribute items and the data index items include status, address offset, and the number of sectors required for the corresponding data area; the first sector of the data area is the data area index table for each type of data, and each data item corresponds to an index, which stores key information of the data, and the key information includes entry number, status, and the absolute physical address of the data storage.
[0040] like Figure 2 As shown, in an embodiment of the present invention, the index table is used to locate the position of each type of data. In the index table attribute item and the data index item of the index table, the status uses the magic number 0x9824f8e9 to indicate whether the data type already exists. The number of sectors is calculated based on the required data size, and the address offset is the absolute physical address of the Flash.
[0041] In a specific embodiment, for an index table attribute item, the number of sectors is 1, and the address offset calculation formula of the index table attribute item is as follows:
[0042] Address offset = (1 + total number of sectors used by existing data) × sector size
[0043] For data index items, the address offset of the data index item is the same as the address offset of the index table attribute item; the number of sectors is determined by the data type size, data type entry, and sector size, and its calculation formula is as follows:
[0044] Number of sectors = (data type size × data type entry ÷ sector size) + 3
[0045] Assign a number from 1 to n to each type of data to determine the position of the data index item in the index table. The data index item offset address is:
[0046] Data index item offset address = type number × size of index item data structure
[0047] like Figure 3As shown, in an embodiment of the present invention, after the system is powered on, the index table is created and updated according to each data type number, including the following steps:
[0048] Step 101: Read the content of the data index item from the index table according to the number n;
[0049] Step 102: Determine whether the data index item status magic number 0x9824f8e9 already exists; if so, execute step 103; if not, execute steps 1a to 1d and then execute step 103;
[0050] Step 1a: Create a new data index item numbered n;
[0051] Step 1b: Read the entire content of the index table;
[0052] Step 1c: Determine whether the magic number 0x9824f8e9 of the index table attribute item status already exists. If not, assign a value to the index table attribute item. If so, assign a value to the data index item.
[0053] Step 1d: Calculate the address offset of the attribute item in the index table;
[0054] Step 103: Update the index table.
[0055] like Figure 4 As shown, in the embodiment of the present invention, respective address spaces have been applied for all types of data in the index table creation stage. Figure 4 Take the structure of the first type of data area applied for as an example: the Flash sector size is 4KBytes, the data type size is 32Bytes, and a minimum of 100 entries are required to be stored. According to the previous formula, the required number of sectors is 3. Since the index table already uses 1 sector, the address offset of this type of data in the Flash is 1 sector, that is, 4K.
[0056] The first sector of the data area contains the index table for this data type. This index table is used to locate each data item within this data type. Each data item corresponds to an index, which stores key information about the data. The data area index structure primarily includes the item number, status, and the absolute physical address where the data is stored. It is important to note that the status values are defined as "used (0xFFFFFFF7)" and "erasable (0xFFFFFFF6)" because rewriting from "used" to "erasable" does not require erasing the Flash memory.
[0057] like Figure 5As shown, in an embodiment of the present invention, before data is stored, the most recent index content with a status of "used" is searched in the data area index table to determine the absolute storage address of the newly added data in the SPI Flash. The storage is looped back based on the position determined after retrieving the data index table, and an erase operation is performed only when the storage reaches the position of the first address of each sector. This ensures that the required number of data is stored and avoids the time and life loss of the SPI Flash caused by frequent erasing of sectors and processing of data. The status of the newly stored data is "used", the status of the previous data is set to "deletable", and the data index table is updated.
[0058] When writing data, the following steps are included:
[0059] Step 201: writing data;
[0060] Step 202: The data area index table is retrieved to find the index entry with the status of "used" 0xFFFFFFF7, and the storage address is obtained to start storing data;
[0061] Step 203: Determine whether the storage address is the first address of the sector. If so, erase the sector before storing. If not, store directly without erasing.
[0062] Step 204: After storage is completed, the status value of the current data area index table is modified to be deletable 0xFFFFFFF6, and a new index entry with a status value of 0xFFFFFFF7 is added to the data area index table;
[0063] Step 205: Update the data area index table.
[0064] In the embodiment of the present invention, as described above, each time data is stored, the number, status and other information of the current data entry are recorded. Therefore, when data needs to be read, the physical address where the required data is stored can be easily calculated based on the number of currently stored data, the size of each data item, and other information, and the required amount of data can be read. Figure 6 As shown, when reading out all the stored data, the following steps are included:
[0065] Step 301: Search the data area index table to find the index item with the status value 0xFFFFFFF7, and obtain the stored data item number n;
[0066] Step 302: Calculate the offset address of each piece of data;
[0067] Step 303: After reading a piece of data, the data entry number n-1 has been stored;
[0068] Step 304: If n=0, the reading ends.
[0069] This invention abstracts data into file operations without relying on a standard file system, shortening data access time and reducing the lifespan of the SPI Flash. The index table, combined with data partitioning, effectively reduces the number of sector erases and enables quick data location locator.
[0070] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for fast accessing SPI Flash data, characterized in that: It includes an index table and a data area, wherein the index table is used to locate the position of each type of data, and the data area is used to store SPI Flash data; The index table includes index table attribute items and data index items, the index table attribute items and data index items use the same data structure, and both the index table attribute items and the data index items include status, address offset, and the number of sectors required for the corresponding data area; The first sector of the data area is a data area index table for each type of data. Each piece of data corresponds to an index and stores key information of the data, including entry number, status, and absolute physical address of the data storage; The status of the index table attribute items and data index items uses the magic number 0x9824f8e9 to indicate whether each type of data managed by the index table already exists; The number of sectors is calculated based on the required data size; the address offset is the absolute physical address of the Flash; After the system is powered on, the index table is created and updated according to each data type number, including the following steps: Step 101: Read the content of the data index item from the index table according to the number n; Step 102: Determine whether the data index item status already exists; if so, execute step 103; if not, execute steps 1a to 1d and then execute step 103; Step 1a: Create a new data index item numbered n; Step 1b: Read the entire content of the index table; Step 1c: Determine whether the index table attribute item state already exists. If not, assign a value to the index table attribute item. If yes, assign a value to the data index item. Step 1d: Calculate the address offset of the attribute item in the index table; Step 103: Update the index table; When writing data, the following steps are included: Step 201: writing data; Step 202: The data area index table is retrieved to find the index entry with the status of "used" 0xFFFFFFF7, and the storage address is obtained to start storing data; Step 203: Determine whether the storage address is the first address of the sector. If so, erase the sector before storing. If not, store directly without erasing. Step 204: After storage is completed, the status value of the current data area index table is modified to be deletable 0xFFFFFFF6, and a new index entry with a status value of 0xFFFFFFF7 is added to the data area index table; Step 205: Update the data area index table.
2. A kind of SPI Flash data fast access method according to claim 1, it is characterized in that, When reading out all stored data, the following steps are included: Step 301: Search the data area index table to find the index item with the status value 0xFFFFFFF7, and obtain the stored data item number n; Step 302: Calculate the offset address of each piece of data; Step 303: After reading a piece of data, the data entry number n-1 has been stored; Step 304: If n=0, the reading ends.
Citation Information
Patent Citations
File storage method of embedded system
CN110262755A
Data storage management method of embedded NOR FLASH chip
CN113886281A
Data storage method for logging instrument
CN114153400A