summaryrefslogtreecommitdiff
path: root/tools/gen_isr_header.py
blob: 6382b1fa5345f0157b901b940b80bccf3cae169d (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
#!/usr/bin/env python3

import subprocess
import os
import re
import datetime

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('/* Generated {datetime.datetime.utcnow()} from {args.input} */')
    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} */')