[0034] Example one
[0035] The storage method and reading method for storing user data in Flash in this embodiment are used to store user data with a fixed length that needs to be saved on the electrical terminal control board in the Flash space of the single-chip microcomputer in the electrical terminal control board. The stored N user data with a fixed length are packaged into data in the following way. Each data packet includes the header, N user data and check code from front to back; when the length of the user data is not fixed, each data packet From front to back, it includes the post header, M user data, reserved characters, end of post and check code; M changes within a predetermined range, M is less than or equal to the predetermined length of the longest user data N, the header or end of the post can be It is used to judge the integrity of the data link. We agree that the length of the stored data packet is fixed, so we agree to 1 byte length of post header and 1 byte length of post end. The content of post header and end of post is defined by the user, or only one of them , The check code adopts the cyclic redundancy check code CRC, which is 8 bits, and the polynomial adopts X8+X4+X3+1; in order to improve the efficiency, the CRC is obtained by the look-up table method; use the Flash that comes with the microcontroller The free part of the space is divided into several storage blocks according to the address order, see figure 1 Shown
[0036] For the data packet in the header, fixed-length N user data and check code format, the storage method for storing the packaged data packet in Flash is:
[0037] First define an array variable user_data[data_size], where data_size=frame header length+user data length N+check code length, unit byte, make user_data[0]=post header, and then save N user data with a fixed length to user_data [1]~user_data[N], N=data_size-2, user_data[data_size-1]=check code; define a pointer to const, unsignedcharconst*write_addr, make write_addr point to the first address of memory block a, and store the block The storage space of a is divided into several areas with data_size as the length. Before storing new data packets, check the storage block a by area. When checking the space, you only need to query the frame header of each area. The header is just the first address of each area. When the first empty area header address is found, that is, the frame header of the area is empty, start to write the packed data packet. After the data writing operation is over, The written data is verified whether it has been written successfully. If the write is not successful, the write operation is performed again; if the write operation is executed M times and the write operation is not successful, the next free area is searched and the above write operation is performed. , It is allowed to replace X areas at most to perform write operations. If X areas cannot be written successfully, it is considered that storage block a is damaged and the storage operation is exited; in this embodiment, M=5, X=50;
[0038] The specific steps are:
[0039] Step (1), judge whether the user data storage function is allowed to be called, that is: judge the user data storage flag bsave=true? If yes, go to step (2), if no, end;
[0040] Step (2), set user_data[0]=post header; then save the user data in user_data[1]~user_data[N];
[0041] Step (3), obtain the check code of the data packet;
[0042] Step (4), let user_data[data_size-1]=check code;
[0043] Step (5): Let write_addr=block_a, block_a is the first address of storage block a, and block_a_end is the end address of storage block a;
[0044] Step (6), the zone change counter y is cleared, that is, y=0;
[0045] Step (7), judge y <50? If yes, go to step (8), if not end;
[0046] Step (8), judge write_addr
[0047] Step (9). Determine whether the area pointed to by write_addr is empty? If yes, execute step (10), if not, point the address pointer to the next area, that is, write_addr+=data_size, and then execute step (8);
[0048] Step (10), judge (write_addr == block_a)? ,
[0049] Or (write_addr> (block_a_end-data_size)))? , If yes, go to step (11), if not, go to step (13);
[0050] Step (11), erase the block_a~block_a_end area; that is, erase the storage block a "empty" once when the user data is saved for the first time or the address overflows;
[0051] Step (12), set write_addr=block_a;
[0052] Step (13), clear the number n of writes to the same area, that is, n=0;
[0053] Step (14), judge whether the number of writes in the same area n is less than 5, if yes, execute step (15), if not, set y++, write_addr+=data_size, and then return to step (7);
[0054] Step (15), write the array user_data[] into the Flash space pointed to by write_addr;
[0055] Step (16), verify whether the writing is successful, if yes, end, if not, let n++, return to step (14).
[0056] The reading method for reading user data stored in Flash is:
[0057] Set a pointer to const, unsignedcharconst*P, and set the value of the flash space of the microcontroller to be "empty" to 0xff;
[0058] Step 1. Let P=block_a;
[0059] Step 2. Determine whether P is less than block_a_end, if yes, go to step 3, if not, end;
[0060] Step 3. Determine P[0] == header, and P[data_size] == 0xff? If yes, go to step 4, if not, set P+=data_size, and return to step 2;
[0061] Step 4. Find the check code of P[0]~P[data_size-2];
[0062] Step 5. Determine whether P[data_size-1] is equal to the check code? If yes, perform step 6, if not, initialize user data, set the save user data flag = true, and end;
[0063] Step 6. Recall the user data, save it to RAM, and end.
[0064] For the data packet in the format of header, variable-length user data M, trailer and check code, the storage method for storing the packaged data packet in Flash is:
[0065] First define an array variable user_data[data_size], data_size=predetermined maximum number of users N+3, make user_data[0]=post header, and then save M user data with variable length to user_data[1]~user_data In [N], user_data[data_size-2]=end of post, user_data[data_size-1]=check code, and the rest are cleared; define a pointer to const, unsignedcharconst*write_addr, to make Write_addr point to the first address of memory block a , Divide the storage space of storage block a into several areas with data_size as the length. Before storing new data packets, first check the storage block a by area to check "empty", and find the first address closest to storage block a After the "empty" area of the first address, start to write the packaged data packet. After the action of writing data is finished, verify whether the written data has been written successfully. If the write is not successful, perform another write Action; if the write action is executed M times and it is not successful, then find the next "empty" area, and then execute the above write operation. Up to X areas are allowed to be replaced to perform the write operation. If none of the X areas can be written successfully , It is considered that the storage block a is damaged, and the storage operation is exited; in this embodiment, M=5, X=50; the specific writing operation principle is the same as the above detailed steps;
[0066] The reading method for reading user data stored in Flash is:
[0067] Set a pointer to const, unsignedcharconst*P, and set the value of the flash space of the microcontroller to be 0xff after erasing;
[0068] Step 1. Let P=block_a;
[0069] Step 2. Determine whether P is less than block_a_end, if yes, go to step 3, if not, end;
[0070] Step 3. Determine P[0] == header, and P[data_size] == 0xff? If yes, go to step 4, if not, set P+=data_size, and return to step 2;
[0071] Step 4. Determine P[data_size-2] == End of post? If yes, go to step 5, if not, go to step 7;
[0072] Step 5. Find the check code of P[0]~P[data_size-2];
[0073] Step 6. Determine whether P[data_size-1] is equal to the check code? If yes, go to step 8, if not, go to step 7;
[0074] Step 7. Initialize user data, set the save user data flag = true, and end;
[0075] Step 8. Recall the user data, save it to RAM, and end.