Makefile 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # Project name
  2. NAME := moistcontrol
  3. # Project source files
  4. SRCS := comm.c \
  5. controller.c \
  6. datetime.c \
  7. log.c \
  8. main.c \
  9. notify_led.c \
  10. ioext.c \
  11. onoffswitch.c \
  12. pcf8574.c \
  13. rv3029.c \
  14. sensor.c \
  15. twi_master.c \
  16. twi_master_sync.c \
  17. util.c
  18. # Project fuse bits
  19. LFUSE := 0xA0
  20. HFUSE := 0xD9
  21. # CPU speed, in Hz
  22. F_CPU := 16000000ul
  23. # Architecture configuration
  24. GCC_ARCH := atmega8
  25. AVRDUDE_ARCH := m8
  26. AVRDUDE_SPEED := 1
  27. AVRDUDE_SLOW_SPEED := 100
  28. # Programmer selection.
  29. # Values can be: avrisp2, mysmartusb
  30. PROGRAMMER := avrisp2
  31. # Additional compiler flags
  32. CFLAGS := -DTWI_SCL_HZ=100000ul \
  33. -DCOMM_BAUDRATE=19200ul \
  34. -DCOMM_PAYLOAD_LEN=12
  35. LDFLAGS :=
  36. # Additional "clean" and "distclean" target files
  37. CLEAN_FILES :=
  38. DISTCLEAN_FILES :=
  39. # =============================================================================
  40. # =============================================================================
  41. # =============================================================================
  42. BINEXT :=
  43. NODEPS :=
  44. # The toolchain definitions
  45. CC = avr-gcc$(BINEXT)
  46. OBJCOPY = avr-objcopy$(BINEXT)
  47. OBJDUMP = avr-objdump$(BINEXT)
  48. SIZE = avr-size$(BINEXT)
  49. MKDIR = mkdir$(BINEXT)
  50. MV = mv$(BINEXT)
  51. RM = rm$(BINEXT)
  52. CP = cp$(BINEXT)
  53. ECHO = echo$(BINEXT)
  54. GREP = grep$(BINEXT)
  55. TRUE = true$(BINEXT)
  56. TEST = test$(BINEXT)
  57. AVRDUDE = avrdude$(BINEXT)
  58. MYSMARTUSB = mysmartusb.py
  59. DOXYGEN = doxygen$(BINEXT)
  60. V := @ # Verbose build: make V=1
  61. O := s # Optimize flag
  62. Q := $(V:1=)
  63. QUIET_CC = $(Q:@=@$(ECHO) ' CC '$@;)$(CC)
  64. QUIET_DEPEND = $(Q:@=@$(ECHO) ' DEPEND '$@;)$(CC)
  65. QUIET_OBJCOPY = $(Q:@=@$(ECHO) ' OBJCOPY '$@;)$(OBJCOPY)
  66. QUIET_SIZE = $(Q:@=@$(ECHO) ' SIZE '$@;)$(SIZE)
  67. WARN_CFLAGS = -Wall -Wextra -Wno-unused-parameter -Wswitch-enum \
  68. -Wsuggest-attribute=noreturn \
  69. -Wundef -Wpointer-arith -Winline \
  70. -Wstack-usage=192 \
  71. -Wcast-qual -Wlogical-op
  72. CFLAGS += -mmcu=$(GCC_ARCH) -std=c99 -g -O$(O) $(WARN_CFLAGS) \
  73. "-Dinline=inline __attribute__((__always_inline__))" \
  74. -fshort-enums -DF_CPU=$(F_CPU) \
  75. -flto
  76. BIN := $(NAME).bin
  77. HEX := $(NAME).hex
  78. EEP := $(NAME).eep.hex
  79. .SUFFIXES:
  80. .DEFAULT_GOAL := all
  81. # Programmer parameters
  82. ifeq ($(PROGRAMMER),mysmartusb)
  83. AVRDUDE_PROGRAMMER := avr910
  84. PROGPORT := /dev/ttyUSB0
  85. endif
  86. ifeq ($(PROGRAMMER),avrisp2)
  87. AVRDUDE_PROGRAMMER := avrisp2
  88. PROGPORT := usb
  89. endif
  90. ifeq ($(AVRDUDE_PROGRAMMER),)
  91. $(error Invalid PROGRAMMER specified)
  92. endif
  93. PROGRAMMER_CMD_PWRCYCLE := \
  94. $(if $(filter mysmartusb,$(PROGRAMMER)), \
  95. $(MYSMARTUSB) -p0 $(PROGPORT) && \
  96. sleep 1 && \
  97. $(MYSMARTUSB) -p1 $(PROGPORT) \
  98. )
  99. PROGRAMMER_CMD_PROG_ENTER := \
  100. $(if $(filter mysmartusb,$(PROGRAMMER)), \
  101. $(MYSMARTUSB) -mp $(PROGPORT) \
  102. )
  103. PROGRAMMER_CMD_PROG_LEAVE := \
  104. $(if $(filter mysmartusb,$(PROGRAMMER)), \
  105. $(MYSMARTUSB) -md $(PROGPORT) \
  106. )
  107. DEPS = $(sort $(patsubst %.c,dep/%.d,$(1)))
  108. OBJS = $(sort $(patsubst %.c,obj/%.o,$(1)))
  109. # Generate dependencies
  110. $(call DEPS,$(SRCS)): dep/%.d: %.c
  111. @$(MKDIR) -p $(dir $@)
  112. @$(MKDIR) -p obj
  113. $(QUIET_DEPEND) -o $@.tmp -MM -MT "$@ $(patsubst dep/%.d,obj/%.o,$@)" $(CFLAGS) $<
  114. @$(MV) -f $@.tmp $@
  115. ifeq ($(NODEPS),)
  116. -include $(call DEPS,$(SRCS))
  117. endif
  118. # Generate object files
  119. $(call OBJS,$(SRCS)): obj/%.o: %.c
  120. @$(MKDIR) -p $(dir $@)
  121. $(QUIET_CC) -o $@ -c $(CFLAGS) $<
  122. all: $(HEX)
  123. %.s: %.c
  124. $(QUIET_CC) $(CFLAGS) -S $*.c
  125. $(BIN): $(call OBJS,$(SRCS))
  126. $(QUIET_CC) $(CFLAGS) -o $(BIN) -fwhole-program $(call OBJS,$(SRCS)) $(LDFLAGS)
  127. $(HEX): $(BIN)
  128. $(QUIET_OBJCOPY) -R.eeprom -O ihex $(BIN) $(HEX)
  129. @$(if $(filter .exe,$(BINEXT)),$(TRUE), \
  130. $(OBJDUMP) -h $(BIN) | $(GREP) -qe .eeprom && \
  131. $(OBJCOPY) -j.eeprom --set-section-flags=.eeprom="alloc,load" \
  132. --change-section-lma .eeprom=0 -O ihex $(BIN) $(EEP) \
  133. || $(TRUE))
  134. @$(ECHO)
  135. $(QUIET_SIZE) --format=SysV $(BIN)
  136. avrdude:
  137. $(call PROGRAMMER_CMD_PROG_ENTER)
  138. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  139. -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) -t
  140. $(call PROGRAMMER_CMD_PWRCYCLE)
  141. $(call PROGRAMMER_CMD_PROG_LEAVE)
  142. install: all
  143. $(call PROGRAMMER_CMD_PROG_ENTER)
  144. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  145. -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
  146. -U flash:w:$(HEX)
  147. $(TEST) -r $(EEP) && ( \
  148. $(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
  149. -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
  150. -U eeprom:w:$(EEP) \
  151. ) || $(TRUE)
  152. $(call PROGRAMMER_CMD_PWRCYCLE)
  153. $(call PROGRAMMER_CMD_PROG_LEAVE)
  154. reset:
  155. $(call PROGRAMMER_CMD_PROG_ENTER)
  156. $(AVRDUDE) -B $(AVRDUDE_SLOW_SPEED) -p $(AVRDUDE_ARCH) \
  157. -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) \
  158. -U signature:r:/dev/null:i -q -q
  159. $(call PROGRAMMER_CMD_PWRCYCLE)
  160. writefuse:
  161. $(call PROGRAMMER_CMD_PROG_ENTER)
  162. $(AVRDUDE) -B $(AVRDUDE_SLOW_SPEED) -p $(AVRDUDE_ARCH) \
  163. -c $(AVRDUDE_PROGRAMMER) -P $(PROGPORT) -q -q \
  164. -U lfuse:w:$(LFUSE):m \
  165. -U hfuse:w:$(HFUSE):m
  166. $(call PROGRAMMER_CMD_PWRCYCLE)
  167. $(call PROGRAMMER_CMD_PROG_LEAVE)
  168. doxygen:
  169. $(DOXYGEN) Doxyfile
  170. clean:
  171. -$(RM) -rf obj dep $(BIN) $(CLEAN_FILES)
  172. distclean: clean
  173. -$(RM) -f $(HEX) $(EEP) $(DISTCLEAN_FILES)
  174. -$(RM) -f $(if $(filter .exe,$(BINEXT)),$(patsubst %.c,%.s,$(SRCS)),*.s)