summaryrefslogtreecommitdiff
path: root/controller/fw/Makefile
blob: e2f0f4cfc2076d895d7a16d9b984580d61f026d1 (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
OPENCM3_DIR 	?= libopencm3
CMSIS_DIR 	?= cmsis
MSPDEBUG_DIR 	?= mspdebug
LIBSODIUM_DIR	?= libsodium
TINYAES_DIR	?= tinyaes
MUSL_DIR	?= musl

C_SOURCES		:= src/main.c src/mspdebug_wrapper.c src/spi_flash.c
C_SOURCES		+= $(MSPDEBUG_DIR)/drivers/jtaglib.c
C_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/TransformFunctions/arm_rfft_f32.c
C_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/TransformFunctions/arm_bitreversal.c
C_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/TransformFunctions/arm_cfft_radix4_f32.c
C_SOURCES		+= $(MUSL_DIR)/src/math/tanhf.c $(MUSL_DIR)/src/math/atanhf.c
C_SOURCES		+= $(MUSL_DIR)/src/math/expm1f.c $(MUSL_DIR)/src/math/log1pf.c

CXX_SOURCES		+= src/ldpc_wrapper.cpp

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

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

OPENCM3_DIR_ABS 	:= $(abspath $(OPENCM3_DIR))
CMSIS_DIR_ABS 		:= $(abspath $(CMSIS_DIR))
MSPDEBUG_DIR_ABS 	:= $(abspath $(MSPDEBUG_DIR))
LIBSODIUM_DIR_ABS	:= $(abspath $(LIBSODIUM_DIR))
TINYAES_DIR_ABS		:= $(abspath $(TINYAES_DIR))
MUSL_DIR_ABS		:= $(abspath $(MUSL_DIR))

CFLAGS		+= -I$(OPENCM3_DIR_ABS)/include -Imspdebug/util -Imspdebug/drivers
CFLAGS		+= -I$(CMSIS_DIR_ABS)/CMSIS/DSP/Include -I$(CMSIS_DIR_ABS)/CMSIS/Core/Include
CFLAGS		+= -I$(abspath musl_include_shims)

CFLAGS	+= -Os -std=gnu11 -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
CFLAGS	+= -fno-common -ffunction-sections -fdata-sections

INT_CFLAGS	+= -Wall -Wextra -Wshadow -Wimplicit-function-declaration -Wundef
INT_CFLAGS	+= -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes

CXXFLAGS	+= -Os -g
CXXFLAGS	+= -nostdlib -ffreestanding
CXXFLAGS	+= -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16
CXXFLAGS	+= -fno-common -ffunction-sections -fdata-sections
CXXFLAGS	+= -Wall -Wextra -Wshadow -Wundef -Wredundant-decls
CXXFLAGS	+= -I.

LDFLAGS		+= --static -nostartfiles
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_ABS)/lib -l$(OPENCM3_LIB) $(shell $(CC) -print-libgcc-file-name)

all: $(BUILDDIR)/$(BINARY)

OBJS := $(addprefix $(BUILDDIR)/,$(C_SOURCES:.c=.o) $(CXX_SOURCES:.cpp=.o))

$(BUILDDIR)/$(BINARY): $(OBJS) $(OPENCM3_DIR)/lib/lib$(OPENCM3_LIB).a $(BUILDDIR)/libsodium/src/libsodium/.libs/libsodium.a $(BUILDDIR)/tinyaes/aes.o
	$(LD) -T$(LDSCRIPT) $(LDFLAGS) -o $@ -Wl,-Map=$(BUILDDIR)/src/$*.map $^

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

$(BUILDDIR)/src/%.o: src/%.cpp
	mkdir -p $(@D)
	$(CXX) $(CXXFLAGS) -o $@ -c $<

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

$(OPENCM3_DIR)/lib/lib$(OPENCM3_LIB).a:
	$(MAKE) -C $(OPENCM3_DIR) -j $(shell nproc)

$(BUILDDIR)/libsodium/src/libsodium/.libs/libsodium.a:
	mkdir -p $(BUILDDIR)/libsodium
	cd $(BUILDDIR)/libsodium && CFLAGS="$(CFLAGS) -DDEV_MODE=1" $(LIBSODIUM_DIR_ABS)/configure --host=arm-none-eabi && $(MAKE) -j $(shell nproc)

$(BUILDDIR)/tinyaes/aes.o:
	mkdir -p $(@D)
	make -C $(@D) -f $(TINYAES_DIR_ABS)/Makefile VPATH=$(TINYAES_DIR_ABS) CFLAGS="$(CFLAGS) -c" CC=$(CC) LD=$(LD) AR=$(AR) aes.o

clean:
	-rm -r $(BUILDDIR)/src
	-rm $(BUILDDIR)/$(BINARY)

mrproper: clean
	-rm -r build
	-$(MAKE) -C $(OPENCM3_DIR) clean

.PHONY: clean mrproper

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