1 TARGET = $(notdir $(CURDIR)) 2 INSTALL_DIR = ../arduino-1.0.5 3 PORT = /dev/ttyUSB0 4 UPLOAD_RATE = 57600 # 19200 5 AVRDUDE_PROGRAMMER = stk500v1 6 MCU = atmega328p # atmega168 7 F_CPU = 16000000 8 9 EXTRA_SRC = 10 EXTRA_CXXSRC = 11 EXTRA_CINCS = 12 EXTRA_CXXINCS = 13 14 15 ### Internal definitions. 16 17 18 ARDUINO = $(INSTALL_DIR)/hardware/arduino/cores/arduino 19 VARIANT = $(INSTALL_DIR)/hardware/arduino/variants/standard 20 SRC = $(ARDUINO)/wiring.c $(ARDUINO)/wiring_digital.c \ 21 $(EXTRA_SRC) 22 CXXSRC = $(ARDUINO)/HardwareSerial.cpp \ 23 $(ARDUINO)/Print.cpp $(ARDUINO)/WString.cpp \ 24 $(ARDUINO)/Stream.cpp $(EXTRA_CXXSRC) 25 FORMAT = ihex 26 27 # Name of this Makefile (used for "make depend"). 28 MAKEFILE = Makefile 29 30 # Debugging format. 31 # Native formats for AVR-GCC's -g are stabs [default], or dwarf-2. 32 # AVR (extended) COFF requires stabs, plus an avr-objcopy run. 33 DEBUG = stabs 34 35 # Place -D or -U options here 36 CDEFS = -DF_CPU=$(F_CPU) 37 CXXDEFS = -DF_CPU=$(F_CPU) 38 39 # Place -I options here 40 CINCS = -I$(ARDUINO) -I$(VARIANT) $(EXTRA_CINCS) 41 CXXINCS = -I$(ARDUINO) -I$(VARIANT) $(EXTRA_CXXINCS) 42 43 # Compiler flag to set the C Standard level. 44 # c89 - "ANSI" C 45 # gnu89 - c89 plus GCC extensions 46 # c99 - ISO C99 standard (not yet fully implemented) 47 # gnu99 - c99 plus GCC extensions 48 CSTANDARD = -std=gnu99 49 CDEBUG = -g$(DEBUG) 50 CWARN = -Wall -Wstrict-prototypes 51 52 OPT = s 53 54 CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) 55 CXXFLAGS = $(CXXDEFS) $(CXXINCS) -O$(OPT) -ffunction-sections -fdata-sections 56 LDFLAGS = -lm -Wl,--gc-sections 57 58 # Combine all necessary flags and optional flags. 59 # Add target processor to flags. 60 ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) 61 ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CXXFLAGS) 62 63 # Programming support using avrdude. Settings and variables. 64 AVRDUDE_PORT = $(PORT) 65 AVRDUDE_WRITE_FLASH = -U flash:w:applet/$(TARGET).hex 66 AVRDUDE_FLAGS = -V -F -C /etc/avrdude.conf \ 67 -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \ 68 -b $(UPLOAD_RATE) 69 70 # Program settings 71 CC = avr-gcc 72 CXX = avr-g++ 73 LD = avr-ld 74 OBJCOPY = avr-objcopy 75 OBJDUMP = avr-objdump 76 AR = avr-ar 77 SIZE = avr-size 78 NM = avr-nm 79 AVRDUDE = avrdude 80 REMOVE = rm -f 81 MV = mv -f 82 83 # Define all object files. 84 OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) 85 86 # Default target. 87 all: applet_files build sizeafter 88 89 build: elf hex 90 91 applet_files: $(TARGET).cpp 92 test -d applet || mkdir applet 93 cat $(ARDUINO)/main.cpp > applet/$(TARGET).cpp 94 cat $(TARGET).cpp >> applet/$(TARGET).cpp 95 96 elf: applet/$(TARGET).elf 97 hex: applet/$(TARGET).hex 98 99 # Program the device. 100 upload: applet/$(TARGET).hex 101 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) 102 103 # Display size of file. 104 HEXSIZE = $(SIZE) --target=$(FORMAT) applet/$(TARGET).hex 105 ELFSIZE = $(SIZE) applet/$(TARGET).elf 106 107 sizebefore: 108 @if [ -f applet/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(HEXSIZE); echo; fi 109 110 sizeafter: 111 @if [ -f applet/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(HEXSIZE); echo; fi 112 113 114 ### File-specific processing. 115 116 117 .SUFFIXES: .elf .hex .eep .lss .sym 118 119 .elf.hex: 120 $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ 121 122 applet/$(TARGET).elf: applet/$(TARGET).cpp applet/core.a 123 $(CXX) $(ALL_CXXFLAGS) -o $@ applet/$(TARGET).cpp -L. applet/core.a $(LDFLAGS) 124 125 applet/core.a: $(OBJ) 126 @for i in $(OBJ); do echo $(AR) rcs applet/core.a $$i; $(AR) rcs applet/core.a $$i; done 127 128 # Compile: create object files from C++ source files. 129 .cpp.o: 130 $(CXX) -c $(ALL_CXXFLAGS) $< -o $@ 131 132 # Compile: create object files from C source files. 133 .c.o: 134 $(CC) -c $(ALL_CFLAGS) $< -o $@ 135 136 # Target: clean project. 137 clean: 138 $(REMOVE) applet/$(TARGET).hex applet/$(TARGET).elf applet/$(TARGET).cpp \ 139 applet/$(TARGET).map applet/core.a $(OBJ) 140 141 .PHONY: all build elf hex eep lss sym program coff extcoff clean applet_files sizebefore sizeafter