aboutsummaryrefslogtreecommitdiff
path: root/driver_fw/Makefile
diff options
context:
space:
mode:
Diffstat (limited to 'driver_fw/Makefile')
-rw-r--r--driver_fw/Makefile231
1 files changed, 175 insertions, 56 deletions
diff --git a/driver_fw/Makefile b/driver_fw/Makefile
index 85bc4c7..c9393bf 100644
--- a/driver_fw/Makefile
+++ b/driver_fw/Makefile
@@ -1,73 +1,192 @@
-CUBE_PATH ?= $(wildcard ~)/ref/stm32cube/STM32CubeF0
-CMSIS_PATH ?= $(CUBE_PATH)/Drivers/CMSIS
-CMSIS_DEV_PATH ?= $(CMSIS_PATH)/Device/ST/STM32F0xx
-HAL_PATH ?= $(CUBE_PATH)/Drivers/STM32F0xx_HAL_Driver
-MAC_ADDR ?= 0xdeadbeef
+########################################################################################################################
+# Dependency directories
+########################################################################################################################
-CC := arm-none-eabi-gcc
-LD := arm-none-eabi-ld
-OBJCOPY := arm-none-eabi-objcopy
-OBJDUMP := arm-none-eabi-objdump
-SIZE := arm-none-eabi-size
+MUSL_DIR ?= upstream/musl
-CFLAGS = -g -Wall -std=gnu11 -O0 -fdump-rtl-expand -Wno-discarded-qualifiers
-CFLAGS += -mlittle-endian -mcpu=cortex-m0 -mthumb
-#CFLAGS += -ffunction-sections -fdata-sections
-LDFLAGS = -nostartfiles
-#LDFLAGS += -specs=rdimon.specs -DSEMIHOSTING
-LDFLAGS += -Wl,-Map=main.map -nostdlib
-#LDFLAGS += -Wl,--gc-sections
-LIBS = -lgcc
-#LIBS += -lrdimon
+########################################################################################################################
+# Algorithm parameters
+########################################################################################################################
-# Technically we're using an STM32F030F4, but apart from the TSSOP20 package that one is largely identical to the
-# STM32F030*6 and there is no separate device header provided for it, so we're faking a *6 device here. This is
-# even documented in stm32f0xx.h. Thanks ST!
-CFLAGS += -DSTM32F030x6 -DHSE_VALUE=8000000 -DMAC_ADDR=$(MAC_ADDR)
+# - none -
-LDFLAGS += -Tstm32_flash.ld
-CFLAGS += -I$(CMSIS_DEV_PATH)/Include -I$(CMSIS_PATH)/Include -I$(HAL_PATH)/Inc -Iconfig -I../common
-#LDFLAGS += -L$(CMSIS_PATH)/Lib/GCC -larm_cortexM0l_math
+########################################################################################################################
+# High-level build parameters
+########################################################################################################################
-SOURCES = main.c startup_stm32f030x6.s system_stm32f0xx.c base.c $(HAL_PATH)/Src/stm32f0xx_ll_utils.c cmsis_exports.c \
- ../common/8b10b.c serial.c mac.c i2c.c lcd1602.c mcp9801.c ina226.c mini-printf.c
+DEBUG ?= 1
+OPT ?= 0
-###################################################
+BUILDDIR ?= build
+BINARY := main.elf
+LDSCRIPT := generic_stm32.ld
+DEVICE := STM32G070RB
-.PHONY: program clean
-all: main.elf
+########################################################################################################################
+# Sources
+########################################################################################################################
-.clang:
- echo flags = $(CFLAGS) > .clang
+ASM_SOURCES := startup.s
-cmsis_exports.c: $(CMSIS_DEV_PATH)/Include/stm32f030x6.h $(CMSIS_PATH)/Include/core_cm0.h
- python3 gen_cmsis_exports.py $^ > $@
+C_SOURCES := src/main.c
-%.o: %.c
- $(CC) -c $(CFLAGS) -o $@ $^
-# $(CC) -E $(CFLAGS) -o $(@:.o=.pp) $^
+CPP_SOURCES := # - none -
-%.o: %.s
- $(CC) -c $(CFLAGS) -o $@ $^
-# $(CC) -E $(CFLAGS) -o $(@:.o=.pp) $^
+MUSL_SOURCES := # - none -
+MUSL_SOURCES := $(addprefix $(MUSL_DIR)/src/,$(MUSL_SOURCES))
+
+C_SOURCES += $(MUSL_SOURCES)
+
+
+########################################################################################################################
+# Low-level build parameters
+########################################################################################################################
+
+PREFIX ?= arm-none-eabi-
+
+HOSTCC := gcc
+CC := $(PREFIX)gcc
+CPP := $(PREFIX)cpp
+CXX := $(PREFIX)g++
+LD := $(PREFIX)gcc
+AR := $(PREFIX)ar
+AS := $(PREFIX)as
+SIZE := $(PREFIX)size
+NM := $(PREFIX)nm
+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
+DOT ?= dot
+
+MUSL_DIR_ABS := $(abspath $(MUSL_DIR))
+CMSIS_DEVICE_DIR_ABS := $(abspath $(CMSIS_DEVICE_DIR))
+
+DEVICE_FAMILY := $(shell echo $(DEVICE) | grep -Eio 'STM32[a-z]{1,2}[0-9]'|cut -c 6-)
+DEVICE_DEFINES := -DSTM32$(DEVICE_FAMILY) $(addprefix -D,$(shell cat stm32_buildinfo.defines))
+
+ARCH_FLAGS ?= -mthumb -mcpu=cortex-m0 -mfloat-abi=soft
+SYSTEM_FLAGS ?= -nostdlib -ffreestanding -nostartfiles
+
+COMMON_CFLAGS += -I$(abspath include)
+COMMON_CFLAGS += -I$(BUILDDIR)
+
+CFLAGS += -I$(abspath tools/musl_include_shims)
+CFLAGS += -I$(abspath upstream/libusb_stm32/inc)
+CFLAGS += -I$(CMSIS_DEVICE_DIR_ABS)/Include
+
+CFLAGS += $(ARCH_FLAGS) $(SYSTEM_FLAGS)
+CFLAGS += -fno-common -ffunction-sections -fdata-sections
+
+COMMON_CFLAGS += -O$(OPT) -std=gnu2x -g
+COMMON_CFLAGS += $(DEVICE_DEFINES)
+COMMON_CFLAGS += -DDEBUG=$(DEBUG)
+
+HOST_CFLAGS += $(COMMON_CFLAGS)
+
+# for musl
+CFLAGS += -Dhidden=
+
+SIM_CFLAGS += -lm -DSIMULATION
+SIM_CFLAGS += -Wall -Wextra -Wpedantic -Wshadow -Wimplicit-function-declaration -Wundef -Wno-unused-parameter
+
+INT_CFLAGS += -Wall -Wextra -Wpedantic -Wshadow -Wimplicit-function-declaration -Wundef -Wno-unused-parameter
+INT_CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
+
+CXXFLAGS += -Os -g
+CXXFLAGS += $(ARCH_FLAGS) $(SYSTEM_FLAGS)
+CXXFLAGS += -fno-common -ffunction-sections -fdata-sections
+CXXFLAGS += -Wall -Wextra -Wshadow -Wundef -Wredundant-decls
+CXXFLAGS += -I.
+
+LDFLAGS += $(ARCH_FLAGS) $(SYSTEM_FLAGS)
+
+LIBS += -lgcc
+#LDFLAGS += -Wl,--gc-sections
+
+LINKMEM_FLAGS ?= --trim-stubs=startup.o --trace-sections .isr_vector --highlight-subdirs $(BUILDDIR)
+
+OBJS := $(addprefix $(BUILDDIR)/,$(C_SOURCES:.c=.o) $(CXX_SOURCES:.cpp=.o) $(ASM_SOURCES:.s=.o))
+ALL_OBJS := $(OBJS)
+ALL_OBJS += $(BUILDDIR)/system.o
+# Add generated source here.
+
+########################################################################################################################
+# Rules
+########################################################################################################################
+
+all: binsize
+
+.PHONY: binsize
+binsize: $(BUILDDIR)/$(BINARY) $(BUILDDIR)/$(BINARY:.elf=-symbol-sizes.pdf)
+ $(LD) -T$(LDSCRIPT) $(LDFLAGS) -Wl,--print-memory-usage -o /dev/null $(ALL_OBJS) $(LIBS)
+ @echo
+ @echo "▐▬▬▬▌ SyMbOL sIzE HiGhScORe LiSt ▐▬▬▬▌"
+ $(NM) --print-size --size-sort --radix=d $< | tail -n 20
+
+.PRECIOUS: $(BUILDDIR)/$(BINARY)
+$(BUILDDIR)/$(BINARY) $(BUILDDIR)/$(BINARY:.elf=.map) &: $(ALL_OBJS)
+ $(LD) -T$(LDSCRIPT) $(LDFLAGS) -o $@ -Wl,-Map=$(BUILDDIR)/$(BINARY:.elf=.map) $(ALL_OBJS) $(LIBS)
+
+build/$(BINARY:.elf=-symbol-sizes.dot): $(ALL_OBJS)
+ $(PYTHON3) tools/linkmem.py $(LINKMEM_FLAGS) $(LD) -T$(LDSCRIPT) $(LDFLAGS) $^ $(LIBS) > $@
+
+%.pdf: %.dot
+ $(DOT) -T pdf $< -o $@
%.dot: %.elf
- r2 -a arm -qc 'aa;agC' $< 2>/dev/null >$@
-
-main.elf: $(SOURCES)
- $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
- $(OBJCOPY) -O ihex $@ $(@:.elf=.hex)
- $(OBJCOPY) -O binary $@ $(@:.elf=.bin)
- $(OBJDUMP) -St $@ >$(@:.elf=.lst)
- $(SIZE) $@
-
-program: main.elf openocd.cfg
- openocd -f openocd.cfg -c "program $< verify reset exit"
+ r2 -a arm -qc 'aa;agRd' $< 2>/dev/null >$@
-clean:
- rm -f **.o **.pp
- rm -f main.elf main.hex main.bin main.map main.lst
- rm -f **.expand
+$(BUILDDIR)/src/%.o: src/%.s
+ mkdir -p $(@D)
+ $(CC) $(COMMON_CFLAGS) $(CFLAGS) $(INT_CFLAGS) -o $@ -c $<
+
+$(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)/%.o: %.c
+ mkdir -p $(@D)
+ $(CC) $(COMMON_CFLAGS) $(CFLAGS) $(EXT_CFLAGS) -o $@ -c $<
+
+$(BUILDDIR)/%.o: %.s
+ mkdir -p $(@D)
+ $(CC) $(COMMON_CFLAGS) $(CFLAGS) $(EXT_CFLAGS) -o $@ -c $<
+
+venv:
+ test -d venv || python3 -m venv --system-site-packages venv
+ source venv/bin/activate && pip install cxxfilt pyelftools libarchive matplotlib
+
+clean:
+ rm -rf $(BUILDDIR)/src
+ rm -f $(BUILDDIR)/**.o
+ rm -f $(BUILDDIR)/$(BINARY)
+ rm -f $(BUILDDIR)/$(BINARY:.elf=.map)
+ rm -f $(BUILDDIR)/$(BINARY:.elf=-symbol-sizes.dot)
+ rm -f $(BUILDDIR)/$(BINARY:.elf=-symbol-sizes.pdf)
+ rm -f $(BUILDDIR)/crc32_test
+ rm -f $(BUILDDIR)/microcobs_test_sg
+ rm -f $(BUILDDIR)/microcobs_test
+ rm -f $(BUILDDIR)/microcobs_decode_test
+
+mrproper: clean
+ rm -rf build
+
+.PHONY: clean mrproper
+
+-include $(OBJS:.o=.d)