dist: Create DFU files in addition to HEX

This commit is contained in:
Keir Fraser
2024-12-10 10:48:56 +00:00
parent 955ccb910d
commit 5d1d1ee516
5 changed files with 76 additions and 4 deletions

View File

@@ -21,7 +21,7 @@ jobs:
sudo apt -y install git gcc-arm-none-eabi python3-pip srecord zip
- name: Dependency packages (pip)
run: python3 -m pip install --user crcmod
run: python3 -m pip install --user crcmod intelhex
- name: Build dist
run: |

View File

@@ -23,7 +23,7 @@ jobs:
sudo apt -y install git gcc-arm-none-eabi python3-pip srecord zip
- name: Dependency packages (pip)
run: python3 -m pip install --user crcmod
run: python3 -m pip install --user crcmod intelhex
- name: Build release
run: |

View File

@@ -33,26 +33,31 @@ out: FORCE
+mkdir -p out/$(mcu)/$(level)/$(target)
target: FORCE out
$(MAKE) -C out/$(mcu)/$(level)/$(target) -f $(ROOT)/Rules.mk target.bin target.hex target.upd $(mcu)=y $(level)=y $(target)=y
$(MAKE) -C out/$(mcu)/$(level)/$(target) -f $(ROOT)/Rules.mk target.bin target.hex target.dfu target.upd $(mcu)=y $(level)=y $(target)=y
dist: level := prod
dist: t := $(ROOT)/out/$(PROJ)-$(VER)
dist: FORCE all
rm -rf out/$(PROJ)-*
mkdir -p $(t)/hex/alt
mkdir -p $(t)/dfu/alt
cd out/stm32f1/$(level)/greaseweazle; \
cp -a target.hex $(t)/hex/$(PROJ)-f1-$(VER).hex; \
cp -a target.dfu $(t)/dfu/$(PROJ)-f1-$(VER).dfu; \
cp -a ../bootloader/target.upd $(t)/$(PROJ)-$(VER).upd; \
$(PYTHON) $(ROOT)/scripts/mk_update.py cat $(t)/$(PROJ)-$(VER).upd \
target.upd
cd out/stm32f1/debug/blinky; \
cp -a target.hex $(t)/hex/alt/blinky-test-f1-$(VER).hex
cp -a target.hex $(t)/hex/alt/blinky-test-f1-$(VER).hex; \
cp -a target.dfu $(t)/dfu/alt/blinky-test-f1-$(VER).dfu
cd out/stm32f7/$(level)/greaseweazle; \
cp -a target.hex $(t)/hex/$(PROJ)-f7-$(VER).hex; \
cp -a target.dfu $(t)/dfu/$(PROJ)-f7-$(VER).dfu; \
$(PYTHON) $(ROOT)/scripts/mk_update.py cat $(t)/$(PROJ)-$(VER).upd \
../bootloader/target.upd target.upd
cd out/at32f4/$(level)/greaseweazle; \
cp -a target.hex $(t)/hex/$(PROJ)-at32f4-$(VER).hex; \
cp -a target.dfu $(t)/dfu/$(PROJ)-at32f4-$(VER).dfu; \
$(PYTHON) $(ROOT)/scripts/mk_update.py cat $(t)/$(PROJ)-$(VER).upd \
../bootloader/target.upd target.upd
cp -a COPYING $(t)/

View File

@@ -20,12 +20,15 @@ FLAGS += -Wno-unused-value -ffunction-sections
ifeq ($(mcu),stm32f1)
FLAGS += -mcpu=cortex-m3 -DSTM32F1=1 -DMCU=1
DFU_DEV = 0x0483:0xdf11
stm32f1=y
else ifeq ($(mcu),stm32f7)
FLAGS += -mcpu=cortex-m7 -DSTM32F7=7 -DMCU=7
DFU_DEV = 0x0483:0xdf11
stm32f7=y
else ifeq ($(mcu),at32f4)
FLAGS += -mcpu=cortex-m4 -DAT32F4=4 -DMCU=4
DFU_DEV = 0x2e3c:0xdf11
at32f4=y
endif
@@ -101,6 +104,9 @@ endif
$(OBJCOPY) -O binary $< $@
chmod a-x $@
%.dfu: %.hex
$(PYTHON) $(ROOT)/scripts/dfu-convert.py -i $< -D $(DFU_DEV) $@
%.upd: %.bin
$(PYTHON) $(ROOT)/scripts/mk_update.py new $@ \
$< $(mcu)-$(FW_MAJOR).$(FW_MINOR)-$(bootloader)

61
scripts/dfu-convert.py Normal file
View File

@@ -0,0 +1,61 @@
# Written by Antonio Galea - 2010/11/18
# Stripped down & modified by Keir Fraser for FlashFloppy project
# Distributed under Gnu LGPL 3.0
# see http://www.gnu.org/licenses/lgpl-3.0.txt
import sys,struct,zlib
from optparse import OptionParser
from intelhex import IntelHex
DEFAULT_DEVICE="0x0483:0xdf11"
def compute_crc(data):
return 0xFFFFFFFF & -zlib.crc32(data) -1
def build(file,targets,device=DEFAULT_DEVICE):
data = b''
for t,target in enumerate(targets):
tdata = b''
for image in target:
tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data']
tdata = struct.pack('<6sBI255s2I',b'Target',0,1,b'ST...',len(tdata),len(target)) + tdata
data += tdata
data = struct.pack('<5sBIB',b'DfuSe',1,len(data)+11,len(targets)) + data
v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
data += struct.pack('<4H3sB',0,d,v,0x011a,b'UFD',16)
crc = compute_crc(data)
data += struct.pack('<I',crc)
open(file,'wb').write(data)
if __name__=="__main__":
usage = """
%prog {-i|--ihex} file.hex [-i file.hex ...] [{-D|--device}=vendor:device] outfile.dfu"""
parser = OptionParser(usage=usage)
parser.add_option("-i", "--ihex", action="append", dest="hexfiles",
help="build a DFU file from given HEXFILES", metavar="HEXFILES")
parser.add_option("-D", "--device", action="store", dest="device",
help="build for DEVICE, defaults to %s" % DEFAULT_DEVICE, metavar="DEVICE")
(options, args) = parser.parse_args()
if options.hexfiles and len(args)==1:
target = []
for h in options.hexfiles:
ih = IntelHex(h)
for (s,e) in ih.segments():
target.append({ 'address': s, 'data': ih.tobinstr(s,e-1) })
outfile = args[0]
device = DEFAULT_DEVICE
if options.device:
device=options.device
try:
v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
except:
print("Invalid device '%s'." % device)
sys.exit(1)
build(outfile,[target],device)
else:
parser.print_help()
sys.exit(1)