platform.mk 6.4 KB

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