A method for locating code running position before abnormal reset of MCU and MCU
By defining the storage area and setting position interface in the MCU, the information of the running position of the code before the abnormal reset is stored and read out, the problem of difficulty in positioning of the MCU is solved, and an efficient and reliable positioning method is realized.
Patent Information
- Application Number
- CN202411869693.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-18
- Publication Date
- 2025-04-08
AI Technical Summary
In the embedded software development of automotive body controllers, the MCU abnormal reset problem is difficult to locate, resulting in system function errors or interruptions, and the traditional Debug debugging method is inefficient.
By defining a storage area in the linker file of the MCU, it is used to store information about the running position of the code before the abnormal reset, and setting a mark and send position interface in the code, the information is written and read out of the storage area to locate the abnormal reset position.
It realizes the rapid and accurate positioning of the code running position before the MCU abnormal reset, improves positioning efficiency, reduces system downtime, and avoids the occurrence of safety accidents.
Smart Images

Figure CN119782129A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of MCU abnormality detection, and in particular, the present invention relates to a method for locating a code running position before an MCU abnormally resets and the MCU. Background Art
[0002] In the embedded software development of automobile body controllers, the MCU (microcontroller) abnormal reset problem has always been a difficult problem that plagues software development engineers.
[0003] In embedded systems, abnormal MCU reset may cause errors or interruptions in system functions. Therefore, it is necessary to locate the abnormal MCU reset position. Locating the abnormal reset position can help developers quickly determine which part of the code or hardware caused the problem. For example, if the MCU in an industrial automation control system frequently resets abnormally, it is found through positioning that it is caused by the code part that communicates with a specific sensor. Then, the sensor interface circuit, communication protocol code and other related parts can be checked in detail to quickly fix the problem and reduce system downtime.
[0004] For some complex embedded products, such as automotive electronic systems and medical equipment, it is crucial to quickly fix abnormal reset problems. Especially in the test of electronic systems in a real car environment, abnormal resets are usually uncertain and sporadic. Taking the automotive engine control unit (ECU) as an example, abnormal reset of the MCU may affect the normal operation of the engine. Locating the reset position helps to solve the problem in time and avoid safety accidents. In the prior art, downloader Debug debugging is usually used for positioning, but the traditional downloader Debug debugging method requires specifying the memory address during operation. Since it is impossible to predict where to set the breakpoint in the code, repeated testing is required and the positioning efficiency is low.
[0005] Therefore, the present invention proposes a method for locating the code running position before abnormal reset of MCU and MCU to solve this problem. Summary of the invention
[0006] The present invention aims to overcome the shortcomings of the prior art and proposes a method for locating the code running position before the MCU is abnormally reset and an MCU, so as to achieve the following purposes: solve the difficulty in troubleshooting the MCU abnormal reset problem in the development of embedded software for automobile controllers and realize the positioning of the code running position before the MCU is abnormally reset.
[0007] In order to achieve the above object, the technical solution adopted by the present invention is: a method for locating the code running position before the abnormal reset of MCU, the method comprising the following steps:
[0008] Step S1, defining a storage area in the linker file of the MCU for storing position information, wherein the storage area retains the previous value after the MCU is reset and will not be cleared;
[0009] Step S2, at different positions of the original code, writing corresponding position information into the storage area by setting a mark position interface;
[0010] Step S3, in the original code, the information in the storage area is read out by setting a sending location interface;
[0011] Step S4: When the MCU is abnormally reset, the code running position before the MCU is abnormally reset in the storage area is read out by calling the sending position interface.
[0012] Preferably, in the step S1, the storage area is constructed based on a non-initialized SRAM.
[0013] Preferably, the storage area is configured to store only a preset amount of location information.
[0014] Preferably, the marking position interface marks the program position in chronological order according to the running order of the task function scheduling in the code; that is, the marking position interface is set at the input or output end of each task function, and when the code executes to the marking position interface, the corresponding position information is saved in the storage area and arranged in chronological order.
[0015] Preferably, the location information includes the function name and line number of the location of the marked location interface.
[0016] Preferably, the location information is stored in the storage area in ASCII code format.
[0017] Preferably, the sending position interface is set after the code initialization is completed.
[0018] Preferably, the sending position interface is configured to: when the MCU receives a preset test message sent through the CAN bus, call the sending position interface to read the position information in the storage area through the CAN bus.
[0019] At the same time, the application also proposes an MCU, which is burned with an executable file compiled and linked according to the above-mentioned method for locating the code running position before the MCU is abnormally reset.
[0020] The technical effect of the present invention is as follows: the present invention provides an efficient and reliable method, which can effectively solve the difficulty in locating the abnormal reset problem of MCU in the development of embedded software for automobile controllers, and provides a powerful auxiliary tool for engineers.
[0021] The present invention has the following advantages:
[0022] (1) It makes up for the defect that the debug downloader cannot locate the abnormal reset scenario and provides a new positioning method.
[0023] (2) It can quantitatively capture occasional uncertain abnormal resets and locate the code position quickly and accurately.
[0024] (3) By storing and parsing information in ASCII code, the information is clear at a glance and easy to read and understand. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] Figure 1 A flow chart of a method for locating the code running position before an abnormal reset of an MCU is provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0026] The following is a further detailed description of the specific implementation of the present invention by describing the embodiments with reference to the accompanying drawings, with the purpose of helping those skilled in the art to have a more complete, accurate and in-depth understanding of the inventive concept and technical solution of the present invention and to facilitate its implementation. In order to make the technical solution of the present invention clearer, the present invention is explained by the following embodiments.
[0027] This embodiment provides a method for locating the code running position before the MCU is abnormally reset. The process is as follows: Figure 1 As shown, the method comprises the following steps:
[0028] Step S1, defining a storage area in the linker file of the MCU for storing position information, wherein the storage area retains the previous value after the MCU is reset and will not be cleared;
[0029] Step S2, at different positions of the original code, writing corresponding position information into the storage area by setting a mark position interface;
[0030] Step S3, in the original code, the information in the storage area is read out by setting a sending location interface;
[0031] Step S4, compile and link the processed code to generate an executable file HEX, and burn it into the MCU. When the MCU is abnormally reset, the code running position before the MCU is abnormally reset in the storage area is read out by calling the sending position interface.
[0032] Specifically, in step S1 of this embodiment, a storage area is defined in the linker file of the MCU based on the non-initialized SRAM to form a section of the non-initialized SRAM, wherein the mark location interface is also defined in the section of the non-initialized SRAM.
[0033] The section of the non-initialized SRAM is configured as follows: it will not be automatically initialized or cleared after the MCU is started or reset, and can be used to save some data that needs to be retained in reset or abnormal situations. In this embodiment, the position information obtained by the mark position interface is saved in the section of the non-initialized SRAM, so that the previously stored position information can be read after reset to help locate the location where the abnormal reset occurs.
[0034] In embedded systems, the memory resources of the MCU are usually limited. Although the non-initialized SRAM provides a flexible storage method, it is easy to cause memory overflow if information is stored without restriction. Therefore, in order to avoid the above situation and improve the working efficiency of the MCU, the storage area, that is, the section of the non-initialized SRAM is configured to only save a preset number of location information. For example, in this embodiment, only the latest two location information are saved (the specific number can be flexibly set according to the actual situation), which can ensure that the location information newly written by the mark location interface can be effectively saved.
[0035] In step S2 of this embodiment, a marking position interface is pre-set at different positions of the code to write the position information of the marking position interface into the section of the non-initialized SRAM. In this embodiment, the marking position interface marks the program position in chronological order according to the running sequence of the task function scheduling in the code; that is, the marking position interface is set at the input end or output end of each task function, and when the code executes to the marking position interface, the corresponding position information is saved in the section of the non-initialized SRAM and arranged in chronological order.
[0036] When executing a task, the MCU usually schedules the task execution according to the task priority and other rules. According to the setting of the mark position interface of this embodiment, the position information is stored in the section of the non-initialized SRAM according to the time sequence of task execution. According to the stored position information, the MCU can locate the code running position before reset after reset.
[0037] Calling the mark position interface according to the specific timing of code execution means that corresponding position "marks" can be left at each key node and important stage of code execution. For example, marking the entry of function calls, the start and end of loop structures, and the front and back of key algorithm modules, thus building a complete and detailed code running track chain, providing an extremely accurate basis for locating the code running position before reset.
[0038] In order to more accurately locate the code running position before reset, taking into account the memory resources of the MCU, in this embodiment, the position information written by the mark position interface includes the function name and line number of the position of the mark position interface, so that the accurate and efficient positioning of the code running position before reset can be achieved. In other preferred embodiments of the present application, if the memory resources of the MCU are sufficient, other position information can also be added to further improve the positioning accuracy.
[0039] In this embodiment, the location information such as the function name and line number of the location of the marking location interface is saved in the section of the non-initialized SRAM in ASCII code format, and is also subsequently parsed in ASCII code format, which facilitates the storage and parsing of information and ensures the accuracy and completeness of the entire process from storage, transmission to parsing of information to the greatest extent; at the same time, as a widely recognized character encoding standard, the information is clear at a glance and easy for engineers to read and understand.
[0040] After the position information of the non-initialized SRAM section is written through the mark position interface, it is necessary to be able to read it in time after the MCU is abnormally reset to achieve positioning. Therefore, in step S3 of this embodiment, a send position interface is set in the code, and when it is called, the position information in the non-initialized SRAM section can be automatically read and sent.
[0041] In this embodiment, the sending position interface is set after the code initialization is completed. Starting to read the position information from the key node of code initialization completion can ensure that the read information completely covers all stages of the MCU from being ready to run to the subsequent actual running process.
[0042] When existing engineers conduct vehicle fault testing, they usually send test messages to each electronic unit of the vehicle through the CAN bus. Different electronic units correspond to different test messages, and each electronic unit feeds back the fault diagnosis results according to the corresponding test message. Therefore, in order to adapt to the current situation of vehicle diagnosis, the sending position interface of this embodiment is configured as follows: when the MCU receives a preset test message sent through the CAN bus, the sending position interface is called to read the location information in the section of the non-initialized SRAM through the CAN bus. For example, the ID of a CAN test message is 0x14, and the corresponding ID has been defined in the sending position interface. At this time, the CAN test message indicates that the MCU is fault diagnosed to call the sending position interface. In specific implementation, the CAN test message ID that calls the sending position interface can be flexibly set according to actual conditions.
[0043] Finally, in step S4 of the embodiment, the code processed according to the above method is compiled and linked to generate an executable file HEX, and burned into the MCU. When the MCU is abnormally reset, the sending position interface is called through the CAN bus, and the information recorded in the non-initialized SRAM is sent out through the CAN message. The user then parses the CAN message read from the CAN bus, and parses the ASCII code information therein to obtain the code running position before the MCU is abnormally reset.
[0044] The present application also proposes an MCU, wherein the MCU is burned with an executable file generated by compiling and linking according to the above-mentioned method for locating the code running position before abnormal reset of the MCU.
[0045] The present invention provides an efficient and reliable method, which can effectively solve the difficulty of locating the abnormal reset problem of MCU in the development of embedded software for automobile controllers, and provides a powerful auxiliary tool for engineers. The present invention has the following advantages:
[0046] (1) It makes up for the defect that the debug downloader cannot locate the abnormal reset scenario and provides a new positioning method.
[0047] (2) It can quantitatively capture occasional uncertain abnormal resets and locate the code position quickly and accurately.
[0048] (3) By storing and parsing information in ASCII code, the information is clear at a glance and easy to read and understand.
[0049] In summary, the present invention proposes a method for locating the code running position before the abnormal reset of the MCU and an MCU, which are used to solve the difficulty of troubleshooting the abnormal reset problem of the MCU in the development of embedded software for automotive controllers. The present invention utilizes the static retention characteristics of the non-initialized SRAM to store the code position information just before the reset into the non-initialized SRAM, and reads the position information through the CAN message to assist engineers in locating and analyzing the problem. Simply put, the present invention defines a section of the static non-initialized SRAM, and provides a sending position interface and a marking position interface, so as to store the position information in the non-initialized SRAM in the form of ASCII code, and reads and parses the code running position before the abnormal reset of the MCU through the CAN bus.
[0050] The present invention is described above by way of example in conjunction with the accompanying drawings. Obviously, the specific implementation of the present invention is not limited to the above-mentioned methods. As long as various non-substantial improvements are made using the method concept and technical solution of the present invention; or the above concept and technical solution of the present invention are directly applied to other occasions without improvement, they are all within the protection scope of the present invention.
Claims
1. A method for locating the code running position before an abnormal reset of an MCU, characterized in that: The method comprises the following steps: Step S1, defining a storage area in the linker file of the MCU for storing position information, wherein the storage area retains the previous value after the MCU is reset and will not be cleared; Step S2, at different positions of the original code, writing corresponding position information into the storage area by setting a mark position interface; Step S3, in the original code, the information in the storage area is read out by setting a sending location interface; Step S4: When the MCU is abnormally reset, the code running position before the MCU is abnormally reset in the storage area is read out by calling the sending position interface.
2. The method for locating the code running position before abnormal reset of an MCU according to claim 1, characterized in that: In the step S1, the storage area is constructed based on a non-initialized SRAM.
3. A method for locating the code running position before abnormal reset of an MCU according to claim 1 or 2, characterized in that: The storage area is configured to store only a preset amount of position information.
4. The method for locating the code running position before abnormal reset of an MCU according to claim 1, characterized in that: The marking position interface marks the program position in chronological order according to the running order of the task function scheduling in the code; that is, the marking position interface is set at the input or output end of each task function, and when the code executes to the marking position interface, the corresponding position information is saved in the storage area and arranged in chronological order.
5. The method for locating the code running position before abnormal reset of MCU according to claim 4 is characterized in that: The location information includes the function name and line number of the location of the marked location interface.
6. A method for locating the code running position before abnormal reset of MCU according to claim 4 or 5, characterized in that: The location information is stored in the storage area in ASCII code format.
7. The method for locating the code running position before abnormal reset of MCU according to claim 1, characterized in that: The sending position interface is set after the code initialization is completed.
8. A method for locating the code running position before abnormal reset of MCU according to claim 1 or 7, characterized in that: The sending location interface is configured to: when the MCU receives a preset test message sent through the CAN bus, call the sending location interface to read the location information in the storage area through the CAN bus.
9. An MCU, characterized in that: The MCU is burned with an executable file generated by compiling and linking according to the method for locating the code running position before abnormal reset of the MCU according to any one of claims 1 to 8.