diff --git a/sw/ext/blackmagic/.gitignore b/sw/ext/blackmagic/.gitignore index d528d805df..9602940ea8 100644 --- a/sw/ext/blackmagic/.gitignore +++ b/sw/ext/blackmagic/.gitignore @@ -1 +1,2 @@ blackmagic*.bin +dfu.py diff --git a/sw/ext/blackmagic/Makefile b/sw/ext/blackmagic/Makefile index 1596dc4d82..ba15bd3b15 100644 --- a/sw/ext/blackmagic/Makefile +++ b/sw/ext/blackmagic/Makefile @@ -5,12 +5,15 @@ BLACKMAGIC = blackmagic-20141017.bin Q=@ -all: $(BLACKMAGIC) - $(Q)../../tools/stm32loader/stm32loader.py -p /dev/ttyACM0 ./$(BLACKMAGIC) +all: $(BLACKMAGIC) dfu.py + $(Q)./stm32_mem.py ./$(BLACKMAGIC) $(BLACKMAGIC): @echo "Downloading latest BMP firmware" wget -O ./$(BLACKMAGIC) $(BLACKSPHERE)/$(BLACKMAGIC) +dfu.py: + ln -s -f ../../tools/dfu/dfu.py dfu.py + clean: $(Q) rm -rf ./$(BLACKMAGIC) diff --git a/sw/ext/blackmagic/stm32_mem.py b/sw/ext/blackmagic/stm32_mem.py new file mode 100755 index 0000000000..9bf20d9c99 --- /dev/null +++ b/sw/ext/blackmagic/stm32_mem.py @@ -0,0 +1,109 @@ +#!/usr/bin/python +# +# stm32_mem.py: STM32 memory access using USB DFU class +# Copyright (C) 2011 Black Sphere Technologies +# Written by Gareth McMullin +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from time import sleep +import struct +from sys import stdout, argv + +import usb +import dfu + +CMD_GETCOMMANDS = 0x00 +CMD_SETADDRESSPOINTER = 0x21 +CMD_ERASE = 0x41 + +def stm32_erase(dev, addr): + erase_cmd = struct.pack("" + print + + devs = dfu.finddevs() + if not devs: + print "No devices found!" + exit(-1) + + for dev in devs: + dfudev = dfu.dfu_device(*dev) + man = dfudev.handle.getString(dfudev.dev.iManufacturer, 30) + product = dfudev.handle.getString(dfudev.dev.iProduct, 30) + if man == "Black Sphere Technologies": break + if man == "STMicroelectronics": break + + print "Device %s: ID %04x:%04x %s - %s" % (dfudev.dev.filename, + dfudev.dev.idVendor, dfudev.dev.idProduct, man, product) + + try: + state = dfudev.get_state() + except: + print "Failed to read device state! Assuming APP_IDLE" + state = dfu.STATE_APP_IDLE + if state == dfu.STATE_APP_IDLE: + dfudev.detach() + print "Run again to upgrade firmware." + exit(0) + + dfudev.make_idle() + + bin = open(argv[1], "rb").read() + + addr = 0x8002000 + while bin: + print ("Programming memory at 0x%08X\r" % addr), + stdout.flush() + stm32_erase(dfudev, addr) + stm32_write(dfudev, bin[:1024]) + + bin = bin[1024:] + addr += 1024 + + stm32_manifest(dfudev) + + print "\nAll operations complete!\n"