atwilc3000: convert .bin into .h + .c file

This commit is contained in:
Matthias Ringwald 2017-07-23 12:51:46 +02:00
parent c823631b5a
commit 44149d3a0f
2 changed files with 84 additions and 2 deletions

View File

@ -9,8 +9,7 @@ wilc3000_bt_firmware.bin:
curl -O https://raw.githubusercontent.com/atwilc3000/firmware/master/wilc3000_bt_firmware.bin
wilc3000_bt_firmware_no_rtc.bin:
curl -O https://raw.github.com/atwilc3000/firmware/raw/master/wilc3000_bt_firmware_no_rtc.bin
curl -O https://raw.githubusercontent.com/atwilc3000/firmware/master/wilc3000_bt_firmware_no_rtc.bin
clean:
rm -f *.h *.c

View File

@ -0,0 +1,83 @@
#!/usr/bin/env python
# BlueKitchen GmbH (c) 2012-2014
# avr-objcopy -I ihex -O binary hci_581_active_uart.hex hci_581_active_uart.bin
# requires IntelHex package https://pypi.python.org/pypi/IntelHex
# docs: http://python-intelhex.readthedocs.io/en/latest/
from intelhex import IntelHex
import glob
usage = '''This script converts the HCI Firmware in .bin format for Atmel WILC3000
into C files to be used with BTstack.
'''
header = '''
/**
* BASENAME.h converted from BASENAME.bin
*/
#ifndef __BASENAME_H
#define __BASENAME_H
#include <stdint.h>
extern const uint8_t atwilc3000_fw_data[];
extern const uint32_t atwilc3000_fw_size;
extern const char * atwilc3000_fw_name;
#endif
'''
code_start = '''
/**
* BASENAME.c converted from BASENAME.bin
*/
#include "BASENAME.h"
const char * atwilc3000_fw_name = "BASENAME";
const uint8_t atwilc3000_fw_data[] = {
'''
code_end = '''
};
const uint32_t atwilc3000_fw_size = sizeof(atwilc3000_fw_data);
'''
def convert_bin(basename):
bin_name = basename + '.bin'
print ('Reading %s' % bin_name)
with open (bin_name, 'rb') as fin:
firm = fin.read()
size = len(firm)
print ('Size %u', size)
with open(basename + '.h', 'w') as fout:
fout.write(header.replace('BASENAME',basename));
with open(basename + '.c', 'w') as fout:
fout.write(code_start.replace('BASENAME',basename));
fout.write(' ')
for i in range(0,size):
if i % 10000 == 0:
print ('- Write %05u/%05u' % (i, size))
byte = ord(firm[i])
fout.write("0x{0:02x}, ".format(byte))
if (i & 0x0f) == 0x0f:
fout.write('\n ')
fout.write(code_end);
print ('Done\n')
files = glob.glob('*.bin')
if not files:
print(usage)
sys.exit(1)
# convert each of them
for name in files:
basename = name.replace('.bin','')
convert_bin(basename)