Controller, program execution method, and electronic device

By introducing a first and a second memory area into the MCU and using function pointers to jump and execute the updated machine code, the problem of difficult MCU program updates is solved, and flexible and efficient function updates are achieved.

CN122152365APending Publication Date: 2026-06-05BEIJING ESWIN COMPUTING TECH CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
BEIJING ESWIN COMPUTING TECH CO LTD
Filing Date
2026-02-02
Publication Date
2026-06-05

Smart Images

  • Figure CN122152365A_ABST
    Figure CN122152365A_ABST
Patent Text Reader

Abstract

The application discloses a controller, a program execution method and an electronic device, and belongs to the technical field of integrated circuit design. A first storage area of the controller is configured to store first instruction codes of an application program, the first instruction codes include at least one target function position, a function at the target function position is a function to be updated, and a function pointer corresponding to the function to be updated is further configured at the target function position. A processor of the controller is configured to acquire second machine codes, store the second machine codes in a second storage area, and the second machine codes are results after the function to be updated at the at least one target function position is updated. The first machine codes are executed, and in the case that the first machine codes are executed to the target function position, the second machine codes in the second storage area matched with the function pointer are executed according to the function pointer at the target function position. Therefore, the flexibility of program updating can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of integrated circuit design technology, and in particular to a controller, a program execution method, and an electronic device. Background Technology

[0002] A microcontroller unit (MCU) is a microcomputer system that integrates a processor, memory, and peripheral interfaces. MCUs enable automated device management through programmable design and are widely used in automotive electronics, industrial control, and consumer electronics. Therefore, with the development of the Internet of Things (IoT) technology and to meet the increasing demands for terminal device functionality, MCUs are being widely embedded in various terminal devices to improve their intelligence and automation capabilities.

[0003] One of the main characteristics of an MCU is its programmability. Specifically, a C language program or an assembly language program is compiled to obtain binary instruction code. By downloading these binary instruction codes into the MCU's memory, the MCU can control the terminal device according to the program logic of the instruction code. This enables the terminal device to control various peripherals.

[0004] In embedded systems, to ensure the MCU can quickly enter a working state after power-on, the program's instruction code is typically stored in the MCU's Read Only Memory (ROM) area. However, since the contents of the ROM are almost impossible to modify through software, and in rare cases the contents can be updated through complex and time-consuming methods such as re-programming the entire ROM or using special equipment, it is extremely difficult to add, remove, or modify the MCU's functions later. Summary of the Invention

[0005] This application provides a controller, a program execution method, and an electronic device to solve at least some of the problems in the related art. The technical solution is as follows: In a first aspect, this application provides a controller, which includes a processor, a first storage area, and a second storage area; the first storage area is configured to store first machine code of an application, the first machine code including at least one target function location, the function at the target function location being a function to be updated, and a function pointer corresponding to the function to be updated being configured at the target function location; the processor is configured to acquire second machine code and store the second machine code in the second storage area of ​​the controller, the second machine code being the result of updating the function to be updated at at least one target function location; the processor is further configured to execute the first machine code, and when the first machine code is executed to the target function location, jump to execute the second machine code in the second storage area that matches the function pointer according to the function pointer at the target function location.

[0006] In some possible implementations, the function pointer includes an address variable that points to the first storage address of the corresponding second machine code in the second storage area. When the first machine code is executed to the target function location, and the processor jumps to execute the second machine code in the second storage area that matches the function pointer at the target function location, the processor is configured to: when the first machine code is executed to the target function location, jump to the first storage address pointed to by the address variable in the function pointer at the target function location; and execute the second machine code at the first storage address pointed to by the address variable.

[0007] In some possible implementations, the function pointer also includes an enable variable, which is used to indicate the enable state of the corresponding function pointer. When the first machine code is executed to the target function location, during the process of jumping to execute the second machine code in the second storage area that matches the function pointer according to the function pointer at the target function location, the processor is configured to: when the first machine code is executed to the target function location, determine whether to perform a jump operation according to the enable state of the function pointer at the target function location; if the enable state of the function pointer at the target function location is valid, jump to execute the second machine code in the second storage area that matches the function pointer according to the function pointer at the target function location.

[0008] In some possible implementations, the function pointer includes an address variable and an enable variable. After acquiring the second machine code, the processor is further configured to: generate configuration information and control information corresponding to the first machine code based on the second machine code; the configuration information is used to indicate the second storage address of the second machine code in the external memory of the controller; the control information is used to characterize the enable state of the function pointer in the first machine code; and update the enable variable of the corresponding function pointer based on the control information. During the process of storing the second machine code in the second storage area of ​​the controller, the processor is configured to: load the second machine code from the second storage address to the first storage address of the second storage area based on the configuration information.

[0009] In some possible implementations, before executing the first machine code, the processor is also configured to configure the enable state of each function pointer according to the type of each function pointer in the first machine code.

[0010] In some possible implementations, the function pointer type includes one or more of the following: function pointers corresponding to functions with no input and output parameters, function pointers corresponding to functions with input and output parameters, and function pointers corresponding to functions with input parameters but no output parameters.

[0011] Secondly, a program execution method is also provided. The method is applied to a controller, where a first storage area of ​​the controller stores a first instruction code of an application. The first instruction code includes at least one target function location, where the function at the target function location is a function to be updated, and a function pointer corresponding to the function to be updated is also configured at the target function location. The method includes: obtaining a second machine code, which is the result of updating the function to be updated at at least one target function location; storing the second machine code in a second storage area of ​​the controller; executing the first machine code; and, when the first machine code is executed to the target function location, jumping to execute the second machine code in the second storage area that matches the function pointer, based on the function pointer at the target function location.

[0012] In some possible implementations, the function pointer includes an address variable that points to the first storage address of the corresponding second machine code in the second storage area. When the first machine code is executed to the target function location, the execution jumps to the second machine code in the second storage area that matches the function pointer, based on the function pointer at the target function location. This includes: when the first machine code is executed to the target function location, the execution jumps to the first storage address pointed to by the address variable in the function pointer at the target function location; and executing the second machine code at the first storage address pointed to by the address variable.

[0013] In some possible implementations, the function pointer also includes an enable variable, which indicates the enable state of the corresponding function pointer. When the first machine code is executed to the target function location, the execution jumps to the second machine code in the second storage area that matches the function pointer, based on the function pointer at the target function location. This includes: when the first machine code is executed to the target function location, determining whether to perform a jump operation based on the enable state of the function pointer at the target function location; if the enable state of the function pointer at the target function location is valid, jumping to the second machine code in the second storage area that matches the function pointer based on the address variable in the function pointer at the target function location.

[0014] In some possible implementations, the function pointer includes an address variable and an enable variable. After obtaining the second machine code, the method further includes: generating configuration information and control information corresponding to the first machine code based on the second machine code; the configuration information is used to indicate the second storage address of the second machine code in the external memory of the controller; the control information is used to characterize the enable state of the function pointer in the first machine code; updating the enable variable of the corresponding function pointer based on the control information; and storing the second machine code in the second storage area of ​​the controller, including: loading the second machine code from the second storage address to the first storage address of the second storage area based on the configuration information.

[0015] In some possible implementations, before executing the first machine code, the method further includes configuring the enable state of each function pointer according to the type of each function pointer in the first machine code.

[0016] In some possible implementations, the function pointer type includes one or more of the following: function pointers corresponding to functions with no input and output parameters, function pointers corresponding to functions with input and output parameters, and function pointers corresponding to functions with input parameters but no output parameters.

[0017] Thirdly, an electronic device is also provided, comprising a controller as described in the first aspect and any possible implementation thereof, and an external memory connected to the controller, the external memory being used to store a second machine code for the controller's processor to load the second machine code into the controller's second storage area.

[0018] Fourthly, an electronic device is provided, comprising a processor and a memory, wherein the memory stores at least one computer program, and the at least one computer program is loaded and executed by the processor to enable the electronic device to implement the program execution method in the second aspect and any possible implementation thereof.

[0019] Fifthly, a computer-readable storage medium is also provided, wherein at least one computer program is stored in the computer-readable storage medium, and the at least one computer program is loaded and executed by a processor to enable the computer to implement the program execution method in the second aspect and any possible implementation thereof.

[0020] Sixthly, a computer program product or computer program is also provided, comprising computer instructions stored in a computer-readable storage medium. The processor of the computer device reads the computer instructions from the computer-readable storage medium, executes the computer instructions, and causes the computer device to perform the program execution method of the second aspect described above and any possible implementation thereof.

[0021] The technical solution provided in this application brings at least the following beneficial effects: This application provides a controller that, for the machine code (i.e., the first machine code) of an application stored in the controller's storage area (i.e., the first storage area), can load the updated result (e.g., addition, removal, or modification) of a function to be updated in the application into a storage area (i.e., the second storage area). Thus, when the first machine code of the application is executed, if it reaches the location of the function to be updated (i.e., the target function location), it jumps to execute the updated result of that function in the second storage area according to the function pointer at that target function location. This eliminates the need to re-program the first machine code in the controller, allowing for flexible application updates, reducing the difficulty of program updates, improving efficiency, and helping to reduce costs. Attached Figure Description

[0022] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0023] Figure 1 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application; Figure 2 This is a schematic diagram of the structure of a controller provided in an embodiment of this application; Figure 3 This is a schematic flowchart of program execution provided in an embodiment of this application; Figure 4 This is a flowchart illustrating a program execution method provided in an embodiment of this application. Detailed Implementation

[0024] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings.

[0025] This application provides a controller that allows for the flexible addition, removal, or modification of certain functions during program execution.

[0026] To facilitate understanding of the technical solution of this application, an implementation environment provided by an embodiment of this application is described below.

[0027] For example, an implementation environment provided in this application embodiment may include a first device and a second device. The first device may act as a host, and the second device may be used to host a target platform. The host is used to compile programs and deploy the compiled program's machine code (such as binary instruction code) to the target platform for execution.

[0028] As an example, the first device can be a physical machine, a virtualized device, an embedded device, or at least one node in a computer cluster, wherein the virtualized device can be a virtual machine or a container, and the embedded device can be a development board, but is not limited thereto.

[0029] As an example, the target platform can be the platform or environment in which the program runs. The target platform can be a chip, such as a Riscv, x86, or ARM architecture chip, or it can be an embedded system (such as an MCU). The second device carrying the target platform can be a physical machine, a virtualized device, an embedded device, or at least one node in a computer cluster.

[0030] In this way, in a cross-compilation scenario, the machine code of the program is compiled for the target platform by the host, and then the machine code is deployed to the target platform hosted on the second device and runs on the target platform.

[0031] In other examples, machine code compiled on the host can also run on that host. For example, the target platform could be a chip on the host side, and the machine code compiled on the host could be deployed to that target platform to run.

[0032] For example, Figure 1 A schematic diagram of an electronic device according to an embodiment of this application is shown. This electronic device can be implemented as a second device carrying a target platform in the above-described implementation environment, but is not limited thereto. Figure 1 As shown, the electronic device 10 may include an MCU 100 and an external memory 200 connected to the MCU 100. The MCU 100 is, for example, the aforementioned target platform. The external memory 200 can be used to store data required by the MCU 100, such as part or all of the machine code of the application.

[0033] For example, the MCU 100 may include a processor 110, internal memory 120, and communication interface 130. The processor 110 may include a Central Processing Unit (CPU), a Graphics Processing Unit (GPU), a Neural Network Processing Unit (NPU), a Tensor Processing Unit (TPU), a System on Chip (SOC), or other types of processors with data processing and / or program execution capabilities. The processor 110 may be a general-purpose processor or a special-purpose processor.

[0034] For example, the internal memory 120 may include a program storage area and a data storage area. The program storage area may include a first storage area and a second storage area. The first storage area can be used to store the machine code (also referred to herein as first machine code) of the application program executed by the processor 110. The first machine code may include at least one objective function location, each objective function location being a function to be updated (e.g., but not limited to adding, removing, or modifying). The data storage area can be used to receive and store data entering from the external memory 200 of the MCU 100. This data can also be transferred to the second storage area of ​​the program storage area. For example, the data storage area receives the updated function machine code (also referred to herein as second machine code) loaded from the external memory 200, and subsequently, this second machine code is input into the second storage area. Thus, the processor 110 can subsequently dynamically execute the first / second machine code in the first and second storage areas to implement the functions of the corresponding application program.

[0035] In one possible implementation, the first and second storage areas, as well as the data storage area, can all be implemented as one or more forms of computer-readable storage media. As an example, the first storage area can be non-volatile memory used to persistently store the first machine code of the application. Non-volatile memory includes, but is not limited to, read-only memory (ROM), hard disks, electrically erasable programmable read-only memory (EPROM), portable compact disc read-only memory (CD-ROM), USB storage, flash memory, etc. The second storage area can be volatile memory used to temporarily store data transferred from the data storage area, such as the second machine code corresponding to the aforementioned updated function. Volatile memory includes, but is not limited to, random access memory (RAM) and / or cache memory, and can directly exchange data with the processor 110.

[0036] In addition, the data storage area may include volatile memory (such as RAM) or erasable non-volatile memory, without being limited to a single type.

[0037] For example, the communication interface 130 can be a wired interface or a wireless interface, used to communicate with external devices under the control of the processor 110. The wired interface can be an Ethernet interface, a controller area network (CAN) interface, a local interconnect network (LIN) interface, or a FlexRay interface, while the wireless interface can be a cellular network interface or a wireless LAN interface, etc. The wired or wireless interface allows the electronic device 10 to communicate with other devices over a network to exchange data.

[0038] For example, external memory 200 may include, but is not limited to, non-volatile and erasable memory, such as EPROM or Flash, to be able to frequently store second machine code corresponding to updated functions in the application.

[0039] It should be noted that Figure 1 The structures shown are merely illustrative of the possible structures of an electronic device 10 provided in this application embodiment, and are not intended to be the sole limitation on the structure or function of the electronic device 10. In fact, the electronic device 10 can be implemented in a manner more complex than... Figure 1 The structure shown may have more or fewer components.

[0040] The following describes a controller provided by an embodiment of this application.

[0041] For example, Figure 2 The diagram shows a schematic representation of a controller according to an embodiment of this application. Figure 2 As shown, the controller may include a processor, a first storage area, and a second storage area. For example, the controller can be applied to the target platform described above or as... Figure 1 The electronic device 10 shown, or implemented as Figure 1 The MCU100 shown is an example, but not limited to it. In this example, the controller is implemented as... Figure 1 Taking the MCU100 shown as an example, the MCU100 includes a processor 110 and a first storage area and a second storage area provided by internal memory 120. Wherein: The first storage area can be configured to store the first machine code of the application, wherein the first machine code includes at least one target function location, the target function at the target function location is the function to be updated, and the target function location is also configured with a function pointer corresponding to the function to be updated; The processor 110 can be configured to acquire a second machine code and store the second machine code in a second storage area of ​​the controller. The second machine code is the result of updating a function at at least one target function location. The processor 110 can also be configured to execute a first machine code. If the first machine code is executed to the target function location, the processor 110 can jump to execute the second machine code in the second storage area that matches the function pointer at the target function location.

[0042] For example Figure 3 As shown, functions F1 and F2 in the first machine code are functions to be updated, and the locations of functions F1 and F2 are the target function locations S1 and S2, respectively. The target function locations S1 and S2 also include function pointers A and B for functions F1 and F2, respectively. The second storage area stores the functions F1' and F2' obtained after updating functions F1 and F2. When the processor 110 executes the first machine code to the target function location S1, it can jump to execute the updated function F1' in the second storage area based on the function pointer A at the target function location S1. After function F1' finishes execution, it returns downwards (that is, from S1 onwards) to continue executing the first machine code of the program. Similarly, when the processor 110 executes the first machine code to the target function location S2, it can jump to execute the updated function F2' in the second storage area based on the function pointer B at the target function location S2.

[0043] In this way, when the application deployed in the chip (i.e., MCU100) needs to be updated, such as adding, removing, or modifying functions at the target function locations within the application, it is not necessary to re-program the first machine code in the first memory area. Instead, the updated machine code (i.e., the second machine code) at these target function locations can be written separately and stored in the second memory area of ​​the MCU100. Therefore, when the application is executed to the target function location, it can automatically jump to the second machine code in the second memory area for execution, that is, automatically execute the updated machine code. This approach offers high flexibility. Compared to updating applications through programming, this embodiment can achieve program updates more efficiently, reducing the difficulty of application updates in the controller, improving update efficiency, and also helping to reduce costs.

[0044] Next, we will introduce the working principle of MCU100 in detail.

[0045] In this embodiment, the host (such as the first device in the above embodiment) can be used to compile the first machine code of the application deployed in the MCU100. For example, the first machine code of the application can be compiled using the toolchain built into the host and deployed to the MCU100.

[0046] During the compilation of the application's first machine code, the locations of functions that are likely to be frequently updated in the application (i.e., target function locations) can be marked, and corresponding function pointers can be configured at each target function location. The function pointer is a pointer variable that stores a memory address, pointing to the updated result of the function at the target function location stored at that memory address, thus allowing the call to access the updated result based on the function pointer.

[0047] For example, function pointers can be categorized into corresponding pointer types based on the function's type. This could involve classifying functions according to their input and output value types, and defining corresponding function pointers for each type.

[0048] As examples, not limitations, the following three types of function pointers are defined according to the input and output value types of functions: typedef void( HookFnPtrO)(void); / / Function pointer to a function with no input or output parameters. typedefint( HookFnPtrl)(int); / / Function pointer corresponding to the function with input and output parameters typedef void( HookFnPtr2)(int,short); / / A function pointer corresponding to a function that takes input parameters but has no output parameters.

[0049] Thus, since different types of functions have different update frequencies, the corresponding function pointers can also be configured with appropriate enable states. For example, functions without input and output parameters have a lower update frequency and may be updated during chip version upgrades; functions with input and output parameters, as well as functions with input but no output parameters, often involve data processing, algorithm optimization, hardware control, interface changes, etc., and their update frequency may be higher. Therefore, in the first machine code of the application, a corresponding enable bit can be configured for the function pointer at each target function location. When the enable bit is in the active state, the function pointer can be used.

[0050] For example, function pointers for functions with input and output parameters, and functions with input but no output parameters, can be given an enabled state, while function pointers for functions without input and output parameters are given an disabled state, meaning these function pointers are unusable. The enabled state is then restored when updating functions without input and output parameters. Therefore, invalid function pointers are ignored during the execution of the first machine code, meaning that jump operations are not triggered at the locations of invalid function pointers in the first machine code, avoiding unnecessary runtime overhead caused by too many function pointers during the execution of the first machine code.

[0051] One example is the control of the enable bit of a function pointer of type HookFnPtr0, as shown in the following code: if(HookEnable&0x02) { HookFnPtr0 ptrFnHook0 = (HookFnPtr0)usPtrFnHook[1]; ptrFnHook0(); } else { FnModuleConfigLoadandSet(); }

[0052] In the code above, `HookEnable` is a multi-bit enable variable. Each bit of `HookEnable` controls whether a function pointer works. When a bit in `HookEnable` corresponding to a function pointer is set to "1", the function pointer works (i.e., it is valid); when set to "0", the function pointer does not work (i.e., it is invalid). For example, function pointer A corresponds to bit 0 of the `HookEnable` variable, function pointer B corresponds to bit 1 of the `HookEnable` variable, function pointer C corresponds to bit 2 of the `HookEnable` variable, and so on. In the code above, 0x02 is the hexadecimal representation of the binary number 00000010, which is the second bit of `HookEnable`, and `&` is the bitwise AND operator. `if(HookEnable&0x02)` checks whether the second bit of `HookEnable` is 1. If it is, it enters the `if` branch; otherwise, it enters the `else` branch.

[0053] In the if branch, HookFnPtr0 is a function pointer type that points to a function with a return value of void and a parameter of void; ptrFnHook0 is a way to dynamically call a function, which is intended to call the function pointed to by HookFnPtr0; usPtrFnHook[1] is an array element (i.e., an address variable) that represents the starting address of the added, removed, or modified function in memory (e.g., the second storage area mentioned above); (HookFnPtr0) is a type casting, used to implement the conversion between the machine code on usPtrFnHook[1] and the function pointer of type HookFnPtr0.

[0054] In the else branch, if the second bit of HookEnable is 0, the FnModuleConfigLoadandSet() function at the target function location in the first machine code of the application is called directly, meaning that this function is not updated at this time.

[0055] It should be noted that the enable bit of the function pointer can also be controlled in other ways with similar logic to the code in the above example, which will not be listed in this embodiment.

[0056] In addition, in the code of this example, usPtrFnHook[1] and HookEnable are global variables. The values ​​of usPtrFnHook[1] and HookEnable can be stored in the external memory 200 of MCU100. After MCU100 is powered on, the values ​​of usPtrFnHook[1] and HookEnable in the external memory 200 are read to configure whether the function pointers in the first machine code are enabled.

[0057] For example, when it is necessary to modify the first machine code of the application, the first machine code can be recompiled on the host machine. That is, the functions at the target function locations are updated in the source file of the first machine code. Thus, the recompiled source file contains the updated results for these functions. Next, the host machine modifies the linker script of the compiler in the toolchain to configure the memory address in the second storage area (also referred to herein as the first storage address) for each updated result of the function, resulting in the aforementioned recompiled binary file of the source file. Then, the binary machine code corresponding to these updated results of the function (i.e., the second machine code) is extracted from this binary file, and this second machine code is deployed to the external memory 200 attached to the MCU100.

[0058] In this example, after the second machine code is deployed to the external memory 200 attached to the MCU100, the processor 110 on the MCU100 can also be configured to generate corresponding configuration information and control information based on the second machine code of the external memory 200, so as to update the functions to be updated in the program at an appropriate time (e.g., when the MCU100 is powered on again).

[0059] The configuration information is used to indicate that the second machine code is moved from the storage address (also called the second storage address) of the external memory 200 to the first storage address of the second storage area; the control information is used to characterize the enable state of the function pointer in the first machine code deployed on the MCU100.

[0060] As an example and not a limitation, the storage information on external memory 200 can be referenced in Table 1 below.

[0061]

[0062] Table 1 In Table 1, "hook" refers to a function call mechanism that changes program behavior by replacing function addresses with function pointers. Hook0 to hook7 represent different function pointers. In other words, each hook in this example uses a function pointer to replace the original call to the function to be updated in the first machine code with a call to the aforementioned second machine code. Therefore, the "EEPROM address" column represents the storage address in external memory 200 (such as EEPROM) (also referred to as the second storage address in this paper), the "value" column represents the value corresponding to the "EEPROM address" column, and the "description" column represents the functional description of the second storage address in the "EEPROM address" column.

[0063] For example, the function description of storage address E8 in Table 1, "[07:00] Ram hook 07~00 enable: Each bit (0 or 1) represents whether the hook function is off or on," means that this address stores the hook enable information in the bit field [07:00] of the second storage area (RAM), where each bit (0 or 1) indicates whether the hook function is off or on. For example, the above-mentioned enable variable HookEnable can be generated based on this function description.

[0064] In Table 1, the functional description of storage address E9, "[07:00] hook code EEPROM start address msb[15:08]: represent the high eight bits of the start EEPROM address where the hook function code is stored," means that this address stores the high 8 bits (Most Significant Bit, MSB) [15:08] of the start address of the hook function code stored in external memory 200. Similarly, the functional description of storage address EA means that this address stores the low 8 bits (Least Significant Bit, LSB) [07:00] of the start address of the hook function code stored in external memory 200. The functional description of storage address EB means that this address stores the high 8 bits of the length of the hook function code, and the functional description of storage address EC means that this address stores the low 8 bits of the length of the hook function code. Therefore, the function call mechanism can be correctly triggered.

[0065] Furthermore, the functional description of storage address ED in Table 1 indicates that this address stores the high 8 bits of the starting address of hook0, and the functional description of storage address EE indicates that this address stores the low 8 bits of the starting address of hook0. This allows us to obtain the entry address of the second machine code corresponding to the function pointer hook0. Storage addresses EF to FE are similar and will not be described in detail.

[0066] Thus, control information can be generated based on the enable information stored on external memory 200 in Table 1, so as to update the enable bits of each function pointer in the first machine code of the application (e.g., update the enable variable HookEnable). In addition, the above configuration information can be generated based on the second storage address of the second machine code corresponding to each function pointer (e.g., hook0 to hook7) stored on external memory 200, so as to indicate that the second machine code is loaded from the second storage address to the first storage address pointed to by the address variable (e.g., usPtrFnHook[1]) in the function pointer.

[0067] Specifically, after the MCU100 is powered on again, the processor 110 on the MCU100 can cache the second machine code from the second memory address in the external memory 200 into the data storage area based on the configuration information, and then load it from the data storage area to the first memory address in the second memory area. Next, the processor 110 can execute the first machine code in the first memory area. In this example, when the processor 110 executes the first machine code to the target function location, it can determine whether to perform a jump operation based on the enable state of the function pointer at the target function location. For example, if the value of the enable bit of the function pointer is "1", the function pointer is enabled and the jump operation is performed; if the value of the enable bit of the function pointer is "0", the function pointer is disabled and the jump operation is not performed, meaning execution continues at the target function location.

[0068] In this example, when performing a jump operation based on a function pointer, the processor 110 can be configured to jump to the first memory address pointed to by the address variable on the function pointer in the second memory area, and execute the second machine code at that first memory address. This executes the updated function logic, achieving the purpose of function updating. Therefore, machine code updates can be achieved without reprogramming the first machine code, offering high flexibility.

[0069] Next, a program execution method provided by an embodiment of this application will be described.

[0070] For example, Figure 4 The diagram illustrates a flowchart of a program execution method according to an embodiment of this application. This method can be executed on any suitable device, equipment, or platform, such as the target platform described above. Figure 1This is executed on the MCU100 shown. Taking execution on a controller such as the MCU100 as an example, the controller's first memory area stores the first instruction code of the application program. The first instruction code includes at least one target function location, the function at the target function location is the function to be updated, and the target function location is also configured with a function pointer corresponding to the function to be updated. Then, as shown... Figure 4 As shown, this method may include steps S610 to S640.

[0071] S610, Obtain the second machine code, which is the result of updating the function to be updated at at least one objective function position; S620, stores the second machine code in the second storage area of ​​the controller; S630, executes the first machine code; S640, when the first machine code is executed to the target function location, jump to execute the second machine code in the second storage area that matches the function pointer, according to the function pointer at the target function location.

[0072] In some possible implementations, the function pointer includes an address variable that points to the first storage address of the corresponding second machine code in the second storage area. When the first machine code is executed to the target function location, the execution jumps to the second machine code in the second storage area that matches the function pointer, based on the function pointer at the target function location. This includes: when the first machine code is executed to the target function location, the execution jumps to the first storage address pointed to by the address variable in the function pointer at the target function location; and executing the second machine code at the first storage address pointed to by the address variable.

[0073] In some possible implementations, the function pointer also includes an enable variable, which indicates the enable state of the corresponding function pointer. When the first machine code is executed to the target function location, the execution jumps to the second machine code in the second storage area that matches the function pointer, based on the function pointer at the target function location. This includes: when the first machine code is executed to the target function location, determining whether to perform a jump operation based on the enable state of the function pointer at the target function location; if the enable state of the function pointer at the target function location is valid, jumping to the second machine code in the second storage area that matches the function pointer based on the address variable in the function pointer at the target function location.

[0074] In some possible implementations, the function pointer includes an address variable and an enable variable. After obtaining the second machine code, the method further includes: generating configuration information and control information corresponding to the first machine code based on the second machine code; the configuration information is used to indicate the second storage address of the second machine code in the external memory of the controller; the control information is used to characterize the enable state of the function pointer in the first machine code; updating the enable variable of the corresponding function pointer based on the control information; and storing the second machine code in the second storage area of ​​the controller, including: loading the second machine code from the second storage address to the first storage address of the second storage area based on the configuration information.

[0075] In some possible implementations, before executing the first machine code, the method further includes configuring the enable state of each function pointer according to the type of each function pointer in the first machine code.

[0076] In some possible implementations, the function pointer type includes one or more of the following: function pointers corresponding to functions with no input and output parameters, function pointers corresponding to functions with input and output parameters, and function pointers corresponding to functions with input parameters but no output parameters.

[0077] It should be noted that, for information regarding the function or beneficial effects of each step in the above-mentioned program execution method, please refer to the description of a controller provided in the embodiments of this application in the foregoing embodiments, which will not be repeated here.

[0078] In an exemplary embodiment, an electronic device is also provided, comprising a processor and a memory, wherein at least one computer program is stored in the memory. The at least one computer program is loaded and executed by one or more processors to enable the electronic device to implement any of the program execution methods described above.

[0079] In an exemplary embodiment, a computer-readable storage medium is also provided, which stores at least one computer program. This at least one computer program is loaded and executed by a processor of a computer device to enable the computer to implement any of the above-described program execution methods. The computer-readable storage medium may be a read-only memory (ROM), a random access memory (RAM), a compact disc read-only memory (CD-ROM), magnetic tape, floppy disk, or optical data storage device, etc.

[0080] In an exemplary embodiment, a computer program product or computer program is also provided, which includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform any of the above-described program execution methods.

[0081] It should be understood that "multiple" as used in this article refers to two or more. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A alone, A and B simultaneously, or B alone. The character " / " generally indicates that the preceding and following related objects have an "or" relationship.

[0082] The above description is merely an exemplary embodiment of this application and is not intended to limit this application. Any modifications, equivalent substitutions, improvements, etc., made within the principles of this application should be included within the protection scope of this application.

Claims

1. A controller, characterized in that, The controller includes a processor, a first storage area, and a second storage area; The first storage area is configured to store the first machine code of the application. The first machine code includes at least one target function location, the function at the target function location is the function to be updated, and the target function location is also configured with a function pointer corresponding to the function to be updated. The processor is configured to acquire a second machine code and store the second machine code in a second storage area, wherein the second machine code is the result of updating the function to be updated at the position of the at least one objective function; The processor is further configured to execute the first machine code, and when the first machine code is executed to the target function location, to jump to execute the second machine code in the second storage area that matches the function pointer, based on the function pointer at the target function location.

2. The controller according to claim 1, characterized in that, The function pointer includes an address variable, which is used to point to the first storage address of the corresponding second machine code in the second storage area; When the first machine code is executed to the target function location, during the process of jumping to execute the second machine code in the second storage area that matches the function pointer according to the function pointer at the target function location, the processor is configured to: When the first machine code is executed to the target function location, the execution jumps to the first storage address pointed to by the address variable in the function pointer at the target function location, according to the address variable in the function pointer at the target function location. Execute the second machine code at the first storage address pointed to by the address variable.

3. The controller according to claim 1, characterized in that, The function pointer also includes an enable variable, which is used to indicate the enable state of the corresponding function pointer; When the first machine code is executed to the target function location, during the process of jumping to execute the second machine code in the second storage area that matches the function pointer according to the function pointer at the target function location, the processor is configured to: When the first machine code is executed to the target function location, it is determined whether to perform a jump operation based on the enable state of the function pointer at the target function location; If the function pointer at the target function location is enabled, the execution jumps to the second machine code in the second storage area that matches the function pointer, based on the function pointer at the target function location.

4. The controller according to any one of claims 1 to 3, characterized in that, The function pointer includes an address variable and an enable variable. After acquiring the second machine code, the processor is further configured to: Based on the second machine code, configuration information and control information corresponding to the first machine code are generated. The configuration information is used to indicate the second storage address of the second machine code in the external memory of the controller. The control information is used to characterize the enable state of the function pointer in the first machine code. Based on the control information, update the enable variable of the corresponding function pointer; During the process of storing the second machine code into the second storage area, the processor is configured to: According to the configuration information, the second machine code is loaded from the second storage address to the first storage address of the second storage area.

5. The controller according to claim 4, characterized in that, Before executing the first machine code, the processor is also configured to: Configure the enable state of each function pointer according to the type of each function pointer in the first machine code.

6. A program execution method, characterized in that, The method is applied to a controller, wherein a first storage area of ​​the controller stores a first instruction code of an application, the first instruction code including at least one target function location, the function at the target function location being the function to be updated, and a function pointer corresponding to the function to be updated is also configured at the target function location. The method includes: Obtain the second machine code, which is the result of updating the function to be updated at at least one objective function position; The second machine code is stored in the second storage area of ​​the controller; Execute the first machine code; When the first machine code is executed to the target function location, the execution jumps to the second machine code in the second storage area that matches the function pointer, based on the function pointer at the target function location.

7. The method according to claim 6, characterized in that, The function pointer includes an address variable, which is used to point to the first storage address of the corresponding second machine code in the second storage area; When the first machine code is executed to the target function location, the execution jumps to execute the second machine code in the second storage area that matches the function pointer, based on the function pointer at the target function location, including: When the first machine code is executed to the target function location, the execution jumps to the first storage address pointed to by the address variable in the function pointer at the target function location, according to the address variable in the function pointer at the target function location. Execute the second machine code at the first storage address pointed to by the address variable.

8. The method according to claim 6, characterized in that, The function pointer also includes an enable variable, which is used to indicate the enable state of the corresponding function pointer; When the first machine code is executed to the target function location, the execution jumps to execute the second machine code in the second storage area that matches the function pointer, based on the function pointer at the target function location, including: When the first machine code is executed to the target function location, it is determined whether to perform a jump operation based on the enable state of the function pointer at the target function location; If the function pointer at the target function location is enabled, the execution jumps to the second machine code in the second storage area that matches the function pointer, based on the address variable in the function pointer at the target function location.

9. The method according to any one of claims 6 to 8, characterized in that, The function pointer includes an address variable and an enable variable. After obtaining the second machine code, the method further includes: Based on the second machine code, configuration information and control information corresponding to the first machine code are generated. The configuration information is used to indicate the second storage address of the second machine code in the external memory of the controller. The control information is used to characterize the enable state of the function pointer in the first machine code. Based on the control information, update the enable variable of the corresponding function pointer; The step of storing the second machine code in the second storage area of ​​the controller includes: According to the configuration information, the second machine code is loaded from the second storage address to the first storage address of the second storage area.

10. The method according to claim 9, characterized in that, Before executing the first machine code, the method further includes: Configure the enable state of each function pointer according to the type of each function pointer in the first machine code.

11. An electronic device, characterized in that, The electronic device includes the controller according to any one of claims 1 to 5, and an external memory connected to the controller; The external memory connected to the controller is used to store the second machine code, so that the processor of the controller can load the second machine code into the second storage area of ​​the controller.