Data writing method and device, computer equipment and storage medium

By introducing an index region in the memory to manage the write position state, the erase operation is performed only under full write conditions, which solves the aging problem caused by frequent erase and write operations of non-volatile memory. This achieves data write reliability and lifespan extension under storage resource constraints and is suitable for optimizing internal flash memory of microcontrollers.

CN121832844APending Publication Date: 2026-04-10SHENZHEN BAYTEST TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-12-25
Publication Date
2026-04-10

AI Technical Summary

Technical Problem

In embedded systems, industrial control equipment, and IoT terminals, frequent erasure and rewriting of non-volatile memory leads to memory cell aging, affecting data reliability and limiting storage resources. The challenge is how to reduce the erasure frequency of the smallest erase/write unit without significantly increasing storage resource consumption, while also ensuring the reliability of data writing and reading.

Method used

By introducing an index region in the memory to record the usage status of the write position, it can determine whether the storage area meets the full write condition. The erase operation is only performed when the full write condition is met, and the target write position is determined in the storage area when the condition is not met. This avoids frequent erasures. By combining the index region with the update of the write position status, the continuity and manageability of the write process are ensured.

Benefits of technology

It reduces the number of erase operations for the smallest erase/write unit, extends the lifespan of the memory, and ensures the reliability of data writing and reading. It is suitable for scenarios with frequent parameter updates, especially for optimizing the lifespan of the flash memory inside the microcontroller.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121832844A_ABST
    Figure CN121832844A_ABST
Patent Text Reader

Abstract

The invention discloses a data writing method and device, computer equipment and a storage medium, and the method comprises the steps: judging whether a storage region meets a preset full writing condition or not based on index data recorded in an index region when to-be-written data is obtained; when a preset full writing condition is not met, determining a target writing position in the storage area based on the index data; when a preset full writing condition is met, executing an erasing operation on the minimum erasing unit, and taking the initial position of the storage area as a target writing position; and recording the target writing position to the index area, and writing the to-be-written data into the target writing position. The triggering times of erasing operation can be reduced, so that the erasing loss of the minimum erasing unit is reduced, and the service life of the memory is prolonged; meanwhile, the target writing position is recorded to the index area, so that a basis can be provided for the determination of the subsequent writing position, and the continuity and manageability of the writing process are ensured.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data processing, and more particularly to a data writing method, apparatus, computer device, and storage medium. Background Technology

[0002] In embedded systems, industrial control equipment, and IoT terminals, non-volatile memory is often used to store operating parameters, configuration data, or status information. The number of erase / write cycles of non-volatile memory is usually limited by its lifespan. Frequent erasure can easily lead to aging of storage cells, resulting in decreased data reliability or even data failure.

[0003] In practical use, some parameter or status data may be frequently updated. For example, in scenarios such as equipment debugging, parameter configuration adjustment, or operation status recording, the memory needs to perform data updates multiple times in a short period of time, that is, erase the corresponding smallest erase / write unit and write the new data. However, since the erasure operation is irreversible and cannot be performed only on a part of the storage space, frequent data updates can easily lead to the smallest erase / write unit being erased multiple times, thereby accelerating the lifespan of the storage unit. Moreover, in application environments with limited storage resources, the available capacity of non-volatile memory is usually small, and the storage space needs to be used for multiple purposes such as program code, running data, and upgrade data.

[0004] Therefore, how to reduce the erase frequency of the smallest erase / write unit without significantly increasing storage resource consumption, while also ensuring the reliability of data writing and reading, remains a technical problem that needs to be solved by those skilled in the art. Summary of the Invention

[0005] Therefore, it is necessary to provide a data writing method, apparatus, computer device, and storage medium to address the aforementioned technical problems. This method has the advantages of reducing the erasure frequency of the smallest erase / write unit without significantly increasing storage resource consumption, while also ensuring the reliability of data writing and reading.

[0006] A data writing method, applied to a memory, the memory including at least one minimum erase / write unit, the minimum erase / write unit including a storage area and a corresponding index area, the method comprising: When data to be written is obtained, the storage area is determined to meet the preset full write condition based on the index data recorded in the index area. If the preset full write condition is not met, the target write location is determined in the storage area based on the index data; When the preset full write condition is met, an erase operation is performed on the smallest erase / write unit, and the starting position of the storage area is taken as the target write position; The target write position is recorded in the index area, and the data to be written is written to the target write position.

[0007] Optionally, the method further includes: When a read request is received, the location of the currently valid data in the storage area is determined based on the index data recorded in the index area. Data is read from the specified location and used as the read result corresponding to the read request.

[0008] Optionally, the storage area includes multiple preset write locations, the index data includes the usage status corresponding to each write location, and the step of determining whether the storage area meets the preset full write condition based on the index data recorded in the index area includes: Obtain the usage status corresponding to each write position in the index data; When the usage status corresponding to each of the write positions is "used", it is determined that the storage area meets the full write condition; If the usage status corresponding to any of the write positions is unused, it is determined that the storage area does not meet the full write condition.

[0009] Optionally, the storage area includes multiple preset write locations, which are arranged in order of their physical locations within the storage area. Determining the target write location in the storage area based on the index data includes: Based on the index data and the order in which the write positions are arranged, the usage status corresponding to each write position is detected sequentially. When a write location is detected as unused, the unused write location is determined as the target write location.

[0010] Optionally, the storage area includes multiple preset write locations, the index data includes the usage status corresponding to each write location, and the erase operation on the smallest erase / write unit includes: The data stored at each write location in the storage area is erased to restore the storage area to its initial state. Change the usage status of each write position stored in the index data to unused; The method further includes: When the smallest erase / write unit does not perform an erase operation, the usage status corresponding to any write position in the index data is prohibited from changing from used to unused.

[0011] Optionally, the method further includes: Obtain the storage capacity of the minimum write unit, the length constraint of a single write, and the length of the data in the index data that records the usage status of the write position; Based on the storage capacity, the length constraint, and the data length, determine the number of write locations that can be set in the storage area and the number of usage states corresponding to the write locations; Based on the number of write positions and the number of usage states, the storage area and the index area are configured in the minimum erase / write unit to be configured, thus obtaining the minimum erase / write unit.

[0012] Optionally, determining the number of configurable write locations in the storage area and the number of usage states corresponding to each write location based on the storage capacity, the length constraint, and the data length includes: Obtain the target usage duration and the update frequency of the data to be written; Based on the storage capacity, the length constraint, and the data length, determine the number of initial write locations and the corresponding number of initial usage states; Based on the target usage duration and the update frequency of the data to be written, the number of initial write positions and the corresponding number of initial usage states are adjusted to obtain the number of configurable write positions in the storage area and the number of usage states corresponding to the write positions.

[0013] A data writing apparatus is applied to a memory, the memory including at least one minimum erase / write unit, the minimum erase / write unit including a storage area and a corresponding index area, the apparatus comprising: The judgment module is used to determine whether the storage area meets the preset full write condition based on the index data recorded in the index area when the data to be written is obtained. The determination module is used to determine the target write location in the storage area based on the index data when the preset full write condition is not met. The erase module is used to perform an erase operation on the smallest erase / write unit when a preset full write condition is met, and to take the starting position of the storage area as the target write position; The writing module is used to record the target write position to the index area and write the data to be written to the target write position.

[0014] A computer device includes a memory, a processor, and computer-readable instructions stored in the memory and executable on the processor, wherein the processor implements the above-described data writing method when executing the computer-readable instructions.

[0015] A readable storage medium having computer-readable instructions stored thereon, which, when executed by a processor, implement the above-described data writing method.

[0016] The aforementioned data writing method, apparatus, computer device, and storage medium, upon acquiring data to be written, determine whether the storage area meets a preset full-write condition based on the index data recorded in the index area; if the preset full-write condition is not met, determine the target write position in the storage area based on the index data; if the preset full-write condition is met, perform an erase operation on the smallest erase / write unit, and use the starting position of the storage area as the target write position; record the target write position in the index area, and write the data to be written to the target write position. Specifically, upon acquiring data to be written, the method first determines whether the storage area meets the full-write condition based on the index data, and only performs an erase operation on the smallest erase / write unit if the full-write condition is met; if the full-write condition is not met, directly determine the target write position in the storage area and complete the writing process. Therefore, compared to the method of erasing the smallest erase / write unit for each write operation, the number of erase operations can be reduced, thereby reducing the erase / write wear of the smallest erase / write unit and extending the lifespan of the memory. At the same time, by recording the target write position in the index area, a basis can be provided for determining the subsequent write position, ensuring the continuity and manageability of the write process. Attached Figure Description

[0017] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the description of the embodiments of the present invention will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0018] Figure 1 This is a flowchart illustrating a data writing method according to an embodiment of the present invention; Figure 2 This is a schematic diagram of a data writing device in one embodiment of the present invention; Figure 3 This is a schematic diagram of a computer device according to an embodiment of the present invention. Detailed Implementation

[0019] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0020] In one embodiment, such as Figure 1 As shown, a data writing method is provided, which is applied to a memory. The memory includes at least one minimum erase / write unit, which includes a storage area and a corresponding index area. The method includes the following steps: 101. When the data to be written is obtained, determine whether the storage area meets the preset full write condition based on the index data recorded in the index area.

[0021] In this embodiment of the invention, the memory can be a non-volatile memory, such as flash memory integrated inside a microcontroller, or other non-volatile storage media external to the processor, as long as the memory has the characteristics of limited minimum erase / write granularity and separation of write and erase operations. The minimum erase / write unit can be understood as the smallest physical unit that can be covered when performing an erase operation in the memory, such as a page or sector in flash memory. The erase operation can only be performed on the entire unit, and cannot erase only a part of it.

[0022] The storage area can be a space within the smallest erase / write unit designated for storing user data. This space can be written to multiple times after a single erase, but data already written cannot be restored to its initial state before an erase operation is performed. The index area is a reserved space within the same smallest erase / write unit for recording the write status (i.e., the usage status of the write position). It reflects the usage of each write position in the storage area, such as which positions have been written to and which are still available for subsequent writes. The data to be written can be parameter data, configuration data, or status data generated or updated during device operation. This type of data is typically written frequently but in small amounts each time. The index data can be understood as status marker information for the write process; its content characterizes whether each write position in the storage area has been used, thus providing a basis for judgment in subsequent write operations.

[0023] During the judgment process, by reading the index data in the index area, it can be determined whether there are still free locations in the storage area available for writing. If the index data indicates that all write locations have been used, the storage area is considered to meet the full write condition, and an erase operation needs to be performed in subsequent steps. Otherwise, the storage area is considered not to be full, and data writing can continue without erasure. In this way, it is possible to distinguish whether an erase operation needs to be triggered in advance during the data writing stage, thereby avoiding unnecessary erasure when there is still available space in the storage area.

[0024] As one possible implementation, the specific organization of the index data can be adjusted according to storage capacity and implementation requirements. For example, bit-marking, byte-marking, or other encoding methods that can represent the usage status can be used, as long as they can reflect the usage of the write position and support the judgment of full write conditions.

[0025] 102. When the preset full write condition is not met, determine the target write location in the storage area based on the index data.

[0026] In this embodiment of the invention, when the preset full-write condition is not met, it can be determined that there is still data space available for writing in the current smallest erase / write unit. In this case, there is no need to perform an erase operation; instead, the target write position is directly determined in the storage area based on the index data. In this embodiment, the index data is used to reflect the usage status of each write position in the storage area. By parsing the index data, it can be determined which write positions have not yet been used, thereby selecting a suitable position for this write operation.

[0027] Specifically, the storage area is divided into multiple preset write locations during the configuration phase. Each write location physically corresponds to a contiguous space within the storage area and has a corresponding usage status marker in the index area. When determining the target write location, the index data can be checked according to pre-agreed rules. For example, the usage status of the write locations can be checked sequentially according to their physical order within the storage area. When a write location is detected to be unused, it is determined as the target write location to carry the data to be written.

[0028] In this way, the data writing process can gradually consume available write positions within the same minimum write unit without frequently triggering erase operations, thus making full use of the minimum write unit. At the same time, since the determination of the target write position is entirely based on index data, the writing process has clear controllability, avoiding random overwriting of storage areas and facilitating subsequent management of the write status.

[0029] As one possible implementation, the order rule for determining the target write position is not limited to a strict physical order. Other traversal methods can also be adopted according to actual application requirements, such as selecting according to a preset logical order, a fixed offset interval, or an order related to the write frequency, as long as it can ensure that unused write positions are selected first when the write position is not fully written.

[0030] 103. When the preset full write condition is met, perform an erase operation on the smallest erase / write unit and use the starting position of the storage area as the target write position.

[0031] In this embodiment of the invention, when a preset full-write condition is met, it means that there are no unused write positions in the storage area within the current smallest erase / write unit used for data writing. Continuing to write at this point will not be possible without erasing. Therefore, an erase operation needs to be performed on the smallest erase / write unit to restore its write capability. In this embodiment, the erase operation is performed at the smallest erase / write unit level. One erase operation restores the entire storage content within the smallest erase / write unit to its initial state, making the storage area writable again.

[0032] After the erase operation is complete, all the previously partitioned write positions in the storage area are initially available. At this point, the starting position of the storage area is determined as the target write position to carry the data to be written. By using the starting position as the first write position after the erase, the write process can restart from a fixed starting point in the storage area, ensuring the consistency and predictability of the write order, and also facilitating the re-establishment of the correspondence between the index data and the write position in subsequent steps.

[0033] By triggering an erase operation only when the write condition is met, and then starting a new write cycle from the beginning of the storage area after the erase, a single erase operation can be mapped to multiple data write processes, thereby reducing the frequency of erase operations per unit time. This avoids premature erasure when there are still available write positions in the storage area, which helps to delay the lifespan of the smallest write unit.

[0034] As one possible implementation, the starting position of the storage area can be the first write position pre-agreed in the smallest erase / write unit, or the first valid write position dynamically determined according to the configuration rules after erasure, as long as it can serve as the starting point of a new round of write process.

[0035] 104. Record the target write position in the index area and write the data to be written to the target write position.

[0036] In this embodiment of the invention, after determining the target write location, index updates and data write operations need to be completed simultaneously to ensure the continuity and manageability of the write process. First, the information corresponding to the target write location can be recorded in the index area. By updating the index data, the index area can reflect the changes in the usage status of the current write location, indicating that the target write location has been occupied. Updating the index data provides a basis for subsequently determining the full write condition and the next write location.

[0037] After completing the recording in the index area, the data to be written is actually written to the target write location. The data to be written can be written to the corresponding storage space according to a pre-agreed data format. During the writing process, there is no need to perform an erase operation on the smallest erase unit, thus avoiding unnecessary erase and write losses. By combining index records with data writing, it can be ensured that each data write has a clear location identifier in the storage area, while the index area always remains consistent with the state of the storage area.

[0038] Through the above steps, the writing process forms a continuous and traceable write sequence within the same smallest write unit, ensuring that data writing operations can be completed smoothly before the full write condition is reached, without destroying the already written data. This writing method not only guarantees the orderliness of data storage but also provides a reliable basis for subsequent full write judgment and erase control.

[0039] As one possible implementation, the order of recording the target write position to the index area and writing the data to be written can be adjusted according to the actual implementation requirements. For example, the data can be written first and then the index data can be updated, or the index information can be updated uniformly after the writing is completed, as long as the consistency between the index data and the actual writing status can be guaranteed.

[0040] It is important to note that in practical applications, the data to be written is typically parameter data, configuration data, or operational status data. This type of data is repeatedly updated during device operation. In traditional storage methods, because the storage area usually employs an overwrite update strategy, each time a parameter or status changes, it is often necessary to first perform an erase operation on the corresponding smallest write unit before writing the new data to a fixed storage location to ensure that the latest data is read. However, since the erase operation can only be performed at the smallest write unit level, this overwrite update method leads to repeated erase triggering when data updates are frequent, thus accelerating the lifespan of the smallest write unit.

[0041] Unlike the methods described above, in this embodiment, parameter data, configuration data, or status data are not updated by repeatedly overwriting the same storage location. Instead, the update process is completed within the same minimum erase / write unit by writing to different write locations multiple times. Whenever data needs to be updated, the original data is not directly erased; instead, the new data is written to an unused target write location, and the corresponding write location status change is recorded through an index area. In this way, multiple copies of historically written data can be sequentially stored in the storage area within a single erase cycle, while the index area is used to identify which write locations have been used and which locations correspond to currently available write space.

[0042] In this write mode, whether an erase operation is needed no longer depends on a single data update, but rather on whether the storage area has been completely filled. An erase operation is only triggered when the index data indicates that all preset write locations have been used, thus initiating a new write cycle. Before the full write condition is met, multiple parameter updates or state changes can be completed by appending to the data, without repeatedly erasing the smallest write unit.

[0043] By managing the write position status through the index region, the data status corresponding to different write positions can be accurately distinguished within the storage area, giving the write process a clear order and boundaries. During subsequent read or management processes, the currently valid data position can be determined based on the index data, thereby obtaining the parameters, configuration, or status information of the latest write operation without overwriting or erasing historical data in the storage area. Thus, while ensuring data manageability, multiple write operations are enabled within a single minimum erase / write unit, effectively reducing the number of erase cycles, lowering erase / write wear, and extending the lifespan of the memory.

[0044] As one possible implementation, the above method is particularly suitable for application scenarios where parameters are updated frequently but the amount of data per update is small, such as repeated adjustments to device configuration parameters, periodic recording of operating status, or dynamic updates of control flags. In these scenarios, different versions of data can be distinguished by selecting the corresponding write position through an index, without having to perform an erase operation every time an update is performed.

[0045] For example, the method of this invention primarily addresses the problem of optimizing the lifespan of flash memory within microcontrollers. In common microcontrollers, flash memory is typically managed in pages as the smallest erase / write unit, with a single page capacity of, for example, 512 bytes. Taking a 64KB flash memory as an example, the entire flash memory space can be divided into approximately 128 pages. In practical engineering applications, at least one page is usually allocated for storing user parameters, configuration data, or operational status data.

[0046] In traditional implementations, the aforementioned user data is often stored in a fixed location within a fixed page. When parameters or status are updated, the update needs to be completed through overwrite. Since overwrite is usually accompanied by erase operations, the page is repeatedly erased when data is frequently updated, thus becoming a weak link affecting the overall lifespan of the flash memory.

[0047] By rationally dividing the storage space within a single page and managing the write status using an index area, user data can be written multiple times within the same page without needing to be erased with each data update. In other words, before a page is completely filled, multiple parameter updates or status changes can be written sequentially to different write locations. Only after all write locations within the page have been used is an erase operation performed, initiating a new write cycle.

[0048] Compared to lifetime optimization schemes using adjacent page rotation storage or multi-page redundant storage, this method only requires one page to complete multiple data update operations. This effectively reduces erase frequency, extends flash memory lifespan, and significantly reduces additional flash memory space usage. It is particularly suitable for microcontroller environments with limited storage resources, ensuring flexibility in parameter updates while reserving more available space for functions such as program code storage or online upgrades.

[0049] As one possible implementation, the page capacity, total flash memory capacity, and number of pages used for user data storage can be adjusted according to the specific microcontroller model and application requirements. The above values ​​are only used to illustrate the application of this method in typical engineering scenarios and do not constitute a limitation on the specific implementation.

[0050] In this embodiment of the invention, when data to be written is acquired, the index data recorded in the index area is used to determine whether the storage area meets a preset full-write condition. If the preset full-write condition is not met, the target write position is determined in the storage area based on the index data. If the preset full-write condition is met, an erase operation is performed on the minimum erase / write unit, and the starting position of the storage area is taken as the target write position. The target write position is recorded in the index area, and the data to be written is written to the target write position. When data to be written is acquired, the index data is used to determine whether the storage area meets the full-write condition, and the erase operation is performed on the minimum erase / write unit only when the full-write condition is met. If the full-write condition is not met, the target write position is directly determined in the storage area and the writing is completed. Therefore, compared with the method of erasing the minimum erase / write unit for each write, the number of erase operation triggers can be reduced, thereby reducing the erase / write wear of the minimum erase / write unit and extending the service life of the memory. At the same time, by recording the target write position in the index area, a basis for determining the subsequent write position can be provided, ensuring the continuity and manageability of the writing process.

[0051] Optionally, the above method can also determine the location of the currently valid data in the storage area based on the index data recorded in the index area when a read request is obtained; and read the data at the location as the read result corresponding to the read request.

[0052] In this embodiment of the invention, upon receiving a read request, the usage status of each write location within the storage area is first analyzed based on the index data recorded in the index area to determine the write location of the currently valid data. The currently valid data typically corresponds to the data most recently written, and its position within the storage area can be directly located using the index data without traversing or comparing the entire storage area.

[0053] After determining the location of the currently valid data, the corresponding data content is read directly from that location, and the read result is returned as the result of this read request. Because the index area is always updated in sync with the write process, the read operation can accurately obtain the latest data version without relying on overwrite writes, thereby avoiding reading historical or invalid data.

[0054] By introducing the above reading method, even if multiple updates of data are stored consecutively within the same smallest write unit, the currently valid data can be quickly and accurately located by relying on the index area's indication of the write status. This reading mechanism, in conjunction with the aforementioned non-overwrite writing method, enables parameter data, configuration data, or runtime status data to reduce erase operations and maintain data read consistency and reliability in scenarios with frequent updates.

[0055] As one possible implementation, the rules for determining the current valid data can be adjusted according to the organization of the index data. For example, the most recently marked as used write position can be used as the current valid position, or the valid data position can be determined among multiple used write positions according to a preset priority rule, as long as the read result is consistent with the most recently written data.

[0056] Optionally, the storage area includes multiple preset write positions, and the index data includes the usage status corresponding to each write position. In the step of determining whether the storage area meets the preset full write condition based on the index data recorded in the index area, the usage status corresponding to each write position in the index data can also be obtained; when the usage status corresponding to each write position is "used", the storage area is determined to meet the full write condition; when the usage status corresponding to any write position is "unused", the storage area is determined not to meet the full write condition.

[0057] In this embodiment of the invention, when all write positions are marked as used, it can be determined that there is no more data space available for writing in the storage area. At this time, the storage area is considered to meet the full write condition, and an erase operation needs to be triggered in subsequent steps. Conversely, as long as there is still any write position in the index data with an unused status, it can be determined that the storage area is not yet full, and data writing can continue without performing an erase operation.

[0058] By using a full-write determination method based on usage status, the process of determining the overall state of the storage area can be transformed into a process of parsing the index data. This avoids directly scanning the data content in the storage area, thereby reducing the complexity of the determination process and improving the efficiency of write control. At the same time, the result of the full-write condition determination comes directly from the state information maintained by the index area, which helps to maintain logical consistency between the write process and erase control.

[0059] As one possible implementation, the representation of the usage state can be extended according to implementation requirements. For example, a binary state can be used to indicate whether a write position is used, or different write stages can be further distinguished within the usage state, as long as reliable judgment of the full write condition can be supported. In addition, the number of write positions can also be flexibly configured according to the capacity of the smallest erase / write unit and the data length to adapt to different memory specifications and application scenarios.

[0060] Optionally, the storage area includes multiple preset write locations, which are arranged in the order of their physical locations within the storage area. In the step of determining the target write location in the storage area based on index data, the usage status of each write location can be detected sequentially based on the index data according to the order of the write locations. When a write location with an unused usage status is detected, the unused write location is determined as the target write location.

[0061] In this embodiment of the invention, the usage status corresponding to each write position in the index data can be read sequentially. Starting from the beginning of the storage area, the corresponding write position is checked one by one in ascending order of physical address to determine whether it has been used. When the usage status of a certain write position is detected as unused, the write position can be determined as the target write position for this write operation to store the data to be written, without needing to continue checking subsequent write positions.

[0062] By employing a detection method based on physical location sequence, the write process can form a continuous and orderly write trajectory within the storage area, avoiding jumpy or randomly distributed write behavior. Sequential writing not only simplifies the management logic of index data but also facilitates rapid determination of full-write conditions in subsequent steps, and establishes clear write cycle boundaries with erase operations.

[0063] As one possible implementation, the order of write positions can be consistent with the physical address order of the storage area, or a fixed traversal order can be formed through logical mapping during the configuration phase, as long as it can be guaranteed that unused write positions are selected first when the storage area is not full, and the erase operation is triggered after the storage area is gradually consumed.

[0064] Optionally, the storage area includes multiple preset write positions, and the index data includes the usage status corresponding to each write position. In the step of performing an erase operation on the smallest erase unit, the data stored at each write position in the storage area can also be erased to restore the storage area to its initial state; the usage status corresponding to each write position stored in the index data can be changed to unused; the above method can also prevent the usage status corresponding to any write position in the index data from being changed from used to unused when the smallest erase unit has not performed an erase operation.

[0065] In this embodiment of the invention, after the erase operation occurs, the original data in each write location within the storage area is cleared, restoring the storage area as a whole to its initial state where it can be rewritten. At the same time, the usage status of each write location recorded in the index area is also uniformly updated to the unused state to reflect that a new round of writing cycle has begun.

[0066] By simultaneously resetting the data content and usage state during the erase operation, it is ensured that the storage area and the index area remain consistent, avoiding situations where data has been erased but the index state has not been updated synchronously, or the index state has been reset but the storage area still retains old data. This synchronous processing method provides a clear initial boundary for subsequent write processes, allowing the write control logic to restart from a defined starting point.

[0067] In addition to the erase operation, to prevent the write state from being manually or abnormally rolled back, updates to the index data are subject to additional constraints when the smallest erase / write unit has not been erased. Specifically, a write position marked as used cannot have its corresponding usage state changed to unused. This constraint ensures that the usage state in the index data changes only in a single direction within an erase cycle, preventing distortion of the write state due to misoperation or abnormal modification. This distortion could affect the accuracy of full-write judgment or write position selection, and consequently, the lifespan of the memory.

[0068] This approach establishes a clear write cycle division within the same smallest write unit: before an erase operation, the usage state of the write position can only accumulate gradually without any rollback; after an erase operation, all write positions and usage states are uniformly reset, entering a new write cycle. This periodic management method helps maintain the stability of the write process and reduces potential risks caused by inconsistent states.

[0069] As one possible implementation, the usage state can be updated immediately after writing is complete, or it can be updated uniformly after the writing process ends, as long as it can be guaranteed that the used state will not be reset to the unused state if an erase operation does not occur. Meanwhile, the triggering condition for the erase operation is still controlled by the full write judgment result and will not occur prematurely due to data changes at a single write location.

[0070] Optionally, the above method can also obtain the storage capacity of the minimum erase / write unit, the length constraint of a single write data, and the data length of the index data recording the usage status of the write position; based on the storage capacity, length constraint, and data length, determine the number of write positions that can be set in the storage area and the number of usage statuses corresponding to the write positions; based on the number of write positions and the number of usage statuses, configure the storage area and the index area in the minimum erase / write unit to be configured to obtain the minimum erase / write unit.

[0071] In this embodiment of the invention, before configuring the minimum erase / write unit, basic parameters related to space partitioning can be obtained to reasonably determine the size of the storage area and the index area without exceeding the storage capacity of the minimum erase / write unit. The storage capacity of the minimum erase / write unit can be denoted as P, representing the minimum storage space size corresponding to one erase operation; the length constraint of a single write operation can be denoted as S, representing the number of bytes required in the storage area for each write of parameter data, configuration data, or status data; the length of the data recording the usage status of the write position in the index data is related to the number of write positions and represents the storage overhead occupied by the index area in the overall space.

[0072] The storage area is configured to contain multiple write locations, denoted as N. Each write location can hold at least S lengths of write data. Therefore, the space occupied by the storage area for writing data is N. S. Meanwhile, the index area is used to record the usage status of each write location. When the index data uses bit-stamping to represent the usage status, each write location corresponds to a usage status bit. Since memory uses bytes as the basic storage unit, the minimum number of bytes required for the index area can be expressed as... N / 8 ,in This indicates rounding up, which is used to ensure that the index area can completely store the usage status of all write positions. Considering the physical characteristics of memory (such as Flash), the division generally needs to be set to a multiple of 8.

[0073] When the storage area and index area are configured within the same minimum erase / write unit, the total space occupied by the data area and index area must not exceed the storage capacity P of the minimum erase / write unit. Therefore, the number of write positions N must satisfy the following capacity constraint:

[0074] By using the above constraints, given the minimum write unit storage capacity P, the single write data length constraint S, and the index data overhead form, the number of write positions N that can be set in the storage area can be determined, and the number of usage states that need to be configured in the index area can be determined simultaneously, thereby avoiding insufficient storage space or index space overflow during the configuration phase.

[0075] In one embodiment, when the number of write locations N is further limited to an integer multiple of 8, the space overhead of the index region can be reduced by... N / 8 Simplifying to N / 8, the above capacity constraint can now be transformed into a group-based calculation. Each group of 8 write positions corresponds to 8S bytes of write data space and 1 byte of index space. Therefore, the maximum number of write positions that can be configured within the total capacity P is:

[0076] Using the above calculation method, the upper limit of the number of write positions that can be supported under capacity constraints can be obtained directly during the configuration stage. Based on this, the storage area and index area are divided in the smallest erase / write unit to be configured, so that the sum of their space occupancy does not exceed the storage capacity of the smallest erase / write unit.

[0077] After determining the number of write positions and the corresponding number of usage states, a storage area can be configured in the minimum erase / write unit to sequentially store the written data, and an index area can be configured to record the usage state of each write position, thereby obtaining a minimum erase / write unit structure that meets the requirements of subsequent data writing methods.

[0078] As one possible implementation, when the single write data length constraint or the recording method of index data changes, the number of write locations can be recalculated based on the above capacity constraint relationship, and the configuration of the storage area and index area can be adjusted accordingly to adapt to different storage needs.

[0079] Optionally, in the step of determining the number of configurable write positions and the number of usage states corresponding to the write positions in the storage area based on storage capacity, length constraints, and data length, the target usage duration and the update frequency of the data to be written can also be obtained; the initial number of write positions and the corresponding initial number of usage states can be determined based on storage capacity, length constraints, and data length; and the initial number of write positions and the corresponding initial number of usage states can be adjusted based on the target usage duration and the update frequency of the data to be written, to obtain the number of configurable write positions and the number of usage states corresponding to the write positions in the storage area.

[0080] In this embodiment of the invention, based on determining the number of write positions according to storage capacity, length constraints and data length, a time dimension parameter related to the usage scenario can be further introduced to adaptively adjust the number of write positions so that the configuration result is more in line with actual usage needs.

[0081] The target usage duration can be understood as the expected usage time of the storage area within a complete erase cycle. For example, under normal operating conditions, the desired time range for maintaining updated configuration parameters or status data without triggering an erase operation. The update frequency of the data to be written can be understood as the number of write operations occurring per unit time, used to characterize the activity level of parameter or status data updates during operation. These two parameters together reflect the pressure level of data writing behavior over time.

[0082] In the actual implementation process, an initial number of write positions and the corresponding initial number of usage states can be determined based solely on storage capacity, the length constraint of a single write operation, and the overhead format of index data. This initial number of write positions satisfies the capacity constraint to ensure that the configuration result is feasible at the spatial level, but it is not necessarily the same as the final number of write positions adopted.

[0083] After obtaining the initial number of write positions, the number can be adjusted based on the target usage duration and the update frequency of the data to be written. For example, when the target usage duration is long and the update frequency is high, the number of write positions can be increased within the capacity constraints, allowing more write operations to be accommodated in a single erase cycle, thereby extending the time interval between erase operations. Conversely, when the update frequency is low or the target usage duration is short, a smaller number of write positions can be selected to reduce the space occupied by the index area and improve the utilization efficiency of the storage area for a single write operation.

[0084] Through the above adjustment process, the final number of write positions and the corresponding number of usage states satisfy both the storage capacity constraint of the minimum erase / write unit and match the time characteristics during actual use, thus achieving a balance between space utilization and lifespan. This adjustment does not change the basic structural form of the write positions and index areas, but rather strategically selects the number of write positions under given structural constraints.

[0085] As one possible implementation, the target usage duration and update frequency can be set during the device initialization phase, or they can be updated during operation based on historical write records or operating modes. After the update, the process of adjusting the number of write positions is re-executed to adapt to the usage requirements of different stages.

[0086] In practical implementation, the target usage duration and update frequency can first be mapped to a unified demand intensity index, and then the number of write locations can be increased or decreased based on the demand intensity index. The mapping process can be completed using a weighted summation method.

[0087] The target usage time is recorded as The update frequency is denoted as The two are then normalized to obtain a dimensionless duration index. With frequency indicators Normalization can be done based on a preset reference value, such as the reference usage time. Compared with the reference update frequency Scaling is performed to obtain , Based on this, a demand intensity index is constructed. ,For example ,in and For the preset weights, and satisfying This is used to express the relative importance of usage duration and update frequency in configuration decisions.

[0088] Subsequently, based on the demand intensity index Number of initial write positions Adjustments were made to obtain the final number of write locations. One implementation is to define the adjustment amount as... The increment is positively correlated, and the number of write locations is increased within the capacity constraint limits, for example... ,in This is the upper limit of the configurable number of write locations, derived from storage capacity and length constraints. This is to round down to the nearest integer to ensure the number of write positions is an integer. In this way, when... When the number is large, the number of write locations increases towards the upper limit. Proximity is increased to extend the number of write cycles that can be tolerated within an erase cycle; when When the number is smaller, the number of write locations is closer. This reduces the space overhead of the index area.

[0089] For example, the upper limit of the number of write locations corresponding to storage capacity and length constraints is... The initial number of write positions is If the target has a long usage time and a high update frequency, it is normalized to obtain... , and take the weight , Then the intensity of demand The number of write positions can then be adjusted to... This allows for an increase in the number of write locations, within the limits of available capacity, thus improving the erase interval. If the target usage duration and update frequency are low, then... Smaller, then Will remain closer As the index level increases, the area occupied by the index also decreases.

[0090] As one possible implementation, the weighted summation is not limited to a linear form. It can also be adjusted using a piecewise function or a threshold strategy. For example, when the update frequency exceeds the first threshold, the number of write locations can be increased to a preset level, and when the target usage time exceeds the second threshold, the level can be further increased. Alternatively, an exponential smoothing method can be used to stabilize the update frequency before participating in the weighting, so as to avoid frequent fluctuations in configuration caused by short-term burst writes.

[0091] It should be understood that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.

[0092] In one embodiment, a data writing device is provided, which corresponds one-to-one with the data writing method in the above embodiments. For example... Figure 2 As shown, the data writing device includes a judgment module 201, a determination module 202, an erasure module 203, and a writing module 204. Detailed descriptions of each functional module are as follows: The judgment module 201 is used to determine whether the storage area meets the preset full write condition based on the index data recorded in the index area when the data to be written is obtained. The determination module 202 is used to determine the target write position in the storage area based on the index data when the preset full write condition is not met. The erasure module 203 is used to perform an erasure operation on the smallest erase unit when a preset full write condition is met, and to take the starting position of the storage area as the target write position; The writing module 204 is used to record the target write position to the index area and write the data to be written to the target write position.

[0093] Optionally, the device further includes: The second determining module is used to determine the location of the currently valid data in the storage area based on the index data recorded in the index area when a read request is received. The reading module is used to read data at the location and use it as the reading result corresponding to the reading request.

[0094] Optionally, the storage area includes multiple preset write locations, the index data includes the usage status corresponding to each write location, and the judgment module 201 is further configured to: Obtain the usage status corresponding to each write position in the index data; When the usage status corresponding to each of the write positions is "used", it is determined that the storage area meets the full write condition; If the usage status corresponding to any of the write positions is unused, it is determined that the storage area does not meet the full write condition.

[0095] Optionally, the storage area includes multiple preset write locations, which are arranged in order of their physical locations within the storage area. The determining module 202 is further configured to: Based on the index data and the order in which the write positions are arranged, the usage status corresponding to each write position is detected sequentially. When a write location is detected as unused, the unused write location is determined as the target write location.

[0096] Optionally, the storage area includes multiple preset write locations, the index data includes the usage status corresponding to each write location, and the erase module 203 is further used for: The data stored at each write location in the storage area is erased to restore the storage area to its initial state. Change the usage status of each write position stored in the index data to unused; The device further includes: The disable module is used to prevent the usage status of any write position in the index data from changing from used to unused when the smallest erase / write unit has not performed an erase operation.

[0097] Optionally, the device further includes: The acquisition module is used to acquire the storage capacity of the minimum erase / write unit, the length constraint of a single write data, and the length of the data in the index data that records the usage status of the write position; The third determining module is used to determine the number of configurable write positions in the storage area and the number of usage states corresponding to the write positions based on the storage capacity, the length constraint and the data length. The configuration module is used to configure the storage area and the index area in the minimum erase / write unit to be configured based on the number of write positions and the number of usage states, so as to obtain the minimum erase / write unit.

[0098] Optionally, the third determining module is further configured to: Obtain the target usage duration and the update frequency of the data to be written; Based on the storage capacity, the length constraint, and the data length, determine the number of initial write locations and the corresponding number of initial usage states; Based on the target usage duration and the update frequency of the data to be written, the number of initial write positions and the corresponding number of initial usage states are adjusted to obtain the number of configurable write positions in the storage area and the number of usage states corresponding to the write positions.

[0099] The modules in the aforementioned data writing device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in the processor of a computer device in hardware form or independent of it, or stored in the memory of a computer device in software form, so that the processor can call and execute the operations corresponding to each module.

[0100] In one embodiment, a computer device is provided, which may be a terminal device, and its internal structure diagram may be as follows: Figure 3 As shown, the computer device includes a processor, memory, and network interface connected via a system bus. The processor provides computing and control capabilities. The memory includes a readable storage medium storing computer-readable instructions. The network interface communicates with external terminals via a network connection. When executed by the processor, the computer-readable instructions implement a data writing method. The readable storage medium provided in this embodiment includes both non-volatile and volatile readable storage media.

[0101] In one embodiment of the application, a readable storage medium is provided, which stores computer-readable instructions. When the computer-readable instructions are executed by a processor, they implement the steps of the data writing method described above.

[0102] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by instructing related hardware with computer-readable instructions. These computer-readable instructions can be stored in a non-volatile readable storage medium or a volatile readable storage medium. When executed, these computer-readable instructions can include the processes of the embodiments of the methods described above. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), RAMbus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.

[0103] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the above-described division of functional units and modules is used as an example. In practical applications, the above functions can be assigned to different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above.

[0104] The above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application, and should all be included within the protection scope of this application.

Claims

1. A data writing method, characterized in that, The method is applied to a memory, the memory including at least one minimum erase / write unit, the minimum erase / write unit including a storage area and a corresponding index area, the method including: When data to be written is obtained, the storage area is determined to meet the preset full write condition based on the index data recorded in the index area. If the preset full write condition is not met, the target write location is determined in the storage area based on the index data; When the preset full write condition is met, an erase operation is performed on the smallest erase / write unit, and the starting position of the storage area is taken as the target write position; The target write position is recorded in the index area, and the data to be written is written to the target write position.

2. The data writing method as described in claim 1, characterized in that, The method further includes: When a read request is received, the location of the currently valid data in the storage area is determined based on the index data recorded in the index area. Data is read from the specified location and used as the read result corresponding to the read request.

3. The data writing method as described in claim 1, characterized in that, The storage area includes multiple preset write locations, and the index data includes the usage status corresponding to each write location. The step of determining whether the storage area meets the preset full-write condition based on the index data recorded in the index area includes: Obtain the usage status corresponding to each write position in the index data; When the usage status corresponding to each of the write positions is "used", it is determined that the storage area meets the full write condition; If the usage status corresponding to any of the write positions is unused, it is determined that the storage area does not meet the full write condition.

4. The data writing method as described in claim 1, characterized in that, The storage area includes multiple preset write locations, which are arranged in order of their physical location within the storage area. Determining the target write location within the storage area based on the index data includes: Based on the index data and the order in which the write positions are arranged, the usage status corresponding to each write position is detected sequentially. When a write location is detected as unused, the unused write location is determined as the target write location.

5. The data writing method as described in claim 1, characterized in that, The storage area includes multiple preset write locations, and the index data includes the usage status corresponding to each write location. The erase operation performed on the smallest erase / write unit includes: The data stored at each write location in the storage area is erased to restore the storage area to its initial state. Change the usage status of each write position stored in the index data to unused; The method further includes: When the smallest erase / write unit does not perform an erase operation, the usage status corresponding to any write position in the index data is prohibited from changing from used to unused.

6. The data writing method according to any one of claims 1 to 5, characterized in that, The method further includes: Obtain the storage capacity of the minimum write unit, the length constraint of a single write, and the length of the data in the index data that records the usage status of the write position; Based on the storage capacity, the length constraint, and the data length, determine the number of write locations that can be set in the storage area and the number of usage states corresponding to the write locations; Based on the number of write positions and the number of usage states, the storage area and the index area are configured in the minimum erase / write unit to be configured, thus obtaining the minimum erase / write unit.

7. The data writing method as described in claim 6, characterized in that, The step of determining the number of configurable write positions in the storage area and the number of usage states corresponding to each write position based on the storage capacity, the length constraint, and the data length includes: Obtain the target usage duration and the update frequency of the data to be written; Based on the storage capacity, the length constraint, and the data length, determine the number of initial write locations and the corresponding number of initial usage states; Based on the target usage duration and the update frequency of the data to be written, the number of initial write positions and the corresponding number of initial usage states are adjusted to obtain the number of configurable write positions in the storage area and the number of usage states corresponding to the write positions.

8. A data writing device, characterized in that, The device is applied to a memory, the memory including at least one minimum erase / write unit, the minimum erase / write unit including a storage area and a corresponding index area, the device comprising: The judgment module is used to determine whether the storage area meets the preset full write condition based on the index data recorded in the index area when the data to be written is obtained. The determination module is used to determine the target write location in the storage area based on the index data when the preset full write condition is not met. The erase module is used to perform an erase operation on the smallest erase / write unit when a preset full write condition is met, and to take the starting position of the storage area as the target write position; The writing module is used to record the target write position to the index area and write the data to be written to the target write position.

9. A computer device comprising a memory, a processor, and computer-readable instructions stored in the memory and running on the processor, characterized in that, When the processor executes the computer-readable instructions, it implements the data writing method as described in any one of claims 1 to 7.

10. A readable storage medium having computer-readable instructions stored thereon, characterized in that, When the computer-readable instructions are executed by the processor, they implement the data writing method as described in any one of claims 1 to 7.