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