Adding support for ATmega328PB to AVR-GCC and AVRDude

AVR-GCC 10 only has partial support for the ATmega328PB microcontroller. For some reason, it does have a “device spec” file but misses other essential files. In this post I’ll explain how to rectify that.

Note that the ATmega328PB was introduced in 2016, and GCC 10.3 was released in 2021. Not sure why GCC does have support for the ATmega328P but not its improved (and 5 years old already) brother ATmega328PB.

I’m developing most of my microcontroller code on Windows, but with some GNU-style tools (like Git-Bash), hence the mixture of forward and backward slashes in paths. All of this should be independent of which platform you actually develop on, though.

Update 2021-05-04: This approach also works for the ATmega168PB microcontroller.


Update 2021-05-09: I zipped the files together into atmega-168pb-328pb-for-avr-gcc-10.1.0.zip. Extract that to the root of your GCC install. After that, you only need to edit avr\include\avr\io.h as described below in the 2nd step.


The problem

# From the C:\avr-gcc-10.1.0-x64-windows directory:
$ find -name '*328p*'
./avr/include/avr/iom328p.h
./avr/lib/avr5/crtatmega328p.o
./avr/lib/avr5/libatmega328p.a
./lib/gcc/avr/10.1.0/device-specs/specs-atmega328p
./lib/gcc/avr/10.1.0/device-specs/specs-atmega328pb

As you can see, there are a bunch of files for the ATmega328P that are missing for the ATmega328PB.

Fixing AVR-GCC

Fixing GCC requires two steps: copying some files, and patching a header file.

1. Copying the files

You can get the files from the ATmega DFP support pack from Microchip’s website, or from your install of Microchip Studio.

From the ATmega DFP Support Pack

  1. Download the ATmega DFP support pack
  2. Unzip the relevant files: unzip -j Atmel.ATmega_DFP.x.y.zzz.atpack
    gcc/dev/atmega328pb/avr5/crtatmega328pb.o
    gcc/dev/atmega328pb/avr5/libatmega328pb.a
    include/avr/iom328pb.h
  3. Place the .o and .a files C:\avr-gcc-10.1.0-x64-windows\avr\lib\avr5\ (so next to their 328P counterparts).
  4. Place the .h file in C:\avr-gcc-10.1.0-x64-windows\avr\include\avr\
  5. Continue with the ‘Common Last Step’ below

From Microchip Studio

Use this if you have Microchip Studio with the ATmega DFP support pack already installed.

  1. Find the DSP at C:\Program Files\Microchip\xc8\vX.YY\dfp\xc8\avr
  2. From lib\avr5, copy crtatmega328pb.o and libatmega328pb.a to C:\avr-gcc-10.1.0-x64-windows\avr\lib\avr5\.
  3. Copy include\avr\iom328pb.h to C:\avr-gcc-10.1.0-x64-windows\avr\include\avr\
  4. Continue with the ‘Common Last Step’ below

2. Patching io.h

Add the following to C:\avr-gcc-10.1.0-x64-windows\avr\include\avr\io.h just underneath the part that handles the ATmega328P.

#elif defined (__AVR_ATmega328PB__)
#  include <avr/iom328pb.h>

You should now be able to use avr-gcc -mmcu=atmega328pb and avr-g++ -mmcu=atmega328pb to compile C and C++ sources for the ATmega328PB chip.

Fixing AVRDude

To actually program the ATmega328PB with AVRDude, you need to update its configuration avrdude.conf. You can find the file in c:\avr-gcc-10.1.0-x64-windows\bin\avrdude.conf or in /etc/avrdude.conf on Linux.

Add this below the ATmega328P definition:

part parent "m328"
    id          = "m328pb";
    desc        = "ATmega328PB";
    signature   = 0x1e 0x95 0x16;

    ocdrev      = 1;
;

Setting up Microchip Studio

To configure Microchip Studio to use this new GCC, you have to add a Toolchain Flavour:

  1. Go to the project properties, “Advanced” tab, and there follow the link to the preferences.
  2. Add a new flavour for both the Atmel AVR 8-bit (C Language) and Atmel AVR 8-bit (CPP language) toolchains. Name it whatever you want, and point it to the bin directory of your GCC install.
  3. Press OK, and select the new toolchain flavour from the drop-down select box.

Credits

This post was adapted from Gonçalo Ribeiro’s gist. I decided to change some things around to match my own development environment. Also having it as part of my blog makes it easier to find for whenever I need to repeat this trick.

dr. Sybren A. Stüvel
dr. Sybren A. Stüvel
Open Source software developer, photographer, drummer, and electronics tinkerer

Related