Single-wire Emulation Device and Encoding / Decoding Method for Microcontroller

Through single-line simulation devices and Manchester encoding, the problems of complex connection between emulators and microcontrollers and driver installation in the prior art are solved, and simplified connection and convenient data transmission are achieved.

CN113741217BActive Publication Date: 2025-07-11JIANGSU JITRI INTELLIGENT INTEGRATED CIRCUIT DESIGN TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111119963.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-09-24
Publication Date
2025-07-11
Estimated Expiration
2041-09-24

AI Technical Summary

Technical Problem

The existing microcontroller emulator needs to be connected to the upper computer PC, which increases the difficulty of development, and the JTAG/SWD interface connection is complex and takes up a lot of resources.

Method used

A single-line simulation device is used to connect the emulator and the microcontroller through a single communication line, and a USB HID device is connected to the upper computer. It uses Manchester encoding to transmit data, and implements encoding and decoding through timer capture.

Benefits of technology

It simplifies the connection operation between the emulator and the microcontroller, reduces development difficulty, reduces resource usage, and realizes convenient encoding and decoding of data transmission.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113741217B_ABST
    Figure CN113741217B_ABST
Patent Text Reader

Abstract

The present invention discloses a single-wire simulation device for a microcontroller and its encoding and decoding method, which facilitates the connection between the emulator and the microcontroller, and can reduce the development difficulty of the emulator. The single-wire simulation device includes an emulator, which is respectively connected to the microcontroller and the host computer. The emulator is connected to the microcontroller by a single wire. One end of the single wire is connected to the debug port of the microcontroller, and the other end is connected to a communication pin of the emulator. The emulator is connected to the host computer through a USB HID device. The emulator includes hardware modules such as a controller, a clock, and a timer. The encoding and decoding method includes: the encoding method, the communication method, and the decoding method between the emulator and the microcontroller. In the encoding method, the communication protocol between the emulator and the microcontroller uses Manchester encoding. When decoding the Manchester encoding, the capture function of the timer is used. When the emulator receives and decodes the data sent by the microcontroller, decoding is performed in the interrupt program through the capture method of the timer.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of microcontrollers, and particularly to a single-wire simulation device and a coding and decoding method for a microcontroller. Background Art

[0002] A microcontroller (MCU) is a single-chip microcomputer that integrates the main parts of a microcomputer on a single chip, and its development requires the use of corresponding development tools and simulation tools. Currently, the main simulation tools on the market are emulators. The simulation interfaces used when the emulator communicates with the microcontroller mainly include the JTAG interface and the SWD interface. The JTAG interface is a four-wire protocol, and the SWD interface is a three-wire protocol (excluding the power supply VCC and the ground wire GND). Developers can download programs through the simulation interface and can perform operations such as breakpoint setting, variable viewing, full-speed running, and single-step running through the Keil system.

[0003] The JTAG interface and the SWD interface have a relatively large number of pins. When developing low-cost microcontrollers, if the number of chip pins reserved is relatively small, it is necessary to repeatedly perform the operations of disassembling and installing the connecting wires, and additional connecting wires and connecting devices are required to achieve interface connection and communication. Such a method is not only complex in operation but also occupies a large amount of resources. In addition, the connection between the emulator and the host PC is generally achieved through the USB interface. When the USB is started, a driver program needs to be installed in the corresponding host computer. Therefore, the emulator provider needs to design a specific driver program, which not only limits the scope of application and flexibility of use of the emulator but also increases the workload of the manufacturer and the development difficulty of the emulator. Summary of the Invention

[0004] Aiming at the problems in the prior art that the method of connecting the emulator to the microcontroller through the JTAG interface or the SWD interface is complex in operation and occupies a large amount of resources, and when connecting the emulator to the host PC through the USB interface, a driver program needs to be installed, increasing the development difficulty of the emulator, the present invention provides a single-wire simulation device for a microcontroller, the structure design of which is simple and reasonable, facilitating the connection between the emulator and the microcontroller, and reducing the development difficulty of the emulator.

[0005] To achieve the above object, the present invention adopts the following technical solutions:

[0006] A single-wire simulation device for a microcontroller, which includes an emulator. The emulator is used in the development of the microcontroller. The emulator is respectively connected to the microcontroller and the host computer. It is characterized in that the emulator and the microcontroller are connected by a single wire. The single wire is a communication line. One end of the single wire is connected to the debug port of the microcontroller, and the other end is connected to the communication pin of the emulator. The emulator is connected to the host computer through a USB HID device. The emulator includes a controller, a clock, and a timer. The clock and the timer are both connected to the controller.

[0007] It is further characterized in that,

[0008] The 12th pin of the controller is connected to the microcontroller through the single wire;

[0009] The model of the controller is STM32F103;

[0010] The USB HID device is connected to the 29th pin of the controller of the emulator through a USB HID device connection circuit. The USB HID device connection circuit includes a triode Q1, resistors R15, R23, R26, R34, and R35;

[0011] It further includes a light prompt module. The light prompt module includes light-emitting diodes YELLOW, RED, GREEN, resistors R36, R53, R54, R21, and a capacitor C21. The light prompt module is connected to the 7th, 31st, and 45th pins of the controller;

[0012] An external circuit is connected to the periphery of the controller. The external circuit includes a crystal oscillator Y1. One end of the crystal oscillator Y1 is respectively connected to a capacitor C23, a resistor R37, and the 5th pin of the controller. The other end of the crystal oscillator Y1 is respectively connected to one end of the resistor R37, one end of the capacitor C28, and the 6th pin of the controller. The other ends of the capacitors C23 and C28 are grounded. The 9th pin of the controller is respectively connected to one end of capacitors C29 and C30. The other ends of capacitors C29 and C30 are grounded. The 44th pin of the controller is connected to one end of a resistor R3. The other end of the resistor R3 is grounded.

[0013] A coding and decoding method based on the above single-wire simulation device. The coding and decoding method includes a coding method, a communication method, and a decoding method between the emulator and the microcontroller. It is characterized in that,

[0014] In the coding method, the communication protocol between the emulator and the microcontroller adopts Manchester coding;

[0015] When decoding the Manchester code, use the capture function of the timer of the emulator to perform single-wire communication with the microcontroller through the communication pin of the controller in the emulator. The communication method includes: controlling the output mode and input mode of the communication pin of the emulator. When the communication pin is in the output mode, the emulator sends data to the microcontroller. When the communication pin is in the input mode, the microcontroller sends data to the emulator;

[0016] When the emulator receives the data sent by the microcontroller, it is carried out through the interruption of the capture mode of the second timer. The decoding method includes: B1. Data initialization. The data includes the number of received data bits, denoted as count_int (i.e., the number of interrupts), with an initial value of 0. The period for the microcontroller to send one bit of data, denoted as capture_T. The half period for the microcontroller to send one bit of data, denoted as in_half_T, with an initial value of 0. Decoding error, denoted as debug_error. The previous counting value of the timer, denoted as capture_value1, and the current counting value of the timer, denoted as capture_value2, both with initial values of 0. The time difference capture_value between the rising edge and the falling edge of the communication pin, and the current level value of the communication pin, denoted as io_value, with a high level of 1 and a low level of 0. An array capture_data

[256] of 256 received data is used to store the received data;

[0017] B2. Enter the interrupt;

[0018] B3. Determine whether the voltage of the communication pin meets the conditions, that is, determine whether the level of the communication pin is high level 1. If so, set the falling edge of the clock signal of the communication pin to trigger, and the high level value of the communication pin is 1. Otherwise, set the rising edge of the clock signal of the communication pin to trigger, and the level value of the communication pin is 0;

[0019] B4. Obtain the current counting value of the timer. The time difference between the rising edge and the falling edge of the communication pin is the current counting value of the timer minus the previous counting value of the timer;

[0020] B5. Determine whether the number of received data bits is equal to zero. If so, the array of received data is equal to the current level value of the communication pin, the number of received data bits is accumulated by 1, and the interrupt returns. If not, enter step B51:

[0021] B51. If capture_T - 2 <= capture_value <= capture_T + 2, then:

[0022] a. If in_half_T = 1, then debug_error = 1, and interrupt and return.

[0023] b. If in_half_T = 0, then capture_data[count_int] = io_value, the number of bits of the received data is incremented by 1, and interrupt and return.

[0024] B52. If capture_T / 2 - 2 <= capture_value <= capture_T / 2 + 2, then:

[0025] a. If in_half_T = 0, then in_half_T = 1, and interrupt and return.

[0026] b. If in_half_T = 1, then capture_data[count_int] = io_value, the number of bits of the received data is incremented by 1, and in_half_T = 0 and interrupt and return.

[0027] B53. If the capture_value exceeds the pre-set threshold range, then debug_error = 1, and interrupt and return.

[0028] B6. Interrupt and return, and repeat step B1 when the next interrupt signal is received.

[0029] After the data reception is completed, the values in the array capture_data

[256] are combined to obtain the required decoded data.

[0030] Its further feature is that

[0031] In the communication method, the communication frequency between the emulator and the microcontroller is 10k - 500kHz;

[0032] In the communication method, the output mode and input mode of the communication pin of the emulator are controlled by a clock and a timer, including: A1: Before the controller in the emulator performs normal reading and writing on the microcontroller, the frequency of the microcontroller is measured to establish a handshake connection, and the microcontroller sends a clock signal when it is powered on or reset.

[0033] A2. When the communication pin of the emulator is in the input mode, capture is performed by the timer. The timer captures the rising edge of the clock signal and calculates the duration between two adjacent edges as the clock period at the same time.

[0034] A3. When the communication pin of the emulator is in the output mode, a number of "0"s are sent to the microcontroller, and the number of "0"s is the same as the number of the clock signals. After receiving the "0"s, the microcontroller sends a handshake connection signal to the emulator;

[0035] A4. After the emulator and the microcontroller successfully handshake, the emulator reads and writes relevant debug registers of the microcontroller, and the reading and writing include writing data and reading data.

[0036] In step A3, the handshake connection signal is ECHO, and ECHO consists of 010;

[0037] In step A4, if writing data, the emulator issues a write instruction and writes the data to be written. After receiving the write instruction and the data, the microcontroller returns a first response signal;

[0038] If reading data, the emulator issues a read instruction. After receiving the read instruction, the microcontroller issues response data. After receiving the response data, the emulator gives a second response signal;

[0039] The microcontroller issues the response data when the communication pin of the emulator is in the output mode. The response data includes 0 or 1. The ways for the microcontroller to send 0 or 1 include:

[0040] When sending 0, the communication pin outputs a high level, with a delay, then the communication pin outputs a low level, with a delay;

[0041] When sending 1, the communication pin outputs a low level, with a delay, then the communication pin outputs a high level, with a delay;

[0042] The length of the delay needs to ensure that the output frequency of the emulator is equal to the frequency issued by the microcontroller when establishing the handshake connection.

[0043] Adopting the above structure of the present invention can achieve the following beneficial effects: (1) In the single-wire simulation device of the present application, the emulator and the microcontroller are connected by a single wire. The single wire is a single communication line. One end of the single wire is connected to the debug port of the microcontroller, and the other end is connected to the communication pin of the emulator. Thus, the communication connection between the emulator and the microcontroller is realized through a single communication line. This connection only occupies the communication pin of the emulator and does not need to occupy more pin or port resources. The structure is simple, and the connection operation is simple and fast.

[0044] (2) The emulator is connected to the host computer through a USB HID device. The USB HID device is a human-machine interface device that complies with the HID class specification and can be used without installing a driver. Therefore, when connecting the emulator to the host computer through the USB HID device, there is no need to develop a dedicated driver or install a driver in the host computer, which reduces the development difficulty.

[0045] (3) This application also provides an encoding and decoding method. When this method is applied to the microcontroller development process, it includes the encoding method, communication method, and decoding method between the emulator and the microcontroller. When the connection between the emulator and the microcontroller is a single-wire connection, the encoding operation in the development process is implemented through the Manchester encoding method. By controlling the communication pin of the emulator and the single-wire connection to the microcontroller, the communication for data input or data output is realized. By controlling the interrupt of the communication pin and the measurement of the communication pin level value, the decoding of the received data is realized. Thus, the encoding and decoding operations in the data transmission process when the microcontroller is connected to the emulator by a single wire are realized. The encoding and decoding operations are convenient and fast, which is conducive to the realization of single-wire communication connection and also facilitates the development of the microcontroller. BRIEF DESCRIPTION OF THE DRAWINGS

[0046] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following will briefly introduce the drawings required for the description of the embodiments. The drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.

[0047] Figure 1 It is the connection structure block diagram of the single-wire simulation device of the present invention;

[0048] Figure 2 It is the circuit schematic diagram of the controller interface circuit and its peripheral circuits in the emulator of the present invention;

[0049] Figure 3 It is the operation flow chart of the emulator of the present invention;

[0050] Figure 4 It is the clock signal diagram of the Manchester encoding in the encoding and decoding method of the present invention;

[0051] Figure 5 It is the flowchart of the microcontroller receiving data decoding of the present invention;

[0052] Figure 6 It is the circuit schematic diagram of the USB HID device connection circuit of the present invention;

[0053] Figure 7 It is the circuit schematic diagram of the light prompt module of the present invention;

[0054] Figure 8 The figure shows the signal diagram (i.e., the communication effect diagram) transmitted between the emulator and the microcontroller captured by a logic analyzer after adopting the single-wire simulation device and the encoding and decoding method of the present invention. Specific embodiments

[0055] In order to enable those skilled in the art to better understand the solution of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.

[0056] It should be noted that the terms "including" and "having" and any variations thereof in the specification and claims of the present invention and the above-mentioned drawings are intended to cover non-exclusive inclusion. For example, a process, method, device, product or equipment that includes a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or equipment.

[0057] In an embodiment of the present invention, a single-wire simulation device for a microcontroller is provided. Refer to Figure 1 、 Figure 2 , which includes an emulator 1. The emulator 1 is used in the development of the microcontroller 2. The emulator 1 is respectively connected to the microcontroller 2 and the host computer 4. The emulator 1 and the microcontroller 2 are connected through a single wire 5 (excluding the power supply line VCC and the ground wire GND). The single wire 5 is a signal line. One end of the single wire 5 is connected to the debugging port of the microcontroller 2, and the other end is connected to the communication pin PA2 of the emulator 1. Data exchange between the emulator and the microcontroller is realized through the single wire 5. When the emulator 1 is connected to the USB of the host computer 4 (i.e., a PC), a standard USB HID device 3 is used, so there is no need to develop a special driver for the emulator because the Windows of the host computer 4 comes with a USB HID driver.

[0058] The emulator 1 includes a controller, a clock, and a timer. The clock and the timer are both connected to the controller. In this embodiment, the model of the controller is STM32F103. Firmware is written here. The controller performs simulation data communication with the microcontroller 2 through a GPIO pin (i.e., the communication pin PA2). The microcontroller is an 8051-type microcontroller, which has a built-in Debug hardware module responsible for communicating with the emulator and can control the execution process of the microcontroller.

[0059] The USB HID device is connected to the 29th, 32nd, and 33rd pins of the controller through the USB HID device connection circuit, as shown in Figure 6 , the USB HID device connection circuit includes transistor Q1, resistors R15, R23, R26, R34, and R35; the specific connection structure of the USB HID device connection circuit is as follows: the 12th pin of the controller is connected to the other end of a single wire, the 29th pin of the controller is connected to one end of resistor R15, the other end of resistor R15 is respectively connected to one end of resistor R23 and the base of transistor Q1, the emitter of transistor Q1 and the other end of resistor R23 are both connected to the voltage source VDD, the drain of transistor Q1 is connected to one end of resistor R26, and the other end is respectively connected to one end of resistor R35 and the 33rd pin of the controller, the other end of resistor R35 is connected to the 3rd pin of USB HID device J2, the 2nd pin of USB HID device J2 is connected to one end of resistor R34, the other end of resistor R34 is connected to the 32nd pin of the controller, the power supply port VCC of USB HID device J2 is connected to the voltage source VBUS, the 5th pin of USB HID device J2 is respectively connected to the 6th pin, one end of capacitor C74, and one end of resistor R31, and one end of capacitor C74 and the other end of resistor R31 are grounded. Transistor Q1 has a switching function to control the conduction or shutdown of the USB HID device, and resistors R15, R23, R26, R34, and R35 have voltage division and current limiting functions.

[0060] It also includes a light prompt module, as shown in Figure 7, the light prompt module includes light-emitting diodes YELLOW, RED, GREEN, resistors R36, R53, R54, R21, and capacitor C21. The light prompt module is connected to pins 7, 31, and 45 of the controller. The specific connection circuit structure of each electronic component in the light prompt module includes: pin 31 of the controller is connected to the negative electrode of the light-emitting diode YELLOW, the positive electrode of the light-emitting diode YELLOW is connected to one end of the resistor R36, the other end of the resistor R36 is respectively connected to one end of the resistor R53 and the voltage source VDD, the other end of the resistor R53 is connected to the positive electrode of the light-emitting diode RED, the negative electrode of the light-emitting diode RED is grounded, the negative electrode of the light-emitting diode GREEN is connected to pin 45 of the controller, the positive electrode of the light-emitting diode GREEN is connected to one end of the resistor R54, the other end of the resistor R54 is connected to the voltage source VDDO, pin 7 of the controller is respectively connected to one end of the resistor R21 and one end of the capacitor C21, the other end of the capacitor C21 is grounded, and the other end of the resistor R21 is connected to the voltage source VDD. The light-emitting diodes YELLOW, RED, and GREEN are used to display the working state of the controller. It is judged whether the emulator communicates normally with the microcontroller and the emulator with the host computer according to the light-emitting diodes. If it is on, it indicates normal communication. Otherwise, it indicates abnormal communication, prompting the operator to reconnect or repair. The resistors R36, R53, R54, and R21 are used for voltage division and current limiting, and the capacitor C21 is used for filtering.

[0061] There is a peripheral circuit connected to the controller. The peripheral circuit includes a crystal oscillator Y1. One end of the crystal oscillator Y1 is respectively connected to the capacitor C23, the resistor R37, and pin 5 of the controller. The other end of the crystal oscillator Y1 is respectively connected to one end of the resistor R37, one end of the capacitor C28, and pin 6 of the controller. The other ends of the capacitors C23 and C28 are grounded. Pin 9 of the controller is respectively connected to one ends of the capacitors C29 and C30. The other ends of the capacitors C29 and C30 are grounded. Pin 44 of the controller is connected to one end of the resistor R3, and the other end of the resistor R3 is grounded. The capacitors C23, C28, C29, and C30 are used for filtering to filter out interference signals in the communication data, further ensuring the accuracy of the emulator development and debugging, and at the same time ensuring the stability of the controller operation.

[0062] The working process of writing the emulator firmware refers to Figure 3 , and the working process specifically includes: sequentially implementing clock initialization, port initialization, timer initialization, USB initialization, and data processing. Among them, data processing includes sequentially implementing receiving USB data transfer commands, sending commands to the microcontroller through a single wire, decoding the data signals sent by the microcontroller in the interrupt, and replying to the USB data transfer commands. The reply to the USB data transfer commands is mainly realized by the emulator sending USB data packets to the microcontroller.

[0063] The emulator is connected to the host computer via USB and is recognized as a USB HID device. The host computer runs Keil software. According to the Keil AGDI document, a DLL plugin is written for Keil to call. Keil calls the functions in the DLL plugin, and the DLL calls the USB HID functions to communicate with the emulator.

[0064] The following is a specific embodiment of the data encoding and decoding method during data processing in the operation of the emulator firmware. Based on the encoding and decoding method of the above single-wire simulation device, the encoding and decoding method includes the encoding method, communication method, and decoding method between the emulator and the microcontroller. In the encoding method, the communication protocol between the emulator and the microcontroller adopts Manchester (i.e., Manchester) encoding. Refer to Figure 4 , when the data has a falling edge change within one cycle, it is 0, and when the data has a rising edge change within one cycle, it is 1. Figure 4 In, clock is the clock signal, data is the data change (the data is represented by 0 and 1), and the Manchester encoding follows the IEEE802.3 standard. Figure 4 In, the curve corresponding to Manchester represents the distance between the rising edge and the falling edge, and this distance is capture_T or capture_T / 2.

[0065] When decoding the above Manchester encoding, the first timer capture function of the controller is used, and the communication pin PA2 of the controller in the emulator is used to communicate with the microcontroller. The communication method includes: controlling the output mode and input mode of the communication pin PA2 of the emulator. When the communication pin PA2 is in the output mode, the emulator sends data to the microcontroller. When the communication pin PA2 is in the input mode, the microcontroller sends data to the emulator; the communication frequency between the emulator and the microcontroller is 10k - 500kHz.

[0066] In the above communication method, the output mode and input mode of the communication pin PA2 of the emulator are controlled through the clock and timer, including: A1: Before the controller in the emulator performs normal reading and writing to the microcontroller, the frequency of the microcontroller is measured to establish a handshake connection. When the microcontroller is powered on or reset, it emits 64 clock signals; A2. When the communication pin PA2 of the emulator is in the input mode, capture is performed through the timer. The timer captures 64 rising edges of the clock signal and calculates the duration between two adjacent edges as the clock period.

[0067] A3. Set the communication pin PA2 of the emulator to the output state and send 64 "0"s to the microcontroller. The sending frequency of the microcontroller is approximately equal to its output frequency, that is, the number of "0"s is the same as the number of clock signals. After the microcontroller receives 64 "0"s, it sends a handshake connection signal to the emulator. The handshake connection signal is ECHO, and ECHO consists of 010;

[0068] A4. After the emulator and the microcontroller successfully handshake, the emulator reads and writes the relevant debug registers of the microcontroller. The reading and writing include writing data and reading data. Specifically: If writing data, the emulator sends a write instruction and writes the data to be written. After the microcontroller receives the write instruction and data, it replies with a first response signal; If reading data, the emulator sends a read instruction. After the microcontroller receives the read instruction, it sends response data. After the emulator receives the instruction and response data, it gives a second response signal;

[0069] The microcontroller sends response data when the communication pin PA2 of the emulator is in the output mode. The response data includes 0 or 1. The ways for the microcontroller to send 0 or 1 are as follows: When sending 0, the communication pin PA2 outputs a high level, with a delay, then the communication pin PA2 outputs a low level, with a delay; When sending 1, the communication pin PA2 outputs a low level, with a delay, then the communication pin PA2 outputs a high level, with a delay; Among them, the length of the delay needs to ensure that the output frequency of the emulator is equal to the frequency sent by the microcontroller when establishing the handshake connection.

[0070] Reference Figure 5 , when the emulator receives the data sent by the microcontroller, it is through the interrupt function of the second timer capture method. The communication pin PA2 is switched to the input mode. First, capture the falling edge (the Manchester protocol stipulates that the first bit of data sent by the microcontroller is 0, and the first falling edge is captured). The data decoding method includes: B1. Data initialization; The data specifically includes: the number of received data bits count_int received by the emulator (that is, the number of interrupts), with an initial value of 0;

[0071] The period for the microcontroller to send one bit of data is denoted as the variable capture_T;

[0072] The half period for the microcontroller to send one bit of data is denoted as the variable in_half_T, with an initial value of 0;

[0073] Decoding error is denoted as the variable debug_error;

[0074] The value of the first timer is denoted as the variable capture_value1, and the value of the second timer is denoted as the variable capture_value2, with initial values both being 0;

[0075] The time difference between the rising edge and the falling edge of communication pin PA2 is denoted as variable capture_value;

[0076] The current level value of communication pin PA2 is denoted as variable io_value, with high level being 1 and low level being 0;

[0077] An array of 256 received data is denoted as variable capture_data

[256] , which is used to store the received data;

[0078] Decode according to the following steps: B2. Enter the interrupt;

[0079] B3. Determine whether the voltage of communication pin PA2 meets the conditions, that is, determine whether the level of communication pin PA2 is high level 1. If so, set the falling edge of the clock signal of communication pin PA2 to trigger, and the high level value of communication pin PA2 is 1, that is, io_value = 1. Otherwise, set the rising edge of the clock signal of communication pin PA2 to trigger, and the level value of communication pin PA2 is 0, that is, io_value = 0;

[0080] B4. Obtain the current count value of the timer, that is, capture_value2 = the current count value of timer TIM2. The time difference between the rising edge and the falling edge of the communication pin is the current count value of the timer minus the previous count value of the timer, that is, capture_value = capture_value2 - capture_value1;

[0081] B5. Determine whether the number of received data bits is equal to zero. If so, that is, count_int = 0, then the array of received data is equal to the current level value of communication pin PA2, that is, capture_data[count_int] = io_value, and the number of received data bits is incremented by 1, that is, count_int++, and interrupt returns. If not, that is, count_int!= 0, then enter step B51:

[0082] B51. If capture_T - 2 <= capture_value <= capture_T + 2, then:

[0083] a. If in_half_T = 1, then debug_error = 1, and interrupt returns;

[0084] b. If in_half_T = 0, then capture_data[count_int] = io_value, and the number of received data bits is incremented by 1, that is, count_int++, and interrupt returns; At this time, the captured data period = capture_T, allowing for errors.

[0085] B52. If capture_T / 2-2<=capture_value<=capture_T / 2+2, then:

[0086] a. If in_half_T = 0, then in_half_T = 1, and the interrupt returns; half the cycle of the currently received data, that is, capture_T / 2;

[0087] b. If in_half_T = 1, then capture_data[count_int] = io_value, the number of bits of received data is accumulated plus 1 (i.e. count_int++), and in_half_T = 0 interrupt returns, indicating that there is half a cycle of data before, and the current is another half cycle, so one bit of data is sampled;

[0088] B53, capture_value value exceeds or is not within the preset threshold range, that is, capture_value value is too small or too large, then debug_error = 1, interrupt return, indicating that the captured data cycle is not within the preset range;

[0089] B6. In the above steps B1 to B, after the interrupt returns, repeat step B1 the next time an interrupt signal is received.

[0090] After the data is received, the values ​​in the array capture_data

[256] are combined to obtain the required decoded data. During the data reception process, if debug_error=1, it means that this set of data is wrong. The above-mentioned single-line simulation device and encoding and decoding method are applied to the development of microcontrollers. When the simulation device and the microcontroller use the above-mentioned single line and the above-mentioned encoding and decoding method to send and reply data, a logic analyzer is used to collect the communication waveform between the simulator and the microcontroller. After the simulator sends a signal, the microcontroller quickly replies based on the encoding and decoding method of this application. See Figure 8 , the horizontal axis represents time, and the curve represents the communication signal. Figure 8 It can be seen that the single-line device and encoding and decoding method of the present application can achieve effective and fast transmission of data signals.

[0091] The above are only preferred embodiments of the present application, and the present invention is not limited to the above embodiments. It is understood that other improvements and changes directly derived or associated by those skilled in the art without departing from the spirit and concept of the invention should be considered to be included in the scope of protection of the invention.

Claims

1. A coding and decoding method, the method comprising: Coding method, communication method, and decoding method for an emulator and a microcontroller, characterized in that: In the coding method, the communication protocol between the emulator and the microcontroller uses Manchester coding; When decoding the Manchester coding, the capture function of the timer in the emulator is used, and single-wire communication is performed between the emulator and the microcontroller through the communication pin of the controller in the emulator. The communication method includes: controlling the output mode and input mode of the communication pin of the emulator. When the communication pin is in the output mode, the emulator sends data to the microcontroller. When the communication pin is in the input mode, the microcontroller sends data to the emulator; When the emulator receives and decodes the data sent by the microcontroller, decoding is performed in the interrupt program of the capture method of the timer. The decoding method includes: B1. Data initialization. The data includes the number of received data bits, denoted as count_int, with an initial value of 0, the period for the microcontroller to send one data bit, denoted as capture_T, the half-period for the microcontroller to send one data bit, denoted as in_half_T, with an initial value of 0, decoding error, denoted as debug_error, the previous counting value of the timer, denoted as capture_value1, the current counting value of the timer, denoted as capture_value2, both with initial values of 0, the time difference between the rising edge and the falling edge of the communication pin, denoted as capture_value, the current level value of the communication pin, denoted as io_value, with a high level of 1 and a low level of 0, and an array of 256 received data, denoted as capture_data[256], for storing the received data; B2. Enter the interrupt; B3. Determine whether the voltage of the communication pin meets the conditions, that is, determine whether the level of the communication pin is high level 1. If so, set the falling edge of the clock signal of the communication pin to trigger, and the high level value of the communication pin is 1. Otherwise, set the rising edge of the clock signal of the communication pin to trigger, and the level value of the communication pin is 0; B4. Obtain the current counting value of the timer. The time difference between the rising edge and the falling edge of the communication pin is the current counting value of the timer minus the previous counting value of the timer; B5. Determine whether the number of received data bits is equal to zero. If so, the array of received data is equal to the current level value of the communication pin, the number of received data bits is incremented by 1, and the interrupt returns. If not, go to step B51: B51. If capture_T - 2 <= capture_value <= capture_T + 2, then: a. If in_half_T = 1, then debug_error = 1, and the interrupt returns; b. If in_half_T = 0, then capture_data[count_int] = io_value, the number of bits of the received data is incremented by 1, and the interrupt returns; B52. If capture_T / 2 - 2 <= capture_value <= capture_T / 2 + 2, then: a. If in_half_T = 0, then in_half_T = 1, and the interrupt returns; b. If in_half_T = 1, then capture_data[count_int] = io_value, the number of bits of the received data is incremented by 1, and in_half_T = 0 and the interrupt returns; B53. If the capture_value exceeds the pre-set threshold range, then debug_error = 1, and the interrupt returns; B6. The interrupt returns, and the steps in B1 are repeated when the next interrupt signal is received; After the data reception is completed, the values in the array capture_data[256] are combined to obtain the required decoded data.

2. The encoding and decoding method according to claim 1, wherein In the communication method, the communication frequency between the emulator and the microcontroller is 10k to 500kHz.

3. The encoding and decoding method according to claim 2, wherein In the communication method, the output mode and input mode of the communication pins of the emulator are controlled by a clock and a timer, including: A1: Before the controller in the emulator performs normal reading and writing on the microcontroller, the frequency of the microcontroller is measured to establish a handshake connection, and a clock signal is sent when the microcontroller is powered on or reset; A2. When the communication pin of the emulator is in the input mode, capture is performed by the timer. The timer captures the rising edge of the clock signal and calculates the duration between two adjacent edges as the clock period; A3. When the communication pin of the emulator is in the output mode, a number of "0"s are sent to the microcontroller. The number of "0"s is the same as the number of clock signals. After the microcontroller receives the "0"s, a handshake connection signal is sent to the emulator; A4. After the emulator and the microcontroller successfully handshake, the emulator reads and writes the relevant debug registers of the microcontroller. The reading and writing include writing data and reading data.

4. The encoding and decoding method according to claim 3, wherein In step A4, if the write data is performed, the emulator sends a write instruction and writes the data to be written. After the microcontroller receives the write instruction and data, it returns a first response signal; If the read data is performed, the emulator sends a read instruction. After the microcontroller receives the read instruction, it sends response data. After the emulator receives the response data, it gives a second response signal.

5. The encoding and decoding method according to claim 4, characterized in that, The microcontroller sends the response data when the communication pin of the emulator is in the output mode. The response data includes 0 or 1. The ways for the microcontroller to send 0 or 1 include: When sending 0, the communication pin outputs a high level, with a delay, then the communication pin outputs a low level, with a delay; when sending 1, the communication pin outputs a low level, with a delay, then the communication pin outputs a high level, with a delay; the length of the delay needs to ensure that the output frequency of the emulator is equal to the frequency sent by the microcontroller when establishing a handshake connection.

Citation Information

Patent Citations

  • Microcontroller single-line programming debugging interface device and debugging method

    CN107622010A

  • Single-line simulation device of microcontroller

    CN215494580U