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