Random number generation method and micro-control unit using same

By utilizing the randomness of the GPIO port and internal clock of the microcontroller unit, and writing it to flash memory, the problem of the lack of a random number generator for the low-cost microcontroller unit is solved, and randomness without RNG module is achieved Number generation ensures randomness and consistency of random numbers.

CN119179462APending Publication Date: 2024-12-24SCHNEIDER ELECTRIC IND SAS
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310744405.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-06-21
Publication Date
2024-12-24

AI Technical Summary

Technical Problem

The lack of random number generator for low-cost microcontrollers leads to difficulties in application scenarios where random numbers are needed, and cannot meet the needs of microcontroller products equipped with RNG modules.

Method used

By using the reading of the GPIO port of the microcontroller unit in the floating state and the clock difference between the internal clock as the seed of the random number generation algorithm, a random number is generated and the generated random number is written into the flash memory for the next time. Generate new random numbers when powered on, avoiding dependence on RNG modules and addition of additional components.

Benefits of technology

It realizes the ability to generate random numbers in low-cost microcontroller unit products without RNG modules, ensuring that the random numbers generated each time power on are different from the previous one, with the same randomness as those with RNG module products, and is suitable for those without RNG. The microcontrol unit product of the module.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119179462A_ABST
    Figure CN119179462A_ABST
Patent Text Reader

Abstract

A method of generating a random number for a micro-control unit, a micro-control unit using the method, and an apparatus including the micro-control unit are provided. The method comprises the following steps: in response to the power-on of the micro-control unit, reading a random number generated by the previous power-on of the micro-control unit from a flash memory of the micro-control unit; in the case of reading failure, random information of the micro-control unit is collected, the random information is used as a seed of a random number generation algorithm to generate a random number, and in the case of reading success, the read random number is used as the seed of the random number generation algorithm to generate a random number; and writing the generated random number into a flash memory of the micro-control unit.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to a random number generation method, a micro control unit using the method, and a device including the micro control unit. Background Art

[0002] Some low-cost microcontroller units (MCU) products do not have a random number generator (RNG). However, in some application scenarios, electrical devices equipped with the MCU need to generate random numbers. In order to avoid replacing the low-cost MCU products, a random number generation method that can be used for such MCUs is desired. Summary of the invention

[0003] According to one aspect of the present disclosure, a random number generation method for a microcontroller unit is provided. The method comprises: in response to the microcontroller unit being powered on, reading the random number generated by the microcontroller unit at the last power-on in the flash memory of the microcontroller unit; in the case of a read failure, collecting random information of the microcontroller unit, and using the random information as a seed of a random number generation algorithm to generate a random number, and in the case of a read success, using the read random number as a seed of a random number generation algorithm to generate a random number; and writing the generated random number into the flash memory of the microcontroller unit.

[0004] Optionally, writing the generated random number into the flash memory of the micro control unit includes: sequentially writing the generated random number into one of the multiple storage units according to the flash memory addresses of the multiple storage units in the first space selected in the flash memory, each of the multiple storage units corresponding to the minimum write unit of the flash memory.

[0005] Optionally, reading the most recently stored random number includes: searching for the storage unit in which the random number is most recently written among multiple storage units in the first space and reading the random number therein; and when a random number is written to the last storage unit in the first space in flash address order, erasing the first space to reuse the first space to store new random numbers.

[0006] Optionally, the first space includes multiple pages, and the writing of the generated random number into the flash memory of the microcontroller unit also includes: when the flash memory address of the storage unit in the first space where the random number is to be written is the starting address of one of the multiple pages, a predetermined value is written into a storage unit for recording the usage of the page among the multiple storage units of the second space selected in the flash memory, and the multiple storage units of the second space are respectively used to record the usage of each of the multiple pages of the first space.

[0007] Optionally, searching for a storage unit in which a random number is most recently written in a plurality of storage units in the first space includes searching for a storage unit in which a random number is most recently written in a page of the first space corresponding to a storage unit in which a predetermined value is most recently written in the second space.

[0008] Optionally, the plurality of storage units of the second space respectively used to record the usage status of each of the plurality of pages of the first space are also erased when the first space is erased.

[0009] According to another aspect of the present disclosure, another random number generation method for a microcontroller unit is provided. The method includes: in response to the microcontroller unit being powered on, collecting random information of the microcontroller unit; and using the random information as a seed of a random number generation algorithm to generate a random number, wherein the random information includes a reading of a general-purpose input / output port (GPIO) of the microcontroller unit in a floating state and / or a clock difference between a high-speed internal clock and a low-speed internal clock of the microcontroller unit.

[0010] According to another aspect of the present disclosure, a microcontroller unit is provided, comprising: a reading module, which reads a random number generated by the microcontroller unit at a previous power-on in a flash memory of the microcontroller unit in response to the microcontroller unit being powered on; a collection module, which collects random information of the microcontroller unit when the reading module fails to read; a random number generation module, which uses the random information as a seed of a random number generation algorithm to generate a random number when the reading module fails to read, and uses the read random number as a seed of a random number generation algorithm to generate a random number when the reading module succeeds in reading; and a writing module, which is used to write the generated random number into the flash memory of the microcontroller unit.

[0011] According to another aspect of the present disclosure, another microcontroller unit is provided, comprising: an acquisition module and a random number generation module, wherein the acquisition module acquires random information of the microcontroller unit in response to the microcontroller unit being powered on, wherein the random information includes a reading of a general input / output port of the microcontroller unit in a floating state and / or a clock difference between a high-speed internal clock and a low-speed internal clock of the microcontroller unit. The random number generation module generates a random number using the random information as a seed of a random number generation algorithm.

[0012] According to another aspect of the present disclosure, a device is provided, the device comprising the aforementioned micro control unit.

[0013] According to another aspect of the present disclosure, a computer-readable storage medium is provided, on which instructions executable by a processor are stored. When the instructions are executed, the processor is caused to perform any of the methods described above.

[0014] The random number generation method according to the above aspects of the present disclosure and the microcontroller unit using the method utilize the readings of the microcontroller unit's own universal input and output ports in a floating state and the randomness of the clock difference between the microcontroller unit's high-speed internal clock and the low-speed internal clock to generate random numbers. It does not rely on an RNG module and does not require the addition of additional components. It is suitable for application scenarios in which the aforementioned low-cost microcontroller unit products without an RNG module need to generate random numbers. BRIEF DESCRIPTION OF THE DRAWINGS

[0015] The various aspects, features and advantages of the present disclosure will become clearer and easier to understand through the following description of the embodiments in conjunction with the accompanying drawings, in which:

[0016] Figure 1 is a schematic flow chart of a random number generation method according to an embodiment of the present disclosure;

[0017] Figure 2A and Figure 2B is based on Figure 1 An exemplary distribution diagram of random numbers generated by the random number generation method;

[0018] Figure 3 is a schematic flow chart of a random number generation method according to another embodiment of the present disclosure;

[0019] Figure 4 is based on Figure 3 An exemplary distribution diagram of random numbers generated by the random number generation method shown;

[0020] Figure 5 is an exemplary distribution diagram of random numbers generated by a microcontroller unit having an RNG module using the RNG module;

[0021] Figure 6 is a schematic structural diagram of a first space according to an embodiment of the present disclosure;

[0022] Figure 7 is a diagram showing a schematic process of searching for the latest stored random number using a binary algorithm according to an embodiment of the present disclosure;

[0023] Figure 8 is a schematic structural diagram of a first space and a second space according to another embodiment of the present disclosure;

[0024] Fig. 9 is a schematic block diagram of a micro control unit according to an embodiment of the present disclosure;

[0025] Fig.10 is a schematic block diagram of a device including a micro control unit according to an embodiment of the present disclosure; and

[0026] Fig.11 is a schematic diagram of a computer-readable storage medium according to an embodiment of the present disclosure. DETAILED DESCRIPTION

[0027] The present disclosure will be described in detail below with reference to exemplary embodiments of the present disclosure. However, the present disclosure is not limited to the embodiments described herein, and it can be implemented in many different forms. The described embodiments are only used to make the present disclosure thorough and complete, and to fully convey the concept of the present disclosure to those skilled in the art. The features of the various embodiments described can be combined or replaced with each other, unless explicitly excluded or should be excluded according to the context.

[0028] Unless otherwise defined, the technical terms or scientific terms used in this disclosure should be understood by people with ordinary skills in the field to which this disclosure belongs. The words "first", "second" and similar words used in this disclosure do not indicate any order, quantity or importance, but are only used to distinguish different components.

[0029] In the drawings, the same reference numerals denote components having the same or similar structures or functions, and repeated descriptions thereof will be omitted in the following description.

[0030] Figure 1 is a schematic flowchart of a random number generation method according to an embodiment of the present disclosure.

[0031] refer to Figure 1 According to an embodiment of the present disclosure, the random number generation method 100 includes steps S110 and S120.

[0032] In step S110, in response to the microcontroller being powered on, random information of the microcontroller is collected, including, for example, a reading of a GPIO port of the microcontroller in a floating state and / or a clock difference between a high speed internal clock (HSI) and a low speed internal clock (LSI) of the microcontroller.

[0033] In one example, the reading of the GPIO port in a floating state can be used as a seed for generating random numbers. The GPIO port in a floating state is equivalent to an unconnected switch without any definite level signal input. Due to the presence of electromagnetic radiation and other noise sources in the surrounding environment, interference signals will be transmitted to the GPIO port, making its reading random. In addition, since the input circuit of the GPIO port usually has a high input impedance, the port is very sensitive to small current and voltage changes from its surrounding environment. This sensitivity further increases the randomness of its readings in a floating state. This randomness of the GPIO port can be avoided by connecting it to ground or power via a pull-up resistor or a pull-down resistor, but is used as a seed for generating random numbers in the present disclosure.

[0034] In another example, the clock difference between the HSI and LSI of the microcontroller unit can be used as a seed for generating random numbers. The microcontroller unit usually includes at least two independent clock sources, HSI and LSI. Since the accuracy of HSI is higher and the accuracy of LSI is lower, there is a clock difference between the two. For example, the clock difference between HSI and LSI can be detected by detecting the number of HSI clocks between two consecutive edges of the LSI signal, that is, the clock error of LSI with HSI as the clock reference. For example, it is known that the clock frequency of HSI is 8MHz and the clock frequency of LSI is 8000Hz. Due to the random error of LSI, the number of HSI clocks detected between two consecutive edges of the LSI signal is 880, which means that the actual clock period of LSI is 110 microseconds, and there is an error of 15 with its actual clock period of 125 microseconds. This 15 can be used as a seed to generate random numbers.

[0035] In another example, the combination of the reading of the GPIO port and the clock difference between the HSI and the LSI can also be used as a seed for generating a random number. For example, a function result (such as taking the average, taking the median, etc.) calculated with these two as independent variables is used as the seed for generating a random number.

[0036] The random number generation algorithm can be any algorithm known to those skilled in the art or appearing in the future to generate random numbers based on a random number seed. This article will not list them in detail to avoid obscuring the present disclosure.

[0037] like Figure 1As shown, in step S110, random information x of the microcontroller unit is collected in response to the microcontroller unit being powered on. In step S120, the random information x is used as the seed of the random number generation algorithm to generate a random number y. In a specific application scenario, it is expected that the microcontroller unit can generate a random number, and it is expected that the microcontroller unit can generate a random number different from the random number generated at the previous power-on each time it is powered on. Therefore, steps S110 and S120 can be performed each time the microcontroller unit is powered on. For example, corresponding to n times the microcontroller unit is powered on, the random information collected respectively is x0 to x n-1 , the random numbers generated are y0 to y n-1 n is an integer greater than 1. However, the present disclosure is not limited thereto, and step S110 and step S120 may be performed whenever a random number needs to be generated. For example, in another application scenario, the steps may be performed cyclically after the microcontroller unit is powered on to generate multiple random numbers.

[0038] In this way, the random number generation method 100 according to the embodiment of the present disclosure uses the random information provided by the microcontroller unit itself as a seed to generate random numbers. There is no need to configure an RNG module or additional components. This meets the need that the random number generated by the microcontroller unit each time it is powered on is different from the previous time in a specific application scenario. This method is suitable for low-cost microcontroller units that do not have an RNG module.

[0039] Figure 2A and Figure 2B is based on Figure 1 An exemplary distribution diagram of random numbers generated by the random number generation method.

[0040] refer to Figure 2A , which shows the distribution of random numbers generated by using the readings of the GPIO port of the microcontroller in a floating state as the seed of the random number generation algorithm. For example, in response to n power-ups of the microcontroller, the readings of the GPIO port in a floating state collected are respectively x0 to x n-1 , change x0 to x n-1 As the seed of the random number generation algorithm, generate random numbers y0 to y n-1 n is an integer greater than 1. As shown in the figure, 1 is generated about 130 times, 2 is generated about 10 times, 14 is generated about 80 times, and so on.

[0041] refer to Figure 2B , which shows the distribution of random numbers generated by using the clock difference between the HSI and LSI of the microcontroller as the seed of the random number generation algorithm. For example, in response to the n times of power-on of the microcontroller, the clock differences between the HSI and LSI respectively collected are x0 to x n-1 , change x0 to x n-1As the seed of the random number generation algorithm, generate random numbers y0 to y n-1 n is an integer greater than 1. For example, 1 is generated about 10 times, 5 is generated about 80 times, 8 is generated about 150 times, and so on.

[0042] Figure 3 is a schematic flowchart of a random number generation method according to another embodiment of the present disclosure.

[0043] Different from method 100, which uses the reading of the GPIO port in a floating state or the clock difference between HSI and LSI as a seed to generate a random number each time the microcontroller unit is powered on, method 300 uses the reading of the GPIO port in a floating state or the clock difference between HSI and LSI as a seed to generate the first random number only when the microcontroller unit is powered on for the first time, and calls the previously generated random number as a seed to generate the next random number each time the microcontroller unit is powered on thereafter.

[0044] refer to Figure 3 , method 300 includes steps S310 to S350.

[0045] In step S310, in response to the microcontroller unit being powered on, the random number generated in the previous power-on is read from the flash memory of the microcontroller unit. If the reading fails, it means that the microcontroller unit has not yet generated a random number, for example, when the microcontroller unit is powered on for the first time, when it is reset, or when the method 300 is first implemented on it, the first random number has not yet been generated, and the process proceeds to step S330. If the reading succeeds, it means that the random number has been generated in the previous power-on, and the process proceeds to step S340.

[0046] In step S320, random information of the microcontroller unit is collected. As mentioned above, this random information may include, for example, the reading of the GPIO port of the microcontroller unit in a floating state and / or the clock difference between the HSI and LSI of the microcontroller unit. Then, the process proceeds to step S330. In step S330, the first random number is generated by using the collected random information as a seed of a random number generation algorithm.

[0047] In step S340 , a random number is generated using the random number read in step S310 as a seed of a random number generation algorithm.

[0048] In step S350, the first random number generated in step S330 or the random number generated in step S340 is written into the flash memory of the micro control unit.

[0049] For example, taking the rand() function as a random number generation algorithm, when the microcontroller unit is powered on for the first time, a random number is read from the flash memory of the microcontroller unit. Since no random number has been written to the flash memory, the read fails. Then, random information x0 is collected (such as the reading of the GPIO port or the clock difference between HSI and LSI), and x0 is used as a seed to generate the first random number y0=rand(x0). The first random number y0 is then written to the flash memory. When the microcontroller unit is powered on for the second time, the random number generated at the first power-on is read from the flash memory of the microcontroller unit. If the read is successful, the second random number y1=rand(y0) is generated using the random number read as a seed. The second random number y1 is then also written to the flash memory. And so on, corresponding to the i-th power-on of the microcontroller unit, y i =rand(y i-1 ), where i=1,2,…,n, and n is an integer greater than or equal to 1.

[0050] It should be understood that, for ease of explanation, the above description describes that the microcontroller generates a random number each time it is powered on, but the present disclosure is not limited to this. Random numbers can be generated at any time as needed, and the microcontroller can also generate multiple random numbers each time it is powered on, each random number using the previously generated random number as a seed.

[0051] Figure 4 is based on Figure 3 An exemplary distribution diagram of random numbers generated by the random number generation method shown.

[0052] refer to Figure 4 , which shows the distribution of random numbers generated by executing method 300 using the reading of the GPIO port of the microcontroller in a floating state as the initial seed for generating the random number. For example, 0 is generated about 50 times, 1 is generated about 55 times, 3 is generated about 45 times, and so on.

[0053] Will Figure 4 and Figure 2A and 2B By comparison, it can be seen Figure 4 The random number distribution shown is obviously better than Figure 2A and Figure 2B The random number distribution shown has better randomness.

[0054] Figure 5 is an exemplary distribution diagram of random numbers generated by a microcontroller unit having an RNG module using the RNG module.

[0055] Then Figure 4 and Figure 5 By comparison, it can be seen Figure 4 The random number distribution shown has Figure 5 The random number distribution shown is fairly random.

[0056] Thus, the random number generation method 300 according to another embodiment of the present disclosure uses the random information provided by the microcontroller itself as the initial seed to generate random numbers, without configuring the RNG module or additional components, and can meet the need that the random number generated by the microcontroller each time it is powered on is different from the previous one in a specific application scenario, and the random number generated by it has the same randomness as the random number generated by the RNG module. The method 300 is suitable for low-cost microcontroller products without RNG modules, and is also suitable for replacing the function of the RNG module in microcontroller products equipped with RNG modules.

[0057] The above describes the basic principle of the random number generation method according to the embodiment of the present disclosure, which involves the reading of random numbers (step S310) and the storage of random numbers (step S350). Figures 6 to 8 Explain in more detail how random numbers are stored in the flash memory of the microcontroller unit and how random numbers are read from the flash memory.

[0058] Figure 6 is a schematic structural diagram of the first space according to an embodiment of the present disclosure.

[0059] As described in the previous method 300, the random number generated last time is called as a seed to generate the next random number, and each time a random number is generated, the random number is written into the flash memory of the micro control unit. In order to perform this storage, a first space can be selected in the flash memory for these random numbers. The first space includes a plurality of storage units, each storage unit corresponds to the minimum write unit of the flash memory, and each written random number occupies a storage unit. As is well known, the number of erase and write times of the flash memory of the micro control unit is limited, for example, z times, for example, z is ten thousand. In a specific application scenario, an electrical device including a micro control unit needs to generate a random number each time it is powered on, and it needs to be powered on q times during its entire life cycle, and the order of magnitude of q can be as high as 10 million. In this way, the first space can contain q / z storage units.

[0060] by Figure 6The first space 610 is shown as an example, which includes q / z storage units, and 0, 1, 2, ..., q / z-3, q / z-2, q / z-1 in the figure are the numbers of the corresponding storage units, and these storage units are arranged continuously according to the flash memory address. Writing the generated random number into the flash memory of the micro control unit in step S350 includes writing the generated random number into one of the multiple storage units in sequence according to the flash memory addresses of the multiple storage units in the first space selected in the flash memory. For example, when the first random number y0 is generated, y0 is written into the storage unit numbered 0; when the second random number y1 is generated, y1 is written into the storage unit numbered 1; when the third random number y2 is generated, y2 is written into the storage unit numbered 2, and so on. Before the random numbers are written into these storage units, they are in a state of never being used or being used and then erased, that is, the bit values ​​are all 1, which is represented by "FF..." in the figure.

[0061] As described above, in step S340, the latest stored random number (i.e., the previous random number) is read from the flash memory as a seed of the random number generation algorithm to generate the next random number. The reading includes: searching for the storage unit in which the random number is written most recently among the multiple storage units in the first space and reading the random number therein, and erasing the first space 610 to reuse the first space to store a new random number when the last storage unit in the flash memory address sequence of the first space is written with a random number.

[0062] by Figure 6 The first space 610 is shown as an example. Since the random numbers are written into the storage units numbered 0 to q / z-1 in sequence according to the flash memory address, a binary search algorithm or other algorithms with the same function can be used to search for the storage unit where the latest random number is written from these storage units. The process of searching for the latest stored random number is explained below using the binary search algorithm as an example.

[0063] Figure 7 is a diagram illustrating a schematic process of searching for the most recently stored random number using a binary algorithm according to an embodiment of the present disclosure.

[0064] refer to Figure 7 For ease of illustration, it is assumed that the first space has only 15 storage units.

[0065] First, according to the value of the storage unit numbered 14 being "FF...", it can be determined that the first space is not full. On the contrary, if the value of the storage unit numbered 14 is not "FF...", the first space is full, and the random number stored in the storage unit numbered 14 is the most recently stored random number (i.e., the previous random number).

[0066] In diagram 710, the search range is memory cells numbered 0-14, the midpoint of the range is memory cell numbered 7, and since memory cell numbered 7 is "FF...", it can be determined to continue searching within memory cells numbered 0-6.

[0067] In diagram 720, the search range is storage cells numbered 0-6, the midpoint of this range is storage cell numbered 3, and since storage cell numbered 3 is not "FF..." and storage cell numbered 4 is not "FF..." either, it can be determined to continue searching within storage cells numbered 4-6.

[0068] In diagram 730, the storage cells numbered 4 to 6 are used as the search range, and the midpoint of this range is the storage cell numbered 5. Since the storage cell numbered 5 is not "FF..." and the storage cell numbered 6 is "FF...", it can be determined that the storage cell numbered 5 is the storage cell in which the latest random number is written. Therefore, the random number y5 in the storage cell numbered 5 is read as the seed for generating the next random number. Since the principle of the binary algorithm is well known, it will not be repeated here.

[0069] After reading the latest stored random number, it is determined whether the first space is full. If it is full, the first space is erased to reuse the first space to store new random numbers. Since the random numbers are written into the storage units in the order of the flash memory addresses, when the last storage unit in the order of the flash memory addresses of the first space is written with a random number, it is determined that the first space is full, and then the first space is erased.

[0070] For example, as mentioned above, in Figure 7 In the example, if it is determined during the binary search that the value of the storage unit numbered 14 is not "FF...", it is determined that the first space has been filled, and the random number stored in the storage unit numbered 14 is the most recently stored random number (i.e., the previous random number).

[0071] In this way, according to the number of random numbers that need to be read and written, the first space that can meet the number is selected in the flash memory of the microprocessor unit to be dedicated to reading and writing the random numbers, which can efficiently utilize the flash memory space and avoid the flash memory being unable to support the reading and writing of random numbers for the entire life cycle of the electrical equipment due to excessive reading and writing of a single flash memory address.

[0072] Figure 8 is a schematic structural diagram of a first space and a second space according to another embodiment of the present disclosure.

[0073] In one example, the aforementioned first space dedicated to reading and writing random numbers includes multiple pages according to the number of random numbers that need to be read and written. In order to more easily read and write random numbers, a second space can also be selected in the flash memory to record the usage of multiple pages of the first space. The second space may include multiple storage units respectively used to record the usage of each of the multiple pages of the first space. In this case, reading the latest stored random number from the flash memory in step S340 also includes: when the flash memory address of the storage unit in the first space to which the random number is to be written is the starting address of one of the multiple pages, a predetermined value is written into the storage unit in the multiple storage units of the second space for recording the usage of the page.

[0074] by Figure 8 Taking the example shown as an example, the first space 810 includes pages 1 to 4, and each page contains m (for example, m can be 256 or other values) storage units. For example, page 1 includes storage units with flash memory addresses from add_0 to add_m; page 2 includes storage units with flash memory addresses from add_m+1 to add_2m+1, and so on. In order to record the usage of pages 1 to 4, page 0 is selected as the second space. The numbers "00" to "03" in the figure are storage units in the second space used to record the usage of each page in page 1 to page 4. For example, when the flash memory address of the storage unit to be written with the random number is the starting address "add_0" of page 1 (for example, the write address writeaddress=add_0), the predetermined value "AA55" is written into the storage unit numbered 00; when the flash memory address of the storage unit to be written with the random number is the starting address "add_m+1" of page 2 (for example, the write address writeaddress=add_m+1), the predetermined value "AA55" is written into the storage unit numbered 01; and so on. Before the predetermined value is written into the storage unit numbered 00 to 03, they are in a state of never being used or being used and then erased, that is, the bit values ​​are all 1, which is represented by "FF..." in the figure.

[0075] Due to the existence of the second space, reading the most recently stored random number from the flash memory in step S340 includes searching for the storage unit in which the random number is most recently written in the page of the first space corresponding to the storage unit in which the predetermined value is most recently written in the second space.

[0076] by Figure 8Taking the example shown as an example, according to the fact that the values ​​of the storage cells numbered 00 to 02 in the second space 820 are all "AA55" and the value of the storage cell numbered 03 is "FF...", it can be determined that the storage cell numbered 02 is the storage cell that has been most recently written with the predetermined value "AA55". The page of the first space corresponding to the storage cell numbered 02 is page 3, and then the binary algorithm or other algorithms with the same function can be used to search for the storage cell with the flash address add_2m+4 in page 3 where the most recently written random number is stored. In this way, the random number y in the storage cell is read. 2m+4 , and use the random number as a seed to generate the next random number. The example process of the binary algorithm has been described above, and it is similar here and will not be repeated.

[0077] Due to the existence of the second space, when it is determined that the first space is full and the first space is erased, it is also necessary to erase multiple storage units of the second space respectively used to record the usage of each of the multiple pages of the first space.

[0078] by Figure 8 Taking the example shown as an example, if the storage unit with the flash address add_4m+3 is also written with a random number, it can be determined that the first space 810 is full, and thus all the storage units in the first space 810 and the storage units numbered 00 to 03 in the second space 820 are erased.

[0079] As an alternative, you can first erase page 1 in the first space 810 and the storage unit numbered 00 in the second space 820, and then erase the corresponding pages and the corresponding storage units numbered 01 to 03 in the second space when the random number is to be written to the starting storage unit of each of the remaining pages 2 to 4.

[0080] The foregoing description is that the random numbers are written in the order of page 1 to page 4, but the present disclosure is not limited thereto, and the random numbers may also be written in a different page order.

[0081] Fig. 9 is a schematic block diagram of a micro control unit according to an embodiment of the present disclosure.

[0082] refer to Fig. 9 In one embodiment, the microcontroller unit 900 includes a collection module 910 and a random number generation module 920. The collection module 910 collects random information of the microcontroller unit 900 in response to the microcontroller unit being powered on, and the random information includes a reading of a GPIO port of the microcontroller unit in a floating state and / or a clock difference between the HSI and LSI of the microcontroller unit. The random number generation module 920 uses the random information as a seed of a random number generation algorithm to generate a random number.

[0083] In an alternative embodiment, the microcontroller unit 900 may further include a read module 930 and a write storage module 940. The read module 930 reads the random number generated by the microcontroller unit at the previous power-on in the flash memory of the microcontroller unit in response to the microcontroller unit being powered on. In this alternative embodiment, the acquisition module 910 acquires the random information of the microcontroller unit only when the read module 930 fails to read, rather than acquiring the random information as long as the microcontroller unit is powered on. In this alternative embodiment, the random number generation module 920 generates random numbers using the random information acquired by the acquisition module 910 as the seed of the random number generation algorithm when the read module 930 fails to read, and generates random numbers using the random number read as the seed of the random number generation algorithm when the read module 930 succeeds in reading. The write module is used to write the random number generated by the random number generation module 920 into the flash memory of the microcontroller unit.

[0084] The above is only combined with Fig. 9 A portion of a microcontroller unit 900 according to an embodiment of the present disclosure is described, and the acquisition module 910, the random number generation module 920, the reading module 930, and the writing module 940 can refer to and cite the aforementioned random number generation method 100 or 300 and its additional aspects and related drawings. For example, one or more steps in the aforementioned method 300 can be modified to implement the acquisition module 910, the random number generation module 920, the reading module 930, and the writing module 940 and their additional aspects. To avoid repetition, it will not be repeated here.

[0085] Fig.10 is a schematic block diagram of a device including a micro control unit according to an embodiment of the present disclosure.

[0086] The device 1000 may be various electrical devices that require a random number generation function, such as a frequency regulator that needs to generate random numbers to simulate random changes in loads in order to achieve frequency control and stability of the power system, a random power supply that needs to use random numbers to generate randomized power waveforms to reduce electromagnetic interference, noise, and harmonics, a random sampling device that needs to generate random time points to ensure the fairness and accuracy of sampling, a load simulator that needs to use random numbers to simulate the random behavior of real loads to evaluate the performance and stability of the power system, and a distributed power system device that needs to generate random numbers to generate random energy allocation plans to achieve power balance and optimization, etc.

[0087] Fig.11 is a schematic diagram of a computer-readable storage medium according to an embodiment of the present disclosure.

[0088] refer to Fig.11The computer readable storage medium 1100 stores computer program instructions 1110. When the computer program instructions 1110 are executed by the processor, the aforementioned random number generation method 100 or 300 is executed.

[0089] The computer-readable storage medium 1100 includes, but is not limited to, for example, volatile memory and / or non-volatile memory or and / or any recording medium with the same function that can be thought of by a person of ordinary skill in the technical field to which the present disclosure belongs. Volatile memory may include, for example, random access memory (RAM) and / or cache memory, etc. Non-volatile memory may include, for example, read-only memory (ROM), hard disk, flash memory, optical disk, magnetic disk, etc.

[0090] The computer readable program instructions 1110 may be assembler instructions, instruction set architecture (ISA) instructions, machine instructions, machine-dependent instructions, microcode, firmware instructions, state setting data, configuration data for an integrated circuit, or source code or object code written in any combination of one or more programming languages, including object oriented programming languages ​​such as Smalltalk, C++, or similar languages, and procedural programming languages ​​such as the "C" programming language or similar programming languages.

[0091] The basic principles of the present disclosure are described above in conjunction with specific embodiments. However, it should be noted that the advantages, strengths, effects, etc. mentioned in the present disclosure are only examples and not limitations, and it cannot be considered that these advantages, strengths, effects, etc. are necessary for each embodiment of the present disclosure. In addition, the specific details disclosed above are only for the purpose of illustration and ease of understanding, rather than limitation, and the above details do not limit the present disclosure to being implemented by adopting the above specific details. Those skilled in the art will recognize that some variations, modifications, changes, additions and sub-combinations thereof do not depart from the scope of the present disclosure.

Claims

1. A method for generating random numbers for a microcontroller unit, comprising: In response to the microcontroller unit being powered on, reading a random number generated by the microcontroller unit when it was powered on last time from a flash memory of the microcontroller unit; In the case of a reading failure, random information of the microcontroller unit is collected, and the random information is used as a seed of a random number generation algorithm to generate a random number, and in the case of a reading success, the random number read is used as a seed of a random number generation algorithm to generate a random number; as well as The generated random number is written into the flash memory of the micro control unit.

2. The method according to claim 1, wherein: Writing the generated random number into the flash memory of the micro control unit comprises: The generated random number is sequentially written into one of a plurality of storage cells of a first space selected in a flash memory at flash memory addresses of the plurality of storage cells, each of the plurality of storage cells corresponding to a minimum write unit of the flash memory.

3. The method according to claim 2, wherein: Reading a random number generated by the microcontroller unit when it was powered on last time in the flash memory of the microcontroller unit comprises: Searching for a storage unit in which a random number is written most recently among a plurality of storage units in the first space and reading the random number therein; and When a random number is written to the last storage unit of the first space in the flash address sequence, the first space is erased to reuse the first space to store a new random number.

4. The method according to claim 3, wherein: The first space includes a plurality of pages, and The step of writing the generated random number into the flash memory of the microcontroller unit also includes: when the flash memory address of the storage unit in the first space where the random number is to be written is the starting address of one of the multiple pages, writing a predetermined value into a storage unit in the multiple storage units of the second space selected in the flash memory for recording the usage of the page, and the multiple storage units in the second space are respectively used to record the usage of each of the multiple pages in the first space.

5. The method according to claim 4, wherein: Searching for a storage unit in which a random number is most recently written among a plurality of storage units in the first space comprises: The memory cell in which the random number is written most recently is searched in the page of the first space corresponding to the memory cell in which the predetermined value is written most recently in the second space.

6. The method according to claim 4, wherein: The plurality of storage units of the second space are erased while the first space is erased.

7. A method for generating random numbers for a microcontroller unit, comprising: In response to the micro control unit being powered on, collecting random information of the micro control unit; using the random information as a seed for a random number generation algorithm to generate a random number, The random information includes a reading of a general purpose input / output port of the micro control unit in a floating state and / or a clock difference between a high-speed internal clock and a low-speed internal clock of the micro control unit.

8. A micro control unit, comprising: A reading module, in response to the micro control unit being powered on, reads a random number generated by the micro control unit when it was powered on last time in a flash memory of the micro control unit; A collection module, for collecting random information of the micro control unit when the reading module fails to read; A random number generation module, which uses the random information as a seed of a random number generation algorithm to generate a random number when the reading module fails to read, and uses the read random number as a seed of a random number generation algorithm to generate a random number when the reading module succeeds in reading; as well as The writing module writes the generated random number into the flash memory of the micro control unit.

9. A micro control unit, comprising: an acquisition module, in response to the microcontroller unit being powered on, acquiring random information of the microcontroller unit, the random information including a reading of a general purpose input / output port of the microcontroller unit in a floating state and / or a clock difference between a high-speed internal clock and a low-speed internal clock of the microcontroller unit; as well as The random number generation module uses the random information as a seed of a random number generation algorithm to generate a random number.

10. An apparatus comprising: A microcontroller unit according to any one of claims 8 or 9.

11. A computer-readable storage medium having stored thereon instructions executable by a processor, wherein when the instructions are executed, the processor is caused to perform the method according to any one of claims 1 to 7.