A parameter storage mechanism for embedded systems
By using bidirectional mapping and background thread monitoring of structured memory data blocks and storage media in embedded systems, the problems of busy writing, data integrity and erasure fatigue of Flash and E2PROM storage media without an operating system are solved. It realizes configuration parameter storage with strong real-time performance and good data integrity, prolongs the memory life and reduces manual configuration work.
Patent Information
- Application Number
- CN202011481455.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-16
- Publication Date
- 2024-09-10
- Estimated Expiration
- 2040-12-16
AI Technical Summary
In low-end small microcontroller systems without operating system support, Flash and E2PROM storage media have problems such as busy writing, data integrity issues, parameter loading errors, default parameter clearing, and erasing fatigue when writing configuration parameters. Affect the real-time and reliability of the system.
It uses structured memory data blocks and two independent storage areas on the storage medium for bidirectional mapping, uses the control header of the CRC check field to ensure data integrity, and provides real-time monitoring and asynchronous data caching through background threads to provide error detection and Recovery mechanism, filters invalid repeated write requests, and automatically generates default configuration parameters.
It realizes configuration parameter storage with strong real-time performance and good data integrity in embedded systems such as 8051, MSP430 and ARM, which extends the service life of the memory, reduces the manual configuration workload, and improves the reliability and real-time performance of the system. .
Smart Images

Figure CN112596808B
Abstract
Description
Technical Field
[0001] The present invention relates to a configuration parameter storage mechanism for an embedded system, which can solve the real-time writing and data integrity problems of a small number of configuration parameters without the help of a real-time scheduling function of an operating system, and is mainly used for parameter configuration of a low-end small single-chip computer system without an operating system, and belongs to the field of embedded software design. Background Art
[0002] In 8051, MSP430 and ARM microcontroller embedded systems, it is often necessary to save some configuration parameters set by the user through the serial port, network or human-machine interface during operation. The correct writing of parameters is very critical. If the parameters are not written or the written information is incomplete, it will have an adverse effect on the operation of the system. The most common onboard storage media of embedded systems are FRAM, E2PROM and Flash. The first medium, FRAM, has the advantages of fast speed, low latency and high reliability. It is an ideal parameter storage medium. However, its manufacturing process is complex, its capacity is relatively small, its cost is relatively high, and its cost performance is not high. It is only used in some key businesses. Most embedded systems still use relatively cheap E2PROM and Flash. Especially with the rapid development of personal consumer electronic products such as mobile phones and digital cameras, the use of Flash storage media has been greatly popularized, thereby significantly reducing the unit bit cost of Flash storage media. Moreover, most Flash storage chips use the SPI bus interface, and the bandwidth is usually tens of Mbps, while E2PROM usually uses the I2C bus interface, and the bandwidth is usually no more than 400Kbps. Therefore, in many embedded systems with high real-time requirements, Flash storage media has a trend of gradually replacing E2PROM. However, without the support of embedded operating systems, the use of these two storage media will face the following problems:
[0003] (1) Both storage media have a write-busy state. Flash media must be erased before writing. The busy time is usually tens of milliseconds or even hundreds of milliseconds. This is unacceptable for embedded systems with high real-time requirements, such as radio transceivers. System freezes of more than 1ms will cause message reception failure.
[0004] (2) During the process of writing parameters, the system may suddenly lose power or power on again. If the system is reset or restarted during the process of writing parameters, the integrity of the data will be destroyed. In this case, a data backup mechanism is needed.
[0005] (3) During the system power-on process, due to power jitter or unstable power-on, the processor has completed the boot and entered the parameter loading stage, which may cause parameter loading errors. At this time, an error detection mechanism and data recovery method are needed.
[0006] (4) When the system leaves the factory or is reset on-site by the customer, the configuration parameters will be completely cleared, and the data in the storage medium will be completely invalid. When mass-producing products, manually presetting the default parameters of each system takes a lot of time and is prone to errors. At this time, a mechanism is also needed to provide a set of default parameters to configure the system operation.
[0007] (5) E2PROM and Flash media also have a limited write operation life. If the number of write operations exceeds a certain limit, it will cause erase fatigue and irreversible permanent damage, and data can no longer be stored reliably. Due to the drive of state logic, applications will frequently refresh configuration parameters in some cases. The values of some member variables are actually unchanged. Therefore, a large number of repeated write operations will reduce the service life of the memory. At this time, a mechanism is needed to filter out invalid requests and only execute truly valid operations. Summary of the invention
[0008] In order to solve the performance and reliability problems of configuration parameters in embedded systems without real-time operating systems such as 8051, MSP430 and ARM, this method proposes a set of mechanisms that can achieve strong real-time reading and writing, good data integrity, filter invalid repeated write requests, extend the service life of the memory, and automatically generate default configuration parameters for the system to reduce the manual configuration workload during batch production. The specific method is as follows:
[0009] (1) A set of system parameters to be configured is placed in a structured memory block with continuous addresses. When reading and writing configuration parameters, the application directly accesses the member variables in the inner block instead of the storage medium, thus achieving high-speed real-time access. Two non-overlapping storage areas are specified on the storage medium, and an 8-byte control header with a CRC check field is used to bidirectionally map the contents of the memory data block to the two storage areas on the storage medium.
[0010] (2) The application selects a storage area with correct data verification only when it is powered on and initialized, and loads its content into the memory data block at one time; if the data in one area is incorrect, the storage area is overwritten with the data in the storage area with correct verification; if both storage areas are incorrect, the memory data block is filled with the system default configuration parameters and the two storage areas are overwritten;
[0011] (3) After the application completes the initialization work and enters the main loop, it no longer loads configuration parameters from the storage medium, but monitors the system's configuration parameter write requests in real time; once any member variable of the configuration parameters is changed, the entire memory data block will be cached to two storage areas. When caching data, it is necessary to operate in units of storage areas. Only after the first storage area is completely written can the second storage area be written; before writing data to each storage area, it is necessary to erase the storage area in units of sectors. If there are multiple sectors, they are erased one by one; after the erase operation is completed, the memory page FIFO can be used as the maximum write unit for writing. If the length of the structured memory data block exceeds the size of a page FIFO, it needs to be written in multiple fragments; before each sector erase operation and data write operation, the working status of the memory needs to be detected. If it is busy, it is necessary to wait continuously and the operation can only be performed when it is not busy;
[0012] (4) In the process of writing the data in the structured memory data block to the two storage areas, each step is split into atomic operations that do not contain blocking waiting behaviors; as long as the working status of the storage medium is detected to be busy, the next step is stopped and the detection is continued; if there is a new write request during this process, the current process is terminated immediately and the cache process is started from the beginning. Similarly, when setting configuration parameters, the application also needs to compare the new value with the member variable in the memory data block; if the two are consistent, the operation is abandoned, otherwise the member variable is modified and a data change request is set to notify the background thread to asynchronously cache the content of the structured memory data block to the two areas on the storage medium, and update the corresponding header control information and CRC checksum;
[0013] (5) In order to adapt to various storage media such as E2PROM, Flash and FRAM, this method also sets up a hardware abstraction layer (HAL). This abstraction layer has a total of 6 independent application program interface API functions, which are responsible for completing the following function set:
[0014] (a) Function to read data from storage medium:
[0015] char GetUserBlock(char iDevice,long dwAddress,char*pBuffer,shortiSize)
[0016] Parameter definition and description:
[0017] 1) char iDevice: the device number of the storage medium, ranging from 0X00-0XFF, the default value = 0X00
[0018] 2) long dwAddress: 32-bit byte address to read the storage medium
[0019] 3) char*pBuffer: pointer to the memory where data is stored when it is read from the storage medium
[0020] 4) short iSize: the size of data read at one time, up to 64K bytes
[0021] Return value: 0X00 = operation failed, 0X01 = operation successful (b) Function to write data to the storage medium:
[0022] char SetUserBlock(char iDevice,long dwAddress,char*pBuffer,shortiSize)
[0023] Parameter definition and description:
[0024] 1) char iDevice: the device number of the storage medium, ranging from 0X00-0XFF, the default value = 0X00
[0025] 2) long dwAddress: 32-bit byte address to be written to the storage medium
[0026] 3) char*pBuffer: pointer to the memory where the data to be written to the storage medium is stored
[0027] 4) short iSize: the size of data read at one time, up to 64K bytes
[0028] Return value: 0X00 = operation failed, 0X01 = operation successful (c) Function to obtain the write page FIFO size of the storage medium:
[0029] unsigned short UserBlock_GetPageSize(void)
[0030] Return value: the size of the on-chip cache of the storage chip, in bytes. This value refers to the size of the largest data block that can be written at one time and guaranteed not to overflow when the device is idle (d) Get the erase sector size function of the storage medium:
[0031] unsigned short UserBlock_GetSectorSize(void)
[0032] Return value: the sector size of the memory chip, in bytes. If the chip does not need to be erased before writing (such as FRAM and E2PROM storage media), please return 0X01; if it is not equal to 0X01, the value refers to the size of a sector that needs to be erased before writing the value to the memory chip (such as Flash). (e) Function to erase the contents of the specified sector:
[0033] void UserBlock_SetSectorAddr(char iDevice,unsigned long dwAddr)
[0034] Parameter definition and description:
[0035] 1) char iDevice: the device number of the storage medium, ranging from 0X00-0XFF, the default value = 0X00
[0036] 2) unsigned long dwAddr: the logical address of the storage medium to be erased
[0037] Return value: 0X00 = operation failed, 0X01 = operation successful (f) Function to check whether the storage medium is busy:
[0038] char UserBlock_IsWritable(char iDevice)
[0039] Parameter definition and description:
[0040] 1) char iDevice: the device number of the storage medium, ranging from 0X00-0XFF, the default value = 0X00
[0041] Return value: 0X00 = idle state, 0X01 = busy state
[0042] With the help of this hardware abstraction layer, the parameter storage mechanism can reliably read and write storage media without worrying about the physical characteristics and working mechanism of the storage medium, thereby achieving the universality of the driver interface. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] The accompanying drawings, which constitute a part of the specification, illustrate embodiments of the present disclosure and, together with the description, serve to explain the principles of the present disclosure.
[0044] The present disclosure may be more clearly understood from the following detailed description with reference to the accompanying drawings, in which:
[0045] Figure 1 is a mapping diagram between a structured memory data block and two storage areas in the present disclosure;
[0046] Figure 2 It is the data correctness verification logic of the two storage areas A and B;
[0047] Figure 3 It is the operation process of loading configuration parameters from storage media;
[0048] Figure 4 It is the process of the application layer interface API function of the parameter storage system;
[0049] Figure 5 It is the workflow of the background thread that caches the memory data blocks into two storage areas A and B;
[0050] Figure 6 It is the detailed workflow of the background thread writing to a single storage area of A or B;
[0051] It should be understood that the size of each part shown in the accompanying drawings is not drawn according to the actual proportional relationship. In addition, the same or similar reference numerals represent the same or similar components. DETAILED DESCRIPTION
[0052] The parameter storage mechanism consists of the following three parts:
[0053] (1) A data loading function, which is responsible for loading configuration parameters from the storage medium to the memory block during the system power-on initialization phase
[0054] (2) A background worker thread responsible for writing the data cache of the memory data block to the two storage areas of the storage medium
[0055] (3) Several application program interface API functions corresponding to the member variables in the structured memory data block, responsible for reading and writing the values of the member variables. These three parts are connected through the structured memory data block as a link. The working method of part (1), i.e., the data loading function, is as follows:
[0056] (1) The data loading function is executed during the application initialization phase, before entering the system's main loop. It is only executed once. After entering the main loop, the data loading operation is no longer executed.
[0057] (2) Once the configuration parameters are loaded from the storage medium into the structured memory block, the subsequent application programming interface API function directly accesses the data in the memory block when accessing the configuration parameters, and no longer accesses the storage medium. This greatly improves the real-time performance of the system operation.
[0058] (3) During the data loading process, if the data verification of the storage area is incorrect, it is necessary to delay for a period of time and try again to eliminate the interference of accidental factors such as power jitter; this cycle repeats until the data of a storage area is corrected or the number of attempts reaches a certain upper limit.
[0059] (4) If multiple retries find that the data in both storage areas are incorrect, the system is notified to fill the structured memory data block with the default configuration parameters, and the "dirty" flags of the two storage areas are set, and the data update request Request is set to 1, indicating that after entering the main loop, the background working thread caches the data in the memory block to the two storage areas, thereby realizing the automatic writing process of the default configuration parameters after the product leaves the factory.
[0060] (5) If the data verification of one storage area is correct, but the data verification of another storage area is incorrect, then the configuration parameters are loaded from the storage area with the correct verification into the memory block, and the area with the incorrect verification is set to "dirty" and the data update request flag Request=1 is set, indicating that after entering the main loop, the background working thread writes the data cache in the memory data block to the area marked as "dirty", thereby realizing the mirror backup of the two storage areas. Part (2), that is, the background working thread is only responsible for the one-way data caching of the structured memory data block to the two storage areas, and no longer reversely loads data from the two storage areas to the memory data block. This thread needs to be placed in the main loop after the system is initialized, and monitors the data update request submitted in part (3), that is, the application program interface API function in real time. If there is no data update request, it is in a non-blocking waiting state and continues to monitor; once a new data update request arrives, it enters the data caching stage, and its working principle is as follows:
[0061] (1) First, you need to erase the entire contents of the first storage area, erasing the capacity of a memory sector at a time. If the capacity of the structured memory block exceeds the capacity of a sector, you need to erase the contents of multiple adjacent sectors one by one; and before each erase operation, you need to detect the memory working status, and the erase operation can only be performed when it is not busy; if a new data update request is detected during this period, the current sector erase is immediately terminated and a new cache operation is restarted.
[0062] (2) After completing the erase operation of the first storage area, data can be written to the storage area. First, the control information of the storage area needs to be written, a total of 8 bytes, including a 4-byte starting address, a 2-byte data length and a 2-byte CRC check; then the data fragment is written with the memory page FIFO as the largest unit. If the length of the structured memory data block exceeds the size of a page FIFO, it needs to be written in multiple steps until all the data is written. Before each write operation, the memory working status needs to be detected, and the write operation can only be performed when it is not busy; if a new data update request is detected during this period, the current data write is terminated immediately and a new cache operation is restarted.
[0063] (3) After completing the data write operation of the first storage area, the sector erase operation of the second storage area can be started. The operation process is exactly the same as the erase operation of the first storage area. If a new data update request is detected during the non-blocking waiting period when the memory working state is "busy", the current sector erase operation needs to be terminated immediately and a new cache operation needs to be restarted.
[0064] (4) After the erase operation of the second storage area is completed, data can be written to the second storage area. The operation process is exactly the same as the write operation of the first storage area. If a new data update request is detected during the non-blocking waiting period when the memory working state is "busy", the current data writing work needs to be terminated immediately and a new cache operation needs to be restarted.
[0065] Part (3) is the application program interface API function for the member variables in the structured memory block. These API interfaces are functions used by the application to save or read configuration parameters. In principle, there are as many sets of read and write functions as there are member variables; these functions do not deal directly with the storage medium, but directly access the structured memory data block, thus ensuring the real-time performance of the system. If you need to modify the value of the member variable, you only need to set the "dirty" flag of the two storage areas and set a new data update request to notify the background worker thread to perform data caching operations and safely and reliably write the contents of the memory data block to the two storage areas of the storage medium. The "interception" processing of repeated data write operations is also built into the write operation function; if the system needs to frequently refresh the system configuration parameters due to the drive of the state logic, and these parameters are an unchanging fixed value in some cases, then a large number of write operations are actually invalid repeated requests, then the filtering behavior at this time will greatly reduce the actual memory write operations, thereby extending the service life of the memory. From the above analysis, it can be seen that the parameter storage mechanism establishes a two-way mapping relationship between a structured memory data block and two independent storage areas on the storage medium, and arranges the data loading operation and the data cache writing operation in the system initialization phase and the background thread of the system main loop after initialization, respectively, which improves the real-time performance of the system; with the help of the mutual mirroring and master-slave backup mechanism of the two storage areas, the self-recovery ability of the data is ensured, and the reliability of the system is improved; the storage area uses an 8-byte control header with a CRC check field to effectively ensure the error detection ability of the data. When it is found that the data in the two storage areas are incorrect, it can be judged that the system has restored the factory configuration or the first power-on after the production is offline, and the configuration parameters can be automatically generated, which greatly reduces the workload of manual configuration, thereby solving a series of problems in the configuration parameter writing without the support of the embedded operating system.
[0066] Figure 1 It is a mapping diagram between structured memory data blocks and two storage areas. Each memory data block M corresponds to two storage medium areas A and B on the storage medium, where area A is the first storage area and the main storage area; area B is the second storage area and the backup storage area. The two storage areas are exactly the same in function, except for the order of writing. The two storage areas can be adjacent or non-adjacent in logical addresses, but there can be no overlapping parts; the data organization structure of the two storage areas A and B is exactly the same, and its data structure consists of an 8-byte data header and the data block itself, as follows:
[0067] (1) S0: 4-byte starting address
[0068] (2) S1: 2 bytes of data length
[0069] (3) S2: 2-byte CRC checksum
[0070] (4) S3: Data block corresponding to memory parameters
[0071] The following is a description of the four fields of this data structure:
[0072] (1) S0 field: The starting address of these 4 bytes is the address of the 8-byte header control information of the storage area on the storage medium. For a specific memory data block, this address is a fixed constant. If there are multiple memory data blocks in the system, the address of each memory data block is different, and its corresponding S0 data segment is also different. (2) S1 field: The data size of these two bytes represents the length of the S3 field, that is, the length of the structured memory data block, in bytes.
[0073] (3) S2 field: These two bytes are the S3 field, which is the 16-bit CRC checksum of the entire structured memory data block. It is used to check for errors in any bit of the S3 field.
[0074] (4) S3 field: This field is the content of the memory data block, and its length is the length of the memory data block.
[0075] It can be seen that, for a memory data block M corresponding to a parameter storage, the contents of its two medium storage areas A and B, and the contents of its S1, S2 and S3 fields are exactly the same, and the S0 field is different because it corresponds to a different storage address.
[0076] Figure 2 It is the data correctness verification logic of the two storage areas A and B. The specific rules are as follows:
[0077] (1) S0 This field should be the specified address constant. If they are the same, it is a match. Otherwise, it is a mismatch.
[0078] (2) S1 This field should be a specified length constant. If they are the same, it is a match. Otherwise, it is a mismatch.
[0079] (3) Calculate the CRC value of the S3 field and compare it with the S2 field. If they are the same, it is a match. Otherwise, it is a mismatch. If all the above three fields match, the storage area is verified successfully, and the value R0 or R1 = 1. Otherwise, the verification fails, and the value R0 or R1 = 0
[0080] Figure 3 It is the operation of loading configuration parameters from the storage medium to the structured memory data block after the system is powered on and the basic initialization is completed. The workflow is as follows:
[0081] (1) Load configuration parameters from storage areas A and B into memory blocks and verify the correctness of their data R0 and R1
[0082] (2) Since area A is loaded first and area B is loaded later, the data in area B is actually filled in the memory block.
[0083] (3) Next, we determine the data verification results of the A and B storage areas. There are four cases in total, as follows:
[0084] a) R0=0, R1=0: that is, the verification of storage area A fails, and the verification of storage area B fails
[0085] b) R0=0, R1=1: that is, the verification of storage area A fails, and the verification of storage area B succeeds
[0086] c) R0=1, R1=0: that is, the verification of storage area A succeeds, and the verification of storage area B fails
[0087] d) R0=1, R1=1: That is, the verification of storage area A is successful, and the verification of storage area B is successful.
[0088] For these four verification situations, the processing rules are as follows:
[0089] (1) R0=0, R1=0: After a delay of 100ms, repeat the reading and verification of R0 and R1. If the result is still the same after a certain number of times, it is considered that the parameter loading has failed and the entire data loading process will be exited. This is usually the first power-on state after the product leaves the factory or is restored to the factory configuration. The memory is in a brand new state and has not been initialized. As long as there is a verification result of R0=1 or R1=1, it means that the product has been initialized and configured, and the reading failure is caused by accidental factors such as unstable power supply. In this case, wait for a while until the system is stable, and then try a few more times to get the correct verification result. At this time, execute the processing methods (2)(3)(4).
[0090] (2) R0=0, R1=1: The data in area A is incorrect, but the data in storage area B is correct. This usually happens when the system is updating the data in area A and a processor reset or system power failure occurs. Since only part of area A is updated, the data is incomplete, resulting in verification failure. Area B has not yet started to update, and the correct data from the last time is saved. In this case, the data in area B needs to be loaded into the memory block, that is, "rolled back" or "restored" to the most recent system configuration. At the same time, set storage area A to "dirty" and set update request Request=1 to instruct the background thread to overwrite storage area B with the memory data block.
[0091] (3) R0=1, R1=0: The data in area A is incorrect, but the data in area B is correct. Similar to situation (2), the system usually updates the data in area A, and then during the update of area B, a processor reset or system power failure occurs. At this time, only part of the data in area B is updated, so the data is incomplete, resulting in verification failure. Since the data in area B is loaded into the memory block at this time, the data in area A needs to be reloaded into the memory data block, and the B storage area is set to "dirty", and the update request Request is set to 1, instructing the background thread to overwrite the B storage area with the memory data block.
[0092] (4) R0=1, R1=1: The data in both storage areas A and B are correct, and the data loaded from storage area A or storage area B to the memory block is the same. This is the case when the system is working normally and no exceptions occur. If the data verification of both storage areas A and B is incorrect during the entire data loading process, a failure flag is returned, indicating that the system needs to overwrite the memory block with the default configuration parameters, and then set both storage areas to "dirty", and set the request flag Request=1 to instruct the background thread to overwrite the two storage areas A and B with the memory data block; if only one of the storage areas A or B is "dirty", only the data of that storage area needs to be updated, and the other storage area does not need to be updated.
[0093] Figure 4 This is the process of the application layer interface API function of the parameter storage system. Since all configuration parameters are organized in a structured memory block with continuous addresses, and the application interface only accesses one member variable at a time, when the application layer interface modifies the member variables in the memory block, it can be "intercepted" and data filtered. The specific method is as follows:
[0094] (1) Compare the new value to be written with the current data of the member variable in the memory block. If the two are the same, it means that it is a repeated write behavior. There is no need to write again. Just give up directly. This can reduce a large number of unnecessary repeated write operations. On the one hand, it can save some processor computing power and improve the system's response speed and real-time performance; on the other hand, it can also extend the service life of the storage medium.
[0095] (2) If the two are different, it means that it is a new valid modification request. At this time, the "dirty" flags of both storage areas A and B need to be updated at the same time to indicate to the background thread that both areas A and B need to be updated; at the same time, the data update request Request is incremented by 1 to indicate that this is a new request. Since any changes to the data update request Request will be monitored in real time by the background thread and decremented by 1, the range of this value is usually not large. However, considering that the value will automatically overflow and return to zero if it exceeds 255, it needs to be limited.
[0096] Figure 5 It is the workflow of the background thread responsible for caching memory data blocks into two storage areas A and B. This thread needs to be placed in the main loop of the application and is in a running state. It monitors the data update requests issued by the application by calling the API interface function that accesses the configuration parameters in real time. If the data update request is always zero, it means that no new data update request has arrived. At this time, the thread is in the task monitoring state and does not perform any operation; once the data update request Request is not zero, it immediately enters the data cache write state. In the data cache write state, it is first necessary to detect the "dirty" flag of the two storage areas A and B. If the "dirty" flag is set, it means that the area needs to be updated, otherwise it does not need to be updated. In the system initialization phase, it may be the case that one area is "dirty" and the other area is not "dirty", but in the normal operation phase, both storage areas will be "dirty". When the thread updates the two storage areas A and B, it updates the A storage area first, and then updates the B storage area after all the data in the A area is updated; this ensures that no matter if a processor reset or system power failure occurs during the update of the A or B storage area, only one storage area will fail the verification due to incomplete data, while the data in the other storage area is complete. The data update of each storage area is divided into two major steps. The first step is sector erasing, and the second step is data writing. For E2PROM and FRAM storage media that do not require sector erasing operations to perform data writing, it is necessary to set the memory sector size = 1. At this time, the sector erase operation will be skipped and the second step of data writing will be executed directly; the detailed steps for sector erasing and data writing are in the attached. Figure 6 Introduced in.
[0097] Figure 6 This is the detailed workflow of the background thread writing to a single storage area of A or B, which is equivalent to the attached Figure 5A partial enlarged view of the "Update A / B storage area data" section in Figure 1. If the amount of data in the memory block is large and exceeds the size of a sector, it is necessary to erase sectors one by one until all sectors are erased; the data writing process is the same. The maximum amount of data written at a time is the size of a page FIFO. If the amount of data in the memory block is too large and exceeds the size of a page FIFO, it is necessary to perform multiple write processes until all data is written. In order to ensure the real-time performance and reliability of this thread, the following design needs to be made during the cache writing phase:
[0098] (1) Each operation on the media storage is an action that does not involve blocking or waiting.
[0099] (2) Before each sector erase and data write operation, you need to read the working status of the memory. The operation can only be performed when it is not "busy", otherwise you need to continue waiting.
[0100] (3) Sector erase and data write operations must be performed immediately after status query, and no other thread switching behavior can occur in between. This is to ensure that when the system has multiple sets of configuration parameters and multiple background threads, there will be no state disorder and write conflicts.
[0101] (4) During the waiting period when the memory working status is "busy", it is necessary to detect whether there is a new data update request. If there is, the current write request is abandoned and the entire data cache write process is restarted. This can improve the real-time performance of the system on the one hand, and reduce the number of memory erase and write times on the other hand, thereby increasing the service life of the memory.
Claims
1. A parameter storage method for an embedded system, characterized in that A set of system parameters that need to be configured is placed in a structured memory block with continuous addresses. When reading and writing configuration parameters, the application directly accesses the member variables in the inner block instead of the storage medium, so as to achieve high-speed real-time access. Two storage areas with non-overlapping addresses are specified on the storage medium, and the contents in the memory data block are bidirectionally mapped to the two storage areas on the storage medium through an 8-byte control header with a CRC check field. The application selects a storage area with correct data verification only at power-on initialization, and loads its contents into the memory data block at one time. If the data in one area is incorrect, the data in the storage area with correct verification is overwritten. If both storage areas are incorrect, the memory data block is filled with the system default configuration parameters, and the two storage areas are overwritten. After the application completes the initialization work and enters the main loop, it no longer loads configuration parameters from the storage medium, but monitors the system's configuration parameter write requests in real time; once any member variable of the configuration parameter is changed, the entire memory data block will be cached to two storage areas; when caching data, it is necessary to operate in units of storage areas, and after the first storage area is completely written, write to the second storage area; before writing data to each storage area, it is necessary to erase the storage area in units of sectors, and if there are multiple sectors, erase them one by one; after the erase operation is completed, the memory page FIFO is used as the maximum write unit. For row write operations, if the length of a structured memory data block exceeds the size of a page FIFO, it needs to be written in multiple segments; before each sector erase operation and data write operation, the working status of the memory needs to be detected. If it is busy, it needs to wait continuously and perform the operation when it is not busy; in the process of writing data in the structured memory data block to the two storage areas, each step is split into atomic operations that do not contain blocking waiting behaviors; as long as the working status of the storage medium is detected to be busy, the next step is stopped and the detection is continued; if there is a new write request during this process, the current process is terminated immediately and the cache process starts from the beginning; Similarly, when the application sets configuration parameters, it also needs to compare the new values with the member variables in the memory data block; If the two are consistent, the operation is abandoned; otherwise, the member variable is modified and a data change request is set to notify the background thread to asynchronously cache the contents of the structured memory data block to two areas on the storage medium, and update the corresponding header control information and CRC checksum; a hardware abstraction layer is set, which has a total of 6 independent application program interface API functions, including a read function, a write function, a function for querying the working status of the memory, a function for reading the page FIFO size, a function for reading the sector size and a function for erasing a sector.
2. The parameter storage method of the embedded system according to claim 1, characterized in that The two areas on the storage medium are composed of four parts, namely a four-byte storage address, a two-byte data length, a two-byte CRC data checksum, and the content of the structured memory data block; and only when all the fields of the first three parts are checked correctly does it mean that the data in the storage area is correct, otherwise it is incorrect.
