#!/usr/bin/env python3 # # Intel HEX to ST microelectronics DfuSe file converter # Copyright (C)2015 Thomas Kindler # # 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 . # 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 as 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.tobinarray() if len(image_data) != len(bytes(image_data)): # catch and correct python2.7 issue using this hack image_data = ih.tobinstr() data = struct.pack( "