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