A method and system for testing embedded python
By translating Python into C++ code and testing it on the STM32L431RC platform, the problem of excessive resource consumption of MicroPython on microcontrollers was solved, realizing efficient embedded Python applications that meet the requirements of high real-time performance and low resource consumption.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SUZHOU UNIV
- Filing Date
- 2023-02-02
- Publication Date
- 2026-06-26
AI Technical Summary
Existing MicroPython interpreters consume a lot of Flash space and computing resources on microcontrollers, making them unsuitable for developing high real-time embedded applications, and they lack effective performance verification methods.
A compiled embedded Python solution based on type annotation and static analysis is adopted to translate Python into C++ code and test it on the STM32L431RC hardware platform, including the calculation of timer count values and the output of difference.
This effectively improved the running efficiency of Python on microcontrollers, reduced Flash usage, enhanced real-time performance and portability, and validated the method of translating it into C++.
Smart Images

Figure CN116009834B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of embedded Python technology, and in particular to a testing method and system for embedded Python. Background Technology
[0002] In recent years, the artificial intelligence industry has developed rapidly, especially with machine learning algorithms being widely used in various fields. Python has become one of the most popular programming languages along with the trend of artificial intelligence. In the field of embedded programming, C language was previously used for programming; however, after Python became a popular language, the MicroPython interpreter also emerged.
[0003] MicroPython is an extremely small Python interpreter that can run on Windows, Unix systems, and some microcontrollers. It compresses the bulky Python interpreter to under 256KB and supports most Python 3 features. Python offers superior readability and reliability compared to C, and using MicroPython can significantly improve programming efficiency. However, the nature of its interpreter means it consumes more Flash memory and computing resources, making it unsuitable for developing embedded applications with low resource consumption and high real-time requirements. Therefore, most applications currently written in MicroPython have relatively low real-time requirements, such as educational robots. To truly utilize Python for embedded applications on microcontrollers, a method to improve Python's execution efficiency must be found.
[0004] Therefore, in its authorized patent CN112346722B, the applicant proposed a compiled embedded Python solution to address the above problems. This solution implements a Python-to-C++ translator based on type annotations and static analysis, and integrates the translator into the AHL-GEC-IDE embedded development integrated development environment jointly produced by Soochow University and ARM.
[0005] However, the patent application did not further verify and test the performance of the embedded Python. This application addresses these shortcomings. Summary of the Invention
[0006] In view of this, the purpose of this application is to propose a testing method and system for embedded Python, which can specifically solve existing problems.
[0007] To achieve the above objectives, this application proposes a testing method for embedded Python, comprising:
[0008] S1. Start the timer and retrieve the timer count value;
[0009] S2. Calculate the value of the nth term in the Fibonacci sequence;
[0010] S3. Retrieve the timer count value again;
[0011] S4. Calculate the difference between the two timer count values;
[0012] S5. Output the difference to the PC via serial port.
[0013] Furthermore, the testing method is based on the STM32L431RC hardware platform.
[0014] Furthermore, the STM32L431RC hardware platform includes 256KB of Flash and 64KB of RAM, an RTC, a 32-bit timer, a 16-bit PWM timer for motor control, four 16-bit timers, and two 16-bit timers.
[0015] Furthermore, prior to step S1, the embedded Python program porting step includes the following:
[0016] The source program is translated into header files, main function source files, and source files. These three files are then placed together with the STM32L431RC chip header files, kernel files, startup files, and linker files. The arm-none-eabi-g++ tool is used to compile and link the program to generate an embedded Python program that can be ported to the STM32L431RC.
[0017] Furthermore, the test method is performed at a 48MHz system clock.
[0018] Furthermore, n includes integers from 20 to 30.
[0019] Furthermore, following step S5, the MicroPython program porting steps are as follows:
[0020] (1) Install Cygwin in Windows, selecting make and gcc during installation; (2) Open the Cygwin terminal and navigate to the mpy-cross folder under the MicroPython project directory, using the make command to generate mpy-cross.exe; (3) Create a new Python source file named frozentest.py in the mpy-cross folder, and paste the experimental program into this Python source file, using mpy-cross. (3) Use the frozentest.py command to generate the frozentest.mpy file; (4) Enter the ports folder under the MicroPython root directory, copy the Minimal folder under the ports folder, and rename it to STM32L431RC; (5) Copy the chip file, kernel file and linker file of STM32L431RC to the STM32L431RC folder, modify the startup code in main.c, modify the serial port used in uart_core.c, modify the Makefile, and copy the frozentest.mpy file to the STM32L431RC folder; (6) Enter the STM32L431RC folder in the Cygwin terminal, use the make CROSS=1 command to compile and generate a MicroPython program that can be ported to STM32L431RC.
[0021] For the purposes described above, this application also proposes a testing system for embedded Python, comprising:
[0022] The first timing module is used to start the timer and retrieve the timer count value;
[0023] The sequence calculation module is used to calculate the value of the nth term in the Fibonacci sequence.
[0024] The second timing module is used to retrieve the timer count value again;
[0025] The time calculation module is used to calculate the difference between two timer count values;
[0026] The timing output module is used to output the difference to a PC via a serial port.
[0027] In summary, the advantages of this application and the user experience it brings are as follows:
[0028] The testing method described in this application effectively verifies that translating Python into C++ can significantly reduce the time consumed by Python type judgment and reduce the Flash space occupied by unused functions. It is an effective method to improve Python's real-time performance, reduce Flash usage, and enhance portability. Attached Figure Description
[0029] In the accompanying drawings, unless otherwise specified, the same reference numerals throughout the various drawings denote the same or similar parts or elements. These drawings are not necessarily drawn to scale. It should be understood that these drawings depict only some embodiments disclosed in this application and should not be construed as limiting the scope of this application.
[0030] Figure 1 A flowchart illustrating a testing method for embedded Python according to an embodiment of this application is shown.
[0031] Figure 2 This diagram illustrates the comparison results of calculating the 25th to 30th terms of the Fibonacci sequence in this application.
[0032] Figure 3 A schematic diagram of the configuration of a test system for embedded Python according to an embodiment of this application is shown.
[0033] Figure 4 A schematic diagram of the structure of an electronic device provided in one embodiment of this application is shown.
[0034] Figure 5 A schematic diagram of a storage medium provided in one embodiment of this application is shown. Detailed Implementation
[0035] The present application will now be described in further detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are for illustrative purposes only and are not intended to limit the invention. Furthermore, it should be noted that, for ease of description, only the parts relevant to the invention are shown in the accompanying drawings.
[0036] It should be noted that, unless otherwise specified, the embodiments and features described in this application can be combined with each other. This application will now be described in detail with reference to the accompanying drawings and embodiments.
[0037] Figure 1 A flowchart illustrating a testing method for embedded Python according to an embodiment of this application is provided. Figure 1 As shown, the testing method for this embedded Python includes:
[0038] S1. Start the timer and retrieve the timer count value;
[0039] S2. Calculate the value of the nth term in the Fibonacci sequence;
[0040] S3. Retrieve the timer count value again;
[0041] S4. Calculate the difference between the two timer count values;
[0042] S5. Output the difference to the PC via serial port.
[0043] This application uses the STM32L431RC as the hardware platform for experiments. By comparing the efficiency and program size of embedded Python and MicroPython in Fibonacci sequence calculations, it verifies that embedded Python can effectively improve the time and space efficiency of Python running on a microcontroller. The calculation of the nth term of the Fibonacci sequence is a typical divide-and-conquer algorithm, and its recursive implementation has a time complexity of O(2n). As the number of calculated terms increases, a large number of function calls are added. The real-time performance of CE-Python can be measured by calculating the time consumed by these function calls.
[0044] The following section describes the real-time performance of CE-Python under the recursive implementation of the Fibonacci sequence, based on the experimental procedure design and experimental results analysis.
[0045] 1. Hardware Platform Introduction
[0046] The STM32L431RC is an ultra-low-power microcontroller based on the high-performance Cortex-M4 core, with a 32-bit reduced instruction set and an operating frequency of up to 80MHz. It features 256KB of Flash and 64KB of RAM, and includes a low-power RTC, a general-purpose 32-bit timer, a dedicated 16-bit PWM timer for motor control, four general-purpose 16-bit timers, and two 16-bit low-power timers.
[0047] 2 Experimental Procedure and Transplantation
[0048] The experimental program uses a recursive algorithm for calculating Fibonacci numbers, implemented on an STM32L431RC chip. It employs a timer staking method to obtain the time consumed in calculating the nth term of the Fibonacci sequence, and outputs this time to a PC via serial port. The overall process is as follows: First, start the timer and retrieve its count value; then, calculate the value of the nth term of the Fibonacci sequence; next, retrieve the timer count value again; finally, calculate the difference between the two count values and output it to the PC via serial port. The program is shown in Table 1.
[0049] Table 1. Python source code for calculating Fibonacci numbers.
[0050]
[0051] 2.1 Porting MicroPython Programs
[0052] MicroPython uses a minimal interpreter, and the program porting process involves six steps: (1) Install Cygwin in a Windows environment, selecting make and gcc during installation; (2) Open the Cygwin terminal and navigate to the mpy-cross folder under the MicroPython project directory, using the make command to generate mpy-cross.exe; (3) Create a new Python source file named frozentest.py in the mpy-cross folder, paste the experimental program into this source file, and use the mpy-crossfrozentest.py command to generate frozentest.exe. (4) Enter the ports folder under the MicroPython root directory, copy the Minimal folder under the folder, and rename it to STM32L431RC; (5) Copy the official chip file, kernel file and link file of STM32L431RC to the STM32L431RC folder, modify the startup code in main.c, modify the serial port used in uart_core.c, modify Makefile, and copy the frozentest.mpy file to the folder; (6) Enter the STM32L431RC folder in the Cygwin terminal, use the make CROSS=1 command to compile and generate MicroPython programs that can be ported to STM32L431RC.
[0053] 2.2 Embedded Python Program Porting
[0054] Embedded Python directly translates the experimental source code into three files. These three files, along with the official STM32L431RC chip header file, kernel file, startup file, and linker file, are then compiled and linked using the arm-none-eabi-g++ tool to generate an embedded Python program portable to the STM32L431RC. The three translated files are shown in Tables 2 and 3.
[0055] Table 2. Header files and main function source files translated from embedded Python.
[0056]
[0057] Table 3 Source files translated from embedded Python
[0058]
[0059]
[0060] 3. Experimental Results and Analysis
[0061] The experiment was conducted under a 48MHz system clock. The time efficiency of the two methods was analyzed by comparing the time required for embedded Python and MicroPython to calculate the nth term of the Fibonacci sequence.
[0062] To improve the measurement accuracy of the timer-based staking method and ensure the program remains within the measurable range, the experiment selected the calculation time for the 20th to 30th terms of the Fibonacci sequence. When calculating less than the 20th term, the computer time was short, and the comparison effect was not obvious. However, when calculating more than the 30th term, MicroPython could not continue running. The experiment used milliseconds (ms) as the smallest unit of measurement, while the CPU clock's operating accuracy is at the microsecond level (μs). Therefore, the output error caused by accuracy was less than 1ms. The calculation time result for each term was taken as the mode of 20 tests. The experimental results are shown in Table 4, where the comparison bar chart of the calculation time for terms 25 to 30 is shown below. Figure 2 As shown.
[0063] Table 4. Real-time performance test results of the Fibonacci sequence.
[0064]
[0065]
[0066] Since the program for calculating the nth Fibonacci number only needs to change one parameter without adding or removing any parameters, the Flash usage of different Fibonacci number calculation programs remained basically unchanged in the experiment. The average size of the .bin file for embedded Python was 12KB, and the average size of the .bin file for MicroPython was 47KB.
[0067] The experimental results show that translating Python into C++ code can effectively improve the running efficiency of Python code on microcontrollers and reduce Flash usage.
[0068] The experimental test results of this application show that embedded Python can give full play to the advantages of statically typed languages, greatly improve the running efficiency of Python on microcontrollers, and meet the requirements of writing embedded applications with high real-time performance and low resource consumption on microcontrollers using Python.
[0069] The application embodiment provides an embedded Python testing system, which is used to execute the embedded Python testing method described in the above embodiment, such as... Figure 3 As shown, the system includes:
[0070] The first timing module 301 is used to start the timer and retrieve the timer count value;
[0071] Sequence calculation module 302 is used to calculate the value of the nth term of the Fibonacci sequence;
[0072] The second timing module 303 is used to retrieve the timer count value again;
[0073] The time calculation module 304 is used to calculate the difference between two timer count values;
[0074] The timing output module 305 is used to output the difference to a PC via a serial port.
[0075] The embedded Python testing system provided in the above embodiments of this application and the embedded Python testing method provided in the embodiments of this application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the applications they store.
[0076] This application also provides an electronic device corresponding to the embedded Python testing method provided in the foregoing embodiments, for executing the embedded Python testing method. This application does not limit the scope of the embodiments.
[0077] Please refer to Figure 4 This illustrates a schematic diagram of an electronic device provided by some embodiments of this application. For example... Figure 4 As shown, the electronic device 20 includes: a processor 200, a memory 201, a bus 202, and a communication interface 203. The processor 200, the communication interface 203, and the memory 201 are connected via the bus 202. The memory 201 stores a computer program that can run on the processor 200. When the processor 200 runs the computer program, it executes the embedded Python testing method provided in any of the foregoing embodiments of this application.
[0078] The memory 201 may include high-speed random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Communication between this system network element and at least one other network element is achieved through at least one communication interface 203 (which can be wired or wireless), such as the Internet, wide area network, local area network, or metropolitan area network.
[0079] Bus 202 can be an ISA bus, PCI bus, or EISA bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. The memory 201 is used to store programs. After receiving an execution instruction, the processor 200 executes the program. The embedded Python testing method disclosed in any of the foregoing embodiments of this application can be applied to the processor 200, or implemented by the processor 200.
[0080] The processor 200 may be an integrated circuit chip with signal processing capabilities. In implementation, each step of the above method can be completed by the integrated logic circuitry in the hardware of the processor 200 or by instructions in software form. The processor 200 may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), an off-the-shelf programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of this application. The general-purpose processor may be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of this application can be directly embodied in the execution of a hardware decoding processor, or executed by a combination of hardware and software modules in the decoding processor. The software modules may reside in random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or other mature storage media in the art. The storage medium is located in memory 201. The processor 200 reads the information in memory 201 and, in conjunction with its hardware, completes the steps of the above method.
[0081] The electronic device provided in this application embodiment and the embedded Python testing method provided in this application embodiment are based on the same inventive concept and have the same beneficial effects as the methods they adopt, run or implement.
[0082] This application also provides a computer-readable storage medium corresponding to the embedded Python testing method provided in the foregoing embodiments. Please refer to... Figure 5 The computer-readable storage medium shown is an optical disc 30, on which a computer program (i.e., a program product) is stored. When the computer program is run by a processor, it executes the embedded Python test method provided in any of the foregoing embodiments.
[0083] It should be noted that examples of the computer-readable storage medium may also include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other optical and magnetic storage media, which will not be elaborated here.
[0084] The computer-readable storage medium provided in the above embodiments of this application and the embedded Python testing method provided in the embodiments of this application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the applications stored therein.
[0085] It should be noted that:
[0086] The algorithms and displays provided herein are not inherently related to any particular computer, virtual system, or other device. Various general-purpose systems can also be used in conjunction with the teachings herein. The required structure for constructing such systems is apparent from the above description. Furthermore, this application is not directed to any particular programming language. It should be understood that the content of this application described herein can be implemented using various programming languages, and the above description of specific languages is for the purpose of disclosing the best mode of implementation of this application.
[0087] Numerous specific details are set forth in the specification provided herein. However, it will be understood that embodiments of this application may be practiced without these specific details. In some instances, well-known methods, structures, and techniques have not been shown in detail so as not to obscure the understanding of this specification.
[0088] Similarly, it should be understood that, in order to simplify this application and aid in understanding one or more of the various inventive aspects, in the above description of exemplary embodiments of this application, various features of this application are sometimes grouped together into a single embodiment, figure, or description thereof. However, this method of disclosure should not be construed as reflecting an intention that the claimed application requires more features than are expressly recited in each claim. Rather, as reflected in the following claims, inventive aspects lie in fewer than all features of a single foregoing disclosed embodiment. Therefore, the claims following the detailed description are hereby expressly incorporated into that detailed description, wherein each claim itself is a separate embodiment of this application.
[0089] Those skilled in the art will understand that modules in the device of the embodiments can be adaptively changed and placed in one or more devices different from that embodiment. Modules, units, or components in the embodiments can be combined into a single module, unit, or component, and further, they can be divided into multiple sub-modules, sub-units, or sub-components. Except where at least some of such features and / or processes or units are mutually exclusive, any combination can be used to combine all features disclosed in this specification (including the accompanying claims, abstract, and drawings) and all processes or units of any method or device so disclosed. Unless expressly stated otherwise, each feature disclosed in this specification (including the accompanying claims, abstract, and drawings) may be replaced by an alternative feature that serves the same, equivalent, or similar purpose.
[0090] Furthermore, those skilled in the art will understand that although some embodiments described herein include certain features but not others included in other embodiments, combinations of features from different embodiments are intended to be within the scope of this application and form different embodiments. For example, in the following claims, any of the claimed embodiments can be used in any combination.
[0091] The various component embodiments of this application can be implemented in hardware, or as software modules running on one or more processors, or a combination thereof. Those skilled in the art will understand that microprocessors or digital signal processors (DSPs) can be used in practice to implement some or all of the functions of some or all of the components in the virtual machine creation system according to the embodiments of this application. This application can also be implemented as a device or system program (e.g., a computer program and computer program product) for performing part or all of the methods described herein. Such an implementation of this application can be stored on a computer-readable medium, or can be in the form of one or more signals. Such signals can be downloaded from an Internet website, provided on a carrier signal, or provided in any other form.
[0092] It should be noted that the above embodiments are illustrative of this application and not restrictive, and that those skilled in the art can devise alternative embodiments without departing from the scope of the appended claims. In the claims, any reference signs placed between parentheses should not be construed as limiting the claims. The word "comprising" does not exclude the presence of elements or steps not listed in the claims. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. This application can be implemented by means of hardware comprising several different elements and by means of a suitably programmed computer. In the unit claims enumerating several systems, several of these systems may be embodied by the same item of hardware. The use of the words first, second, and third, etc., does not indicate any order. These words can be interpreted as names.
[0093] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any person skilled in the art can easily conceive of various variations or substitutions within the technical scope disclosed in this application, and these should all be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A testing method for embedded Python, characterized in that, Includes the following steps: S1. Start the timer and retrieve the timer count value; S2. Calculate the value of the nth term in the Fibonacci sequence; S3. Retrieve the timer count value again; S4. Calculate the difference between the two timer count values; S5. Output the difference to the PC via serial port; Prior to step S1, the embedded Python program porting steps are as follows: The source program is translated into header files, main function source files, and source files. These three files are then placed together with the STM32L431RC chip header files, kernel files, startup files, and linker files. The arm-none-eabi-g++ tool is used to compile and link the program to generate an embedded Python program that can be ported to the STM32L431RC. Following step S5, the MicroPython program porting steps are as follows: (1) Install Cygwin in Windows environment, and check make and gcc during the installation process; (2) Open Cygwin terminal and go to the mpy-cross folder under the MicroPython project directory, and use the make command to generate mpy-cross.exe; (3) Create a new Python source file named frozentest.py in the mpy-cross folder, and paste the experimental program into the Python source file, and use the mpy-cross frozentest.py command to generate frozentest.mpy file; (4) Go to the ports folder under the MicroPython root directory, copy the Minimal folder under the ports folder, and rename it to STM32L431RC; (5) Copy the chip file, kernel file and linker file of STM32L431RC to the STM32L431RC folder, modify the startup code in main.c, modify the serial port used in uart_core.c, modify Makefile, and copy the frozentest.mpy file to the STM32L431RC folder; (6) Enter the STM32L431RC folder in Cygwin terminal, use the make CROSS=1 command to compile and generate MicroPython program that can be ported to STM32L431RC.
2. The method according to claim 1, characterized in that, The test method is based on the STM32L431RC hardware platform.
3. The method according to claim 2, characterized in that, The STM32L431RC hardware platform includes 256KB of Flash and 64KB of RAM, an RTC, a 32-bit timer, a 16-bit PWM timer for motor control, four 16-bit timers, and two 16-bit timers.
4. The method according to claim 1, characterized in that, The test method was performed at a system clock of 48MHz.
5. The method according to claim 1, characterized in that, The number n includes integers from 20 to 30.
6. An embedded Python testing system, using the method described in any one of claims 1-5, characterized in that, include: The first timing module is used to start the timer and retrieve the timer count value; The sequence calculation module is used to calculate the value of the nth term in the Fibonacci sequence. The second timing module is used to retrieve the timer count value again; The time calculation module is used to calculate the difference between two timer count values; The timing output module is used to output the difference to a PC via a serial port.
7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, The processor executes the computer program to implement the method as described in any one of claims 1-5.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that, The program is executed by a processor to implement the method as described in any one of claims 1-5.