Introduction
Arduino IDE 2 comes with debugging capability for 32-bit Arm based Arduino boards. If you try to select Arduino Uno, you may see "Debugging is not supported by 'Arduino Uno'". This tutorial will show how to adding the missing pieces to debug Arduino Uno and other ATmega328P boards.
This tutorial will demonstrate:
- How to convert a Adafruit CH552 QT Py into a debugWire debug probe/programmer combo (USBtinyISP compatible).
- Setting up Arduino IDE 2 with board support package.
- Automatically set fuses to enable debugging on ATmega328P.
- Doing debugging.
- Automatically set fuses back and restore Arduino bootloader.
Page last edited October 10, 2024
Text editor powered by tinymce.
Program CH552
First you need to make sure you have CH55xduino version 0.0.24 or higher. This version includes necessary patch for debug probe to work on 16MHz clock.
Choose "16 MHz (internal), 3.3V or 5V" in "Clock Source" and "USER CODE w/ 266B USB ram" in "USB settings".
Choose "16 MHz (internal), 3.3V or 5V" in "Clock Source" and "USER CODE w/ 266B USB ram" in "USB settings".
Compile and upload code. Hold middle button and connect CH552 board to computer, and flash the firmware. More info for this setup can be found in Adafruit CH552 QT Py tutorial.
Windows User only:
AVRDUDE tool shipped with Arduino IDE is relatively old and requires a LibUSB driver. Using Zadig to install the libusb-win32 driver.
Let's test if the Adafruit CH552 QT Py is working properly.
Switch board to "Arduino Uno".
Select "Programmer" as "USBTinyISP".
Click "Burn Bootloader".
You should see some failure like "initialization failed, rc=-1" because there isn't an Arduino connected yet. But this message indicates the CH552 respond to computer correctly.
However, if you see "Error: Could not find USBtiny device (0x1781/0xc9f)". That means the CH552 is not working properly, you need to check the compile settings, driver, or check if it is visible in device manager in your computer's OS.
Then you would connect an Arduino Uno's ISP port to CH552. The 5V and GND should be connected. The Reset of Arduino should be connected to A0(P1.1) of CH552. And SPI lines (SCK, MISO, MOSI) should be connected to corresponding pins on CH552.
After connecting your Arduino. If every wire is connected properly. The power LED on Arduino should be on. And if you burn bootloader once more, you should see "avrdude done. Thank you." that shows a successful burn.
Congratulations! Now you have a fully functional debugWire debug probe/programmer combo (USBtinyISP compatible).
You may also use the probe/programmer combo for other AVR chip just like a USBtinyISP.
Page last edited October 10, 2024
Text editor powered by tinymce.
Connect Arduino
Connect the Arduino the same way as we test the Arduino.
The 5V and GND should be connected. The Reset of Arduino should be connected to A0(P1.1) of CH552. And SPI lines (SCK, MISO, MOSI) should be connected to corresponding pins on CH552.
In Arduino IDE2, Add board "Arduino 8-bit with Debug" by,
Clicking "File"->"Preferences"
Click the button on the right side of Additional boards manager URLs
In the text box, add a new row of the link in the next text box. A long URL seem breaks the layout of this page.
In board manager, search "debug" and install the board "Arduino 8-bit with Debug"
URL here:
https://raw.githubusercontent.com/DeqingSun/unoDebugTestPackage/main/package_uno_debug_index.json
Now open the code you'd like to debug. Select board "Arduino Uno with dwire-debug". The port does not matter as we are not using it.
"Verify" the code to compile it.
Click the left of the line number to add a breakpoint. Then click "Debug".
After a few seconds, the code will pause at the breakpoint. You may add othe breakpoint, view data value, modify data value, stepping, etc. Just like a regular debugger.
You may stop by click the stop button.
When you are done with debugging. You can flash the bootloader back and solder the "RESET-EN" back.
"Power cycle" the Arduino by unplug it and plug it back to your computer.
Try to burn the bootloader twice. The 1st try will fail as expect.
The 2nd try will be successful.
Page last edited October 10, 2024
Text editor powered by tinymce.
How does everything works
How does Arduino IDE do the debug?
On the computer, we are running the IDE, debugger and debugger server. The IDE will interact with user, while debugger translates between c codes and machine codes. And the debugger server acts as a bridge between the debugger and debug hardware. On the hardware side, the debug probe will act as a bridge and connects the debug target and the debug server.
In Arduino IDE case, it uses a plug-in "Cortex Debug" from Marus to do debugging. This plug-in is supposed to debug a Arm Cortex microcontroller with GDB and OPENOCD. 8-bit AVR chip does not have OPENOCD and we have a modified version of dwire-debug to mimic an OPENOCD to make the full chain work.
When you do some debugging operation in Arduino IDE, for example adding a break point, Arduino IDE will tell GDB to add a breakpoint at a line in C code, GDB will figure out which memory address the breakpoint should be, dwire will convert it to debugwire commands, and the CH552 will send the signals to a ATmegs328P to add such a breakpoint.
Why official Arduino Uno board does not support debug while "Arduino Uno with dwire-debug" support debug?
The "Arduino Uno with dwire-debug" is using the same compiler and board support files to the official Arduino Uno. However a Arduino Uno board does not come with debug probe, and the official Arduino Uno board package does not come with a debug server.
The CH552 board act as a debug probe, and the "Arduino Uno with dwire-debug" package includes a debug server, that why we can do debugging with these components.
How debug works in signal level?
The debug-wire is a single wire half-duplex serial protocol. It runs on 128 division of the CPU speed. So for a 16Mhz Arduino, the communication will be 125K baud rate.
Before communication, the probe can pull the signal down for some time to send a break signal. After the break signal the ATmega328P will respond a 0x55 signal for probe to measure the baudrate.
After that, the probe can send command in serial and the ATmega328P will respond. For example, the probe send "0xF3" to read the chip signature, and the ATmega328P respond "0x95, 0x0F" that indicate itself is a ATmega328P.
There is no official release of the protocol and all works are based on reverse engineering. More info can be found on https://www.ruemohr.org/docs/debugwire.html
Page last edited October 10, 2024
Text editor powered by tinymce.