Skip to content

Commit

Permalink
Support flashing using st-flash from the open source STLink tools
Browse files Browse the repository at this point in the history
Making this change after a user reported a problem with OpenOCD on their
machine. This offers an alternative for uploading firmware with ST-Link
programmers.
  • Loading branch information
stecman committed Mar 12, 2022
1 parent 95ab4d8 commit d74628c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
67 changes: 45 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
# See libopencm3/ld/devices.data for valid device name patterns
DEVICE ?= stm32f103c4

# Programmer to use (name of interface config in OpenOCD)
# For ST-Link v1 or v2 programmers, use "stlink"
# See the interfaces/*.cfg files installed by openocd for options
# Programmer to use. This can be either:
#
# - The name of an interface config file in OpenOCD like "jlink" or "stlink"
# (see the interfaces/*.cfg files installed by openocd for other options);
#
# OR
#
# - The special string "st-flash" to flash using stlink tools instead of OpenOCD
# (Available at github.com/stlink-org/stlink)
#
PROGRAMMER ?= jlink

# Serial number for USB interface (must be 12 or more characters in hex)
Expand All @@ -28,22 +35,24 @@ ifeq ($(DEVICE), stm32f070f6)
CDEFS += -DSTM32F070F6
endif


# Pick OpenOCD target config based on DEVICE parameter if not set externally
OOCD_TARGET ?= $(shell python ./scripts/openocd_target.py $(DEVICE))

# Pick appropriate transport for programmer
ifeq ($(PROGRAMMER), stlink)
TRANSPORT ?= hla_swd
else
TRANSPORT ?= swd
# Configure OpenOCD if we're using that
ifneq ($(PROGRAMMER), st-flash)
# Pick OpenOCD target config based on DEVICE parameter if not set externally
OOCD_TARGET ?= $(shell python ./scripts/openocd_target.py $(DEVICE))

# Pick appropriate transport for programmer
ifeq ($(PROGRAMMER), stlink)
TRANSPORT ?= hla_swd
else
TRANSPORT ?= swd
endif

OOCD_FLAGS = -f interface/$(PROGRAMMER).cfg \
-c "transport select $(TRANSPORT)" \
-f target/$(OOCD_TARGET).cfg \
-f openocd.cfg
endif

OOCD_FLAGS = -f interface/$(PROGRAMMER).cfg \
-c "transport select $(TRANSPORT)" \
-f target/$(OOCD_TARGET).cfg \
-f openocd.cfg


# Generate the actual build targets based on this what's configured above
OPENCM3_DIR=libopencm3
Expand All @@ -57,20 +66,34 @@ FLASH_SIZE_BYTES = $(shell python ./scripts/parse_defs.py _ROM $(genlink_defs))
SRAM_SIZE_BYTES = $(shell python ./scripts/parse_defs.py _RAM $(genlink_defs))


reset:
$(OOCD) -f $(OOCD_FILE) -c 'reset ()'

flash: all
ifeq ($(PROGRAMMER), st-flash)
st-flash --reset write $(OUTPUT_BIN) 0x8000000
else
BINARY=$(OUTPUT_ELF) $(OOCD) $(OOCD_FLAGS) -c 'program_and_run ()'
endif

# Some utility targets are only supported when using OpenOCD
ifneq ($(PROGRAMMER), st-flash)
flash_and_debug: all
# Flash and halt, waiting for the debugger to attach
BINARY=$(OUTPUT_ELF) $(OOCD) $(OOCD_FLAGS) -c 'program_and_attach ()'

reset:
# Reset the target board
$(OOCD) -f $(OOCD_FILE) -c 'reset ()'
endif

# Start a GDB debug server. Once started, used 'make debug_gdb' to connect in
# a separate terminal (openocd breaks if backgrounded within the Makefile)
debug_server:
# This doesn't like being backgrounded from make for some reason
# Run this as a background task, then run debug_gdb
ifeq ($(PROGRAMMER), st-flash)
st-util --listen_port=3333
else
$(OOCD) $(OOCD_FLAGS) -c 'attach ()'
endif

# Connect to a GDB server started with 'make debug_server'
debug_gdb:
$(GDB) $(OUTPUT_ELF) -ex 'target remote :3333' \

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Firmware for the [Hardware Boot Selection Switch](https://hackaday.io/project/17

## Building

On Linux, you'll need `gcc-arm-none-eabi` and `python` installed to build and `openocd` to flash.
On Linux, you'll need `gcc-arm-none-eabi` and `python` installed to build and `openocd` or [`st-flash`](https://github.com/stlink-org/stlink) to flash.

```sh
# Pull in libopencm3
Expand All @@ -23,6 +23,9 @@ make flash

# Flash with an ST-Link programmer connected by SWD
make flash PROGRAMMER=stlink

# Flash using st-flash instead of openocd (only supports ST-Link programmers)
make flash PROGRAMMER=st-flash
```

This project supports building and flashing multiple different parts. The following parts have been tested:
Expand Down

0 comments on commit d74628c

Please sign in to comment.