Last active
March 13, 2017 01:06
-
-
Save scttnlsn/2287c3889a59031cf97e04b0d56ee821 to your computer and use it in GitHub Desktop.
AVR Makefile
This file contains hidden or 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
TARGET = example | |
MCU = atmega328p | |
PROG = avrisp | |
PORT = /dev/cu.*usb* | |
BAUD = 57600 | |
F_CPU = 16000000 | |
# Sources | |
LIBS = $(shell find lib/* -type d 2> /dev/null) | |
SRC = $(wildcard src/*.c) $(foreach LIB, $(LIBS), $(wildcard $(LIB)/*.c)) | |
PSRC = $(wildcard src/*.cpp) $(foreach LIB, $(LIBS), $(wildcard $(LIB)/*.cpp)) | |
OBJ = $(SRC:.c=.o) $(PSRC:.cpp=.o) | |
LST = $(SRC:.c=.lst) $(PSRC:.cpp=.lst) | |
# Compiler flags | |
CFLAGS = -D F_CPU=$(F_CPU) -g -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Iinclude $(patsubst %, -I%, $(LIBS)) | |
LDFLAGS = -lm -Wl,-Map=$(TARGET).map,--cref | |
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) -Wa,-adhlns=$(<:.c=.lst) | |
ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) -Wa,-adhlns=$(<:.cpp=.lst) | |
# Commands | |
CC = avr-gcc | |
OBJCOPY = avr-objcopy | |
OBJDUMP = avr-objdump | |
SIZE = avr-size | |
AVRDUDE = avrdude | |
REMOVE = rm -f | |
%.hex: %.elf | |
$(OBJCOPY) -O ihex -R .eeprom $< $@ | |
%.eep: %.elf | |
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $< $@ | |
%.lss: %.elf | |
$(OBJDUMP) -h -S $< > $@ | |
%.elf: $(OBJ) | |
$(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS) | |
%.o: %.c | |
$(CC) -c $(ALL_CFLAGS) $< -o $@ | |
%.o: %.cpp | |
$(CC) -c $(ALL_CXXFLAGS) $< -o $@ | |
.SECONDARY: $(OBJS) | |
all: info $(TARGET).elf $(TARGET).hex $(TARGET).eep $(TARGET).lss size | |
size: $(TARGET).elf | |
$(SIZE) -C --mcu=$(MCU) $(TARGET).elf | |
info: | |
@$(CC) --version | |
@echo | |
@echo "libs:" | |
@echo $(LIBS) | |
@echo | |
@echo "srcs:" | |
@echo $(SRC) $(PSRC) | |
@echo | |
@echo "objects:" | |
@echo $(OBJ) | |
@echo | |
flash: $(TARGET).hex $(TARGET).eep | |
$(AVRDUDE) -p $(MCU) -P $(PORT) -c $(PROG) -b $(BAUD) -U flash:w:$(TARGET).hex -vvv | |
clean: | |
$(REMOVE) $(TARGET).hex | |
$(REMOVE) $(TARGET).eep | |
$(REMOVE) $(TARGET).elf | |
$(REMOVE) $(TARGET).map | |
$(REMOVE) $(TARGET).lss | |
$(REMOVE) $(OBJ) | |
$(REMOVE) $(LST) | |
serial: | |
screen $(PORT) 9600 | |
.PHONY: all size info flash clean serial |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment