avrmakelib.mk 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. ######################################################
  2. # AVR make library #
  3. # Copyright (c) 2015-2016 Michael Buesch <m@bues.ch> #
  4. # Version 1.5 #
  5. ######################################################
  6. ifeq ($(NAME),)
  7. $(error NAME not defined)
  8. endif
  9. ifeq ($(SRCS),)
  10. $(error SRCS not defined)
  11. endif
  12. ifeq ($(F_CPU),)
  13. $(error F_CPU not defined)
  14. endif
  15. ifeq ($(GCC_ARCH),)
  16. $(error GCC_ARCH not defined)
  17. endif
  18. ifeq ($(AVRDUDE_ARCH),)
  19. $(error AVRDUDE_ARCH not defined)
  20. endif
  21. _uppercase = $(shell echo $(1) | tr a-z A-Z)
  22. _lowercase = $(shell echo $(1) | tr A-Z a-z)
  23. # The toolchain definitions
  24. CC := avr-gcc
  25. OBJCOPY := avr-objcopy
  26. OBJDUMP := avr-objdump
  27. SIZE := avr-size
  28. MKDIR := mkdir
  29. MV := mv
  30. RM := rm
  31. CP := cp
  32. ECHO := echo
  33. GREP := grep
  34. TRUE := true
  35. TEST := test
  36. AVRDUDE := avrdude
  37. MYSMARTUSB := mysmartusb.py
  38. DOXYGEN := doxygen
  39. PYTHON2 := python2
  40. PYTHON3 := python3
  41. SPARSE := sparse
  42. V := @ # Verbose build: make V=1
  43. C := 0 # Sparsechecker build: make C=1
  44. DEBUG := 0 # Debug build: make DEBUG=1
  45. O := s # Optimize flag
  46. Q := $(V:1=)
  47. QUIET_CC = $(Q:@=@$(ECHO) ' CC '$@;)$(CC)
  48. QUIET_DEPEND = $(Q:@=@$(ECHO) ' DEPEND '$@;)$(CC)
  49. QUIET_OBJCOPY = $(Q:@=@$(ECHO) ' OBJCOPY '$@;)$(OBJCOPY)
  50. QUIET_SIZE = $(Q:@=@$(ECHO) ' SIZE '$@;)$(SIZE)
  51. QUIET_PYTHON2 = $(Q:@=@$(ECHO) ' PYTHON2 '$@;)$(PYTHON2)
  52. QUIET_PYTHON3 = $(Q:@=@$(ECHO) ' PYTHON3 '$@;)$(PYTHON3)
  53. QUIET_RM = $(Q:@=@$(ECHO) ' RM '$@;)$(RM)
  54. ifeq ($(C),1)
  55. QUIET_SPARSE = $(Q:@=@$(ECHO) ' SPARSE '$@;)$(SPARSE)
  56. else
  57. QUIET_SPARSE = @$(TRUE)
  58. endif
  59. FUNC_STACK_LIMIT ?= 128
  60. WARN_CFLAGS := -Wall -Wextra -Wno-unused-parameter -Wswitch-enum \
  61. -Wsuggest-attribute=noreturn \
  62. -Wundef -Wpointer-arith -Winline \
  63. $(if $(FUNC_STACK_LIMIT),-Wstack-usage=$(FUNC_STACK_LIMIT)) \
  64. -Wcast-qual -Wlogical-op -Wshadow \
  65. -Wconversion
  66. DEFINE_CFLAGS := "-Dinline=inline __attribute__((__always_inline__))" \
  67. -DF_CPU=$(F_CPU) \
  68. $(if $(BOOT_OFFSET),-DBOOT_OFFSET=$(BOOT_OFFSET)) \
  69. $(if $(DEBUG),-DDEBUG=$(DEBUG))
  70. MAIN_CFLAGS := -mmcu=$(GCC_ARCH) -std=gnu11 -g0 -O$(O) \
  71. $(WARN_CFLAGS) \
  72. $(DEFINE_CFLAGS) \
  73. -mcall-prologues -mrelax -mstrict-X \
  74. -fshort-enums -flto
  75. MAIN_LDFLAGS :=
  76. INSTRUMENT_CFLAGS := -DINSTRUMENT_FUNCTIONS=1 \
  77. -finstrument-functions \
  78. -finstrument-functions-exclude-file-list=.h
  79. MAIN_SPARSEFLAGS := -gcc-base-dir=/usr/lib/avr \
  80. -I/usr/lib/avr/include \
  81. -D__STDC_HOSTED__=1 \
  82. -D__AVR_ARCH__=5 \
  83. -D__AVR_$(subst MEGA,mega,$(call _uppercase,$(GCC_ARCH)))__=1 \
  84. -Wsparse-all
  85. CFLAGS := $(MAIN_CFLAGS) \
  86. $(if $(INSTRUMENT_FUNC),$(INSTRUMENT_CFLAGS)) \
  87. $(CFLAGS) \
  88. -include sparse.h
  89. BOOT_CFLAGS := $(MAIN_CFLAGS) -DBOOTLOADER \
  90. $(if $(BOOT_INSTRUMENT_FUNC),$(INSTRUMENT_CFLAGS)) \
  91. $(BOOT_CFLAGS) \
  92. -include sparse.h
  93. LDFLAGS := $(MAIN_LDFLAGS) -fwhole-program \
  94. $(LDFLAGS)
  95. BOOT_LDFLAGS := $(MAIN_LDFLAGS) -fwhole-program \
  96. -Wl,--section-start=.text=$(BOOT_OFFSET) \
  97. $(BOOT_LDFLAGS)
  98. SPARSEFLAGS := $(subst gnu11,gnu99,$(CFLAGS)) \
  99. $(MAIN_SPARSEFLAGS) $(SPARSEFLAGS)
  100. BOOT_SPARSEFLAGS := $(subst gnu11,gnu99,$(BOOT_CFLAGS)) \
  101. $(MAIN_SPARSEFLAGS) $(BOOT_SPARSEFLAGS)
  102. BIN := $(NAME).bin
  103. HEX := $(NAME).hex
  104. EEP := $(NAME).eep.hex
  105. BOOT_BIN := $(NAME).bootloader.bin
  106. BOOT_HEX := $(NAME).bootloader.hex
  107. OBJ_DIR := obj
  108. DEP_DIR := dep
  109. BOOT_OBJ_DIR := obj-boot
  110. BOOT_DEP_DIR := dep-boot
  111. .SUFFIXES:
  112. .DEFAULT_GOAL := all
  113. # Programmer parameters
  114. AVRDUDE_SPEED ?= 1
  115. AVRDUDE_SLOW_SPEED ?= 200
  116. AVRDUDE_PROGRAMMER :=
  117. ifeq ($(PROGRAMMER),mysmartusb)
  118. AVRDUDE_PROGRAMMER := avr910
  119. PROGPORT := /dev/ttyUSB0
  120. endif
  121. ifeq ($(PROGRAMMER),avrisp2)
  122. AVRDUDE_PROGRAMMER := avrisp2
  123. PROGPORT := usb
  124. endif
  125. ifeq ($(AVRDUDE_PROGRAMMER),)
  126. $(error Invalid PROGRAMMER specified)
  127. endif
  128. define _programmer_cmd_pwrcycle
  129. $(if $(filter mysmartusb,$(PROGRAMMER)), \
  130. $(MYSMARTUSB) -p0 $(PROGPORT) && \
  131. sleep 1 && \
  132. $(MYSMARTUSB) -p1 $(PROGPORT) \
  133. )
  134. endef
  135. define _programmer_cmd_prog_enter
  136. $(if $(filter mysmartusb,$(PROGRAMMER)), \
  137. $(MYSMARTUSB) -mp $(PROGPORT) \
  138. )
  139. endef
  140. define _programmer_cmd_prog_leave
  141. $(if $(filter mysmartusb,$(PROGRAMMER)), \
  142. $(MYSMARTUSB) -md $(PROGPORT) \
  143. )
  144. endef
  145. DEPS = $(sort $(patsubst %.c,$(2)/%.d,$(1)))
  146. OBJS = $(sort $(patsubst %.c,$(2)/%.o,$(1)))
  147. # Generate dependencies
  148. $(call DEPS,$(SRCS),$(DEP_DIR)): $(DEP_DIR)/%.d: %.c
  149. @$(MKDIR) -p $(dir $@)
  150. @$(MKDIR) -p $(OBJ_DIR)
  151. $(QUIET_DEPEND) -o $@.tmp -MM $(if $(GEN_SRCS),-MG) -MT "$@ $(patsubst $(DEP_DIR)/%.d,$(OBJ_DIR)/%.o,$@)" $(CFLAGS) $<
  152. @$(MV) -f $@.tmp $@
  153. ifneq ($(BOOT_SRCS),)
  154. $(call DEPS,$(BOOT_SRCS),$(BOOT_DEP_DIR)): $(BOOT_DEP_DIR)/%.d: %.c
  155. @$(MKDIR) -p $(dir $@)
  156. @$(MKDIR) -p $(BOOT_OBJ_DIR)
  157. $(QUIET_DEPEND) -o $@.tmp -MM $(if $(BOOT_GEN_SRCS),-MG) -MT "$@ $(patsubst $(BOOT_DEP_DIR)/%.d,$(BOOT_OBJ_DIR)/%.o,$@)" $(BOOT_CFLAGS) $<
  158. @$(MV) -f $@.tmp $@
  159. endif
  160. -include $(call DEPS,$(SRCS),$(DEP_DIR))
  161. ifneq ($(BOOT_SRCS),)
  162. -include $(call DEPS,$(BOOT_SRCS),$(BOOT_DEP_DIR))
  163. endif
  164. # Generate object files
  165. $(call OBJS,$(SRCS),$(OBJ_DIR)): $(OBJ_DIR)/%.o: %.c
  166. @$(MKDIR) -p $(dir $@)
  167. $(QUIET_CC) -o $@ -c $(CFLAGS) $<
  168. $(QUIET_SPARSE) $(SPARSEFLAGS) $<
  169. ifneq ($(BOOT_SRCS),)
  170. $(call OBJS,$(BOOT_SRCS),$(BOOT_OBJ_DIR)): $(BOOT_OBJ_DIR)/%.o: %.c
  171. @$(MKDIR) -p $(dir $@)
  172. $(QUIET_CC) -o $@ -c $(BOOT_CFLAGS) $<
  173. $(QUIET_SPARSE) $(BOOT_SPARSEFLAGS) $<
  174. endif
  175. all: $(HEX) $(if $(BOOT_SRCS),$(BOOT_HEX))
  176. %.s: %.c
  177. $(QUIET_CC) $(CFLAGS) -fno-lto -S $*.c
  178. $(BIN): $(call OBJS,$(SRCS),$(OBJ_DIR))
  179. $(QUIET_CC) $(CFLAGS) -o $(BIN) $(call OBJS,$(SRCS),$(OBJ_DIR)) $(LDFLAGS)
  180. $(BOOT_BIN): $(call OBJS,$(BOOT_SRCS),$(BOOT_OBJ_DIR))
  181. $(QUIET_CC) $(BOOT_CFLAGS) -o $(BOOT_BIN) $(call OBJS,$(BOOT_SRCS),$(BOOT_OBJ_DIR)) $(BOOT_LDFLAGS)
  182. $(HEX): $(BIN)
  183. $(QUIET_OBJCOPY) -R.eeprom -O ihex $(BIN) $(HEX)
  184. @$(OBJDUMP) -h $(BIN) | $(GREP) -qe .eeprom && \
  185. $(OBJCOPY) -j.eeprom --set-section-flags=.eeprom="alloc,load" \
  186. --change-section-lma .eeprom=0 -O ihex $(BIN) $(EEP) \
  187. || $(TRUE)
  188. $(QUIET_SIZE) --format=SysV $(BIN)
  189. $(BOOT_HEX): $(BOOT_BIN)
  190. $(QUIET_OBJCOPY) -R.eeprom -O ihex $(BOOT_BIN) $(BOOT_HEX)
  191. $(QUIET_SIZE) --format=SysV $(BOOT_BIN)
  192. define _avrdude_interactive
  193. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  194. -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) -t
  195. endef
  196. define _avrdude_reset
  197. $(AVRDUDE) -B $(AVRDUDE_SLOW_SPEED) -p $(AVRDUDE_ARCH) \
  198. -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
  199. -U signature:r:/dev/null:i -q -q
  200. endef
  201. define _avrdude_write_flash
  202. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  203. -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
  204. $(if $(BOOT_SRCS),-U flash:w:$(BOOT_HEX)) \
  205. -U flash:w:$(HEX)
  206. endef
  207. define _avrdude_write_eeprom
  208. $(TEST) -r $(EEP) && ( \
  209. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  210. -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
  211. -U eeprom:w:$(EEP) \
  212. ) || $(TRUE)
  213. endef
  214. define _avrdude_write_fuse
  215. $(AVRDUDE) -B $(AVRDUDE_SLOW_SPEED) -p $(AVRDUDE_ARCH) \
  216. -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) -q -q \
  217. -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m $(if $(EFUSE),-U efuse:w:$(EFUSE):m)
  218. endef
  219. write_flash: all
  220. $(call _programmer_cmd_prog_enter)
  221. $(call _avrdude_write_flash)
  222. $(call _programmer_cmd_pwrcycle)
  223. $(call _programmer_cmd_prog_leave)
  224. writeflash: write_flash
  225. write_eeprom: all
  226. $(call _programmer_cmd_prog_enter)
  227. $(call _avrdude_write_eeprom)
  228. $(call _programmer_cmd_pwrcycle)
  229. $(call _programmer_cmd_prog_leave)
  230. writeeeprom: write_eeprom
  231. write_mem: all
  232. $(call _programmer_cmd_prog_enter)
  233. $(call _avrdude_write_flash)
  234. $(call _avrdude_write_eeprom)
  235. $(call _programmer_cmd_pwrcycle)
  236. $(call _programmer_cmd_prog_leave)
  237. writemem: write_mem
  238. install: write_mem
  239. write_fuses:
  240. $(call _programmer_cmd_prog_enter)
  241. $(call _avrdude_write_fuse)
  242. $(call _programmer_cmd_pwrcycle)
  243. $(call _programmer_cmd_prog_leave)
  244. write_fuse: write_fuses
  245. writefuse: write_fuses
  246. writefuses: write_fuses
  247. print_fuses:
  248. @$(if $(LFUSE),echo "LFUSE (low fuse) = $(LFUSE)",$(TRUE))
  249. @$(if $(HFUSE),echo "HFUSE (high fuse) = $(HFUSE)",$(TRUE))
  250. @$(if $(EFUSE),echo "EFUSE (extended fuse) = $(EFUSE)",$(TRUE))
  251. printfuses: print_fuses
  252. reset:
  253. $(call _programmer_cmd_prog_enter)
  254. $(call _avrdude_reset)
  255. $(call _programmer_cmd_pwrcycle)
  256. avrdude:
  257. $(call _programmer_cmd_prog_enter)
  258. $(call _avrdude_interactive)
  259. $(call _programmer_cmd_pwrcycle)
  260. $(call _programmer_cmd_prog_leave)
  261. doxygen:
  262. $(DOXYGEN) Doxyfile
  263. clean:
  264. -$(QUIET_RM) -rf \
  265. $(OBJ_DIR) $(DEP_DIR) \
  266. $(BOOT_OBJ_DIR) $(BOOT_DEP_DIR) \
  267. $(BIN) $(BOOT_BIN) \
  268. *.pyc *.pyo __pycache__ \
  269. $(GEN_SRCS) $(BOOT_GEN_SRCS) \
  270. $(CLEAN_FILES)
  271. distclean: clean
  272. -$(QUIET_RM) -f \
  273. $(HEX) $(BOOT_HEX) \
  274. $(EEP) \
  275. *.s \
  276. $(DISTCLEAN_FILES)