Advertisement
Guest User

AVR Makefile

a guest
Jun 28th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 11.13 KB | None | 0 0
  1. # Hey Emacs, this is a -*- makefile -*-
  2. #
  3. # WinAVR makefile written by Eric B. Weddington, Jörg Wunsch, et al.
  4. # Released to the Public Domain
  5. # Please read the make user manual!
  6. #
  7. # Additional material for this makefile was submitted by:
  8. #  Tim Henigan
  9. #  Peter Fleury
  10. #  Reiner Patommel
  11. #  Sander Pool
  12. #  Frederik Rouleau
  13. #  Markus Pfaff
  14. #
  15. # On command line:
  16. #
  17. # make all = Make software.
  18. #
  19. # make clean = Clean out built project files.
  20. #
  21. # make coff = Convert ELF to AVR COFF (for use with AVR Studio 3.x or VMLAB).
  22. #
  23. # make extcoff = Convert ELF to AVR Extended COFF (for use with AVR Studio
  24. #                4.07 or greater).
  25. #
  26. # make program = Download the hex file to the device, using avrdude.  Please
  27. #                customize the avrdude settings below first!
  28. #
  29. # make filename.s = Just compile filename.c into the assembler code only
  30. #
  31. # To rebuild project do "make clean" then "make all".
  32. #
  33.  
  34. # mth 2004/09
  35. # Differences from WinAVR 20040720 sample:
  36. # - DEPFLAGS according to Eric Weddingtion's fix (avrfreaks/gcc-forum)
  37. # - F_OSC Define in CFLAGS and AFLAGS
  38.  
  39.  
  40. # MCU name
  41. MCU = atmega328p
  42.  
  43. # Main Oscillator Frequency
  44. # This is only used to define F_OSC in all assembler and c-sources.
  45. F_CPU = 16000000
  46.  
  47. # Output format. (can be srec, ihex, binary)
  48. FORMAT = ihex
  49.  
  50. # Target file name (without extension).
  51. TARGET = main
  52.  
  53.  
  54. # List C source files here. (C dependencies are automatically generated.)
  55. SRC = lcd-routines.c  $(TARGET).c
  56.  
  57.  
  58. # List Assembler source files here.
  59. # Make them always end in a capital .S.  Files ending in a lowercase .s
  60. # will not be considered source files but generated files (assembler
  61. # output from the compiler), and will be deleted upon "make clean"!
  62. # Even though the DOS/Win* filesystem matches both .s and .S the same,
  63. # it will preserve the spelling of the filenames, and gcc itself does
  64. # care about how the name is spelled on its command-line.
  65. ASRC =
  66.  
  67.  
  68.  
  69. # Optimization level, can be [0, 1, 2, 3, s].
  70. # 0 = turn off optimization. s = optimize for size.
  71. # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
  72. OPT = s
  73.  
  74. # Debugging format.
  75. # Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
  76. # AVR (extended) COFF requires stabs, plus an avr-objcopy run.
  77. #DEBUG = stabs
  78. DEBUG = dwarf-2
  79.  
  80. # List any extra directories to look for include files here.
  81. #     Each directory must be seperated by a space.
  82. EXTRAINCDIRS =
  83.  
  84.  
  85. # Compiler flag to set the C Standard level.
  86. # c89   - "ANSI" C
  87. # gnu89 - c89 plus GCC extensions
  88. # c99   - ISO C99 standard (not yet fully implemented)
  89. # gnu99 - c99 plus GCC extensions
  90. CSTANDARD = -std=gnu99
  91.  
  92. # Place -D or -U options here
  93. CDEFS =
  94.  
  95. # Place -I options here
  96. CINCS =
  97.  
  98.  
  99. # Compiler flags.
  100. #  -g*:          generate debugging information
  101. #  -O*:          optimization level
  102. #  -f...:        tuning, see GCC manual and avr-libc documentation
  103. #  -Wall...:     warning level
  104. #  -Wa,...:      tell GCC to pass this to the assembler.
  105. #    -adhlns...: create assembler listing
  106. CFLAGS = -g$(DEBUG)
  107. CFLAGS += $(CDEFS) $(CINCS)
  108. CFLAGS += -O$(OPT)
  109. CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
  110. CFLAGS += -Wall -Wstrict-prototypes
  111. CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
  112. CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
  113. CFLAGS += $(CSTANDARD)
  114. CFLAGS += -DF_OSC=$(F_OSC)
  115.  
  116.  
  117.  
  118. # Assembler flags.
  119. #  -Wa,...:   tell GCC to pass this to the assembler.
  120. #  -ahlms:    create listing
  121. #  -gstabs:   have the assembler create line number information; note that
  122. #             for use in COFF files, additional information about filenames
  123. #             and function names needs to be present in the assembler source
  124. #             files -- see avr-libc docs [FIXME: not yet described there]
  125. ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
  126. ASFLAGS += -DF_OSC=$(F_OSC)
  127.  
  128.  
  129. #Additional libraries.
  130.  
  131. # Minimalistic printf version
  132. PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
  133.  
  134. # Floating point printf version (requires MATH_LIB = -lm below)
  135. PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
  136.  
  137. PRINTF_LIB =
  138.  
  139. # Minimalistic scanf version
  140. SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
  141.  
  142. # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
  143. SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
  144.  
  145. SCANF_LIB =
  146.  
  147. MATH_LIB = -lm
  148.  
  149. # External memory options
  150.  
  151. # 64 KB of external RAM, starting after internal RAM (ATmega128!),
  152. # used for variables (.data/.bss) and heap (malloc()).
  153. #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
  154.  
  155. # 64 KB of external RAM, starting after internal RAM (ATmega128!),
  156. # only used for heap (malloc()).
  157. #EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
  158.  
  159. EXTMEMOPTS =
  160.  
  161. # Linker flags.
  162. #  -Wl,...:     tell GCC to pass this to linker.
  163. #    -Map:      create map file
  164. #    --cref:    add cross reference to  map file
  165. LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
  166. LDFLAGS += $(EXTMEMOPTS)
  167. LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
  168.  
  169.  
  170.  
  171.  
  172.  
  173. # Programming support using avrdude. Settings and variables.
  174.  
  175. # Programming hardware: alf avr910 avrisp bascom bsd
  176. # dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
  177. #
  178. # Type: avrdude -c ?
  179. # to get a full listing.
  180. #
  181. AVRDUDE_PROGRAMMER = arduino
  182.  
  183. # com1 = serial port. Use lpt1 to connect to parallel port.
  184. AVRDUDE_PORT = /dev/ttyUSB0    # programmer connected to serial device
  185.  
  186. # baudrate for programmer
  187. AVRDUDE_BAUDRATE = 57600
  188.  
  189. AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
  190. #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
  191.  
  192.  
  193. # Uncomment the following if you want avrdude's erase cycle counter.
  194. # Note that this counter needs to be initialized first using -Yn,
  195. # see avrdude manual.
  196. #AVRDUDE_ERASE_COUNTER = -y
  197.  
  198. # Uncomment the following if you do /not/ wish a verification to be
  199. # performed after programming the device.
  200. #AVRDUDE_NO_VERIFY = -V
  201.  
  202. # Increase verbosity level.  Please use this when submitting bug
  203. # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
  204. # to submit bug reports.
  205. #AVRDUDE_VERBOSE = -v -v
  206.  
  207. AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) -b $(AVRDUDE_BAUDRATE)
  208. AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
  209. AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
  210. AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
  211.  
  212.  
  213.  
  214. # ---------------------------------------------------------------------------
  215.  
  216. # Define directories, if needed.
  217. DIRAVR = c:/winavr
  218. DIRAVRBIN = $(DIRAVR)/bin
  219. DIRAVRUTILS = $(DIRAVR)/utils/bin
  220. DIRINC = .
  221. DIRLIB = $(DIRAVR)/avr/lib
  222.  
  223.  
  224. # Define programs and commands.
  225. SHELL = sh
  226. CC = avr-gcc
  227. OBJCOPY = avr-objcopy
  228. OBJDUMP = avr-objdump
  229. SIZE = avr-size
  230. NM = avr-nm
  231. AVRDUDE = avrdude
  232. REMOVE = rm -f
  233. COPY = cp
  234.  
  235.  
  236.  
  237.  
  238. # Define Messages
  239. # English
  240. MSG_ERRORS_NONE = Errors: none
  241. MSG_BEGIN = -------- begin --------
  242. MSG_END = --------  end  --------
  243. MSG_SIZE_BEFORE = Size before:
  244. MSG_SIZE_AFTER = Size after:
  245. MSG_COFF = Converting to AVR COFF:
  246. MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
  247. MSG_FLASH = Creating load file for Flash:
  248. MSG_EEPROM = Creating load file for EEPROM:
  249. MSG_EXTENDED_LISTING = Creating Extended Listing:
  250. MSG_SYMBOL_TABLE = Creating Symbol Table:
  251. MSG_LINKING = Linking:
  252. MSG_COMPILING = Compiling:
  253. MSG_ASSEMBLING = Assembling:
  254. MSG_CLEANING = Cleaning project:
  255.  
  256.  
  257.  
  258.  
  259. # Define all object files.
  260. OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
  261.  
  262. # Define all listing files.
  263. LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)
  264.  
  265.  
  266. # Compiler flags to generate dependency files.
  267. ### GENDEPFLAGS = -Wp,-M,-MP,-MT,$(*F).o,-MF,.dep/$(@F).d
  268. GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d
  269.  
  270. # Combine all necessary flags and optional flags.
  271. # Add target processor to flags.
  272. ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
  273. ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
  274.  
  275.  
  276.  
  277.  
  278.  
  279. # Default target.
  280. all: begin gccversion sizebefore build sizeafter finished end
  281.  
  282. build: elf hex eep lss sym
  283.  
  284. elf: $(TARGET).elf
  285. hex: $(TARGET).hex
  286. eep: $(TARGET).eep
  287. lss: $(TARGET).lss
  288. sym: $(TARGET).sym
  289.  
  290.  
  291.  
  292. # Eye candy.
  293. # AVR Studio 3.x does not check make's exit code but relies on
  294. # the following magic strings to be generated by the compile job.
  295. begin:
  296.     @echo
  297.     @echo $(MSG_BEGIN)
  298.  
  299. finished:
  300.     @echo $(MSG_ERRORS_NONE)
  301.  
  302. end:
  303.     @echo $(MSG_END)
  304.     @echo
  305.  
  306.  
  307. # Display size of file.
  308. HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
  309. ELFSIZE = $(SIZE) -A $(TARGET).elf
  310. sizebefore:
  311.     @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi
  312.  
  313. sizeafter:
  314.     @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi
  315.  
  316.  
  317.  
  318. # Display compiler version information.
  319. gccversion :
  320.     @$(CC) --version
  321.  
  322.  
  323.  
  324. # Program the device.  
  325. program: $(TARGET).hex $(TARGET).eep
  326.     $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
  327.  
  328.  
  329.  
  330.  
  331. # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
  332. COFFCONVERT=$(OBJCOPY) --debugging \
  333. --change-section-address .data-0x800000 \
  334. --change-section-address .bss-0x800000 \
  335. --change-section-address .noinit-0x800000 \
  336. --change-section-address .eeprom-0x810000
  337.  
  338.  
  339. coff: $(TARGET).elf
  340.     @echo
  341.     @echo $(MSG_COFF) $(TARGET).cof
  342.     $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
  343.  
  344.  
  345. extcoff: $(TARGET).elf
  346.     @echo
  347.     @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
  348.     $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
  349.  
  350.  
  351.  
  352. # Create final output files (.hex, .eep) from ELF output file.
  353. %.hex: %.elf
  354.     @echo
  355.     @echo $(MSG_FLASH) $@
  356.     $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
  357.  
  358. %.eep: %.elf
  359.     @echo
  360.     @echo $(MSG_EEPROM) $@
  361.     -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
  362.     --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
  363.  
  364. # Create extended listing file from ELF output file.
  365. %.lss: %.elf
  366.     @echo
  367.     @echo $(MSG_EXTENDED_LISTING) $@
  368.     $(OBJDUMP) -h -S $< > $@
  369.  
  370. # Create a symbol table from ELF output file.
  371. %.sym: %.elf
  372.     @echo
  373.     @echo $(MSG_SYMBOL_TABLE) $@
  374.     $(NM) -n $< > $@
  375.  
  376.  
  377.  
  378. # Link: create ELF output file from object files.
  379. .SECONDARY : $(TARGET).elf
  380. .PRECIOUS : $(OBJ)
  381. %.elf: $(OBJ)
  382.     @echo
  383.     @echo $(MSG_LINKING) $@
  384.     $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)
  385.  
  386.  
  387. # Compile: create object files from C source files.
  388. %.o : %.c
  389.     @echo
  390.     @echo $(MSG_COMPILING) $<
  391.     $(CC) -c $(ALL_CFLAGS) $< -o $@
  392.  
  393.  
  394. # Compile: create assembler files from C source files.
  395. %.s : %.c
  396.     $(CC) -S $(ALL_CFLAGS) $< -o $@
  397.  
  398.  
  399. # Assemble: create object files from assembler source files.
  400. %.o : %.S
  401.     @echo
  402.     @echo $(MSG_ASSEMBLING) $<
  403.     $(CC) -c $(ALL_ASFLAGS) $< -o $@
  404.  
  405.  
  406.  
  407. # Target: clean project.
  408. clean: begin clean_list finished end
  409.  
  410. clean_list :
  411.     @echo
  412.     @echo $(MSG_CLEANING)
  413.     $(REMOVE) $(TARGET).hex
  414.     $(REMOVE) $(TARGET).eep
  415.     $(REMOVE) $(TARGET).obj
  416.     $(REMOVE) $(TARGET).cof
  417.     $(REMOVE) $(TARGET).elf
  418.     $(REMOVE) $(TARGET).map
  419.     $(REMOVE) $(TARGET).obj
  420.     $(REMOVE) $(TARGET).a90
  421.     $(REMOVE) $(TARGET).sym
  422.     $(REMOVE) $(TARGET).lnk
  423.     $(REMOVE) $(TARGET).lss
  424.     $(REMOVE) $(OBJ)
  425.     $(REMOVE) $(LST)
  426.     $(REMOVE) $(SRC:.c=.s)
  427.     $(REMOVE) $(SRC:.c=.d)
  428.     $(REMOVE) .dep/*
  429.  
  430.  
  431.  
  432. # Include the dependency files.
  433. -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
  434.  
  435.  
  436. # Listing of phony targets.
  437. .PHONY : all begin finish end sizebefore sizeafter gccversion \
  438. build elf hex eep lss sym coff extcoff \
  439. clean clean_list program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement