xref: /btstack/chipset/da145xx/convert_hex_files.py (revision aaa1682ba28bbe779e8e859f8529b64c4cd5f80d)
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  da145xx_fw_data[];
28extern const uint32_t da145xx_fw_size;
29extern const char *   da145xx_fw_name;
30
31#endif
32'''
33
34code_start = '''
35/**
36 * BASENAME.c converted from BASENAME.hex
37 */
38
39#define BTSTACK_FILE__ "BASENAME.c"
40
41#include "BASENAME.h"
42
43const char *   da145xx_fw_name = "BASENAME";
44
45const uint8_t  da145xx_fw_data[] = {
46'''
47
48code_end = '''
49};
50const uint32_t da145xx_fw_size = sizeof(da145xx_fw_data);
51'''
52
53def convert_hex(basename):
54	hex_name = basename + '.hex'
55	print('Reading %s' % hex_name)
56	ih = IntelHex(hex_name)
57	# f = open(basename + '.txt', 'w') # open file for writing
58	# ih.dump(f)                    # dump to file object
59	# f.close()                     # close file
60	size = 	ih.maxaddr() - ih.minaddr() + 1
61	print('- Start: %x' % ih.minaddr())
62	print('- End:   %x' % ih.maxaddr())
63
64	with open(basename + '.h', 'w') as fout:
65		fout.write(header.replace('BASENAME',basename));
66
67	with open(basename + '.c', 'w') as fout:
68		fout.write(code_start.replace('BASENAME',basename));
69		fout.write('    ')
70		for i in range(0,size):
71			if i % 1000 == 0:
72				print('- Write %05u/%05u' % (i, size))
73			byte = ih[ih.minaddr() + i]
74			fout.write("0x{0:02x}, ".format(byte))
75			if (i & 0x0f) == 0x0f:
76				fout.write('\n    ')
77		fout.write(code_end);
78		print ('Done\n')
79
80
81files =  glob.glob('*.hex')
82if not files:
83    print(usage)
84    sys.exit(1)
85
86# convert each of them
87for name in files:
88	basename = name.replace('.hex','')
89	convert_hex(basename)
90