devel/avr-gcc
devel/avr-binutils
devel/avrdude
They can be installed via binary package management
pkg install devel/avr-gcc
pkg install devel/avr-binutils
pkg install devel/avrdude
or from ports
cd /usr/ports/devel/avr-gcc
make install clean
cd /usr/ports/devel/avr-binutils
make install clean
cd /usr/ports/devel/avrdude
make install clean
This works pretty straight forward. First compile and link as usual, then use objcopy to extract the eeprom section into an Intel HEX file and after that flash using avrdude. If one wants to compile - for example - blink.c
and upload it to an ATMega328P (i.e. for example an Arduino pro mini) using the Arduino bootloader on Port /dev/ttyU0 (check permissions if something doesn't work and possibly set them in /etc/devfs.conf):
avr-gcc -Wall -mmcu=atmega328p -Os blink.c -Wl,-gc-sections -Wl,-relax -o blink.o
avr-objcopy -R .eeprom -O ihex blink.o blink.hex
avrdude -v -p atmega328p -c arduino -P /dev/ttyU0 -b 57600 -D -U flash:w:blink.hex:i
This can of crouse be done parameterized with a Makefile:
MCU=atmega328p
CFLAGS=-Wall -mmcu=$(MCU) -Os
LDFLAGS=-Wl,-gc-sections -Wl,-relax
CC=avr-gcc
all: blink.hex
clean:
rm -f *.o *.hex *.obj *.hex
%.hex: %.obj
avr-objcopy -R .eeprom -O ihex $< $@
%obj: blink.c
$(CC) $(CFLAGS) blink.c $(LDFLAGS) -o $@
flash: blink.hex
avrdude -v -p $(MCU) -c arduino -P /dev/ttyU0 -b 57600 -D -U flash:w:blink.hex:i
.PHONY: flash clean
Note that different boards use different chips and different BAUD rates:
Board | Chip | Baud |
---|---|---|
Arduino Pro Mini | atmega328p | 57600 |
This is a simple "blink" test program that blinks with a requency of 1 Hz and a duty cycle of 50%
#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
int main() {
DDRB = DDRB | 0x20;
for(;;) {
PORTB = PORTB | 0x20;
_delay_ms(500);
PORTB = PORTB & ~0x20;
_delay_ms(500);
}
}