From df6f89abeab60e91a348484cdceadb758290f234 Mon Sep 17 00:00:00 2001 From: Physick <96335032+DegustatorPonos@users.noreply.github.com> Date: Thu, 18 Dec 2025 16:37:25 +0500 Subject: Template init --- .gitignore | 3 + .gitmodules | 3 + GenClangd.py | 6 ++ Makefile | 6 ++ README.md | 3 + libopencm3 | 1 + rules.mk | 177 +++++++++++++++++++++++++++++++++++++++++++ src/Makefile | 17 +++++ src/bin/main.d | 18 +++++ src/bin/main.o | Bin 0 -> 77016 bytes src/blink-led.bin | Bin 0 -> 692 bytes src/blink-led.elf | Bin 0 -> 92140 bytes src/generated.stm32f103c8.ld | 69 +++++++++++++++++ src/main.c | 14 ++++ 14 files changed, 317 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 GenClangd.py create mode 100644 Makefile create mode 100644 README.md create mode 160000 libopencm3 create mode 100644 rules.mk create mode 100644 src/Makefile create mode 100644 src/bin/main.d create mode 100644 src/bin/main.o create mode 100755 src/blink-led.bin create mode 100755 src/blink-led.elf create mode 100644 src/generated.stm32f103c8.ld create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fe9c26d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.clangd + +libopencm3 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6bccd68 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libopencm3"] + path = libopencm3 + url = https://github.com/libopencm3/libopencm3 diff --git a/GenClangd.py b/GenClangd.py new file mode 100644 index 0000000..7616559 --- /dev/null +++ b/GenClangd.py @@ -0,0 +1,6 @@ +import os + +if __name__ == "__main__": + path = os.getcwd() + with open(".clangd", "w") as file: + file.write("CompileFlags:\n Add: -I"+path+"/libopencm3/include\n") diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e9068fb --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +all: + make -C src + +flash: + make -C src + st-flash --reset write src/*.bin 0x8000000 diff --git a/README.md b/README.md new file mode 100644 index 0000000..96f5491 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# STM32F103 Bluepill template + +## Links diff --git a/libopencm3 b/libopencm3 new file mode 160000 index 0000000..5e7dc5d --- /dev/null +++ b/libopencm3 @@ -0,0 +1 @@ +Subproject commit 5e7dc5d092e52bbfbb8b5929e2097732e1b7f81c diff --git a/rules.mk b/rules.mk new file mode 100644 index 0000000..e417d2f --- /dev/null +++ b/rules.mk @@ -0,0 +1,177 @@ +# This version of rules.mk expects the following to be defined before +# inclusion.. +### REQUIRED ### +# OPENCM3_DIR - duh +# PROJECT - will be the basename of the output elf, eg usb-gadget0-stm32f4disco +# CFILES - basenames only, eg main.c blah.c +# CXXFILES - same for C++ files. Must have cxx suffix! +# DEVICE - the full device name, eg stm32f405ret6 +# _or_ +# LDSCRIPT - full path, eg ../../examples/stm32/f4/stm32f4-discovery/stm32f4-discovery.ld +# OPENCM3_LIB - the basename, eg: opencm3_stm32f4 +# OPENCM3_DEFS - the target define eg: -DSTM32F4 +# ARCH_FLAGS - eg, -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 +# (ie, the full set of cpu arch flags, _none_ are defined in this file) +# +### OPTIONAL ### +# INCLUDES - fully formed -I paths, if you want extra, eg -I../shared +# BUILD_DIR - defaults to bin, should set this if you are building multiarch +# OPT - full -O flag, defaults to -Os +# CSTD - defaults -std=c99 +# CXXSTD - no default. +# OOCD_INTERFACE - eg stlink-v2 +# OOCD_TARGET - eg stm32f4x +# both only used if you use the "make flash" target. +# OOCD_FILE - eg my.openocd.cfg +# This overrides interface/target above, and is used as just -f FILE +### TODO/FIXME/notes ### +# No support for stylecheck. +# No support for BMP/texane/random flash methods, no plans either +# No support for magically finding the library. +# C++ hasn't been actually tested with this..... sorry bout that. ;) +# Second expansion/secondary not set, add this if you need them. + +BUILD_DIR ?= bin +OPT ?= -Os +CSTD ?= -std=c99 + +# Be silent per default, but 'make V=1' will show all compiler calls. +# If you're insane, V=99 will print out all sorts of things. +V?=0 +ifeq ($(V),0) +Q := @ +NULL := 2>/dev/null +endif + +# Tool paths. +PREFIX ?= arm-none-eabi- +CC = $(PREFIX)gcc +CXX = $(PREFIX)g++ +LD = $(PREFIX)gcc +OBJCOPY = $(PREFIX)objcopy +OBJDUMP = $(PREFIX)objdump +OOCD ?= openocd + +OPENCM3_INC = $(OPENCM3_DIR)/include + +# Inclusion of library header files +INCLUDES += $(patsubst %,-I%, . $(OPENCM3_INC) ) + +OBJS = $(CFILES:%.c=$(BUILD_DIR)/%.o) +OBJS += $(CXXFILES:%.cxx=$(BUILD_DIR)/%.o) +OBJS += $(AFILES:%.S=$(BUILD_DIR)/%.o) +GENERATED_BINS = $(PROJECT).elf $(PROJECT).bin $(PROJECT).map $(PROJECT).list $(PROJECT).lss + +TGT_CPPFLAGS += -MD +TGT_CPPFLAGS += -Wall -Wundef $(INCLUDES) +TGT_CPPFLAGS += $(INCLUDES) $(OPENCM3_DEFS) + +TGT_CFLAGS += $(OPT) $(CSTD) -ggdb3 +TGT_CFLAGS += $(ARCH_FLAGS) +TGT_CFLAGS += -fno-common +TGT_CFLAGS += -ffunction-sections -fdata-sections +TGT_CFLAGS += -Wextra -Wshadow -Wno-unused-variable -Wimplicit-function-declaration +TGT_CFLAGS += -Wredundant-decls -Wstrict-prototypes -Wmissing-prototypes + +TGT_CXXFLAGS += $(OPT) $(CXXSTD) -ggdb3 +TGT_CXXFLAGS += $(ARCH_FLAGS) +TGT_CXXFLAGS += -fno-common +TGT_CXXFLAGS += -ffunction-sections -fdata-sections +TGT_CXXFLAGS += -Wextra -Wshadow -Wredundant-decls -Weffc++ + +TGT_ASFLAGS += $(OPT) $(ARCH_FLAGS) -ggdb3 + +TGT_LDFLAGS += -T$(LDSCRIPT) -L$(OPENCM3_DIR)/lib -nostartfiles +TGT_LDFLAGS += $(ARCH_FLAGS) +TGT_LDFLAGS += -specs=nano.specs +TGT_LDFLAGS += -Wl,--gc-sections +# OPTIONAL +#TGT_LDFLAGS += -Wl,-Map=$(PROJECT).map +ifeq ($(V),99) +TGT_LDFLAGS += -Wl,--print-gc-sections +endif + +# Linker script generator fills this in for us. +ifeq (,$(DEVICE)) +LDLIBS += -l$(OPENCM3_LIB) +endif +# nosys is only in newer gcc-arm-embedded... +#LDLIBS += -specs=nosys.specs +LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group + +# Burn in legacy hell fortran modula pascal yacc idontevenwat +.SUFFIXES: +.SUFFIXES: .c .S .h .o .cxx .elf .bin .list .lss + +# Bad make, never *ever* try to get a file out of source control by yourself. +%: %,v +%: RCS/%,v +%: RCS/% +%: s.% +%: SCCS/s.% + +all: $(PROJECT).elf $(PROJECT).bin +flash: $(PROJECT).flash + +# error if not using linker script generator +ifeq (,$(DEVICE)) +$(LDSCRIPT): +ifeq (,$(wildcard $(LDSCRIPT))) + $(error Unable to find specified linker script: $(LDSCRIPT)) +endif +else +# if linker script generator was used, make sure it's cleaned. +GENERATED_BINS += $(LDSCRIPT) +endif + +# Need a special rule to have a bin dir +$(BUILD_DIR)/%.o: %.c + @printf " CC\t$<\n" + @mkdir -p $(dir $@) + $(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $< + +$(BUILD_DIR)/%.o: %.cxx + @printf " CXX\t$<\n" + @mkdir -p $(dir $@) + $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $< + +$(BUILD_DIR)/%.o: %.S + @printf " AS\t$<\n" + @mkdir -p $(dir $@) + $(Q)$(CC) $(TGT_ASFLAGS) $(ASFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $< + +$(PROJECT).elf: $(OBJS) $(LDSCRIPT) $(LIBDEPS) + @printf " LD\t$@\n" + $(Q)$(LD) $(TGT_LDFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@ + +%.bin: %.elf + @printf " OBJCOPY\t$@\n" + $(Q)$(OBJCOPY) -O binary $< $@ + +%.lss: %.elf + $(OBJDUMP) -h -S $< > $@ + +%.list: %.elf + $(OBJDUMP) -S $< > $@ + +%.flash: %.elf + @printf " FLASH\t$<\n" +ifeq (,$(OOCD_FILE)) + $(Q)(echo "halt; program $(realpath $(*).elf) verify reset" | nc -4 localhost 4444 2>/dev/null) || \ + $(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ + -f target/$(OOCD_TARGET).cfg \ + -c "program $(realpath $(*).elf) verify reset exit" \ + $(NULL) +else + $(Q)(echo "halt; program $(realpath $(*).elf) verify reset" | nc -4 localhost 4444 2>/dev/null) || \ + $(OOCD) -f $(OOCD_FILE) \ + -c "program $(realpath $(*).elf) verify reset exit" \ + $(NULL) +endif + +clean: + rm -rf $(BUILD_DIR) $(GENERATED_BINS) + +.PHONY: all clean flash +-include $(OBJS:.o=.d) + diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..9e86f13 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,17 @@ +PROJECT = blink-led +BUILD_DIR = bin + +CFILES = main.c + +# TODO - you will need to edit these two lines! +DEVICE=stm32f103c8 +OOCD_FILE = board/stm32f4discovery.cfg + +# You shouldn't have to edit anything below here. +VPATH += $(SHARED_DIR) +INCLUDES += $(patsubst %,-I%, . $(SHARED_DIR)) +OPENCM3_DIR=../libopencm3 + +include $(OPENCM3_DIR)/mk/genlink-config.mk +include ../rules.mk +include $(OPENCM3_DIR)/mk/genlink-rules.mk diff --git a/src/bin/main.d b/src/bin/main.d new file mode 100644 index 0000000..0bf80b9 --- /dev/null +++ b/src/bin/main.d @@ -0,0 +1,18 @@ +bin/main.o: main.c ../libopencm3/include/libopencm3/stm32/rcc.h \ + ../libopencm3/include/libopencm3/cm3/common.h \ + /usr/lib/gcc/arm-none-eabi/14.2.0/include/stdint.h \ + /usr/arm-none-eabi/include/stdint.h \ + /usr/arm-none-eabi/include/machine/_default_types.h \ + /usr/arm-none-eabi/include/sys/features.h \ + /usr/arm-none-eabi/include/_newlib_version.h \ + /usr/arm-none-eabi/include/sys/_intsup.h \ + /usr/arm-none-eabi/include/sys/_stdint.h \ + /usr/lib/gcc/arm-none-eabi/14.2.0/include/stdbool.h \ + ../libopencm3/include/libopencm3/stm32/memorymap.h \ + ../libopencm3/include/libopencm3/stm32/f1/memorymap.h \ + ../libopencm3/include/libopencm3/cm3/memorymap.h \ + ../libopencm3/include/libopencm3/stm32/f1/rcc.h \ + ../libopencm3/include/libopencm3/stm32/common/rcc_common_all.h \ + ../libopencm3/include/libopencm3/stm32/gpio.h \ + ../libopencm3/include/libopencm3/stm32/f1/gpio.h \ + ../libopencm3/include/libopencm3/stm32/common/gpio_common_all.h diff --git a/src/bin/main.o b/src/bin/main.o new file mode 100644 index 0000000..faa4061 Binary files /dev/null and b/src/bin/main.o differ diff --git a/src/blink-led.bin b/src/blink-led.bin new file mode 100755 index 0000000..028bf9d Binary files /dev/null and b/src/blink-led.bin differ diff --git a/src/blink-led.elf b/src/blink-led.elf new file mode 100755 index 0000000..05500f1 Binary files /dev/null and b/src/blink-led.elf differ diff --git a/src/generated.stm32f103c8.ld b/src/generated.stm32f103c8.ld new file mode 100644 index 0000000..7b7234a --- /dev/null +++ b/src/generated.stm32f103c8.ld @@ -0,0 +1,69 @@ +EXTERN(vector_table) +ENTRY(reset_handler) +MEMORY +{ + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K + rom (rx) : ORIGIN = 0x08000000, LENGTH = 64K +} +SECTIONS +{ + .text : { + *(.vectors) + *(.text*) + . = ALIGN(4); + *(.rodata*) + . = ALIGN(4); + } >rom + .preinit_array : { + . = ALIGN(4); + __preinit_array_start = .; + KEEP (*(.preinit_array)) + __preinit_array_end = .; + } >rom + .init_array : { + . = ALIGN(4); + __init_array_start = .; + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array)) + __init_array_end = .; + } >rom + .fini_array : { + . = ALIGN(4); + __fini_array_start = .; + KEEP (*(.fini_array)) + KEEP (*(SORT(.fini_array.*))) + __fini_array_end = .; + } >rom + .ARM.extab : { + *(.ARM.extab*) + } >rom + .ARM.exidx : { + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; + } >rom + . = ALIGN(4); + _etext = .; + .noinit (NOLOAD) : { + *(.noinit*) + } >ram + . = ALIGN(4); + .data : { + _data = .; + *(.data*) + *(.ramtext*) + . = ALIGN(4); + _edata = .; + } >ram AT >rom + _data_loadaddr = LOADADDR(.data); + .bss : { + *(.bss*) + *(COMMON) + . = ALIGN(4); + _ebss = .; + } >ram + /DISCARD/ : { *(.eh_frame) } + . = ALIGN(4); + end = .; +} +PROVIDE(_stack = ORIGIN(ram) + LENGTH(ram)); diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..7b446ca --- /dev/null +++ b/src/main.c @@ -0,0 +1,14 @@ +#define STM32F1 +#include +#include + +int main(void) { + rcc_periph_clock_enable(RCC_GPIOC); + + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); + while (1) { + for (int i = 0; i < 1000000; i++) + __asm__("nop"); + gpio_toggle(GPIOC, GPIO13); + } +} -- cgit v1.3