-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it easier to flash different devices and use ST-Link programmer
- Added parameters to specify a programmer (interface) for openocd to use - Added device name to firmware blobs so the flash command won't upload firmware to a device unless it was compiled for that device. - Added small scripts to leverage the DEVICE parameter as much as possible in the build process. This includes automatically determining openocd target config from DEVICE, and getting the ROM/RAM size information already present in libopencm3 (but poorly exposed) - Moved Makefile to project root for consistency with other projects - Removed opencm3 build as a makefile dependency. `genlink-config.mk` in opencm3 isn't written in a way that can be used as a build dependency, since it refuses to set a library name if the name it wants to use doesn't exist on disk already. This means the make has to be run twice to actually succeed. - Added credit to size script. This was mentioned in an Embedded Artistry newsletter a while ago and I hadn't intended to widely distribute this copy of it. Resolves #5
- Loading branch information
Showing
8 changed files
with
166 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Device to build for | ||
# See libopencm3/ld/devices.data for valid device name patterns | ||
DEVICE ?= stm32f103c8 | ||
|
||
# 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 ?= jlink | ||
|
||
# Serial number for USB interface (must be 12 or more characters in hex) | ||
SERIALNUM ?= D000000000E7 | ||
CDEFS += -DUSB_SERIALNUM="\"$(SERIALNUM)\"" | ||
|
||
# Base OpenCM3 build | ||
PROJECT = boot-swtich | ||
|
||
CFILES = \ | ||
src/clock.c \ | ||
src/fat.c \ | ||
src/main.c \ | ||
src/usb.c \ | ||
|
||
CSTD = -std=c11 | ||
OPT = -Os | ||
|
||
# Flag specific devices that needs special pin mapping | ||
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 | ||
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 | ||
include $(OPENCM3_DIR)/mk/genlink-config.mk | ||
include rules.mk | ||
include $(OPENCM3_DIR)/mk/genlink-rules.mk | ||
|
||
# Extract parameters for size script from generated device flags | ||
genlink_defs = $(shell $(OPENCM3_DIR)/scripts/genlink.py $(DEVICES_DATA) $(DEVICE) DEFS) | ||
FLASH_SIZE_BYTES = $(shell python ./scripts/parse_defs.py _ROM $(genlink_defs)) | ||
SRAM_SIZE_BYTES = $(shell python ./scripts/parse_defs.py _RAM $(genlink_defs)) | ||
|
||
test: | ||
@echo $(genlink_cppflags) | ||
@echo $(FLASH_SIZE_BYTES) $(SRAM_SIZE_BYTES) | ||
|
||
reset: | ||
$(OOCD) -f $(OOCD_FILE) -c 'reset ()' | ||
|
||
flash: all | ||
BINARY=$(OUTPUT_ELF) $(OOCD) $(OOCD_FLAGS) -c 'program_and_run ()' | ||
|
||
flash_and_debug: all | ||
BINARY=$(OUTPUT_ELF) $(OOCD) $(OOCD_FLAGS) -c 'program_and_attach ()' | ||
|
||
debug_server: | ||
# This doesn't like being backgrounded from make for some reason | ||
# Run this as a background task, then run debug_gdb | ||
$(OOCD) $(OOCD_FLAGS) -c 'attach ()' | ||
|
||
debug_gdb: | ||
$(GDB) $(OUTPUT_ELF) -ex 'target remote :3333' \ | ||
|
||
disasm: $(OUTPUT_ELF) | ||
$(OBJDUMP) -d $(OUTPUT_ELF) | ||
|
||
.PHONY: flash reset debug_gdb debug_server disasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# | ||
# Print an OpenOCD target name for a given libopencm3 device name | ||
# This could be done with sed from Make, but I expect this to grow | ||
# | ||
|
||
from __future__ import print_function | ||
|
||
import sys | ||
import re | ||
|
||
if len(sys.argv) < 2: | ||
print("Usage: %s <DEVICE>") | ||
sys.exit(1) | ||
|
||
device = sys.argv[1] | ||
|
||
if device.startswith('stm32'): | ||
match = re.match(r'(stm32f[0-9]).*', device) | ||
print(match.group(1) + "x") | ||
|
||
else: | ||
sys.stderr.write("Unsure how to determine OpenOCD target config for '%s'" % device) | ||
sys.exit(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# | ||
# Extract a particular definition (-D) value from passed compiler args | ||
# | ||
# This is a bit messy as the genlink.py script in libopencm3 isn't written in | ||
# a way that allows reuse of its parsing code in other Python scripts. This | ||
# script primarily exists to parse RAM and ROM size data from genlink.py. | ||
# | ||
|
||
from __future__ import print_function | ||
|
||
import re | ||
import sys | ||
|
||
if len(sys.argv) < 2: | ||
print("Usage: %s <NAME> [-Dvar=value, ...]") | ||
sys.exit(1) | ||
|
||
search = sys.argv[1] | ||
pattern = "-D%s=" % search | ||
value = None | ||
|
||
for arg in sys.argv[2:]: | ||
if arg.startswith(pattern): | ||
value = arg[len(pattern):] | ||
break | ||
else: | ||
print("Couldn't find variable named '%s' in args: %s" % (search, sys.argv[2:])) | ||
sys.exit(2) | ||
|
||
# Convert kilobytes to bytes | ||
match = re.match(r'([0-9]+)K', value) | ||
if match: | ||
value = int(match.group(1)) * 1024 | ||
|
||
print(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.