set history save on
set history size 10000
set history filename ~/.gdb_history
file main.elf
target remote :1442
set listsize 0
define cll
make
load main.elf
mon reset
refresh
list
end
Servos are a powerful addition to the embedded programming toolkit as they enable motion. Not high-speed motion as in a electric motor, however, motion which can be easily controlled and typically in an arc or as angles. Servos are much more precise as to how they can move in comparison to electric motors. See Sources above for a few example tutorials as to how to use them.
Check the version number on avr-gdb as of December 2021, it is 11.1. On Ubuntu, the latest version is 8.1 from 2018 which had quite a few bugs. See notes on this page as to how to ensure you have the latest version.
Be sure to use target remote:1442 in gdb, if you are having connection issues.
If you find your self executing a lengthy command quite a bit (such as “monitor reset init”), use “define” to create a shorter command for it. If you put it into the .gdbinit file (as I have), it will remember from session to session.
gdb can be incredibly easy to use. Norman Matloff’s tutorial is great for illustrating the commands needed. I recommend reading and practicing with it, its well worth it!
Start with using gdb to replace printf statements. Run code, have it stop by using a “breakpoint function name” or “breakpoint line number”, then use “print” or “display” to get the values of variables. This will work the same as inserting print statements, however, it doesn’t affect your code.
It took me less time to learn gdb, and write these two tutorials than the time I spent attempting to debug Visual Studio Code. Your mileage may vary, however, a simple gdb/code editor setup is fast and easy to learn and use.
If your code ends, you will see “exit(status=0)….”. To restart the program, simply issue a monitor reset, which will reset the processor. Press “c” and the program will begin running again.
gdb operates in at least two modes:
command, indicated by (gdb) prompt. This is where you will enter all of the commands to interact with gdb
execution, indicated by a lack of a (gdb) prompt. This means the program is running and hitting a breakpoint or watchpoint will return you back to command mode. Or you can hit Ctrl-C which will interrupt the process and put into command mode.
Enter the command layout src or start by using gdb –tui and the window will divide into two horizontal windows. The top window will show 20 lines of source and the bottom window will continue to be the command window. This is a great way to view a breakpoint. For example, set a breakpoint on a function name, then “n(ext)” through the code to view execution. Once you have entered “n”, you can simply hit the return key and it will advance using the next command.
If in tui, use info win to show the number of lines per window then use wh [window name] n to change the number of lines to n. For example, use wh src 40 to change the window showing the source code to 40 lines. Use fs [window name] to change the window focus. Typically, it is in the source (src) window, and to change the focus to the command window, use fs cmd.
Note found elsewhere, I’ll link to it when I find it: “remember to use -g only for the image you pass to gdb, not the firmware you load into the device (if it can grok elf).
Also another very critical prerequisite for C programs is -O0 (which is usually default). But if you use -Os or something, you can get very unpredictable debugging results.
This btw. makes debugging some code on avrs for example almost impossible, as they often rely on -Os to fit the image in there.
But.. it is very possible and comfy to debug simple optimized C programs in assembly, if you know roughly what to expect.
Use objdump -dS on the optimized .elf file with debugging info and you’ll roughly see what assembly code was produced for what C code.”
In a previous entry, I discussed how to setup Bloom and gdb using an MPLAB Snap. Given the power of Bloom’s feature called Insight, I want to present some examples of how to use Insight to debug the AVR8 family of microcontrollers.
Where I illustrate how to setup using avr-gdb to debug code on the ATmega328P and replacing avarice with Bloom and the Atmel Dragon with the Microchip Snap.
In a previous entry, I discussed using an Atmel Dragon (no longer available), avarice (an older application managing communication between the Dragon and gdb), and avr-gdb to debug a program running on a Uno (ATmega328P). My goal has been to find a suitable board which replaces the Dragon, however, at a reasonable (<$50) price point. The board would need to work with avarice or I need to find a replacement for avarice as well. This new combination needs to provide both the ability to debug as well as program the 328P.