avr.mk 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. COMMON_VPATH += $(DRIVER_PATH)/avr
  15. COMPILEFLAGS += -funsigned-char
  16. COMPILEFLAGS += -funsigned-bitfields
  17. COMPILEFLAGS += -ffunction-sections
  18. COMPILEFLAGS += -fdata-sections
  19. COMPILEFLAGS += -fpack-struct
  20. COMPILEFLAGS += -fshort-enums
  21. CFLAGS += $(COMPILEFLAGS)
  22. CFLAGS += -fno-inline-small-functions
  23. CFLAGS += -fno-strict-aliasing
  24. CPPFLAGS += $(COMPILEFLAGS)
  25. CPPFLAGS += -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. # Autodetect teensy loader
  68. ifndef TEENSY_LOADER_CLI
  69. ifneq (, $(shell which teensy-loader-cli 2>/dev/null))
  70. TEENSY_LOADER_CLI ?= teensy-loader-cli
  71. else
  72. TEENSY_LOADER_CLI ?= teensy_loader_cli
  73. endif
  74. endif
  75. # Program the device.
  76. program: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
  77. $(PROGRAM_CMD)
  78. teensy: $(BUILD_DIR)/$(TARGET).hex
  79. $(TEENSY_LOADER_CLI) -mmcu=$(MCU) -w -v $(BUILD_DIR)/$(TARGET).hex
  80. BATCHISP ?= batchisp
  81. flip: $(BUILD_DIR)/$(TARGET).hex
  82. $(BATCHISP) -hardware usb -device $(MCU) -operation erase f
  83. $(BATCHISP) -hardware usb -device $(MCU) -operation loadbuffer $(BUILD_DIR)/$(TARGET).hex program
  84. $(BATCHISP) -hardware usb -device $(MCU) -operation start reset 0
  85. DFU_PROGRAMMER ?= dfu-programmer
  86. dfu: $(BUILD_DIR)/$(TARGET).hex sizeafter
  87. until $(DFU_PROGRAMMER) $(MCU) get bootloader-version; do\
  88. echo "Error: Bootloader not found. Trying again in 5s." ;\
  89. sleep 5 ;\
  90. done
  91. if $(DFU_PROGRAMMER) --version 2>&1 | grep -q 0.7 ; then\
  92. $(DFU_PROGRAMMER) $(MCU) erase --force;\
  93. else\
  94. $(DFU_PROGRAMMER) $(MCU) erase;\
  95. fi
  96. $(DFU_PROGRAMMER) $(MCU) flash $(BUILD_DIR)/$(TARGET).hex
  97. $(DFU_PROGRAMMER) $(MCU) reset
  98. dfu-start:
  99. $(DFU_PROGRAMMER) $(MCU) reset
  100. $(DFU_PROGRAMMER) $(MCU) start
  101. flip-ee: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
  102. $(COPY) $(BUILD_DIR)/$(TARGET).eep $(BUILD_DIR)/$(TARGET)eep.hex
  103. $(BATCHISP) -hardware usb -device $(MCU) -operation memory EEPROM erase
  104. $(BATCHISP) -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(BUILD_DIR)/$(TARGET)eep.hex program
  105. $(BATCHISP) -hardware usb -device $(MCU) -operation start reset 0
  106. $(REMOVE) $(BUILD_DIR)/$(TARGET)eep.hex
  107. dfu-ee: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
  108. if $(DFU_PROGRAMMER) --version 2>&1 | grep -q 0.7 ; then\
  109. $(DFU_PROGRAMMER) $(MCU) flash --eeprom $(BUILD_DIR)/$(TARGET).eep;\
  110. else\
  111. $(DFU_PROGRAMMER) $(MCU) flash-eeprom $(BUILD_DIR)/$(TARGET).eep;\
  112. fi
  113. $(DFU_PROGRAMMER) $(MCU) reset
  114. avrdude: $(BUILD_DIR)/$(TARGET).hex
  115. ls /dev/tty* > /tmp/1; \
  116. echo "Detecting Pro Micro port, reset your Pro Micro now.\c"; \
  117. while [ -z $$USB ]; do \
  118. sleep 1; \
  119. echo ".\c"; \
  120. ls /dev/tty* > /tmp/2; \
  121. USB=`diff /tmp/1 /tmp/2 | grep -o '/dev/tty.*'`; \
  122. done; \
  123. echo ""; \
  124. echo "Detected Pro Micro port at $$USB"; \
  125. sleep 1; \
  126. avrdude -p $(MCU) -c avr109 -P $$USB -U flash:w:$(BUILD_DIR)/$(TARGET).hex
  127. # Convert hex to bin.
  128. flashbin: $(BUILD_DIR)/$(TARGET).hex
  129. $(OBJCOPY) -Iihex -Obinary $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
  130. $(COPY) $(BUILD_DIR)/$(TARGET).bin $(TARGET).bin;
  131. $(COPY) $(BUILD_DIR)/$(TARGET).bin FLASH.bin;
  132. # Generate avr-gdb config/init file which does the following:
  133. # define the reset signal, load the target file, connect to target, and set
  134. # a breakpoint at main().
  135. gdb-config:
  136. @$(REMOVE) $(GDBINIT_FILE)
  137. @echo define reset >> $(GDBINIT_FILE)
  138. @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
  139. @echo end >> $(GDBINIT_FILE)
  140. @echo file $(BUILD_DIR)/$(TARGET).elf >> $(GDBINIT_FILE)
  141. @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
  142. ifeq ($(DEBUG_BACKEND),simulavr)
  143. @echo load >> $(GDBINIT_FILE)
  144. endif
  145. @echo break main >> $(GDBINIT_FILE)
  146. debug: gdb-config $(BUILD_DIR)/$(TARGET).elf
  147. ifeq ($(DEBUG_BACKEND), avarice)
  148. @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
  149. @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
  150. $(BUILD_DIR)/$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
  151. @$(WINSHELL) /c pause
  152. else
  153. @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
  154. $(DEBUG_MFREQ) --port $(DEBUG_PORT)
  155. endif
  156. @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
  157. # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
  158. COFFCONVERT = $(OBJCOPY) --debugging
  159. COFFCONVERT += --change-section-address .data-0x800000
  160. COFFCONVERT += --change-section-address .bss-0x800000
  161. COFFCONVERT += --change-section-address .noinit-0x800000
  162. COFFCONVERT += --change-section-address .eeprom-0x810000
  163. coff: $(BUILD_DIR)/$(TARGET).elf
  164. @$(SECHO) $(MSG_COFF) $(BUILD_DIR)/$(TARGET).cof
  165. $(COFFCONVERT) -O coff-avr $< $(BUILD_DIR)/$(TARGET).cof
  166. extcoff: $(BUILD_DIR)/$(TARGET).elf
  167. @$(SECHO) $(MSG_EXTENDED_COFF) $(BUILD_DIR)/$(TARGET).cof
  168. $(COFFCONVERT) -O coff-ext-avr $< $(BUILD_DIR)/$(TARGET).cof