1#!/usr/bin/env python3 2# BlueKitchen GmbH (c) 2012-2014 3 4# avr-objcopy -I ihex -O binary hci_581_active_uart.hex hci_581_active_uart.bin 5 6# requires IntelHex package https://pypi.python.org/pypi/IntelHex 7# docs: http://python-intelhex.readthedocs.io/en/latest/ 8 9from intelhex import IntelHex 10import glob 11import sys 12 13usage = '''This script converts the HCI Firmware in .hex format for Dialog Semiconductor 14into C files to be used with BTstack. 15''' 16 17header = ''' 18/** 19 * BASENAME.h converted from BASENAME.hex 20 */ 21 22#ifndef BASENAME_H 23#define BASENAME_H 24 25#include <stdint.h> 26 27extern const uint8_t da14581_fw_data[]; 28extern const uint32_t da14581_fw_size; 29extern const char * da14581_fw_name; 30 31#endif 32''' 33 34code_start = ''' 35/** 36 * BASENAME.c converted from BASENAME.hex 37 */ 38 39#include "BASENAME.h" 40 41const char * da14581_fw_name = "BASENAME"; 42 43const uint8_t da14581_fw_data[] = { 44''' 45 46code_end = ''' 47}; 48const uint32_t da14581_fw_size = sizeof(da14581_fw_data); 49''' 50 51def convert_hex(basename): 52 hex_name = basename + '.hex' 53 print('Reading %s' % hex_name) 54 ih = IntelHex(hex_name) 55 # f = open(basename + '.txt', 'w') # open file for writing 56 # ih.dump(f) # dump to file object 57 # f.close() # close file 58 size = ih.maxaddr() - ih.minaddr() + 1 59 print('- Start: %x' % ih.minaddr()) 60 print('- End: %x' % ih.maxaddr()) 61 62 with open(basename + '.h', 'w') as fout: 63 fout.write(header.replace('BASENAME',basename)); 64 65 with open(basename + '.c', 'w') as fout: 66 fout.write(code_start.replace('BASENAME',basename)); 67 fout.write(' ') 68 for i in range(0,size): 69 if i % 1000 == 0: 70 print('- Write %05u/%05u' % (i, size)) 71 byte = ih[ih.minaddr() + i] 72 fout.write("0x{0:02x}, ".format(byte)) 73 if (i & 0x0f) == 0x0f: 74 fout.write('\n ') 75 fout.write(code_end); 76 print ('Done\n') 77 78 79files = glob.glob('*.hex') 80if not files: 81 print(usage) 82 sys.exit(1) 83 84# convert each of them 85for name in files: 86 basename = name.replace('.hex','') 87 convert_hex(basename) 88