Arduino Nano Debugging via DebugWire
Well, I have been creating a bigger project on my mega2560 boards. Debugging here is very comfortable via the JTAG interface. Everything was working fine, porting the implementation to an Arduino nano device however failed.
Unfortunately, the smaller microcontrollers do not support JTAG at all.
The obvious debugging option is using several
Serial.print commands. This is working to some extent – but you will not get any further insights. I found strange errors and I was barely capable of finding out where the program was getting stuck.
Further reading reveals that there still is an option for in-system-debugging even with the smaller devices. Atmel has introduced DebugWire, a proprietary 1-wire bus protocol.
For using DebugWire you will need a programmer like the AVR Dragon or a JTAG ICE MK2. Although I have both, I am using the Dragon as it has the prototype area having proved being useful later on!
First (failed) steps with the nano
Sounds good, but how do we use it with a Nano? – There is a general problem: In order to make programming easy for novice users, the usb chip resets the controller via a DTR line connected to RST via a 100nF capacity for getting the Arduino bootloader punching in.
Well, this is a problem for DebugWire as this protocol uses the RST line for communication which totally fails with this capacity.
Due to the Nanos being cheap, I decided to just cut off the capacity (I am using external programmers anyway) – this is a little tricky as there is no good information available which cap to remove. Taking a look at the board, it seems obvious removing the capacity next to the RST pin.
Still, I was not able to start actual debugging. Further information from Atmel suggests increasing the pull-up resistance for the RST line (it is 1k, they suggest >10k).
So I further removed the SMD resistor next to the RST line and soldered 20k between the RST and 5V PIN.
Yay, it works!
OMG, screwed up fuse settings aka ‘bricked’ device!
Usually, you use the ISP upload. Unfortunately, this must be disabled for using DebugWire. Everytime you start a debug session, the SPIEN fuse is disabled, whereas the DWEN fuse is enabled.
After this fuse change, you will not longer be able to use ISP. The ISP settings may be restored by returning from debug mode with the SPI enable option – other people reported that this might not always work properly.
As my first tries for debugging did not succeed (the SPI was disabled, DW enabled), I had no chance to communicate with the microcontroller again. Here comes the High Voltage Serial Programming (HVSP) interface into play: This interface (also supported by the Dragon board) can be used to access the fuses again – even with screwed up fuse settings.
Finding the correct pin configuration however was a little tricky, leave a comment in case this helped you saving a lot of time. Helpful is information here and the datasheet and the nano pinout schematic.
Please note that the XTAL1 (PB6) Pin is not available externally, just take a jumpwire and create an on-the-fly connection.
Some final notes: I have also removed the external oscillator as I was having some trouble before replacing the RST pull-up. This is NOT necessary. I also remove another capacity not being necessary for DebugWire operation.