Makefile 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # The architecture configuration
  2. ARCH = atmega88
  3. AVRDUDE_ARCH = m88
  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. TARGET = 0 # Target selection: make TARGET=0
  15. DEBUG = 0 # Debug build: make DEBUG=1
  16. V = @ # Verbose build: make V=1
  17. C = 0 # Sparsechecker build: make C=1
  18. Q = $(V:1=)
  19. QUIET_CC = $(Q:@=@echo ' CC '$@;)$(CC)
  20. QUIET_DEPEND = $(Q:@=@echo ' DEPEND '$@;)$(CC)
  21. QUIET_OBJCOPY = $(Q:@=@echo ' OBJCOPY '$@;)$(OBJCOPY)
  22. QUIET_SIZE = $(Q:@=@echo ' SIZE '$@;)$(SIZE)
  23. QUIET_READELF = $(Q:@=@echo ' READELF '$@;)$(READELF)
  24. ifeq ($(C),1)
  25. QUIET_SPARSE = $(Q:@=@echo ' SPARSE '$@;)$(SPARSE)
  26. else
  27. QUIET_SPARSE = @/bin/true
  28. endif
  29. CFLAGS = -mmcu=$(ARCH) -std=c99 -O2 -Wall \
  30. "-Dinline=inline __attribute__((__always_inline__))" \
  31. -DDEBUG=$(DEBUG) -DTARGET=$(TARGET)
  32. SPARSEFLAGS = $(CFLAGS) -I "/usr/lib/avr/include" -D__AVR_ARCH__=5 \
  33. -D__AVR_ATmega88__=1 -D__ATTR_PROGMEM__="" -Dsignal=dllexport \
  34. -Dexternally_visible=dllexport
  35. # The fuse bits
  36. # Ext Clock, Startup 6CK/14CK + 65ms
  37. # BOD off
  38. # SPI enabled
  39. LFUSE = 0xE0
  40. HFUSE = 0xDF
  41. EFUSE = 0xF9
  42. SRCS = main.c
  43. NAME = debounce
  44. BIN = $(NAME).bin
  45. HEX = $(NAME).hex
  46. EEP = $(NAME).eep.hex
  47. .SUFFIXES:
  48. .PHONY: all avrdude install_flash install_eeprom install reset writefuse clean distclean
  49. .DEFAULT_GOAL := all
  50. DEPS = $(sort $(patsubst %.c,dep/%.d,$(1)))
  51. OBJS = $(sort $(patsubst %.c,obj/%.o,$(1)))
  52. # Generate dependencies
  53. $(call DEPS,$(SRCS)): dep/%.d: %.c
  54. @mkdir -p $(dir $@)
  55. $(QUIET_DEPEND) -o $@.tmp -MM -MG -MT "$@ $(patsubst dep/%.d,obj/%.o,$@)" $(CFLAGS) $< && mv -f $@.tmp $@
  56. -include $(call DEPS,$(SRCS))
  57. # Generate object files
  58. $(call OBJS,$(SRCS)): obj/%.o:
  59. @mkdir -p $(dir $@)
  60. $(QUIET_SPARSE) $(SPARSEFLAGS) $<
  61. $(QUIET_CC) -o $@ -c $(CFLAGS) $<
  62. all: $(HEX)
  63. %.s: %.c
  64. $(QUIET_CC) $(CFLAGS) -S $*.c
  65. $(BIN): $(call OBJS,$(SRCS))
  66. $(QUIET_CC) $(CFLAGS) -o $(BIN) $(call OBJS,$(SRCS)) $(LDFLAGS)
  67. $(HEX): $(BIN)
  68. $(QUIET_OBJCOPY) -R.eeprom -O ihex $(BIN) $(HEX)
  69. # $(QUIET_OBJCOPY) -j.eeprom --set-section-flags=.eeprom="alloc,load" \
  70. # --change-section-lma .eeprom=0 -O ihex $(BIN) $(EEP)
  71. $(QUIET_SIZE) $(BIN)
  72. $(QUIET_READELF) -S $(BIN) | egrep '(Name|text|eeprom|data|bss)'
  73. @echo Built target $(TARGET)
  74. avrdude:
  75. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  76. -c $(PROGRAMMER) -P $(PROGPORT) -t
  77. install_flash:
  78. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  79. -c $(PROGRAMMER) -P $(PROGPORT) -U flash:w:$(HEX)
  80. install_eeprom:
  81. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  82. -c $(PROGRAMMER) -P $(PROGPORT) -U eeprom:w:$(EEP)
  83. install: all install_flash
  84. # Reset the microcontroller through avrdude
  85. reset:
  86. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  87. -c $(PROGRAMMER) -P $(PROGPORT) \
  88. -U signature:r:/dev/null:i -q -q
  89. writefuse:
  90. $(AVRDUDE) -B 5 -p $(AVRDUDE_ARCH) \
  91. -c $(PROGRAMMER) -P $(PROGPORT) -q -q \
  92. -U lfuse:w:$(LFUSE):m \
  93. -U hfuse:w:$(HFUSE):m
  94. # -U efuse:w:$(EFUSE):m
  95. clean:
  96. rm -Rf *~ *.o obj dep $(BIN)
  97. distclean: clean
  98. rm -f *.s $(HEX) $(EEP)
  99. help:
  100. @echo "Debouncer Makefile"
  101. @echo ""
  102. @echo "BUILD TARGETS (make TARGET=x):"
  103. @echo " TARGET=0 - Build target for \"cncjoints\""
  104. @echo ""
  105. @echo ""
  106. @echo "Cleanup:"
  107. @echo " all - build the firmware (default target)"
  108. @echo " clean - remove object files"
  109. @echo " distclean - remove object, binary and hex files"
  110. @echo ""
  111. @echo "avrdude operations:"
  112. @echo " install - flash the program code"
  113. @echo " writefuse - write the fuse bits"
  114. @echo " reset - pull the external device reset pin"
  115. @echo " avrdude - run avrdude in interactive mode"
  116. @echo ""
  117. @echo "Generic:"
  118. @echo " *.s - create an assembly file from a *.c file"