Data storage method, electronic equipment and storage medium
The management module manages storage blocks based on the check cycle and header information in the storage management system, optimizes the use of Flash memory, reduces the number of erase times, improves space utilization and write efficiency, solves the problems of frequent erasure and low efficiency of Flash memory in the existing technology, and extends the service life of Flash.
Patent Information
- Application Number
- CN202510765773.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-10
- Publication Date
- 2025-09-19
- Estimated Expiration
- 2045-06-10
AI Technical Summary
In the prior art, Flash memory is frequently erased, has low space utilization, and low writing efficiency, resulting in low data storage efficiency of the MCU and affecting the service life of the Flash.
The management module obtains the header information of the storage block based on the preset inspection cycle, determines the current valid block, and uses it as a block to be erased when the remaining space is lower than the threshold. Other cleaned storage blocks are used as blocks to be used, update the header information and erase them, give priority to using one storage block for data storage, and replace other storage blocks when space is insufficient to reduce the number of erase times.
The space utilization rate of the non-volatile memory is improved, the service life of the Flash is extended, and the data writing efficiency is improved, thus solving the problem of low data writing efficiency.
Smart Images

Figure CN120669913A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of data storage, and in particular to a data storage method, electronic device, and storage medium. Background Art
[0002] With the continuous advancement of automotive intelligence, smart cockpit systems have become a crucial component of vehicles. The MCU (Microcontroller Unit) is a crucial component of smart cockpit systems, responsible for key functions such as overall system control, power management, and communication. NXP's S32K series microcontrollers are high-performance, low-power MCU chips designed specifically for automotive and industrial applications. They feature large on-chip Flash memory for data storage.
[0003] When writing data to the Flash memory, data can only be written from 1 to 0. This means that the same Flash address cannot be written repeatedly. Erasing changes all data to 1, and the erase unit is the sector. The S32K chip has an 8KB sector size, which means that the entire 8KB of Flash memory must be erased before new data can be written. Based on business needs, the MCU needs to store data in Flash, such as vehicle settings, system settings, and mileage. This data is updated periodically during vehicle operation, requiring frequent storage in Flash.
[0004] In related technologies, the Flash driver module can allocate a sector for each data unit. Before writing, the erase interface needs to be called for erasure, resulting in low space utilization and write efficiency, and the Flash needs to be erased frequently, affecting the service life of the Flash. Summary of the Invention
[0005] In view of the above-mentioned defects or deficiencies in the prior art, the present application aims to provide a data storage method, electronic device and storage medium to solve the problems of frequent memory erasure, low space utilization and low writing efficiency in the related art.
[0006] An embodiment of the present application provides a data storage method, which is applied to a storage management system. The storage management system includes a management module and a non-volatile memory. The non-volatile memory includes multiple storage blocks. The method includes:
[0007] The management module obtains header information of each storage block based on a preset inspection cycle, and determines a currently valid block in each storage block according to the header information, wherein the currently valid block is a storage block for data storage;
[0008] In response to detecting that the remaining storage space of the current valid block is lower than a preset threshold, the management module uses the current valid block as a block to be erased, and uses other erased storage blocks except the block to be erased as blocks to be used;
[0009] The management module updates the header information of the block to be used, erases the data in the block to be erased, and updates the header information of the block to be erased after the erasure is completed.
[0010] Optionally, the management module uses other erased storage blocks except the to-be-erased blocks as to-be-used blocks, including:
[0011] The management module determines whether there is a cleanly erased block in other storage blocks except the block to be erased based on the header information;
[0012] If it exists, the management module uses the erased block as a block to be used; otherwise, the management module determines an initialization block in other storage blocks based on the header information, erases the data in the initialization block, and updates the header information of the initialization block after erasing.
[0013] Optionally, the method further includes:
[0014] In response to receiving the storage event, the management module determines a current valid block in each of the storage blocks based on header information of each of the storage blocks;
[0015] The management module writes the target data corresponding to the storage event into the current valid block.
[0016] Optionally, the management module writes the target data corresponding to the storage event into the current valid block, including:
[0017] The management module obtains storage-related information of the target data, and generates a data header of the target data based on the storage-related information;
[0018] The management module writes the data header into the first blank area and writes the target data into the second blank area;
[0019] Among them, the storage-related information includes data ID, data length and the storage address corresponding to the second blank area, the first blank area is the first blank area from front to back in the current valid block, and the second blank area is the first blank area from back to front in the current valid block.
[0020] Optionally, the storage-related information further includes a validity identifier, and before the management module writes the target data into the second blank area, the following further includes:
[0021] The management module updates the validity flag of the target data to being written;
[0022] After the management module writes the target data into the second blank area, the method further includes:
[0023] The management module updates the validity flag of the target data to writing completed.
[0024] Optionally, the storage management system further includes an interface module and a queue module, and the method further includes:
[0025] The interface module determines whether the target data needs to be written immediately in response to the call of the software module;
[0026] If immediate writing is required, the interface module sends the storage event to the management module through the queue module.
[0027] Optionally, after the interface module determines whether the target data needs to be written immediately, the method further includes:
[0028] If the target data does not need to be written immediately, the interface module stores the target data in the management module and sets a sleep flag for the target data;
[0029] The management module detects a sleep execution instruction, determines a current valid block in each storage block based on header information of each storage block, and writes target data of the sleep flag into the current valid block.
[0030] Optionally, the interface module sends the storage event to the management module through the queue module, including:
[0031] If the interface module detects that multiple target data need to be written immediately at the same time, the interface module sends a storage event corresponding to one of the target data to the management module through the queue module;
[0032] During the process of writing the target data, the storage event corresponding to the next target data is sent to the management module through the queue module until the storage events corresponding to all target data are sent to the management module in sequence.
[0033] An embodiment of the present application further provides an electronic device, comprising:
[0034] processor and memory;
[0035] The processor is used to execute the steps of the data storage method provided in any embodiment of the present application by calling the program or instructions stored in the memory.
[0036] An embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a program or instruction, and the program or instruction enables a computer to execute the steps of the data storage method provided in any embodiment of the present application.
[0037] In summary, the present application proposes a data storage method, which is applied to a storage management system. The management module in the storage management system obtains the header information of each storage block of the non-volatile memory in the storage management system based on a preset inspection cycle, determines the current valid block according to the header information, and the current valid block is a block for storing data. Then, in response to detecting that the remaining storage space of the current valid block is lower than a preset threshold, the management module uses the current valid block as a block to be erased, and uses other erased storage blocks other than the block to be erased as blocks to be used, and then updates the header information of the block to be used, and erases the data in the block to be erased. After erasing is completed, the header information of the block to be erased is updated, so that the block can be replaced in time when the current valid block space is insufficient. Another cleanly erased storage block is used as a new valid block for data storage, and the current valid block with insufficient space is erased to facilitate the next block replacement. This method can greatly reduce the number of erasures of the non-volatile memory by giving priority to using a storage block for data storage, replacing other storage blocks and erasing data when its space is insufficient, thereby ensuring the service life of the memory, and improving the space utilization of the non-volatile memory. There is no need to erase before each data writing, thus solving the problem of low data writing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] In order to more clearly illustrate the specific implementation methods of the present application or the technical solutions in the prior art, the following is a brief introduction to the drawings required for use in the specific implementation methods or the description of the prior art. Obviously, the drawings described below are some implementation methods of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0039] Figure 1 This is a flow chart of a data storage method provided by an embodiment of the present application;
[0040] Figure 2 This is a schematic diagram of generating a storage event provided by an embodiment of the present application;
[0041] Figure 3 It is a data storage structure provided by an embodiment of the present application;
[0042] Figure 4 This is a structural diagram of a data header provided in an embodiment of the present application;
[0043] Figure 5 This is a schematic diagram of updating a validity identification provided in an embodiment of the present application;
[0044] Figure 6 This is a schematic diagram of an interaction of a storage management system provided by an embodiment of the present application;
[0045] Figure 7 This is a flowchart of real-time storage and dormant storage provided by an embodiment of the present application;
[0046] Figure 8 This is a dormant storage process diagram provided by an embodiment of the present application;
[0047] Figure 9 This is a process diagram of multiple software module storage provided by an embodiment of the present application;
[0048] Figure 10 This is a schematic diagram of a block replacement process provided by an embodiment of the present application;
[0049] Figure 11 This is a structural diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0050] The present application will be further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are merely for the purpose of explaining the relevant invention and are not intended to limit the invention. It should also be noted that, for ease of description, only portions relevant to the invention are shown in the accompanying drawings.
[0051] It should be noted that, in the absence of conflict, the embodiments and features of the embodiments in this application can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.
[0052] Before introducing the data storage method provided by the embodiment of the present application in detail, the technical problem solved by the method is first described. In the existing S32K management solution, data reading and writing can be performed by relying on the Flash driver module of the Driver layer or the FEE module of the MCAL layer.
[0053] The Flash driver module is the underlying driver software, providing simple read, write, erase, and status interfaces. When calling the read and write interfaces, the module must input parameters such as the storage read address, data length, and data buff. To prevent overwriting between different data units, each data unit must be allocated an entire sector. Before writing, the erase interface must be called to erase the data. This approach results in low space utilization and write efficiency.
[0054] The FEE module provides Flash management functionality. When calling the read / write interface, only the data ID and data buff need to be entered; no erase operation is required. However, before calling the write interface, it is necessary to confirm that the FEE module is in the idle state. Furthermore, after calling the write interface, data is not written to the Flash immediately; the FEE main function must be polled and executed before the write operation can proceed.
[0055] During actual project development, some important application data needs to be stored in real time, while some data needs to be stored during hibernation. Furthermore, scenarios often arise where multiple data needs to be written simultaneously. Neither of the two existing storage solutions mentioned above can meet the needs of actual project development. The first solution suffers from poor usability, low space utilization, and low write efficiency. The second solution prevents other data from being written while one data item is being written, lacks real-time performance, and lacks hibernation storage functionality.
[0056] Therefore, in order to solve the above problems, an embodiment of the present application provides a data storage method, which is applied to a storage management system. The storage management system includes a management module and a non-volatile memory, and the non-volatile memory includes multiple storage blocks.
[0057] The management module may be a NVM (Non-Volatile Memory Module) module, that is, a software module for managing data and storage services; and the management module may be created based on FreeRTOS (Free Real-Time Operating System, an open source real-time operating system).
[0058] The non-volatile memory may be a Flash memory, and the entire storage space of the non-volatile memory may be divided into a plurality of storage blocks. Specifically, the number of storage blocks to be divided may be determined based on the data storage requirements of the vehicle system. For example, the Flash memory of the S32K may be divided into two storage blocks.
[0059] When storing data, data can be written sequentially in a memory block according to address sequence until the remaining space in the memory block is insufficient. The memory block can then be erased and the data can be written to other memory blocks.
[0060] Figure 1 This is a flow chart of a data storage method provided by an embodiment of the present application. Figure 1 , the data storage method specifically includes:
[0061] S110 , the management module obtains header information of each storage block based on a preset inspection cycle, and determines a currently valid block in each storage block according to the header information, wherein the currently valid block is a storage block used for data storage.
[0062] The preset check period may be a pre-set period for regularly checking the remaining space of the storage block in use. The header information may be used to describe the usage status of the storage block, for example, the header information may be the initialization status, the erase clean status, the block changing status, and the in-use status.
[0063] For example, header information can be set for each storage block, and the length of the header information is 8 bytes. The value of the header information is 0xFFFFFFFF, which indicates the initialized state, that is, unused after leaving the factory; the value of the header information is 0x00FFFFFF, which indicates the erased state, that is, the storage block has been erased and is a clean storage block without any data stored; the value of the header information is 0x0000FFFF, which indicates the block being swapped, that is, the new valid block to which data needs to be written; the value of the header information is 0x000000FF, which indicates the in-use state, that is, the valid block storing data.
[0064] Specifically, the management module can check the remaining space of the storage blocks in use according to a preset check period, that is, determine whether the blocks need to be replaced. If the management module detects that the cumulative check time has reached the preset check period, it can obtain the header information of each storage block and determine the storage block with the header information indicating that it is in use as the currently valid block. The currently valid block is the currently valid block in use, and the management module can check the remaining space of the currently valid block to determine whether the currently used storage block needs to be replaced.
[0065] In the embodiment of the present application, the currently valid block is a storage block used for data storage. For data that the software module needs to write, the management module can write the data into the currently valid block.
[0066] In some embodiments, the method provided in the embodiments of the present application further includes:
[0067] In response to receiving the storage event, the management module determines a current valid block in each storage block based on header information of each storage block; and writes target data corresponding to the storage event into the current valid block.
[0068] The storage event can be an event generated by the software module by calling the interface when there is a need for data storage. Figure 2 This is a schematic diagram of generating a storage event provided by an embodiment of the present application, such as Figure 2 As shown, the software module can call the interface to send the storage request to the queue module, and then the queue module can generate the storage request and send it to the management module, and the management module stores the data in the Flash.
[0069] Specifically, when the management module receives a storage event, it can determine the storage block with the header information indicating that it is in use as the current valid block based on the header information of each storage block, and then write the target data corresponding to the storage event into the current valid block.
[0070] In the embodiments of the present application, considering that in the related art, the old data storage structure needs to reserve an area in front of the Flash, which is used to store the link (i.e., storage address) of each data, used to locate the position of the data in the Flash. Since data will be updated, each data will be written to the Flash multiple times. Therefore, a link area is also reserved after the data in the Flash. When the data is stored again, the address of the new data can be stored in this area. In this way, the old data can be linked to the latest data.
[0071] However, this old storage structure has the following problem: when the data stored in the Flash is damaged due to an accident (such as unexpected power failure or physical damage to the device), the link part of the data cannot be read, and then the storage location of the new data cannot be found, causing the data "link" to be disconnected. Even if the new data may not be damaged, it cannot be read at this time.
[0072] Therefore, in order to solve the above problems, in an embodiment of the present application, a new data storage structure can be set up, in which the data header is written from front to back into the current valid block, and the data body is written from back to front into the current valid block, and the data header stores the address of the data body.
[0073] In one example, the management module writes the target data corresponding to the storage event into the current valid block, including:
[0074] The management module obtains storage-related information of the target data and generates a data header of the target data based on the storage-related information; the management module writes the data header into the first blank area and writes the target data into the second blank area.
[0075] Among them, the storage related information includes data ID, data length and the storage address corresponding to the second blank area. The first blank area is the first blank area from the front to the back in the current valid block, and the second blank area is the first blank area from the back to the front in the current valid block.
[0076] Specifically, the management module can use a pointer to locate the first blank area from the back to the front in the current valid block, and then assign the storage address of the blank area to the target data, and use the storage address and the data ID and data length of the target data as storage related information to generate a data header containing the data ID, data length and the storage address corresponding to the second blank area.
[0077] Among them, the data ID can be that when the software module needs to store data, it applies to the management module through the interface, the software module provides the data length to be stored, and then the management module can assign a unique data ID to the data, which can be used as a data identifier to read and write data later.
[0078] Furthermore, the management module can write the data header of the target data from front to back to the current valid block, that is, write it into the first blank area from front to back in the current valid block, and write the data body of the target data, that is, the target data, from back to front to the current valid block, that is, write it into the first blank area from back to front in the current valid block.
[0079] For example, Figure 3 This is a data storage structure provided by an embodiment of the present application, such as Figure 3 As shown, the header information of the current valid block is located above the storage block, and the data headers of data 1, data 2, data 3, etc. are written into the storage block from top to bottom, and the data bodies of data 1, data 2, data 3, etc. are written into the storage block from bottom to top.
[0080] Through the above example, the data header can be stored in the Flash from front to back, and the data body can be stored in the Flash from back to front. Since the length of the data header is fixed, when reading data from the Flash subsequently, the data header is read according to the fixed address offset each time. If a data header has been read and another data header of the same data is read from a new address, the data header of the new address can be used to replace the data header of the old address. After all data headers have been read, the data body can be read according to the storage address in the data header.
[0081] This data storage structure allows for reading the next data header at a fixed-length offset even when data is accidentally corrupted. This prevents the latest data from being read, reduces the probability of data loss, and saves storage space occupied by the link, improving Flash space utilization. Furthermore, user-unaware storage is achieved, with flexible storage enabled through address management, reducing the number of Flash erases and thus extending the life of the Flash.
[0082] In addition, in the embodiment of the present application, considering that data transmission may fail due to abnormal power failure or the like during the data storage process, and it is impossible to know whether the data has been transmitted after power is restored, a validity flag can also be set in the data header to describe whether the data is complete.
[0083] Optionally, the storage related information further includes a validity flag, and before the management module writes the target data into the second blank area, the method further includes: the management module updates the validity flag of the target data to being written;
[0084] After the management module writes the target data into the second blank area, the method further includes: the management module updates the validity flag of the target data to writing completed.
[0085] The validity flag can describe whether the data is fully transmitted, that is, whether the data is valid. For example, the validity flag can occupy 4 bytes. A validity flag value of 0xFFFFFFFF indicates that the data is empty, that is, the data has not yet been transmitted or stored. A validity flag value of 0x0000FFFF indicates that the data is being written, that is, the data is not fully written. A validity flag value of 0x000000FF indicates that the data is fully written, that is, the data is complete.
[0086] In addition to the validity identifier, the storage-related information may also include a check value of the target data, such as a CRC (Cyclic Redundancy Check) value. For example, Figure 4 This is a structural diagram of a data header provided in an embodiment of the present application. Figure 4 As shown, the data header may include a validity identifier (Status), a data ID (DataID), a data length (DataLen), a check value (DataCRC), and a storage address (Address). The validity identifier is 4 bytes in size, the data ID is 2 bytes in size, the data length is 2 bytes in size, the check value is 4 bytes in size, and the storage address is 4 bytes in size.
[0087] Specifically, the management module may update the validity flag of the target data to "writing in progress" before writing the target data into the second blank area, and update the validity flag of the target data to "writing completed" after writing the target data into the second blank area, ie, after writing is completed.
[0088] For example, Figure 5 This is a schematic diagram of updating a validity identifier provided by an embodiment of the present application, such as Figure 5 As shown, before the management module writes the target data, the validity flag can be updated to writing, and then the management module writes the data and determines whether the write is successful. If not, it returns to continue writing the data. If so, the validity flag is updated to writing completed.
[0089] The above optional implementation method, by setting a validity flag and updating the validity flag during the data transmission process, can achieve the purpose of judging whether the data transmission is complete based on the validity flag. Even if an abnormal situation occurs during the data storage process, if the validity flag of the data is written when the data is read, it means that the data is complete and reliable, ensuring the security and reliability of the data.
[0090] In the embodiment of the present application, considering that if the management module runs periodically to store data, it may cause the management module to erase the Flash more frequently, therefore, the embodiment of the present application can adopt a triggered operation method to support real-time storage and dormant storage, reduce the frequency of erasing the Flash and extend the service life of the Flash.
[0091] In one example, the storage management system further includes an interface module and a queue module. The method provided in the embodiment of the present application further includes:
[0092] The interface module responds to the call of the software module and determines whether the target data needs to be written immediately; if it needs to be written immediately, the interface module sends a storage event to the management module through the queue module.
[0093] Specifically, when the software module needs to write data, it can call the interface module, and then the interface module determines whether the target data needs to be written immediately. If so, the interface module sends a request to the queue module to enable the queue module to generate a storage event and send it to the management module. Then, the management module immediately executes the data writing process after receiving the storage event.
[0094] Exemplarily, before writing data, the software module may provide a storage mode parameter indicating whether the data needs to be stored in real time. The interface module may determine whether the target data needs to be written immediately based on the storage mode parameter.
[0095] Figure 6 This is an interactive diagram of a storage management system provided by an embodiment of the present application, such as Figure 6 As shown, software modules 1-4 can communicate with the interface module, sending data storage requests to the interface module. The interface module can then communicate with the management module, invoking the queue module to send storage events to the management module. The management module can also communicate with the Flash driver, the underlying driver software used to control Flash. The management module calls the Flash driver to read and write data.
[0096] Through the above method, the queue mechanism can be used to trigger data storage. Different from executing data writing in a fixed period, in the embodiment of the present application, whenever the user calls the interface module, the interface module determines whether it needs to be written immediately. If it is necessary, the queue module sends a storage event to inform the management module that there is a storage task. After receiving the storage event, the management module immediately executes the relevant process, realizing real-time storage of data, improving the real-time performance of data storage, and realizing the function of real-time response storage.
[0097] In addition, in an embodiment of the present application, if the interface module determines that the target data does not need to be written immediately, the target data can be temporarily stored in the management module using a dormant write method, and the target data can be written into the current valid block when the management module receives the dormant notification.
[0098] Optionally, after the interface module determines whether the target data needs to be written immediately, the method provided in the embodiment of the present application further includes:
[0099] If the target data does not need to be written immediately, the interface module stores the target data to the management module and sets a sleep flag for the target data; the management module detects the sleep execution instruction, determines the current valid block in each storage block based on the header information of each storage block, and writes the target data with the sleep flag to the current valid block.
[0100] Specifically, if the interface module determines that the target data does not need to be written immediately, it can temporarily store it in the management module and set a sleep flag for it. Furthermore, if the management module detects a sleep execution instruction issued by a system (such as a vehicle computer system), it can first determine the current valid block based on the header information and then write the target data with the sleep flag to the current valid block.
[0101] For example, Figure 7 This is a flowchart of real-time storage and dormant storage provided by an embodiment of the present application, such as Figure 7 As shown, the software module can call the write data interface provided by the interface module and send a storage request, and then the interface module can make a parameter validity judgment, that is, verify the validity of the data. If the verification fails, the result can be returned to the software module. If the verification passes, it can be determined whether to write the data immediately.
[0102] If immediate writing is not required, the data is temporarily stored and the sleep flag is set. If immediate writing is required, a storage event is sent to the management module, which then executes the data write process. After the management module completes the write, it calls a callback function to notify the software module of the write result. When the system detects a sleep request, it sends a sleep execution instruction to the management module, which then writes all sleep-flagged data and sends a write completion notification to the system after the write is complete, allowing the system to continue the sleep process.
[0103] Figure 8 This is a dormant storage process diagram provided by an embodiment of the present application, such as Figure 8 As shown, when the system detects a sleep request, it can send a sleep execution instruction to the management module. The management module then determines whether there is data with a sleep flag. If not, it directly notifies the system that the sleep data write is complete. If so, it writes all data with the sleep flag and notifies the system that the sleep data write is complete. Further, after receiving the sleep data write completion notification, the system can continue to execute the sleep process.
[0104] Through the above optional implementation method, hibernation storage of data can be realized. For data with low real-time storage requirements, it can be written through pre-hibernation storage to avoid immediately writing data with low real-time requirements to occupy system memory, thereby ensuring the writing stability and writing efficiency of data with high real-time requirements.
[0105] In the embodiment of the present application, considering that the write function call of the interface is to run the code, which takes a shorter time, while writing data requires waiting for the Flash action to be completed, which takes a longer time, if there are multiple software modules calling the interface for data storage at the same time, if a storage event is generated for each software module in turn and waits for Flash to write, it may lead to low data storage efficiency. Therefore, in order to improve data storage efficiency, for such situations, the data writing efficiency can also be improved through an asynchronous execution mechanism, and the storage event of the next data is sent during the writing of the previous data.
[0106] Optionally, the interface module sends a storage event to the management module through the queue module, including:
[0107] If the interface module detects that multiple target data need to be written immediately at the same time, the storage event corresponding to one of the target data is sent to the management module through the queue module; during the process of writing the target data, the storage event corresponding to the next target data is sent to the management module through the queue module until the storage events corresponding to all target data are sent to the management module in sequence.
[0108] Specifically, if the interface module detects that multiple software modules need to write data in real time at the same time, it can first send a storage event corresponding to one of the target data to the management module through the queue module, and then, during the writing of the target data, call the queue module to send the storage event corresponding to the next target data to the management module, and so on, until the storage events corresponding to all target data are sent to the management module in sequence.
[0109] Among them, the queue module can also set up an event caching mechanism. The interface module can call the queue module in sequence to generate storage events corresponding to each target data, and then the queue module can cache all storage events. In the process of the management module writing a target data, the interface module can read the next storage event from the cached storage event through the queue module and send it to the management module.
[0110] Figure 9 This is a process diagram of a multi-software module storage provided by an embodiment of the present application, such as Figure 9 As shown, software module A can call the write data interface provided by the interface module. The interface module can perform parameter validity judgment, that is, verify the validity of the data. If the verification fails, it can return the result to software module A. If the verification passes, it can determine whether to write the data immediately. If it does not need to be written immediately, the data is temporarily stored and the sleep flag is set. If it is necessary to write immediately, a storage event is sent to the management module, and the result is returned to software module A through the interface after the writing is completed.
[0111] During this process, software module B can also call the write data interface provided by the interface module. The interface module can perform parameter validity judgment, that is, verify the legitimacy of the data. If the verification fails, it can return the result to software module B. If the verification passes, it can determine whether to write the data immediately. If it does not need to be written immediately, the data is temporarily stored and the sleep flag is set. If it is necessary to write immediately, a storage event is sent to the management module, and the result is returned to software module B through the interface after the write is completed.
[0112] If both software module A and software module B need to write data immediately, the interface module can cache the storage event in the queue module, and send the storage event of the next data to the management module through the queue module during the process of writing one data.
[0113] Through the above optional implementation, the next write action can be executed immediately after the management module completes the previous write action, without waiting for the Flash action to be completed and then calling the interface to generate a storage event, which can reduce waiting time and thus improve data writing efficiency.
[0114] S120 , in response to detecting that the remaining storage space of the current valid block is lower than a preset threshold, the management module uses the current valid block as a block to be erased, and uses other erased storage blocks except the block to be erased as blocks to be used.
[0115] Specifically, when the management module performs a block replacement check on the storage blocks in use according to a preset check period, it may first determine the storage block in use, ie, the current valid block, according to the header information of each storage block.
[0116] After determining the current valid block, it is further determined whether the remaining storage space of the current valid block is less than a preset threshold. The preset threshold may be determined based on the size of data generated by each controller in the vehicle, for example, the preset threshold may be the occupied storage space corresponding to the largest data in each controller.
[0117] Specifically, if the remaining storage space is higher than a preset threshold, it means that the current valid block can still be used. If the remaining storage space is lower than the preset threshold, it means that the non-volatile memory needs to be swapped, that is, the storage block in use needs to be replaced.
[0118] In an embodiment of the present application, if the remaining storage space of the current valid block is lower than a preset threshold, the management module can use the current valid block as a block to be erased, and select a cleanly erased storage block from other storage blocks except the block to be erased as a block to be used.
[0119] In a specific implementation, the management module uses other erased storage blocks except the blocks to be erased as blocks to be used, including:
[0120] Based on the header information, the management module determines whether there is a clean block in other storage blocks except the block to be erased; if so, the management module uses the clean block as the block to be used; otherwise, the management module determines the initialization block in other storage blocks based on the header information, erases the data in the initialization block, and updates the header information of the initialization block after the erasure is completed.
[0121] Specifically, the management module can determine whether there is a storage block with header information in the erased state based on the header information of other storage blocks except the block to be erased. If so, it can be used as an erased block, and then the erased block can be used as a block to be used.
[0122] If there is no erased block, it can be determined based on the header information of other storage blocks whether there is a storage block with header information in the initialized state. If so, it can be used as the initialization block and the data in the initialization block can be erased. After the erasure is completed, the header information of the initialization block is updated to the erased state, so that the storage block can be selected as the block to be used later.
[0123] Through the above implementation, the management module can first determine whether there is a cleanly erased storage block. If not, it can select the initialization block for erasure, so that it can be used as a block to be used later, realizing the block replacement process.
[0124] S130: The management module updates the header information of the block to be used and erases the data in the block to be erased. After the erasure is completed, the management module updates the header information of the block to be erased.
[0125] Specifically, after the management module determines the block to be used, if there is data to be written at this time, the header information of the block to be used can be updated to the block changing state first. After the data is written, the header information of the block to be used can be updated to the in-use state. If there is no data to be written at this time, the header information of the block to be used can be directly updated to the in-use state.
[0126] After updating the header information of the block to be used, the block to be used can be used as the latest current valid block for data storage. In addition to the header information of the block to be used, the data in the block to be erased needs to be erased. After the erasure is completed, the header information of the block to be erased is updated to the erased clean state so that it can be used as the block to be used the next time the block is replaced.
[0127] For example, Figure 10 This is a schematic diagram of a block replacement process provided by an embodiment of the present application, such as Figure 10 As shown, for example, the non-volatile memory is divided into two storage blocks (BlockA and BlockB). Each storage block is in the factory initialized state, i.e., 0xFFFFFFFF. When the vehicle is first powered on, the management module cannot find a currently valid block and sets BlockA's header information to the in-use state, i.e., 0x000000FF. Furthermore, when the management module detects that BlockA has insufficient remaining space, it performs a block swap check to determine whether BlockB has been completely erased. If BlockB is not completely erased, it is erased and the header information is updated to the clear state, i.e., 0x00FFFFFF.
[0128] Furthermore, after BlockB is erased, the management module may set the header information of BlockB to be in use, i.e., 0x000000FF, to set BlockB as a valid block, and erase BlockA. After erasing, the header information of BlockA is updated to be erased clean, i.e., 0x00FFFFFF.
[0129] The data storage method provided in an embodiment of the present application is applied to a storage management system. A management module in the storage management system obtains header information of each storage block of a non-volatile memory in the storage management system based on a preset check cycle, determines a current valid block based on the header information, and the current valid block is a block used to store data. Then, in response to detecting that the remaining storage space of the current valid block is lower than a preset threshold, the management module sets the current valid block as a block to be erased, and sets other erased storage blocks other than the block to be erased as blocks to be used, and then updates the header information of the block to be used, and erases the data in the block to be erased. After erasing is completed, the header information of the block to be erased is updated, so that the block can be replaced in time when the current valid block space is insufficient. Another cleanly erased storage block is used as a new valid block for data storage, and the current valid block with insufficient space is erased to facilitate the next block replacement. This method can greatly reduce the number of erasures of the non-volatile memory by giving priority to using one storage block for data storage and replacing other storage blocks and erasing data when its space is insufficient, thereby ensuring the service life of the memory and improving the space utilization of the non-volatile memory. There is no need to erase before each data writing, which solves the problem of low data writing efficiency.
[0130] Figure 11 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 11 As shown, the electronic device 400 includes one or more processors 401 and a memory 402 .
[0131] The processor 401 may be a central processing unit (CPU) or other forms of processing units having data processing capabilities and / or instruction execution capabilities, and may control other components in the electronic device 400 to perform desired functions.
[0132] The memory 402 may include one or more computer program products, which may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory may, for example, include random access memory (RAM) and / or cache memory (cache), etc. The non-volatile memory may, for example, include read-only memory (ROM), a hard disk, a flash memory, etc. One or more computer program instructions may be stored on the computer-readable storage medium, and the processor 401 may run the program instructions to implement the data storage method and / or other desired functions of any embodiment of the present application described above. Various contents such as initial external parameters, thresholds, etc. may also be stored in the computer-readable storage medium.
[0133] In one example, electronic device 400 may further include an input device 403 and an output device 404, which are interconnected via a bus system and / or other connection mechanisms (not shown). Input device 403 may include, for example, a keyboard, a mouse, etc. Output device 404 may output various information to the outside, including warning information, braking force, etc. Output device 404 may include, for example, a display, a speaker, a printer, a communication network, and remote output devices connected thereto.
[0134] Of course, to simplify, Figure 11 Only some of the components related to the present application in the electronic device 400 are shown, and components such as buses, input / output interfaces, etc. are omitted. In addition, the electronic device 400 may further include any other appropriate components according to specific application scenarios.
[0135] In addition to the above methods and devices, an embodiment of the present application may also be a computer program product, which includes computer program instructions, which, when executed by a processor, enable the processor to perform the steps of the data storage method provided by any embodiment of the present application.
[0136] The computer program product may be written in any combination of one or more programming languages to implement the program code for performing the operations of the embodiments of the present application, including object-oriented programming languages such as Java, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computing device, partially on the user's computing device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.
[0137] In addition, an embodiment of the present application may also be a computer-readable storage medium having computer program instructions stored thereon, which, when executed by a processor, causes the processor to execute the steps of the data storage method provided by any embodiment of the present application.
[0138] The computer-readable storage medium can adopt any combination of one or more readable media. The readable medium can be a readable signal medium or a readable storage medium. The readable storage medium can, for example, include but is not limited to a system, device or component of electricity, magnetism, light, electromagnetic, infrared, or semiconductor, or any combination thereof. More specific examples (non-exhaustive list) of readable storage media include: an electrical connection with one or more wires, a portable 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 thereof.
[0139] It should be noted that the terms used in this application are only for describing specific embodiments and are not intended to limit the scope of this application. As shown in the specification and claims of this application, unless the context clearly indicates an exception, the words "one", "an", "a kind of" and / or "the" do not specifically refer to the singular and may also include the plural. The terms "comprise", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method or device. In the absence of further restrictions, the elements defined by the sentence "comprise a..." do not exclude the presence of other identical elements in the process, method or device comprising the elements.
[0140] It should also be noted that the terms "center", "up", "down", "left", "right", "vertical", "horizontal", "inside", "outside", etc., indicating orientations or positional relationships, are based on the orientations or positional relationships shown in the accompanying drawings, and are only for the convenience of describing this application and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as a limitation on this application. Unless otherwise clearly specified and limited, the terms "installed", "connected", "connected", etc. should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection, or an indirect connection through an intermediate medium, or it can be a communication between the internal parts of two elements. For those of ordinary skill in the art, the specific meanings of the above terms in this application can be understood according to specific circumstances.
[0141] This article uses specific examples to illustrate the principles and implementation methods of this application. The description of the above embodiments is only used to help understand the method and core ideas of this application. The above is only the preferred implementation method of this application. It should be pointed out that due to the limitations of textual expression, there are objectively infinite specific structures. For ordinary technicians in this technical field, without departing from the principles of this application, they can also make several improvements, modifications or changes, and can also combine the above technical features in an appropriate manner; these improvements, modifications, changes or combinations, or the direct application of the inventive concept and technical solution to other occasions without improvement, should be regarded as the scope of protection of this application.
Claims
1. A data storage method, characterized in that: Applied to a storage management system, the storage management system includes a management module and a non-volatile memory, the non-volatile memory includes a plurality of storage blocks, and the method includes: The management module obtains header information of each storage block based on a preset inspection cycle, and determines a currently valid block in each storage block according to the header information, wherein the currently valid block is a storage block for data storage; In response to detecting that the remaining storage space of the current valid block is lower than a preset threshold, the management module uses the current valid block as a block to be erased, and uses other erased storage blocks except the block to be erased as blocks to be used; The management module updates the header information of the block to be used, erases the data in the block to be erased, and updates the header information of the block to be erased after the erasure is completed.
2. The data storage method according to claim 1, wherein: The management module uses other erased storage blocks except the to-be-erased blocks as to-be-used blocks, including: The management module determines whether there is a cleanly erased block in other storage blocks except the block to be erased based on the header information; If it exists, the management module uses the erased block as a block to be used; otherwise, the management module determines an initialization block in other storage blocks based on the header information, erases the data in the initialization block, and updates the header information of the initialization block after erasing.
3. The data storage method according to claim 1, wherein: The method further comprises: In response to receiving the storage event, the management module determines a current valid block in each of the storage blocks based on header information of each of the storage blocks; The management module writes the target data corresponding to the storage event into the current valid block.
4. The data storage method according to claim 3, characterized in that: The management module writes the target data corresponding to the storage event into the current valid block, including: The management module obtains storage-related information of the target data, and generates a data header of the target data based on the storage-related information; The management module writes the data header into the first blank area and writes the target data into the second blank area; Among them, the storage-related information includes data ID, data length and the storage address corresponding to the second blank area, the first blank area is the first blank area from front to back in the current valid block, and the second blank area is the first blank area from back to front in the current valid block.
5. The data storage method according to claim 4, characterized in that: The storage related information further includes a validity identifier, and before the management module writes the target data into the second blank area, further includes: The management module updates the validity flag of the target data to being written; After the management module writes the target data into the second blank area, the method further includes: The management module updates the validity flag of the target data to writing completed.
6. The data storage method according to claim 3, characterized in that: The storage management system further includes an interface module and a queue module, and the method further includes: The interface module determines whether the target data needs to be written immediately in response to the call of the software module; If immediate writing is required, the interface module sends the storage event to the management module through the queue module.
7. The data storage method according to claim 6, characterized in that: After the interface module determines whether the target data needs to be written immediately, the method further includes: If the target data does not need to be written immediately, the interface module stores the target data in the management module and sets a sleep flag for the target data; The management module detects a sleep execution instruction, determines a current valid block in each storage block based on header information of each storage block, and writes target data of the sleep flag into the current valid block.
8. The data storage method according to claim 6, characterized in that: The interface module sends the storage event to the management module through the queue module, including: If the interface module detects that multiple target data need to be written immediately at the same time, the interface module sends a storage event corresponding to one of the target data to the management module through the queue module; During the process of writing the target data, the storage event corresponding to the next target data is sent to the management module through the queue module until the storage events corresponding to all target data are sent to the management module in sequence.
9. An electronic device, characterized in that: The electronic device comprises: processor and memory; The processor is configured to execute the steps of the data storage method according to any one of claims 1 to 8 by calling the program or instruction stored in the memory.
10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a program or instruction, which enables a computer to execute the steps of the data storage method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Service life early warning method and apparatus of NAND Flash memory
CN106297898A
Data read-write method and system of Flash memory, medium and equipment
CN114637469A
Non-volatile memory, recording apparatus and recording method
CN1243317A