summaryrefslogtreecommitdiff
path: root/controller/fw/Makefile
blob: 3b9cca680ad11bb590447145516a8205f47ca02d (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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 src/freq_meas.c src/dsss_demod.c
C_SOURCES		+= $(MSPDEBUG_DIR)/drivers/jtaglib.c

CMSIS_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/TransformFunctions/arm_rfft_fast_init_f32.c
CMSIS_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/TransformFunctions/arm_rfft_fast_f32.c
CMSIS_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/TransformFunctions/arm_cfft_init_f32.c
CMSIS_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/TransformFunctions/arm_cfft_f32.c
CMSIS_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/TransformFunctions/arm_cfft_radix8_f32.c
CMSIS_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/CommonTables/arm_const_structs.c
CMSIS_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/CommonTables/arm_common_tables.c
CMSIS_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/TransformFunctions/arm_bitreversal.c
CMSIS_SOURCES		+= $(CMSIS_DIR)/CMSIS/DSP/Source/TransformFunctions/arm_bitreversal2.c

MUSL_SOURCES		+= $(MUSL_DIR)/src/math/tanhf.c $(MUSL_DIR)/src/math/atanhf.c
MUSL_SOURCES		+= $(MUSL_DIR)/src/math/expm1f.c $(MUSL_DIR)/src/math/log1pf.c
MUSL_SOURCES		+= $(MUSL_DIR)/src/math/expf.c $(MUSL_DIR)/src/math/exp2f_data.c
MUSL_SOURCES		+= $(MUSL_DIR)/src/math/powf.c
MUSL_SOURCES		+= $(MUSL_DIR)/src/math/__math_oflowf.c
MUSL_SOURCES		+= $(MUSL_DIR)/src/math/__math_uflowf.c
MUSL_SOURCES		+= $(MUSL_DIR)/src/math/__math_xflowf.c
MUSL_SOURCES		+= $(MUSL_DIR)/src/math/__math_invalidf.c
MUSL_SOURCES		+= $(MUSL_DIR)/src/math/powf_data.c

C_SOURCES += $(CMSIS_SOURCES) $(MUSL_SOURCES)

CXX_SOURCES		+= src/ldpc_wrapper.cpp

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

PREFIX		?= arm-none-eabi-


FMEAS_ADC_SAMPLING_RATE ?= 1000
FMEAS_ADC_MAX			?= 4096
FMEAS_FFT_LEN			?= 256
FMEAS_FFT_WINDOW 		?= gaussian
FMEAS_FFT_WINDOW_SIGMA 	?= 16.0
# TODO: validate
FMEAS_SAMPLING_RATE		?= 10.0

DSSS_GOLD_CODE_NBITS 	?= 5
DSSS_DECIMATION			?= 10
# TODO maybe auto adjust this based on detection rate?
DSSS_THESHOLD_FACTOR	?= 5.0f
DSSS_WAVELET_WIDTH		?= 7.3
DSSS_WAVELET_LUT_SIZE	?= 69
DSSS_FILTER_FC			?= 3e-3
DSSS_FILTER_ORDER		?= 12

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

HOST_CC				?= $(HOST_PREFIX)gcc
HOST_CXX			?= $(HOST_PREFIX)g++
HOST_LD				?= $(HOST_PREFIX)gcc
HOST_AR				?= $(HOST_PREFIX)ar
HOST_AS				?= $(HOST_PREFIX)as
HOST_OBJCOPY		?= $(HOST_PREFIX)objcopy
HOST_OBJDUMP		?= $(HOST_PREFIX)objdump

PYTHON3 ?= python3

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))

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

COMMON_CFLAGS	+= -Os -std=gnu11 -g -DSTM32F4
CFLAGS			+= -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16
#SIM_CFLAGS		+= -mthumb -mcpu=cortex-m4 -mfloat-abi=soft
# Note: libopencm3 requires some standard libc definitions from stdint.h and stdbool.h, so we don't pass -nostdinc here.
CFLAGS			+= -nostdlib -ffreestanding
CFLAGS			+= -fno-common -ffunction-sections -fdata-sections
COMMON_CFLAGS	+= -DDSSS_GOLD_CODE_NBITS=$(DSSS_GOLD_CODE_NBITS) -DFMEAS_FFT_LEN=$(FMEAS_FFT_LEN) -DFMEAS_ADC_MAX=$(FMEAS_ADC_MAX)
COMMON_CFLAGS	+= -DFMEAS_ADC_SAMPLING_RATE=$(FMEAS_ADC_SAMPLING_RATE)
COMMON_CFLAGS	+= -DFMEAS_FFT_WINDOW_SIGMA=$(FMEAS_FFT_WINDOW_SIGMA)
COMMON_CFLAGS	+= -DDSSS_DECIMATION=$(DSSS_DECIMATION)
COMMON_CFLAGS	+= -DDSSS_THESHOLD_FACTOR=$(DSSS_THESHOLD_FACTOR)
COMMON_CFLAGS	+= -DDSSS_WAVELET_WIDTH=$(DSSS_WAVELET_WIDTH)
COMMON_CFLAGS	+= -DDSSS_WAVELET_LUT_SIZE=$(DSSS_WAVELET_LUT_SIZE)

# for musl
CFLAGS			+= -Dhidden=

SIM_CFLAGS	+= -lm -DSIMULATION

INT_CFLAGS	+= -Wall -Wextra -Wpedantic -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.

COMMON_LDFLAGS	+= -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -g
LDFLAGS			+= --static -nostartfiles
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)

src/dsss_demod.c: $(BUILDDIR)/generated/dsss_gold_code.h $(BUILDDIR)/generated/dsss_butter_filter.h

$(BUILDDIR)/generated/dsss_gold_code.h: $(BUILDDIR)/generated/gold_code_$(DSSS_GOLD_CODE_NBITS).h
	ln -srf $< $@

.PRECIOUS: $(BUILDDIR)/generated/gold_code_%.c
$(BUILDDIR)/generated/gold_code_%.c $(BUILDDIR)/generated/gold_code_%.h: | $(BUILDDIR)/generated
	$(PYTHON3) tools/gold_code_header_gen.py -v dsss_gold_code_table -$(patsubst .%,%,$(suffix $@)) $* > $@

.PRECIOUS: $(BUILDDIR)/generated/fmeas_fft_window.c
$(BUILDDIR)/generated/fmeas_fft_window.c: | $(BUILDDIR)/generated
	$(PYTHON3) tools/fft_window_header_gen.py -v fmeas_fft_window_table $(FMEAS_FFT_WINDOW) $(FMEAS_FFT_LEN) $(FMEAS_FFT_WINDOW_SIGMA) > $@

.PRECIOUS: $(BUILDDIR)/generated/dsss_cwt_wavelet.c
$(BUILDDIR)/generated/dsss_cwt_wavelet.c: | $(BUILDDIR)/generated
	$(PYTHON3) tools/cwt_wavelet_header_gen.py -v dsss_cwt_wavelet_table $(DSSS_WAVELET_LUT_SIZE) $(DSSS_WAVELET_WIDTH) > $@

.PRECIOUS: $(BUILDDIR)/generated/dsss_butter_filter.h
$(BUILDDIR)/generated/dsss_butter_filter.h: | $(BUILDDIR)/generated
	$(PYTHON3) tools/butter_filter_gen.py -m dsss_filter $(DSSS_FILTER_FC) $(FMEAS_SAMPLING_RATE) $(DSSS_FILTER_ORDER) > $@

$(BUILDDIR)/generated: ; mkdir -p $@

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

ALL_OBJS := $(OBJS)
ALL_OBJS += $(OPENCM3_DIR)/lib/lib$(OPENCM3_LIB).a
ALL_OBJS += $(BUILDDIR)/libsodium/src/libsodium/.libs/libsodium.a
ALL_OBJS += $(BUILDDIR)/tinyaes/aes.o
ALL_OBJS += $(BUILDDIR)/levmarq/levmarq.o
ALL_OBJS += $(BUILDDIR)/generated/gold_code_$(DSSS_GOLD_CODE_NBITS).o
ALL_OBJS += $(BUILDDIR)/generated/fmeas_fft_window.o
ALL_OBJS += $(BUILDDIR)/generated/dsss_cwt_wavelet.o 

$(BUILDDIR)/$(BINARY): $(ALL_OBJS)
	$(LD) -T$(LDSCRIPT) $(COMMON_LDFLAGS) $(LDFLAGS) -o $@ -Wl,-Map=$(BUILDDIR)/src/$*.map $^

tools: $(BUILDDIR)/tools/freq_meas_test
$(BUILDDIR)/tools/freq_meas_test: tools/freq_meas_test.c src/freq_meas.c levmarq/levmarq.c $(BUILDDIR)/generated/fmeas_fft_window.c $(CMSIS_SOURCES)
	mkdir -p $(@D)
	$(HOST_CC) $(COMMON_CFLAGS) $(SIM_CFLAGS) -o $@ $^

tools: $(BUILDDIR)/tools/dsss_demod_test
$(BUILDDIR)/tools/dsss_demod_test: tools/dsss_demod_test.c src/dsss_demod.c $(BUILDDIR)/generated/dsss_cwt_wavelet.c $(BUILDDIR)/generated/gold_code_$(DSSS_GOLD_CODE_NBITS).c
	mkdir -p $(@D)
	$(HOST_CC) $(COMMON_CFLAGS) $(SIM_CFLAGS) -o $@ $^

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

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

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

$(BUILDDIR)/%.o: %.c
	mkdir -p $(@D)
	$(CC) $(COMMON_CFLAGS) $(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="$(COMMON_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="$(COMMON_CFLAGS) $(CFLAGS) -c" CC=$(CC) LD=$(LD) AR=$(AR) aes.o

$(BUILDDIR)/musl/lib/libc.a:
	mkdir -p $(BUILDDIR)/musl
	cd $(BUILDDIR)/musl && CFLAGS="$(SIM_CFLAGS) $(COMMON_CFLAGS)" CC=$(CC) LD=$(LD) AR=$(AR) $(MUSL_DIR_ABS)/configure && $(MAKE) TARGET=arm-linux-musleabihf GCC_CONFIG="-mcpu=cortex-m4 -mfloat-abi=soft" -j $(shell nproc)

build/ldpc_decoder_test.so: src/ldpc_decoder.c
	gcc -fPIC -shared -Wall -Wextra -Wpedantic -std=gnu11 -O0 -g -o $@ $^

clean:
	rm -rf $(BUILDDIR)/src
	rm -rf $(BUILDDIR)/generated
	rm -f $(BUILDDIR)/$(BINARY)
	rm -f $(BUILDDIR)/tools/freq_meas_test

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

.PHONY: clean mrproper

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