Skip to content

Commit

Permalink
Add firmware revision info in DFU file (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Baldanos authored and bvernoux committed Sep 30, 2018
1 parent 1b83986 commit 18aaa6d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ MAKEFLAGS += --no-builtin-rules
#Set to 1 HYDRAFW_NFC to include HydraNFC extension support
export HYDRAFW_NFC ?= 1
export HYDRAFW_DEBUG ?= 0
export FW_REVISION := $(shell python build-scripts/hydrafw-revision.py)

HYDRAFW_OPTS =

Expand Down Expand Up @@ -276,7 +277,7 @@ FORCE:
ifeq ($(USE_VERBOSE_COMPILE),yes)
echo Creating ./common/hydrafw_version.hdr
-rm -f $(OBJDIR)/common.o
python build-scripts/hydrafw-version.py ./common/hydrafw_version.hdr
python build-scripts/hydrafw-version.py ./common/hydrafw_version.hdr
else
@echo Creating ./common/hydrafw_version.hdr
@rm -f $(OBJDIR)/common.o
Expand All @@ -285,10 +286,10 @@ endif

%.dfu: %.hex $(LDSCRIPT)
ifeq ($(USE_VERBOSE_COMPILE),yes)
python build-scripts/dfu-convert.py -i $< $@
python build-scripts/dfu-convert.py -r $(FW_REVISION) -i $< $@
else
@echo Creating $@
@python build-scripts/dfu-convert.py -i $< $@
@python build-scripts/dfu-convert.py -r $(FW_REVISION) -i $< $@
endif

# This rule hook is defined in the ChibiOS build system
Expand Down
22 changes: 19 additions & 3 deletions src/build-scripts/dfu-convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from intelhex import IntelHex

DEFAULT_DEVICE="0x0483:0xdf11"
DEFAULT_REVISION="0.0"

def named(tuple,names):
return dict(list(zip(names.split(),tuple)))
Expand All @@ -19,6 +20,9 @@ def cstring(string):
return string.split(b'\0',1)[0]
def compute_crc(data):
return 0xFFFFFFFF & -zlib.crc32(data) -1
def rev2byte(revision):
ma,mi = revision.split('.')
return (int(ma,16)<<8 | int(mi,16))

def parse(file,dump_images=False):
print('File: "%s"' % file)
Expand Down Expand Up @@ -56,7 +60,7 @@ def parse(file,dump_images=False):
if data:
print("PARSE ERROR")

def build(file,targets,device=DEFAULT_DEVICE):
def build(file,targets,device=DEFAULT_DEVICE, revision="0.0"):
data = b''
for t,target in enumerate(targets):
tdata = b''
Expand All @@ -66,7 +70,8 @@ def build(file,targets,device=DEFAULT_DEVICE):
data += tdata
data = struct.pack('<5sBIB',b'DfuSe',1,len(data)+11,len(targets)) + data
v,d=[int(x,0) & 0xFFFF for x in device.split(':',1)]
data += struct.pack('<4H3sB',0,d,v,0x011a,b'UFD',16)
rev = rev2byte(revision)
data += struct.pack('<4H3sB',rev,d,v,0x011a,b'UFD',16)
crc = compute_crc(data)
data += struct.pack('<I',crc)
open(file,'wb').write(data)
Expand All @@ -84,6 +89,8 @@ def build(file,targets,device=DEFAULT_DEVICE):
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")
parser.add_option("-r", "--revision", action="store", dest="revision",
help="Revision number, defaults to %s" % DEFAULT_REVISION, metavar="REVISION")
parser.add_option("-d", "--dump", action="store_true", dest="dump_images",
default=False, help="dump contained images to current directory")
(options, args) = parser.parse_args()
Expand Down Expand Up @@ -119,6 +126,15 @@ def build(file,targets,device=DEFAULT_DEVICE):
print("Address %s invalid." % address)
sys.exit(1)
target.append({ 'address': address, 'data': data })

revision = DEFAULT_REVISION
if options.revision:
try:
rev2byte(options.revision)
revision = options.revision
except ValueError:
print("Invalid revision value.")
sys.exit(1)

outfile = args[0]
device = DEFAULT_DEVICE
Expand All @@ -129,7 +145,7 @@ def build(file,targets,device=DEFAULT_DEVICE):
except:
print("Invalid device '%s'." % device)
sys.exit(1)
build(outfile,[target],device)
build(outfile,[target],device, revision)
elif len(args)==1:
infile = args[0]
if not os.path.isfile(infile):
Expand Down
8 changes: 8 additions & 0 deletions src/build-scripts/hydrafw-revision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from git import *
import re

r = re.compile("v(\d+\.\d+).*")

git=Repo(search_parent_directories=True).git
version = git.describe(tags=True,always=True,dirty=True,long=True)
print(r.search(version).group(1))

0 comments on commit 18aaa6d

Please sign in to comment.