A NOR flash-based log text rolling storage method and device
By using NOR Flash in a small MCU system and dividing it into an index area and a log text area, the problems of log storage affecting system performance and shortening Flash lifespan are solved, achieving efficient storage and retrieval and extending the lifespan of the Flash.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- GUANGZHOU ROBUSTEL CO LTD
- Filing Date
- 2024-06-03
- Publication Date
- 2026-07-14
AI Technical Summary
In small MCU systems, existing log text storage methods affect system performance and real-time performance, and shorten Flash lifespan, making it impossible to effectively avoid log loss.
NOR Flash is used to store log text, and the index area and log text area are divided. The log text position is recorded through index information, which reduces the number of erases, improves storage and reading efficiency, and balances Flash wear.
It improves the efficiency of log text storage and retrieval, ensures the performance and real-time performance of the MCU system, extends the lifespan of Flash memory, and avoids log loss.
Smart Images

Figure CN118503217B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a method and apparatus for rolling storage of log text based on NOR Flash. Background Technology
[0002] During the operation of a software system, bugs are inevitable. The most common way to solve bugs is to locate the problem by analyzing the logs recorded during system operation. However, in some small MCU systems, resources are usually limited, and the required functions need to be implemented with minimal hardware cost and maximum computing efficiency. It is not feasible to use some readily available and complex file systems to quickly and efficiently store log text.
[0003] The most common method for storing log text is to write the log content to a file. However, this method requires the system to mount a file system, which consumes additional system and hardware resources for small MCU systems. Moreover, due to the characteristics of the file system, log content is usually cached in RAM when written to the file. It is only written to Flash when the content in RAM reaches a certain amount. This can lead to logs not being written to Flash in a timely manner, and when the system loses power or is unexpectedly reset, logs from the most recent period may be lost.
[0004] However, to ensure that log content is written to Flash promptly each time, the file system needs to be refreshed or saved after each write. This results in the underlying file system logic erasing and writing multiple sectors for each log write (tens of bytes), reducing Flash lifespan. Furthermore, erasing sectors is very time-consuming, typically taking tens to 100 milliseconds, impacting system performance and real-time capabilities.
[0005] CN113986842A discloses a method for real-time database log rolling storage, including setting archive file rolling parameters; applying archive file rolling parameters; setting log file rolling parameters; and setting a log management mechanism. The archive file is rolled back when the available space on the disk containing the archive file is less than the minimum available disk space; when the disk usage rate reaches the maximum disk usage rate; and when the percentage of available disk space to total space is less than the minimum reserved disk space percentage. By setting these two parameters, the rolling storage of archive files is controlled, solving the data storage problem. This method dynamically stores log text by setting parameters and mechanisms; however, in small MCU systems, this method consumes certain resources, affects system performance, and is not conducive to the development of small MCU systems.
[0006] The technical problem that this invention aims to solve is: how to improve the efficiency of log text storage, avoid affecting the performance and real-time performance of the MCU system, and improve the lifespan of Flash memory. Summary of the Invention
[0007] The main objective of this invention is to provide a method for rolling log text storage based on NOR Flash. By setting up an index area and a log text area, the log text can be directly read through the index information, improving the storage and retrieval efficiency of log text. Furthermore, this method reduces the number of Flash erase cycles, extending the Flash's lifespan. Simultaneously, a rolling log text storage device based on NOR Flash is also provided.
[0008] To achieve the above objectives, the technical solution adopted in this application is as follows:
[0009] A method for scrolling log text storage based on NOR Flash includes the following steps:
[0010] Step 1: Divide the log text storage area into an index area and a log text area; the index area and the log text area each contain multiple sectors; the index area is used to store the index information of the log text so that the location of the log text can be found; the log text area is used to store the log text; the log text has a start position and an end position;
[0011] Step 2: Determine if the remaining space in the sector where the end position of the log text area is located is sufficient to write log text; if it is sufficient, start writing log text from the position after the last end position.
[0012] If not satisfied, proceed to step 3;
[0013] Step 3: Determine if the next sector of the sector where the end position is located is the sector where the start position of the log text is located; if not, proceed to step 4; if yes, proceed to step 5.
[0014] Step 4: Erase the sector following the sector where the end position is located, start writing log text from the position following the previous end position, update the index information, and roll the updated index information to the index area.
[0015] Step 5: Update the starting position and index information, and write the updated index information to the index area in a rolling manner; erase the sector following the sector where the ending position is located, start writing log text from the position following the previous ending position, update the index information, and write the updated index information to the index area in a rolling manner again.
[0016] Preferably, the updated index information is rolled into the index area as follows: each sector of the index area includes multiple locations for storing index information, the updated index information is written to the next location after the location where the original index information was stored, and the original index information is cleared.
[0017] Preferably, after the index information has been written to the position of one sector, the next sector is erased, the updated index information is written, and the original index information is cleared; when the index information is written to the last sector of the index area, the next time it returns to the first sector and starts writing again in a loop.
[0018] Preferably, the index information includes the sector number of the start position of the log text, the sector number of the end position, FLAG, and CRC checksum; the value of FLAG is set not to be all 0x00 or 0xFF.
[0019] Preferably, the starting position is calculated as the sector number of the starting position multiplied by the sector size; the ending position is calculated as the sector number of the ending position multiplied by the sector size + X.
[0020] Where X is the position of the last non-0xFF value.
[0021] Preferably, if it is the first time using or restarting, initialization is required, specifically: traversing the index information of the index area; if the index information passes the CRC check, the index information is recorded for the next writing of log text; if no index information passes the CRC check, the index area and log text area are erased, and the initialization index information is written in the first sector of the index area.
[0022] Preferably, the number of sectors in the index area * sector size / 16 is greater than or equal to the number of sectors in the log text area * 2.
[0023] Preferably, when it is necessary to read log text, the specific operation is as follows: if the end position is after the start position, then read the content between the start position and the end position; if the end position is before the start position, then it is necessary to read it in two parts: first read the content from the start position to the end position of the log text area, and then read the content from the start position of the log text area to the end position of the log.
[0024] in,
[0025] NOR Flash is a type of flash memory used to store program code, firmware, operating systems, and other data. It has lower storage density and higher cost, but offers faster read speeds, lower read latency, and better random access performance.
[0026] CRC (Cyclic Redundancy Check) is a fast algorithm that generates a short, fixed-length check code based on data such as network packets or computer files. It is primarily used to detect or verify errors that may occur during data transmission or storage. CRC utilizes the principles of division and remainders to achieve error detection, and it has advantages such as clear principles and simple implementation.
[0027] Additionally, a NOR Flash-based log text scrolling storage device is also provided, comprising the following units:
[0028] Partition unit: used to divide the log text storage area into an index area and a log text area;
[0029] First judgment unit: Determine whether the remaining space in the sector where the end position of the log text area is located is sufficient to write log text; if it is sufficient, start writing log text from the position after the last end position;
[0030] Second judgment unit: Determine whether the next sector of the sector where the end position is located is the sector where the log text begins;
[0031] First write unit: Erase the sector following the sector where the end position is located, start writing log text from the position following the last end position, update index information, and roll the updated index information to the index area.
[0032] The second writing unit updates the starting position and index information, and writes the updated index information to the index area in a rolling manner; it erases the sector following the sector where the ending position is located, and starts writing log text from the position following the previous ending position, updates the index information, and writes the updated index information to the index area in a rolling manner.
[0033] Preferably, it further includes a reading unit; the reading unit is used to read log text.
[0034] Compared with existing technologies, this solution has the following advantages:
[0035] The rolling storage method for log text in this case uses NOR Flash to store log text and sets up an index area and a log text area. The position of the log text is recorded through the index information. When it needs to be read, the position of the log text can be found through the index information and read directly. Moreover, the next sector is used only after the capacity of the previous sector is exhausted, which reduces the number of sectors that need to be erased each time the log text is written, improves the efficiency of storage and reading, and does not require mounting a file system, which can ensure the performance and real-time performance of the MCU system.
[0036] Furthermore, by setting up an index area and a log text area, and specifically setting the number of sectors in the index area and the log text area, the index area and the log text area can have similar erase counts. Moreover, by storing index information and log text in a rolling manner, the Flash wear is evened out, thus improving the lifespan of the Flash.
[0037] Secondly, by writing the log text directly to Flash using this method, not only is it possible to avoid erasing a large number of sectors at the same time, thus avoiding impacting efficiency, but it also avoids the loss of recent logs when the system loses power or is accidentally reset. Attached Figure Description
[0038] Figure 1 This is a flowchart of the log text scrolling storage method in Example 1;
[0039] Figure 2 This is a schematic diagram showing the log text before and after writing in Example 1;
[0040] Figure 3 This is a schematic diagram showing the index information before and after the update in Example 1;
[0041] Figure 4 This is a flowchart of the initialization process for Example 1;
[0042] Figure 5 This is a flowchart of log text reading in Example 1;
[0043] Figure 6 This is a schematic diagram of the log text scrolling storage device of Embodiment 2. Detailed Implementation
[0044] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. The components of this application implemented as described and shown in the accompanying drawings can generally be arranged and designed in various different configurations.
[0045] Example 1
[0046] refer to Figure 1 A method for rolling storage of log text based on NOR Flash includes the following steps:
[0047] Step 1: Divide the log text storage area into an index area and a log text area; the index area and the log text area each contain multiple sectors; the index area is used to store the index information of the log text so that the location of the log text can be found; the log text area is used to store the log text; the log text has a start position and an end position;
[0048] In this embodiment, the method is mainly applied to small MCU systems. Small MCU systems are usually resource-constrained and need to achieve the required functions with the minimum hardware cost and the highest computing efficiency. Therefore, it is necessary to directly store the log text in Flash so that the log text can be retrieved to solve the bug.
[0049] The log text storage area is divided into an index area and a log text area. The index area stores index information, including the sector number of the start and end positions of the retrieved log text, and a CRC checksum. The system can locate the stored position of the log text using the sector numbers of the start and end positions in the index information, thereby enabling the retrieval of the log text.
[0050] If this method has already been used, then when you need to write to the log text, proceed directly to step 2;
[0051] If this method is being used for the first time or upon restarting, initialization is required. Specifically: First, the index information in the index area is traversed. If any index information passes the CRC check, it indicates that the system has been initialized, and this index information only needs to be recorded for the next log text write. If no index information passes the CRC check, it indicates that the system has not been initialized. The index area and log text area are erased, and initialization index information is written to the first sector of the index area, meaning writing starts from the first sector. The start and end positions of the log text are then obtained through the initialization index information. By performing CRC check first, log text data loss due to forgetfulness or error during initialization is avoided. Furthermore, when the system is powered on again, the previous log text storage location is retrieved by traversing the index area, thus enabling continuous writing of log text.
[0052] Step 2: Determine if the remaining space in the sector where the end position of the log text area is located is sufficient to write log text; if it is sufficient, start writing log text from the position after the last end position.
[0053] In this embodiment, since the log text is determined based on the start and end positions, the new log text is written starting from the position after the end position of the old log text, and then the end position of the new log text is recorded in RAM, so that the system can know the position of the complete log text when reading the log text later.
[0054] Secondly, since they are stored in the same sector and the starting position has not changed, there is no need to update the starting position.
[0055] If not satisfied, proceed to step 3;
[0056] Step 3: Determine if the next sector of the sector where the end position is located is the sector where the start position of the log text is located; if not, proceed to step 4; if yes, proceed to step 5.
[0057] In this embodiment, the method uses rolling storage. Therefore, there is a possibility that the previous log text may be overwritten. When the log text is overwritten, the starting position of the log text needs to be updated in RAM to prevent the system from being unable to locate the complete log text.
[0058] Step 4: Erase the sector following the sector where the end position is located, start writing log text from the position following the previous end position, update the index information, and roll the updated index information to the index area.
[0059] In this embodiment, the sector following the end position is not the sector where the log text begins. Therefore, the start position of the log text remains unchanged and does not need to be updated. However, when the log text starts writing from the sector following the previous end position to the sector following the end position, not only does the end position change, but the sector number of the end position in the index information also changes. Therefore, it is necessary to update the end position and the index information. The end position is recorded in RAM, and the index information is recorded in the index area. The index information in the index area is written by rolling, which reduces the number of sectors that need to be erased when writing the index information and the log text, thus improving writing efficiency.
[0060] Step 5: Update the starting position and index information, and write the updated index information to the index area in a rolling manner; erase the sector following the sector where the ending position is located, start writing log text from the position following the previous ending position, update the index information, and write the updated index information to the index area in a rolling manner again.
[0061] In this embodiment, the sector following the sector where the end position is located is the sector where the log text begins. Therefore, when erasing the next sector of the log text area, the starting position of the log text is cleared. To avoid read errors, the new starting position of the log text needs to be recorded in RAM first. The new starting position is the first position in the next sector after the erased sector. Since the sector where the starting position is located has changed, the index information also needs to be updated simultaneously and rolled into the index area. Similar to step 4, when the log text starts writing from the position following the previous end position to the sector following the sector where the end position is located, not only does the end position change, but the sector number where the end position is located in the index information also changes. Therefore, the end position and index information need to be updated, the end position is recorded in RAM, and the index information is rolled into the index area again. In step 5, since the index information is updated twice, the index information moves two positions.
[0062] This method enables the cyclic writing of index information and log text. Moreover, the index information is only updated and rolled over when the sector where the log text is written changes, which greatly reduces the number of sectors that need to be erased each time the log text is written, thus improving the efficiency of storing log text.
[0063] Preferably, the updated index information is rolled into the index area as follows: each sector of the index area includes multiple locations for storing index information, the updated index information is written to the next location after the location where the original index information was stored, and the original index information is cleared.
[0064] In this embodiment, a sector has n locations where index information can be stored. After each update, the index information is written to the next location. For example, if the original index information is at location 1, the updated index information is written to location 2, and so on, until the last location of the sector.
[0065] Preferably, after the index information has been written to the position of one sector, the next sector is erased, the updated index information is written, and the original index information is cleared; when the index information is written to the last sector of the index area, the next time it returns to the first sector and starts writing again in a loop.
[0066] In this embodiment, when all positions in the original sector have been written with index information, the next sector is erased, and writing begins from the first position of the next sector, while the original index information is cleared. When the last sector is completely used up, writing continues from the first sector, thus achieving cyclical rolling writing of index information.
[0067] Preferably, the starting position is calculated as the sector number of the starting position multiplied by the sector size; the ending position is calculated as the sector number of the ending position multiplied by the sector size + X.
[0068] Where X is the position of the last non-0xFF value.
[0069] In this embodiment, the log text content cannot be 0xFF, therefore, the end position can be clearly known. Sector numbers are counted starting from 0. A characteristic of NOR Flash is that after erasing the Flash, all content in the entire sector is 0xFF. When no index information is written to a specific location, the data is all 0xFF. When it is necessary to delete the index information, a string of 0x00 is written to the corresponding index information location. For valid index information, it is only necessary to ensure that the FLAG is not entirely 0x00 or 0xFF, because the sector number of the start position, the sector number of the end position, and the CRC checksum can all be 0xFF or 0x00. If the FLAG is entirely 0xFF or 0x00, then the index information may be entirely 0xFF or 0x00, making it impossible to determine the validity of the index information. Therefore, simply setting the FLAG to a value that is not entirely 0x00 or 0xFF can avoid this situation. 0x00 and 0xFF are hexadecimal representations.
[0070] Therefore, when the system reads the log text, it only needs to read the last non-0xFF value.
[0071] Preferably, the number of sectors in the index area * sector size / 16 is greater than or equal to the number of sectors in the log text area * 2.
[0072] In this embodiment, the index information size is 16 bytes, and the number of times each sector of the index area can write index information is the sector size / 16. Therefore, by setting the number of sectors in the index area * sector size / 16 to equal the number of sectors in the log text area * 2, the number of erasures in the index area and the log text area can be made approximately the same, thereby balancing the wear of the index area and the log text area and improving the lifespan of the Flash.
[0073] Preferably, when it is necessary to read log text, the specific operation is as follows: if the end position is after the start position, then read the content between the start position and the end position; if the end position is before the start position, then it is necessary to read it in two parts: first read the content from the start position to the end position of the log text area, and then read the content from the start position of the log text area to the end position of the log.
[0074] In this embodiment, if the end position is after the start position, the log text is read sequentially. However, if the end position is before the start position, the content from the start position to the end of the log text area needs to be read first, and then the content from the beginning of the log text area to the end of the log text area needs to be read to avoid the log text data being read in the wrong order, which would affect the staff's analysis.
[0075] Example 2
[0076] A NOR Flash-based log text scrolling storage device includes the following units:
[0077] Partition unit: used to divide the log text storage area into an index area and a log text area;
[0078] First judgment unit: Determine whether the remaining space in the sector where the end position of the log text area is located is sufficient to write log text; if it is sufficient, start writing log text from the position after the last end position;
[0079] Second judgment unit: Determine whether the next sector of the sector where the end position is located is the sector where the log text begins;
[0080] First write unit: Erase the sector following the sector where the end position is located, start writing log text from the position following the last end position, update index information, and roll the updated index information to the index area.
[0081] The second writing unit updates the starting position and index information, and writes the updated index information to the index area in a rolling manner; it erases the sector following the sector where the ending position is located, and starts writing log text from the position following the previous ending position, updates the index information, and writes the updated index information to the index area in a rolling manner.
[0082] Preferably, it further includes a reading unit; the reading unit is used to read log text.
[0083] In this embodiment, the specific workflow of this storage device is as follows: First, the partitioning unit divides the log text storage area into an index area and a log text area; the index area stores index information, and the log text area stores log text.
[0084] When log text needs to be written, the first judgment unit checks whether the remaining space in the sector where the end position of the log text area is located is sufficient for writing log text. If it is sufficient, the log text is written starting from the next position after the previous end position, and the end position is updated in RAM, indicating successful log text writing. If it is not sufficient, the first judgment unit sends a signal to the second judgment unit, which determines whether the sector after the end position is the sector where the log text begins. If not, the second judgment unit sends a signal to the first writing unit, which erases the sector after the end position and starts writing log text starting from the next position after the previous end position, updating the end position and index information (end bit). If the sector number of the starting position is set, the end position is recorded in RAM, and the updated index information is rolled into the index area, at which point the log text is successfully written. If not, the second judgment unit sends a signal to the second writing unit, and the second writing unit updates the starting position and index information of the log text (the sector number of the starting position), records the starting position in RAM, and rolls the updated index information into the index area. After the update is completed, the next sector of the sector where the end position is located is erased, and the log text is written from the next position after the previous end position. At this time, the end position and index information (the sector number of the end position) are updated, the end position is recorded in RAM, and the updated index information is rolled into the index area.
[0085] When it is necessary to read the log text, the reading unit determines the relationship between the end position and the start position. If the end position is after the start position, the content between the start position and the end position is read. If the end position is before the start position, it needs to be read in two steps: first, the content from the start position to the end of the log text area is read, and then the content from the start position to the end position of the log text area is read.
[0086] Although embodiments of the invention have been shown and described, those skilled in the art will understand that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the claims and their equivalents.
Claims
1. A method for scrolling storage of log text based on NOR FLASH, characterized in that, Includes the following steps: Step 1: Divide the log text storage area into an index area and a log text area; the index area and the log text area each contain multiple sectors; the index area is used to store the index information of the log text so that the location of the log text can be found; the log text area is used to store the log text; the log text has a start position and an end position; Step 2: Determine if the remaining space in the sector where the end position of the log text area is located is sufficient to write log text; if it is sufficient, start writing log text from the position after the last end position; If not satisfied, proceed to step 3; Step 3: Determine if the next sector of the sector where the end position is located is the sector where the log text begins; if not, proceed to step 4; if yes, proceed to step 5. Step 4: Erase the sector following the sector where the end position is located, write log text from the position following the previous end position, update the index information, and roll the updated index information to the index area. Step 5: Update the starting position and index information, and write the updated index information to the index area in a rolling manner; erase the sector following the sector where the ending position is located, write the log text from the position following the previous ending position, update the index information, and write the updated index information to the index area in a rolling manner again. The number of sectors in the index area * sector size / 16 is greater than or equal to the number of sectors in the log text area * 2; The process of rolling the updated index information to the index area involves the following steps: each sector of the index area includes multiple locations for storing index information. The updated index information is written to the next location after the original index information location, and the original index information is cleared.
2. The method for rolling log text storage based on NOR FLASH according to claim 1, characterized in that, After the index information is written to the position of one sector, the next sector is erased, the updated index information is written, and the original index information is cleared; when the index information is written to the last sector of the index area, the next time it returns to the first sector and starts writing again in a loop.
3. The method for rolling log text storage based on NOR FLASH according to claim 1, characterized in that, The index information includes the sector number of the start position of the log text, the sector number of the end position, FLAG, and CRC checksum; the value of FLAG is set to not all be 0x00 or 0xFF.
4. The method for rolling log text storage based on NOR FLASH according to claim 3, characterized in that, The starting position is calculated as the sector number of the starting position multiplied by the sector size; the ending position is calculated as the sector number of the ending position multiplied by the sector size plus X. Where X is the position of the last non-0xFF value.
5. The method for rolling log text storage based on NOR FLASH according to claim 1, characterized in that, If it is the first time using it or upon restarting, initialization is required. Specifically, the index information in the index area is traversed. If the index information passes the CRC check, it is recorded for use in the next log text writing. If no index information passes the CRC check, the index area and log text area are erased, and the initialization index information is written to the first sector of the index area.
6. The method for rolling log text storage based on NOR FLASH according to claim 1, characterized in that, When it is necessary to read the log text, the specific operation is as follows: if the end position is after the start position, then read the content between the start position and the end position; if the end position is before the start position, then it is necessary to read it in two parts: first read the content from the start position to the end position of the log text area, and then read the content from the start position of the log text area to the end position of the log.
7. A log text scrolling storage device based on NOR FLASH, characterized in that, Includes the following units: Partition unit: used to divide the log text storage area into an index area and a log text area; First judgment unit: Determine whether the remaining space in the sector where the end position of the log text area is located is sufficient to write log text; if it is sufficient, start writing log text from the position after the last end position; Second judgment unit: Determine whether the next sector of the sector where the end position is located is the sector where the log text begins; First write unit: Erase the sector following the sector where the end position is located, start writing log text from the position following the last end position, update index information, and roll the updated index information to the index area. The second writing unit updates the starting position and index information, and writes the updated index information to the index area in a rolling manner; it erases the sector following the sector where the ending position is located, and starts writing log text from the position following the previous ending position, updates the index information, and writes the updated index information to the index area in a rolling manner. The number of sectors in the index area * sector size / 16 is greater than or equal to the number of sectors in the log text area * 2; The process of rolling the updated index information to the index area involves the following steps: each sector of the index area includes multiple locations for storing index information. The updated index information is written to the next location after the original index information location, and the original index information is cleared.
8. A log text scrolling storage device based on NOR FLASH according to claim 7, characterized in that, It also includes a reading unit; the reading unit is used to read log text.