Method for storing data in FLASH in real time
By using task scheduling and real-time operating systems in embedded systems, creating high-priority write tasks and low-priority erase tasks, the problem of conflicts in the existing technology of host computer monitoring and erase operation during FLASH storage is solved, and automatic real-time storage of data in FLASH and maximum retention of historical data is achieved.
Patent Information
- Application Number
- CN202411853344.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-16
- Publication Date
- 2025-04-29
AI Technical Summary
When using FLASH storage in embedded systems, the upper computer needs to continuously monitor the storage space, making it difficult to ensure that historical data is retained to the maximum extent during erasing, and writing operations cannot be performed during erasing, which may lead to failure of real-time data storage.
Through task scheduling, high-priority write tasks and low-priority erase tasks are created, and inter-task scheduling is used to avoid conflicts between writes and erase operations, so as to realize that the embedded system automatically stores data in FLASH in real time.
Without the need for upper computer control, the automatic real-time storage of data in FLASH is realized, which ensures the priority of write operations, avoids conflicts between write and erase operations, and retains historical data to the greatest extent.
Smart Images

Figure CN119902705A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a method for storing data in real time in FLASH, belonging to the technical field of embedded systems. Background Art
[0002] At present, the erase instruction of FLASH by embedded program is usually issued by the host computer. This operation has the following main disadvantages: first, the host computer needs to keep paying attention to the remaining storage space of FLASH to avoid the situation where the latest data cannot be stored; second, it is difficult to ensure that historical data is retained to the maximum extent during erasure; third, the write operation cannot be performed when erasing FLASH, and it is difficult for the host computer to avoid the problem of possible storage failure of real-time data. Summary of the invention
[0003] The technical problem solved by the present invention is: to overcome the deficiencies of the prior art and provide a method for storing data in FLASH in real time, without the need for host computer control, by avoiding conflicts between write and erase operations through task scheduling, thereby realizing automatic real-time data storage in FLASH by the embedded system.
[0004] The technical solution of the present invention is: a method for storing data in real time in FLASH, comprising:
[0005] Initialization phase: Erase the NowSector sector to be written and the next adjacent sector, and initialize the EraseFlag to 0;
[0006] Task scheduling stage: creating a write task and an erase task, and performing inter-task scheduling through a real-time operating system; the write task has a higher priority than the erase task;
[0007] The erasing task includes:
[0008] S11, determine whether the erase enable flag EraseFlag is 1; if the erase enable flag EraseFlag is 1, enter S12; otherwise, use the real-time operating system to suspend the erase task for a time slice and then re-execute S11;
[0009] S12, erase the EraseSector sector, and then set the erase enable flag EraseFlag to 0 to turn off the erase operation; then use the real-time operating system to suspend the erase task for a time slice and enter S11;
[0010] The writing task includes:
[0011] Determine whether the remaining space of the sector currently to be written is less than the length of the real-time data to be stored; if not, write the real-time data to FLASH; if less, change the storage sector to the next adjacent sector, start the erase operation and write the real-time data to FLASH.
[0012] Furthermore, the initialization phase includes:
[0013] Traverse the sectors. If the previous sector header of a sector with an unmarked header has been written with a preset mark, the unmarked sector is the NowSector sector currently to be written, and the next adjacent sector of the NowSector sector is the EraseSector sector; erase the NowSector sector and the EraseSector sector; initialize the erase enable flag EraseFlag to 0.
[0014] Furthermore, the step of writing the task includes:
[0015] S31, determine whether the data stored starting from the current address ADDR will cross sectors, if it does, execute S32, if it does not, write real-time data to FLASH; use the real-time operating system to suspend the writing task for a time slice, and then execute S31;
[0016] S32, perform cross-region processing, and then execute S33;
[0017] S33, set the erase enable flag EraseFlag to 1 to enable the erase operation; write a preset mark to the header of the NowSector sector, and update the current address ADDR to the address after the address stored in the preset mark; write real-time data to FLASH; use the real-time operating system to suspend the write task for a time slice, and then execute S31.
[0018] Furthermore, the cross-region processing steps are:
[0019] S41, judge: if the NowSector sector is the last sector, change the NowSector sector to the first sector and execute step S43; otherwise, add 1 to the NowSector sector and enter step S42;
[0020] S42, judging: if the changed NowSector sector is the last sector, then the EraseSector sector is changed to the first sector; otherwise, executing S43;
[0021] S43, the EraseSector sector is changed to the next sector adjacent to the NowSector sector.
[0022] Further, writing real-time data to FLASH includes:
[0023] Write data of length LENGTH starting from the current address ADDR. After writing is completed, the current address ADDR is incremented by LENGTH to point to the next address to be written.
[0024] Furthermore, the preset mark 0xA5A5 is written into the sector header to indicate that the sector has stored real-time data.
[0025] Furthermore, the initialization phase searches for the sector that most recently stored the real-time data through the preset mark.
[0026] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of a method for storing data in real time in FLASH are implemented.
[0027] A FLASH storage device comprises a FLASH memory, a processor and a computer program stored in the FLASH memory and executable on the processor. When the processor executes the computer program, the steps of a method for storing data in real time in FLASH are implemented.
[0028] The advantages of the present invention compared with the prior art are:
[0029] The present invention does not require the host computer to send an erase instruction. The method of sequential cyclic data storage is adopted to ensure that there is no situation where there is insufficient space to store new data. At the same time, the next adjacent sector of the write sector is erased. At this time, the erased data is the data that has been stored for the longest time, so as to retain the latest historical data to the maximum extent; the priority of the write task is higher than that of the erase task, which can ensure that the write operation takes precedence over the erase operation, and ensure the real-time data storage. Furthermore, although the erase task has a low priority, the time required to store a sector is sufficient for erasing the next adjacent sector. The operating system will definitely find the write task suspension gap to execute the erase task, thereby avoiding the conflict between the write and erase operations, and realizing the embedded system to automatically store data in FLASH in real time. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present invention. Moreover, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:
[0031] Figure 1 This is a flowchart of the FLASH writing task of the present invention;
[0032] Figure 2 This is a flowchart of the FLASH erasing task of the present invention;
[0033] Figure 3 Schematic diagram of data storage. DETAILED DESCRIPTION
[0034] In order to better understand the above technical scheme, the technical scheme of the present invention is described in detail below through the accompanying drawings and specific embodiments. It should be understood that the embodiments of the present invention and the specific features in the embodiments are detailed descriptions of the technical scheme of the present invention, rather than limitations on the technical scheme of the present invention. In the absence of conflict, the embodiments of the present invention and the technical features in the embodiments can be combined with each other.
[0035] The following is a further detailed description of a method for storing data in FLASH in real time provided by an embodiment of the present invention in conjunction with the accompanying drawings of the specification. The specific implementation method may include: the FLASH erase operation is automatically executed by the embedded system, and the data is stored in a circular manner in address order. At the same time, the next sector adjacent to the sector being written is erased. The real-time operating system is used to schedule the erase operation and the storage operation, create a low-priority erase task and a high-priority write task, and store data in real time.
[0036] The solution provided in the embodiment of the present invention specifically includes the following steps:
[0037] Initialization phase: traverse the sectors. If the previous sector header of an unmarked sector is written with the preset mark 0xA5A5, then the unmarked sector is the NowSector sector currently to be written, and the next adjacent sector of the NowSector sector is the EraseSector sector; erase the NowSector sector and the EraseSector sector. The erase enable flag EraseFlag is initialized to 0;
[0038] Task scheduling stage: Create high-priority write tasks and low-priority erase tasks, and schedule tasks through the real-time operating system.
[0039] like Figure 1 , Figure 3 In the task scheduling phase, the steps of writing the task are as follows:
[0040] (a1) Determine whether the data with a length of LENGTH stored starting from the current address ADDR will cross sectors. If not, proceed to step (a6); if yes, proceed to step (a2);
[0041] (a2) Determination: If the NowSector sector is the last sector, change the NowSector sector to the first sector and execute step (a4); otherwise, increment the NowSector sector by 1 and enter step (a3);
[0042] (a3) Determination: If the changed NowSector sector is the last sector, the EraseSector sector is changed to the first sector and step (a5) is executed; otherwise, step (a4) is executed;
[0043] (a4) The EraseSector sector is changed to the next sector adjacent to the NowSector sector, and step (a5) is executed;
[0044] (a5) Set the erase enable flag EraseFlag to 1 to enable the erase operation; the header of NowSector is marked as 0xA5A5, and ADDR is updated to the first address of the NowSector sector plus 2 under the principle of storing one byte at one address; go to step (a6);
[0045] (a6) Write real-time data of length LENGTH starting from the current address ADDR. After writing is completed, the current address ADDR is incremented by LENGTH to point to the address to be written next time. Use the real-time operating system to suspend the writing task for a time slice and then enter (a1).
[0046] like Figure 2 In the task scheduling stage, the erasing task steps are as follows:
[0047] (b1) Determine whether EraseFlag is 1. If EraseFlag is 1, proceed to step (b2). Otherwise, use the real-time operating system to suspend a time slice and then execute step (b1) again;
[0048] (b2) EraseSector, and then set EraseFlag to 0 to disable the erase operation. Finally, use the real-time operating system to suspend a time slice and then enter step (b1).
[0049] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage and optical storage, etc.) containing computer-usable program code.
[0050] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0051] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0052] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0053] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
[0054] The contents not described in detail in the specification of the present invention belong to the common knowledge of those skilled in the art.
Claims
1. A method for storing data in real time in FLASH, characterized in that: include: Initialization phase: Erase the NowSector sector to be written and the next adjacent sector, and initialize the EraseFlag to 0; Task scheduling stage: creating a write task and an erase task, and performing inter-task scheduling through a real-time operating system; the priority of the write task is higher than that of the erase task; The erasing task includes: S11, determine whether the erase enable flag EraseFlag is 1; if the erase enable flag EraseFlag is 1, enter S12; otherwise, use the real-time operating system to suspend the erase task for a time slice and then re-execute S11; S12, erase the EraseSector sector, and then set the erase enable flag EraseFlag to 0 to turn off the erase operation; then use the real-time operating system to suspend the erase task for a time slice and enter S11; The writing task includes: Determine whether the remaining space of the sector currently to be written is less than the length of the real-time data to be stored; if not, write the real-time data to FLASH; if less, change the storage sector to the next adjacent sector, start the erase operation and write the real-time data to FLASH.
2. A method for storing data in FLASH in real time according to claim 1, characterized in that: The initialization phase includes: Traverse the sectors. If the previous sector header of a sector with an unmarked header has been written with a preset mark, the unmarked sector is the NowSector sector currently to be written, and the next adjacent sector of the NowSector sector is the EraseSector sector; erase the NowSector sector and the EraseSector sector; initialize the erase enable flag EraseFlag to 0.
3. A method for storing data in FLASH in real time according to claim 1, characterized in that: The steps of writing the task include: S31, determine whether the data stored starting from the current address ADDR will cross sectors, if it does, execute S32, if it does not, write real-time data to FLASH; use the real-time operating system to suspend the writing task for a time slice, and then execute S31; S32, perform cross-region processing, and then execute S33; S33, set the erase enable flag EraseFlag to 1 to enable the erase operation; write a preset mark to the header of the NowSector sector, and update the current address ADDR to the address after the address stored in the preset mark; write real-time data to FLASH; use the real-time operating system to suspend the write task for a time slice, and then execute S31.
4. A method for storing data in FLASH in real time according to claim 3, characterized in that: The cross-region processing steps are: S41, judging: if the NowSector sector is the last sector, then changing the NowSector sector to the first sector, and executing step S43; Otherwise, the NowSector sector is incremented by 1, and the process goes to step S42; S42, judging: if the changed NowSector sector is the last sector, then the EraseSector sector is changed to the first sector; otherwise, executing S43; S43, the EraseSector sector is changed to the next sector adjacent to the NowSector sector.
5. A method for storing data in FLASH in real time according to claim 3, characterized in that: Writing real-time data to FLASH includes: Write data of length LENGTH starting from the current address ADDR. After writing is completed, the current address ADDR is incremented by LENGTH to point to the next address to be written.
6. A method for storing data in FLASH in real time according to claim 2 or 3, characterized in that: The preset mark 0xA5A5 is written into the sector header to indicate that the sector has stored real-time data.
7. A method for storing data in FLASH in real time according to claim 6, characterized in that: The initialization stage searches for the sector that stored the real-time data most recently through the preset mark.
8. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
9. A FLASH storage device, comprising a FLASH memory, a processor, and a computer program stored in the FLASH memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.
Citation Information
Cited By
Method and device for offline storage of real-time sensing data based on Flash ROM in single-chip microcomputer
CN121455423A
A method and device for storing real-time sensing data based on off-line storage of internal flash ROM of a single-chip microcomputer
CN121455423B