From 7ce546c185eb8b5fec30d5e6476a2b5cbf0562f8 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Tue, 4 Jan 2022 01:34:59 +0100 Subject: [PATCH] Update to recent libopencm3 --- .gitignore | 1 + 1bitsy-stm32f415rgt.ld | 32 --------- Makefile | 13 ++-- libopencm3 | 2 +- libopencm3.rules.mk | 155 ++++++++++++++++++++++++++--------------- libopencm3.target.mk | 10 +-- src/Makefile | 13 +--- src/main.c | 2 +- 8 files changed, 111 insertions(+), 117 deletions(-) delete mode 100644 1bitsy-stm32f415rgt.ld diff --git a/.gitignore b/.gitignore index 8cd3e9f..c42f2ff 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ *.map *.stylecheck nbproject +generated.* # MacOSX .DS_Store diff --git a/1bitsy-stm32f415rgt.ld b/1bitsy-stm32f415rgt.ld deleted file mode 100644 index 66e7e73..0000000 --- a/1bitsy-stm32f415rgt.ld +++ /dev/null @@ -1,32 +0,0 @@ -/* - * This file is part of the libopencm3 project. - * - * Copyright (C) 2009 Uwe Hermann - * Copyright (C) 2011 Stephen Caudle - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library. If not, see . - */ - -/* Linker script for 1Bitsy (STM32F415RGT, 1024K flash, 128K RAM). */ - -/* Define memory regions. */ -MEMORY -{ - rom (rx) : ORIGIN = 0x08000000, LENGTH = 1024K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 128K -} - -/* Include the common ld script. */ -INCLUDE libopencm3_stm32f4.ld - diff --git a/Makefile b/Makefile index 93a8ca8..5c547e1 100644 --- a/Makefile +++ b/Makefile @@ -17,8 +17,7 @@ ## along with this library. If not, see . ## -PREFIX ?= arm-none-eabi -#PREFIX ?= arm-elf +PREFIX ?= arm-none-eabi- # Be silent per default, but 'make V=1' will show all compiler calls. ifneq ($(V),1) @@ -27,11 +26,7 @@ Q := @ MAKEFLAGS += --no-print-directory endif -# In case you are using an older compiler or want to set the C standard to a -# specific version globally for the project this is where that would go. -#CFLAGS += -std=gnu11 - -OPENCM3_DIR := $(realpath libopencm3) +OPENCM3_DIR ?= $(realpath libopencm3) BUILD_RULES = elf all: build @@ -51,7 +46,7 @@ images: build build: lib src lib: - $(Q)if [ ! "`ls -A libopencm3`" ] ; then \ + $(Q)if [ ! "`ls -A $(OPENCM3_DIR)`" ] ; then \ printf "######## ERROR ########\n"; \ printf "\tlibopencm3 is not initialized.\n"; \ printf "\tPlease run:\n"; \ @@ -61,7 +56,7 @@ lib: printf "######## ERROR ########\n"; \ exit 1; \ fi - $(Q)$(MAKE) -C libopencm3 lib TARGETS="stm32/f4" + $(Q)$(MAKE) -C $(OPENCM3_DIR) lib TARGETS="stm32/f4" src: lib @printf " BUILD $@\n"; diff --git a/libopencm3 b/libopencm3 index b76d853..d030a66 160000 --- a/libopencm3 +++ b/libopencm3 @@ -1 +1 @@ -Subproject commit b76d853a723d39bbc083495bb7460cf3f071476c +Subproject commit d030a664750e6a805bfb18719100e0afa90d0ff1 diff --git a/libopencm3.rules.mk b/libopencm3.rules.mk index 7749d1e..3e0311d 100644 --- a/libopencm3.rules.mk +++ b/libopencm3.rules.mk @@ -28,27 +28,28 @@ endif ############################################################################### # Executables -PREFIX ?= arm-none-eabi - -CC := $(PREFIX)-gcc -CXX := $(PREFIX)-g++ -LD := $(PREFIX)-gcc -AR := $(PREFIX)-ar -AS := $(PREFIX)-as -OBJCOPY := $(PREFIX)-objcopy -OBJDUMP := $(PREFIX)-objdump -GDB := $(PREFIX)-gdb +PREFIX ?= arm-none-eabi- + +CC := $(PREFIX)gcc +CXX := $(PREFIX)g++ +LD := $(PREFIX)gcc +AR := $(PREFIX)ar +AS := $(PREFIX)as +OBJCOPY := $(PREFIX)objcopy +OBJDUMP := $(PREFIX)objdump +GDB := $(PREFIX)gdb STFLASH = $(shell which st-flash) STYLECHECK := /checkpatch.pl STYLECHECKFLAGS := --no-tree -f --terse --mailback STYLECHECKFILES := $(shell find . -name '*.[ch]') +OPT := -Os +DEBUG := -ggdb3 +CSTD ?= -std=c99 ############################################################################### # Source files -LDSCRIPT ?= $(BINARY).ld - OBJS += @@ -71,48 +72,69 @@ ifeq ($(V),1) $(info Using $(OPENCM3_DIR) path to library) endif -INCLUDE_DIR = $(OPENCM3_DIR)/include -LIB_DIR = $(OPENCM3_DIR)/lib -SCRIPT_DIR = ../scripts +define ERR_DEVICE_LDSCRIPT_CONFLICT +You can either specify DEVICE=blah, and have the LDSCRIPT generated, +or you can provide LDSCRIPT, and ensure CPPFLAGS, LDFLAGS and LDLIBS +all contain the correct values for the target you wish to use. +You cannot provide both! +endef + +ifeq ($(strip $(DEVICE)),) +# Old style, assume LDSCRIPT exists +DEFS += -I$(OPENCM3_DIR)/include +LDFLAGS += -L$(OPENCM3_DIR)/lib +LDLIBS += -l$(LIBNAME) +LDSCRIPT ?= $(BINARY).ld +else +# New style, assume device is provided, and we're generating the rest. +ifneq ($(strip $(LDSCRIPT)),) +$(error $(ERR_DEVICE_LDSCRIPT_CONFLICT)) +endif +include $(OPENCM3_DIR)/mk/genlink-config.mk +endif + +OPENCM3_SCRIPT_DIR = $(OPENCM3_DIR)/scripts +EXAMPLES_SCRIPT_DIR = $(OPENCM3_DIR)/../scripts ############################################################################### # C flags -CFLAGS += -g -CFLAGS += -Wextra -Wshadow -Wimplicit-function-declaration -CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes -CFLAGS += -fno-common -ffunction-sections -fdata-sections +TGT_CFLAGS += $(OPT) $(CSTD) $(DEBUG) +TGT_CFLAGS += $(ARCH_FLAGS) +TGT_CFLAGS += -Wextra -Wshadow -Wimplicit-function-declaration +TGT_CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes +TGT_CFLAGS += -fno-common -ffunction-sections -fdata-sections ############################################################################### # C++ flags -CXXFLAGS += -Os -g -CXXFLAGS += -Wextra -Wshadow -Wredundant-decls -Weffc++ -CXXFLAGS += -fno-common -ffunction-sections -fdata-sections +TGT_CXXFLAGS += $(OPT) $(CXXSTD) $(DEBUG) +TGT_CXXFLAGS += $(ARCH_FLAGS) +TGT_CXXFLAGS += -Wextra -Wshadow -Wredundant-decls -Weffc++ +TGT_CXXFLAGS += -fno-common -ffunction-sections -fdata-sections ############################################################################### # C & C++ preprocessor common flags -CPPFLAGS += -MD -CPPFLAGS += -Wall -Wundef -CPPFLAGS += -I$(INCLUDE_DIR) $(DEFS) +TGT_CPPFLAGS += -MD +TGT_CPPFLAGS += -Wall -Wundef +TGT_CPPFLAGS += $(DEFS) ############################################################################### # Linker flags -LDFLAGS += --static -nostartfiles -LDFLAGS += -L$(LIB_DIR) -LDFLAGS += -T$(LDSCRIPT) -LDFLAGS += -Wl,-Map=$(*).map -LDFLAGS += -Wl,--gc-sections +TGT_LDFLAGS += --static -nostartfiles +TGT_LDFLAGS += -T$(LDSCRIPT) +TGT_LDFLAGS += $(ARCH_FLAGS) $(DEBUG) +TGT_LDFLAGS += -Wl,-Map=$(*).map -Wl,--cref +TGT_LDFLAGS += -Wl,--gc-sections ifeq ($(V),99) -LDFLAGS += -Wl,--print-gc-sections +TGT_LDFLAGS += -Wl,--print-gc-sections endif ############################################################################### # Used libraries -LDLIBS += -l$(LIBNAME) LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group ############################################################################### @@ -130,10 +152,33 @@ bin: $(BINARY).bin hex: $(BINARY).hex srec: $(BINARY).srec list: $(BINARY).list +GENERATED_BINARIES=$(BINARY).elf $(BINARY).bin $(BINARY).hex $(BINARY).srec $(BINARY).list $(BINARY).map images: $(BINARY).images flash: $(BINARY).flash -stlink-flash: $(BINARY).stlink-flash + +# Either verify the user provided LDSCRIPT exists, or generate it. +ifeq ($(strip $(DEVICE)),) +$(LDSCRIPT): + ifeq (,$(wildcard $(LDSCRIPT))) + $(error Unable to find specified linker script: $(LDSCRIPT)) + endif +else +include $(OPENCM3_DIR)/mk/genlink-rules.mk +endif + +$(OPENCM3_DIR)/lib/lib$(LIBNAME).a: +ifeq (,$(wildcard $@)) + $(warning $(LIBNAME).a not found, attempting to rebuild in $(OPENCM3_DIR)) + $(MAKE) -C $(OPENCM3_DIR) +endif + +# Define a helper macro for debugging make errors online +# you can type "make print-OPENCM3_DIR" and it will show you +# how that ended up being resolved by all of the included +# makefiles. +print-%: + @echo $*=$($*) %.images: %.bin %.hex %.srec %.list %.map @printf "*** $* images generated ***\n" @@ -154,32 +199,32 @@ stlink-flash: $(BINARY).stlink-flash @printf " OBJDUMP $(*).list\n" $(Q)$(OBJDUMP) -S $(*).elf > $(*).list -%.elf %.map: $(OBJS) $(LDSCRIPT) $(LIB_DIR)/lib$(LIBNAME).a +%.elf %.map: $(OBJS) $(LDSCRIPT) $(OPENCM3_DIR)/lib/lib$(LIBNAME).a @printf " LD $(*).elf\n" - $(Q)$(LD) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(*).elf + $(Q)$(LD) $(TGT_LDFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(*).elf %.o: %.c @printf " CC $(*).c\n" - $(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $(*).o -c $(*).c + $(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).c %.o: %.cxx @printf " CXX $(*).cxx\n" - $(Q)$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $(*).o -c $(*).cxx + $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).cxx %.o: %.cpp @printf " CXX $(*).cpp\n" - $(Q)$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $(*).o -c $(*).cpp + $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).cpp clean: @printf " CLEAN\n" - $(Q)$(RM) *.o *.d *.elf *.bin *.hex *.srec *.list *.map + $(Q)$(RM) $(GENERATED_BINARIES) generated.* $(OBJS) $(OBJS:%.o=%.d) stylecheck: $(STYLECHECKFILES:=.stylecheck) styleclean: $(STYLECHECKFILES:=.styleclean) # the cat is due to multithreaded nature - we like to have consistent chunks of text on the output %.stylecheck: % - $(Q)$(SCRIPT_DIR)$(STYLECHECK) $(STYLECHECKFLAGS) $* > $*.stylecheck; \ + $(Q)$(OPENCM3_SCRIPT_DIR)$(STYLECHECK) $(STYLECHECKFLAGS) $* > $*.stylecheck; \ if [ -s $*.stylecheck ]; then \ cat $*.stylecheck; \ else \ @@ -194,27 +239,21 @@ styleclean: $(STYLECHECKFILES:=.styleclean) @printf " FLASH $<\n" $(Q)$(STFLASH) write $(*).bin 0x8000000 -ifeq ($(OOCD_SERIAL),) -%.oocd-flash: %.hex +ifeq ($(OOCD_FILE),) +%.oocd-flash: %.elf @printf " FLASH $<\n" - @# IMPORTANT: Don't use "resume", only "reset" will work correctly! - $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ - -f board/$(OOCD_BOARD).cfg \ - -c "init" -c "reset init" \ - -c "flash write_image erase $(*).hex" \ - -c "reset" \ - -c "shutdown" $(NULL) + (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 $(*).elf verify reset exit" \ + $(NULL) else -%.oocd-flash: %.hex +%.oocd-flash: %.elf @printf " FLASH $<\n" - @# IMPORTANT: Don't use "resume", only "reset" will work correctly! - $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ - -f board/$(OOCD_BOARD).cfg \ - -c "ft2232_serial $(OOCD_SERIAL)" \ - -c "init" -c "reset init" \ - -c "flash write_image erase $(*).hex" \ - -c "reset" \ - -c "shutdown" $(NULL) + (echo "halt; program $(realpath $(*).elf) verify reset" | nc -4 localhost 4444 2>/dev/null) || \ + $(OOCD) -f $(OOCD_FILE) \ + -c "program $(*).elf verify reset exit" \ + $(NULL) endif ifeq ($(BMP_PORT),) diff --git a/libopencm3.target.mk b/libopencm3.target.mk index 01a2c58..5d1db41 100644 --- a/libopencm3.target.mk +++ b/libopencm3.target.mk @@ -19,18 +19,21 @@ ## along with this library. If not, see . ## +# You should use linker script generation! Specify device! +ifeq ($(DEVICE),) LIBNAME = opencm3_stm32f4 -DEFS = -DSTM32F4 +DEFS += -DSTM32F4 FP_FLAGS ?= -mfloat-abi=hard -mfpu=fpv4-sp-d16 ARCH_FLAGS = -mthumb -mcpu=cortex-m4 $(FP_FLAGS) +endif ################################################################################ # OpenOCD specific variables OOCD ?= openocd OOCD_INTERFACE ?= stlink-v2 -OOCD_BOARD ?= stm32f4discovery +OOCD_TARGET ?= stm32f4x ################################################################################ # Black Magic Probe specific variables @@ -43,6 +46,3 @@ BMP_PORT ?= include ../libopencm3.rules.mk - - - diff --git a/src/Makefile b/src/Makefile index 51fe6e1..7f42978 100644 --- a/src/Makefile +++ b/src/Makefile @@ -19,17 +19,8 @@ BINARY = 1bitsy-example -OBJS = main.o - -LDSCRIPT = ../1bitsy-stm32f415rgt.ld - -# You can enable size optimization for project deployment, but it can get -# in the way of debugging your code -#CFLAGS += -Os +DEVICE=STM32F415RGT -# If your project is using a specific C standard you can set it by uncommenting -# the following line and adjusting the -std flag accordingly -#CFLAGS += -std=gnu11 -pedantic +OBJS = main.o include ../libopencm3.target.mk - diff --git a/src/main.c b/src/main.c index e5bc2b5..3a96781 100644 --- a/src/main.c +++ b/src/main.c @@ -26,7 +26,7 @@ /* Set STM32 to 168 MHz. */ static void clock_setup(void) { - rcc_clock_setup_hse_3v3(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_168MHZ]); + rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_168MHZ]); /* Enable GPIOA clock. */ rcc_periph_clock_enable(RCC_GPIOA);