When you can't quite figure out what is going on with your project at run time, Debugging Tools can save you a lot of headaches, but you have to find a hardware debugger first....
GDBStub allows us to use the GDB debugging system directly on our Arduino Uno board (or other AVR target), without having to purchase and wire up a Hardware Debugger.
The only downside is it consumes our Serial interface, so we will also connect SoftwareSerial so we can keep our custom Serial print commands, alongside hardware debugging.
Hardware SetupSimply attach the USB lead to your Uno and your PC as normal.
If you don't need the additional Serial interface, you can skip the rest of this section.
Wire your Uno to your USB TTL Serial breakout board as shown below
(Rx/Tx can be changed in software so allocate to other pins if in use)
Open the project you wish to debug.
Then we can configure our Visual Micro IDE for debugging by selecting the options shown below:-
You can install the GDBStub library manually, or by using the Visual Micro Library Manager:
- vMicro > Add Library > Download and Install Arduino Library > Manage Libraries Tab
- Enter "gdb" as the Search term
- For the Uno we can use the "avr-debugger by Jan Dolinay"
- Left click to install the library (see more about libraries here)
- Click the Rescan button at the top when the install has completed
- Now close the Visual Micro Explorer
To harness the GDBStub we will need to add a few lines of code:
- Include the avr8-stub.h
- Add debug_init() to your setup() routine
- Add breakpoint() to the top of loop()
If you are not using the Serial Breakout then you can just comment out all Serial Code for now (wrap with a #define to make it easy to switch on and off).
NOTE - Visual Micro provides _DEBUG automatically if you enable the Project Setting ""Configuration Define _DEBUG" and set its value to 1. When you change to "Release" Configuration this code will deactivate.
To wrap all of your code to be compatible with SoftwareSerial and Serial we suggest you:-
- Include the Software Serial Library from vMicro > Add Library > Current Platform
- Add a #define for the Serial Object in use e.g. USE_SERIAL
- Change all references from Serial to USE_SERIAL
- Add the SoftwareSerial object with the Rx/Tx pins you wish to use
That's all that's needed, and you can switch back to Serial by changing the USE_SERIAL define now.
Setup Additional Serial MonitorSo we can view the additional SoftwareSerial port whilst debugging we will need to make it visible.
Open vMicro > General > Monitor Alternative, and select the USB Port for your TTL Serial Device, a new Serial Monitor Window should appear connected to this port, without changing your upload port.
Add a break point to your sketch by clicking in the left hand margin.
You can add more than one, and up to 4 at any one time.
Ensure the correct COM port is set for upload.
To start the debugging process, you can either:
- "Debug > Attach to Process" button if your code has already been uploaded to the board
- "Debug > Start Debugging" if your code has not been uploaded
Now you can debug in Visual Micro as normal on your Arduino Uno, without an external hardware debugger.
NOTE - you may need to re-open the alternative serial monitor again after debugging begins the first time. After this it will be remembered as part of your Visual Studio Window Layout.
The below Videos go through this process in two steps (1) GDBStub Setup, (2) Software Serial Setup (enable captions on these videos)
Useful Links
Comments
Please log in or sign up to comment.