Makefile 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # The architecture configuration
  2. ARCH = atmega8
  3. AVRDUDE_ARCH = m8
  4. AVRDUDE = avrdude
  5. AVRDUDE_SPEED = 1
  6. PROGRAMMER = avrisp2
  7. PROGPORT = usb
  8. # The toolchain definitions
  9. CC = avr-gcc
  10. OBJCOPY = avr-objcopy
  11. SIZE = avr-size
  12. READELF = avr-readelf
  13. SPARSE = sparse
  14. V = @ # Verbose build: make V=1
  15. C = 0 # Sparsechecker build: make C=1
  16. Q = $(V:1=)
  17. QUIET_CC = $(Q:@=@echo ' CC '$@;)$(CC)
  18. QUIET_DEPEND = $(Q:@=@echo ' DEPEND '$@;)$(CC)
  19. QUIET_OBJCOPY = $(Q:@=@echo ' OBJCOPY '$@;)$(OBJCOPY)
  20. QUIET_SIZE = $(Q:@=@echo ' SIZE '$@;)$(SIZE)
  21. QUIET_READELF = $(Q:@=@echo ' READELF '$@;)$(READELF)
  22. ifeq ($(C),1)
  23. QUIET_SPARSE = $(Q:@=@echo ' SPARSE '$@;)$(SPARSE)
  24. else
  25. QUIET_SPARSE = @/bin/true
  26. endif
  27. CFLAGS = -mmcu=$(ARCH) -std=gnu99 -g0 -O2 -Wall \
  28. "-Dinline=inline __attribute__((__always_inline__))"
  29. SPARSEFLAGS = $(CFLAGS) -I "/usr/lib/avr/include" -D__AVR_ARCH__=4 \
  30. -D__AVR_ATmega8__=1 -D__ATTR_PROGMEM__="" -Dsignal=dllexport \
  31. -Dexternally_visible=dllexport
  32. SRCS = main.c util.c valves.c sensor.c remote.c
  33. NAME = pressure_control
  34. BIN = $(NAME).bin
  35. HEX = $(NAME).hex
  36. EEP = $(NAME).eep.hex
  37. FUSES = $(NAME).fuses.bin
  38. .SUFFIXES:
  39. .PHONY: all avrdude install_flash install_eeprom install reset writefuse clean distclean
  40. .DEFAULT_GOAL := all
  41. DEPS = $(sort $(patsubst %.c,dep/%.d,$(1)))
  42. OBJS = $(sort $(patsubst %.c,obj/%.o,$(1)))
  43. # Generate dependencies
  44. $(call DEPS,$(SRCS)): dep/%.d: %.c
  45. @mkdir -p $(dir $@)
  46. $(QUIET_DEPEND) -o $@.tmp -MM -MG -MT "$@ $(patsubst dep/%.d,obj/%.o,$@)" $(CFLAGS) $< && mv -f $@.tmp $@
  47. -include $(call DEPS,$(SRCS))
  48. # Generate object files
  49. $(call OBJS,$(SRCS)): obj/%.o:
  50. @mkdir -p $(dir $@)
  51. $(QUIET_SPARSE) $(SPARSEFLAGS) $<
  52. $(QUIET_CC) -o $@ -c $(CFLAGS) $<
  53. all: $(HEX) $(FUSES)
  54. %.s: %.c
  55. $(QUIET_CC) $(CFLAGS) -S $*.c
  56. %.o: %.c
  57. $(QUIET_SPARSE) $(SPARSEFLAGS) $<
  58. $(QUIET_CC) -o $@ -c $(CFLAGS) $<
  59. $(BIN): $(call OBJS,$(SRCS))
  60. $(QUIET_CC) $(CFLAGS) -o $(BIN) $(call OBJS,$(SRCS)) $(LDFLAGS)
  61. $(HEX): $(BIN)
  62. $(QUIET_OBJCOPY) -R.eeprom -R.fuse -O ihex $(BIN) $(HEX)
  63. $(QUIET_OBJCOPY) -j.eeprom --set-section-flags=.eeprom="alloc,load" \
  64. --change-section-lma .eeprom=0 -O ihex $(BIN) $(EEP)
  65. $(QUIET_SIZE) $(BIN)
  66. $(QUIET_READELF) -S $(BIN) | egrep '(Name|text|eeprom|data|bss)'
  67. avrdude:
  68. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  69. -c $(PROGRAMMER) -P $(PROGPORT) -t
  70. install_flash:
  71. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  72. -c $(PROGRAMMER) -P $(PROGPORT) -U flash:w:$(HEX)
  73. install_eeprom:
  74. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  75. -c $(PROGRAMMER) -P $(PROGPORT) -U eeprom:w:$(EEP)
  76. install: all install_flash install_eeprom
  77. # Reset the microcontroller through avrdude
  78. reset:
  79. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  80. -c $(PROGRAMMER) -P $(PROGPORT) \
  81. -U signature:r:/dev/null:i -q -q
  82. $(FUSES): $(BIN)
  83. $(QUIET_OBJCOPY) -j.fuse -O binary $(BIN) $(FUSES)
  84. writefuses: $(FUSES)
  85. @echo Low fuse is: $$(hexdump -e '1/1 "0x%02X "' $(FUSES) | cut -d ' ' -f 1)
  86. @echo High fuse is: $$(hexdump -e '1/1 "0x%02X "' $(FUSES) | cut -d ' ' -f 2)
  87. $(AVRDUDE) -B 100 -p $(AVRDUDE_ARCH) \
  88. -c $(PROGRAMMER) -P $(PROGPORT) -q -q \
  89. -U lfuse:w:$$(hexdump -e '1/1 "0x%02X "' $(FUSES) | cut -d ' ' -f 1):m \
  90. -U hfuse:w:$$(hexdump -e '1/1 "0x%02X "' $(FUSES) | cut -d ' ' -f 2):m
  91. clean:
  92. rm -Rf *~ *.o obj dep $(BIN)
  93. distclean: clean
  94. rm -f *.s $(HEX) $(EEP) $(FUSES)
  95. help:
  96. @echo "Makefile"
  97. @echo ""
  98. @echo "Targets:"
  99. @echo " all - build the firmware (default target)"
  100. @echo " clean - remove object files"
  101. @echo " distclean - remove object, binary and hex files"
  102. @echo ""
  103. @echo "Targets that operate on the device through avrdude:"
  104. @echo " install - flash the program code"
  105. @echo " writefuse - write the fuse bits"
  106. @echo " reset - pull the external device reset pin"
  107. @echo " avrdude - run avrdude in interactive mode"
  108. @echo ""
  109. @echo "Generic targets:"
  110. @echo " *.s - create an assembly file from a *.c file"