Mcu code generation method and device, equipment and storage medium
Patent Information
- Application Number
- CN202610881173.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-17
- Publication Date
- 2026-08-07
AI Technical Summary
并且,代码生成器依赖于提前编写好的标准封装库,这些标准封装库的开发需要芯片原厂投入大量的时间、人力和物力成本
[0020]本公开实施例提供的技术方案与现有技术相比具有如下优点:
Smart Images

Figure CN122526546A_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of microcontroller technology, and in particular to MCU code generation methods, apparatus, devices and storage media. Background Technology
[0002] With the widespread application of microcontroller units (MCUs) in various fields, such as smart homes, industrial control, and the Internet of Things (IoT), the demand for MCU code development is increasing. Traditionally, MCU code development is done through code generators. This approach requires developers to have a deep understanding of the MCU chip's hardware architecture, register configuration, and the functions and usage of various peripherals before they can use the code generator to generate the MCU chip's code. Furthermore, code generators rely on pre-written standard package libraries, the development of which requires chip manufacturers to invest significant time, manpower, and resources. This approach makes MCU code development difficult, inefficient, and costly. Summary of the Invention
[0003] To address the aforementioned technical problems, this disclosure provides an MCU code generation method, apparatus, device, and storage medium.
[0004] This disclosure provides a method for generating MCU code. The method includes: receiving development requirements expressed in natural language by a user on a first displayed interface; performing semantic analysis on the development requirements using an AI agent to determine first resources and functional modules of the MCU chip corresponding to the development requirements; generating configuration parameters based on the first resources and the functional modules; sending the configuration parameters to a code generator using the AI agent, wherein the AI agent and the code generator communicate via an API interface; and generating driver code for the MCU chip corresponding to the development requirements using the code generator based on the configuration parameters.
[0005] In some embodiments, the method further includes: receiving a program flowchart input by a user on a displayed second interface, the program flowchart being a flowchart representing the application's running logic, determined according to the application function to be implemented; using the AI agent to identify the program flowchart and obtain an identification result, the identification result including at least one node and a flow path, determining the application execution action corresponding to each of the at least one node and the execution logic relationship between the nodes based on the identification result; using the AI agent to determine the corresponding second resource of the MCU chip and the configuration parameters of the second resource based on the application execution action corresponding to each node and the execution logic relationship between the nodes, generating application layer code based on the second resource and the configuration parameters of the second resource; and integrating the application layer code and the driver code of the MCU chip so that the application layer code can call the driver code to implement the application function to be implemented.
[0006] In some embodiments, the method further includes: displaying a third interface, the third interface including the resource usage and resource configuration parameters of the MCU chip.
[0007] In some embodiments, the method further includes: receiving a configuration operation performed by a user on a displayed fourth interface, the configuration operation including a configuration operation for a third resource of the MCU chip and configuration parameters of the third resource; generating driver code for the MCU chip based on the configuration operation using the code generator; and displaying the resource usage and resource configuration parameters of the MCU chip corresponding to the configuration operation.
[0008] In some embodiments, the method further includes: performing at least one of the following checks on the generated code according to preset code specifications and syntax rules: variable definition, function call, syntax, and code style; and displaying a reminder message if an error is detected in the generated code, the reminder message including the error location and error type.
[0009] In some embodiments, the method further includes: for each sub-code set included in the generated code, generating unit test code corresponding to the sub-code set based on the function implemented by the sub-code set and the relationship between the input parameters and output parameters of the sub-code set; the unit test code is used to perform boundary condition and exception testing on the input parameters to verify the correctness of the output parameters of the sub-code set; the generated code is the driver code of the MCU chip or the application layer code; the integrated application layer code and the driver code of the MCU chip are integrated with the unit test code to generate application engineering code.
[0010] In some embodiments, the method further includes: acquiring training data of the MCU chip, the training data including the hardware architecture, instruction set, register configuration, technical documents, and application cases of the MCU chip; and performing model training based on the training data to obtain the AI agent.
[0011] This disclosure provides an MCU code generation device, the device comprising: a visual interaction module for receiving development requirements expressed in natural language by a user on a first displayed interface; an AI agent and code generator integration module for performing semantic analysis on the development requirements using the AI agent, determining first resources and functional modules of the MCU chip corresponding to the development requirements, and generating configuration parameters based on the first resources and the functional modules; the AI agent and code generator integration module is further configured to send the configuration parameters to a code generator using the AI agent, wherein the AI agent and the code generator communicate via an API interface; the AI agent and code generator integration module is further configured to generate driver code for the MCU chip corresponding to the development requirements using the code generator based on the configuration parameters.
[0012] In some embodiments, the device further includes a code integration module; a visualization interaction module, further configured to receive a program flowchart input by a user on a displayed second interface, the program flowchart being a flowchart representing the application's running logic, determined according to the application's functions to be implemented; an AI agent and code generator integration module, further configured to use the AI agent to identify the program flowchart and obtain an identification result, the identification result including at least one node and a flow path, and to determine the application execution action corresponding to each of the at least one node and the execution logic relationship between the nodes based on the identification result; the AI agent and code generator integration module, further configured to use the AI agent to determine the corresponding second resource of the MCU chip and the configuration parameters of the second resource based on the application execution action corresponding to each node and the execution logic relationship between the nodes, and to generate application layer code based on the second resource and the configuration parameters of the second resource; and a code integration module, configured to integrate the application layer code and the MCU chip's driver code, so that the application layer code can call the driver code to implement the application's functions to be implemented.
[0013] In some embodiments, the visualization interaction module is further configured to display a third interface, which includes the resource usage and resource configuration parameters of the MCU chip.
[0014] In some embodiments, the visualization interaction module is further configured to receive configuration operations performed by the user on the displayed fourth interface, the configuration operations including configuration operations on the third resources of the MCU chip and the configuration parameters of the third resources; the AI agent and code generator integration module is further configured to use the code generator to generate driver code for the MCU chip based on the configuration operations; the visualization interaction module is further configured to display the resource usage and resource configuration parameters of the MCU chip corresponding to the configuration operations.
[0015] In some embodiments, the apparatus further includes: an automatic code inspection module; the automatic code inspection module is configured to perform at least one of the following checks on the generated code according to preset code specifications and syntax rules: variable definition, function call, syntax, and code style; and a visual interaction module is configured to display a reminder message when an error is detected in the generated code, the reminder message including the error location and error type.
[0016] In some embodiments, the apparatus further includes: a test generation module; the test generation module is configured to generate unit test code corresponding to each sub-code set included in the generated code, based on the function implemented by the sub-code set and the relationship between the input parameters and output parameters of the sub-code set; the unit test code is used to perform boundary condition and exception testing on the input parameters to verify the correctness of the output parameters of the sub-code set; the generated code is the driver code of the MCU chip or the application layer code; the test generation module is further configured to integrate the integrated application layer code and the driver code of the MCU chip with the unit test code to generate application engineering code.
[0017] In some embodiments, the apparatus further includes: a model training module; the model training module is configured to acquire training data of the MCU chip, the training data including the hardware architecture, instruction set, register configuration, technical documents, and application cases of the MCU chip; the model training module is also configured to perform model training based on the training data to obtain the AI agent.
[0018] This disclosure also provides an electronic device, including: a memory; a processor; and a computer program; wherein the computer program is stored in the memory and configured to be executed by the processor to implement the MCU code generation method provided in this disclosure.
[0019] This disclosure also provides a computer-readable storage medium storing a computer program / instructions thereon, which, when executed by a processor, implements the MCU code generation method provided in this disclosure.
[0020] The technical solution provided in this disclosure has the following advantages compared with the prior art: This embodiment of the disclosure receives development requirements expressed in natural language by a user on a first displayed interface. An AI agent performs semantic analysis on the development requirements to determine the first resources and functional modules of the MCU chip corresponding to the requirements, and generates configuration parameters based on these resources and modules. The AI agent then sends these configuration parameters to a code generator, with communication between the AI agent and the code generator occurring via an API interface. The code generator then generates driver code for the MCU chip corresponding to the development requirements based on the configuration parameters. In this way, even non-professional MCU developers can understand their development requirements by describing them in simple natural language, allowing the AI agent to generate the necessary configuration parameters for the code generator. The code generator can then generate driver code for the MCU chip based on these parameters. This eliminates the need for manual, complex configurations (such as configuring registers and writing code) and avoids reliance on pre-written standard package libraries. It reduces the difficulty of MCU code development, improves efficiency, and lowers costs. Attached Figure Description
[0021] The accompanying drawings, which are incorporated in and form a part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure.
[0022] To more clearly illustrate the technical solutions in the embodiments of this disclosure or the prior art, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0023] Figure 1 A flowchart illustrating an MCU code generation method provided in this embodiment of the disclosure; Figure 2 A flowchart illustrating another MCU code generation method provided in this embodiment of the disclosure; Figure 3 A schematic diagram of a program flowchart provided for an embodiment of this disclosure; Figure 4 This is a schematic diagram of the structure of the MCU code generation apparatus provided in the embodiments of this disclosure; Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this disclosure. Detailed Implementation
[0024] To better understand the above-mentioned objectives, features, and advantages of this disclosure, the solutions disclosed herein will be further described below. It should be noted that, unless otherwise specified, the embodiments and features described herein can be combined with each other.
[0025] Numerous specific details are set forth in the following description in order to provide a full understanding of this disclosure, but this disclosure may also be implemented in other ways different from those described herein; obviously, the embodiments in the specification are only some, and not all, of the embodiments of this disclosure.
[0026] Traditionally, MCU code development is done using code generators. While this simplifies the development process to some extent, it still requires developers to have a deep understanding of the MCU chip. Code generators generate MCU code based on various parameters and values manually configured by the developer. This demands that developers spend a significant amount of time learning the MCU chip's hardware architecture, register configurations, and the functions and usage of various peripherals. For novice developers, the learning curve is steep, increasing development time and difficulty. For example, when configuring a complex communication interface, such as the Serial Peripheral Interface (SPI) or Inter-Integrated Circuit (I2C), it's not only necessary to understand the principles of the communication protocol but also the specific register settings of the corresponding peripheral in the MCU. Even a slight mistake can lead to configuration errors, affecting the entire system's communication functionality.
[0027] Furthermore, code generators rely on pre-written standard package libraries (such as HAL, LL, etc.). Developing these standard package libraries requires chip manufacturers to invest significant time, manpower, and resources. From analyzing the underlying architecture of the MCU chip to abstracting various hardware functions into easily callable function interfaces, and then to comprehensively testing and optimizing these function interfaces, the entire process is complex and time-consuming. Once the chip's hardware architecture changes or new functional requirements arise, substantial resources must be reinvested in updating and maintaining the standard package libraries. For example, when a chip manufacturer releases a new MCU model with improved hardware resources and performance, a new set of adapted standard package libraries needs to be developed to ensure the code generator can support the new chip. This not only increases development costs but may also result in the code generator being unable to provide timely support after the new chip is released, impacting product promotion and application.
[0028] In summary, current MCU code development solutions suffer from high development difficulty, low efficiency, and high cost.
[0029] To address the aforementioned technical problems, this disclosure provides a method for generating MCU code. The method is described below with reference to specific embodiments.
[0030] Figure 1 This is a flowchart illustrating an MCU code generation method provided in an embodiment of the present disclosure. This method can be executed by an MCU code generation device, which can be implemented in software and / or hardware, and is generally integrated into an electronic device. Figure 1 As shown, the method includes the following steps: S101. Receive the development requirements expressed in natural language by the user on the first displayed interface.
[0031] In some embodiments, a text input box or dialog box may be displayed on the first interface, where users can input development requirements expressed in natural language.
[0032] In this context, development requirements expressed in natural language can refer to MCU driver development task description text written in human natural language. These requirements may implicitly include the resources, functional modules, and configuration parameters of the MCU chip to be used. Development requirements expressed in natural language can be unstructured data that code generators cannot directly recognize.
[0033] MCU chip resources include, but are not limited to, hardware peripherals (such as I / O ports, UART / USART serial ports, CAN, USB, etc.), storage resources (such as RAM memory, Flash memory), pins, timers, serial ports, etc. Functional modules refer to the functions that the resources on the MCU chip can perform, such as, but not limited to, GPIO inputs, GPIO push-pull outputs, GPIO open-drain outputs, pull-up / pull-down pins, serial port transceiver, timer timing, etc. Configuration parameters refer to the specific parameter values that enable the resources on the MCU chip to operate as required, such as: GPIO port: A; pin number: 0; speed: 50MHz, etc.
[0034] For example, a development requirement expressed in natural language could be "to implement an LED light that blinks once per second". S102. Use an AI agent to perform semantic analysis on the development requirements, determine the first resources and functional modules of the MCU chip corresponding to the development requirements, and generate configuration parameters based on the first resources and functional modules.
[0035] In some embodiments, the first resource may be one or more resources included on the MCU chip. The functional module may be a functional module corresponding to the first resource. That is, one or more functions that the one or more hardware resources included in the first resource can implement.
[0036] In some embodiments, the AI agent may also generate configuration parameters in conjunction with development requirements when generating configuration parameters.
[0037] In some embodiments, the AI agent includes a pre-trained large model. The AI agent can use the pre-trained large model to determine the first resources and functional modules of the MCU chip corresponding to the development requirements, and generate configuration parameters based on the first resources and the functional modules.
[0038] In some embodiments, training data of the MCU chip can be acquired, including but not limited to the MCU chip's hardware architecture, instruction set, register configuration, technical documents, and application cases; model training is performed based on the training data to obtain an AI agent. As one possible implementation, this process can be performed before step S102.
[0039] In some embodiments, model training can be performed using deep learning techniques.
[0040] This disclosure describes a large-scale model trained specifically for a particular MCU chip. It involves collecting and organizing a large amount of data about the MCU chip, including its hardware architecture, instruction set, and register configuration, and then using deep learning technology to train the large model. This large model can deeply understand the characteristics and resource situation of the MCU chip. For example, for a newly released MCU chip, the large model can quickly grasp its unique functions and resource allocation methods by studying its detailed technical documentation and practical application cases, thereby providing intelligent decision support for subsequent code generation.
[0041] In some embodiments, the AI agent is based on natural language processing technology and machine learning algorithms. When a user inputs development requirements expressed in natural language, the AI agent performs semantic analysis on the development requirements, first converting the development requirements into a logical representation that a computer can understand.
[0042] For example, a development requirement expressed in natural language could be "to implement an LED light that blinks once per second". The computer-understandable logical representation converted by the AI agent can be seen in Table 1 below.
[0043] Table 1
[0044] Furthermore, the AI agent determines the first resources and functional modules of the corresponding MCU chip in the development requirements based on the logical representation of the pre-trained large model. For example, referring to the example shown in Table 1, the first resources include: GPIO port A and pin 0. The functional modules include GPIO output functions.
[0045] Furthermore, the AI agent generates configuration parameters based on the pre-trained large model, taking the first resource and functional modules as examples. For instance, the first resource includes GPIO port A and pin 0. The functional modules include GPIO output functionality. The configuration parameters can be found in Table 2 below.
[0046] Table 2
[0047] S103. Use the AI agent to send configuration parameters to the code generator, wherein the AI agent and the code generator communicate through an API interface.
[0048] In some embodiments, the AI agent and the code generator can communicate bidirectionally through a unified interface, with step S103 using an API interface as an example. The AI agent can send configuration parameters, instructions, and other information to the code generator. The AI agent can also receive feedback from the code generator regarding the current status and error messages.
[0049] In some embodiments, the API interface can be implemented through a set of API functions. The AI agent can call specific functions of the code generator (e.g., start code generation, obtain configuration parameters, etc.) through AI functions, thereby realizing communication with the code generator.
[0050] S104. Use the code generator to generate driver code for the MCU chip that corresponds to the development requirements based on the configuration parameters.
[0051] For example, in this embodiment of the disclosure, the code generator can be either STM32CubeMX or MCU Studio.
[0052] In this embodiment of the disclosure, when the code generator generates driver code for the MCU chip based on configuration parameters, it does not need to call the standard package library. The code generator can generate driver code by formatting and sorting the configuration parameters.
[0053] For example, taking the configuration parameters as shown in Table 2 as an example, the driver code generated by the code generator can be seen in Table 3 below. Table 3
[0054] based on Figure 1The solution shown, by integrating AI technology, allows even non-professional MCU developers to understand their development needs by describing them in simple natural language. The AI agent then generates the configuration parameters required by the code generator, which in turn generates the driver code for the MCU chip based on these parameters. For example, an engineer familiar only with business logic but lacking understanding of the underlying principles of the MCU might want to implement a function that collects temperature and humidity sensor data and transmits it via Bluetooth. Figure 1 The solution shown only requires you to describe to the system "collect temperature and humidity data and send it to your mobile phone via Bluetooth", and the system can automatically generate all the necessary driver code, including sensor driver, Bluetooth communication and data processing code, without the need for manual complex configuration (such as configuring registers, writing code, etc.).
[0055] Furthermore, the code generator can generate driver code based on configuration parameters, eliminating the need for chip manufacturers to invest significant resources in developing standard driver libraries and reducing reliance on pre-written standard driver libraries. This not only saves chip manufacturers costs associated with developing and maintaining standard driver libraries but also enables new chips to be launched to market faster, reducing time-to-market costs and enhancing the market competitiveness of chip manufacturers.
[0056] Therefore, it can reduce the development difficulty of MCU code, improve efficiency, and reduce costs.
[0057] In some embodiments, such as Figure 2 As shown, Figure 1 The method shown also includes steps S201 to S204. This disclosure does not limit the order of steps S201 to S203 and steps S101 to S104.
[0058] S201. Receive the program flowchart input by the user on the second displayed interface. The program flowchart is a flowchart used to characterize the running logic of the application, which is determined according to the functions of the application to be implemented.
[0059] The second interface can be the same as or different from the first interface.
[0060] In some embodiments, a dialog box may be displayed on the second interface, in which the user can import a program flowchart.
[0061] In some embodiments, a program flowchart can refer to a flowchart that uses graphic symbols (such as start and end boxes, decision boxes, referrer boxes, processing boxes, etc.) to depict the software's operational logic. The program flowchart can differ depending on the specific application functionality to be implemented.
[0062] See Figure 3This is a schematic diagram of a program flowchart provided in an embodiment of this disclosure. The application function to be implemented by this program flowchart is to turn an LED light on and off.
[0063] S202. Use an AI agent to identify the program flowchart and obtain the identification result. The identification result includes at least one node and the flow direction. Based on the identification result, determine the application execution action corresponding to each node in at least one node, as well as the execution logic relationship between nodes.
[0064] In some embodiments, the AI agent can perform digital analysis on the program flowchart to obtain recognition results.
[0065] In some embodiments, a node can refer to an independent graphical unit in a program flowchart that represents a step in program execution. Nodes may include, but are not limited to, start nodes, end nodes, execution nodes, conditional decision nodes, loop control nodes, etc. Flow direction can refer to the lines connecting the nodes in the program flowchart and their pointing relationships, used to indicate the order in which nodes are executed.
[0066] For example: combining Figure 3 The example flowchart shown illustrates how an AI agent identifies program flowcharts, including nodes such as program startup, hardware initialization, button reading, button status judgment (e.g., whether the button is released), LED lighting, delay (e.g., a 1-second delay), LED turning off, and loop control. The program startup node is the initial node, while the hardware initialization, button reading, LED lighting, delay, and LED turning off nodes are execution nodes. The button status judgment node is a condition judgment node. The flow is as follows: Program Startup → Hardware Initialization → Button Reading → Condition Judgment; If condition is true, execute the blinking loop; If condition is false, pause blinking and remain in a waiting state; After blinking completes, automatically loop back to button reading.
[0067] The execution action corresponding to a node refers to the specific business operation that the application needs to perform. The execution logic relationships between nodes can include sequential execution, conditional branching, and loop-jumping execution. Sequential execution means that after the previous node completes, the next node is executed immediately. Conditional branching means that different subsequent nodes are selected for execution based on the truth value of a condition. Loop-jumping execution means that after the program finishes execution, it jumps to the previous node to achieve repeated loop execution.
[0068] For example, for a flowchart containing conditional statements and loop structures, accurately parse out information such as the conditions for the statements, the number of iterations, or the termination conditions.
[0069] For example: still combining Figure 3The example program flowchart shown illustrates the following actions performed by the application program at the program startup node: starting the MCU main program, initializing the system clock, and preparing the peripheral operating environment. The actions performed by the application program at the hardware initialization node include: reading the GPIO pin level corresponding to the button, obtaining the current button press state, etc., which will not be detailed further.
[0070] S203. Using the AI agent, determine the second resource of the corresponding MCU chip and the configuration parameters of the second resource based on the action executed by the application corresponding to each node and the execution logic relationship between the nodes, and generate application layer code based on the second resource and the configuration parameters of the second resource.
[0071] In some embodiments, the second resource may be one or more resources included on the MCU chip. The second resource and the first resource may be the same or different. For a description of the configuration parameters of the second resource, please refer to the description of the configuration parameters generated based on the first resource and the functional module.
[0072] In some embodiments, the AI agent can use the pre-trained large model to determine the required chip resources (i.e., the second resource) and the configuration parameters of the second resource. For example, if there is a timed task in the program flowchart, it is determined that a timer resource is needed, and the configuration parameters of the timer resource (such as the division factor, counting period, etc.) are calculated according to the time requirements of the timed task. Then, application layer code is generated based on the configuration parameters of the second and second resources.
[0073] In some embodiments, the application layer code generation process follows certain coding standards and programming styles to ensure that the generated application layer code has good readability and maintainability.
[0074] For example: combining Figure 3 The example shown can be represented by the application layer code in Table 4.
[0075] Table 4
[0076] S204. Integrate the application layer code and the MCU chip driver code so that the application layer code can call the driver code to implement the application function to be implemented.
[0077] In some embodiments, the generated application-layer code is integrated with the driver code of the MCU chip. Through appropriate function calls and variable passing, the application-layer code can correctly call the driver code to achieve the required functions. During the integration process, necessary optimizations are performed on the code, such as removing duplicate code and adjusting the code structure, to improve code execution efficiency.
[0078] For example, combining the application layer code example shown in step S203, the integrated code can be as follows: / / Code begins / / Driver code / / LED driver initialization: Configure GPIOA0 as push-pull output void LED_Init(void) { / / Enable GPIOA clock __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitTypeDef gpio_conf; gpio_conf.Pin = GPIO_PIN_0; gpio_conf.Mode= GPIO_MODE_OUTPUT_PP; gpio_conf.Pull = GPIO_NOPULL; gpio_conf.Speed= GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA,&gpio_conf); } / / LED lit void LED_On(void) { HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0, GPIO_PIN_SET); } / / LED off void LED_Off(void) { HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0, GPIO_PIN_RESET); } / / Key driver initialization: Configure GPIOA1 as pull-up input void KEY_Init(void) { __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitTypeDef gpio_conf; gpio_conf.Pin = GPIO_PIN_1; gpio_conf.Mode = GPIO_MODE_INPUT; gpio_conf.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOA,&gpio_conf); } / / Read button status uint8_t KEY_Read(void) { return HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1); } / / Simple delay function void Delay_ms(uint32_t ms) { HAL_Delay(ms); } / / Application layer code void App_LedBlinkProcess(void) { uint8_t key = KEY_Read(); / / Read key presses / / Check if the button is released if (key == 1) { / / Not pressed → Perform blinking LED_On(); Delay_ms(1000); LED_Off(); Delay_ms(1000); } else { / / Press → Turn off LED LED_Off(); } } / / Main program (integration of driver code and application layer code) int main(void) { / / System initialization HAL_Init(); SystemClock_Config(); / / Call driver initialization LED_Init(); KEY_Init(); / / Loop through application layer code while (1) { App_LedBlinkProcess(); } } / / End of code This disclosure embodiment utilizes a program flowchart in the software solution, enabling the AI agent to autonomously analyze the required chip resources and software solution, thereby generating application-layer code and integrating it with the MCU module driver. In traditional development, writing application-layer code requires developers to comprehensively consider multiple factors such as chip resources, business logic, and underlying drivers, making development quite difficult. The AI agent of this invention can automatically analyze the required chip resources, such as timers and interrupts, based on the program flowchart and generate corresponding application-layer code. For example, for a motor control program with complex logic, the AI agent can accurately determine the required PWM module resources based on the flowchart, generate corresponding PWM control code, and effectively integrate it with the motor drive code to output complete application engineering code.
[0079] In some embodiments, such as Figure 1 The method also includes displaying a third interface. This third interface includes the MCU chip's resource usage and resource configuration parameters.
[0080] The third interface can be on the same interface as the first and second interfaces mentioned above, or it can be on different display interfaces.
[0081] In some embodiments, the third interface may display different elements, such as graphics and icons, which can be used to display the usage status and configuration parameters of different resources.
[0082] For example, taking the memory of an MCU chip as an example, the third interface can display the memory usage (such as total memory, memory usage, memory usage percentage, etc.) in a bar chart.
[0083] For example, taking resources as peripherals (such as I / O ports, serial ports, buses, etc.) as an example, the third interface can display the usage status of peripherals through icons, color codes, etc.
[0084] In some embodiments, users can also manually configure the code generator. In this embodiment, such as... Figure 1The method also includes: receiving configuration operations performed by the user on the displayed fourth interface, the configuration operations including configuration operations on the third resources of the MCU chip and the configuration parameters of the third resources; generating driver code for the MCU chip based on the configuration operations using a code generator; and displaying the resource usage and resource configuration parameters of the MCU chip corresponding to the configuration operations.
[0085] In this embodiment, the fourth interface can be located on the same display interface as the first to third interfaces, or it can be located on different display interfaces.
[0086] The third resource can be one or more resources included in the MCU chip. The third resource can be the same as or different from the first resource, second resource, etc. mentioned above.
[0087] In some embodiments, the fourth interface includes configuration items for resources. For example, taking a serial port as an example, the configuration list may include working mode, baud rate, clock division, FIFO, etc. The configuration operations performed by the user on the fourth interface may include checking configuration items, manually entering configuration parameters, etc. For example, the user can manually check the configuration items of the resource on the fourth interface, or directly fill in the specific configuration parameters of the resource.
[0088] In this embodiment, when the user manually configures the code generator to generate code, the interface can update and display the current configuration status and resource changes in real time based on the user's configuration. For example, when the user manually adjusts the configuration of a GPIO port, the corresponding GPIO port icon color and configuration parameters on the interface are updated immediately, allowing the user to intuitively understand the impact of the configuration.
[0089] This disclosure provides users with two methods: AI interaction and manual code generator configuration, catering to the habits and needs of different developers. Those familiar with traditional development methods can continue to complete development through manual code generator configuration. Meanwhile, those seeking efficiency can quickly achieve their goals with the help of AI interaction.
[0090] In this embodiment of the disclosure, the visual interface can intuitively display the resource utilization of the MCU chip. Developers can understand in real time which resources are occupied and which resources are still available, facilitating reasonable resource planning, avoiding resource conflicts, and optimizing code performance. For example, when developing a multi-tasking MCU application, developers can clearly see the usage of resources such as memory and timers through the visual interface, adjust task allocation in a timely manner, and ensure stable system operation.
[0091] In some embodiments, code inspection may be automatically performed after code generation (such as, but not limited to, the driver code, application layer code, and code resulting from the integration of the application layer code and driver layer code described above). In this embodiment, for example... Figure 1 The method also includes: performing at least one of the following checks on the generated code according to preset code style and syntax rules: variable definition (e.g., whether the variable definition is correct), function call (e.g., whether the function call is correct), syntax (e.g., whether there are syntax errors), and code style (e.g., whether the code style is consistent); if an error is detected in the generated code, a reminder message is displayed, which includes the error location (e.g., the line of code where the error is located) and the error type.
[0092] For example, reminder messages can be displayed in various forms such as pop-ups, lists, etc.
[0093] In some embodiments, at least one of the above checks can also be performed by invoking a static code analysis tool.
[0094] This disclosure provides an embodiment with automatic static code analysis capabilities, enabling the immediate detection of syntax errors after code generation, thus avoiding oversights that may occur during manual code checks by developers. The automatic static code analysis function performs syntax and logic checks on the code immediately after generation, promptly identifying and alerting to errors, and helping developers quickly correct problems.
[0095] In some embodiments, the generated code can also be tested using test code. In this embodiment, such as... Figure 1 The method also includes: for each sub-code set included in the generated code, generating unit test code corresponding to the sub-code set based on the function implemented by the sub-code set and the relationship between the input parameters and output parameters of the sub-code set; the unit test code is used to perform boundary condition and exception test on the input parameters to verify the correctness of the output parameters of the sub-code set; the generated code is the driver code or application layer code of the MCU chip; the integrated application layer code and the driver code of the MCU chip are integrated with the unit test code to generate application engineering code.
[0096] In some embodiments, the generated code may include one or more sets of sub-code. Different sets of sub-code can be used to implement different functions. For example, sub-code set 1 implements LED initialization, sub-code set 2 implements LED lighting, sub-code set 3 implements button reading, and sub-code set 4 implements a delay function, etc.
[0097] Boundary conditions can include inputting maximum, minimum, or critical values. Exceptions can include inputting incorrect, invalid, or null values.
[0098] By integrating the application layer code, MCU chip driver code, and unit test code, users can easily run and test the code at any time during development, ensuring code quality.
[0099] This disclosure generates test code for different code modules (i.e., sub-code sets), greatly improving code reliability. In traditional development, manually writing test code is not only time-consuming but also difficult to cover all possible situations. Automatically generated test code can comprehensively test the performance of the generated code under various boundary conditions and abnormal situations, ensuring the correctness of each code module. This reduces code debugging time, shortens the development cycle, improves overall development efficiency, and enables products to be delivered to the market faster.
[0100] To implement the above embodiments, this disclosure also proposes an MCU code generation apparatus.
[0101] Figure 4 This is a schematic diagram of the structure of an MCU code generation apparatus provided in an embodiment of this disclosure. The apparatus 400 can be implemented by software and / or hardware and is generally integrated into an electronic device. Figure 4 As shown, the device includes a visualization interaction module 401, an AI agent and code generator integration module 402, a code integration module 403, an automatic code inspection module 404, a test generation module 405, and a model training module 406.
[0102] The visualization interaction module 401 is used to receive development requirements expressed in natural language by the user on the first displayed interface; the AI agent and code generator integration module 402 is used to perform semantic analysis on the development requirements using the AI agent, determine the first resources and functional modules of the MCU chip corresponding to the development requirements, and generate configuration parameters based on the first resources and functional modules; the AI agent and code generator integration module 402 is also used to send the configuration parameters to the code generator using the AI agent, wherein the AI agent and the code generator communicate through an API interface; the AI agent and code generator integration module 402 is also used to generate the driver code of the MCU chip corresponding to the development requirements based on the configuration parameters using the code generator.
[0103] In some embodiments, the visualization interaction module 401 is further configured to receive a program flowchart input by the user on the displayed second interface. The program flowchart is a flowchart used to characterize the application's running logic, determined according to the application function to be implemented. The AI agent and code generator integration module 402 is further configured to use the AI agent to identify the program flowchart and obtain an identification result. The identification result includes at least one node and the flow direction. Based on the identification result, the application execution action corresponding to each node in the at least one node and the execution logic relationship between the nodes are determined. The AI agent and code generator integration module 402 is further configured to use the AI agent to determine the second resource of the corresponding MCU chip and the configuration parameters of the second resource based on the application execution action corresponding to each node and the execution logic relationship between the nodes. The application layer code is generated based on the second resource and the configuration parameters of the second resource. The code integration module 403 is configured to integrate the application layer code and the driver code of the MCU chip so that the application layer code can call the driver code to implement the application function to be implemented.
[0104] In some embodiments, the visualization interaction module 401 is also used to display a third interface, which includes the resource usage and resource configuration parameters of the MCU chip.
[0105] In some embodiments, the visualization interaction module 401 is further configured to receive configuration operations performed by the user on the displayed fourth interface, the configuration operations including configuration operations on the third resources of the MCU chip and the configuration parameters of the third resources; the AI agent and code generator integration module 402 is further configured to generate driver code for the MCU chip based on the configuration operations using the code generator; the visualization interaction module 401 is further configured to display the resource usage and resource configuration parameters of the MCU chip corresponding to the configuration operations.
[0106] In some embodiments, the automatic code inspection module 404 is used to perform at least one of the following checks on the generated code according to preset code specifications and syntax rules: variable definition, function call, syntax, and code style; the visual interaction module 401 is also used to display a reminder message when an error is detected in the generated code, the reminder message including the error location and error type.
[0107] In some embodiments, the test generation module 405 is used to generate unit test code corresponding to each sub-code set included in the generated code, based on the functions implemented by the sub-code set and the relationship between the input parameters and output parameters of the sub-code set; the unit test code is used to perform boundary condition and exception tests on the input parameters to verify the correctness of the output parameters of the sub-code set; the generated code is the driver code of the MCU chip or the application layer code; the test generation module 405 is also used to integrate the integrated application layer code and the driver code of the MCU chip with the unit test code to generate application engineering code.
[0108] In some embodiments, the model training module 406 is used to acquire training data of the MCU chip, including the hardware architecture, instruction set, register configuration, technical documents, and application cases of the MCU chip; the model training module 406 is also used to train a model based on the training data to obtain an AI agent.
[0109] The MCU code generation apparatus provided in this disclosure can execute the MCU code generation method provided in any embodiment of this disclosure, and has the corresponding functional modules and beneficial effects of executing the method.
[0110] Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this disclosure. The electronic device can be a terminal as described in the above embodiments. The electronic device provided in this disclosure can execute the processing flow provided in the method embodiments, such as… Figure 5 As shown, the electronic device 50 includes: a memory 51, a processor 52, a computer program, and a communication interface 53; wherein the computer program is stored in the memory 51 and is configured to be executed by the processor 52 using the MCU code generation method described above.
[0111] In addition, this disclosure also provides a computer-readable storage medium storing a computer program thereon, which is executed by a processor to implement the MCU code generation method described in the above embodiments.
[0112] Furthermore, this disclosure also provides a computer program product, which includes a computer program or instructions that, when executed by a processor, implement the MCU code generation method described above.
[0113] It should be noted that, in this document, relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0114] The above description is merely a specific embodiment of this disclosure, enabling those skilled in the art to understand or implement it. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this disclosure. Therefore, this disclosure is not to be limited to the embodiments described herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for generating MCU code, characterized in that, The method includes: Receive development requirements expressed in natural language by the user on the first displayed interface; Using an AI agent, semantic analysis is performed on the development requirements to determine the first resources and functional modules of the MCU chip corresponding to the development requirements, and configuration parameters are generated based on the first resources and the functional modules. The configuration parameters are sent to the code generator using the AI agent, wherein the AI agent and the code generator communicate via an API interface; The code generator is used to generate driver code for the MCU chip corresponding to the development requirements based on the configuration parameters.
2. The method according to claim 1, characterized in that, The method further includes: The system receives a program flowchart input by the user on the second displayed interface. The program flowchart is a flowchart that represents the running logic of the application, determined according to the functions of the application to be implemented. The AI agent is used to identify the program flowchart to obtain the identification result, which includes at least one node and the flow direction. Based on the identification result, the application execution action corresponding to each of the at least one node and the execution logic relationship between the nodes are determined. The AI agent uses the actions executed by the application corresponding to each node and the execution logic relationship between the nodes to determine the second resource of the corresponding MCU chip and the configuration parameters of the second resource, and generates application layer code based on the second resource and the configuration parameters of the second resource. The application layer code and the driver code of the MCU chip are integrated so that the application layer code can call the driver code to implement the application function to be implemented.
3. The method according to claim 1 or 2, characterized in that, The method further includes: The third interface is displayed, which includes the resource usage and resource configuration parameters of the MCU chip.
4. The method according to claim 1 or 2, characterized in that, The method further includes: The system receives configuration operations performed by the user on the displayed fourth interface, the configuration operations including configuration operations for the third resource of the MCU chip and the configuration parameters of the third resource; The code generator is used to generate driver code for the MCU chip based on the configuration operation; Displays the resource usage and resource configuration parameters of the MCU chip corresponding to the configuration operation.
5. The method according to claim 1 or 2, characterized in that, The method further includes: The generated code will undergo at least one of the following checks based on the preset code style and syntax rules: Variable definition, function call, syntax, and code style; If an error is detected in the generated code, a warning message is displayed, which includes the location and type of the error.
6. The method according to claim 2, characterized in that, The method further includes: For each sub-code set included in the generated code, based on the function implemented by the sub-code set and the relationship between the input parameters and output parameters of the sub-code set, unit test code corresponding to the sub-code set is generated; the unit test code is used to perform boundary condition and abnormal case tests on the input parameters to verify the correctness of the output parameters of the sub-code set; the generated code is the driver code of the MCU chip or the application layer code; The integrated application layer code and MCU chip driver code are combined with the unit test code to generate application engineering code.
7. The method according to claim 1 or 2, characterized in that, The method further includes: Obtain training data for the MCU chip, including the MCU chip's hardware architecture, instruction set, register configuration, technical documents, and application cases; The AI agent is obtained by training the model based on the training data.
8. An MCU code generation device, characterized in that, The device includes: The visual interaction module is used to receive development requirements expressed in natural language by the user on the first displayed interface; The AI agent and code generator integration module is used to perform semantic analysis on the development requirements using the AI agent, determine the first resources and functional modules of the MCU chip corresponding to the development requirements, and generate configuration parameters based on the first resources and the functional modules. The AI agent and code generator integration module is also used to send the configuration parameters to the code generator using the AI agent, wherein the AI agent and the code generator communicate through an API interface; The AI agent and code generator integration module is also used to generate driver code for the MCU chip corresponding to the development requirements based on the configuration parameters using the code generator.
9. An electronic device, characterized in that, include: Memory; processor; as well as Computer programs; The computer program is stored in the memory and configured to be executed by the processor to implement the method as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, It stores a computer program / instruction thereon, which, when executed by a processor, implements the steps of the method described in any one of claims 1-7.
