Advertisement
Guest User

Arduino Makefile

a guest
Mar 7th, 2011
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 9.42 KB | None | 0 0
  1. # Arduino 0021 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/tty.usbserial-*
  42. ARDUINO_PORT ?= /dev/tty.usb*
  43. ARDUINO_UPLOAD_RATE ?= 115200
  44. # ARDUINO_UPLOAD_RATE ?= 57600
  45. ARDUINO_AVRDUDE_PROGRAMMER ?= stk500v1
  46. # ARDUINO_AVRDUDE_PROGRAMMER ?= stk500v2
  47. ARDUINO_MCU ?= atmega328p
  48. #ARDUINO_MCU ?= atmega168
  49. ARDUINO_F_CPU ?= 16000000
  50.  
  51.  
  52. TARGET = $(notdir $(CURDIR))
  53. INSTALL_DIR = /Applications/Arduino.app/Contents/Resources/Java
  54. UPLOAD_RATE =       $(ARDUINO_UPLOAD_RATE)
  55. PORT =          $(ARDUINO_PORT)
  56. AVRDUDE_PROGRAMMER =    $(ARDUINO_AVRDUDE_PROGRAMMER)
  57. MCU =           $(ARDUINO_MCU)
  58. F_CPU =         $(ARDUINO_F_CPU)
  59.  
  60. ############################################################################
  61. # Below here nothing should be changed...
  62.  
  63. VERSION=21
  64. ARDUINO = $(INSTALL_DIR)/hardware/arduino/cores/arduino
  65. AVR_TOOLS_PATH = $(INSTALL_DIR)/hardware/tools/avr/bin
  66. # AVR_TOOLS_PATH = /usr/bin
  67. AVRDUDE_PATH = $(INSTALL_DIR)/hardware/tools/avr/bin
  68. C_MODULES =  \
  69. $(ARDUINO)/wiring_pulse.c \
  70. $(ARDUINO)/wiring_analog.c \
  71. $(ARDUINO)/pins_arduino.c \
  72. $(ARDUINO)/wiring.c \
  73. $(ARDUINO)/wiring_digital.c \
  74. $(ARDUINO)/WInterrupts.c \
  75. $(ARDUINO)/wiring_shift.c \
  76. # end of C_MODULES
  77.  
  78. CXX_MODULES = \
  79. $(ARDUINO)/Tone.cpp \
  80. $(ARDUINO)/main.cpp \
  81. $(ARDUINO)/WMath.cpp \
  82. $(ARDUINO)/Print.cpp \
  83. $(ARDUINO)/HardwareSerial.cpp
  84.  
  85. ARDUINO_LIB = \
  86. $(wildcard $(INSTALL_DIR)/libraries/*/*.cpp) \
  87. $(wildcard $(INSTALL_DIR)/libraries/*/*/*.cpp) \
  88. $(wildcard $(INSTALL_DIR)/libraries/*/*/*/*.cpp)
  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/avr/etc/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. #applet_files: $(TARGET).pde
  185. applet/$(TARGET).cpp: $(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.     # and prototypes for setup() and Loop()
  190.     # Then the .cpp file will be compiled. Errors during compile will
  191.     # refer to this new, automatically generated, file.
  192.     # Not the original .pde file you actually edit...
  193.     test -d applet || mkdir applet
  194.     echo '#include "WProgram.h"' > applet/$(TARGET).cpp
  195.     echo 'void setup();' >> applet/$(TARGET).cpp
  196.     echo 'void loop();' >> applet/$(TARGET).cpp
  197.     cat $(TARGET).pde >> applet/$(TARGET).cpp
  198.  
  199. elf: applet/$(TARGET).elf
  200. hex: applet/$(TARGET).hex
  201. eep: applet/$(TARGET).eep
  202. lss: applet/$(TARGET).lss
  203. sym: applet/$(TARGET).sym
  204.  
  205. # Program the device.  
  206. upload: applet/$(TARGET).hex
  207.     $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)
  208.  
  209.  
  210.     # Display size of file.
  211. HEXSIZE = $(SIZE) --target=$(FORMAT) applet/$(TARGET).hex
  212. ELFSIZE = $(SIZE)  applet/$(TARGET).elf
  213. sizebefore:
  214.     @if [ -f applet/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(HEXSIZE); echo; fi
  215.  
  216. sizeafter:
  217.     @if [ -f applet/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(HEXSIZE); echo; fi
  218.  
  219.  
  220. # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
  221. COFFCONVERT=$(OBJCOPY) --debugging \
  222. --change-section-address .data-0x800000 \
  223. --change-section-address .bss-0x800000 \
  224. --change-section-address .noinit-0x800000 \
  225. --change-section-address .eeprom-0x810000
  226.  
  227.  
  228. coff: applet/$(TARGET).elf
  229.     $(COFFCONVERT) -O coff-avr applet/$(TARGET).elf $(TARGET).cof
  230.  
  231.  
  232. extcoff: $(TARGET).elf
  233.     $(COFFCONVERT) -O coff-ext-avr applet/$(TARGET).elf $(TARGET).cof
  234.  
  235.  
  236. .SUFFIXES: .elf .hex .eep .lss .sym
  237.  
  238. .elf.hex:
  239.     $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
  240.  
  241. .elf.eep:
  242.     $(OBJCOPY) -O $(FORMAT) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
  243.     --no-change-warnings \
  244.     --change-section-lma .eeprom=0 $< $@
  245.  
  246. # Create extended listing file from ELF output file.
  247. .elf.lss:
  248.     $(OBJDUMP) -h -S $< > $@
  249.  
  250. # Create a symbol table from ELF output file.
  251. .elf.sym:
  252.     $(NM) -n $< > $@
  253.  
  254.     # Link: create ELF output file from library.
  255. #applet/$(TARGET).elf: $(TARGET).pde applet/core.a
  256. applet/$(TARGET).elf: applet/$(TARGET).o applet/core.a
  257.     $(LD) $(ALL_LDFLAGS) -o $@ applet/$(TARGET).o applet/core.a
  258.  
  259. applet/core.a: $(OBJ_MODULES)
  260.     @for i in $(OBJ_MODULES); do echo $(AR) rcs applet/core.a $$i; $(AR) rcs applet/core.a $$i; done
  261.  
  262.  
  263.  
  264. # Compile: create object files from C++ source files.
  265. .cpp.o:
  266.     $(CXX) -c $(ALL_CXXFLAGS) $< -o $@
  267.  
  268. # Compile: create object files from C source files.
  269. .c.o:
  270.     $(CC) -c $(ALL_CFLAGS) $< -o $@
  271.  
  272.  
  273. # Compile: create assembler files from C source files.
  274. .c.s:
  275.     $(CC) -S $(ALL_CFLAGS) $< -o $@
  276.  
  277.  
  278. # Assemble: create object files from assembler source files.
  279. .S.o:
  280.     $(CC) -c $(ALL_ASFLAGS) $< -o $@
  281.  
  282.  
  283. # Automatic dependencies
  284. %.d: %.c
  285.     $(CC) -M $(ALL_CFLAGS) $< | sed "s;$(notdir $*).o:;$*.o $*.d:;" > $@
  286.  
  287. %.d: %.cpp
  288.     $(CXX) -M $(ALL_CXXFLAGS) $< | sed "s;$(notdir $*).o:;$*.o $*.d:;" > $@
  289.  
  290.  
  291. # Target: clean project.
  292. clean:
  293.     $(REMOVE) applet/$(TARGET).hex applet/$(TARGET).eep applet/$(TARGET).cof applet/$(TARGET).elf \
  294.     applet/$(TARGET).map applet/$(TARGET).sym applet/$(TARGET).lss applet/core.a \
  295.     $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d)
  296.  
  297. .PHONY: all build elf hex eep lss sym program coff extcoff clean applet_files sizebefore sizeafter
  298.  
  299. #include $(SRC:.c=.d)
  300. #include $(CXXSRC:.cpp=.d)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement