xref: /btstack/chipset/csr/convert_psr.py (revision c9921182ab4b1f83e3e5c671446dca5ffdf45b90)
1#!/usr/bin/env python3
2# BlueKitchen GmbH (c) 2019
3
4import sys
5
6usage = '''
7CSR/Qualcomm PSR conversion tool for use with BTstack
8Copyright 2019 BlueKitchen GmbH
9
10Usage:
11$ ./convert_prs.py file.prs
12
13'''
14
15msg_seqno = 0
16indent = '    '
17
18def write_verbatim(fout, text):
19    fout.write(text + '\n')
20
21def write_set_varid_cmd(fout, varid, msg_payload):
22    global msg_seqno
23
24    # msg_payload must be at least 4 uint16
25    while len(msg_payload) < 4:
26        msg_payload.append(0)
27
28    # setup msg
29    msg_type   = 0x0002
30    msg_len    = 5 + len(msg_payload)
31    msg_seqno += 1
32    msg_status = 0
33    msg = [msg_type, msg_len, msg_seqno, varid, msg_status] + msg_payload
34
35    # setup hci command
36    hci_payload_descriptor = 0xc2
37    hci_param_len          = len(msg) * 2 + 1
38    hci_cmd = [ 0x00, 0xfc, hci_param_len, hci_payload_descriptor]
39
40    # convert list of uint16_t to sequence of uint8_t in little endian
41    for value in msg:
42        hci_cmd.append(value & 0xff)
43        hci_cmd.append(value >> 8)
44
45    fout.write(indent + ', '.join(["0x%02x" % val for val in hci_cmd]) + ',\n')
46
47def write_key(fout, key, value):
48    # setup ps command
49    payload_len = len(value)
50    stores   = 0x0008 # psram
51    ps_cmd = [key, payload_len, stores] + value
52
53    # write ps command
54    write_set_varid_cmd(fout, 0x7003, ps_cmd)
55
56def write_warm_reset(fout):
57    write_verbatim(fout, indent + "// WarmReset")
58    write_set_varid_cmd(fout, 0x4002, [])
59
60
61# check args
62if len(sys.argv) != 2:
63    print(usage)
64    sys.exit(1)
65
66prs_file = sys.argv[1]
67fout     = sys.stdout
68
69with open (prs_file, 'rt') as fin:
70    for line_with_nl in fin:
71        line = line_with_nl.strip()
72        if line.startswith('&'):
73            # pskey
74            parts = line.split('=')
75            key = int(parts[0].strip().replace('&','0x'), 16)
76            value = [int('0x'+i, 16) for i in parts[1].strip().split(' ')]
77            write_key(fout, key, value)
78        elif line.startswith('#'):
79            #ifdef, ..
80            write_verbatim(fout, line)
81        else:
82            # comments
83            write_verbatim(fout, indent + line)
84
85write_warm_reset(fout)
86