platform.mk 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105523
  15. ifneq ($(findstring 12.,$(shell avr-gcc --version 2>/dev/null)),)
  16. COMPILEFLAGS += --param=min-pagesize=0
  17. endif
  18. COMPILEFLAGS += -funsigned-char
  19. COMPILEFLAGS += -funsigned-bitfields
  20. COMPILEFLAGS += -ffunction-sections
  21. COMPILEFLAGS += -fdata-sections
  22. COMPILEFLAGS += -fpack-struct
  23. COMPILEFLAGS += -fshort-enums
  24. COMPILEFLAGS += -mcall-prologues
  25. COMPILEFLAGS += -fno-builtin-printf
  26. # Linker relaxation is only possible if
  27. # link time optimizations are not enabled.
  28. ifeq ($(strip $(LTO_ENABLE)), no)
  29. COMPILEFLAGS += -mrelax
  30. endif
  31. ASFLAGS += $(AVR_ASFLAGS)
  32. CFLAGS += $(COMPILEFLAGS) $(AVR_CFLAGS)
  33. CFLAGS += -fno-inline-small-functions
  34. CFLAGS += -fno-strict-aliasing
  35. CXXFLAGS += $(COMPILEFLAGS)
  36. CXXFLAGS += -fno-exceptions $(CXXSTANDARD)
  37. LDFLAGS += -Wl,--gc-sections
  38. # Use AVR's libc minimal printf implementation which has less features
  39. # and thus can shave ~400 bytes. Usually we use the xprintf
  40. # implementation but keyboards that use s(n)printf automatically
  41. # pull in the AVR libc implementation, which is ~900 bytes heavy.
  42. AVR_USE_MINIMAL_PRINTF ?= no
  43. ifeq ($(strip $(AVR_USE_MINIMAL_PRINTF)), yes)
  44. LDFLAGS += -Wl,--whole-archive -lprintf_min -Wl,--no-whole-archive
  45. endif
  46. OPT_DEFS += -DF_CPU=$(F_CPU)UL
  47. MCUFLAGS = -mmcu=$(MCU)
  48. # List any extra directories to look for libraries here.
  49. # Each directory must be seperated by a space.
  50. # Use forward slashes for directory separators.
  51. # For a directory that has spaces, enclose it in quotes.
  52. EXTRALIBDIRS =
  53. #---------------- External Memory Options ----------------
  54. # 64 KB of external RAM, starting after internal RAM (ATmega128!),
  55. # used for variables (.data/.bss) and heap (malloc()).
  56. #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
  57. # 64 KB of external RAM, starting after internal RAM (ATmega128!),
  58. # only used for heap (malloc()).
  59. #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
  60. EXTMEMOPTS =
  61. #---------------- Debugging Options ----------------
  62. # Debugging format.
  63. # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
  64. # AVR Studio 4.10 requires dwarf-2.
  65. # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
  66. DEBUG = dwarf-2
  67. # For simulavr only - target MCU frequency.
  68. DEBUG_MFREQ = $(F_CPU)
  69. # Set the DEBUG_UI to either gdb or insight.
  70. # DEBUG_UI = gdb
  71. DEBUG_UI = insight
  72. # Set the debugging back-end to either avarice, simulavr.
  73. DEBUG_BACKEND = avarice
  74. #DEBUG_BACKEND = simulavr
  75. # GDB Init Filename.
  76. GDBINIT_FILE = __avr_gdbinit
  77. # When using avarice settings for the JTAG
  78. JTAG_DEV = /dev/com1
  79. # Debugging port used to communicate between GDB / avarice / simulavr.
  80. DEBUG_PORT = 4242
  81. # Debugging host used to communicate between GDB / avarice / simulavr, normally
  82. # just set to localhost unless doing some sort of crazy debugging when
  83. # avarice is running on a different computer.
  84. DEBUG_HOST = localhost
  85. #============================================================================
  86. # Convert hex to bin.
  87. bin: $(BUILD_DIR)/$(TARGET).hex
  88. ifeq ($(BOOTLOADER),lufa-ms)
  89. $(eval BIN_PADDING=$(shell n=`expr 32768 - $(BOOTLOADER_SIZE)` && echo $$(($$n)) || echo 0))
  90. $(OBJCOPY) -Iihex -Obinary $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin --pad-to $(BIN_PADDING)
  91. else
  92. $(OBJCOPY) -Iihex -Obinary $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
  93. endif
  94. $(COPY) $(BUILD_DIR)/$(TARGET).bin $(TARGET).bin;
  95. # copy bin to FLASH.bin
  96. flashbin: bin
  97. $(COPY) $(BUILD_DIR)/$(TARGET).bin FLASH.bin;
  98. # Generate avr-gdb config/init file which does the following:
  99. # define the reset signal, load the target file, connect to target, and set
  100. # a breakpoint at main().
  101. gdb-config:
  102. @$(REMOVE) $(GDBINIT_FILE)
  103. @echo define reset >> $(GDBINIT_FILE)
  104. @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
  105. @echo end >> $(GDBINIT_FILE)
  106. @echo file $(BUILD_DIR)/$(TARGET).elf >> $(GDBINIT_FILE)
  107. @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
  108. ifeq ($(DEBUG_BACKEND),simulavr)
  109. @echo load >> $(GDBINIT_FILE)
  110. endif
  111. @echo break main >> $(GDBINIT_FILE)
  112. debug: gdb-config $(BUILD_DIR)/$(TARGET).elf
  113. ifeq ($(DEBUG_BACKEND), avarice)
  114. @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
  115. @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
  116. $(BUILD_DIR)/$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
  117. @$(WINSHELL) /c pause
  118. else
  119. @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
  120. $(DEBUG_MFREQ) --port $(DEBUG_PORT)
  121. endif
  122. @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
  123. # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
  124. COFFCONVERT = $(OBJCOPY) --debugging
  125. COFFCONVERT += --change-section-address .data-0x800000
  126. COFFCONVERT += --change-section-address .bss-0x800000
  127. COFFCONVERT += --change-section-address .noinit-0x800000
  128. COFFCONVERT += --change-section-address .eeprom-0x810000
  129. coff: $(BUILD_DIR)/$(TARGET).elf
  130. @$(SECHO) $(MSG_COFF) $(BUILD_DIR)/$(TARGET).cof
  131. $(COFFCONVERT) -O coff-avr $< $(BUILD_DIR)/$(TARGET).cof
  132. extcoff: $(BUILD_DIR)/$(TARGET).elf
  133. @$(SECHO) $(MSG_EXTENDED_COFF) $(BUILD_DIR)/$(TARGET).cof
  134. $(COFFCONVERT) -O coff-ext-avr $< $(BUILD_DIR)/$(TARGET).cof
  135. ifeq ($(strip $(BOOTLOADER)), qmk-dfu)
  136. QMK_BOOTLOADER_TYPE = DFU
  137. else ifeq ($(strip $(BOOTLOADER)), qmk-hid)
  138. QMK_BOOTLOADER_TYPE = HID
  139. endif
  140. bootloader:
  141. ifeq ($(strip $(QMK_BOOTLOADER_TYPE)),)
  142. $(call CATASTROPHIC_ERROR,Invalid BOOTLOADER,Please set BOOTLOADER to "qmk-dfu" or "qmk-hid" first!)
  143. else
  144. make -C lib/lufa/Bootloaders/$(QMK_BOOTLOADER_TYPE)/ clean
  145. $(QMK_BIN) generate-dfu-header --quiet --keyboard $(KEYBOARD) --output lib/lufa/Bootloaders/$(QMK_BOOTLOADER_TYPE)/Keyboard.h
  146. $(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))
  147. $(eval PROGRAM_SIZE_KB=$(shell n=`expr $(MAX_SIZE) / 1024` && echo $$(($$n)) || echo 0))
  148. $(eval BOOT_SECTION_SIZE_KB=$(shell n=`expr $(BOOTLOADER_SIZE) / 1024` && echo $$(($$n)) || echo 0))
  149. $(eval FLASH_SIZE_KB=$(shell n=`expr $(PROGRAM_SIZE_KB) + $(BOOT_SECTION_SIZE_KB)` && echo $$(($$n)) || echo 0))
  150. 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)
  151. printf "Bootloader$(QMK_BOOTLOADER_TYPE).hex copied to $(TARGET)_bootloader.hex\n"
  152. cp lib/lufa/Bootloaders/$(QMK_BOOTLOADER_TYPE)/Bootloader$(QMK_BOOTLOADER_TYPE).hex $(TARGET)_bootloader.hex
  153. endif
  154. production: $(BUILD_DIR)/$(TARGET).hex bootloader cpfirmware
  155. @cat $(BUILD_DIR)/$(TARGET).hex | awk '/^:00000001FF/ == 0' > $(TARGET)_production.hex
  156. @cat $(TARGET)_bootloader.hex >> $(TARGET)_production.hex
  157. echo "File sizes:"
  158. $(SIZE) $(TARGET).hex $(TARGET)_bootloader.hex $(TARGET)_production.hex