Skip to content

Instantly share code, notes, and snippets.

@blondie7575
Created December 9, 2012 22:55
Show Gist options
  • Save blondie7575/4247360 to your computer and use it in GitHub Desktop.
Save blondie7575/4247360 to your computer and use it in GitHub Desktop.
# Makefile
#
# Parts of this are borrowed from http://electrons.psychogenic.com
#
DEVICE=atmega324pa
CLOCK = 20000000UL
PROGRAMMER = usbtiny
FUSES = -U lfuse:w:0xe0:m -U hfuse:w:0xd9:m -U efuse:w:0xff:m
#############################
# Source files (use .S for assembly)
SOURCES=\
main.S
#############################
CFLAGS=-I. -g -mmcu=$(DEVICE) -Os -DF_CPU=$(CLOCK) \
-fpack-struct -fshort-enums \
-funsigned-bitfields -funsigned-char \
-Wall -Wstrict-prototypes \
-Wa,-ahlms=$(firstword \
$(filter %.lst, $(<:.c=.lst)))
ASMFLAGS =-I. -mmcu=$(DEVICE) -DF_CPU=$(CLOCK) \
-x assembler-with-cpp \
-Wa,-gstabs,-ahlms=$(firstword \
$(<:.S=.lst))
LDFLAGS=-Wl,-Map,main.out.map -mmcu=$(DEVICE)
CFILES=$(filter %.c, $(SOURCES))
ASMFILES=$(filter %.S, $(SOURCES))
OBJDEPS=$(CFILES:.c=.o) \
$(ASMFILES:.S=.o)
M4DEPS=$(ASMFILES:.S=_m4.S)
LST=$(filter %.lst, $(OBJDEPS:.o=.lst))
.SUFFIXES : .c .o .out .S .hex .ee.hex .h
.PHONY: writeflash clean stats gdbinit stats fuse
#############################
# Make targets:
# all, disasm, stats, hex, writeflash/install, clean, fuse
all: main.out
disasm: main.out
avr-objdump -d main.out
stats: main.out
avr-objdump -h main.out
avr-size main.out
hex: main.hex main.ee.hex
writeflash: hex
avrdude -c $(PROGRAMMER) -p $(DEVICE) -U flash:w:main.hex:i
install: writeflash
clean:
rm -f main.out main.out.map
rm -f $(OBJDEPS)
rm -f $(M4DEPS)
rm -f $(LST)
rm -f main.hex main.ee.hex main.elf
fuse:
avrdude -c $(PROGRAMMER) -p $(DEVICE) $(FUSES)
main.out: $(M4DEPS)
avr-gcc $(LDFLAGS) -o main.out $(M4DEPS)
#############################
# M4 Preprocessing
%_m4.S: %.S
m4 < $< > $@
#############################
# Generating object files
#.S.o :
# avr-gcc $(ASMFLAGS) -c $< -o $@
#############################
# Generating hex files
# hex files from elf
.out.hex:
avr-objcopy -j .text -j .data -O ihex $< $@
.out.ee.hex:
avr-objcopy -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment