avrmakelib.mk 9.6 KB

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