If there are several setting parameters that need to be stored in Flash, how would we normally store it? Will different parameters be stored in different pages, or will these parameters be bundled into a structure, and each modification will be written at the same time? Store parameters to a fixed address, each parameter will occupy a block of Flash. All the parameters are bundled together into a Flash block. Then, when only one parameter is modified, all the parameters need to be saved together. So what better way? Learning Msos some time ago and seeing that the method used to store the parameters is well-designed. It stores the parameter's variable address and value in Flash. The highlight is the use of the variable address of the parameter to mark the different variables. This storage method uses two data structures: Typedef struct { Uint Address; // The address of the parameter variable Uint Data; // The value of the parameter variable }CellStruct; To store a variable, you need to store the address of this variable along with its value in the memory area. The core of this storage method is this data structure. This way you can use *((uint *)(Address)) = Data to assign the stored value directly to the corresponding variable. Simply put, the different parameters are marked according to the address value. 2. The data structure of the storage area Typedef struct { This storage uses two functions: Read variable values ​​in memory and update variable values Variable storage function 2.1 Reading of parameters The flow chart is as above, the main steps are as follows: Update the value of the variable based on the address of the variable written in Flash. Store the address value stored in Flash in a temporary array, and determine whether there is invalid data repeatedly stored according to the address value, and invalidate the invalid array; Empty the Flash memory area and save the valid variables in the temporary array back to Flash. Through these steps, the variables stored in the memory area are read out, and duplicate invalid data in the memory area is cleared. Here is the source code: #define pUint(address) *((uint *)(address)) #define PageSize 1024 //Stm32F103R8T6 1Page=1024Byte #define ParameterAddress (FLASH_BASE + (63 * 1024)) #define ParameterSpace PageSize / 4/2 staTIc void ReadAllParameter ( Void) { bool CleanFlag; int i, j; 2.2 Writing parameters The parameter is very simple to write, according to the write point in the data structure, the address and value of the variable is written to Flash. staTIc bool WriteParameter(void * dataPointer) { 2.3 How to use Instructions: Each time when power on, call the function to read all variables; Modify a parameter, call the write parameter function; When reading parameters, it is necessary to set up a ParameterSpace array in RAM. If this value is too large, it will exceed the size of the stack, making the memory overflow. Therefore, the storage area cannot be opened too much. This method is easy to use, especially when updating variable values, updating the corresponding values ​​based on the stored variable addresses. In fact, its essence is the same as we use variable names to mark different variables. However, there are disadvantages: First of all, it also stores the value of the variable's address and variable, which is equivalent to using twice as much storage space. As stated in the notes above, the storage area cannot be opened too large, otherwise the temporary array will exceed the size of the stack.