2015-10-24 20:03:30 +02:00
# Arduino (Nano) with ATmega328P programming script
# detailed configuration on http://arduino.cc/en/uploads/Main/boards.txt
# required packages: avrdude gcc-avr avr-libc
# the output firmware name
TARGET = firmware
# the target (arduino nano)
DEVICE = atmega328p
F_CPU = 16000000UL
# the flasher
PROGRAMMER = arduino
PORT = /dev/ttyUSB0
2016-01-27 23:23:55 +01:00
# flash using the bootloader pre-programmed on the arduino
# in this case we first require the optiboot bootloader to be flashed (which supports a higher baurate of 115200bps instead of the 57600bps of the default arduino bootloader)
# reset the arduino device to start the bootloader
2015-11-08 18:49:49 +01:00
FLASHER = avrdude -p $( DEVICE) -c $( PROGRAMMER) -P $( PORT) -b 115200 -D
2015-10-24 20:03:30 +02:00
# compiler executables
CC = avr-gcc
OBJDUMP = avr-objdump
OBJCOPY = avr-objcopy
SIZE = avr-size
# library directories, compiler, and linker flags
2015-11-10 12:27:12 +01:00
LIBS = lib aes
2015-11-09 12:41:36 +01:00
CFLAGS = -g -Wall -Werror -Os -mcall-prologues -std= c99
2015-10-24 20:03:30 +02:00
CFLAGS += -I. $( patsubst %,-I%,$( LIBS) )
CFLAGS += -mmcu= $( DEVICE) -DF_CPU= $( F_CPU)
2015-11-09 12:41:36 +01:00
AFLAGS = -Wall -Werror -x assembler-with-cpp
AFLAGS += -I. $( patsubst %,-I%,$( LIBS) )
AFLAGS += -mmcu= $( DEVICE)
2015-10-24 20:03:30 +02:00
LDFLAGS = -Wl,-Map= $( TARGET) .map,--cref
LDFLAGS += -I. $( patsubst %,-I%,$( LIBS) )
LDFLAGS += -mmcu= $( DEVICE)
# floating point printf version (requires -lm below)
2015-11-10 12:27:12 +01:00
LDFLAGS += -Wl,-u,vfprintf -lprintf_flt
2015-10-24 20:03:30 +02:00
# math library
2015-11-10 12:27:12 +01:00
LDFLAGS += -lm
2015-10-24 20:03:30 +02:00
# source files to compile
2015-11-09 12:41:36 +01:00
CSRC = $( wildcard *.c) $( foreach LIB,$( LIBS) ,$( wildcard $( LIB) /*.c) )
ASRC = $( wildcard *.S) $( foreach LIB,$( LIBS) ,$( wildcard $( LIB) /*.S) )
2015-10-24 20:03:30 +02:00
# header files
HEADER = $( SRC:.c= .h)
# object files
2015-11-09 12:41:36 +01:00
OBJ = $( patsubst %.c,%.o,$( CSRC) ) $( patsubst %.S,%.o,$( ASRC) )
# listing files
LST = $( patsubst %.c,%.lst,$( CSRC) ) $( patsubst %.S,%.lst,$( ASRC) )
$( info $ ( OBJ ) )
2015-10-24 20:03:30 +02:00
all : compile flash
$( info EEPROM has to be programmed separately)
debug : CFLAGS += -DDEBUG
debug : map lst all
# reset board by setting DTR
2016-01-27 23:23:55 +01:00
# the capacitor on DTR will create a pulse on RESET
2015-10-24 20:03:30 +02:00
# after reset the bootloader is start
# the bootloader can be used to reflash the device
reset :
2015-11-08 18:49:49 +01:00
stty 115200 raw ignbrk hup < $( PORT)
2015-10-24 20:03:30 +02:00
# flash the device using the internal bootloader
flash : $( TARGET ) .hex reset
$( FLASHER) -U flash:w:$<:i
2015-11-08 18:49:49 +01:00
# write EEPROM on the device (optiboot has EEPROM capabilities disabled by default)
2015-10-24 20:03:30 +02:00
eeprom : $( TARGET ) _eeprom .hex reset
$( FLASHER) -U eeprom:w:$<:i
2015-11-08 18:49:49 +01:00
# write bootloader (optiboot) and fuses
bootloader.hex :
wget -O $@ https://github.com/Optiboot/optiboot/raw/master/optiboot/bootloaders/optiboot/optiboot_atmega328.hex
bootloader : bootloader .hex
avrdude -p $( DEVICE) -c usbtiny -U lfuse:w:0xff:m -U hfuse:w:0xda:m -U efuse:w:0x05:m -U flash:w:$<:i
2015-10-24 20:03:30 +02:00
# create main target firmware
compile : $( TARGET ) .elf
$( SIZE) --format= avr --mcu= $( DEVICE) $( TARGET) .elf
# C + ASM file
lst : $( TARGET ) .lst
# contains global and static variables
map : $( TARGET ) .map
# compile source files
2015-11-09 12:41:36 +01:00
%.o : %.c
2015-10-24 20:03:30 +02:00
$( CC) $( CFLAGS) -c -o $@ $<
2015-11-09 12:41:36 +01:00
%.o : %.S
$( CC) $( AFLAGS) -c -o $@ $<
2015-10-24 20:03:30 +02:00
# link compiled files
%.elf : $( OBJ )
2015-11-09 12:41:36 +01:00
$( info elf)
2015-10-24 20:03:30 +02:00
$( CC) $( LDFLAGS) -o $@ $^
$(TARGET).map : $( OBJ )
$( CC) $( LDFLAGS) -Wl,-Map= $@ ,--cref -o /dev/null $^
# create extended listing for additional information
%.lst : %.elf
$( OBJDUMP) --section-headers --source $< > $@
# create flashable ihex from firmware
%.hex : %.elf
$( OBJCOPY) --only-section .text --only-section .data --output-target ihex $< $@
%_eeprom.hex : %.elf
$( OBJCOPY) --only-section .eeprom --change-section-lma .eeprom= 0 --output-target ihex $< $@
clean :
rm -f $( TARGET) $( TARGET) .hex $( TARGET) _eeprom.hex $( TARGET) .elf $( TARGET) .lst $( TARGET) .map $( LST) $( OBJ)