From 8cc05c79cea62609d02231492c054a0cca2b2289 Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 25 Sep 2023 23:57:36 +0200 Subject: Center firmware WIP --- center_fw/Makefile | 283 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 186 insertions(+), 97 deletions(-) (limited to 'center_fw/Makefile') diff --git a/center_fw/Makefile b/center_fw/Makefile index 98c0f5b..681eb35 100644 --- a/center_fw/Makefile +++ b/center_fw/Makefile @@ -1,102 +1,191 @@ -# Megumin LED display firmware -# Copyright (C) 2018 Sebastian Götte -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -CUBE_PATH ?= $(wildcard ~)/ref/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 - -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 - -CFLAGS = -g -Wall -std=gnu11 -O0 -fdump-rtl-expand -DMAC_ADDR=$(MAC_ADDR) -DADC_BUFSIZE=1024 -CFLAGS += -mlittle-endian -mcpu=cortex-m0 -march=armv6-m -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 - -# 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 - -LDFLAGS += -Tstm32_flash.ld -CFLAGS += -I$(CMSIS_DEV_PATH)/Include -I$(CMSIS_PATH)/Include -I$(HAL_PATH)/Inc -Iconfig -Wno-unused -I../common -LDFLAGS += -L$(CMSIS_PATH)/Lib/GCC -larm_cortexM0l_math - -################################################### - -.PHONY: program clean - -all: main.elf - -.clang: - echo flags = $(CFLAGS) > .clang - -cmsis_exports.c: $(CMSIS_DEV_PATH)/Include/stm32f030x6.h $(CMSIS_PATH)/Include/core_cm0.h - python3 tools/gen_cmsis_exports.py $^ > $@ - -%.o: %.c - $(CC) -c $(CFLAGS) -o $@ $^ -# $(CC) -E $(CFLAGS) -o $(@:.o=.pp) $^ - -%.o: %.s - $(CC) -c $(CFLAGS) -o $@ $^ -# $(CC) -E $(CFLAGS) -o $(@:.o=.pp) $^ + +######################################################################################################################## +# Dependency directories +######################################################################################################################## + +MUSL_DIR ?= upstream/musl + +######################################################################################################################## +# Algorithm parameters +######################################################################################################################## + +# - none - + +######################################################################################################################## +# High-level build parameters +######################################################################################################################## + +DEBUG ?= 1 +OPT ?= 0 + +BUILDDIR ?= build +BINARY := main.elf +LDSCRIPT := generic_stm32.ld +DEVICE := STM32G030F4 + + +######################################################################################################################## +# Sources +######################################################################################################################## + +ASM_SOURCES := startup.s + +C_SOURCES := src/main.c common/8b10b.c + +CPP_SOURCES := # - none - + +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) -I$(abspath common) +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) + +GEN_HEADERS := $(BUILDDIR)/generated/waveform_tables.h + +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 >$@ - -sources.tar.xz: main.c adc.c ../common/8b10b.c Makefile - tar -caf $@ $^ - -# don't ask... -sources.tar.xz.zip: sources.tar.xz - zip $@ $^ - -sources.c: sources.tar.xz.zip - xxd -i $< | head -n -1 | sed 's/=/__attribute__((section(".source_tarball"))) =/' > $@ - -main.elf: main.c startup_stm32f030x6.s system_stm32f0xx.c base.c - $(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 >$@ + +$(BUILDDIR)/src/%.o: src/%.s + mkdir -p $(@D) + $(CC) $(COMMON_CFLAGS) $(CFLAGS) $(INT_CFLAGS) -o $@ -c $< + +$(BUILDDIR)/src/%.o: src/%.c $(GEN_HEADERS) + 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 -f **.o - rm -f main.elf main.hex main.bin main.map main.lst - rm -f **.expand - rm -f cmsis_exports.c - rm -f sources.tar.xz - rm -f sources.tar.xz.zip - rm -f sources.c - rm -f *.dot - rm -f protocol_test + rm -rf $(BUILDDIR)/src + rm -rf $(BUILDDIR)/generated + 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) + +mrproper: clean + rm -rf build + +.PHONY: clean mrproper +-include $(OBJS:.o=.d) -- cgit