|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +This script generates the ADC channel! macro with arguents for all pins |
| 5 | +of all chip subfamilies. |
| 6 | +
|
| 7 | +The strategy is, to get the maximum possible support for the most |
| 8 | +finest currently possible chip subfamily distinction. |
| 9 | +
|
| 10 | +That means, that some impls for some chips are more than the actual |
| 11 | +chip is actually capable of. |
| 12 | +""" |
| 13 | + |
| 14 | +import re |
| 15 | + |
| 16 | +import subprocess |
| 17 | + |
| 18 | +# Hacky way to create a dict variable with a bash script |
| 19 | +# which is then saved to a file which then can be directly imported. |
| 20 | +channel = subprocess.check_output(['bash', 'adc_channel.sh']) |
| 21 | +with open("gen.py", "wb") as f: |
| 22 | + f.write(channel) |
| 23 | + |
| 24 | +import gen |
| 25 | + |
| 26 | +CHIP_SELECT = { |
| 27 | + "stm32f301x6": r".*STM32F301.(6|8|\(6-8\)).*\.xml", |
| 28 | + "stm32f302x6": r".*STM32F302.(6|8|\(6-8\)).*\.xml", |
| 29 | + "stm32f303x6": r".*STM32F303.(6|8|\(6-8\)).*\.xml", |
| 30 | + "stm32f302xb": r".*STM32F302.(B|C|\(B-C\)|\(8-B-C\)).*\.xml", |
| 31 | + "stm32f303xb": r".*STM32F303.(B|C|\(B-C\)|\(8-B-C\)).*\.xml", |
| 32 | + "stm32f302xd": r".*STM32F302.(D|E|\(D-E\)).*\.xml", |
| 33 | + "stm32f303xd": r".*STM32F303.(D|E).*\.xml", |
| 34 | + "stm32f328": r".*STM32F328.*\.xml", |
| 35 | + "stm32f358": r".*STM32F358.*\.xml", |
| 36 | + "stm32f398": r".*STM32F398.*\.xml", |
| 37 | + "stm32f373": r".*STM32F37(3|8).*\.xml", |
| 38 | + "stm32f334": r".*STM32F334.*\.xml", |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +def main(): |
| 43 | + chip_post = {} |
| 44 | + |
| 45 | + # Split ADC into adc and channel |
| 46 | + for chip_name, pin_list in gen.CHANNELS.items(): |
| 47 | + pins = [] |
| 48 | + for pin in pin_list: |
| 49 | + adc_list = [] |
| 50 | + for i in [1, 2]: |
| 51 | + try: |
| 52 | + (adc, channel) = pin[i].split("_") |
| 53 | + except IndexError: |
| 54 | + continue |
| 55 | + adc_list.append((adc, int(channel))) |
| 56 | + pins.append((pin[0], adc_list)) |
| 57 | + chip_post[chip_name] = pins |
| 58 | + |
| 59 | + chip_results = {} |
| 60 | + |
| 61 | + # Group into chip categories and select chip with max entries |
| 62 | + # Choosing the maximum list is a compromise right now: |
| 63 | + # 1. The stm32cubemx database is much more fine-grained about |
| 64 | + # the types of chips than our current feature is |
| 65 | + # 2. Only allowing the minimal subset of all chip-subfamilies |
| 66 | + # is can be pretty annoying and would severly restrict the |
| 67 | + # channel implementation for the stm32f303xc and st32f303xd |
| 68 | + # for example. |
| 69 | + # 3. This goes a little against the current impl strategy, |
| 70 | + # that is, allowing more than a chip might be possible to use. |
| 71 | + # But the alternative of finer feature selection is a pretty |
| 72 | + # big change and gives no real benefit, other than annoying |
| 73 | + # the user. |
| 74 | + for chip, regex in CHIP_SELECT.items(): |
| 75 | + grouping = [] |
| 76 | + for chip_name, _ in chip_post.items(): |
| 77 | + if re.search(regex, chip_name): |
| 78 | + grouping.append(chip_post[chip_name]) |
| 79 | + grouping = list(max(grouping)) |
| 80 | + chip_results[chip] = grouping |
| 81 | + |
| 82 | + # Print the resulintg dictionary in a format, compatible with the |
| 83 | + # `channel!` macro. |
| 84 | + for type, result in chip_results.items(): |
| 85 | + type_variant_map = { |
| 86 | + 'x6': 'x8', |
| 87 | + 'xb': 'xc', |
| 88 | + 'xd': 'xe', |
| 89 | + } |
| 90 | + for key, value in type_variant_map.items(): |
| 91 | + if type[-2:] == key: |
| 92 | + type_variant = type[:-2] + value |
| 93 | + print(f'}} else if #[cfg(any(feature = "{type}", feature = "{type_variant}"))] {{') |
| 94 | + break |
| 95 | + else: |
| 96 | + print(f'}} else if #[cfg(feature = "{type}")] {{') |
| 97 | + print(" channel!([") |
| 98 | + for elem in result: |
| 99 | + line = f" ({', '.join([str(e) for e in elem])})" |
| 100 | + line = str(elem).replace("'", "") |
| 101 | + print(" " + line + ",") |
| 102 | + print(" ]);") |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments