platform.mk (8848B)
1 # Hey Emacs, this is a -*- makefile -*- 2 ############################################################################## 3 # Compiler settings 4 # 5 CC = $(CC_PREFIX) avr-gcc 6 OBJCOPY = avr-objcopy 7 OBJDUMP = avr-objdump 8 SIZE = avr-size 9 AR = avr-ar 10 NM = avr-nm 11 HEX = $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature 12 EEP = $(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) 13 BIN = 14 15 COMPILEFLAGS += $(call cc-option,--param=min-pagesize=0) 16 17 # Fix ICE's: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116389 18 COMPILEFLAGS += $(call cc-option,-mlra) 19 20 COMPILEFLAGS += -funsigned-char 21 COMPILEFLAGS += -funsigned-bitfields 22 COMPILEFLAGS += -ffunction-sections 23 COMPILEFLAGS += -fdata-sections 24 COMPILEFLAGS += -fpack-struct 25 COMPILEFLAGS += -fshort-enums 26 COMPILEFLAGS += -mcall-prologues 27 COMPILEFLAGS += -fno-builtin-printf 28 29 # On older compilers, linker relaxation is only possible if link time optimizations are not enabled. 30 ifeq ($(strip $(LTO_ENABLE)), no) 31 COMPILEFLAGS += -mrelax 32 else 33 # Newer compilers may support both, so quickly check before adding `-mrelax`. 34 COMPILEFLAGS += $(call cc-option,-mrelax,,-flto=auto) 35 endif 36 37 ASFLAGS += $(AVR_ASFLAGS) 38 39 CFLAGS += $(COMPILEFLAGS) $(AVR_CFLAGS) 40 CFLAGS += -fno-inline-small-functions 41 CFLAGS += -fno-strict-aliasing 42 43 CXXFLAGS += $(COMPILEFLAGS) 44 CXXFLAGS += -fno-exceptions $(CXXSTANDARD) 45 46 LDFLAGS += -Wl,--gc-sections 47 48 # Use AVR's libc minimal printf implementation which has less features 49 # and thus can shave ~400 bytes. Usually we use the xprintf 50 # implementation but keyboards that use s(n)printf automatically 51 # pull in the AVR libc implementation, which is ~900 bytes heavy. 52 AVR_USE_MINIMAL_PRINTF ?= no 53 ifeq ($(strip $(AVR_USE_MINIMAL_PRINTF)), yes) 54 LDFLAGS += -Wl,--whole-archive -lprintf_min -Wl,--no-whole-archive 55 endif 56 57 OPT_DEFS += -DF_CPU=$(F_CPU)UL 58 59 MCUFLAGS = -mmcu=$(MCU) 60 61 # List any extra directories to look for libraries here. 62 # Each directory must be seperated by a space. 63 # Use forward slashes for directory separators. 64 # For a directory that has spaces, enclose it in quotes. 65 EXTRALIBDIRS = 66 67 68 #---------------- External Memory Options ---------------- 69 70 # 64 KB of external RAM, starting after internal RAM (ATmega128!), 71 # used for variables (.data/.bss) and heap (malloc()). 72 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff 73 74 # 64 KB of external RAM, starting after internal RAM (ATmega128!), 75 # only used for heap (malloc()). 76 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff 77 78 EXTMEMOPTS = 79 80 #---------------- Debugging Options ---------------- 81 82 # Debugging format. 83 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs. 84 # AVR Studio 4.10 requires dwarf-2. 85 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run. 86 DEBUG = dwarf-2 87 88 # For simulavr only - target MCU frequency. 89 DEBUG_MFREQ = $(F_CPU) 90 91 # Set the DEBUG_UI to either gdb or insight. 92 # DEBUG_UI = gdb 93 DEBUG_UI = insight 94 95 # Set the debugging back-end to either avarice, simulavr. 96 DEBUG_BACKEND = avarice 97 #DEBUG_BACKEND = simulavr 98 99 # GDB Init Filename. 100 GDBINIT_FILE = __avr_gdbinit 101 102 # When using avarice settings for the JTAG 103 JTAG_DEV = /dev/com1 104 105 # Debugging port used to communicate between GDB / avarice / simulavr. 106 DEBUG_PORT = 4242 107 108 # Debugging host used to communicate between GDB / avarice / simulavr, normally 109 # just set to localhost unless doing some sort of crazy debugging when 110 # avarice is running on a different computer. 111 DEBUG_HOST = localhost 112 113 #============================================================================ 114 115 SIZE_MARGIN = 1024 116 117 check-size: 118 $(eval MAX_SIZE=$(shell n=`$(CC) -E -mmcu=$(MCU) -D__ASSEMBLER__ $(CFLAGS) $(OPT_DEFS) platforms/avr/bootloader_size.c 2> /dev/null | $(SED) -ne 's/\r//;/^#/n;/^AVR_SIZE:/,$${s/^AVR_SIZE: //;p;}'` && echo $$(($$n)) || echo 0)) 119 $(eval CURRENT_SIZE=$(shell if [ -f $(BUILD_DIR)/$(TARGET).hex ]; then $(SIZE) --target=$(FORMAT) $(BUILD_DIR)/$(TARGET).hex | $(AWK) 'NR==2 {print $$4}'; else printf 0; fi)) 120 $(eval FREE_SIZE=$(shell expr $(MAX_SIZE) - $(CURRENT_SIZE))) 121 $(eval OVER_SIZE=$(shell expr $(CURRENT_SIZE) - $(MAX_SIZE))) 122 $(eval PERCENT_SIZE=$(shell expr $(CURRENT_SIZE) \* 100 / $(MAX_SIZE))) 123 if [ $(MAX_SIZE) -gt 0 ] && [ $(CURRENT_SIZE) -gt 0 ]; then \ 124 $(SILENT) || printf "$(MSG_CHECK_FILESIZE)" | $(AWK_CMD); \ 125 if [ $(CURRENT_SIZE) -gt $(MAX_SIZE) ]; then \ 126 $(REMOVE) $(TARGET).$(FIRMWARE_FORMAT); \ 127 $(REMOVE) $(BUILD_DIR)/$(TARGET).{hex,bin,uf2}; \ 128 printf "\n * $(MSG_FILE_TOO_BIG)"; $(PRINT_ERROR_PLAIN); \ 129 else \ 130 if [ $(FREE_SIZE) -lt $(SIZE_MARGIN) ]; then \ 131 $(PRINT_WARNING_PLAIN); printf " * $(MSG_FILE_NEAR_LIMIT)"; \ 132 else \ 133 $(PRINT_OK); $(SILENT) || printf " * $(MSG_FILE_JUST_RIGHT)"; \ 134 fi ; \ 135 fi ; \ 136 fi 137 138 # Convert hex to bin. 139 bin: $(BUILD_DIR)/$(TARGET).hex 140 ifeq ($(BOOTLOADER),lufa-ms) 141 $(eval BIN_PADDING=$(shell n=`expr 32768 - $(BOOTLOADER_SIZE)` && echo $$(($$n)) || echo 0)) 142 $(OBJCOPY) -Iihex -Obinary $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin --pad-to $(BIN_PADDING) 143 else 144 $(OBJCOPY) -Iihex -Obinary $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin 145 endif 146 $(COPY) $(BUILD_DIR)/$(TARGET).bin $(TARGET).bin; 147 148 # copy bin to FLASH.bin 149 flashbin: bin 150 $(COPY) $(BUILD_DIR)/$(TARGET).bin FLASH.bin; 151 152 # Generate avr-gdb config/init file which does the following: 153 # define the reset signal, load the target file, connect to target, and set 154 # a breakpoint at main(). 155 gdb-config: 156 @$(REMOVE) $(GDBINIT_FILE) 157 @echo define reset >> $(GDBINIT_FILE) 158 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE) 159 @echo end >> $(GDBINIT_FILE) 160 @echo file $(BUILD_DIR)/$(TARGET).elf >> $(GDBINIT_FILE) 161 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE) 162 ifeq ($(DEBUG_BACKEND),simulavr) 163 @echo load >> $(GDBINIT_FILE) 164 endif 165 @echo break main >> $(GDBINIT_FILE) 166 167 debug: gdb-config $(BUILD_DIR)/$(TARGET).elf 168 ifeq ($(DEBUG_BACKEND), avarice) 169 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays. 170 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \ 171 $(BUILD_DIR)/$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT) 172 @$(WINSHELL) /c pause 173 174 else 175 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \ 176 $(DEBUG_MFREQ) --port $(DEBUG_PORT) 177 endif 178 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE) 179 180 181 182 183 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. 184 COFFCONVERT = $(OBJCOPY) --debugging 185 COFFCONVERT += --change-section-address .data-0x800000 186 COFFCONVERT += --change-section-address .bss-0x800000 187 COFFCONVERT += --change-section-address .noinit-0x800000 188 COFFCONVERT += --change-section-address .eeprom-0x810000 189 190 191 192 coff: $(BUILD_DIR)/$(TARGET).elf 193 @$(SECHO) $(MSG_COFF) $(BUILD_DIR)/$(TARGET).cof 194 $(COFFCONVERT) -O coff-avr $< $(BUILD_DIR)/$(TARGET).cof 195 196 197 extcoff: $(BUILD_DIR)/$(TARGET).elf 198 @$(SECHO) $(MSG_EXTENDED_COFF) $(BUILD_DIR)/$(TARGET).cof 199 $(COFFCONVERT) -O coff-ext-avr $< $(BUILD_DIR)/$(TARGET).cof 200 201 ifeq ($(strip $(BOOTLOADER)), qmk-dfu) 202 QMK_BOOTLOADER_TYPE = DFU 203 else ifeq ($(strip $(BOOTLOADER)), qmk-hid) 204 QMK_BOOTLOADER_TYPE = HID 205 endif 206 207 bootloader: cpfirmware 208 ifeq ($(strip $(QMK_BOOTLOADER_TYPE)),) 209 $(call CATASTROPHIC_ERROR,Invalid BOOTLOADER,Please set BOOTLOADER to "qmk-dfu" or "qmk-hid" first!) 210 else 211 make -C lib/lufa/Bootloaders/$(QMK_BOOTLOADER_TYPE)/ clean TARGET=Bootloader$(QMK_BOOTLOADER_TYPE) 212 $(QMK_BIN) generate-dfu-header --quiet --keyboard $(KEYBOARD) --output lib/lufa/Bootloaders/$(QMK_BOOTLOADER_TYPE)/Keyboard.h 213 $(eval MAX_SIZE=$(shell n=`$(CC) -E -mmcu=$(MCU) -D__ASSEMBLER__ $(CFLAGS) $(OPT_DEFS) platforms/avr/bootloader_size.c 2> /dev/null | sed -ne 's/\r//;/^#/n;/^AVR_SIZE:/,$${s/^AVR_SIZE: //;p;}'` && echo $$(($$n)) || echo 0)) 214 $(eval PROGRAM_SIZE_KB=$(shell n=`expr $(MAX_SIZE) / 1024` && echo $$(($$n)) || echo 0)) 215 $(eval BOOT_SECTION_SIZE_KB=$(shell n=`expr $(BOOTLOADER_SIZE) / 1024` && echo $$(($$n)) || echo 0)) 216 $(eval FLASH_SIZE_KB=$(shell n=`expr $(PROGRAM_SIZE_KB) + $(BOOT_SECTION_SIZE_KB)` && echo $$(($$n)) || echo 0)) 217 make -C lib/lufa/Bootloaders/$(QMK_BOOTLOADER_TYPE)/ MCU=$(MCU) ARCH=$(ARCH) F_CPU=$(F_CPU) FLASH_SIZE_KB=$(FLASH_SIZE_KB) BOOT_SECTION_SIZE_KB=$(BOOT_SECTION_SIZE_KB) TARGET=Bootloader$(QMK_BOOTLOADER_TYPE) 218 printf "Bootloader$(QMK_BOOTLOADER_TYPE).hex copied to $(TARGET)_bootloader.hex\n" 219 cp lib/lufa/Bootloaders/$(QMK_BOOTLOADER_TYPE)/Bootloader$(QMK_BOOTLOADER_TYPE).hex $(TARGET)_bootloader.hex 220 endif 221 222 production: $(BUILD_DIR)/$(TARGET).hex bootloader cpfirmware 223 @cat $(BUILD_DIR)/$(TARGET).hex | awk '/^:00000001FF/ == 0' > $(TARGET)_production.hex 224 @cat $(TARGET)_bootloader.hex >> $(TARGET)_production.hex 225 echo "File sizes:" 226 $(SIZE) $(TARGET).hex $(TARGET)_bootloader.hex $(TARGET)_production.hex