xref: /btstack/chipset/em9301/convert_emp.py (revision f2122e1b0c71b95f88fd663a687353d8fb7386d9)
1*f2122e1bSMatthias Ringwald#!/usr/bin/env python
2*f2122e1bSMatthias Ringwald# BlueKitchen GmbH (c) 2018
3*f2122e1bSMatthias Ringwaldimport sys
4*f2122e1bSMatthias Ringwald
5*f2122e1bSMatthias Ringwaldusage = '''This script converts a set of configurations and patches in .emp format for EM9304
6*f2122e1bSMatthias Ringwaldinto C files to be used with BTstack.
7*f2122e1bSMatthias Ringwald
8*f2122e1bSMatthias RingwaldUsage:
9*f2122e1bSMatthias Ringwald$ ./convert_emp.py container.emp
10*f2122e1bSMatthias Ringwald'''
11*f2122e1bSMatthias Ringwald
12*f2122e1bSMatthias Ringwaldheader = '''/**
13*f2122e1bSMatthias Ringwald * BASENAME.h converted from BASENAME.emp
14*f2122e1bSMatthias Ringwald */
15*f2122e1bSMatthias Ringwald
16*f2122e1bSMatthias Ringwald#ifndef __BASENAME_H
17*f2122e1bSMatthias Ringwald#define __BASENAME_H
18*f2122e1bSMatthias Ringwald
19*f2122e1bSMatthias Ringwald#include <stdint.h>
20*f2122e1bSMatthias Ringwaldextern const uint8_t  container_blob_data[];
21*f2122e1bSMatthias Ringwaldextern const uint32_t container_blob_size;
22*f2122e1bSMatthias Ringwaldextern const char *   container_blob_name;
23*f2122e1bSMatthias Ringwald
24*f2122e1bSMatthias Ringwald#endif
25*f2122e1bSMatthias Ringwald'''
26*f2122e1bSMatthias Ringwald
27*f2122e1bSMatthias Ringwaldcode_start = '''/**
28*f2122e1bSMatthias Ringwald * BASENAME.c converted from BASENAME.bin
29*f2122e1bSMatthias Ringwald * Size: SIZE bytes
30*f2122e1bSMatthias Ringwald */
31*f2122e1bSMatthias Ringwald
32*f2122e1bSMatthias Ringwald#include <stdint.h>
33*f2122e1bSMatthias Ringwald
34*f2122e1bSMatthias Ringwaldconst char *   container_blob_name = "BASENAME";
35*f2122e1bSMatthias Ringwald
36*f2122e1bSMatthias Ringwaldconst uint8_t  container_blob_data[] = {
37*f2122e1bSMatthias Ringwald'''
38*f2122e1bSMatthias Ringwald
39*f2122e1bSMatthias Ringwaldcode_end = '''
40*f2122e1bSMatthias Ringwald};
41*f2122e1bSMatthias Ringwaldconst uint32_t container_blob_size = sizeof(container_blob_data);
42*f2122e1bSMatthias Ringwald'''
43*f2122e1bSMatthias Ringwald
44*f2122e1bSMatthias Ringwalddef convert_emp(basename):
45*f2122e1bSMatthias Ringwald	emp_name = basename + '.emp'
46*f2122e1bSMatthias Ringwald	print ('Reading %s' % emp_name)
47*f2122e1bSMatthias Ringwald
48*f2122e1bSMatthias Ringwald	with open (emp_name, 'rb') as fin:
49*f2122e1bSMatthias Ringwald		firm = fin.read()
50*f2122e1bSMatthias Ringwald		size = len(firm)
51*f2122e1bSMatthias Ringwald
52*f2122e1bSMatthias Ringwald		# reduce size by 4 as it ends with four zero bytes that indicate the end of the file
53*f2122e1bSMatthias Ringwald		size -= 4
54*f2122e1bSMatthias Ringwald		print ('Size %u' % size)
55*f2122e1bSMatthias Ringwald
56*f2122e1bSMatthias Ringwald
57*f2122e1bSMatthias Ringwald		# don't write .h file as we would need to store its name in btstack_chipset_em9301.c, too
58*f2122e1bSMatthias Ringwald		# with open(basename + '.h', 'w') as fout:
59*f2122e1bSMatthias Ringwald		# 	fout.write(header.replace('BASENAME',basename));
60*f2122e1bSMatthias Ringwald
61*f2122e1bSMatthias Ringwald		with open(basename + '.c', 'w') as fout:
62*f2122e1bSMatthias Ringwald			fout.write(code_start.replace('BASENAME',basename).replace('SIZE',str(size)));
63*f2122e1bSMatthias Ringwald			fout.write('    ')
64*f2122e1bSMatthias Ringwald			for i in range(0,size):
65*f2122e1bSMatthias Ringwald				if i % 1000 == 0:
66*f2122e1bSMatthias Ringwald					print ('- Write %05u/%05u' % (i, size))
67*f2122e1bSMatthias Ringwald				byte = ord(firm[i])
68*f2122e1bSMatthias Ringwald				fout.write("0x{0:02x}, ".format(byte))
69*f2122e1bSMatthias Ringwald				if (i & 0x0f) == 0x0f:
70*f2122e1bSMatthias Ringwald					fout.write('\n    ')
71*f2122e1bSMatthias Ringwald			fout.write(code_end);
72*f2122e1bSMatthias Ringwald			print ('Done\n')
73*f2122e1bSMatthias Ringwald
74*f2122e1bSMatthias Ringwald# check usage: 1 param
75*f2122e1bSMatthias Ringwaldif not len(sys.argv) == 2:
76*f2122e1bSMatthias Ringwald    print(usage)
77*f2122e1bSMatthias Ringwald    sys.exit(1)
78*f2122e1bSMatthias Ringwald
79*f2122e1bSMatthias Ringwaldname = sys.argv[1]
80*f2122e1bSMatthias Ringwaldbasename = name.replace('.emp','')
81*f2122e1bSMatthias Ringwaldconvert_emp(basename)
82