Created
February 13, 2016 21:41
-
-
Save pilinux/c0dd034b914af2473214 to your computer and use it in GitHub Desktop.
Common Makefile for all avr C projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##########------------------------------------------------------########## | |
########## Project-specific Details ########## | |
########## Check these every time you start a new project ########## | |
########## or when you change your MCU or USB PORT ########## | |
##########------------------------------------------------------########## | |
MCU = atmega8 ## Name of your microcontroller device. | |
F_CPU = 8000000UL ## Frequency | |
BAUD = 19200 ## BAUD rate | |
PORT = /dev/ttyUSB0 ## USB PORT of your computer | |
##########------------------------------------------------------########## | |
########## Programmer Defaults ########## | |
########## Set up once, then forget about it ########## | |
##########------------------------------------------------------########## | |
PROGRAMMER_TYPE = avr910 | |
PROGRAMMER_ARGS = -b $(BAUD) -P $(PORT) | |
# extra arguments to avrdude: baud rate, chip type, -F flag, etc. | |
##########------------------------------------------------------########## | |
########## Program Locations ########## | |
########## Won't need to change if they're in your PATH ########## | |
##########------------------------------------------------------########## | |
CC = avr-gcc | |
OBJCOPY = avr-objcopy | |
OBJDUMP = avr-objdump | |
AVRSIZE = avr-size | |
AVRDUDE = avrdude | |
##########------------------------------------------------------########## | |
########## Makefile Magic! ########## | |
########## Summary: ########## | |
########## We want a .hex file ########## | |
########## Compile source files into .elf ########## | |
########## Convert .elf file into .hex ########## | |
########## You shouldn't need to edit below. ########## | |
##########------------------------------------------------------########## | |
## The name of your project (without the .c) | |
# TARGET = name it automatically after the enclosing directory | |
TARGET = $(lastword $(subst /, ,$(CURDIR))) | |
# Object files: will find all .c/.h files in current directory. | |
# If you have any other (sub-)directories with code, | |
# you can add them in to SOURCES below in the wildcard statement. | |
SOURCES=$(wildcard *.c) | |
OBJECTS=$(SOURCES:.c=.o) | |
HEADERS=$(SOURCES:.c=.h) | |
## Compilation options, type man avr-gcc if you're curious. | |
CPPFLAGS = -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) | |
CFLAGS = -Os -g -std=gnu99 -Wall | |
## Use short (8-bit) data types | |
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums | |
## Splits up object files per function | |
CFLAGS += -ffunction-sections -fdata-sections | |
LDFLAGS = -Wl,-Map,$(TARGET).map | |
## Optional, but often ends up with smaller code | |
LDFLAGS += -Wl,--gc-sections | |
## Relax shrinks code even more, but makes disassembly messy | |
## LDFLAGS += -Wl,--relax | |
## LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf | |
## LDFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf | |
TARGET_ARCH = -mmcu=$(MCU) | |
## Explicit pattern rules: | |
## To make .o files from .c files | |
%.o: %.c $(HEADERS) Makefile | |
$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<; | |
$(TARGET).elf: $(OBJECTS) | |
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@ | |
%.hex: %.elf | |
$(OBJCOPY) -j .text -j .data -O ihex $< $@ | |
%.eeprom: %.elf | |
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ | |
%.lst: %.elf | |
$(OBJDUMP) -S $< > $@ | |
## These targets don't have files named after them | |
.PHONY: all disassemble disasm eeprom size clean squeaky_clean flash fuses | |
all: $(TARGET).hex | |
debug: | |
@echo | |
@echo "Source files:" $(SOURCES) | |
@echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) | |
@echo | |
# Optionally create listing file from .elf | |
# This creates approximate assembly-language equivalent of your code. | |
# Useful for debugging time-sensitive bits, | |
# or making sure the compiler does what you want. | |
disassemble: $(TARGET).lst | |
disasm: disassemble | |
# Optionally show how big the resulting program is | |
size: $(TARGET).elf | |
$(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf | |
clean: | |
rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ | |
$(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ | |
$(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ | |
$(TARGET).eeprom | |
squeaky_clean: | |
rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ *.eeprom | |
##########------------------------------------------------------########## | |
########## Programmer-specific details ########## | |
########## Flashing code to AVR using avrdude ########## | |
##########------------------------------------------------------########## | |
flash: $(TARGET).hex | |
$(AVRDUDE) -x no_blockmode -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< | |
## An alias | |
program: flash | |
flash_eeprom: $(TARGET).eeprom | |
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< | |
avrdude_terminal: | |
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment