summaryrefslogtreecommitdiff
path: root/controller/fw/Makefile
blob: 5672f8a02b7d9d575037717f186c1e16838a9a8d (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
SOURCES		:= main.c mspdebug_wrapper.c spi_flash.c
SOURCES		+= mspdebug/drivers/jtaglib.c

BUILDDIR 	?= build
BINARY 		:= safetyreset
LDSCRIPT 	:= stm32f407.ld
LIBNAME		:= opencm3_stm32f4

OPENCM3_DIR 	?= libopencm3

PREFIX		?= arm-none-eabi-


CC		:= $(PREFIX)gcc
CXX		:= $(PREFIX)g++
LD		:= $(PREFIX)gcc
AR		:= $(PREFIX)ar
AS		:= $(PREFIX)as
OBJCOPY		:= $(PREFIX)objcopy
OBJDUMP		:= $(PREFIX)objdump
GDB		:= $(PREFIX)gdb


CFLAGS		+= -I$(OPENCM3_DIR)/include -Imspdebug/util -Imspdebug/drivers

CFLAGS	+= -Os -std=c11 -g -DSTM32F4
# Note: libopencm3 requires some standard libc definitions from stdint.h and stdbool.h, so we don't pass -nostdinc here.
CFLAGS	+= -nostdlib -ffreestanding
CFLAGS	+= -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -g
CFLAGS	+= -Wextra -Wshadow -Wimplicit-function-declaration -Wundef
CFLAGS	+= -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
CFLAGS	+= -fno-common -ffunction-sections -fdata-sections

LDFLAGS		+= --static -nostartfiles
LDFLAGS		+= -T$(LDSCRIPT)
LDFLAGS		+= -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -g
LDFLAGS		+= -Wl,--cre
#LDFLAGS	+= -Wl,--gc-sections
LDFLAGS		+= -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
LDFLAGS		+= -L$(OPENCM3_DIR)/lib
LDFLAGS		+= -l$(LIBNAME) $(shell $(CC) -print-libgcc-file-name)

all: elf

elf: $(BUILDDIR)/$(BINARY).elf
bin: $(BUILDDIR)/$(BINARY).bin
hex: $(BUILDDIR)/$(BINARY).hex
srec: $(BUILDDIR)/$(BINARY).srec
list: $(BUILDDIR)/$(BINARY).list

images: $(BUILDDIR)/$(BINARY).images

$(OPENCM3_DIR)/lib/lib$(LIBNAME).a:
	echo "Warning, $@ not found, attempting to rebuild in $(OPENCM3_DIR)"
	$(MAKE) -C $(OPENCM3_DIR)

OBJS := $(addprefix $(BUILDDIR)/,$(SOURCES:%.c=%.o))

# Define a helper macro for debugging make errors online
# you can type "make print-OPENCM3_DIR" and it will show you
# how that ended up being resolved by all of the included
# makefiles.
print-%:
	@echo $*=$($*)

$(BUILDDIR)/%.images: $(BUILDDIR)/%.bin $(BUILDDIR)/%.hex $(BUILDDIR)/%.srec $(BUILDDIR)/%.list $(BUILDDIR)/%.map

$(BUILDDIR)/%.bin: $(BUILDDIR)/%.elf
	$(OBJCOPY) -Obinary $< $@

$(BUILDDIR)/%.hex: $(BUILDDIR)/%.elf
	$(OBJCOPY) -Oihex $< $@

$(BUILDDIR)/%.srec: $(BUILDDIR)/%.elf
	$(OBJCOPY) -Osrec $< $@

$(BUILDDIR)/%.list: $(BUILDDIR)/%.elf
	$(OBJDUMP) -S $< > $@

$(BUILDDIR)/%.elf: $(OBJS) $(LDSCRIPT) $(OPENCM3_DIR)/lib/lib$(LIBNAME).a
	$(LD) $(OBJS) $(LDFLAGS) -o $@ -Wl,-Map=$*.map 

$(BUILDDIR)/%.o: %.c
	mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -o $@ -c $<

clean:
	-rm -r $(BUILDDIR)

.PHONY: images clean elf bin hex srec list

-include $(OBJS:.o=.d)