Data storage method, readable medium, and electronic device

By predicting the amount of flash memory data and free capacity before the system is shut down and switching sectors in advance, the problem of increasing time caused by data synchronization during system shutdown is solved, and a more efficient data storage process is achieved.

CN117806565BActive Publication Date: 2025-08-01欧摩威软件系统开发(重庆)有限公司
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202410021303.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-01-05
Publication Date
2025-08-01
Estimated Expiration
2044-01-05

AI Technical Summary

Technical Problem

In the electronic control unit, the system shutdown time increases due to insufficient sector space when data is synchronized to flash memory.

Method used

Before the system is shut down, count the total amount of data to be written into the flash memory and the current sector idle capacity, and determine and switch sectors in advance to avoid sector handover during the system shutdown stage.

Benefits of technology

Avoid increasing system shutdown time, reducing storage space waste, and optimizing the system shutdown process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117806565B_ABST
    Figure CN117806565B_ABST
Patent Text Reader

Abstract

The present application relates to the technical field of data processing, and discloses a data storage method, medium and electronic device, which can pre-confirm whether the data of multiple NvM blocks to be written into the flash memory will trigger a sector switch of the flash memory and switch sectors in advance to avoid sector switching during the system shutdown phase and avoid increasing the system shutdown time. The method includes: obtaining the total amount of data corresponding to multiple NvM blocks to be written into the flash memory, and obtaining the free capacity of the first sector currently used in the flash memory, where the data in the multiple NvM blocks is the data in the random access memory; corresponding to the total amount of data corresponding to the multiple NvM blocks being greater than the free capacity of the first sector, switching the currently used sector in the flash memory from the first sector to the second sector; corresponding to detecting a system shutdown instruction for the electronic device, when a synchronous write instruction is detected, sequentially storing each data block in the multiple NvM blocks into multiple Nv blocks in the second sector.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the technical field of data processing, and particularly to a data storage method, a readable medium, and an electronic device. Background Art

[0002] An electronic control unit (ECU) usually includes common types of memories such as a random access memory (RAM) and a non-volatile memory (NVM), and the NVM can be a flash memory or an electrically erasable programmable read-only memory (EEPROM). For example, the NVM can be a flash memory in the FEE mode, where FEE refers to Flash Emulating EEPROM, that is, Flash EEPROM Emulation. Usually, during the shutdown phase of the AUTOSAR system, the NvM_WriteAll function can be called to synchronize the data of the application program (APP) in the RAM to the NVM such as the flash memory.

[0003] Refer to Figure 1 As shown, it is a schematic diagram of a data storage architecture related to FEE. This architecture includes a non-volatile memory manager (NVRAM Manager), a RAM, and a flash memory. AUTOSAR stipulates that the application program (APP) can only access non-volatile memories such as the flash memory through the NVRAM manager. It can be understood that the data stored in the flash memory can be called Nv Data (i.e., non-volatile data, also called Nv data), and the Nv data can be stored as non-volatile storage blocks (Nv Blocks, i.e., Nv blocks). Moreover, the structure for managing and storing non-volatile data can be called NvRAM Block (NvM Block or NvM block). An NvM block enabled with the Write All attribute defines a RAM block in the RAM. And, a RAM block corresponds to an Nv block, and when the NvM_WriteAll function is called, the Nv block is written into the flash memory. Among them, the valid data of the RAM block defined by the NvM block enabled with the Write All attribute and information such as the corresponding check data all need to be synchronized to the flash memory when the NvM_WriteAll function is called. At this time, an Nv block can include the valid data corresponding to the RAM and the corresponding check data, etc. In addition, the flash memory in the FEE mode usually includes multiple sectors, and each sector can store multiple Nv blocks.

[0004] Then, when synchronizing the data of the RAM block from the RAM to the flash memory, if there is not enough storage space in the current sector, a sector switch needs to be triggered. After the switch, these data are stored in the other sector. Thus, in the shutdown phase of the system, if the amount of Nv data to be stored in the flash memory is large and there is not enough storage space in the current sector, it will cause a sector switch, thereby increasing the system shutdown time. Summary of the Invention

[0005] Embodiments of the present application provide a data storage method, medium, and electronic device, which can avoid increasing the system shutdown time.

[0006] In a first aspect, an embodiment of the present application provides a data storage method, which is characterized in that it is applied to an electronic device. The electronic device includes a random access memory and a flash memory. The method includes: obtaining the total amount of data of a plurality of data blocks to be written into the flash memory (such as NvM blocks, that is, RAM blocks), and obtaining the free capacity of the first sector currently used in the flash memory. The data in the plurality of data blocks to be written is the data in the random access memory (RAM); corresponding to the total amount of data of the plurality of data blocks to be written being greater than the free capacity of the first sector, switching the currently used sector in the flash memory from the first sector to the second sector; corresponding to detecting a system shutdown instruction (such as a shutdown instruction) for the electronic device, storing each data block in the plurality of data blocks to be written into the second sector in sequence.

[0007] The present application can count the total amount of data of a plurality of NvM blocks (that is, a plurality of data blocks to be written) to be stored before the system shutdown phase, and count the free capacity corresponding to the remaining storage space of the current sector (that is, the first sector) in the flash memory, so as to determine whether the total amount of data of the plurality of NvM blocks is greater than the free capacity. If the total amount of data of the plurality of NvM blocks is greater than the free capacity, the sector is switched, so that when entering the system shutdown phase, the data of the plurality of NvM blocks can be directly stored in the switched sector. Thus, there is no need to switch sectors in the system shutdown phase, avoiding increasing the system shutdown time.

[0008] In a possible implementation of the above first aspect, the flash memory is in the flash emulation electrically erasable programmable read-only memory FEE mode. It can be understood that in the FEE mode, the flash memory can include multiple sectors, and each sector includes multiple Nv blocks to support synchronizing the data in the RAM block to the sector of the flash memory before the system shutdown.

[0009] In a possible implementation of the first aspect above, the data block to be written has a preset write attribute, and the usage status of the data block to be written is a preset usage status; wherein, the preset write attribute is used to indicate that when the data block is written to the flash memory, all fields or attributes of the data block are written to the flash memory; the usage status of the data block is used to indicate whether the data in the data block is allowed to be written to the flash memory, and the preset usage status is used to indicate that the data in the data block is allowed to be written to the flash memory. Among them, the preset write attribute may be the WriteAll attribute. The usage status of the RAM block can be set by the NvM_SetRamBlockStatus(BlockId,Status) function. Then, the preset usage status of the RAM block indicates that the flag (block.valid flag) for setting the usage status by the NvM_SetRamBlockStatus(BlockId,Status) function is TRUE. That is, for a RAM block with the WriteAll attribute enabled, the electronic device can use the function NvM_SetRamBlockStatus(BlockId,Status) to mark that the RAM block needs to be written to the flash memory during the NvM_WriteAll process, and at the same time, the corresponding block.valid flag will be set to TRUE.

[0010] In a possible implementation of the first aspect above, corresponding to detecting a system shutdown instruction for the electronic device, storing each data block in the plurality of data blocks to be written to the second sector in sequence, includes: corresponding to detecting a system shutdown instruction for the electronic device, when detecting a synchronous write instruction (an instruction to call the NvM_WriteAll function) for the plurality of data blocks to be written, storing each data block in the plurality of data blocks to be written to the second sector in sequence, wherein the synchronous write instruction is used to write a data block with a preset write attribute and a preset usage status to the flash memory.

[0011] In a possible implementation of the first aspect above, obtaining the total data volume of multiple data blocks to be written to the flash memory and obtaining the free capacity of the first sector currently in use in the flash memory includes: detecting a preprocessing instruction for the system shutdown of the electronic device; counting multiple data blocks to be processed with a preset usage status in the random access memory; obtaining multiple data blocks to be written based on the multiple data blocks to be processed, where one data block to be written includes the valid data of one data block to be processed, as well as the corresponding check data and storage control data; calculating the data volume of the multiple data blocks to be written and calculating the free capacity of the first sector. In this way, the data storage method provided in this application can, before the system shutdown stage of the electronic device, uniformly count the total data volume corresponding to the RAM blocks whose block.valid flag to be written to the flash memory is set to TURE, and count the free capacity of the current sector in the flash memory, so as to ensure that it can be determined in time before the system shuts down whether the RAM to be written to the flash memory needs to trigger sector switching.

[0012] In a possible implementation of the first aspect above, obtaining the total data volume of multiple data blocks to be written to the flash memory and obtaining the free capacity of the first sector currently in use in the flash memory includes: detecting an immediate write instruction (such as an instruction to call the NvM_WriteBlock() function), where the data volume of the data block indicated by the immediate write instruction is less than or equal to the free capacity of the flash memory, storing the data block indicated by the immediate write instruction in the first sector, and subtracting the data volume of the data block indicated by the immediate write instruction from the free capacity of the first sector to update the free capacity of the first sector, where the data block indicated by the immediate write instruction is different from the multiple data blocks to be written, and the immediate write instruction is used to immediately write the valid data of the indicated data block to the flash memory; detecting a status setting instruction, setting the data block indicated by the status setting instruction to the preset usage status, and adding the data volume corresponding to the data block indicated by the status setting instruction to the total data volume of the multiple data blocks to be written to update the total data volume of the multiple data blocks to be written, where the data volume corresponding to the data block indicated by the status setting instruction includes the data volume of the valid data of the data block, as well as the corresponding check data and storage control data. In this way, the data storage method provided in this application can, during the operation of the electronic device, count in real time the total data volume corresponding to the RAM blocks whose block.valid flag to be written to the flash memory is set to True, and count in real time the free capacity of the current sector in the flash memory, so as to ensure that it can be determined in time before the system shuts down whether the RAM to be written to the flash memory needs to trigger sector switching.

[0013] In a possible implementation of the first aspect above, the method further includes: when detecting an immediate write instruction and the data volume of the data block indicated by the immediate write instruction is greater than the free capacity of the flash memory, switching the currently used sector in the flash memory from the first sector to the second sector, and storing the data block indicated by the immediate write instruction in the second sector. That is, during the operation of the electronic device, data in the RAM block can be written into the flash memory. For example, the NvM_WriteBlock() function is called to write data in the RAM into the currently active sector with storage space.

[0014] In a possible implementation of the first aspect above, the method further includes: corresponding to detecting a system shutdown instruction for the electronic device, when detecting a synchronous write instruction for multiple data blocks to be written, and when the total data volume of the multiple data blocks to be written is less than or equal to the free capacity of the first sector, storing each data block in the multiple data blocks to be written into the first sector in sequence. That is, during the system shutdown phase of the electronic device, data in the RAM block with a preset usage state can be written into the flash memory. For example, the NvM_WriteAll() function is called to write this data in the RAM into the currently active sector with storage space.

[0015] In a possible implementation of the first aspect above, the system of the electronic device is an AUTOSAR system. At this time, the electronic device can be an in-vehicle electronic device, such as a vehicle.

[0016] In a second aspect, an embodiment of the present application provides a readable medium, on which instructions are stored. When the instructions are executed on an electronic device, the electronic device is caused to execute the data storage method in the first aspect and any of its possible implementation manners.

[0017] In a third aspect, an embodiment of the present application provides an electronic device, including: a memory for storing instructions executed by one or more processors of the electronic device, and a processor, which is one of the processors of the electronic device, for executing the data storage method in the first aspect and any of its possible implementation manners. Description of the Drawings

[0018] Figure 1 According to some embodiments of the present application, a schematic diagram of a data storage architecture related to FEE is shown;

[0019] Figure 2A According to some embodiments of the present application, a schematic diagram of a data storage process based on MvM blocks is shown;

[0020] Figure 2B According to some embodiments of the present application, a schematic diagram of a data storage process based on MvM blocks is shown;

[0021] Figure 3According to some embodiments of the present application, a schematic diagram of a data storage process based on NvM blocks is shown;

[0022] Figure 4 According to some embodiments of the present application, a schematic diagram of a data storage method is shown;

[0023] Figure 5 According to some embodiments of the present application, a schematic diagram of a data storage method based on the first statistical method is shown;

[0024] Figure 6 According to some embodiments of the present application, a schematic diagram of a function call process for sector switching is shown;

[0025] Figure 7 According to some embodiments of the present application, a schematic diagram of a data storage method based on the second statistical method is shown;

[0026] Figure 8 According to some embodiments of the present application, a schematic diagram of a function call process for sector switching is shown;

[0027] Figure 9 According to some embodiments of the present application, a schematic diagram of the structure of a vehicle is shown. Detailed implementation manners

[0028] Illustrative embodiments of the present application include, but are not limited to, data storage methods, media, and electronic devices.

[0029] As described in the background art, during the shutdown phase of the operating system of an electronic device, if a sector switch occurs in the flash memory in FEE mode, the system shutdown time will increase.

[0030] In a possible implementation manner, during the system shutdown phase of the electronic device, sector switch judgments can be made for each NvM block one by one, so as to store data in the flash memory in FEE mode for each NvM block one by one. Specifically, when the amount of data of an NvM block to be written to the flash memory (denoted as FEE block size) is greater than the free capacity corresponding to the remaining storage space in the current sector of the flash memory (denoted as free size), sector switching starts. Refer to Figure 2A and Figure 2B , a schematic diagram of a data storage process based on MvM blocks is shown. As Figure 2AAs shown, during the system shutdown phase, the data in NvM blocks 6, 5... 1 needs to be synchronized to the flash memory in sequence. If the data volume of NvM block 6, FEE block size1, is less than the free capacity free size1 of the current active sector 1 in the flash memory, the data in NvM block 6 can be stored in an Nv block in sector 1. At this time, the free capacity free size1 is obtained by subtracting the capacity of the used Nv blocks 1 - 10 from the total capacity of sector 1. As Figure 2B shown, assume that NvM block 6 has been stored in sector 1 and forms Nv block 11, and continue to judge the data volume of block 5. If the data volume of NvM block 5, FEE block size2, is greater than the free capacity free size2 of the current active sector 1 in the flash memory, then sector 1 needs to be switched to sector 2 to write the data of NvM block 5 in an Nv block in sector 2, and the data of NvM blocks 4... 1 written in sector 2 subsequently. At this time, the free capacity free size2 is obtained by subtracting the capacity of Nv blocks 1 - 11 from the total capacity of sector 1. In this way, when there are data storage requirements for multiple NvM blocks during the system shutdown phase and these data are to be stored in the flash memory, it is possible that the data of some NvM blocks among these NvM blocks are stored in the current sector, while the remaining storage space in the current sector is not sufficient to store the data of the next NvM block, which will cause sector switching, thereby increasing the system shutdown time.

[0031] In another possible implementation, to avoid sector switching during the system shutdown phase, two thresholds, such as THRESHOLD1 and THRESHOLD2, can be set, and THRESHOLD1 > THRESHOLD2. Then, during the system shutdown phase, when writing the data of an NvM block to the flash memory, if the free capacity corresponding to the remaining storage space of the current sector is less than THRESHOLD1, a background page switch of the sector is triggered. If the free capacity corresponding to the remaining storage space of the current sector is less than THRESHOLD2, an immediate page switch of the forced sector is triggered. Among them, the Backgroud page switch process can be interrupted by a new read / write operation and continue to move data after completing other read / write operations. The immediate page switch cannot be interrupted until the page switch is completed, and then other read / write operations can be executed. In this way, when the threshold is set relatively large, it is beneficial to avoid sector switching during the system shutdown phase, thereby avoiding increasing the system shutdown time. Also, performing a background page switch on the sector when the free capacity corresponding to the remaining storage space of the current sector is less than THRESHOLD1 is beneficial to prioritize NvM operations such as other read / write operations during system shutdown, thereby helping to reduce the system shutdown time. However, if the threshold is set too small, it will not be possible to avoid sector switching during the system shutdown phase, especially when storing the data of multiple NvM blocks to the flash memory during the system shutdown phase. If the threshold is set too large, sector switching will occur when the free capacity of the remaining storage space of the current sector is relatively large, resulting in a large waste of storage space during the sector switching process.

[0032] To avoid sector switching during the system shutdown phase due to too small a threshold setting and to avoid large storage resource waste during the sector switching process due to too large a threshold setting, the embodiment of the present application provides a data storage method. Before the system shutdown phase, such as during the pre_shutdown stage of system shutdown, it can be pre-determined whether the data of multiple NvM blocks to be stored will trigger sector switching of the flash memory and the sectors can be switched in advance to avoid sector switching during the system shutdown phase. Specifically, the present application can count the total amount of data of multiple NvM blocks to be stored before the system shutdown phase and count the free capacity corresponding to the remaining storage space of the current sector in the flash memory to determine whether the total amount of data of the multiple NvM blocks is greater than the free capacity. If the total amount of data of the multiple NvM blocks is greater than the free capacity, the sector is switched, so that when entering the system shutdown phase, the data of the multiple NvM blocks can be directly stored in the switched sector, thus avoiding sector switching during the system shutdown phase and avoiding increasing the system shutdown time.

[0033] Thus, the above solution can confirm in advance whether writing data to the flash memory during the system shutdown phase will trigger sector switching. Compared with the method of setting a threshold, this solution will not cause waste of storage space due to a large threshold, nor will it fail to avoid sector switching during system shutdown due to a small threshold setting.

[0034] Referring to Figure 3 As shown, it is a schematic diagram of the data storage process of an NvM block provided by an embodiment of the present application. As Figure 3 shown, during the operation of the system of the electronic device, the total data volume of NvM blocks 1-6 to be written to the flash memory can be counted (denoted as NvM_WriteAll total size), and the free capacity corresponding to the remaining storage space of the currently active sector 1 in the flash memory can be counted (denoted as section free size, or free size). At this time, the free capacity free size is obtained by subtracting the capacity of Nv blocks 1-10 from the total capacity of sector 1. Then, if free size is less than NvM_WriteAll total size, the sector is switched, for example, sector 1 is switched to sector 2. After the sector switching is completed, during the system shutdown phase, all the data blocks to be written to the flash memory are uniformly written to the same sector, and the situation where some data blocks are written to one sector and then switched to another sector to write the remaining data blocks will not occur. Thus, this solution pre-judges whether to switch sectors before the system shutdown phase and performs sector switching in advance to avoid increasing the system shutdown time due to sector switching during the system shutdown phase.

[0035] In some embodiments, referring to Figure 3 the sector example shown, the process of sector switching is described. Among them, when the currently active sector is sector 1, when the data volume of the NvM block to be written is greater than the free capacity of sector 1, the valid data in sector 2 can be first moved to sector 3, and then all the data in sector 2 is erased. Then, the currently active sector is switched from 1 to 2 to store the NvM block to be written in sector 2 to complete the sector switching.

[0036] It can be understood that for a flash memory using the FEE mode, when writing data to the flash memory, there is usually one sector in the flash memory as the currently active sector for writing data, and there is at least one other sector that can directly erase the content and be used as the switched sector. And during the sector switching process, when a sector is used as the switched sector, the valid data in this sector has been moved to other sectors. For example, before switching the currently active sector from sector 1 to sector 2, the valid data in sector 2 has been moved to other sectors such as sector 3.

[0037] In some embodiments, the data storage method provided in this application can be applied to a system using an analog EEPROM. For example, the electronic device includes but is not limited to in-vehicle electronic devices (such as vehicles), mobile phones, tablets, domain controllers, HPC (High Performance Computers), etc. In addition, the operating system in the electronic device can be any operating system that supports flash memory working in the FEE mode. For example, it can be the Automotive Open System Architecture (AUTOSAR). In the following embodiments of this application, the data storage method of the application is mainly described by taking an in-vehicle electronic device with AUTOSAR as an example.

[0038] In some embodiments, a non-volatile storage manager (NVRAM manager) can be set in the electronic device of this application. Among them, AUTOSAR stipulates that the application program (APP) can only access the non-volatile memory, such as flash memory, through the NVRAM manager. Specifically, in this application, the NVRAM manager can write the Nv block corresponding to the RAM block in the RAM into the active sector of the flash memory. For example, when the system is shut down, the application program (APP) running on the electronic device can write the corresponding Nv blocks of some RAM blocks in the RAM into the flash memory through the NVRAM manager. In addition, the Nv block can also be called the FEE block (that is, the FEE block).

[0039] The system shutdown in this application generally refers to the system shutdown operation. In the computer field, the system shutdown usually includes operations such as closing all application programs, saving all data, turning off all devices, entering the low-power mode or powering off, etc. The system shutdown can be manual or automatic. Manual shutdown is usually performed by the user manually operating the system interface or executing the system shutdown command through the command line, while automatic shutdown is usually triggered automatically by the system itself or other programs, such as when the system fails or reaches the preset running time. Specifically, in the AUTOSAR system, the system shutdown operation needs to ensure that all tasks have been completed and all resources have been released to avoid problems such as data loss or system crashes.

[0040] In addition, the pre-shutdown stage of the system shutdown in this application generally refers to the last stage before the system shutdown, that is, the pre-stage of the shutdown process. In this stage, the system usually performs some cleaning operations, such as hibernation confirmation, device de-initialization, resource release, closing all processes, saving all data, releasing all resources, etc. The purpose of this stage is to ensure that the system is in a stable state before shutdown to avoid problems such as data loss or system crashes.

[0041] The data storage method of this application can adopt a memory management mechanism based on flash storage. Specifically, this application can call the NvM_WriteBlock function to perform an immediate write operation on the RAM block corresponding to the NvM block, or call the NvM_WriteAll function before the system shuts down to synchronize the data of the RAM blocks corresponding to multiple NvM blocks to the flash memory.

[0042] In some embodiments, the RAM blocks to be written to the flash memory in this application usually have preset write attributes and a preset usage status. Among them, the preset write attribute can be the WriteAll attribute, and the preset usage status is the valid status. Then, a data block having a preset write attribute means that the WriteAll attribute of the data block is enabled. That is, for this NvM block, when the NvM_WriteAll function is called, it is necessary to check whether the data block has been updated. If the block status is marked as valid (i.e., marked as the preset status), the data of this RAM block will be synchronized to the flash memory.

[0043] In addition, the status update of the NvM block can be set through the NvM_SetRamBlockStatus(BlockId, Status) function. Among them, the BlockId parameter is the ID of the NvM block, and Status being TRUE means that the data of this NvM block has been updated and needs to be synchronized to the flash memory, and Status being FALSE means that this NvM block does not need to be synchronized to the flash memory. Then, the preset usage status of the RAM block means that the flag (block.valid flag) for setting the usage status through the NvM_SetRamBlockStatus(BlockId, Status) function is TRUE. That is, for a RAM block with the WriteAll attribute enabled, the electronic device can use the function NvM_SetRamBlockStatus(BlockId, Status) to mark that this RAM block needs to be written to the flash memory during the NvM_WriteAll process, and at the same time, the corresponding block.valid flag will be set to TRUE.

[0044] In some embodiments, after enabling a RAM block and setting it to the valid state (i.e., the preset usage state), this application needs to ensure that the data in the RAM block can still be correctly read and used after the system restarts. To this end, AUTOSAR provides some application interface (API) functions that can be used to save the data in the RAM block in the NVM so that it can be restored after the system restarts. For example, the Nvm_WriteAll() function can be used to write the data in the RAM block into the NVM so that it can be read and used after the system restarts. At this time, the NvM_Write() function can be used as the write instruction for writing the RAM block to the flash memory. Specifically, in the system shutdown phase of this application, the electronic device can write the data in the RAM block with the valid state into the flash memory through the Nvm_WriteAll() function.

[0045] It can be understood that in the shutdown phase of the electronic device, the Nvm_WriteAll() function will retrieve all NvM blocks with the WriteAll attribute and synchronize the data of the RAM blocks represented as valid into the flash memory.

[0046] This application can use two methods to count the free capacity corresponding to the remaining storage space of the current active sector and the total data volume of the data blocks to be written to the flash memory in the system shutdown phase before the system shutdown phase. Among them, in the first counting method, this application can continuously count the free capacity corresponding to the remaining storage space of the current active sector in the flash memory during the operation of the electronic device and count the total data volume of the data blocks to be written to the flash memory in the system shutdown phase in real time. In the second counting method, this application can uniformly determine the free capacity of the remaining storage space of the current active sector in the flash memory in the pre-processing stage before the system shutdown and count the total data volume of the data blocks to be written to the flash memory in the system shutdown phase.

[0047] In some embodiments, in the first counting method, this application can count the free capacity of the remaining storage space of the current active sector in the flash memory when writing data to the flash memory during the operation of the operating system of the electronic device. And, after the RAM block is updated and the block state is set to valid, re-count the data volume corresponding to the RAM block with the preset usage state in the RAM. In this way, before entering the system shutdown phase, it can be determined whether the total data volume of the data blocks to be written to the flash memory counted is less than the free capacity of the current active sector counted. If it is less, the sector can be switched in advance to avoid switching sectors when entering the system shutdown phase.

[0048] As an example, during the operation of the operating system of the electronic device in this application, the data in the RAM block can be written into the flash memory through the NvM_WriteBlock() function. Among them, in the AUTOSAR architecture, the NvM_WriteBlock() function is used to write data into a specified block (i.e., Nv block) in a specified NvM storage area (such as flash memory). Specifically, the function of the NvM_WriteBlock function is to replace the data in the specified block with the specified data. If the specified block does not exist, a new block will be created. Before writing data, it is necessary to ensure that the specified block has been initialized and can be written with data. If the block has not been initialized, the NvM_InitBlock function needs to be called for initialization. After writing the data, the written data can be obtained and verified by calling the NvM_GetData() function.

[0049] In some embodiments, in the second statistical method, during the pre-processing stage before the system is shut down, this application can real-time statistically calculate the free capacity of the remaining storage space of the currently active sectors in the flash memory, and statistically calculate the data volume corresponding to the RAM blocks in the RAM that have preset write attributes and preset usage statuses. At this time, the data volume corresponding to the RAM blocks in the RAM that have preset write attributes and preset usage statuses is the total data volume of the data blocks to be written into the flash memory during the system shutdown stage. In this way, before entering the system shutdown stage, it can be determined whether the total data volume of the data blocks to be written into the flash memory statistically calculated is greater than the free capacity of the currently active sectors statistically calculated. If it is greater, the sectors can be switched in advance to avoid switching sectors during the system shutdown stage.

[0050] Next, refer to Figure 4 , the process of the data storage method provided by the embodiments of this application will be introduced. This method can be executed by an electronic device, specifically by the NVRAM manager in the electronic device. As Figure 4 shown, this method includes the following steps:

[0051] S401: Obtain the total data volume of multiple data blocks to be written into the flash memory, and obtain the free capacity of the currently used sector 1 (such as the first sector) in the flash memory. Among them, the data in the multiple data blocks to be written is the data in the RAM.

[0052] Among them, each data block to be written can be an Nv block, that is, each data block to be written includes valid data, check data, and storage control data. And the valid data in the data block to be written is the data in a RAM block in the RAM.

[0053] For example, the RAM block has 10-byte length valid data. And the corresponding Nv block has 10-byte length valid data, data check (CRC) (i.e., check data), and storage control data.

[0054] In some embodiments, multiple data blocks to be written in the present application may be RAM blocks of data that need to be written to the flash memory through the NvM_WriteAll() function. For example, the total amount of data of multiple data blocks to be written to the flash memory can be obtained through the NvM_WriteAll total size() function, and the NvM_WriteAll total size() function is the sum of the Nv block lengths corresponding to all RAM blocks to be written. At this time, referring to Figure 3 as shown, the total amount of data of multiple data blocks to be written to the flash memory can be denoted as NvM_WriteAll total size.

[0055] It can be understood that the free capacity of the currently used sector 1 in the flash memory can be obtained by subtracting the used capacity in sector 1 from the total capacity of sector 1. For example, referring to Figure 3 , the free capacity of the currently used sector 1 can be denoted as free size.

[0056] In some embodiments, the present application may obtain the above-mentioned total amount of data and free capacity by using the first statistical method or the second statistical method.

[0057] S402: Corresponding to the total amount of data of the data blocks to be written being greater than the free capacity of sector 1, switch the currently used sector in the flash memory from sector 1 to sector 2 (such as the second sector).

[0058] It can be understood that the total amount of data of multiple data blocks to be written to the flash memory being greater than the free capacity of sector 1 indicates that the current sector 1 does not have enough storage space to store multiple data blocks to be written. Then, it is necessary to switch the currently active sector 1 in the flash memory to sector 2. Among them, when switching from sector 1 to sector 2, the data in sector 2 can be erased and then the multiple data blocks to be written can be stored in sector 2. Or, when switching from sector 1 to sector 2, if there is no data stored in sector 2, the multiple data blocks to be written can be directly stored in sector 2.

[0059] In some embodiments, sector 2 can be randomly selected from the flash memory or determined based on the sorting result of the free capacity of each sector. Exemplarily, the sector with the largest free capacity in the sorting result of the free capacity can be used as sector 2. In addition, in some other embodiments, each sector in the flash memory can be preset. For example, two sectors or three sectors can be set in the flash memory. At this time, sector 2 is an unused one among the preset sectors.

[0060] In some embodiments, referring to Figure 3For the sectors shown, in the present application, valid data in sector 2 can be moved to sector 3, and after erasing the data in sector 2, sector 1 can be switched to sector 2.

[0061] S403: Corresponding to detecting a system shutdown instruction for the electronic device, each data block in multiple data blocks to be written is sequentially stored in sector 2.

[0062] In some embodiments, after detecting a system shutdown instruction for the electronic device, the present application can sequentially store each data block in multiple data blocks to be written in the second sector.

[0063] In some embodiments, upon detecting a system shutdown instruction for the electronic device, the electronic device can perform a system shutdown operation. For example, the system shutdown instruction can be a shutdown instruction. Thus, since the present application switches sectors for multiple data blocks to be written to the flash memory before detecting a system shutdown instruction for the electronic device, that is, before the system shutdown phase, sector switching does not occur during the system shutdown phase, avoiding an increase in the system shutdown time.

[0064] Next, refer to Figure 5 The process of the data storage method under the first statistical method provided by the embodiments of the present application will be described. This method can be executed by an electronic device, specifically by the NVRAM manager in the electronic device. As Figure 5 described, the method includes the following steps:

[0065] S501: During the operation of the electronic device, detect the RAM blocks to be written to the flash memory.

[0066] In some embodiments, the present application can periodically execute S501 to detect whether there are RAM blocks that need to be written to the flash memory. For example, the present application can detect whether the NvM_WriteBlock() function is called to initiate data writing to the flash memory.

[0067] S502: During the operation of the electronic device, detect the RAM blocks set to a preset usage state.

[0068] S503: Upon detecting an immediate write instruction for RAM block 01, obtain the data volume 01 corresponding to RAM block 01, and obtain the free capacity 1 of the currently active sector 1 in the flash memory.

[0069] In some embodiments, the immediate write instruction can be generated by calling the NvM_WriteBlock() function. Among them, RAM block 01 can be a RAM block in the RAM of the electronic device, and this RAM block can be set with a preset write attribute.

[0070] It can be understood that the RAM block 01 indicated by the immediate write instruction includes valid data, and the corresponding Nv block usually only includes this valid data, that is, the data volume corresponding to the RAM block 01 is the data volume of the valid data in the RAM block 01.

[0071] S504: Determine whether the data volume 01 is greater than the free capacity 1. If not, go to S505; if so, go to S512.

[0072] S505: Write the Nv block corresponding to the RAM block 01 into the sector 1, and subtract the data volume 01 from the free capacity 1 to obtain the updated free capacity 2 of the sector 1.

[0073] For example, referring to Figure 3 as shown, the updated free capacity 2 of the sector 1 is the free size.

[0074] S506: Detect that the RAM block 02 with a preset write attribute is set to a preset usage state, obtain the data volume 02 corresponding to the RAM block 02, and obtain the total data volume 10 corresponding to the RAM block with a preset usage state currently to be written into the flash memory.

[0075] Among them, the total data volume 10 corresponding to the RAM block with a preset usage state currently to be written into the flash memory is the total data volume corresponding to the RAM blocks with a preset write attribute and a preset usage state in the RAM before the RAM block 02 is set to the preset usage state. Specifically, the present application can count the RAM blocks whose block.valid flag is set to TRUE through the function NvM_SetRamBlockStatus(BlockId,Status), and calculate the data volume corresponding to these RAM blocks to obtain the NvM_WriteAll total size. For example, the total data volume 10 is the currently calculated NvM_WriteAll total size.

[0076] It can be understood that the RAM block 02 with a preset write attribute and a preset usage state usually only includes valid data, and the corresponding Nv block includes not only this valid data, but also data verification (CRC) and storage control data. Then, the data volume corresponding to the RAM block 02 is the total data volume of the corresponding valid data, data verification (CRC), and storage control data, that is, the data volume 02 corresponding to the RAM block 02 is the data volume of the corresponding Nv block.

[0077] S507: Add the total data volume 10 and the data volume 02 to obtain the updated total data volume 11 of the RAM block with a preset usage state currently to be written into the flash memory.

[0078] For example, referring to Figure 3As shown, the total updated data volume 11 of the RAM blocks with a preset usage status to be written to the flash memory currently is the NvM_WriteAll total size. Specifically, the present application can count the RAM blocks whose block.valid flag is set to TRUE through the function NvM_SetRamBlockStatus(BlockId,Status), and calculate the data volume corresponding to these RAM blocks to obtain the NvM_WriteAll total size.

[0079] S508: Determine whether the total data volume 11 is greater than the free capacity 2. If so, enter S509; if not, enter S511.

[0080] S509: Switch the currently active sector in the flash memory from sector 1 to sector 2.

[0081] Among them, the free capacity 2 is less than the total data volume 11, indicating that there is not enough space in the current sector 1 to store all the data corresponding to the RAM blocks with a preset usage status to be written to the flash memory currently. Therefore, before the system shutdown phase, if it is determined that the total data volume 11 is less than the free capacity 2, the present application can pre-switch the sectors.

[0082] S510: Corresponding to detecting a system shutdown instruction for the electronic device, when a synchronous write instruction is detected, write the Nv blocks corresponding to the RAM blocks with a preset usage status to be written to the flash memory currently into sector 2. That is, during the system shutdown phase, when a synchronous write instruction is detected, write the data blocks to be written to the flash memory into sector 2.

[0083] Among them, the above system shutdown instruction can be a shutdown instruction, and the above synchronous write instruction can be an instruction to call the NvM_WriteAll() function.

[0084] For example, the present application can write all the data corresponding to the RAM blocks with a preset usage status to be written to the flash memory currently into sector 2 through the NvM_WriteAll() function. And each RAM block corresponds to an Nv block in sector 2.

[0085] S511: Corresponding to detecting a system shutdown instruction for the electronic device, when a synchronous write instruction is detected, write the Nv blocks corresponding to the RAM blocks with a preset usage status to be written to the flash memory currently into sector 1. At this time, there is enough storage space in sector 1 to store the Nv blocks corresponding to the RAM blocks with a preset usage status.

[0086] S512: Switch the currently active sector in the flash memory from sector 1 to sector 2, and write the Nv block corresponding to RAM block 01 into sector 2.

[0087] It can be understood that during the operation of the electronic device, when the electronic device replaces the data in the flash memory through the NvM_WriteBlock() function, sector switching judgment can be performed and sector switching can be carried out to ensure the normal operation of the system.

[0088] In this way, the data storage method provided by this application can, during the operation of the electronic device, count in real time the total amount of data corresponding to the RAM blocks whose block.valid flag to be written into the flash memory is set to TRUE, and count in real time the free capacity of the current sector in the flash memory, so as to ensure that it can be timely judged before the system shuts down whether the RAM to be written into the flash memory needs to trigger sector switching. In this way, sector switching can be carried out in advance before the system shuts down to avoid increasing the system shutdown time due to sector switching when the system shuts down.

[0089] In some embodiments, on the basis of Figure 5 showing the data storage method, with reference to Figure 6The function call flow showing sector switching is presented. Specifically, each time the NvM_WriteBlock() function is called during the operation of the electronic device, the first instruction "section free size = section free size - write_block'size" can be executed to update the free capacity of the current active sector in the flash memory (S61). Here, section free size represents the free capacity of the current active sector in the flash memory, and write_block'size represents the amount of data in the RAM block that is currently written to the current active sector through the NvM_WriteBlock() function. Also, each time the NvM_SetRamBlockStatus(BlockId, TRUE) function is called during the operation of the electronic device, the second instruction "NvM_WriteAll total size = NvM_WriteAll total size + block'size" can be executed to update the total amount of data corresponding to the RAM block to be written to the flash memory (S62), that is, to obtain the total amount of data corresponding to the RAM block to be written to the flash memory during the system shutdown phase. Here, NvM_WriteAll total size represents the total amount of data corresponding to the RAM block to be written to the flash memory through the NvM_WriteAll() function, and block'size represents the amount of data in the Nv block corresponding to the RAM block for which the block.valid flag is set to TRUE by the NvM_SetRamBlockStatus(BlockId, Status) function. Thus, when both the NvM_WriteBlock() function and the NvM_SetRamBlockStatus(BlockId, Status) function are called, the judgment step of "section free size < NvM_WriteAll total size" can be executed, that is, to judge whether the free capacity of the current active sector is less than the total amount of data corresponding to the RAM block to be written to the flash memory (S63). If the judgment is yes, then "FEE sector switch" is executed, that is, sector switching (S64).

[0090] It can be understood that when the NvM_WriteBlock() function is called, the free capacity of the current sector decreases, and when the NvM_SetRamBlockStatus() function is called, the amount of data to be written to the flash memory by the NvM_WriteAll() function increases. When these two functions are called, the sizes of section free size and NvM_WriteAll total size are compared to determine in real time whether sector switching is needed. Thus, it can be ensured that sector switching judgment and sector switching are completed in advance before the system shuts down.

[0091] Next, refer to Figure 7 to describe the process of the data storage method under the second statistical method provided by the embodiments of the present application. This method can be executed by an electronic device, specifically by the NVRAM manager in the electronic device. As Figure 7 described, the method includes the following steps:

[0092] S701: Detect a pre-processing instruction for the shutdown of the electronic device system, count the total data volume 20 corresponding to all RAM blocks with a preset usage status that are currently to be written to the flash memory, and obtain the free capacity 3 of the current active sector 1 in the flash memory.

[0093] For example, detecting a pre-processing instruction for the shutdown of the electronic device system can be implemented by the electronic device calling the NvM_PreShutdownCheck() function to enter the pre-processing stage of system shutdown. In addition, the electronic device calls an NvM callback function before the pre_shutdown stage to count the total data volume 20 corresponding to all RAM blocks with a preset usage status that are currently to be written to the flash memory, and obtain the free capacity 3 of the current active sector 1 in the flash memory.

[0094] Refer to Figure 3 shown, the total data volume 20 corresponding to all RAM blocks with a preset usage status that are currently to be written to the flash memory can be NvM_WriteAll total size, and the free capacity 3 of the current active sector 1 in the flash memory can be freesize.

[0095] In some embodiments, the present application can execute S701 once during the pre-processing of system shutdown. Specifically, the present application can count the RAM blocks whose block.valid flag is set to True through the function NvM_SetRamBlockStatus(BlockId,TRUE), and calculate the data volume corresponding to these RAM blocks to obtain NvM_WriteAll total size.

[0096] It can be understood that the data volume corresponding to the RAM blocks that enable the preset write attribute and have a preset usage status is the total data volume of the corresponding valid data, data check (CRC), and storage control data.

[0097] S702: Determine whether the total data volume 20 is greater than the free capacity 3. If so, enter S703; if not, re-execute S705.

[0098] S703: Switch the currently active sector in the flash memory from sector 1 to sector 2.

[0099] Among them, the total data volume 20 is greater than the free capacity 3, indicating that there is not enough space in the current sector 1 to complete the storage of all data corresponding to the RAM blocks with a preset usage status to be written to the flash memory currently. Therefore, before the system shutdown phase of the present application, if it is determined that the total data volume 20 is less than the free capacity 3, the sector can be switched in advance.

[0100] S704: Corresponding to detecting a system shutdown instruction for the electronic device, when a synchronous write instruction is detected, write the Nv block corresponding to the RAM block with a preset usage status to be written to the flash memory currently into sector 2.

[0101] Among them, the above system shutdown instruction can be a shutdown instruction, and the above synchronous write instruction can be an instruction to call the NvM_WriteAll() function.

[0102] For example, the present application can write all data corresponding to the RAM blocks with a preset usage status to be written to the flash memory currently into sector 2 through the NvM_WriteAll() function. And each RAM block corresponds to an Nv block in sector 2.

[0103] S705: Corresponding to detecting a system shutdown instruction for the electronic device, when a synchronous write instruction is detected, write the Nv block corresponding to the RAM block with a preset usage status to be written to the flash memory currently into sector 1.

[0104] In this way, the data storage method provided by the present application can uniformly count the total amount of data corresponding to the RAM blocks whose block.valid flag to be written to the flash memory is set to TRUE and count the free capacity of the current sector in the flash memory during the preprocessing stage before the electronic device system shuts down, so as to ensure that it can be timely determined whether the RAM to be written to the flash memory needs to trigger a sector switch before the system shuts down. In this way, the sector can be switched in advance before the system shuts down to avoid increasing the system shutdown time due to sector switching when the system shuts down.

[0105] In some embodiments, on the basis of Figure 7 showing the data storage method, with reference to Figure 8The function call process showing sector switching is presented. Specifically, each time the NvM_PreShutdownCheck() function is called during the operation of the electronic device, that is, when a pre-processing instruction for system shutdown is detected, the following first program can be executed: the "for all NvM Blocks" program to count the total data volume corresponding to all RAM blocks with a preset usage status to be written to the flash memory currently (S81). And, obtain the free capacity of the current active sector 1 in the flash memory. Among them, in the "for all NvM Blocks" program, the third instruction can be executed for the RAM blocks where block.valid == 1 (i.e., TRUE): NvM_WriteAll total size = NvM_WriteAll total size + block'size.

[0106] For example, the "for all NvM Blocks" program can be:

[0107] for all NvM Blocks

[0108] if(block.valid == 1){

[0109] NvM_WriteAll total size = NvM_WriteAll total size + block'size}

[0110] Furthermore, the present application can execute the judgment step of "section free size < NvM_WriteAll total size", that is, judge whether the free capacity of the current active sector is less than the total data volume corresponding to the RAM blocks to be written to the flash memory (S82). If the judgment is yes, then execute "FEE section switch", that is, sector switching (S83). In this way, it can ensure that sector switching judgment and sector switching are completed in advance before the system shuts down.

[0111] In some embodiments, the electronic device in the present application is taken as an in-vehicle electronic device for illustration. Refer to Figure 9 The hardware structure of the vehicle 1 shown is used to illustrate the hardware structure of the electronic device provided by the embodiments of the present application.

[0112] Next, in combination with Figure 9 The structure of the vehicle is introduced. Figure 9 FIG. shows a schematic structural diagram of a vehicle provided by the embodiments of the present application.

[0113] Figure 9 FIG. is a possible functional framework schematic diagram of a vehicle 1 provided by the embodiments of the present application. As Figure 9As shown, the functional framework of vehicle 1 may include various subsystems, such as the sensor system 10, control system 20, one or more peripheral devices 30 (illustrated by one example), power supply 40, and computer system 50 in the figure. Optionally, vehicle 1 may also include other functional systems, such as an engine system that provides power to vehicle 1, etc., which are not limited in this application.

[0114] Among them,

[0115] The sensor system 10 may include several detection devices that can sense the measured information and convert the sensed information into electrical signals or other required forms of information according to certain rules for output. As shown in the figure, these detection devices may include a global positioning system 11 (GPS), a vehicle speed sensor 12, an inertial measurement unit 13 (IMU), etc., which are not limited in this application.

[0116] The global positioning system GPS 11 is a system that uses GPS positioning satellites to perform real-time positioning and navigation globally. In this application, the global positioning system GPS 11 can be used to achieve the real-time positioning of vehicle 1 and provide the geographical location information of vehicle 1. The vehicle speed sensor 12 is used to detect the driving speed of vehicle 1. The inertial measurement unit 13 may include a combination of an accelerometer and a gyroscope and is a device for measuring the angular rate and acceleration of vehicle 1. For example, during the driving process of vehicle 1, the inertial measurement unit can measure the position and angle changes of the vehicle body based on the inertial acceleration of vehicle 1, such as measuring the acceleration and angular rate of vehicle 1.

[0117] The control system 20 may include a steering unit 21, a braking unit 22, etc.

[0118] The steering unit 21 may represent a system for adjusting the traveling direction of vehicle 1, which may include but is not limited to a steering wheel or any other structural device for adjusting or controlling the traveling direction of vehicle 1. The braking unit 22 may represent a system for slowing down the driving speed of vehicle 1, and may also be referred to as the braking system of vehicle 1. It may include but is not limited to a brake controller, a reducer, or any other structural device for decelerating vehicle 1, etc. In practical applications, the braking unit 22 can use friction to slow down the tires of vehicle 1, thereby slowing down the driving speed of vehicle 1.

[0119] The peripheral device 30 may include several components, such as the communication system 31, touch screen 32, user interface 33, etc. shown in the figure. Among them, the communication system 31 is used to implement network communication between the vehicle 1 and other devices except the vehicle 1, such as the electronic device 2. In practical applications, the communication system 31 can use wireless communication technology or wired communication technology to achieve network communication between the vehicle 1 and other devices. The wired communication technology may refer to communication between the vehicle 1 and other devices through network cables or optical fibers, etc. The wireless communication technology includes but is not limited to global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), wideband code division multiple access (WCDMA), time-division code division multiple access (TD-SCDMA), long term evolution (LTE), wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), and infrared technology (IR), etc.

[0120] The touch screen 32 can be used to detect operation instructions on the touch screen 32. For example, the user performs a touch operation on the content data displayed on the touch screen 32 according to actual needs to implement the functions corresponding to the touch operation, such as playing multimedia files such as music and videos. The user interface 33 may specifically be a touch panel for detecting operation instructions on the touch panel. The user interface 33 can also be physical buttons or a mouse. The user interface 34 can also be a display screen for outputting data and displaying images or data. Optionally, the user interface 34 can also be at least one device belonging to the category of peripheral devices, such as a touch screen, microphone, and speaker, etc.

[0121] A number of functions of vehicle 1 are controlled and implemented by computer system 50. The computer system 50 may include one or more processors 51 (illustrated with one processor as an example) and a memory 52 (which may also be referred to as a storage device). In practical applications, the memory 52 may also be inside the computer system 50 or outside the computer system 50, such as a cache in vehicle 1, etc., which is not limited in this application.

[0122] The processor 51 may include one or more general-purpose processors, such as a graphic processing unit (GPU) and an Electronic Control Unit (ECU). The processor 51 can be used to run the relevant programs or instructions corresponding to the programs stored in the memory 52 to implement the corresponding functions of vehicle 1, such as executing the data storage method in this application. For example, an NVRAM manager is set in the processor 51 to execute the data storage method in this application.

[0123] The memory 52 may include a volatile memory, such as RAM; the memory may also include a non-volatile memory (NVM) (for example), such as ROM, flash memory, HDD, solid-state drive SSD, flash memory (such as flash memory emulating EEPROM) or EEPROM; the memory 52 may also include a combination of the above types of memories. The memory 52 can be used to store a set of program codes or instructions corresponding to the program codes so that the processor 51 can call the program codes or instructions stored in the memory 52 to implement the corresponding functions of vehicle 1. The functions include but are not limited to Figure 9 Some or all of the functions shown in the schematic diagram of the vehicle 1 function framework. In this application, a set of program codes for controlling vehicle 1 can be stored in the memory 52, and the processor 51 can call the program codes to control the safe driving of vehicle 1 and support storing non-volatile data during the system shutdown phase.

[0124] Optionally, in addition to storing program codes or instructions, the memory 52 can also store information such as road maps, driving routes, sensor data, and non-volatile data required to be stored during the system shutdown phase. The computer system 50 can combine with other elements in the schematic diagram of the vehicle 1 function framework, such as sensors in the sensor system, GPS, etc., to implement the relevant functions of vehicle 1. For example, the computer system 50 can control the driving direction or driving speed of vehicle 1 based on the data input of the sensor system 10, etc., which is not limited in this application.

[0125] It should be noted that the above Figure 9This is only a schematic diagram of a possible functional framework of vehicle 1. In actual applications, vehicle 1 may include more or fewer systems or components, which are not limited in this application.

[0126] Embodiments of the mechanisms disclosed in this application can be implemented in hardware, software, firmware, or a combination of these implementation methods. Embodiments of this application can be implemented as a computer program or program code executed on a programmable system, which includes at least one processor, a storage system (including volatile and non-volatile memories and / or storage elements), at least one input device, and at least one output device.

[0127] The program code can be applied to the input instructions to perform the various functions described in this application and generate output information. The output information can be applied to one or more output devices in a known manner. For the purposes of this application, a processing system includes any system having a processor such as, for example, a digital signal processor (DSP), a microcontroller, an application specific integrated circuit (ASIC), or a microprocessor.

[0128] The program code can be implemented in a high-level procedural language or an object-oriented programming language in order to communicate with the processing system. When necessary, the program code can also be implemented in assembly language or machine language. In fact, the mechanisms described in this application are not limited to the scope of any specific programming language. In any case, the language can be a compiled language or an interpreted language.

[0129] In some cases, the disclosed embodiments can be implemented in hardware, firmware, software, or any combination thereof. The disclosed embodiments can also be implemented as instructions carried or stored on one or more transient or non-transient machine-readable (e.g., computer-readable) storage media, which can be read and executed by one or more processors. For example, the instructions can be distributed via a network or via other computer-readable media. Thus, a machine-readable medium can include any mechanism for storing or transmitting information in a form readable by a machine (e.g., a computer), including but not limited to, a floppy disk, a compact disc, an optical disc, a compact disc read-only memory (CD-ROMs), a magneto-optical disc, a read-only memory (ROM), a random access memory (RAM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), a magnetic card or an optical card, a flash memory, or a tangible machine-readable memory for transmitting information (e.g., carrier waves, infrared signals, digital signals, etc.) in the form of electrical, optical, acoustic, or other propagated signals using the Internet. Thus, a machine-readable medium includes any type of machine-readable medium suitable for storing or transmitting electronic instructions or information in a form readable by a machine (e.g., a computer).

[0130] In the accompanying drawings, some structural or method features may be shown in a specific arrangement and / or order. However, it should be understood that such a specific arrangement and / or ordering may not be required. Instead, in some embodiments, these features may be arranged in a manner and / or order different from that shown in the illustrative drawings. Additionally, the inclusion of a structural or method feature in a particular figure does not imply that such a feature is required in all embodiments, and in some embodiments, these features may not be included or may be combined with other features.

[0131] It should be noted that each unit / module mentioned in the device embodiments of this application is a logical unit / module. Physically, a logical unit / module can be a physical unit / module, a part of a physical unit / module, or can be implemented as a combination of multiple physical units / module. The physical implementation manner of these logical units / modules themselves is not the most important. The combination of the functions implemented by these logical units / modules is the key to solving the technical problems proposed in this application. In addition, to highlight the innovative part of this application, the above device embodiments of this application do not introduce units / modules that are not closely related to solving the technical problems proposed in this application. This does not mean that there are no other units / modules in the above device embodiments.

[0132] It should be noted that in the examples and descriptions of this patent, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, such that a process, method, article or device comprising a series of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "comprising one" does not exclude the presence of additional identical elements in the process, method, article or device comprising the element.

[0133] Although this application has been illustrated and described by reference to certain preferred embodiments thereof, those of ordinary skill in the art should understand that various changes in form and detail may be made thereto without departing from the spirit and scope of this application.

Claims

1. A data storage method, characterized in that, Applied to an electronic device, the electronic device including a random access memory and a flash memory, the method comprising: In the case where a system shutdown instruction for the electronic device is not detected, obtaining the total amount of data of a plurality of data blocks to be written to the flash memory, and obtaining the free capacity of a first sector currently in use in the flash memory, wherein the data in the plurality of data blocks to be written is the data in the random access memory; Corresponding to the total amount of data of the plurality of data blocks to be written being greater than the free capacity of the first sector, switching the sector currently in use in the flash memory from the first sector to a second sector; Corresponding to detecting a system shutdown instruction for the electronic device, when a synchronous write instruction for the plurality of data blocks to be written is detected, sequentially storing each data block in the plurality of data blocks to be written into the second sector; The flash memory is in a flash memory emulation electrically erasable programmable read-only memory (FEE) mode; The data block to be written has a preset write attribute, and the usage status of the data block to be written is a preset usage status, and the plurality of data blocks to be written are data blocks having the preset usage status determined from the data blocks having the preset write attribute; Wherein, the preset write attribute is used to indicate that when a data block is written to the flash memory, all fields or attributes of the data block are written to the flash memory; the usage status of the data block is used to indicate whether the data in the data block is allowed to be written to the flash memory, and the preset usage status is used to indicate that the data in the data block is allowed to be written to the flash memory; The synchronous write instruction is used to write a data block having a preset write attribute and a preset usage status to the flash memory.

2. The method according to claim 1, characterized in that, The obtaining the total amount of data of the plurality of data blocks to be written to the flash memory, and obtaining the free capacity of the first sector currently in use in the flash memory, includes: Detecting a preprocessing instruction for system shutdown of the electronic device; Counting a plurality of data blocks to be processed having a preset usage status in the random access memory; Obtaining the plurality of data blocks to be written according to the plurality of data blocks to be processed, wherein one data block to be written includes valid data of one data block to be processed, as well as corresponding check data and storage control data; Calculating the data amount of the plurality of data blocks to be written, and calculating the free capacity of the first sector.

3. The method according to claim 1, characterized in that, The obtaining the total amount of data of the plurality of data blocks to be written to the flash memory, and obtaining the free capacity of the first sector currently in use in the flash memory, includes: Detecting an immediate write instruction, corresponding to the data amount of the data block indicated by the immediate write instruction being less than or equal to the free capacity of the flash memory, storing the data block indicated by the immediate write instruction to the first sector, and subtracting the data amount of the data block indicated by the immediate write instruction from the free capacity of the first sector to update the free capacity of the first sector, wherein the data block indicated by the immediate write instruction is different from the plurality of data blocks to be written, and the immediate write instruction is used to immediately write the valid data of the indicated data block to the flash memory; A status setting instruction is detected, and the data block indicated by the status setting instruction is set to a preset usage status. The total data volume of the multiple data blocks to be written is added with the data volume corresponding to the data block indicated by the status setting instruction to update the total data volume of the multiple data blocks to be written. Wherein, the data volume corresponding to the data block indicated by the status setting instruction includes the valid data of the data block and the data volumes of the corresponding check data and storage control data.

4. The method according to claim 2, wherein The method further includes: When an immediate write instruction is detected and the data volume corresponding to the data block indicated by the immediate write instruction is greater than the free capacity of the flash memory, the currently used sector in the flash memory is switched from the first sector to the second sector, and the data block indicated by the immediate write instruction is stored in the second sector.

5. The method according to claim 1, characterized in that, The method further includes: Corresponding to detecting a system shutdown instruction for the electronic device, when a synchronous write instruction for the multiple data blocks to be written is detected and the total data volume of the multiple data blocks to be written is less than or equal to the free capacity of the first sector, each data block in the multiple data blocks to be written is sequentially stored in the first sector.

6. The method according to any one of claims 1 to 5, characterized in that, The system of the electronic device is an AUTOSAR system.

7. A readable medium, characterized in that, Instructions are stored on the readable medium, and when the instructions are executed on the electronic device, the electronic device executes the data storage method according to any one of claims 1 to 6.

8. An electronic device, characterized in that, It includes: A memory for storing instructions executed by one or more processors of the electronic device, and a processor, which is one of the one or more processors of the electronic device, for executing the data storage method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Data recording method, micro-control unit, data recording device and vehicle

    CN117173812A