micromenu/avr.Makefile
2018-10-19 13:40:49 +02:00

211 lines
4.9 KiB
Makefile

#==== Main Options =============================================================
MCU = atmega328p
F_CPU = 16000000
LFUSE = 0xf1
HFUSE = 0xdf
TARGET = main
SRC = col.cpp hal.cpp main.cpp
SRCDIR = src
INCDIR = inc
OBJDIR = build/avr
BACKUPDIR = backup
#==== Compile Options ==========================================================
OPT = s
CPU = -mmcu=$(MCU) -I.
CPU += -DF_CPU=$(F_CPU)UL
CPU += -O$(OPT)
#CFLAGS += -mint8
#CFLAGS += -mshort-calls
CFLAGS += -funsigned-char
CFLAGS += -funsigned-bitfields
CFLAGS += -fpack-struct
CFLAGS += -fshort-enums
#CFLAGS += -fno-unit-at-a-time
CFLAGS += -Wall
CFLAGS += -Wstrict-prototypes
CFLAGS += -Wundef
#CFLAGS += -Wunreachable-code
#CFLAGS += -Wsign-compare
#CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
#CFLAGS += -flto
CFLAGS += -std=gnu99
CXXFLAGS += -std=c++11
CFLAGS += -I$(INCDIR) -DAVR
CXXFLAGS += -I$(INCDIR) -DAVR
#LDFLAGS =
#==== Programming Options (avrdude) ============================================
AVRDUDE_PROGRAMMER = arduino
AVRDUDE_PORT = /dev/ttyS4
AVRDUDE_BAUD = 19200
#AVRDUDE_NO_VERIFY = -V
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -b $(AVRDUDE_BAUD) -c $(AVRDUDE_PROGRAMMER) $(AVRDUDE_NO_VERIFY)
#==== Targets ==================================================================
CC = avr-gcc
CXX = avr-g++
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRDUDE = avrdude
AVRSIZE = avr-size
REMOVE = rm -f
REMOVEDIR = rm -rf
TAIL = tail
AWK = awk
SRC_PATH = $(SRC:%=$(SRCDIR)/%)
OBJ = $(SRC_PATH:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
MEMORYTYPES = flash eeprom fuse lfuse hfuse efuse boot calibration lock signature application apptable prodsig usersig
avr: build
all: build
help:
@echo 'Basic targets:'
@echo ' build Create all files.'
@echo ' clean Remove files created by make.'
@echo ' size Show the size of each section in the .elf file.'
@echo
@echo 'Create files:'
@echo ' elf Create binary .elf file.'
@echo ' hex Create .hex file containing .text and .data sections.'
@echo ' eep Create .eep file with the EEPROM content.'
@echo ' lss Create .lss file with a listing of the program.'
@echo
@echo 'Flashing:'
@echo ' program Write flash and EEPROM.'
@echo ' flash Write only flash.'
@echo ' eeprom Write only EEPROM.'
@echo ' backup Backup MCU content to "$(BACKUPDIR)". Available memory types:'
@echo ' $(MEMORYTYPES)'
@echo
@echo 'Fuses:'
@echo ' readfuses Read fuses from MCU.'
@echo ' writefuses Write fuses to MCU using .fuse section.'
@echo ' printfuses Print fuses from .fuse section.'
build: elf hex eep lss size
elf: $(OBJDIR)/$(TARGET).elf
hex: $(OBJDIR)/$(TARGET).hex
eep: $(OBJDIR)/$(TARGET).eep
lss: $(OBJDIR)/$(TARGET).lss
size: $(OBJDIR)/$(TARGET).size
program: flash eeprom
flash: $(TARGET).hex
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)
eeprom: $(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_EEPROM)
readfuses:
$(AVRDUDE) $(AVRDUDE_FLAGS) -U lfuse:r:-:i -U hfuse:r:-:i
writefuses:
$(AVRDUDE) $(AVRDUDE_FLAGS) -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m
#writefuses: FUSES = $(shell $(OBJDUMP) -s --section=.fuse $(TARGET).elf | tail -1 | awk '{print substr($$2,1,2),substr($$2,3,2),substr($$2,5,2)}')
#writefuses: $(TARGET).elf
# $(AVRDUDE) $(AVRDUDE_FLAGS) \
# $(if $(word 1,$(FUSES)),-U lfuse:w:0x$(word 1,$(FUSES)):m) \
# $(if $(word 2,$(FUSES)),-U hfuse:w:0x$(word 2,$(FUSES)):m) \
# $(if $(word 3,$(FUSES)),-U efuse:w:0x$(word 3,$(FUSES)):m)
printfuses: FUSES = $(shell $(OBJDUMP) -s --section=.fuse $(TARGET).elf | tail -1 | awk '{print substr($$2,1,2),substr($$2,3,2),substr($$2,5,2)}')
printfuses: $(TARGET).elf
@echo 'FUSES = $(FUSES)'
$(OBJDIR)/%.hex: $(OBJDIR)/%.elf
@$(OBJCOPY) -O ihex -j .text -j .data $< $@
$(OBJDIR)/%.eep: $(OBJDIR)/%.elf
@$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $< $@
$(OBJDIR)/%.lss: $(OBJDIR)/%.elf
@$(OBJDUMP) -h -S $< > $@
.SECONDARY: $(TARGET).elf
.PRECIOUS: $(OBJ)
$(OBJDIR)/%.elf: $(OBJ)
@echo link $^
@$(CXX) $(CPU) $^ -o $@ $(LDFLAGS) #-lstdc++
$(OBJDIR)/%.o: $(SRCDIR)/%.c
@$(shell mkdir -p $(OBJDIR) 2>/dev/null)
@echo compile $<
@$(CC) -c $(CPU) $(CFLAGS) $< -o $@
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
@$(shell mkdir -p $(OBJDIR) 2>/dev/null)
@echo compile $<
@$(CXX) -c $(CPU) $(CXXFLAGS) $< -o $@
$(OBJDIR)/%.size: $(OBJDIR)/%.elf
@$(AVRSIZE) -A $< | tee $@
clean:
@$(REMOVE) "size"
@$(REMOVE) "$(TARGET).hex"
@$(REMOVE) "$(TARGET).eep"
@$(REMOVE) "$(TARGET).elf"
@$(REMOVE) "$(TARGET).lss"
@$(REMOVEDIR) "$(OBJDIR)"
@echo clean $(TARGET)
backup:
@$(shell mkdir -p $(BACKUPDIR) 2>/dev/null)
@for memory in $(MEMORYTYPES); do \
$(AVRDUDE) $(AVRDUDE_FLAGS) -U $$memory:r:$(BACKUPDIR)/$(MCU).$$memory.hex:i; \
done
.PHONY: all build elf hex eep lss clean program flash eeprom readfuses writefuses printfuses backup help