first commit

This commit is contained in:
2022-08-24 08:41:00 +02:00
commit 5765dbd675
2497 changed files with 2244854 additions and 0 deletions
@@ -0,0 +1,206 @@
#!/usr/bin/env python2
#
# Intel HEX to ST microelectronics DfuSe file converter
# Copyright (C)2015 Thomas Kindler <mail@t-kindler.de>
#
# 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 <http://www.gnu.org/licenses/>.
#
import os
import struct
import argparse
from zlib import crc32
from intelhex import IntelHex
def load_hex():
"""
Load hex or binary file.
:return: intelhex object
"""
if args.verbose:
print "Loading %s..." % args.source
try:
ih = IntelHex()
if args.format == "hex":
ih.loadhex(args.source)
else:
ih.loadbin(args.source, args.start_addr)
except Exception, e:
print e
exit(1)
if args.verbose:
print " Start: 0x%08x" % ih.minaddr()
print " End : 0x%08x" % ih.maxaddr()
return ih
def save_dfu(ih):
"""
Save as STMicroelectronics DfuSe file.
see UM0391 - DfuSe File Format Specification
:param ih: intelhex object
"""
if args.verbose:
print "Saving %s..." % args.target
print " Device ID: 0x%04x:0x%04x" % (args.vid, args.pid)
print " Target name: %s" % args.target_name
# Image element
#
image_data = ih.tobinstr()
data = struct.pack(
"<II",
ih.minaddr(), # dwElementAddress
len(image_data) # dwElementSize
) + image_data # Data
# Target prefix
#
szTargetName = args.target_name.encode("ascii")
data = struct.pack(
"<6sBI255sII",
b"Target", # szSignature
0, # bAlternateSetting
1, # bTargetNamed
szTargetName, # szTargetName
len(data), # dwTargetSize
1 # dwNbElements
) + data
# Prefix
#
data = struct.pack(
"<5sBIB",
b"DfuSe", # szSignature
0x01, # bVersion,
len(data)+11, # DFUImageSize,
1 # bTargets
) + data
# Suffix
#
data += struct.pack(
"<HHHH3sB",
0xFFFF, # bcdDevice
args.pid, # idProduct
args.vid, # idVendor
0x011a, # bdcDFU
b"UFD", # ucDfuSignature
16 # bLength
)
dwCRC = ~crc32(data) & 0xFFFFFFFF
data += struct.pack(
"<I",
dwCRC # dwCRC
)
try:
open(args.target, "wb").write(data)
except Exception, e:
print e
exit(1)
# Parse arguments
#
DEFAULT_DEVICE = "0x0483:0xdf11"
parser = argparse.ArgumentParser(
description="Convert hex files to STMicroelectronics DfuSe format"
)
parser.add_argument(
"--version",
action="version",
version="%(prog)s 1.1.0"
)
parser.add_argument(
"-q", "--quiet", dest="verbose",
default=True,
action="store_false",
help="do not print status messages"
)
parser.add_argument(
"source", help="source file"
)
parser.add_argument(
"target", nargs = "?",
help="target file"
)
parser.add_argument(
"-f", "--format",
default="hex", choices=["hex", "bin"],
help="source file format (default: %(default)s)"
)
parser.add_argument(
"-s", "--start", dest="start_addr",
help="set start address (for bin files)"
)
parser.add_argument(
"-d", "--device",
default=DEFAULT_DEVICE,
help="device VID:PID (default: %(default)s)"
)
parser.add_argument(
"-n", "--name", dest="target_name",
default="application",
help="target name (default: %(default)s)"
)
args=parser.parse_args()
# Check arguments
#
if args.target == None:
(root, ext) = os.path.splitext(args.source)
args.target = root + ".dfu"
if args.format == "bin":
if args.start_addr == None:
print "option --start required for binary files"
exit(1)
args.start_addr = int(args.start_addr, 0)
else:
if args.start_addr != None:
print "option --start not allowed for hex files"
exit(1)
args.vid = int(args.device.split(':', 1)[0], 0)
args.pid = int(args.device.split(':', 1)[1], 0)
# Convert file
#
save_dfu( load_hex() )
@@ -0,0 +1,357 @@
#!/usr/bin/python
# (c) Alexander Belchenko, 2007, 2009
"""Benchmarking.
Run each test 3 times and get median value.
Using 10K array as base test time.
Each other test compared with base with next formula::
Tc * Nb
q = ---------
Tb * Nc
Here:
* Tc - execution time of current test
* Tb - execution time of base
* Nb - array size of base (10K)
* Nc - array size of current test
If resulting value is ``q <= 1.0`` it's the best possible result,
i.e. time increase proportionally to array size.
"""
from cStringIO import StringIO
import gc
import sys
import time
import intelhex
def median(values):
"""Return median value for the list of values.
@param values: list of values for processing.
@return: median value.
"""
values.sort()
n = int(len(values) / 2)
return values[n]
def run_test(func, fobj):
"""Run func with argument fobj and measure execution time.
@param func: function for test
@param fobj: data for test
@return: execution time
"""
gc.disable()
try:
begin = time.time()
func(fobj)
end = time.time()
finally:
gc.enable()
return end - begin
def run_readtest_N_times(func, hexstr, n):
"""Run each test N times.
@param func: function for test
@param hexstr: string with content of hex file to read
@param n: times to repeat.
@return: (median time, times list)
"""
assert n > 0
times = []
for i in xrange(n):
sio = StringIO(hexstr)
times.append(run_test(func, sio))
sio.close()
t = median(times)
return t, times
def run_writetest_N_times(func, n):
"""Run each test N times.
@param func: function for test
@param n: times to repeat.
@return: (median time, times list)
"""
assert n > 0
times = []
for i in xrange(n):
sio = StringIO()
times.append(run_test(func, sio))
sio.close()
t = median(times)
return t, times
def time_coef(tc, nc, tb, nb):
"""Return time coefficient relative to base numbers.
@param tc: current test time
@param nc: current test data size
@param tb: base test time
@param nb: base test data size
@return: time coef.
"""
tc = float(tc)
nc = float(nc)
tb = float(tb)
nb = float(nb)
q = (tc * nb) / (tb * nc)
return q
def get_test_data(n1, offset, n2):
"""Create test data on given pattern.
@param n1: size of first part of array at base address 0.
@param offset: offset for second part of array.
@param n2: size of second part of array at given offset.
@return: (overall size, hex file, IntelHex object)
"""
# make IntelHex object
ih = intelhex.IntelHex()
addr = 0
for i in xrange(n1):
ih[addr] = addr % 256
addr += 1
addr += offset
for i in xrange(n2):
ih[addr] = addr % 256
addr += 1
# make hex file
sio = StringIO()
ih.write_hex_file(sio)
hexstr = sio.getvalue()
sio.close()
#
return n1+n2, hexstr, ih
def get_base_10K():
"""Base 10K"""
return get_test_data(10000, 0, 0)
def get_100K():
return get_test_data(100000, 0, 0)
def get_100K_100K():
return get_test_data(100000, 1000000, 100000)
def get_0_100K():
return get_test_data(0, 1000000, 100000)
def get_1M():
return get_test_data(1000000, 0, 0)
class Measure(object):
"""Measure execution time helper."""
data_set = [
# (data name, getter)
('base 10K', get_base_10K), # first should be base numbers
('100K', get_100K),
('1M', get_1M),
('100K+100K', get_100K_100K),
('0+100K', get_0_100K),
]
def __init__(self, n=3, read=True, write=True):
self.n = n
self.read = read
self.write = write
self.results = []
def measure_one(self, data):
"""Do measuring of read and write operations.
@param data: 3-tuple from get_test_data
@return: (time readhex, time writehex)
"""
_unused, hexstr, ih = data
tread, twrite = 0.0, 0.0
if self.read:
tread = run_readtest_N_times(intelhex.IntelHex, hexstr, self.n)[0]
if self.write:
twrite = run_writetest_N_times(ih.write_hex_file, self.n)[0]
return tread, twrite
def measure_all(self):
for name, getter in self.data_set:
data = getter()
times = self.measure_one(data)
self.results.append((name, times, data[0]))
def print_report(self, to_file=None):
if to_file is None:
to_file = sys.stdout
base_title, base_times, base_n = self.results[0]
base_read, base_write = base_times
read_report = ['%-10s\t%7.3f' % (base_title, base_read)]
write_report = ['%-10s\t%7.3f' % (base_title, base_write)]
for item in self.results[1:]:
cur_title, cur_times, cur_n = item
cur_read, cur_write = cur_times
if self.read:
qread = time_coef(cur_read, cur_n,
base_read, base_n)
read_report.append('%-10s\t%7.3f\t%7.3f' % (cur_title,
cur_read,
qread))
if self.write:
qwrite = time_coef(cur_write, cur_n,
base_write, base_n)
write_report.append('%-10s\t%7.3f\t%7.3f' % (cur_title,
cur_write,
qwrite))
if self.read:
to_file.write('Read operation:\n')
to_file.write('\n'.join(read_report))
to_file.write('\n\n')
if self.write:
to_file.write('Write operation:\n')
to_file.write('\n'.join(write_report))
to_file.write('\n\n')
HELP = """\
Usage: python _bench.py [OPTIONS]
Options:
-h this help
-n N repeat tests N times
-r run only tests for read operation
-w run only tests for write operation
If option -r or -w is not specified then all tests will be run.
"""
def main(argv=None):
"""Main function to run benchmarks.
@param argv: command-line arguments.
@return: exit code (0 is OK).
"""
import getopt
# default values
test_read = None
test_write = None
n = 3 # number of repeat
if argv is None:
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv, 'hn:rw', [])
for o,a in opts:
if o == '-h':
print(HELP)
return 0
elif o == '-n':
n = int(a)
elif o == '-r':
test_read = True
elif o == '-w':
test_write = True
if args:
raise getopt.GetoptError('Arguments are not used.')
except getopt.GetoptError, msg:
txt = str(msg)
print(txt)
return 1
if (test_read, test_write) == (None, None):
test_read = test_write = True
m = Measure(n, test_read, test_write)
m.measure_all()
m.print_report()
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
"""
Some Results
************
21/04/2007 revno.40
Python 2.5 @ Windows XP, Intel Celeron M CPU 430 @ 1.73GHz
Read operation:
base 10K 0.031
100K 0.360 1.161
1M 3.500 1.129
100K+100K 0.719 1.160
0+100K 0.360 1.161
Write operation:
base 10K 0.031
100K 0.297 0.958
1M 2.953 0.953
100K+100K 1.328 2.142
0+100K 0.312 1.006
21/04/2007 revno.46
Python 2.5 @ Windows XP, Intel Celeron M CPU 430 @ 1.73GHz
Read operation:
base 10K 0.016
100K 0.203 1.269
1M 2.000 1.250
100K+100K 0.422 1.319
0+100K 0.203 1.269
Write operation:
base 10K 0.031
100K 0.297 0.958
1M 2.969 0.958
100K+100K 1.328 2.142
0+100K 0.312 1.006
22/04/2007 revno.48
Python 2.5 @ Windows XP, Intel Celeron M CPU 430 @ 1.73GHz
Read operation:
base 10K 0.016
100K 0.187 1.169
1M 1.891 1.182
100K+100K 0.406 1.269
0+100K 0.188 1.175
Write operation:
base 10K 0.031
100K 0.296 0.955
1M 2.969 0.958
100K+100K 1.328 2.142
0+100K 0.312 1.006
19/08/2008 revno.72
Python 2.5.2 @ Windows XP, Intel Celeron M CPU 430 @ 1.73GHz
Read operation:
base 10K 0.016
100K 0.171 1.069
1M 1.734 1.084
100K+100K 0.375 1.172
0+100K 0.172 1.075
Write operation:
base 10K 0.016
100K 0.156 0.975
1M 1.532 0.957
100K+100K 0.344 1.075
0+100K 0.156 0.975
"""
@@ -0,0 +1,57 @@
# Copyright (c) 2011, Bernhard Leiner
# All rights reserved.
#
# Redistribution and use in source and binary forms,
# with or without modification, are permitted provided
# that the following conditions are met:
#
# * Redistributions of source code must retain
# the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce
# the above copyright notice, this list of conditions
# and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the author nor the names
# of its contributors may be used to endorse
# or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''Compatibility functions for python 2 and 3.
@author Bernhard Leiner (bleiner AT gmail com)
@version 1.0
'''
__docformat__ = "javadoc"
import sys
if sys.version_info[0] >= 3:
def asbytes(s):
if isinstance(s, bytes):
return s
return s.encode('latin1')
def asstr(s):
if isinstance(s, str):
return s
return s.decode('latin1')
else:
asbytes = str
asstr = str