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