Don't like ads? PRO users don't see any ads ;-)
Guest

arduino makefile

By: a guest on Jul 26th, 2011  |  syntax: None  |  size: 10.33 KB  |  hits: 96  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # Arduino 0022 Makefile
  2. # Arduino adaptation by mellis, eighthave, oli.keller, pyrou
  3. #
  4. # This makefile allows you to build sketches from the command line
  5. # without the Arduino environment (or Java).
  6. #
  7. # Detailed instructions for using the makefile:
  8. #
  9. #  1. Copy this file into the folder with your sketch. There should be a
  10. #     file with the same name as the folder and with the extension .pde
  11. #     (e.g. foo.pde in the foo/ folder).
  12. #
  13. #  2. Modify the line containg "INSTALL_DIR" to point to the directory that
  14. #     contains the Arduino installation (for example, under Mac OS X, this
  15. #     might be /Applications/Arduino.app).
  16. #
  17. #  3. Modify the line containing "PORT" to refer to the filename
  18. #     representing the USB or serial connection to your Arduino board
  19. #     (e.g. PORT = /dev/tty.USB0).  If the exact name of this file
  20. #     changes, you can use * as a wildcard (e.g. PORT = /dev/tty.usb*).
  21. #
  22. #  4. Set the line containing "MCU" to match your board's processor.
  23. #     Older one's are atmega8 based, newer ones like Arduino Mini, Bluetooth
  24. #     or Diecimila have the atmega168.  If you're using a LilyPad Arduino,
  25. #     change F_CPU to 8000000.
  26. #
  27. #  5. At the command line, change to the directory containing your
  28. #     program's file and the makefile.
  29. #
  30. #  6. Type "make" and press enter to compile/verify your program.
  31. #
  32. #  7. Type "make upload", reset your Arduino board, and press enter to
  33. #     upload your program to the Arduino board.
  34. #
  35. # $Id$
  36.  
  37.  
  38. # Default Environment vars
  39. # can be overridden
  40.  
  41. ARDUINO_PORT ?= /dev/ttyUSB0
  42. ARDUINO_UPLOAD_RATE ?= 57600
  43. ARDUINO_AVRDUDE_PROGRAMMER ?= stk500v1
  44. ARDUINO_MCU ?= atmega328p
  45. #ARDUINO_MCU ?= atmega168
  46. ARDUINO_F_CPU ?= 16000000
  47.  
  48.  
  49. TARGET = $(notdir $(CURDIR))
  50. INSTALL_DIR = $(HOME)/arduino/arduino-0022
  51. UPLOAD_RATE =           $(ARDUINO_UPLOAD_RATE)
  52. PORT =                  $(ARDUINO_PORT)
  53. AVRDUDE_PROGRAMMER =    $(ARDUINO_AVRDUDE_PROGRAMMER)
  54. MCU =                   $(ARDUINO_MCU)
  55. F_CPU =                 $(ARDUINO_F_CPU)
  56.  
  57. ############################################################################
  58. # Below here nothing should be changed...
  59.  
  60. VERSION=22
  61. ARDUINO = $(INSTALL_DIR)/hardware/arduino/cores/arduino
  62. #AVR_TOOLS_PATH = $(INSTALL_DIR)/hardware/tools/avr/bin
  63. AVR_TOOLS_PATH = /usr/bin
  64. AVRDUDE_PATH = $(INSTALL_DIR)/hardware/tools
  65. C_MODULES =  \
  66. $(ARDUINO)/wiring_pulse.c \
  67. $(ARDUINO)/wiring_analog.c \
  68. $(ARDUINO)/pins_arduino.c \
  69. $(ARDUINO)/wiring.c \
  70. $(ARDUINO)/wiring_digital.c \
  71. $(ARDUINO)/WInterrupts.c \
  72. $(ARDUINO)/wiring_shift.c \
  73. # end of C_MODULES
  74.  
  75. CXX_MODULES = \
  76. $(ARDUINO)/Tone.cpp \
  77. $(ARDUINO)/main.cpp \
  78. $(ARDUINO)/WMath.cpp \
  79. $(ARDUINO)/Print.cpp \
  80. $(ARDUINO)/HardwareSerial.cpp
  81.  
  82. ARDUINO_LIB = \
  83. $(wildcard $(INSTALL_DIR)/libraries/*/*.cpp) \
  84. $(wildcard $(INSTALL_DIR)/libraries/*/*/*.cpp) \
  85. $(wildcard $(INSTALL_DIR)/libraries/*/*/*/*.cpp) \
  86. $(wildcard $(INSTALL_DIR)/libraries/*/*.c) \
  87. $(wildcard $(INSTALL_DIR)/libraries/*/*/*.c) \
  88. $(wildcard $(INSTALL_DIR)/libraries/*/*/*/*.c)
  89.  
  90.  
  91. CXX_APP = applet/$(TARGET).cpp
  92. MODULES = $(C_MODULES) $(CXX_MODULES) $(ARDUINO_LIB)
  93. SRC = $(C_MODULES)
  94. CXXSRC = $(CXX_MODULES) $(CXX_APP) $(ARDUINO_LIB)
  95. FORMAT = ihex
  96.  
  97.  
  98. # Name of this Makefile (used for "make depend").
  99. MAKEFILE = Makefile
  100.  
  101. # Debugging format.
  102. # Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
  103. # AVR (extended) COFF requires stabs, plus an avr-objcopy run.
  104. #DEBUG = stabs
  105. DEBUG =
  106.  
  107. OPT = s
  108.  
  109. # Place -D or -U options here
  110. CDEFS = -DF_CPU=$(F_CPU)L -DARDUINO=$(VERSION)
  111. CXXDEFS = -DF_CPU=$(F_CPU)L -DARDUINO=$(VERSION)
  112.  
  113. AVR_HEADERS = $(INSTALL_DIR)/hardware/tools/avr/avr/include/avr
  114.  
  115. ARDUINO_LIB_INCLUDES = $(addprefix -I, $(dir $(ARDUINO_LIB)))
  116.  
  117. # Place -I options here
  118. CINCS = -I$(CURDIR) -I$(ARDUINO) $(ARDUINO_LIB_INCLUDES) -I$(AVR_HEADERS)
  119. CXXINCS = -I$(CURDIR) -I$(ARDUINO) $(ARDUINO_LIB_INCLUDES) -I$(AVR_HEADERS)
  120.  
  121. # Compiler flag to set the C Standard level.
  122. # c89   - "ANSI" C
  123. # gnu89 - c89 plus GCC extensions
  124. # c99   - ISO C99 standard (not yet fully implemented)
  125. # gnu99 - c99 plus GCC extensions
  126. #CSTANDARD = -std=gnu99
  127. CDEBUG = -g$(DEBUG)
  128. #CWARN = -Wall -Wstrict-prototypes
  129. #CWARN = -Wall   # show all warnings
  130. CWARN = -w      # suppress all warnings
  131. ####CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
  132. CTUNING = -ffunction-sections -fdata-sections
  133. CXXTUNING = -fno-exceptions -ffunction-sections -fdata-sections
  134. #CEXTRA = -Wa,-adhlns=$(<:.c=.lst)
  135.  
  136. CFLAGS = $(CDEBUG) -O$(OPT) $(CWARN) $(CTUNING) $(CDEFS) $(CINCS) $(CSTANDARD) $(CEXTRA)
  137. CXXFLAGS = $(CDEBUG) -O$(OPT) $(CWARN) $(CXXTUNING) $(CDEFS) $(CINCS)
  138. #ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
  139. LDFLAGS = -O$(OPT) -lm -Wl,--gc-sections
  140.  
  141.  
  142. # Programming support using avrdude. Settings and variables.
  143. AVRDUDE_PORT = $(PORT)
  144. AVRDUDE_WRITE_FLASH = -U flash:w:applet/$(TARGET).hex
  145.  
  146. #AVRDUDE_FLAGS = -V -F -C $(INSTALL_DIR)/hardware/tools/avr/etc/avrdude.conf \
  147.  
  148. AVRDUDE_FLAGS = -V -F -C $(INSTALL_DIR)/hardware/tools/avrdude.conf \
  149. -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \
  150. -b $(UPLOAD_RATE)
  151.  
  152. # Program settings
  153. CC = $(AVR_TOOLS_PATH)/avr-gcc
  154. CXX = $(AVR_TOOLS_PATH)/avr-g++
  155. LD = $(AVR_TOOLS_PATH)/avr-gcc
  156. OBJCOPY = $(AVR_TOOLS_PATH)/avr-objcopy
  157. OBJDUMP = $(AVR_TOOLS_PATH)/avr-objdump
  158. AR  = $(AVR_TOOLS_PATH)/avr-ar
  159. SIZE = $(AVR_TOOLS_PATH)/avr-size
  160. NM = $(AVR_TOOLS_PATH)/avr-nm
  161. AVRDUDE = $(AVRDUDE_PATH)/avrdude
  162. REMOVE = rm -f
  163. MV = mv -f
  164.  
  165. # Define all object files.
  166. OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o)
  167. OBJ_MODULES = $(C_MODULES:.c=.o) $(CXX_MODULES:.cpp=.o) $(ARDUINO_LIB:.cpp=.o)
  168.  
  169. # Define all listing files.
  170. LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst)
  171.  
  172. # Combine all necessary flags and optional flags.
  173. # Add target processor to flags.
  174. ALL_CFLAGS = $(CFLAGS) -mmcu=$(MCU)
  175. ALL_CXXFLAGS = $(CXXFLAGS) -mmcu=$(MCU)
  176. ALL_ASFLAGS = -x assembler-with-cpp $(ASFLAGS) -mmcu=$(MCU)
  177. ALL_LDFLAGS = $(LDFLAGS) -mmcu=$(MCU)
  178.  
  179. # Default target.
  180. all: applet_files build sizeafter
  181.  
  182. build: elf hex
  183.  
  184. ##
  185. #applet_files: $(TARGET).pde
  186. #       # Here is the "preprocessing".
  187. #       # It creates a .cpp file based with the same name as the .pde file.
  188. #       # On top of the new .cpp file comes the WProgram.h header.
  189. #       # At the end there is a generic main() function attached,
  190. #       # plus special magic to get around the pure virtual error
  191. #       # undefined reference to `__cxa_pure_virtual' from Print.o.
  192. #       # Then the .cpp file will be compiled. Errors during compile will
  193. #       # refer to this new, automatically generated, file.
  194. #       # Not the original .pde file you actually edit...
  195. #       test -d applet || mkdir applet
  196. #       echo '#include "WProgram.h"' > applet/$(TARGET).cpp
  197. #       cat $(TARGET).pde >> applet/$(TARGET).cpp
  198. #       echo 'extern "C" void __cxa_pure_virtual() { while (1) ; }' >> applet/$(TARGET).cpp
  199. #       cat $(ARDUINO_CORE)/main.cpp >> applet/$(TARGET).cpp
  200.  
  201. ##
  202. #applet_files: $(TARGET).pde
  203. applet/$(TARGET).cpp: $(TARGET).pde
  204.         # Here is the "preprocessing".
  205.         # It creates a .cpp file based with the same name as the .pde file.
  206.         # On top of the new .cpp file comes the WProgram.h header.
  207.         # and prototypes for setup() and Loop()
  208.         # Then the .cpp file will be compiled. Errors during compile will
  209.         # refer to this new, automatically generated, file.
  210.         # Not the original .pde file you actually edit...
  211.         test -d applet || mkdir applet
  212.         echo '#include "WProgram.h"' > applet/$(TARGET).cpp
  213.         echo 'void setup();' >> applet/$(TARGET).cpp
  214.         echo 'void loop();' >> applet/$(TARGET).cpp
  215.         echo 'extern "C" void __cxa_pure_virtual() { while (1) ; }' >> applet/$(TARGET).cpp
  216.         cat $(TARGET).pde >> applet/$(TARGET).cpp
  217.  
  218. elf: applet/$(TARGET).elf
  219. hex: applet/$(TARGET).hex
  220. eep: applet/$(TARGET).eep
  221. lss: applet/$(TARGET).lss
  222. sym: applet/$(TARGET).sym
  223.  
  224. # Program the device.  
  225. upload: applet/$(TARGET).hex
  226.         $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)
  227.  
  228.  
  229.         # Display size of file.
  230. HEXSIZE = $(SIZE) --target=$(FORMAT) applet/$(TARGET).hex
  231. ELFSIZE = $(SIZE)  applet/$(TARGET).elf
  232. sizebefore:
  233.         @if [ -f applet/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(HEXSIZE); echo; fi
  234.  
  235. sizeafter:
  236.         @if [ -f applet/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(HEXSIZE); echo; fi
  237.  
  238.  
  239. # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
  240. COFFCONVERT=$(OBJCOPY) --debugging \
  241. --change-section-address .data-0x800000 \
  242. --change-section-address .bss-0x800000 \
  243. --change-section-address .noinit-0x800000 \
  244. --change-section-address .eeprom-0x810000
  245.  
  246.  
  247. coff: applet/$(TARGET).elf
  248.         $(COFFCONVERT) -O coff-avr applet/$(TARGET).elf $(TARGET).cof
  249.  
  250.  
  251. extcoff: $(TARGET).elf
  252.         $(COFFCONVERT) -O coff-ext-avr applet/$(TARGET).elf $(TARGET).cof
  253.  
  254.  
  255. .SUFFIXES: .elf .hex .eep .lss .sym
  256.  
  257. .elf.hex:
  258.         $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
  259.  
  260. .elf.eep:
  261.         $(OBJCOPY) -O $(FORMAT) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
  262.         --no-change-warnings \
  263.         --change-section-lma .eeprom=0 $< $@
  264.  
  265. # Create extended listing file from ELF output file.
  266. .elf.lss:
  267.         $(OBJDUMP) -h -S $< > $@
  268.  
  269. # Create a symbol table from ELF output file.
  270. .elf.sym:
  271.         $(NM) -n $< > $@
  272.  
  273.         # Link: create ELF output file from library.
  274. #applet/$(TARGET).elf: $(TARGET).pde applet/core.a
  275. applet/$(TARGET).elf: applet/$(TARGET).o applet/core.a
  276.         $(LD) $(ALL_LDFLAGS) -o $@ applet/$(TARGET).o applet/core.a
  277.  
  278. applet/core.a: $(OBJ_MODULES)
  279.         @for i in $(OBJ_MODULES); do echo $(AR) rcs applet/core.a $$i; $(AR) rcs applet/core.a $$i; done
  280.  
  281.  
  282.  
  283. # Compile: create object files from C++ source files.
  284. .cpp.o:
  285.         $(CXX) -c $(ALL_CXXFLAGS) $< -o $@
  286.  
  287. # Compile: create object files from C source files.
  288. .c.o:
  289.         $(CC) -c $(ALL_CFLAGS) $< -o $@
  290.  
  291.  
  292. # Compile: create assembler files from C source files.
  293. .c.s:
  294.         $(CC) -S $(ALL_CFLAGS) $< -o $@
  295.  
  296.  
  297. # Assemble: create object files from assembler source files.
  298. .S.o:
  299.         $(CC) -c $(ALL_ASFLAGS) $< -o $@
  300.  
  301.  
  302. # Automatic dependencies
  303. %.d: %.c
  304.         $(CC) -M $(ALL_CFLAGS) $< | sed "s;$(notdir $*).o:;$*.o $*.d:;" > $@
  305.  
  306. %.d: %.cpp
  307.         $(CXX) -M $(ALL_CXXFLAGS) $< | sed "s;$(notdir $*).o:;$*.o $*.d:;" > $@
  308.  
  309.  
  310. # Target: clean project.
  311. clean:
  312.         $(REMOVE) applet/$(TARGET).hex applet/$(TARGET).eep applet/$(TARGET).cof applet/$(TARGET).elf \
  313.         applet/$(TARGET).map applet/$(TARGET).sym applet/$(TARGET).lss applet/core.a \
  314.         $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d)
  315.  
  316. .PHONY: all build elf hex eep lss sym program coff extcoff clean applet_files sizebefore sizeafter
  317.  
  318. #include $(SRC:.c=.d)
  319. #include $(CXXSRC:.cpp=.d)