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