aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 1f573335389ad0d094ea219d7bf7b24f25d583a1 (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
include config.mk

CC ?= avr-gcc
GDB ?= avr-gdb
OBJCOPY ?= avr-objcopy
AVRDUDE ?= avrdude
QEMU ?= qemu-system-avr

MCU ?= atmega32u4
SIM_MCU ?= atmega2560
COMMON_CFLAGS += -Wall -Wextra -DF_CPU=16000000UL
CFLAGS += $(COMMON_CFLAGS) -mmcu=$(MCU) -O3
SIM_CFLAGS += $(COMMON_CFLAGS) -mmcu=$(SIM_MCU) -g3 -gdwarf-2 -DWDT_DISABLE

COMMON_LDFLAGS += -Wl,-u,vfprintf -lprintf_flt
LDFLAGS += $(COMMON_LDFLAGS) -mmcu=$(MCU)
SIM_LDFLAGS += $(COMMON_LDFLAGS) -mmcu=$(SIM_MCU)

$(shell mkdir -p build/{real,sim})

all: build/altimeter_sim.elf build/altimeter.hex

# Run the sim build software in qemu, halted ready for `make gdb` to attach and
# continue. Note that sim build mocks out some hardware components
emu: build/altimeter_sim.elf
	$(QEMU) -s -S -nographic -machine mega2560 -bios $<

# Attach GDB session to qemu instance started with `make emu`
gdb:
	$(GDB) -ex "target remote :1234"

# Flash AVR software to microcontroller with AVRdude
flash: build/altimeter.hex
	$(AVRDUDE) -F -V -c avr109 -p $(MCU) -P $(PORT) -b 115200 -U flash:w:$<

clean:
	rm -rf build/

# ELF for real hardware. No mock/sim hardware included (except barometer,
# haven't written that code yet)
build/altimeter.elf: \
	build/real/barometer_sim.o \
	build/real/display_sim.o \
	build/real/altimeter.o \
	build/real/util.o
	$(CC) -o $@ $^ $(LDFLAGS)

# ELF for simulator/emu. Note that whever I/O is required, the *_sim.{o,c}
# versions of each component will provide a mocked interface. This allows for
# operation in QEMU (see `emu` recipe) without requring SPI, I²C and
# peripherals to be emulated. Useful for UI testing and development etc.
build/altimeter_sim.elf: \
	build/sim/barometer_sim.o \
	build/sim/display_sim.o \
	build/sim/altimeter.o \
	build/sim/util.o
	$(CC) -o $@ $^ $(SIM_LDFLAGS)

build/sim/%.o: %.c
	$(CC) -c -o $@ $^ $(SIM_CFLAGS)

build/real/%.o: %.c
	$(CC) -c -o $@ $^ $(CFLAGS)

build/%.hex: build/%.elf
	$(OBJCOPY) -O ihex -R .eeprom $< $@

build/%.elf:
	$(CC) -o $@ $^ $(CFLAGS)

.PHONY: all emu gdb flash clean