version 0.1 written by Akiba, taken from here:
[chibi.git] / demo / Makefile
blobbda84af9e29c673dc63c50833ddbafa04533d49f
1 # Hey Emacs, this is a -*- makefile -*-
2 #----------------------------------------------------------------------------
3 # WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al.
5 # Released to the Public Domain
7 # Additional material for this makefile was written by:
8 # Peter Fleury
9 # Tim Henigan
10 # Colin O'Flynn
11 # Reiner Patommel
12 # Markus Pfaff
13 # Sander Pool
14 # Frederik Rouleau
15 # Carlos Lamas
17 #----------------------------------------------------------------------------
18 # On command line:
20 # make all = Make software.
22 # make clean = Clean out built project files.
24 # make coff = Convert ELF to AVR COFF.
26 # make extcoff = Convert ELF to AVR Extended COFF.
28 # make program = Download the hex file to the device, using avrdude.
29 # Please customize the avrdude settings below first!
31 # make debug = Start either simulavr or avarice as specified for debugging,
32 # with avr-gdb or avr-insight as the front end for debugging.
34 # make filename.s = Just compile filename.c into the assembler code only.
36 # make filename.i = Create a preprocessed source file for use in submitting
37 # bug reports to the GCC project.
39 # To rebuild project do "make clean" then "make all".
40 #----------------------------------------------------------------------------
43 # MCU name
44 MCU = at90usb1286
47 # Processor frequency.
48 # This will define a symbol, F_CPU, in all source code files equal to the
49 # processor frequency. You can then use this symbol in your source code to
50 # calculate timings. Do NOT tack on a 'UL' at the end, this will be done
51 # automatically to create a 32-bit value in your source code.
52 # Typical values are:
53 # F_CPU = 1000000
54 # F_CPU = 1843200
55 # F_CPU = 2000000
56 # F_CPU = 3686400
57 # F_CPU = 4000000
58 # F_CPU = 7372800
59 # F_CPU = 8000000
60 # F_CPU = 11059200
61 # F_CPU = 14745600
62 # F_CPU = 16000000
63 # F_CPU = 18432000
64 # F_CPU = 20000000
65 F_CPU = 8000000
68 # Output format. (can be srec, ihex, binary)
69 FORMAT = ihex
72 # Target file name (without extension).
73 TARGET = chb_demo
76 # Object files directory
77 # To put object files in current directory, use a dot (.), do NOT make
78 # this an empty or blank macro!
79 OBJDIR = .
82 # List C source files here. (C dependencies are automatically generated.)
83 SRC = $(TARGET).c \
84 ../chibi/chb.c \
85 ../chibi/chb_drvr.c \
86 ../chibi/chb_buf.c \
87 ../chibi/chb_spi.c \
88 ../chibi/chb_eeprom.c \
89 ../freakusb/usb/usb.c \
90 ../freakusb/usb/ctrl.c \
91 ../freakusb/usb/usb_buf.c \
92 ../freakusb/class/cdc/desc.c \
93 ../freakusb/class/cdc/cdc.c \
94 ../freakusb/hw/at90usbxx6_7/ep.c \
95 ../freakusb/hw/at90usbxx6_7/hw.c \
96 ../freakusb/hw/at90usbxx6_7/isr.c
99 # List C++ source files here. (C dependencies are automatically generated.)
100 CPPSRC =
103 # List Assembler source files here.
104 # Make them always end in a capital .S. Files ending in a lowercase .s
105 # will not be considered source files but generated files (assembler
106 # output from the compiler), and will be deleted upon "make clean"!
107 # Even though the DOS/Win* filesystem matches both .s and .S the same,
108 # it will preserve the spelling of the filenames, and gcc itself does
109 # care about how the name is spelled on its command-line.
110 ASRC =
113 # Optimization level, can be [0, 1, 2, 3, s].
114 # 0 = turn off optimization. s = optimize for size.
115 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
116 OPT = s
119 # Debugging format.
120 # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
121 # AVR Studio 4.10 requires dwarf-2.
122 # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
123 DEBUG = dwarf-2
126 # List any extra directories to look for include files here.
127 # Each directory must be seperated by a space.
128 # Use forward slashes for directory separators.
129 # For a directory that has spaces, enclose it in quotes.
130 EXTRAINCDIRS = . ../chibi ../freakusb/usb ../freakusb/hw/at90usbxx6_7 ../freakusb/class/cdc
133 # Compiler flag to set the C Standard level.
134 # c89 = "ANSI" C
135 # gnu89 = c89 plus GCC extensions
136 # c99 = ISO C99 standard (not yet fully implemented)
137 # gnu99 = c99 plus GCC extensions
138 CSTANDARD = -std=gnu99
141 # Place -D or -U options here for C sources
142 CDEFS = -DF_CPU=$(F_CPU)UL -DCHB_DEBUG
145 # Place -D or -U options here for ASM sources
146 ADEFS = -DF_CPU=$(F_CPU)
149 # Place -D or -U options here for C++ sources
150 CPPDEFS = -DF_CPU=$(F_CPU)UL
151 #CPPDEFS += -D__STDC_LIMIT_MACROS
152 #CPPDEFS += -D__STDC_CONSTANT_MACROS
156 #---------------- Compiler Options C ----------------
157 # -g*: generate debugging information
158 # -O*: optimization level
159 # -f...: tuning, see GCC manual and avr-libc documentation
160 # -Wall...: warning level
161 # -Wa,...: tell GCC to pass this to the assembler.
162 # -adhlns...: create assembler listing
163 CFLAGS = -g$(DEBUG)
164 CFLAGS += $(CDEFS)
165 CFLAGS += -O$(OPT)
166 CFLAGS += -funsigned-char
167 CFLAGS += -funsigned-bitfields
168 CFLAGS += -fpack-struct
169 CFLAGS += -fshort-enums
170 CFLAGS += -Wall
171 #CFLAGS += -Wstrict-prototypes
172 #CFLAGS += -mshort-calls
173 #CFLAGS += -fno-unit-at-a-time
174 #CFLAGS += -Wundef
175 #CFLAGS += -Wunreachable-code
176 #CFLAGS += -Wsign-compare
177 CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
178 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
179 CFLAGS += $(CSTANDARD)
182 #---------------- Compiler Options C++ ----------------
183 # -g*: generate debugging information
184 # -O*: optimization level
185 # -f...: tuning, see GCC manual and avr-libc documentation
186 # -Wall...: warning level
187 # -Wa,...: tell GCC to pass this to the assembler.
188 # -adhlns...: create assembler listing
189 CPPFLAGS = -g$(DEBUG)
190 CPPFLAGS += $(CPPDEFS)
191 CPPFLAGS += -O$(OPT)
192 CPPFLAGS += -funsigned-char
193 CPPFLAGS += -funsigned-bitfields
194 CPPFLAGS += -fpack-struct
195 CPPFLAGS += -fshort-enums
196 CPPFLAGS += -fno-exceptions
197 CPPFLAGS += -Wall
198 CFLAGS += -Wundef
199 #CPPFLAGS += -mshort-calls
200 #CPPFLAGS += -fno-unit-at-a-time
201 #CPPFLAGS += -Wstrict-prototypes
202 #CPPFLAGS += -Wunreachable-code
203 #CPPFLAGS += -Wsign-compare
204 CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
205 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
206 #CPPFLAGS += $(CSTANDARD)
209 #---------------- Assembler Options ----------------
210 # -Wa,...: tell GCC to pass this to the assembler.
211 # -adhlns: create listing
212 # -gstabs: have the assembler create line number information; note that
213 # for use in COFF files, additional information about filenames
214 # and function names needs to be present in the assembler source
215 # files -- see avr-libc docs [FIXME: not yet described there]
216 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
217 # dump that will be displayed for a given single line of source input.
218 ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
221 #---------------- Library Options ----------------
222 # Minimalistic printf version
223 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
225 # Floating point printf version (requires MATH_LIB = -lm below)
226 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
228 # If this is left blank, then it will use the Standard printf version.
229 PRINTF_LIB =
230 #PRINTF_LIB = $(PRINTF_LIB_MIN)
231 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
234 # Minimalistic scanf version
235 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
237 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
238 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
240 # If this is left blank, then it will use the Standard scanf version.
241 SCANF_LIB =
242 #SCANF_LIB = $(SCANF_LIB_MIN)
243 #SCANF_LIB = $(SCANF_LIB_FLOAT)
246 MATH_LIB = -lm
249 # List any extra directories to look for libraries here.
250 # Each directory must be seperated by a space.
251 # Use forward slashes for directory separators.
252 # For a directory that has spaces, enclose it in quotes.
253 EXTRALIBDIRS =
257 #---------------- External Memory Options ----------------
259 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
260 # used for variables (.data/.bss) and heap (malloc()).
261 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
263 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
264 # only used for heap (malloc()).
265 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
267 EXTMEMOPTS =
271 #---------------- Linker Options ----------------
272 # -Wl,...: tell GCC to pass this to linker.
273 # -Map: create map file
274 # --cref: add cross reference to map file
275 LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
276 LDFLAGS += $(EXTMEMOPTS)
277 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
278 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
279 #LDFLAGS += -T linker_script.x
283 #---------------- Programming Options (avrdude) ----------------
285 # Programming hardware
286 # Type: avrdude -c ?
287 # to get a full listing.
289 AVRDUDE_PROGRAMMER = stk500v2
291 # com1 = serial port. Use lpt1 to connect to parallel port.
292 AVRDUDE_PORT = com1 # programmer connected to serial device
294 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
295 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
298 # Uncomment the following if you want avrdude's erase cycle counter.
299 # Note that this counter needs to be initialized first using -Yn,
300 # see avrdude manual.
301 #AVRDUDE_ERASE_COUNTER = -y
303 # Uncomment the following if you do /not/ wish a verification to be
304 # performed after programming the device.
305 #AVRDUDE_NO_VERIFY = -V
307 # Increase verbosity level. Please use this when submitting bug
308 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
309 # to submit bug reports.
310 #AVRDUDE_VERBOSE = -v -v
312 AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
313 AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
314 AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
315 AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
319 #---------------- Debugging Options ----------------
321 # For simulavr only - target MCU frequency.
322 DEBUG_MFREQ = $(F_CPU)
324 # Set the DEBUG_UI to either gdb or insight.
325 # DEBUG_UI = gdb
326 DEBUG_UI = insight
328 # Set the debugging back-end to either avarice, simulavr.
329 DEBUG_BACKEND = avarice
330 #DEBUG_BACKEND = simulavr
332 # GDB Init Filename.
333 GDBINIT_FILE = __avr_gdbinit
335 # When using avarice settings for the JTAG
336 JTAG_DEV = /dev/com1
338 # Debugging port used to communicate between GDB / avarice / simulavr.
339 DEBUG_PORT = 4242
341 # Debugging host used to communicate between GDB / avarice / simulavr, normally
342 # just set to localhost unless doing some sort of crazy debugging when
343 # avarice is running on a different computer.
344 DEBUG_HOST = localhost
348 #============================================================================
351 # Define programs and commands.
352 SHELL = sh
353 CC = avr-gcc
354 OBJCOPY = avr-objcopy
355 OBJDUMP = avr-objdump
356 SIZE = avr-size
357 AR = avr-ar rcs
358 NM = avr-nm
359 AVRDUDE = avrdude
360 REMOVE = rm -f
361 REMOVEDIR = rm -rf
362 COPY = cp
363 WINSHELL = cmd
366 # Define Messages
367 # English
368 MSG_ERRORS_NONE = Errors: none
369 MSG_BEGIN = -------- begin --------
370 MSG_END = -------- end --------
371 MSG_SIZE_BEFORE = Size before:
372 MSG_SIZE_AFTER = Size after:
373 MSG_COFF = Converting to AVR COFF:
374 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
375 MSG_FLASH = Creating load file for Flash:
376 MSG_EEPROM = Creating load file for EEPROM:
377 MSG_EXTENDED_LISTING = Creating Extended Listing:
378 MSG_SYMBOL_TABLE = Creating Symbol Table:
379 MSG_LINKING = Linking:
380 MSG_COMPILING = Compiling C:
381 MSG_COMPILING_CPP = Compiling C++:
382 MSG_ASSEMBLING = Assembling:
383 MSG_CLEANING = Cleaning project:
384 MSG_CREATING_LIBRARY = Creating library:
389 # Define all object files.
390 OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
392 # Define all listing files.
393 LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
396 # Compiler flags to generate dependency files.
397 GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
400 # Combine all necessary flags and optional flags.
401 # Add target processor to flags.
402 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
403 ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
404 ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
410 # Default target.
411 all: begin gccversion sizebefore build sizeafter end
413 # Change the build target to build a HEX file or a library.
414 build: elf hex eep lss sym
415 #build: lib
418 elf: $(TARGET).elf
419 hex: $(TARGET).hex
420 eep: $(TARGET).eep
421 lss: $(TARGET).lss
422 sym: $(TARGET).sym
423 LIBNAME=lib$(TARGET).a
424 lib: $(LIBNAME)
428 # Eye candy.
429 # AVR Studio 3.x does not check make's exit code but relies on
430 # the following magic strings to be generated by the compile job.
431 begin:
432 @echo
433 @echo $(MSG_BEGIN)
435 end:
436 @echo $(MSG_END)
437 @echo
440 # Display size of file.
441 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
442 ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
444 sizebefore:
445 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
446 2>/dev/null; echo; fi
448 sizeafter:
449 @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
450 2>/dev/null; echo; fi
454 # Display compiler version information.
455 gccversion :
456 @$(CC) --version
460 # Program the device.
461 program: $(TARGET).hex $(TARGET).eep
462 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
465 # Generate avr-gdb config/init file which does the following:
466 # define the reset signal, load the target file, connect to target, and set
467 # a breakpoint at main().
468 gdb-config:
469 @$(REMOVE) $(GDBINIT_FILE)
470 @echo define reset >> $(GDBINIT_FILE)
471 @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
472 @echo end >> $(GDBINIT_FILE)
473 @echo file $(TARGET).elf >> $(GDBINIT_FILE)
474 @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
475 ifeq ($(DEBUG_BACKEND),simulavr)
476 @echo load >> $(GDBINIT_FILE)
477 endif
478 @echo break main >> $(GDBINIT_FILE)
480 debug: gdb-config $(TARGET).elf
481 ifeq ($(DEBUG_BACKEND), avarice)
482 @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
483 @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
484 $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
485 @$(WINSHELL) /c pause
487 else
488 @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
489 $(DEBUG_MFREQ) --port $(DEBUG_PORT)
490 endif
491 @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
496 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
497 COFFCONVERT = $(OBJCOPY) --debugging
498 COFFCONVERT += --change-section-address .data-0x800000
499 COFFCONVERT += --change-section-address .bss-0x800000
500 COFFCONVERT += --change-section-address .noinit-0x800000
501 COFFCONVERT += --change-section-address .eeprom-0x810000
505 coff: $(TARGET).elf
506 @echo
507 @echo $(MSG_COFF) $(TARGET).cof
508 $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
511 extcoff: $(TARGET).elf
512 @echo
513 @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
514 $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
518 # Create final output files (.hex, .eep) from ELF output file.
519 %.hex: %.elf
520 @echo
521 @echo $(MSG_FLASH) $@
522 $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
524 %.eep: %.elf
525 @echo
526 @echo $(MSG_EEPROM) $@
527 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
528 --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
530 # Create extended listing file from ELF output file.
531 %.lss: %.elf
532 @echo
533 @echo $(MSG_EXTENDED_LISTING) $@
534 $(OBJDUMP) -h -S -z $< > $@
536 # Create a symbol table from ELF output file.
537 %.sym: %.elf
538 @echo
539 @echo $(MSG_SYMBOL_TABLE) $@
540 $(NM) -n $< > $@
544 # Create library from object files.
545 .SECONDARY : $(TARGET).a
546 .PRECIOUS : $(OBJ)
547 %.a: $(OBJ)
548 @echo
549 @echo $(MSG_CREATING_LIBRARY) $@
550 $(AR) $@ $(OBJ)
553 # Link: create ELF output file from object files.
554 .SECONDARY : $(TARGET).elf
555 .PRECIOUS : $(OBJ)
556 %.elf: $(OBJ)
557 @echo
558 @echo $(MSG_LINKING) $@
559 $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
562 # Compile: create object files from C source files.
563 $(OBJDIR)/%.o : %.c
564 @echo
565 @echo $(MSG_COMPILING) $<
566 $(CC) -c $(ALL_CFLAGS) $< -o $@
569 # Compile: create object files from C++ source files.
570 $(OBJDIR)/%.o : %.cpp
571 @echo
572 @echo $(MSG_COMPILING_CPP) $<
573 $(CC) -c $(ALL_CPPFLAGS) $< -o $@
576 # Compile: create assembler files from C source files.
577 %.s : %.c
578 $(CC) -S $(ALL_CFLAGS) $< -o $@
581 # Compile: create assembler files from C++ source files.
582 %.s : %.cpp
583 $(CC) -S $(ALL_CPPFLAGS) $< -o $@
586 # Assemble: create object files from assembler source files.
587 $(OBJDIR)/%.o : %.S
588 @echo
589 @echo $(MSG_ASSEMBLING) $<
590 $(CC) -c $(ALL_ASFLAGS) $< -o $@
593 # Create preprocessed source for use in sending a bug report.
594 %.i : %.c
595 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
598 # Target: clean project.
599 clean: begin clean_list end
601 clean_list :
602 @echo
603 @echo $(MSG_CLEANING)
604 $(REMOVE) $(TARGET).hex
605 $(REMOVE) $(TARGET).eep
606 $(REMOVE) $(TARGET).cof
607 $(REMOVE) $(TARGET).elf
608 $(REMOVE) $(TARGET).map
609 $(REMOVE) $(TARGET).sym
610 $(REMOVE) $(TARGET).lss
611 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
612 $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
613 $(REMOVE) $(SRC:.c=.s)
614 $(REMOVE) $(SRC:.c=.d)
615 $(REMOVE) $(SRC:.c=.i)
616 $(REMOVEDIR) .dep
619 # Create object files directory
620 $(shell mkdir $(OBJDIR) 2>/dev/null)
623 # Include the dependency files.
624 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
627 # Listing of phony targets.
628 .PHONY : all begin finish end sizebefore sizeafter gccversion \
629 build elf hex eep lss sym coff extcoff \
630 clean clean_list program debug gdb-config