platform.mk 7.2 KB

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