avr.mk 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # Hey Emacs, this is a -*- makefile -*-
  2. ##############################################################################
  3. # Compiler settings
  4. #
  5. CC = avr-gcc
  6. OBJCOPY = avr-objcopy
  7. OBJDUMP = avr-objdump
  8. SIZE = avr-size
  9. AR = avr-ar rcs
  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. CFLAGS += $(COMPILEFLAGS)
  21. CFLAGS += -fno-inline-small-functions
  22. CFLAGS += -fno-strict-aliasing
  23. CPPFLAGS += $(COMPILEFLAGS)
  24. CPPFLAGS += -fno-exceptions -std=c++11
  25. LDFLAGS +=-Wl,--gc-sections
  26. OPT_DEFS += -DF_CPU=$(F_CPU)UL
  27. MCUFLAGS = -mmcu=$(MCU)
  28. # List any extra directories to look for libraries here.
  29. # Each directory must be seperated by a space.
  30. # Use forward slashes for directory separators.
  31. # For a directory that has spaces, enclose it in quotes.
  32. EXTRALIBDIRS =
  33. #---------------- External Memory Options ----------------
  34. # 64 KB of external RAM, starting after internal RAM (ATmega128!),
  35. # used for variables (.data/.bss) and heap (malloc()).
  36. #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
  37. # 64 KB of external RAM, starting after internal RAM (ATmega128!),
  38. # only used for heap (malloc()).
  39. #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
  40. EXTMEMOPTS =
  41. #---------------- Debugging Options ----------------
  42. # Debugging format.
  43. # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
  44. # AVR Studio 4.10 requires dwarf-2.
  45. # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
  46. DEBUG = dwarf-2
  47. # For simulavr only - target MCU frequency.
  48. DEBUG_MFREQ = $(F_CPU)
  49. # Set the DEBUG_UI to either gdb or insight.
  50. # DEBUG_UI = gdb
  51. DEBUG_UI = insight
  52. # Set the debugging back-end to either avarice, simulavr.
  53. DEBUG_BACKEND = avarice
  54. #DEBUG_BACKEND = simulavr
  55. # GDB Init Filename.
  56. GDBINIT_FILE = __avr_gdbinit
  57. # When using avarice settings for the JTAG
  58. JTAG_DEV = /dev/com1
  59. # Debugging port used to communicate between GDB / avarice / simulavr.
  60. DEBUG_PORT = 4242
  61. # Debugging host used to communicate between GDB / avarice / simulavr, normally
  62. # just set to localhost unless doing some sort of crazy debugging when
  63. # avarice is running on a different computer.
  64. DEBUG_HOST = localhost
  65. #============================================================================
  66. # Autodecct teensy loader
  67. ifneq (, $(shell which teensy-loader-cli 2>/dev/null))
  68. TEENSY_LOADER_CLI ?= teensy-loader-cli
  69. else
  70. TEENSY_LOADER_CLI ?= teensy_loader_cli
  71. endif
  72. # Program the device.
  73. program: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
  74. $(PROGRAM_CMD)
  75. teensy: $(BUILD_DIR)/$(TARGET).hex
  76. $(TEENSY_LOADER_CLI) -mmcu=$(MCU) -w -v $(BUILD_DIR)/$(TARGET).hex
  77. BATCHISP ?= batchisp
  78. flip: $(BUILD_DIR)/$(TARGET).hex
  79. $(BATCHISP) -hardware usb -device $(MCU) -operation erase f
  80. $(BATCHISP) -hardware usb -device $(MCU) -operation loadbuffer $(BUILD_DIR)/$(TARGET).hex program
  81. $(BATCHISP) -hardware usb -device $(MCU) -operation start reset 0
  82. DFU_PROGRAMMER ?= dfu-programmer
  83. dfu: $(BUILD_DIR)/$(TARGET).hex sizeafter
  84. until $(DFU_PROGRAMMER) $(MCU) get bootloader-version; do\
  85. echo "Error: Bootloader not found. Trying again in 5s." ;\
  86. sleep 5 ;\
  87. done
  88. ifneq (, $(findstring 0.7, $(shell $(DFU_PROGRAMMER) --version 2>&1)))
  89. $(DFU_PROGRAMMER) $(MCU) erase --force
  90. else
  91. $(DFU_PROGRAMMER) $(MCU) erase
  92. endif
  93. $(DFU_PROGRAMMER) $(MCU) flash $(BUILD_DIR)/$(TARGET).hex
  94. $(DFU_PROGRAMMER) $(MCU) reset
  95. dfu-start:
  96. $(DFU_PROGRAMMER) $(MCU) reset
  97. $(DFU_PROGRAMMER) $(MCU) start
  98. flip-ee: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
  99. $(COPY) $(BUILD_DIR)/$(TARGET).eep $(BUILD_DIR)/$(TARGET)eep.hex
  100. $(BATCHISP) -hardware usb -device $(MCU) -operation memory EEPROM erase
  101. $(BATCHISP) -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(BUILD_DIR)/$(TARGET)eep.hex program
  102. $(BATCHISP) -hardware usb -device $(MCU) -operation start reset 0
  103. $(REMOVE) $(BUILD_DIR)/$(TARGET)eep.hex
  104. dfu-ee: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
  105. ifneq (, $(findstring 0.7, $(shell dfu-programmer --version 2>&1)))
  106. $(DFU_PROGRAMMER) $(MCU) flash --eeprom $(BUILD_DIR)/$(TARGET).eep
  107. else
  108. $(DFU_PROGRAMMER) $(MCU) flash-eeprom $(BUILD_DIR)/$(TARGET).eep
  109. endif
  110. $(DFU_PROGRAMMER) $(MCU) reset
  111. # Convert hex to bin.
  112. flashbin: $(BUILD_DIR)/$(TARGET).hex
  113. $(OBJCOPY) -Iihex -Obinary $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
  114. $(COPY) $(BUILD_DIR)/$(TARGET).bin $(TARGET).bin;
  115. $(COPY) $(BUILD_DIR)/$(TARGET).bin FLASH.bin;
  116. # Generate avr-gdb config/init file which does the following:
  117. # define the reset signal, load the target file, connect to target, and set
  118. # a breakpoint at main().
  119. gdb-config:
  120. @$(REMOVE) $(GDBINIT_FILE)
  121. @echo define reset >> $(GDBINIT_FILE)
  122. @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
  123. @echo end >> $(GDBINIT_FILE)
  124. @echo file $(BUILD_DIR)/$(TARGET).elf >> $(GDBINIT_FILE)
  125. @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
  126. ifeq ($(DEBUG_BACKEND),simulavr)
  127. @echo load >> $(GDBINIT_FILE)
  128. endif
  129. @echo break main >> $(GDBINIT_FILE)
  130. debug: gdb-config $(BUILD_DIR)/$(TARGET).elf
  131. ifeq ($(DEBUG_BACKEND), avarice)
  132. @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
  133. @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
  134. $(BUILD_DIR)/$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
  135. @$(WINSHELL) /c pause
  136. else
  137. @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
  138. $(DEBUG_MFREQ) --port $(DEBUG_PORT)
  139. endif
  140. @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
  141. # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
  142. COFFCONVERT = $(OBJCOPY) --debugging
  143. COFFCONVERT += --change-section-address .data-0x800000
  144. COFFCONVERT += --change-section-address .bss-0x800000
  145. COFFCONVERT += --change-section-address .noinit-0x800000
  146. COFFCONVERT += --change-section-address .eeprom-0x810000
  147. coff: $(BUILD_DIR)/$(TARGET).elf
  148. @$(SECHO) $(MSG_COFF) $(BUILD_DIR)/$(TARGET).cof
  149. $(COFFCONVERT) -O coff-avr $< $(BUILD_DIR)/$(TARGET).cof
  150. extcoff: $(BUILD_DIR)/$(TARGET).elf
  151. @$(SECHO) $(MSG_EXTENDED_COFF) $(BUILD_DIR)/$(TARGET).cof
  152. $(COFFCONVERT) -O coff-ext-avr $< $(BUILD_DIR)/$(TARGET).cof