Debuggin Arduino Mega 2560 with JTAGICE3

Whenever we want to do some debugging on our board, it is a good idea to use some debugger (like JTAGICE3, Dragon…) instead of using Serial.print() for our variables. This simply guide will allow this in only a few steps.

First, we need to activate some fuses of our board. Thus, we need to connect our debugger to ICSP header,  which can be seen on Figure 1a. Then, just connect debugger and board to your pc via USB cable (Figure 1b). The 6 pin connector it shown on Figure 2.

Figure 1 (a: Arduino board with ICSP header. b: Arduino and debugger first setup)

Figure 2: 6-pin connector (left), 10-pin connector (center)

Now, you should open Atmel Studio and click on Tools -> Device Programing. In the dialog box, select “JTAGICE3” (or your debugger) for the tool,  “ATmega2560” for the device  “SPI” for the interface. Then, in the same box, go to “Fuses”. There you have to put a check mark in the “OCDEN”, “JTAGEN”, and “BOOTRST” check boxes, and finally click “Program”.

Figure 3: configuring fuses

After this, unplug USB power from board and debugger, and disconnect both between them too. Now it is time to use a new interface for conection: JTAG. You can achieve this by using a 10-pin conector (Figure 2) and an adapter board (Figure 4a), which can be found here (https://oshpark.com/shared_projects/HtnXf6wb). You also have to make an union between squares you can see on Figure 4b. Finally we should connect all following Figure 5 setup.

 

 

 

 

 

 

Figure 4 (a: adapter board with pins, b: you have to weld these pads)

Figure 5: adapter board connected to Arduino

And now we are ready to debug! Call up the setup dialog box in Atmel Studio once again by clicking on Tools -> Device Programing. In the dialog box, select “JTAGICE3” for the tool,  “ATmega2560” for the device and now “JTAG” for the interface. Then just press apply.

For ending this tutorial, we will see how to set breakpoints and watchs. First, go to Debug -> Continue, so debugging will start. You can create a breakpoint just by right-clicking one line and click on Breakpoint -> Insert breakpoint. Then it is posible to disable or delete it, in the same way.  This tool will allow you to run the program and then automatically stop where you want. You can also debug line by line by pressing Step Over or Step into at the toolbar (Figure 6).

Figure 6: Adding a breakpoint (right) and control panel for debug (top)

And what about watchs? A watch enable us having a direct look inside the value of a variable, in real time. In this example, I have created 3 watch, each one for “posit”, “velocit” and “solar”, all of them are pointers who point at structs with 4 components… And we can know all of that values just by looking at the bottom panel (Figure 7).

Figure 7: Adding watchs. The values of struct pointed by posit are shown

References

http://www.avrfreaks.net/comment/1807511#comment-1807511

https://oshpark.com/shared_projects/HtnXf6wb

http://www.atmel.com/webdoc/jtagice3/jtagice3.getting_started_assembling.html

https://store.arduino.cc/usa/arduino-mega-2560-rev3