summaryrefslogtreecommitdiffstats
path: root/firmware/Makefile
blob: 5e35d846ba3df4fec762d85f905feabe657d8f3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# The architecture configuration
ARCH		?= atmega88
AVRDUDE_ARCH	?= m88
AVRDUDE		?= avrdude
AVRDUDE_SPEED	?= 1
PROGRAMMER	?= avrisp2
PROGPORT	?= usb

# The toolchain definitions
CC		= avr-gcc
OBJCOPY		= avr-objcopy
SIZE		= avr-size
READELF		= avr-readelf
SPARSE		= sparse

V		= @		# Verbose build:  make V=1
C		= 0		# Sparsechecker build:  make C=1
Q		= $(V:1=)
QUIET_CC	= $(Q:@=@echo '     CC       '$@;)$(CC)
QUIET_DEPEND	= $(Q:@=@echo '     DEPEND   '$@;)$(CC)
QUIET_OBJCOPY	= $(Q:@=@echo '     OBJCOPY  '$@;)$(OBJCOPY)
QUIET_SIZE	= $(Q:@=@echo '     SIZE     '$@;)$(SIZE)
QUIET_READELF	= $(Q:@=@echo '     READELF  '$@;)$(READELF)
ifeq ($(C),1)
QUIET_SPARSE	= $(Q:@=@echo '     SPARSE   '$@;)$(SPARSE)
else
QUIET_SPARSE	= @/bin/true
endif

CFLAGS		= -mmcu=$(ARCH) -std=c99 -g0 -Os -Wall -DIN_FIRMWARE \
		  "-Dinline=inline __attribute__((__always_inline__))"
SPARSEFLAGS	= $(CFLAGS) -I "/usr/lib/avr/include" -D__AVR_ARCH__=4 \
		  -D__AVR_ATmega88__=1 -D__ATTR_PROGMEM__="" -Dsignal=dllexport \
		  -Dexternally_visible=dllexport -Dnaked=dllexport \
		  -Wdeclaration-after-statement -Wdo-while -Wptr-subtraction-blows \
		  -Wreturn-void -Wshadow -Wtypesign -Wundef

# The fuse bits
LFUSE	= 0xE0
HFUSE	= 0xDC
EFUSE	= 0x01

F_CPU	:= 20000000
CFLAGS	+= -DF_CPU=$(F_CPU)

SRCS	= main.c util.c lcd.c ltc1446.c ext_control.c
BIN	= psu.bin
HEX	= psu.hex
EEP	= psu.eep.hex

.SUFFIXES:
.PHONY: all avrdude install_flash install_eeprom install reset writefuse clean distclean
.DEFAULT_GOAL := all

DEPS = $(sort $(patsubst %.c,dep/%.d,$(1)))
OBJS = $(sort $(patsubst %.c,obj/%.o,$(1)))

# Generate dependencies
$(call DEPS,$(SRCS)): dep/%.d: %.c 
	@mkdir -p $(dir $@)
	$(QUIET_DEPEND) -o $@.tmp -MM -MT "$@ $(patsubst dep/%.d,obj/%.o,$@)" $(CFLAGS) $< && mv -f $@.tmp $@

-include $(call DEPS,$(SRCS))

# Generate object files
$(call OBJS,$(SRCS)): obj/%.o:
	@mkdir -p $(dir $@)
	$(QUIET_SPARSE) $(SPARSEFLAGS) $<
	$(QUIET_CC) -o $@ -c $(CFLAGS) $<

all: $(HEX)

%.s: %.c
	$(QUIET_CC) $(CFLAGS) -S $*.c

$(BIN): $(call OBJS,$(SRCS))
	$(QUIET_CC) $(CFLAGS) -o $(BIN) $(call OBJS,$(SRCS)) $(LDFLAGS)

$(HEX): $(BIN)
	$(QUIET_OBJCOPY) -R.eeprom -O ihex $(BIN) $(HEX)
	$(QUIET_OBJCOPY) -j.eeprom --set-section-flags=.eeprom="alloc,load" \
			 --change-section-lma .eeprom=0 -O ihex $(BIN) $(EEP)
	@echo -n "flash:   "
	@$(SIZE) $(BIN) --format=SysV | grep '.text' | awk '{print $$2;}'
	@echo -n "eeprom:  "
	@$(SIZE) $(BIN) --format=SysV | grep '.eeprom' | awk '{print $$2;}'
	@echo -n "ram:     "
	@echo "$$($(SIZE) $(BIN) --format=SysV | grep '.data' | awk '{print $$2;}') +\
		$$($(SIZE) $(BIN) --format=SysV | grep '.bss' | awk '{print $$2;}')" | bc

avrdude:
	$(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
	 -c $(PROGRAMMER) -P $(PROGPORT) -t

install_flash:
	$(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
	 -c $(PROGRAMMER) -P $(PROGPORT) -U flash:w:$(HEX)

install_eeprom:
	$(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
	 -c $(PROGRAMMER) -P $(PROGPORT) -U eeprom:w:$(EEP)

install: all install_flash install_eeprom

# Reset the microcontroller through avrdude
reset:
	$(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
	 -c $(PROGRAMMER) -P $(PROGPORT) \
	 -U signature:r:/dev/null:i -q -q

writefuse:
	$(AVRDUDE) -B $(AVRDUDE_SPEED) -p $(AVRDUDE_ARCH) \
	 -c $(PROGRAMMER) -P $(PROGPORT) -q -q \
	 -U lfuse:w:$(LFUSE):m \
	 -U hfuse:w:$(HFUSE):m \
	 -U efuse:w:$(EFUSE):m

clean:
	-rm -Rf *~ obj dep $(BIN)

distclean: clean
	-rm -f *.s $(HEX) $(EEP)

help:
	@echo "OpenPSU Makefile"
	@echo ""
	@echo "Targets:"
	@echo "  all       - build the firmware (default target)"
	@echo "  clean     - remove object files"
	@echo "  distclean - remove object, binary and hex files"
	@echo ""
	@echo "Targets that operate on the device through avrdude:"
	@echo "  install   - flash the program code"
	@echo "  writefuse - write the fuse bits"
	@echo "  reset     - pull the external device reset pin"
	@echo "  avrdude   - run avrdude in interactive mode"
	@echo ""
	@echo "Generic targets:"
	@echo "  *.s       - create an assembly file from a *.c file"
bues.ch cgit interface