Wellys Dev

  • Home
  • Search/Topics
  • Writings
  • About

    2021

  • 2021-12-30
    Developing in C for the ATmega328: Hints on Using Bloom and avr-gdb

    Where I provide hints on using avr-gdb and bloom to debug code on the ATmega328P.

    Sources

    • gdb Resources
    • Bloom (replaces avarice)
    • Microchip SNAP (replaces the Atmel Dragon)
    • Atmel-ICE Debugger
    • GDB Documentation
    • Guide to Faster, Less Frustrating Debugging - Norman Matloff

    Hints

    1. 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.
    2. Be sure to use target remote:1442 in gdb, if you are having connection issues.
    3. 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.
    4. 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!
    5. Another great tutorial is Beej’s Quick Guide to GDB, particularly for the gdb –tui guidance.
    6. 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.
    7. 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.
    8. 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.
    9. 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.
    10. 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.
    11. 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.
    12. 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.”
  • 2021-12-30
    Developing in C for the ATmega328: Examples of Bloom with gdb to Debug

    Where I show some examples as to how to use Bloom and avr-gdb to debug code on the ATmega328P.

    Sources

    • gdb Resources
    • gdb Hints (from RP2040 effort)
    • Bloom (replaces avarice)
    • Microchip SNAP (replaces the Atmel Dragon)

    Introduction

    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.

  • 2021-12-24
    Developing in C for the ATmega328: Setup Bloom and gdb for Hardware Debug

    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.

    Sources

    • gdb Resources
    • gdb Hints (from RP2040 effort)
    • Bloom (replaces avarice)
    • Microchip SNAP (replaces the Atmel Dragon)
    • Atmel-ICE Debugger more expensive version for both ARM and AVR

    Introduction

    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.

  • 2021-12-20
    Hardware Debug: Using gdb to Debug

    Where I illustrate debugging code for the ATmega328P using the Atmel Dragon, avr-gdb and avarice. This is a overview of a hardware debugger approach, for a more detailed view on Linux using Bloom, follow this guide.

    Update (Feb 2022): I also recommend reviewing this tutorial as it is quite detailed and offers another method for using gdb. While it does use PlatformIO and VS Code, I believe its helpful to review the author’s approach to using gdb.

  • 2021-12-15
    Developing in C for the ATmega328P: Using Windows, Advanced

    Where I setup the advanced, Standard C tool chain (avr-gcc) for the ATmega328P on Windows.

    If you are looking to install the AVR toolchain for C using Windows Subsystem for Linux, don’t bother.

    Background

    Note: This tutorial hasn’t been strictly tested by me on Windows 11 system, that said, it has been performed on numerous other Windows 11 systems without issues.

    Windows has two different operating environments, Windows (the Graphical User Interface or GUI) and the command line interface (CLI). The former is the typical method of using Windows, particularly when using applications such as Word, Excel and Powerpoint. The latter is used for program automation and Windows program development and it is typically entered by typing cmd in the Windows search bar. We’ll introduce a new and improved method, shortly.

  • 2021-12-07
    Developing in C for the ATmega328: Multitasking

    Where I illustrate developing multitasking code for the ATmega328P using a new Standard C framework.

    Sources

    • A Multitasking Kernal in One Line of code Simplistic, yet powerful approach to multitasking. examples/oneline
    • AVR Scheduler Nicely documented Round Robin Scheduler. examples/RR_Scheduler
    • Preemptive Multitasking for the AVR Simplified OS which provides multitasking. examples/RIOS
    • Writing a Preemptive Task Scheduler for AVR
    • avr-libc Standard AVR C Library
    • Github: Programming the Arduino Uno in C
    • Multi-tasking the Arduino - Part 1

    Introduction

    The value of using multitasking on an embedded microcontroller, is that it allows you to do “multiple things at once”, hence the term multitasking. It also allows you to separate specific tasks into functions. Then debug each function separately to ensure your board works as bug free as possible.

  • 2021-12-07
    Developing in C for the ATmega328: Using the Standard C Framework

    Where I illustrate developing code for the ATmega328P using the new Standard C framework.

    Sources

    • Github: Programming the Arduino Uno in C
    • Microchip AVR ATmega328 Go to this page for the datasheet, you will need it.
    • Definitive UNO pinout
    • avr-libc Standard AVR C Library

    Introduction

    The Github repository referenced above is a new Standard C framework which mimics a subset of the Arduino framework. [Note: Standard C refers to using a standardized version of C, in this case, avr-gcc (ANSI C) as compared to Arduino C++, which is a combination of C, C++ and Arduino classes and templates. Many universities have an expectation of their students to understand C in the context of ANSI C.] The intent of the repository is to provide the capability for someone to use Standard C in programming an Arduino Uno (or similar type of microcontroller.)

  • 2021-11-20
    DEPRECATED: Developing in C for the ATmega328P: Windows Setup Using WSL1

    DEPRECATED: Due to Microsoft’s inability to enable serial ports in WSL 2, attempting to perform embedded development in WSL is futile. I strongly recommend Linux, macOS or Windows, instead.

    Where I setup the Standard C tool chain (avr-gcc) for the ATmega328P on Windows 10 using Windows Subsystem for Linux (WSL1).

    If you are looking to install the AVR toolchain for C using Windows 10 (and not WSL), go here.

    Background

    Windows has two different operating environments, Windows (the Graphical User Interface or GUI) and the command line, otherwise known as the command prompt. The former is the typical method of using Windows and the latter is used for program automation and Windows program development. It is entered by typing cmd in the Windows search bar. It is this type of command line environment which we want to use to develop C software for the Uno. For example, the GCC compiler for C is intended for use in a command line environment, however, the environment is expected to Linux and not Windows command prompt.

  • 2021-11-20
    Developing in C for the ATmega328P: macOS Setup

    Where I setup the Standard C toolchain (avr-gcc) for the ATmega328P on macOS using homebrew.

    Test with the Arduino IDE!

    After performing these steps on multiple machines, I’ve found it best to install and test using the Arduino IDE before going forward with the installation instructions below. This will reduce the errors to something more manageable and having the Arduino IDE is handy for its Serial Monitor as well it provides an easy method to which port the Uno is connected.

  • 2021-11-20
    Developing in C for the ATmega328P: Linux Setup

    Where I setup the Standard C toolchain for the ATmega328P for Linux, with specific emphasis on the Raspberry Pi.

    Test with the Arduino IDE!

    Note: If you have a GUI interface to your Linux PC, by all means, install the Arduino Legacy IDE (1.8.19). In the steps regarding the Raspberry Pi, the interface will be the terminal and can’t use the Arduino IDE GUI. After performing these steps on multiple machines, I’ve found it best to install and test using the Arduino IDE before going forward with the installation instructions below. This will reduce the errors to something more manageable and having the Arduino IDE is handy for its Serial Monitor as well it provides an easy method to which port the Uno is connected.

Page 7 of 8
Copyright © 2025 Lief Koepsel
  • Home
  • Search/Topics
  • Writings
  • About