xref: /btstack/chipset/atwilc3000/convert_firmware_bin.py (revision 6f6f8b16b187b6f853cd67c766447d952356c78d)
1#!/usr/bin/env python3
2# BlueKitchen GmbH (c) 2017
3import sys
4
5usage = '''This script converts the HCI Firmware in .bin format for Atmel WILC3000
6into C files to be used with BTstack.
7
8Usage:
9$ ./convert_firmware_bin.py firmware.bin
10'''
11
12header = '''
13/**
14 * BASENAME.h converted from BASENAME.bin
15 */
16
17#ifndef BASENAME_H
18#define BASENAME_H
19
20#include <stdint.h>
21
22extern const uint8_t  atwilc3000_fw_data[];
23extern const uint32_t atwilc3000_fw_size;
24extern const char *   atwilc3000_fw_name;
25
26#endif
27'''
28
29code_start = '''
30/**
31 * BASENAME.c converted from BASENAME.bin
32 */
33
34#include "BASENAME.h"
35
36const char *   atwilc3000_fw_name = "BASENAME";
37
38const uint8_t  atwilc3000_fw_data[] = {
39'''
40
41code_end = '''
42};
43const uint32_t atwilc3000_fw_size = sizeof(atwilc3000_fw_data);
44'''
45
46def convert_bin(basename):
47	bin_name = basename + '.bin'
48	print ('Reading %s' % bin_name)
49
50	with open (bin_name, 'rb') as fin:
51		firm = fin.read()
52		size = len(firm)
53		print ('Size %u', size)
54
55		with open(basename + '.h', 'w') as fout:
56			fout.write(header.replace('BASENAME',basename));
57
58		with open(basename + '.c', 'w') as fout:
59			fout.write(code_start.replace('BASENAME',basename));
60			fout.write('    ')
61			for i in range(0,size):
62				if i % 10000 == 0:
63					print ('- Write %05u/%05u' % (i, size))
64				byte = ord(firm[i])
65				fout.write("0x{0:02x}, ".format(byte))
66				if (i & 0x0f) == 0x0f:
67					fout.write('\n    ')
68			fout.write(code_end);
69			print ('Done\n')
70
71# check usage: 1 param
72if not len(sys.argv) == 2:
73    print(usage)
74    sys.exit(1)
75
76name = sys.argv[1]
77basename = name.replace('.bin','')
78convert_bin(basename)
79