micromenu/avr.Makefile

207 lines
4.8 KiB
Text
Raw Normal View History

2018-10-14 17:32:59 +00:00
#==== Main Options =============================================================
MCU = atmega328p
F_CPU = 16000000
LFUSE = 0xf1
HFUSE = 0xdf
TARGET = main
2018-10-19 11:16:47 +00:00
SRC = col.cpp hal.cpp main.cpp
2018-10-14 17:32:59 +00:00
2018-10-19 11:16:47 +00:00
SRCDIR = src
INCDIR = inc
OBJDIR = build/avr
2018-10-14 17:32:59 +00:00
BACKUPDIR = backup
#==== Compile Options ==========================================================
OPT = s
2018-10-19 11:16:47 +00:00
CPU = -mmcu=$(MCU) -I.
CPU += -DF_CPU=$(F_CPU)UL
CPU += -O$(OPT)
2018-10-14 17:32:59 +00:00
#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
2018-10-19 11:16:47 +00:00
CFLAGS += -Wundef
2018-10-14 17:32:59 +00:00
#CFLAGS += -Wunreachable-code
#CFLAGS += -Wsign-compare
#CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
#CFLAGS += -flto
2018-10-19 11:16:47 +00:00
CFLAGS += -std=gnu99
CXXFLAGS += -std=c++11
CFLAGS += -I$(INCDIR) -DAVR
CXXFLAGS += -I$(INCDIR) -DAVR
2018-10-14 17:32:59 +00:00
#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 ==================================================================
2018-10-19 11:16:47 +00:00
CC = avr-gcc
CXX = avr-g++
2018-10-14 17:32:59 +00:00
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRDUDE = avrdude
AVRSIZE = avr-size
REMOVE = rm -f
REMOVEDIR = rm -rf
TAIL = tail
AWK = awk
2018-10-19 11:16:47 +00:00
SRC_PATH = $(SRC:%=$(SRCDIR)/%)
OBJ = $(SRC_PATH:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
2018-10-14 17:32:59 +00:00
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
2018-10-19 11:16:47 +00:00
avr: build
2018-10-14 17:32:59 +00:00
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
2018-10-19 11:16:47 +00:00
elf: $(OBJDIR)/$(TARGET).elf
hex: $(OBJDIR)/$(TARGET).hex
eep: $(OBJDIR)/$(TARGET).eep
lss: $(OBJDIR)/$(TARGET).lss
size: $(OBJDIR)/$(TARGET).size
2018-10-14 17:32:59 +00:00
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)'
2018-10-19 11:16:47 +00:00
$(OBJDIR)/%.hex: $(OBJDIR)/%.elf
@$(OBJCOPY) -O ihex -j .text -j .data $< $@
2018-10-14 17:32:59 +00:00
2018-10-19 11:16:47 +00:00
$(OBJDIR)/%.eep: $(OBJDIR)/%.elf
@$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $< $@
2018-10-14 17:32:59 +00:00
2018-10-19 11:16:47 +00:00
$(OBJDIR)/%.lss: $(OBJDIR)/%.elf
@$(OBJDUMP) -h -S $< > $@
2018-10-14 17:32:59 +00:00
.SECONDARY: $(TARGET).elf
.PRECIOUS: $(OBJ)
2018-10-25 14:21:57 +00:00
$(OBJDIR)/%.elf: $(OBJ) | $(OBJDIR)
2018-10-19 11:16:47 +00:00
@echo link $^
@$(CXX) $(CPU) $^ -o $@ $(LDFLAGS) #-lstdc++
2018-10-14 17:32:59 +00:00
2018-10-25 14:21:57 +00:00
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
2018-10-19 11:16:47 +00:00
@echo compile $<
@$(CC) -c $(CPU) $(CFLAGS) $< -o $@
2018-10-14 17:32:59 +00:00
2018-10-25 14:21:57 +00:00
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp | $(OBJDIR)
2018-10-19 11:16:47 +00:00
@echo compile $<
@$(CXX) -c $(CPU) $(CXXFLAGS) $< -o $@
2018-10-14 17:32:59 +00:00
2018-10-19 11:16:47 +00:00
$(OBJDIR)/%.size: $(OBJDIR)/%.elf
@$(AVRSIZE) -A $< | tee $@
2018-10-14 17:32:59 +00:00
2018-10-25 14:21:57 +00:00
$(OBJDIR):
mkdir -p $(OBJDIR)
2018-10-14 17:32:59 +00:00
clean:
2018-10-19 11:16:47 +00:00
@$(REMOVEDIR) "$(OBJDIR)"
2018-10-25 14:21:57 +00:00
@echo clean $(OBJDIR)
2018-10-14 17:32:59 +00:00
backup:
2018-10-19 11:16:47 +00:00
@$(shell mkdir -p $(BACKUPDIR) 2>/dev/null)
2018-10-14 17:32:59 +00:00
@for memory in $(MEMORYTYPES); do \
$(AVRDUDE) $(AVRDUDE_FLAGS) -U $$memory:r:$(BACKUPDIR)/$(MCU).$$memory.hex:i; \
done
2018-10-19 11:16:47 +00:00
.PHONY: all build elf hex eep lss clean program flash eeprom readfuses writefuses printfuses backup help