Skip to content

Instantly share code, notes, and snippets.

@tspspi
Last active March 10, 2019 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tspspi/2bd6a7dcf8f09e320f0d172de6dc622c to your computer and use it in GitHub Desktop.
Save tspspi/2bd6a7dcf8f09e320f0d172de6dc622c to your computer and use it in GitHub Desktop.
How to use avr-gcc and avrdude with Arduino Pro-Mini on FreeBSD

How to use AVR-GCC and avrdude with Arduino pro-mini on FreeBSD

Required packages / ports

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

Compiling and flashing

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:

BoardChipBaud
Arduino Pro Miniatmega328p57600

A simple test program

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);
        }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment