]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/avr.mk
0e014a412aa7957be40f33e03b9f27e654266485
[qmk_firmware.git] / tmk_core / avr.mk
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
15 COMMON_VPATH += $(DRIVER_PATH)/avr
16
17 COMPILEFLAGS += -funsigned-char
18 COMPILEFLAGS += -funsigned-bitfields
19 COMPILEFLAGS += -ffunction-sections
20 COMPILEFLAGS += -fdata-sections
21 COMPILEFLAGS += -fpack-struct
22 COMPILEFLAGS += -fshort-enums
23
24 CFLAGS += $(COMPILEFLAGS)
25 CFLAGS += -fno-inline-small-functions
26 CFLAGS += -fno-strict-aliasing
27
28 CPPFLAGS += $(COMPILEFLAGS)
29 CPPFLAGS += -fno-exceptions -std=c++11
30
31 LDFLAGS +=-Wl,--gc-sections
32
33 OPT_DEFS += -DF_CPU=$(F_CPU)UL
34
35 MCUFLAGS = -mmcu=$(MCU)
36
37 # List any extra directories to look for libraries here.
38 #     Each directory must be seperated by a space.
39 #     Use forward slashes for directory separators.
40 #     For a directory that has spaces, enclose it in quotes.
41 EXTRALIBDIRS =
42
43
44 #---------------- External Memory Options ----------------
45
46 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
47 # used for variables (.data/.bss) and heap (malloc()).
48 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
49
50 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
51 # only used for heap (malloc()).
52 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
53
54 EXTMEMOPTS =
55
56 #---------------- Debugging Options ----------------
57
58 # Debugging format.
59 #     Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
60 #     AVR Studio 4.10 requires dwarf-2.
61 #     AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
62 DEBUG = dwarf-2
63
64 # For simulavr only - target MCU frequency.
65 DEBUG_MFREQ = $(F_CPU)
66
67 # Set the DEBUG_UI to either gdb or insight.
68 # DEBUG_UI = gdb
69 DEBUG_UI = insight
70
71 # Set the debugging back-end to either avarice, simulavr.
72 DEBUG_BACKEND = avarice
73 #DEBUG_BACKEND = simulavr
74
75 # GDB Init Filename.
76 GDBINIT_FILE = __avr_gdbinit
77
78 # When using avarice settings for the JTAG
79 JTAG_DEV = /dev/com1
80
81 # Debugging port used to communicate between GDB / avarice / simulavr.
82 DEBUG_PORT = 4242
83
84 # Debugging host used to communicate between GDB / avarice / simulavr, normally
85 #     just set to localhost unless doing some sort of crazy debugging when
86 #     avarice is running on a different computer.
87 DEBUG_HOST = localhost
88
89 #============================================================================
90 # Autodetect teensy loader
91 ifndef TEENSY_LOADER_CLI
92     ifneq (, $(shell which teensy-loader-cli 2>/dev/null))
93         TEENSY_LOADER_CLI ?= teensy-loader-cli
94     else
95         TEENSY_LOADER_CLI ?= teensy_loader_cli
96     endif
97 endif
98
99 # Program the device.
100 program: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
101         $(PROGRAM_CMD)
102
103 teensy: $(BUILD_DIR)/$(TARGET).hex
104         $(TEENSY_LOADER_CLI) -mmcu=$(MCU) -w -v $(BUILD_DIR)/$(TARGET).hex
105         
106 BATCHISP ?= batchisp 
107
108 flip: $(BUILD_DIR)/$(TARGET).hex
109         $(BATCHISP) -hardware usb -device $(MCU) -operation erase f
110         $(BATCHISP) -hardware usb -device $(MCU) -operation loadbuffer $(BUILD_DIR)/$(TARGET).hex program
111         $(BATCHISP) -hardware usb -device $(MCU) -operation start reset 0
112         
113 DFU_PROGRAMMER ?= dfu-programmer
114
115 dfu: $(BUILD_DIR)/$(TARGET).hex sizeafter
116         until $(DFU_PROGRAMMER) $(MCU) get bootloader-version; do\
117                 echo "Error: Bootloader not found. Trying again in 5s." ;\
118                 sleep 5 ;\
119         done
120         if $(DFU_PROGRAMMER) --version 2>&1 | grep -q 0.7 ; then\
121                 $(DFU_PROGRAMMER) $(MCU) erase --force;\
122         else\
123                 $(DFU_PROGRAMMER) $(MCU) erase;\
124         fi
125         $(DFU_PROGRAMMER) $(MCU) flash $(BUILD_DIR)/$(TARGET).hex
126         $(DFU_PROGRAMMER) $(MCU) reset
127
128 dfu-start:
129         $(DFU_PROGRAMMER) $(MCU) reset
130         $(DFU_PROGRAMMER) $(MCU) start
131
132 flip-ee: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
133         $(COPY) $(BUILD_DIR)/$(TARGET).eep $(BUILD_DIR)/$(TARGET)eep.hex
134         $(BATCHISP) -hardware usb -device $(MCU) -operation memory EEPROM erase
135         $(BATCHISP) -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(BUILD_DIR)/$(TARGET)eep.hex program
136         $(BATCHISP) -hardware usb -device $(MCU) -operation start reset 0
137         $(REMOVE) $(BUILD_DIR)/$(TARGET)eep.hex
138
139 dfu-ee: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
140         if $(DFU_PROGRAMMER) --version 2>&1 | grep -q 0.7 ; then\
141                 $(DFU_PROGRAMMER) $(MCU) flash --eeprom $(BUILD_DIR)/$(TARGET).eep;\
142         else\
143                 $(DFU_PROGRAMMER) $(MCU) flash-eeprom $(BUILD_DIR)/$(TARGET).eep;\
144         fi
145         $(DFU_PROGRAMMER) $(MCU) reset
146
147 avrdude: $(BUILD_DIR)/$(TARGET).hex
148         if grep -q -s Microsoft /proc/version; then \
149                 echo 'ERROR: AVR flashing cannot be automated within the Windows Subsystem for Linux (WSL) currently. Instead, take the .hex file generated and flash it using AVRDUDE, AVRDUDESS, or XLoader.'; \
150         else \
151                 ls /dev/tty* > /tmp/1; \
152                 echo "Detecting USB port, reset your controller now.\c"; \
153                 while [ -z $$USB ]; do \
154                         sleep 1; \
155                         echo ".\c"; \
156                         ls /dev/tty* > /tmp/2; \
157                         USB=`diff /tmp/1 /tmp/2 | grep -o '/dev/tty.*'`; \
158                 done; \
159                 echo ""; \
160                 echo "Detected controller on USB port at $$USB"; \
161                 sleep 1; \
162                 avrdude -p $(MCU) -c avr109 -P $$USB -U flash:w:$(BUILD_DIR)/$(TARGET).hex; \
163         fi
164
165 # Convert hex to bin.
166 bin: $(BUILD_DIR)/$(TARGET).hex
167         $(OBJCOPY) -Iihex -Obinary $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
168         $(COPY) $(BUILD_DIR)/$(TARGET).bin $(TARGET).bin;
169
170 # copy bin to FLASH.bin
171 flashbin: bin
172         $(COPY) $(BUILD_DIR)/$(TARGET).bin FLASH.bin; 
173
174 # Generate avr-gdb config/init file which does the following:
175 #     define the reset signal, load the target file, connect to target, and set
176 #     a breakpoint at main().
177 gdb-config:
178         @$(REMOVE) $(GDBINIT_FILE)
179         @echo define reset >> $(GDBINIT_FILE)
180         @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
181         @echo end >> $(GDBINIT_FILE)
182         @echo file $(BUILD_DIR)/$(TARGET).elf >> $(GDBINIT_FILE)
183         @echo target remote $(DEBUG_HOST):$(DEBUG_PORT)  >> $(GDBINIT_FILE)
184 ifeq ($(DEBUG_BACKEND),simulavr)
185         @echo load  >> $(GDBINIT_FILE)
186 endif
187         @echo break main >> $(GDBINIT_FILE)
188
189 debug: gdb-config $(BUILD_DIR)/$(TARGET).elf
190 ifeq ($(DEBUG_BACKEND), avarice)
191         @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
192         @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
193         $(BUILD_DIR)/$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
194         @$(WINSHELL) /c pause
195
196 else
197         @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
198         $(DEBUG_MFREQ) --port $(DEBUG_PORT)
199 endif
200         @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
201
202
203
204
205 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
206 COFFCONVERT = $(OBJCOPY) --debugging
207 COFFCONVERT += --change-section-address .data-0x800000
208 COFFCONVERT += --change-section-address .bss-0x800000
209 COFFCONVERT += --change-section-address .noinit-0x800000
210 COFFCONVERT += --change-section-address .eeprom-0x810000
211
212
213
214 coff: $(BUILD_DIR)/$(TARGET).elf
215         @$(SECHO) $(MSG_COFF) $(BUILD_DIR)/$(TARGET).cof
216         $(COFFCONVERT) -O coff-avr $< $(BUILD_DIR)/$(TARGET).cof
217
218
219 extcoff: $(BUILD_DIR)/$(TARGET).elf
220         @$(SECHO) $(MSG_EXTENDED_COFF) $(BUILD_DIR)/$(TARGET).cof
221         $(COFFCONVERT) -O coff-ext-avr $< $(BUILD_DIR)/$(TARGET).cof
222