2020/11/24

Building for new ATmega 0-series chips on Debian

I previously wrote about building code for the Microchip ATtiny 1-series chips on Debian machines. It requires copying some files around into places the host OS can find them.

I've recently been looking at the equally new ATmega 0-series, and it seems to require similar steps to find those files.

Start off by downloading the "Atmel ATmega Series Device Support" file from http://packs.download.atmel.com/. This is a free and open download, licensed under Apache v2. This file carries the extension atpack but it's actually just a ZIP file:

$ file Atmel.ATmega_DFP.1.6.364.atpack
Atmel.ATmega_DFP.1.6.364.atpack: Zip archive data, at least v1.0 to extract

Note that by default it'll unpack into the working directory, so you'll want to create a temporary folder to work in:

$ mkdir pack

$ cd pack/

$ unzip ~/Atmel.ATmega_DFP.1.6.364.atpack 
Archive:  /home/leo/Atmel.ATmega_DFP.1.6.364.atpack
   creating: atdf/
   creating: avrasm/
   creating: avrasm/inc/
...

From here, you can now copy the relevant files out to where avr-gcc will find them. Since this time we also have to supply avr-gcc with some device spec files, we'll have to find out where it wants to keep them:

$ dpkg -S specs-attiny814
gcc-avr: /usr/lib/gcc/avr/5.4.0/device-specs/specs-attiny814

This now gives us the path to copy them into:

$ sudo cp gcc/dev/atmega*0[89]/device-specs/specs-atmega* /usr/lib/gcc/avr/5.4.0/device-specs/
$ sudo cp include/avr/iom*0[89].h /usr/lib/avr/include/avr/
$ sudo cp gcc/dev/atmega*0[89]/avrxmega3/*.{o,a} /usr/lib/avr/lib/avrxmega3/

Having done this we find we can now compile firmware for these new chips:

$ avr-gcc -std=gnu99 -Wall -Os -DF_CPU=20000000 -mmcu=atmega3208 -flto -ffunction-sections -fshort-enums -o .build/firmware.elf src/main.c
$ avr-size .build/firmware.elf
   text    data     bss     dec     hex filename
   4087      24      11    4122    101a .build/firmware.elf
$ avr-objcopy -j .text -j .rodata -j .data -O ihex .build/firmware.elf firmware-flash.hex