aboutsummaryrefslogtreecommitdiff
path: root/driver_fw/tools/gen_isr_header.py
blob: c965fc8f8e9eb34f9c6b4c7a44d3aeec3392515d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3

import subprocess
import os
import re
import datetime
from pathlib import Path

def cpp_preprocess(input_path, cpp='cpp'):
    return subprocess.check_output([cpp, '-P', input_path]).decode()

def gen_isr_header(f, cpp='cpp'):
    stripped_code = cpp_preprocess(args.input, args.use_cpp)

    armed = False
    for line in stripped_code.splitlines():
        line = line.strip()

        if armed:
            if not line.startswith('.word'):
                break

            word, value = line.split()
            assert word == '.word'
            if value == '0':
                yield None
            else:
                yield value

        else:
            if line.startswith('g_pfnVectors:'):
                armed = True

    else:
        raise ValueError('Cannot find interrupt vector definition!')

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--use-cpp', type=str, default=os.getenv('CPP', 'cpp'), help='cpp (C preprocessor) executable to use')
    parser.add_argument('-g', '--generate-include-guards', action='store_true', help='Whether to generate include guards')
    parser.add_argument('input', help='Input stm32****_startup.s file')
    args = parser.parse_args()

    print('/* AUTOGENERATED FILE! DO NOT MODIFY! */')
    print(f'/* Generated {datetime.datetime.utcnow()} from {args.input} by {Path(__file__).name} */')
    if args.generate_include_guards:
        include_guard_id = '__ISR_HEADER_' + re.sub('[^A-Za-z0-9]', '_', args.input.split('/')[-1]) + '__'
        print(f'#ifndef {include_guard_id}')
        print(f'#define {include_guard_id}')

    print()
    for i, handler_name in enumerate(gen_isr_header(args.input, args.use_cpp)):
        if handler_name is None:
            print(f'/* IRQ {i} is undefined for this part. */')
        else:
            print(f'void {handler_name}(void); {" " * (30-len(handler_name))} /* {i:> 3} */')
    print()

    print(f'#define NUM_IRQs {i+1}')
    print('extern uint32_t g_pfnVectors[NUM_IRQs];')
    print('#define isr_vector g_pfnVectors')
    print()

    if args.generate_include_guards:
        print(f'#endif /* {include_guard_id} */')