1 | # MCU name
|
2 | MCU = atmega2560
|
3 |
|
4 | # Target file name (without extension).
|
5 | TARGET = main
|
6 |
|
7 | # List C source files here. (C dependencies are automatically generated.)
|
8 | SRC = $(TARGET).c
|
9 |
|
10 | # More source files with SRC += myCfile.c and so on
|
11 |
|
12 |
|
13 | # Start Address
|
14 | # use 0x0000 for normal program
|
15 | # use 0x3800 for bootloader
|
16 | #LOAD_ADDR=0x3800
|
17 | LOAD_ADDR=0x0000
|
18 |
|
19 | # Output format. (can be srec, ihex, binary)
|
20 | FORMAT = ihex
|
21 |
|
22 | # List Assembler source files here.
|
23 | # Make them always end in a capital .S. Files ending in a lowercase .s
|
24 | # will not be considered source files but generated files (assembler
|
25 | # output from the compiler), and will be deleted upon "make clean"!
|
26 | # Even though the DOS/Win* filesystem matches both .s and .S the same,
|
27 | # it will preserve the spelling of the filenames, and gcc itself does
|
28 | # care about how the name is spelled on its command-line.
|
29 | ASRC =
|
30 |
|
31 | # Optimization level, can be [0, 1, 2, 3, s].
|
32 | # 0 = turn off optimization. s = optimize for size.
|
33 | # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
|
34 | OPT = s
|
35 |
|
36 | # Debugging format.
|
37 | # Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
|
38 | # AVR (extended) COFF requires stabs, plus an avr-objcopy run.
|
39 | DEBUG = dwarf-2
|
40 |
|
41 | # List any extra directories to look for include files here.
|
42 | # Each directory must be seperated by a space.
|
43 | EXTRAINCDIRS =
|
44 |
|
45 |
|
46 | # Compiler flag to set the C Standard level.
|
47 | # c89 - "ANSI" C
|
48 | # gnu89 - c89 plus GCC extensions
|
49 | # c99 - ISO C99 standard (not yet fully implemented)
|
50 | # gnu99 - c99 plus GCC extensions
|
51 | CSTANDARD = -std=gnu99
|
52 |
|
53 | # Place -D or -U options here
|
54 |
|
55 | # Set ur clock speed here
|
56 | CDEFS = -DF_CPU=16000000L
|
57 |
|
58 | # Place -I options here
|
59 | CINCS =
|
60 |
|
61 | # Compiler flags.
|
62 | # -g*: generate debugging information
|
63 | # -O*: optimization level
|
64 | # -f...: tuning, see GCC manual and avr-libc documentation
|
65 | # -Wall...: warning level
|
66 | # -Wa,...: tell GCC to pass this to the assembler.
|
67 | # -adhlns...: create assembler listing
|
68 | CFLAGS = -g$(DEBUG)
|
69 | CFLAGS += $(CDEFS) $(CINCS)
|
70 | CFLAGS += -O$(OPT)
|
71 | CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
|
72 | CFLAGS += -Wall -Wstrict-prototypes
|
73 | CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
|
74 | CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
|
75 | CFLAGS += $(CSTANDARD)
|
76 |
|
77 | ## Zusatz Flags
|
78 | CFLAGS += -fno-inline-small-functions
|
79 | CFLAGS += -fno-split-wide-types
|
80 | CFLAGS += -fno-tree-scev-cprop
|
81 | CFLAGS += -fno-move-loop-invariants
|
82 | CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections
|
83 | CFLAGS += -Wl,--relax
|
84 | ## -- ende --
|
85 |
|
86 | ## Extrem Flags
|
87 | #CFLAGS += -mtiny-stack
|
88 | ##
|
89 |
|
90 | # Assembler flags.
|
91 | # -Wa,...: tell GCC to pass this to the assembler.
|
92 | # -ahlms: create listing
|
93 | # -gstabs: have the assembler create line number information; note that
|
94 | # for use in COFF files, additional information about filenames
|
95 | # and function names needs to be present in the assembler source
|
96 | # files -- see avr-libc docs [FIXME: not yet described there]
|
97 | ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
|
98 |
|
99 | #Additional libraries.
|
100 |
|
101 | # Minimalistic printf version
|
102 | PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
|
103 |
|
104 | # Floating point printf version (requires MATH_LIB = -lm below)
|
105 | PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
|
106 |
|
107 | PRINTF_LIB =
|
108 |
|
109 | # Minimalistic scanf version
|
110 | SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
|
111 |
|
112 | # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
|
113 | SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
|
114 |
|
115 | SCANF_LIB =
|
116 |
|
117 | MATH_LIB = -lm
|
118 |
|
119 | # External memory options
|
120 |
|
121 | # 64 KB of external RAM, starting after internal RAM (ATmega128!),
|
122 | # used for variables (.data/.bss) and heap (malloc()).
|
123 | #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
|
124 |
|
125 | # 64 KB of external RAM, starting after internal RAM (ATmega128!),
|
126 | # only used for heap (malloc()).
|
127 | #EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
|
128 |
|
129 | EXTMEMOPTS =
|
130 |
|
131 | # Linker flags.
|
132 | # -Wl,...: tell GCC to pass this to linker.
|
133 | # -Map: create map file
|
134 | # --cref: add cross reference to map file
|
135 | LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
|
136 | LDFLAGS += $(EXTMEMOPTS)
|
137 | LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
|
138 | LDFLAGS += -Ttext=$(LOAD_ADDR)
|
139 | LDFLAGS += $(GLCD-LIB)
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | # Programming support using avrdude. Settings and variables.
|
145 |
|
146 | # Programming hardware: alf avr910 avrisp bascom bsd
|
147 | # dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
|
148 | #
|
149 | # Type: avrdude -c ?
|
150 | # to get a full listing.
|
151 | #
|
152 |
|
153 | # ARDUINO UNO, MEGA1280, NANO and so on
|
154 | #AVRDUDE_PROGRAMMER = stk500v1
|
155 |
|
156 | # ARDUINO MEGA2560
|
157 | AVRDUDE_PROGRAMMER = stk500v2
|
158 |
|
159 | # com1 = serial port. Use lpt1 to connect to parallel port.
|
160 |
|
161 | # UNO, NANO, MEGA1280 and so on
|
162 | #AVRDUDE_PORT = /dev/ttyUSB0 # programmer connected to serial device
|
163 |
|
164 | # MEGA2560
|
165 | AVRDUDE_PORT = /dev/ttyACM0 # programmer connected to serial device
|
166 |
|
167 | AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
|
168 | #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
|
169 |
|
170 |
|
171 | # Uncomment the following if you want avrdude's erase cycle counter.
|
172 | # Note that this counter needs to be initialized first using -Yn,
|
173 | # see avrdude manual.
|
174 | #AVRDUDE_ERASE_COUNTER = -y
|
175 |
|
176 | # Uncomment the following if you do /not/ wish a verification to be
|
177 | # performed after programming the device.
|
178 | #AVRDUDE_NO_VERIFY = -V
|
179 |
|
180 | # Increase verbosity level. Please use this when submitting bug
|
181 | # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
|
182 | # to submit bug reports.
|
183 | #AVRDUDE_VERBOSE = -v -v
|
184 |
|
185 | AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
|
186 | AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
|
187 | AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
|
188 | AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
|
189 |
|
190 | # MEGA2560
|
191 | AVRDUDE_FLAGS += -b115200
|
192 |
|
193 | # All other Arduino
|
194 | #AVRDUDE_FLAGS += -b57600
|
195 |
|
196 |
|
197 | # ---------------------------------------------------------------------------
|
198 |
|
199 | # Define directories, if needed.
|
200 | DIRAVR = c:/winavr
|
201 | DIRAVRBIN = $(DIRAVR)/bin
|
202 | DIRAVRUTILS = $(DIRAVR)/utils/bin
|
203 | DIRINC = .
|
204 | DIRLIB = $(DIRAVR)/avr/lib
|
205 |
|
206 |
|
207 | # Define programs and commands.
|
208 | SHELL = sh
|
209 | CC = avr-gcc
|
210 | OBJCOPY = avr-objcopy
|
211 | OBJDUMP = avr-objdump
|
212 | SIZE = avr-size
|
213 | NM = avr-nm
|
214 | AVRDUDE = avrdude
|
215 | REMOVE = rm -f
|
216 | COPY = cp
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | # Define Messages
|
222 | # English
|
223 | MSG_ERRORS_NONE = Errors: none
|
224 | MSG_BEGIN = -------- begin --------
|
225 | MSG_END = -------- end --------
|
226 | MSG_SIZE_BEFORE = Size before:
|
227 | MSG_SIZE_AFTER = Size after:
|
228 | MSG_COFF = Converting to AVR COFF:
|
229 | MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
|
230 | MSG_FLASH = Creating load file for Flash:
|
231 | MSG_EEPROM = Creating load file for EEPROM:
|
232 | MSG_EXTENDED_LISTING = Creating Extended Listing:
|
233 | MSG_SYMBOL_TABLE = Creating Symbol Table:
|
234 | MSG_LINKING = Linking:
|
235 | MSG_COMPILING = Compiling:
|
236 | MSG_ASSEMBLING = Assembling:
|
237 | MSG_CLEANING = Cleaning project:
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | # Define all object files.
|
243 | OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
|
244 |
|
245 | # Define all listing files.
|
246 | LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)
|
247 |
|
248 |
|
249 | # Compiler flags to generate dependency files.
|
250 | GENDEPFLAGS = -Wp,-M,-MP,-MT,$(*F).o,-MF,.dep/$(@F).d
|
251 |
|
252 | # Combine all necessary flags and optional flags.
|
253 | # Add target processor to flags.
|
254 | ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
|
255 | ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | # Default target.
|
262 | all: begin gccversion sizebefore build sizeafter finished end
|
263 |
|
264 | build: elf hex eep lss sym
|
265 |
|
266 | elf: $(TARGET).elf
|
267 | hex: $(TARGET).hex
|
268 | eep: $(TARGET).eep
|
269 | lss: $(TARGET).lss
|
270 | sym: $(TARGET).sym
|
271 |
|
272 |
|
273 |
|
274 | # Eye candy.
|
275 | # AVR Studio 3.x does not check make's exit code but relies on
|
276 | # the following magic strings to be generated by the compile job.
|
277 | begin:
|
278 | @echo
|
279 | @echo $(MSG_BEGIN)
|
280 |
|
281 | finished:
|
282 | @echo $(MSG_ERRORS_NONE)
|
283 |
|
284 | end:
|
285 | @echo $(MSG_END)
|
286 | @echo
|
287 |
|
288 |
|
289 | # Display size of file.
|
290 | HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
|
291 | ELFSIZE = $(SIZE) -A $(TARGET).elf
|
292 | sizebefore:
|
293 | @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi
|
294 |
|
295 | sizeafter:
|
296 | @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi
|
297 |
|
298 |
|
299 |
|
300 | # Display compiler version information.
|
301 | gccversion :
|
302 | @$(CC) --version
|
303 |
|
304 |
|
305 |
|
306 | # Program the device.
|
307 | program: $(TARGET).hex $(TARGET).eep
|
308 | $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
|
314 | COFFCONVERT=$(OBJCOPY) --debugging \
|
315 | --change-section-address .data-0x800000 \
|
316 | --change-section-address .bss-0x800000 \
|
317 | --change-section-address .noinit-0x800000 \
|
318 | --change-section-address .eeprom-0x810000
|
319 |
|
320 |
|
321 | coff: $(TARGET).elf
|
322 | @echo
|
323 | @echo $(MSG_COFF) $(TARGET).cof
|
324 | $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
|
325 |
|
326 |
|
327 | extcoff: $(TARGET).elf
|
328 | @echo
|
329 | @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
|
330 | $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
|
331 |
|
332 |
|
333 |
|
334 | # Create final output files (.hex, .eep) from ELF output file.
|
335 | %.hex: %.elf
|
336 | @echo
|
337 | @echo $(MSG_FLASH) $@
|
338 | $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
|
339 |
|
340 | %.eep: %.elf
|
341 | @echo
|
342 | @echo $(MSG_EEPROM) $@
|
343 | -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
|
344 | --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
|
345 |
|
346 | # Create extended listing file from ELF output file.
|
347 | %.lss: %.elf
|
348 | @echo
|
349 | @echo $(MSG_EXTENDED_LISTING) $@
|
350 | $(OBJDUMP) -h -S $< > $@
|
351 |
|
352 | # Create a symbol table from ELF output file.
|
353 | %.sym: %.elf
|
354 | @echo
|
355 | @echo $(MSG_SYMBOL_TABLE) $@
|
356 | $(NM) -n $< > $@
|
357 |
|
358 |
|
359 |
|
360 | # Link: create ELF output file from object files.
|
361 | .SECONDARY : $(TARGET).elf
|
362 | .PRECIOUS : $(OBJ)
|
363 | %.elf: $(OBJ)
|
364 | @echo
|
365 | @echo $(MSG_LINKING) $@
|
366 | $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)
|
367 |
|
368 |
|
369 | # Compile: create object files from C source files.
|
370 | %.o : %.c
|
371 | @echo
|
372 | @echo $(MSG_COMPILING) $<
|
373 | $(CC) -c $(ALL_CFLAGS) $< -o $@
|
374 |
|
375 |
|
376 | # Compile: create assembler files from C source files.
|
377 | %.s : %.c
|
378 | $(CC) -S $(ALL_CFLAGS) $< -o $@
|
379 |
|
380 |
|
381 | # Assemble: create object files from assembler source files.
|
382 | %.o : %.S
|
383 | @echo
|
384 | @echo $(MSG_ASSEMBLING) $<
|
385 | $(CC) -c $(ALL_ASFLAGS) $< -o $@
|
386 |
|
387 |
|
388 |
|
389 | # Target: clean project.
|
390 | clean: begin clean_list finished end
|
391 |
|
392 | clean_list :
|
393 | @echo
|
394 | @echo $(MSG_CLEANING)
|
395 | $(REMOVE) $(TARGET).hex
|
396 | $(REMOVE) $(TARGET).eep
|
397 | $(REMOVE) $(TARGET).obj
|
398 | $(REMOVE) $(TARGET).cof
|
399 | $(REMOVE) $(TARGET).elf
|
400 | $(REMOVE) $(TARGET).map
|
401 | $(REMOVE) $(TARGET).obj
|
402 | $(REMOVE) $(TARGET).a90
|
403 | $(REMOVE) $(TARGET).sym
|
404 | $(REMOVE) $(TARGET).lnk
|
405 | $(REMOVE) $(TARGET).lss
|
406 | $(REMOVE) $(OBJ)
|
407 | $(REMOVE) $(LST)
|
408 | $(REMOVE) $(SRC:.c=.s)
|
409 | $(REMOVE) $(SRC:.c=.d)
|
410 | $(REMOVE) .dep/*
|
411 | $(REMOVE) *~
|
412 |
|
413 |
|
414 |
|
415 | # Include the dependency files.
|
416 | -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
|
417 |
|
418 |
|
419 | # Listing of phony targets.
|
420 | .PHONY : all begin finish end sizebefore sizeafter gccversion \
|
421 | build elf hex eep lss sym coff extcoff \
|
422 | clean clean_list program
|