platform.mk 6.6 KB

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