From 5aaef38965b3a27f48e654a6f4c0e56f31c49ded Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Wed, 20 Sep 2023 22:19:11 +0200 Subject: [PATCH] Initial commit: basic infrastructure --- .gitignore | 9 ++ .gitmodules | 6 + Makefile | 180 ++++++++++++++++++++++++++++ fxplib | 1 + ldscripts/tinyfancontrol-debug.ld | 103 ++++++++++++++++ ldscripts/tinyfancontrol-release.ld | 11 ++ libopencm3 | 1 + src/main.c | 45 +++++++ 8 files changed, 356 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 Makefile create mode 160000 fxplib create mode 100644 ldscripts/tinyfancontrol-debug.ld create mode 100644 ldscripts/tinyfancontrol-release.ld create mode 160000 libopencm3 create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f8f3e6b --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# Build results +/bin +/obj + +# Vim swap files +.*.sw? + +# generated config HEX files +utils/*.hex diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..732f4c2 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "fxplib"] + path = fxplib + url = https://github.com/cfr34k/fxplib.git +[submodule "libopencm3"] + path = libopencm3 + url = https://github.com/libopencm3/libopencm3.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f59976f --- /dev/null +++ b/Makefile @@ -0,0 +1,180 @@ +# --- START OF CONFIG --------------------------------------------------- +# Edit the following variables for your own needs + +# toolchain configuration +PREFIX ?= arm-none-eabi- +CC = $(PREFIX)gcc +LD = $(PREFIX)gcc +OBJCOPY = $(PREFIX)objcopy +OBJDUMP = $(PREFIX)objdump +GDB = $(PREFIX)gdb +SIZE = $(PREFIX)size + +OOCD = openocd +OOCD_CFG = ./oocd/tinyfancontrol.cfg + +TOOLCHAIN_DIR ?= /usr/arm-none-eabi +OPENCM3_DIR ?= ./libopencm3 + +# default build configuration +# "make BUILD=release" does a release build +BUILD:=debug + +# basic build flags configuration +CFLAGS+=-Wall -std=c99 -pedantic -Wextra -Wimplicit-function-declaration \ + -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \ + -Wundef -Wshadow \ + -fno-common -ffunction-sections -fdata-sections -mcpu=cortex-m0 -mthumb \ + -mfloat-abi=soft -MD -DSTM32L0 + +LDFLAGS+=--static \ + -L$(OPENCM3_DIR)/lib \ + -nostartfiles -Wl,--gc-sections \ + -mthumb -mcpu=cortex-m0 -mthumb -mfloat-abi=soft + +# the LD script (RAM-only does not work because the code is too large) +#LDFLAGS+=-Tldscripts/tinyfancontrol-$(BUILD).ld +LDFLAGS+=-Tldscripts/tinyfancontrol-release.ld + +# Flags for libopencm3 +CFLAGS+=-I$(OPENCM3_DIR)/include +LDFLAGS+=-L$(OPENCM3_DIR)/lib -lopencm3_stm32l0 + +# Flags for fxplib +CFLAGS+=-Ifxplib/include -DPOINTPOS=16 +LDFLAGS+=-Lfxplib/lib/$(BUILD) -lfxp_stm32l0 + +# generic linking +LDFLAGS+=-Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group + +# build type specific flags +CFLAGS_debug=-O0 -ggdb -DDEBUG +LDFLAGS_debug= + +CFLAGS_release=-Os -ggdb +LDFLAGS_release= + +# target configuration +TARGET := tinyfancontrol +VERSION := $(shell git describe --always) + +# source files for the project +SOURCE := $(shell find src/ -name '*.c') +INCLUDES := $(shell find src/ -name '*.h') + +# additional dependencies for build (proper targets must be specified by user) +DEPS := build_libfxp + +# default target +all: $(TARGET) + +# user-specific targets +export CFLAGS +export BUILD +export PREFIX +export POSTFIX = stm32l0 +build_libfxp: + cd fxplib && $(MAKE) + +# --- END OF CONFIG ----------------------------------------------------- + +OBJ1=$(patsubst %.c, %.o, $(SOURCE)) +OBJ=$(patsubst src/%, obj/$(BUILD)/%, $(OBJ1)) + +VERSIONSTR="\"$(VERSION)\"" + +CFLAGS+=-DVERSION=$(VERSIONSTR) + +TARGET_BASE := bin/$(BUILD)/$(TARGET) + +CFLAGS+=$(CFLAGS_$(BUILD)) +LDFLAGS+=$(LDFLAGS_$(BUILD)) + +.PHONY show_cflags: + @echo --- Build parameters: ------------------------------------------ + @echo CFLAGS\=$(CFLAGS) + @echo LDFLAGS\=$(LDFLAGS) + @echo SOURCE\=$(SOURCE) + @echo ----------------------------------------------------------------- + +$(TARGET): show_cflags $(TARGET_BASE).elf $(TARGET_BASE).hex \ + $(TARGET_BASE).lss $(TARGET_BASE).bin + @$(SIZE) $(TARGET_BASE).elf + @echo ">>> $(BUILD) build complete." + +$(TARGET_BASE).elf: $(DEPS) $(OBJ) $(INCLUDES) Makefile + @echo Linking $@ ... + @mkdir -p $(shell dirname $@) + @$(LD) -o $(TARGET_BASE).elf $(OBJ) $(LDFLAGS) + +$(TARGET_BASE).hex: $(TARGET_BASE).elf + @echo "Generating $@ ..." + @$(OBJCOPY) -Oihex $< $@ + +$(TARGET_BASE).bin: $(TARGET_BASE).elf + @echo "Generating $@ ..." + @$(OBJCOPY) -Obinary $< $@ + +$(TARGET_BASE).lss: $(TARGET_BASE).elf + @echo "Generating $@ ..." + @$(OBJDUMP) -S $< > $@ + +obj/$(BUILD)/%.o: src/%.c $(INCLUDES) Makefile + @echo "Compiling $< ..." + @mkdir -p $(shell dirname $@) + @$(CC) -c $(CFLAGS) -o $@ $< + +clean: + rm -f $(TARGET_BASE).elf + rm -f $(TARGET_BASE).hex + rm -f $(TARGET_BASE).lss + rm -f $(TARGET_BASE).bin + rm -f $(OBJ) + +program: program_jlink + +reset: reset_jlink + +/tmp/jlink_prog_script: Makefile + echo "Device STM32F030C8" > $@ + echo "connect" >> $@ + echo "S" >> $@ + echo "4000" >> $@ + echo "loadfile $(TARGET_BASE).hex" >> $@ + echo "r" >> $@ + echo "g" >> $@ + echo "exit" >> $@ + +/tmp/jlink_rst_script: Makefile + echo "Device STM32F030C8" > $@ + echo "connect" >> $@ + echo "S" >> $@ + echo "4000" >> $@ + echo "r" >> $@ + echo "g" >> $@ + echo "exit" >> $@ + +program_jlink: /tmp/jlink_prog_script $(TARGET_BASE).hex + JLinkExe ram + + /* C++ Static constructors/destructors, also used for __attribute__ + * ((constructor)) and the likes */ + .preinit_array : { + . = ALIGN(4); + __preinit_array_start = .; + KEEP (*(.preinit_array)) + __preinit_array_end = .; + } >ram + .init_array : { + . = ALIGN(4); + __init_array_start = .; + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array)) + __init_array_end = .; + } >ram + .fini_array : { + . = ALIGN(4); + __fini_array_start = .; + KEEP (*(.fini_array)) + KEEP (*(SORT(.fini_array.*))) + __fini_array_end = .; + } >ram + + /* + * Another section used by C++ stuff, appears when using newlib with + * 64bit (long long) printf support + */ + .ARM.extab : { + *(.ARM.extab*) + } >ram + .ARM.exidx : { + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; + } >ram + + . = ALIGN(4); + _etext = .; + + .data : { + _data = .; + *(.data*) /* Read-write initialized data */ + . = ALIGN(4); + _edata = .; + } >ram + + .bss : { + *(.bss*) /* Read-write zero initialized data */ + *(COMMON) + . = ALIGN(4); + _ebss = .; + } >ram + + /* + * The .eh_frame section appears to be used for C++ exception handling. + * You may need to fix this if you're using C++. + */ + /DISCARD/ : { *(.eh_frame) } + + . = ALIGN(4); + end = .; +} + +PROVIDE(_stack = ORIGIN(ram) + LENGTH(ram)); + + diff --git a/ldscripts/tinyfancontrol-release.ld b/ldscripts/tinyfancontrol-release.ld new file mode 100644 index 0000000..5c2cd72 --- /dev/null +++ b/ldscripts/tinyfancontrol-release.ld @@ -0,0 +1,11 @@ +/* Linker script for LNSC-2420: STM32F0K6T6*/ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 64K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 8K +} + +/* Include the common ld script. */ +INCLUDE cortex-m-generic.ld diff --git a/libopencm3 b/libopencm3 new file mode 160000 index 0000000..32a1692 --- /dev/null +++ b/libopencm3 @@ -0,0 +1 @@ +Subproject commit 32a169207775d6c53c536d46b78ecf8eca3fdd18 diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..5b3b968 --- /dev/null +++ b/src/main.c @@ -0,0 +1,45 @@ +#include +#include +#include + +/* Set up systick to fire freq times per second */ +static void init_systick(int freq) +{ + systick_set_clocksource(STK_CSR_CLKSOURCE_AHB); + /* clear counter so it starts right away */ + STK_CVR = 0; + + systick_set_reload(rcc_ahb_frequency / freq); + systick_counter_enable(); + systick_interrupt_enable(); +} + + +int main(void) +{ + uint64_t timebase_ms = 0; + + init_systick(1000); + + //rs485_enqueue("LNSC-2420 v" VERSION " initialized.\n"); + + // triggered every 1 ms + while (1) { + __WFI(); + } + + return 0; +} + + +/* Called when systick fires */ +void sys_tick_handler(void) +{ + //wait_frame = 0; +} + + +void hard_fault_handler(void) +{ + while (1); +}