If there are several setting parameters that need to be stored in Flash, how do we generally store them? Store different parameters in different pages, or bundle these parameters into a structure, and write them at the same time for each modification?
Store the parameters to a fixed address, each parameter will occupy a block of Flash. And if all the parameters are bundled and stored in the Flash block, then when only one parameter is modified, it is also necessary to store all the parameters together. So is there a better way?
I learned about Msos some time ago and saw that the parameter storage method used in it is well designed. It stores the variable address and value of the parameter into the Flash together. The highlight is the use of the variable address of the parameter to mark different variables.
One, data structure
This storage method uses two data structures:
typedef struct
{
uint Address; //Address of parameter variable
uint Data; //The value of the parameter variable
}CellStruct;
To store a variable, you need to store the address of the variable and its value in the storage area. The core of this storage method is this data structure. In this way, you can use *((uint *)(Address)) = Data to directly assign the stored value to the corresponding variable. Simply put, the different parameters are marked according to the address value.
2. Data structure of the storage area
typedefstruct{
Two, code analysis
This storage method uses two functions:
Read the variable value in the storage area and update the value of the variable
Variable storage function
2.1 Reading of parameters
The flow chart is as above, and the main steps are as follows:
Update the value of the variable according to the variable address stored in Flash;
Store the address value stored in Flash into a temporary array, and judge whether there is invalid data repeatedly stored according to the address value, and invalidate the invalid array;
Clear the Flash storage area and re-store the valid variables in the temporary array into the Flash.
Through these steps, the variables stored in the storage area are read out, and the repeated invalid data in the storage area is cleared. Here is the source code:
#definepUint(address)*((uint*)(address))#definePageSize1024//Stm32F103R8T61Page=1024Byte#defineParameterAddress(FLASH_BASE+(63*1024))#defineParameterSpacePageSize/4/2staticvoidReadAllParameter(void)(boolCleanFlag;inti,j;
2.2 Writing of parameters
The parameter writing is very simple, according to the writing point in the data structure, the address and value of the variable are written into the Flash.
staticboolWriteParameter(void*dataPointer){
2.3 How to use
Instructions:
Each time the power is turned on, the function that reads all variables is called;
When modifying a parameter, call the parameter write function;
Three, matters needing attention
When reading parameters, you need to create an array of ParameterSpace size in RAM. If this value is too large, it will exceed the size of the stack, causing memory overflow. Therefore, the storage area cannot be opened up too large. four,
to sum up
This method is easy to use, especially when updating the variable value, the corresponding value is updated according to the stored variable address. In fact, its essence is the same as when we use variable names to mark different variables. But there are disadvantages:
First, it stores the address of the variable and the value of the variable at the same time, which is equivalent to using twice the storage space;
As mentioned in the precautions above, the storage area cannot be opened too large, otherwise the temporary array will exceed the size of the stack.
With more than 15+ yrs rich MFG experience, you can definitely trust in and cooperate with.
Provide you with the supply of personal protective equipment. to help you safely get back to your daily routine.
Our products include pulse Oximeter Finger, Forehead Thermometer, Automatic foam soap dispenser, etc.
Our strict quality control protocol thoroughly vets every aspect of production, storage, and shipments all the way way to our end customers.
protective equipment, ppe personal protective equipment, definition of personal protective equipment
TOPNOTCH INTERNATIONAL GROUP LIMITED , https://www.itopnoobluetoothes.com