Parameter storage method and device, electronic equipment and computer readable storage medium
By building and writing Flash Page in the SRAM of the MCU, the problem of high parameter storage cost and insufficient pin resources before power-down is solved, efficient and simple parameter storage is achieved, and hardware costs are reduced and the probability of storage failure.
Patent Information
- Application Number
- CN202411257018.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-09
- Publication Date
- 2024-12-27
AI Technical Summary
Existing embedded hardware devices need to save device parameters before powering down. In the prior art, backup batteries or complex log file systems are added, resulting in high costs and insufficient MCU pin resources.
Use SRAM to build the parameters to be saved in the MCU, and write them into multiple pages of Flash in turn. The data structures of the start flag, the data to be saved, the verification code and the end flag are realized.
It avoids increasing the hardware cost of backup batteries and MCU pin usage, reduces hardware costs, improves available pin resources for the device, simplifies storage logic, and improves the timeliness and reliability of parameter storage.
Smart Images

Figure CN119201226A_ABST
Abstract
Description
Background Art
[0002] Various embedded hardware devices with microcontroller units (MCU) (also known as single-chip microcomputers) as the core need to save a small amount of configured device parameters before power failure. The data volume of device parameters is small, but the importance is high. Loss of device parameters will cause the device to fail to boot.
[0003] In the related art, a backup battery (or capacitor) is added to the embedded hardware device so as to notify the MCU to use the backup battery to complete the storage of device parameters after the main battery loses power.
[0004] However, the method of adding an additional backup battery is costly and requires a hardware mechanism to notify the MCU of power failure, which occupies a pin resource of the MCU, resulting in fewer MCU pin resources available for embedded hardware devices.
[0005] It should be noted that the information disclosed in the above background technology section is only used to enhance the understanding of the background of the present application, and therefore may include information that does not constitute the prior art known to ordinary technicians in the field. Summary of the invention
[0006] The present application provides a parameter storage method, device, electronic device and computer-readable storage medium, which at least to some extent overcome the problems of high cost and limited available MCU pin resources in the related art.
[0007] Other features and advantages of the present application will become apparent from the following detailed description, or may be learned in part by the practice of the present application.
[0008] According to one aspect of the present application, a parameter storage method is provided, which is applied to an MCU, wherein the MCU includes an SRAM (Static Random-Access Memory) and a Flash (flash memory), wherein the Flash includes Page 1 (page 1, Flash is organized as multiple Pages) and Page 2, and comprises: in response to receiving a parameter storage instruction, constructing a parameter to be saved in the SRAM, wherein the data structure of the parameter to be saved includes a start flag, data to be saved, a check code of the data to be saved, and an end flag; and writing the parameter to be saved into the Page 1 and the Page 2 in sequence.
[0009] In some embodiments, constructing the parameters to be saved in the SRAM includes: creating an entity variable of the parameter to be saved in the SRAM; filling the start flag and the end flag in the entity variable; turning off the global interrupt of the MCU; obtaining the data to be saved and filling it into the data part to be saved of the entity variable; calculating the check code of the data part to be saved of the entity variable to obtain the check code data, and filling the check code data into the check code part of the entity variable; using the entity variable as the parameter to be saved.
[0010] In some embodiments, the writing of the parameters to be saved into the Page1 and the Page2 in sequence includes: searching for the end mark from the end of the Page1 forward; in response to finding the end mark, determining the relationship between the remaining space of the Page1 and the number of bytes included in the parameters to be saved; when the relationship is that the remaining space is not less than the number of bytes, taking the position of the next byte of the found end mark as the page offset, and writing the parameters to be saved starting from the page offset; in response to successfully writing the parameters to be saved into the Page1, performing a write position legality check on the Page2; if the check is legal, writing the parameters to be saved into the Page2 starting from the page offset; when the relationship is that the remaining space is less than the number of bytes, erasing the Page1, and writing the parameters to be saved starting from the starting position of the Page1; in response to successfully writing the parameters to be saved into the Page1, erasing the Page2, and writing the parameters to be saved starting from the starting position of the Page2.
[0011] In some embodiments, writing the parameters to be saved into the Page1 and the Page2 in sequence includes: searching for the end mark from the end of the Page1; in response to not finding the end mark, writing the parameters to be saved from the starting position of the Page1; in response to successfully writing the parameters to be saved into the Page1, performing a write position legality check on the Page2; when the check is legal, writing the parameters to be saved from the starting position of the Page2; when the check is illegal, erasing the Page2, and writing the parameters to be saved from the starting position of the Page2.
[0012] In some embodiments, it also includes: performing a legality check on the parameters to be saved recorded in the Page1 and Page2; and if the check is legal, reading the parameters to be saved recorded in the Page1.
[0013] In some embodiments, the legitimacy check on the parameters to be saved recorded in the Page1 and Page2 includes: checking the position legitimacy of the parameters to be saved recorded in the Page1 and Page2; and / or checking the legitimacy of the end mark in the Page1 and Page2; and / or checking the legitimacy of the check code in the Page1 and Page2.
[0014] According to another aspect of the present application, a parameter storage device is also provided, which is applied to an MCU, wherein the MCU includes an SRAM and a Flash, and the Flash includes Page1 and Page2, and includes: a construction module, which is used to construct parameters to be saved in the SRAM in response to receiving a parameter storage instruction, and the data structure of the parameters to be saved includes a start flag, data to be saved, a check code of the data to be saved, and an end flag; and a writing module, which is used to write the parameters to be saved into the Page1 and the Page2 in sequence.
[0015] In some embodiments, the construction module is used to create an entity variable of the parameter to be saved in the SRAM; fill in the start flag and the end flag in the entity variable; turn off the global interrupt of the MCU; obtain the data to be saved and fill it into the data part to be saved of the entity variable; calculate the check code of the data part to be saved of the entity variable to obtain the check code data, and fill the check code data into the check code part of the entity variable; use the entity variable as the parameter to be saved.
[0016] In some embodiments, the writing module is used to search for the end mark from the end of the Page1 forward; in response to finding the end mark, determine the relationship between the remaining space of the Page1 and the number of bytes included in the parameters to be saved; when the relationship is that the remaining space is not less than the number of bytes, use the position of the next byte of the found end mark as the page offset, and write the parameters to be saved starting from the page offset; in response to successfully writing the parameters to be saved to the Page1, perform a write position legality check on the Page2; if the check is legal, write the parameters to be saved in the Page2 starting from the page offset; when the relationship is that the remaining space is less than the number of bytes, erase the Page1, and write the parameters to be saved starting from the starting position of the Page1; in response to successfully writing the parameters to be saved to the Page1, erase the Page2, and write the parameters to be saved starting from the starting position of the Page2.
[0017] In some embodiments, the writing module is used to search for the end mark from the end of the Page1 forward; in response to the end mark not being found, writing the parameters to be saved from the starting position of the Page1; in response to successfully writing the parameters to be saved into the Page1, performing a write position legality check on the Page2; when the check is legal, writing the parameters to be saved from the starting position of the Page2; when the check is illegal, erasing the Page2, and writing the parameters to be saved from the starting position of the Page2.
[0018] In some embodiments, the device further comprises: a verification module for verifying the legality of the parameters to be saved recorded in the Page1 and Page2; and a reading module for reading the parameters to be saved recorded in the Page1 if the verification is legal.
[0019] In some embodiments, the verification module is used to verify the position legitimacy of the parameters to be saved recorded in the Page1 and Page2; and / or to verify the legitimacy of the end mark in the Page1 and Page2; and / or to verify the legitimacy of the check code in the Page1 and Page2.
[0020] According to another aspect of the present application, an electronic device is also provided, comprising: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to execute any one of the parameter storage methods described above by executing the executable instructions.
[0021] According to another aspect of the present application, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the parameter storage method described in any one of the above is implemented.
[0022] According to another aspect of the present application, a computer program product is provided, including a computer program, wherein when the computer program is executed by a processor, the parameter storage method described above is implemented.
[0023] The technical solution provided by the embodiments of the present application includes at least the following beneficial effects:
[0024] The technical solution provided by the embodiment of the present application, after receiving the parameter storage instruction, first constructs the parameters to be saved in the SRAM, and then writes the parameters to be saved into two Pages in sequence, so that the parameters to be saved can be saved in the Flash of the MCU. Compared with the method of saving device parameters based on a backup battery after the embedded hardware device loses power, it can avoid adding additional backup batteries, reduce the hardware cost of the embedded hardware device, avoid the need to specifically occupy the MCU pins to notify the MCU to save the device parameters, and increase the MCU pin resources available to the embedded hardware device.
[0025] It should be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] The drawings herein are incorporated into the specification and constitute a part of the specification, illustrate embodiments consistent with the present application, and together with the specification are used to explain the principles of the present application. Obviously, the drawings described below are only some embodiments of the present application, and for ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0027] Figure 1 A flow chart of a parameter storage method in one embodiment of the present application is shown;
[0028] Figure 2 A schematic diagram of a structure of parameters to be saved in one embodiment of the present application is shown;
[0029] Figure 3 A flowchart showing a method of writing parameters to be saved to Page1 and Page2 in one embodiment of the present application is shown;
[0030] Figure 4 A flowchart showing another embodiment of the present application for writing parameters to be saved into Page1 and Page2 is shown;
[0031] Figure 5 A flowchart of reading parameters to be saved in one embodiment of the present application is shown;
[0032] Figure 6 A schematic diagram showing Page1 and Page2 in one embodiment of the present application is shown;
[0033] Figure 7 A schematic diagram showing Page1 and Page2 in another embodiment of the present application is shown;
[0034] Figure 8 A schematic diagram showing Page1 and Page2 in yet another embodiment of the present application is shown;
[0035] Fig. 9 A schematic diagram showing Page1 and Page2 in another embodiment of the present application is shown;
[0036] Fig.10 A schematic diagram of a parameter storage device in one embodiment of the present application is shown;
[0037] Fig.11 A structural block diagram of an electronic device in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0038] Example embodiments will now be described more fully with reference to the accompanying drawings. However, example embodiments can be implemented in a variety of forms and should not be construed as limited to the examples set forth herein; rather, these embodiments are provided so that this application will be more comprehensive and complete and fully convey the concepts of the example embodiments to those skilled in the art. The described features, structures, or characteristics may be combined in any suitable manner in one or more embodiments.
[0039] In addition, the accompanying drawings are only schematic illustrations of the present application and are not necessarily drawn to scale. The same reference numerals in the figures represent the same or similar parts, and thus their repeated description will be omitted. Some of the block diagrams shown in the accompanying drawings are functional entities and do not necessarily correspond to physically or logically independent entities. These functional entities can be implemented in software form, or implemented in one or more hardware modules or integrated circuits, or implemented in different networks and / or processor devices and / or microcontroller devices.
[0040] In the related art, there are generally two ways to implement power-off parameter preservation:
[0041] 1. Add a backup battery (or farad-level capacitor) to the hardware, and notify the MCU immediately after the main battery loses power. The MCU uses the backup battery to save the device parameters.
[0042] The disadvantages of this method are: it requires additional hardware costs (backup batteries or capacitors), and a hardware mechanism is required to notify the MCU of power failure, which occupies an MCU pin resource and is not suitable for embedded hardware devices that are cost-sensitive or have limited MCU resources.
[0043] 2. Use a secure log file system to complete the secure storage of device parameters (such as the ext3 or later file system on the Linux operating system).
[0044] Disadvantages of this method: The design process and implementation of the log file are extremely complex. The complexity of the implementation causes more storage resources of the MCU to be occupied. The complexity of the design logic leads to long execution time, poor timeliness, and difficulty in quickly saving device parameters.
[0045] In view of the problems existing in the above two solutions, the technical solution provided by this application, after receiving the parameter storage instruction, first constructs the parameters to be saved in SRAM, and then writes the parameters to be saved into two Pages in sequence, so that the parameters to be saved can be saved in the Flash of MCU. Compared with the method of saving device parameters based on backup batteries after the embedded hardware device loses power, it can avoid adding additional backup batteries, reduce the hardware cost of embedded hardware devices, avoid the need to occupy MCU pins to notify MCU to save device parameters, and increase the MCU pin resources available for embedded hardware devices. Compared with the method of using a secure log file system to store parameters, the method of storing parameters in this application is simpler in implementation, and the execution program used occupies less storage resources of MCU. The simple logic makes the storage parameter method highly time-effective, conducive to quickly saving device parameters, and reduces the probability of parameter storage failure caused by sudden power failure.
[0046] To facilitate understanding, before introducing the embodiments of the present application, several terms involved in the embodiments of the present application are first explained as follows.
[0047] MCU: Also known as Single Chip Microcomputer or MCU, it is a chip-level computer that appropriately reduces the frequency and specifications of the central processing unit (CPU) and integrates peripheral interfaces such as memory, timer, storage (Flash), USB (Universal Serial Bus), A / D (input / output) conversion, etc. on a single chip;
[0048] Flash: A data storage unit integrated in the MCU chip, which can be used to store program execution code and other data. Flash is organized into multiple pages, each of which is a page. The capacity of each page is determined by the MCU model and is generally 512 bytes (or 1024 bytes, 2048 bytes, 4096 bytes). Flash has two important characteristics:
[0049] 1. The update operation characteristic of each bit of each Byte in the Page is that it can only be written as 0, not 1. Therefore, the MCU supports the erase operation. Once the erase operation is performed, all the data in the entire Page will be rewritten as 1.
[0050] 2. One erase operation of a Page affects its lifespan (the lifespan of a Page is usually 20,000 to 100,000 times).
[0051] SRAM: A data storage unit integrated in the MCU chip. It can be used for MCU operation data or variables. The data stored in SRAM will be lost after power failure. When the embedded hardware device is running, SRAM is used as temporary data storage space.
[0052] The specific implementation of the embodiment of the present application is described in detail below in conjunction with the accompanying drawings.
[0053] Under the above system architecture, a parameter storage method is provided in an embodiment of the present application, and the method can be executed by any electronic device with computing and processing capabilities, for example, the electronic device MCU.
[0054] Figure 1 A flow chart of a parameter storage method in an embodiment of the present application is shown as follows: Figure 1 As shown, the parameter storage method provided in the embodiment of the present application includes the following S101 to S103.
[0055] S101, in response to receiving a parameter storage instruction, constructing a parameter to be saved in an SRAM, wherein the data structure of the parameter to be saved includes a start flag, data to be saved, a check code of the data to be saved, and an end flag.
[0056] The embodiments of the present application do not limit under what circumstances the MCU will receive the parameter storage instruction. For example, when the device parameters of the embedded hardware device configuring the MCU change or in other preset circumstances, the device parameters need to be re-stored, and the parameter storage instruction is sent to the MCU.
[0057] Two pages are randomly selected from a plurality of pages included in the Flash, and the two pages are defined in advance as Page1 and Page2.
[0058] The number of bytes included in the start mark is a first value, the number of bytes included in the end mark is a second value, the number of bytes included in the data to be saved is a third value, and the number of bytes included in the check code is a fourth value.
[0059] The embodiments of the present application do not limit the number of bytes included in each field of the parameter to be saved, and can be set according to experience. For example, the first value and the second value are both 2, the third value is 200, 210, etc., and the fourth value is 2 or 3, etc. In order to facilitate understanding of the technical solution in the present application, the following description is based on the example that the first number, the second number, and the fourth number are all 2, and no further description is given later.
[0060] In one embodiment, constructing parameters to be saved in SRAM may include: creating entity variables of the parameters to be saved in SRAM; filling in the start flag and the end flag in the entity variable; turning off the global interrupt of the MCU; obtaining the data to be saved and filling it into the data to be saved part of the entity variable; calculating the check code of the data to be saved part of the entity variable to obtain the check code data, and filling the check code data into the check code part of the entity variable; using the entity variable as the parameter to be saved.
[0061] For example, an entity variable created in SRAM can be Figure 2 As shown, the parameter to be saved is Para_data, the start mark is Dummy_start, the parameter to be saved is Data, the check code of the parameter to be saved is CC, and the end mark is Dummy_end. It should be noted that the specific names of the various fields in the entity variables created in the SRAM are only exemplary.
[0062] Filling the start flag and end flag in the entity variable includes updating the two bytes corresponding to Dummy_start to the first preset byte string, and updating the two bytes corresponding to Dummy_end to the second preset byte string. The embodiments of the present disclosure do not limit the specific contents of the first preset byte string and the second preset byte string. Any byte string that can be used to identify the starting position of the parameter to be saved can be used as the first preset byte string, and any byte string that can be used to identify the ending position of the parameter to be saved can be used as the second preset byte string. The first preset byte string is different from the second preset byte string.
[0063] By disabling the global interrupt of the MCU, it can be prevented that after the check code of the data to be saved in Data is calculated, the data to be saved in Data is updated again, causing the check code to change, and the previously calculated check code is invalid, which causes problems due to the check code in the subsequent legitimacy check.
[0064] Calculate the check code of the data to be saved in the entity variable, that is, calculate the check code of the data to be saved in Data, and then fill the check code into the byte corresponding to CC in.
[0065] The embodiments of the present application do not limit the method for calculating the check code of the data portion to be saved. For example, CRC (Cyclic Redundancy Check) can be used to calculate the check code of the data portion to be saved.
[0066] After completing the filling of Dummy_start, Data, CC and Dummy_end in Para_data, the filled entity variables are used as parameters to be saved.
[0067] S102, writing the parameters to be saved into Page1 and Page2 in sequence.
[0068] The embodiments of the present application do not limit how to write the parameters to be saved into Page1 and Page2 in sequence.
[0069] In one embodiment, Figure 3 As shown, writing the parameters to be saved into Page1 and Page2 in sequence may include the following S301 to S306.
[0070] S301, searching for an end mark from the end of Page1.
[0071] S302: In response to finding the end mark, determine the relationship between the remaining space of Page1 and the number of bytes included in the parameters to be saved.
[0072] S303, when the relationship is that the remaining space is not less than the number of bytes, the position of the next byte of the found end mark is used as the page offset, and the parameters to be saved are written starting from the page offset.
[0073] S304, in response to successfully writing the parameters to be saved into Page1, performing a write position legality check on Page2; if the check is legal, writing the parameters to be saved into Page2 starting from the page offset.
[0074] S305 , when the relationship is that the remaining space is less than the number of bytes, Page 1 is erased, and the parameters to be saved are written from the beginning position of Page 1 .
[0075] S306 , in response to successfully writing the parameters to be saved into Page 1 , erasing Page 2 , and starting to write the parameters to be saved from the beginning position of Page 2 .
[0076] It should be noted that the above S301-S306 are not executed completely in the order of steps, but can be executed in any order that can achieve writing into Page1 and Page2 according to the content executed by the body.
[0077] Since Page1 is used to store the parameters to be saved, and the data structure of the parameters to be saved includes an end mark, the end mark is searched from the end of Page1. If the end mark is not found, it means that Page1 has not written any parameters. If the end mark is found, it means that Page1 has written the parameters to be saved.
[0078] The number of bytes included in the parameters to be saved is fixed, that is, the number of bytes is a fixed value. In one embodiment, determining the relationship between the remaining space of Page1 and the number of bytes included in the parameters to be saved may include: taking the number of bytes included between the next byte of the end mark found to the end of Page1 as the remaining space of Page1; comparing the relationship between the remaining space and the fixed value, if the remaining space is not less than the fixed value, it means that Page1 can still write the complete parameters to be saved. If the remaining space is less than the fixed value, it means that Page1 cannot write the complete parameters to be saved, and Page1 needs to be erased in this case to record the latest parameters to be saved that need to be stored.
[0079] In one embodiment, the process of writing the parameters to be saved into Page1 includes: writing the parameters to be saved into Page1 according to the update instruction provided by the MCU, and waiting for the writing result; reading the written data from Page1 (Flash) and comparing them with the parameters to be saved constructed in the SRAM to see if they are the same.
[0080] If the above writing result is successful and the data read out and written are the same as the parameters to be saved constructed in the SRAM, it means that the parameters to be saved are successfully written into Page 1, otherwise the end mark is searched forward from the end of Page 1 to start the writing process again.
[0081] In one embodiment, the write position legitimacy check for Page2 may include: at the page offset of Page2, if the contents of the first two bytes are normally an end mark, and the consecutive fixed value bytes backward are all 0xFF (all 1), then the parameters to be saved are written into Page2; if any condition is not met, the operation is stopped.
[0082] In one embodiment, the process of writing the parameters to be saved into Page2 includes: writing the parameters to be saved into Page2 according to the update instruction provided by the MCU, and waiting for the writing result; reading the written data from Page2 (Flash) and comparing them with the parameters to be saved constructed in the SRAM to see if they are the same.
[0083] If the above writing result is successful and the data read out and written are the same as the parameters to be saved constructed in the SRAM, it means that the parameters to be saved are successfully written into Page2, otherwise the writing process starts again from the end of Page1 to find the end mark.
[0084] In one embodiment, Figure 4 As shown, in Figure 3 After S301 in the illustrated embodiment, S401 to S404 may also be included.
[0085] S401 , in response to not finding the end mark, writing the parameters to be saved from the start position of Page 1 .
[0086] S402: In response to successfully writing the parameters to be saved into Page 1, a write position validity check is performed on Page 2.
[0087] S403, when checking for legality, write the parameters to be saved starting from the beginning of Page2.
[0088] S404, when the verification is illegal, erase Page2, and write the parameters to be saved from the beginning of Page2.
[0089] Among them, the write position legitimacy check of Page2 in S402 may include: if the consecutive fixed value bytes from the revelation position of Page2 are all 0xFF (all 1), the write position legitimacy check of Page2 passes and the check is legal, otherwise the check is illegal.
[0090] In one embodiment, after the parameters to be saved are successfully written into Page2, the global interrupt of the MCU is re-enabled.
[0091] The technical solution provided by the embodiment of the present application, after receiving the parameter storage instruction, first constructs the parameters to be saved in the SRAM, and then writes the parameters to be saved into two Pages in sequence, so that the parameters to be saved can be saved in the Flash of the MCU. Compared with the method of saving device parameters based on a backup battery after the embedded hardware device loses power, it can avoid adding additional backup batteries, reduce the hardware cost of the embedded hardware device, avoid the need to specifically occupy the MCU pins to notify the MCU to save the device parameters, and increase the MCU pin resources available to the embedded hardware device.
[0092] Compared with the method of using a secure log file system to store parameters, the method of storing parameters in this application is simpler to implement, and the execution program used occupies less storage resources of the MCU. The simple logic makes this method of storing parameters highly timely, conducive to quickly saving device parameters, and reduces the probability of parameter storage failure caused by sudden power failure.
[0093] In one embodiment, Figure 5 As shown, after the parameters to be saved are completed, the subsequent reading process of the parameters to be saved may include S501 and S502.
[0094] S501, perform a validity check on the parameters to be saved recorded in Page1 and Page2.
[0095] In one embodiment, performing a validity check on the parameters to be saved recorded in Page1 and Page2 may include: performing a position validity check on the parameters to be saved recorded in Page1 and Page2; and / or, performing a validity check on the end mark in Page1 and Page2; and / or, performing a validity check on the check code in Page1 and Page2.
[0096] In one embodiment, the positional validity check of the parameters to be saved recorded in Page1 and Page2 may include: searching for the end mark in reverse from the end of Page1 and Page2 respectively, and the found positions are the first page offset and the second page offset (both are the positions of the first byte of the end mark). If the first page offset and the second page offset meet any one of the following four conditions, the position is considered to be legal.
[0097] Condition 1: If Figure 6 As shown, the first page offset is equal to the second page offset and is equal to 0.
[0098] Condition 2: If Figure 7 As shown, the first page offset is equal to the second page offset and neither is equal to 0.
[0099] Condition 3: If Figure 8 As shown, the first page offset - the second page offset = a fixed value (indicating that the difference between the first page offset and the second page offset is the number of bytes of the parameter to be saved).
[0100] Condition 4: If Fig. 9 As shown, the second page offset is greater than the first page offset, and the first page offset is equal to 0.
[0101] Condition 1 means that the embedded hardware device parameters have not been factory calibrated and the default parameters need to be used for the above Figure 2 Corresponding parameter storage process, after the parameter storage process is completed, the system resets and re-enters the reading process.
[0102] Condition 2 indicates that the last parameters to be saved are successfully stored in both Page 1 and Page 2.
[0103] Condition 3 means that only Page 1 was successfully written when the parameters were stored last time, but Page 2 was not successfully written. At this time, the latest parameters to be saved in Page 1 need to be updated (stored) to Page 2, where the update offset position of Page 2 is the offset of the first page. After the update is completed, the system is reset and re-enters the reading process.
[0104] Condition 4 means that only Page 1 was erased when the parameters were stored last time, and Page 1 did not successfully write the parameters to be saved, while Page 2 did not perform the storage operation. At this time, the latest parameters in Page 2 need to be updated to Page 1, where the update offset position of Page 1 is the second page offset. After the update is completed, the system is reset and re-enters the reading process.
[0105] It should be noted that Figure 6-Figure 9 In the example, 1-N in Page1 and Page2 means that each Page can store the next N parameters to be saved.
[0106] In one embodiment, checking the legitimacy of the end mark in Page1 and Page2 may include: if all of the following three conditions are met, it is considered that the legitimacy check of the end mark has passed.
[0107] Condition 1: The first page offset is equal to the second page offset.
[0108] Condition 2: The first page offset is not equal to 0.
[0109] Condition 3: The first page offset and the content at the next byte of Page1, as well as the second page offset and the content at the next byte of Page2 are all the byte contents corresponding to the end mark.
[0110] In one embodiment, checking the legitimacy of the check codes in Page1 and Page2 may include: determining the data content at the data to be saved in the parameters to be saved of Page1 and Page2 respectively according to the first page offset, the second page offset and the data structure of the parameters to be saved, and calculating the check codes of the data content to obtain check code 1 and check code 2; when the content of the two bytes forward of the first page offset in Page1 is equal to check code 1, and the content of the two bytes forward of the second page offset in Page2 is equal to check code 2, it is considered that the legitimacy check of the check codes in Page1 and Page2 has passed.
[0111] S502: If the verification is legal, read the parameters to be saved recorded in Page1.
[0112] The streamlined data structure of the parameters to be saved can ensure the efficiency and reliability of parameter updates. In addition, by designing a method that can write multiple parameters to be saved simultaneously in one Page, balanced writing can be achieved, thereby increasing the service life of the Page and thus the service life of the MCU.
[0113] Based on the same inventive concept, the present application also provides a parameter storage device in the embodiment, as described in the following embodiment. Since the principle of solving the problem in the device embodiment is similar to that in the above method embodiment, the implementation of the device embodiment can refer to the implementation of the above method embodiment, and the repeated parts will not be repeated.
[0114] Fig.10 A schematic diagram of a parameter storage device in an embodiment of the present application is shown, and the device is applied to an MCU, the MCU includes an SRAM and a Flash, and the Flash includes Page1 and Page2, such as Fig.10 As shown, the device includes: a construction module 1001, which is used to construct the parameters to be saved in the SRAM in response to receiving the parameter storage instruction, and the data structure of the parameters to be saved includes a start flag, data to be saved, a check code of the data to be saved and an end flag; a writing module 1002, which is used to write the parameters to be saved into Page1 and Page2 in sequence.
[0115] In some embodiments, a construction module 1001 is used to create an entity variable of a parameter to be saved in the SRAM; fill in a start flag and an end flag in the entity variable; turn off the global interrupt of the MCU; obtain the data to be saved, and fill it into the data part to be saved of the entity variable; calculate the check code of the data part to be saved of the entity variable, obtain the check code data, and fill the check code data into the check code part of the entity variable; and use the entity variable as a parameter to be saved.
[0116] In some embodiments, the write module 1002 is used to search for an end mark from the end of Page 1 forward; in response to finding the end mark, determine the relationship between the remaining space of Page 1 and the number of bytes included in the parameters to be saved; when the relationship is that the remaining space is not less than the number of bytes, use the position of the next byte of the found end mark as the page offset, and write the parameters to be saved starting from the page offset; in response to successfully writing the parameters to be saved to Page 1, perform a write position legitimacy check on Page 2; if the check is legal, write the parameters to be saved in Page 2 starting from the page offset; when the relationship is that the remaining space is less than the number of bytes, erase Page 1, and write the parameters to be saved starting from the starting position of Page 1; in response to successfully writing the parameters to be saved to Page 1, erase Page 2, and write the parameters to be saved starting from the starting position of Page 2.
[0117] In some embodiments, the write module 1002 is used to search for an end mark from the end of Page 1 forward; in response to not finding the end mark, write the parameters to be saved from the starting position of Page 1; in response to successfully writing the parameters to be saved to Page 1, perform a write position legitimacy check on Page 2; when the check is legal, write the parameters to be saved from the starting position of Page 2; when the check is illegal, erase Page 2 and write the parameters to be saved from the starting position of Page 2.
[0118] In some embodiments, the device further includes: a verification module 1003 for verifying the legality of the parameters to be saved recorded in Page1 and Page2; and a reading module 1004 for reading the parameters to be saved recorded in Page1 if the verification is legal.
[0119] In some embodiments, the verification module 1003 is used to verify the legitimacy of the position of the parameters to be saved recorded in Page1 and Page2; and / or to verify the legitimacy of the end mark in Page1 and Page2; and / or to verify the legitimacy of the check code in Page1 and Page2.
[0120] The technical solution provided by the embodiment of the present application, after receiving the parameter storage instruction, first constructs the parameters to be saved in the SRAM, and then writes the parameters to be saved into two Pages in sequence, so that the parameters to be saved can be saved in the Flash of the MCU. Compared with the method of saving device parameters based on a backup battery after the embedded hardware device loses power, it can avoid adding additional backup batteries, reduce the hardware cost of the embedded hardware device, avoid the need to specifically occupy the MCU pins to notify the MCU to save the device parameters, and increase the MCU pin resources available to the embedded hardware device.
[0121] It should be noted that the construction module 1001 and the writing module 1002 correspond to S101 and S102 in the method embodiment, the inspection module 1003 and the reading module 1004 correspond to S501 and S502 in the method embodiment, and the examples and application scenarios implemented by the modules and the corresponding steps are the same, but are not limited to the contents disclosed in the method embodiment. It should be noted that the modules as part of the device can be executed in a computer system such as a set of computer executable instructions.
[0122] Those skilled in the art will appreciate that various aspects of the present application may be implemented as a system, method or program product. Therefore, various aspects of the present application may be specifically implemented in the following forms, namely: a complete hardware implementation, a complete software implementation (including firmware, microcode, etc.), or a combination of hardware and software, which may be collectively referred to as "circuit", "module" or "system" herein.
[0123] Refer to the following Fig.11 1100 according to this embodiment of the present application is described. Fig.11 The electronic device 1100 shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0124] like Fig.11 As shown, the electronic device 1100 is in the form of a general computing device. The components of the electronic device 1100 may include but are not limited to: at least one processing unit 1110, at least one storage unit 1120, and a bus 1130 connecting different system components (including the storage unit 1120 and the processing unit 1110).
[0125] The storage unit stores a program code, which can be executed by the processing unit 1110, so that the processing unit 1110 performs the steps of various exemplary embodiments of the present application described in the above "Exemplary Method" section of this specification. For example, the processing unit 1110 can perform the following steps of the above method embodiment: S101-S102, S501-S502.
[0126] The storage unit 1120 may include a readable medium in the form of a volatile storage unit, such as a random access memory unit (RAM) 1121 and / or a cache memory unit 1122 , and may further include a read-only memory unit (ROM) 1123 .
[0127] The storage unit 1120 may also include a program / utility 1124 having a set (at least one) of program modules 1125, such program modules 1125 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.
[0128] Bus 1130 may represent one or more of several types of bus structures, including a memory unit bus or memory unit controller, a peripheral bus, an accelerated graphics port, a processing unit, or a local bus using any of a variety of bus architectures.
[0129] The electronic device 1100 may also communicate with one or more external devices 1140 (e.g., keyboards, pointing devices, Bluetooth devices, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 1100, and / or communicate with any device that enables the electronic device 1100 to communicate with one or more other computing devices (e.g., routers, modems, etc.). Such communication may be performed via an input / output (I / O) interface 1150. Furthermore, the electronic device 1100 may also communicate with one or more networks (e.g., local area networks (LANs), wide area networks (WANs), and / or public networks, such as the Internet) via a network adapter 1160. As shown, the network adapter 1160 communicates with other modules of the electronic device 1100 via a bus 1130. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 1100, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0130] Through the description of the above implementation methods, it is easy for those skilled in the art to understand that the example implementation methods described here can be implemented by software or by combining software with necessary hardware. Therefore, the technical solution according to the implementation methods of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.) or on a network, and includes several instructions to enable a computing device (which can be a personal computer, a server, a terminal device, or a network device, etc.) to execute the method according to the implementation methods of the present application.
[0131] In particular, according to an embodiment of the present application, the process described above with reference to the flowchart may be implemented as a computer program product, which includes: a computer program, which implements the above parameter storage method when executed by a processor.
[0132] In an exemplary embodiment of the present application, a computer-readable storage medium is also provided, which may be a readable signal medium or a readable storage medium. The computer-readable storage medium stores a program product capable of implementing the above method of the present application.
[0133] In some possible implementations, various aspects of the present application may also be implemented in the form of a program product, which includes program code. When the program product is run on a terminal device, the program code is used to enable the terminal device to execute the steps of various exemplary implementations of the present application described in the above “Exemplary Method” section of this specification.
[0134] More specific examples of computer-readable storage media in the present application may include, but are not limited to, an electrical connection having one or more conductors, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0135] In the present application, a computer readable storage medium may include a data signal propagated in baseband or as part of a carrier wave, wherein a readable program code is carried. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. A readable signal medium may also be any readable medium other than a readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device.
[0136] Alternatively, the program code contained on the computer-readable storage medium may be transmitted using any appropriate medium, including but not limited to wireless, wired, optical cable, RF, etc., or any suitable combination of the foregoing.
[0137] In specific implementation, the program code for performing the operation of the present application can be written in any combination of one or more programming languages, including object-oriented programming languages such as Java, C++, etc., and also conventional procedural programming languages such as "C" language or similar programming languages. The program code can be executed entirely on the user computing device, partially on the user device, as an independent software package, partially on the user computing device and partially on the remote computing device, or completely on the remote computing device or server. In the case of a remote computing device, the remote computing device can be connected to the user computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computing device (for example, using an Internet service provider to connect through the Internet).
[0138] It should be noted that, although several modules or units of the equipment for action execution are mentioned in the above detailed description, this division is not mandatory. In fact, according to the embodiments of the present application, the features and functions of two or more modules or units described above can be embodied in one module or unit. On the contrary, the features and functions of one module or unit described above can be further divided into being embodied by multiple modules or units.
[0139] In addition, although the steps of the method in the present application are described in a specific order in the drawings, this does not require or imply that the steps must be performed in this specific order, or that all the steps shown must be performed to achieve the desired results. Additionally or alternatively, some steps may be omitted, multiple steps may be combined into one step, and / or one step may be decomposed into multiple steps, etc.
[0140] Through the description of the above implementation modes, it is easy for those skilled in the art to understand that the example implementation modes described here can be implemented by software or by combining software with necessary hardware. Therefore, the technical solution according to the implementation mode of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.) or on a network, and includes several instructions to enable a computing device (which can be a personal computer, a server, a mobile terminal, or a network device, etc.) to execute the method according to the implementation mode of the present application.
[0141] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the invention disclosed herein. The present application is intended to cover any modification, use or adaptation of the present application, which follows the general principles of the present application and includes common knowledge or customary techniques in the art that are not disclosed in the present application. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present application are indicated by the appended claims.
Claims
1. A parameter storage method, characterized in that: Applied to a microcontroller unit MCU, the MCU includes a static random access memory SRAM and a flash memory Flash, the Flash includes pages Page1 and Page2, including: In response to receiving a parameter storage instruction, constructing a parameter to be saved in the SRAM, wherein the data structure of the parameter to be saved includes a start flag, data to be saved, a check code of the data to be saved, and an end flag; The parameters to be saved are written into the Page1 and the Page2 in sequence.
2. The method according to claim 1, characterized in that: The step of constructing the parameters to be saved in the SRAM comprises: Creating entity variables of the parameters to be saved in the SRAM; Filling the start flag and the end flag in the entity variable; Disable the global interrupt of the MCU; Obtain the data to be saved, and fill it into the data to be saved part of the entity variable; Calculating the check code of the data portion to be saved of the entity variable to obtain check code data, and filling the check code data into the check code portion of the entity variable; The entity variable is used as the parameter to be saved.
3. The method according to claim 1, characterized in that The step of sequentially writing the parameters to be saved into the Page1 and the Page2 includes: Search for the end mark from the end of Page1 forward; In response to finding the end mark, determining the relationship between the remaining space of Page1 and the number of bytes included in the parameter to be saved; When the relationship is that the remaining space is not less than the number of bytes, the position of the next byte of the end mark found is used as the page offset, and the parameters to be saved are written starting from the page offset; in response to the successful writing of the parameters to be saved into the Page1, the legitimacy of the write position is checked for the Page2; if the check is legal, the parameters to be saved are written into the Page2 starting from the page offset; When the relationship is that the remaining space is less than the number of bytes, erase Page1 and write the parameters to be saved from the starting position of Page1; in response to successfully writing the parameters to be saved into Page1, erase Page2 and write the parameters to be saved from the starting position of Page2.
4. The method according to claim 1, characterized in that The step of sequentially writing the parameters to be saved into the Page1 and the Page2 includes: Search for the end mark from the end of Page1 forward; In response to not finding the end mark, writing the parameters to be saved from the start position of Page1; in response to successfully writing the parameters to be saved into Page1, performing a write position legitimacy check on Page2; When the verification is legal, the parameters to be saved are written from the start position of the Page2; when the verification is illegal, the Page2 is erased, and the parameters to be saved are written from the start position of the Page2.
5. The method according to claim 1, characterized in that Also includes: Performing a validity check on the parameters to be saved recorded in Page1 and Page2; If the verification is legal, the parameters to be saved recorded in the Page1 are read.
6. The method according to claim 5, characterized in that The checking of the validity of the parameters to be saved recorded in Page1 and Page2 includes: Performing a location validity check on the parameters to be saved recorded in Page1 and Page2; and / or, checking the legitimacy of the end marks in Page1 and Page2; And / or, verify the legitimacy of the verification codes in Page1 and Page2.
7. A parameter storage device, characterized in that: Applied to a microcontroller unit MCU, the MCU includes a static random access memory SRAM and a flash memory Flash, the Flash includes pages Page1 and Page2, including: A construction module, configured to construct, in response to receiving a parameter storage instruction, parameters to be stored in the SRAM, wherein the data structure of the parameters to be stored includes a start flag, data to be stored, a check code of the data to be stored, and an end flag; The writing module is used to write the parameters to be saved into the Page1 and the Page2 in sequence.
8. An electronic device, characterized in that: include: processor; as well as A memory, configured to store executable instructions of the processor; The processor is configured to execute the parameter storage method according to any one of claims 1 to 6 by executing the executable instructions.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the parameter storage method according to any one of claims 1 to 6 is implemented.
10. A computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the parameter storage method according to any one of claims 1 to 6 is implemented.