|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""generate_boards.py: Script generate slc files for all boards """ |
| 4 | + |
| 5 | +import sys |
| 6 | +import argparse |
| 7 | +import subprocess |
| 8 | +from enum import Enum |
| 9 | +from dataclasses import dataclass |
| 10 | +from pathlib import Path |
| 11 | +import os |
| 12 | +import shutil |
| 13 | + |
| 14 | + |
| 15 | +class Boards(str, Enum): |
| 16 | + BRD4186A = "BRD4186A" |
| 17 | + BRD4186C = "BRD4186C" |
| 18 | + BRD4187A = "BRD4187A" |
| 19 | + BRD4187C = "BRD4187C" |
| 20 | + BRD2601B = "BRD2601B" |
| 21 | + BRD2703A = "BRD2703A" |
| 22 | + BRD2608A = "BRD2608A" |
| 23 | + BRD4116A = "BRD4116A" |
| 24 | + BRD4117A = "BRD4117A" |
| 25 | + BRD4118A = "BRD4118A" |
| 26 | + BRD2704A = "BRD2704A" |
| 27 | + BRD4316A = "BRD4316A" |
| 28 | + BRD4317A = "BRD4317A" |
| 29 | + BRD4318A = "BRD4318A" |
| 30 | + BRD4319A = "BRD4319A" |
| 31 | + ALL = "ALL" |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def is_board(boards, board) -> bool: |
| 35 | + if isinstance(board, boards): |
| 36 | + board = board.value |
| 37 | + if board not in boards.__members__: |
| 38 | + return False |
| 39 | + else: |
| 40 | + return True |
| 41 | + |
| 42 | + |
| 43 | +class Family(str, Enum): |
| 44 | + MG26 = "efr32mg26" |
| 45 | + MG24 = "efr32mg24" |
| 46 | + MGM24 = "mgm24" |
| 47 | + |
| 48 | + |
| 49 | +class BoardDict(dict): |
| 50 | + def __setitem__(self, key, value): |
| 51 | + if not isinstance(key, Boards): |
| 52 | + raise KeyError("Key must be an instance of MyEnum") |
| 53 | + super().__setitem__(key, value) |
| 54 | + |
| 55 | + def __getitem__(self, key): |
| 56 | + if not isinstance(key, Boards): |
| 57 | + raise KeyError("Key must be an instance of MyEnum") |
| 58 | + return super().__getitem__(key) |
| 59 | + |
| 60 | + |
| 61 | +@dataclass |
| 62 | +class Board: |
| 63 | + board: Boards |
| 64 | + slc_arguments: str |
| 65 | + family: Family |
| 66 | + |
| 67 | + |
| 68 | +class InvalidInput(Exception): |
| 69 | + def __init__(self, message): |
| 70 | + super().__init__(message) |
| 71 | + |
| 72 | + |
| 73 | +# Configuring slc arguments per board |
| 74 | +_boards = BoardDict() |
| 75 | +_boards[Boards.BRD4186A] = Board(board=Boards.BRD4186A, family=Family.MG24, |
| 76 | + slc_arguments="simple_led:led0:led1,simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,memlcd_usart,dmd_memlcd,i2cspm:sensor,brd4186a") |
| 77 | +_boards[Boards.BRD4186C] = Board(board=Boards.BRD4186C, family=Family.MG24, |
| 78 | + slc_arguments="simple_led:led0:led1,simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,memlcd_usart,dmd_memlcd,i2cspm:sensor,brd4186a") |
| 79 | +_boards[Boards.BRD4187A] = Board(board=Boards.BRD4187A, family=Family.MG24, |
| 80 | + slc_arguments="simple_led:led0:led1,simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,memlcd_usart,dmd_memlcd,i2cspm:sensor,brd4186c") |
| 81 | +_boards[Boards.BRD4187C] = Board(board=Boards.BRD4187C, family=Family.MG24, |
| 82 | + slc_arguments="simple_led:led0:led1,simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,memlcd_usart,dmd_memlcd,i2cspm:sensor,brd4187c") |
| 83 | +_boards[Boards.BRD2601B] = Board(board=Boards.BRD2601B, family=Family.MG24, |
| 84 | + slc_arguments="simple_led:led0:led1,simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,i2cspm:sensor,brd2601b") |
| 85 | +_boards[Boards.BRD2703A] = Board(board=Boards.BRD2703A, family=Family.MG24, |
| 86 | + slc_arguments="simple_led:led0:led1,simple_button:btn0:btn1,uartdrv_eusart:vcom,brd2703a") |
| 87 | +_boards[Boards.BRD2704A] = Board(board=Boards.BRD2704A, family=Family.MGM24, |
| 88 | + slc_arguments="simple_led:led0,uartdrv_eusart:vcom,brd2704a") |
| 89 | +_boards[Boards.BRD4316A] = Board(board=Boards.BRD4316A, family=Family.MGM24, |
| 90 | + slc_arguments="simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,brd4316a") |
| 91 | +_boards[Boards.BRD4317A] = Board(board=Boards.BRD4317A, family=Family.MGM24, |
| 92 | + slc_arguments="simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,i2cspm:sensor,memlcd_usart,dmd_memlcd,brd4317a") |
| 93 | +_boards[Boards.BRD4318A] = Board(board=Boards.BRD4318A, family=Family.MGM24, |
| 94 | + slc_arguments="simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,i2cspm:sensor,memlcd_usart,dmd_memlcd,brd4318a") |
| 95 | +_boards[Boards.BRD4319A] = Board(board=Boards.BRD4319A, family=Family.MGM24, |
| 96 | + slc_arguments="simple_button:btn0:btn1,uartdrv_eusart:vcom,brd4319a") |
| 97 | +_boards[Boards.BRD4116A] = Board(board=Boards.BRD4116A, family=Family.MG26, |
| 98 | + slc_arguments="simple_led:led0:led1,simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,memlcd_usart,dmd_memlcd,i2cspm:sensor,brd4116a") |
| 99 | +_boards[Boards.BRD4117A] = Board(board=Boards.BRD4117A, family=Family.MG26, |
| 100 | + slc_arguments="simple_led:led0:led1,simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,memlcd_usart,dmd_memlcd,i2cspm:sensor,brd4117a") |
| 101 | +_boards[Boards.BRD4118A] = Board(board=Boards.BRD4118A, family=Family.MG26, |
| 102 | + slc_arguments="simple_led:led0:led1,simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,memlcd_usart,dmd_memlcd,i2cspm:sensor,brd4118a") |
| 103 | +_boards[Boards.BRD2608A] = Board(board=Boards.BRD2608A, family=Family.MG26, |
| 104 | + slc_arguments="simple_led:led0:led1,simple_button:btn0:btn1,uartdrv_eusart:vcom,mx25_flash_shutdown_usart,i2cspm:sensor,brd2608a") |
| 105 | + |
| 106 | + |
| 107 | +def _parse_args(): |
| 108 | + """Parses and returns the command line arguments""" |
| 109 | + |
| 110 | + parser = argparse.ArgumentParser(description="Script to generate slc autogen and configuration files.", |
| 111 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 112 | + |
| 113 | + parser.add_argument("--sdk", metavar="si_sdk_path", required=False, default="../simplicity_sdk", |
| 114 | + help="Path to Si SDK to use to generate the slc autogen and configuration files.") |
| 115 | + parser.add_argument("--slcp", metavar="slcp_path", required=False, |
| 116 | + default="../../../examples/platform/silabs/matter-platform.slcp", help="Path to project slcp to use to generate slc files.") |
| 117 | + parser.add_argument("--board", metavar="board", required=False, default="all", |
| 118 | + help="Board for which to generaten autogen and configuration files.") |
| 119 | + parser.add_argument("--output", metavar="output_path", required=False, default="matter/efr32", |
| 120 | + help="Directory where the board genenration will be created. Fomart: ./family/board/output") |
| 121 | + parser.add_argument("--delete-files", metavar="delete-files", required=False, type=_convertStrToBool, default=True, |
| 122 | + help="Option to delete files step for each generation.") |
| 123 | + parser.add_argument("--revert-changes", metavar="revert-changes", required=False, type=_convertStrToBool, default=False, |
| 124 | + help="Option to revert changes to all files that changed. Should only be used after validating the changes aren't necessary.") |
| 125 | + |
| 126 | + return parser.parse_args() |
| 127 | + |
| 128 | + |
| 129 | +def _convertStrToBool(value): |
| 130 | + """ |
| 131 | + Converts a string representation of truth to a boolean value. |
| 132 | +
|
| 133 | + Args: |
| 134 | + value (str or bool): The value to convert. Can be a string like 'yes', 'true', 't', 'y', '1' for True, |
| 135 | + or 'no', 'false', 'f', 'n', '0' for False. If the value is already a boolean, it is returned as is. |
| 136 | +
|
| 137 | + Returns: |
| 138 | + bool: The corresponding boolean value. |
| 139 | +
|
| 140 | + Raises: |
| 141 | + argparse.ArgumentTypeError: If the string value does not match any of the expected true/false values. |
| 142 | + """ |
| 143 | + if isinstance(value, bool): |
| 144 | + return value |
| 145 | + if value.lower() in ('yes', 'true', 't', 'y', '1'): |
| 146 | + return True |
| 147 | + elif value.lower() in ('no', 'false', 'f', 'n', '0'): |
| 148 | + return False |
| 149 | + else: |
| 150 | + raise argparse.ArgumentTypeError('Boolean value expected.') |
| 151 | + |
| 152 | + |
| 153 | +def _configure_sdk(si_sdk_path): |
| 154 | + """ |
| 155 | + Configures slc sdk permissions to enable generation |
| 156 | + """ |
| 157 | + subprocess.run(["slc", "configuration", "--sdk", si_sdk_path], check=True) |
| 158 | + subprocess.run(["slc", "signature", "trust", "--sdk", si_sdk_path], check=True) |
| 159 | + |
| 160 | + |
| 161 | +def _generate_slc(args): |
| 162 | + """ |
| 163 | + Main application function |
| 164 | +
|
| 165 | + Args: |
| 166 | + args (Namespace): The arguments passed to the function. It should contain: |
| 167 | + - board (str): The name of the board to generate the SLC for. |
| 168 | + - sdk (str): The path to the SI SDK. |
| 169 | + - slcp (str): The path to the SLCP. |
| 170 | + - output (str): The path where the output should be saved. |
| 171 | + - delete_files (bool): A flag indicating whether to delete files after generation. |
| 172 | +
|
| 173 | + Raises: |
| 174 | + InvalidInput: If the specified board is not part of the supported boards. |
| 175 | +
|
| 176 | + """ |
| 177 | + |
| 178 | + if args.board not in Boards.__members__: |
| 179 | + raise InvalidInput("Board is not part of the supported boards!") |
| 180 | + |
| 181 | + board = Boards(args.board) |
| 182 | + |
| 183 | + if board == Boards.ALL: |
| 184 | + _generate_all_boards(si_sdk_path=args.sdk, slcp_path=args.slcp, output_path=args.output, |
| 185 | + delete_files=args.delete_files, revert_changes=args.revert_changes) |
| 186 | + else: |
| 187 | + _generate_board(board=_boards[board], si_sdk_path=args.sdk, slcp_path=args.slcp, |
| 188 | + output_path=args.output, delete_files=args.delete_files, revert_changes=args.revert_changes) |
| 189 | + |
| 190 | + |
| 191 | +def _generate_all_boards(si_sdk_path: str, slcp_path: str, output_path: str, delete_files: bool, revert_changes: bool): |
| 192 | + """ |
| 193 | + Generates slc files for all supported boards. |
| 194 | +
|
| 195 | + Args: |
| 196 | + si_sdk_path (str): The path to the SI SDK. |
| 197 | + slcp_path (str): The path to the SLCP. |
| 198 | + output_path (str): The path where the output should be saved. |
| 199 | + delete_files (bool): A flag indicating whether to delete files after generation. |
| 200 | + revert_changes (bool); A flag indicating whether to revert changes after generation |
| 201 | + """ |
| 202 | + |
| 203 | + for board in _boards: |
| 204 | + _generate_board(board=_boards[board], si_sdk_path=si_sdk_path, |
| 205 | + slcp_path=slcp_path, output_path=output_path, delete_files=delete_files, revert_changes=revert_changes) |
| 206 | + |
| 207 | + |
| 208 | +def _generate_board(board: Board, si_sdk_path: str, slcp_path: str, output_path: str, delete_files: bool, revert_changes: bool): |
| 209 | + """ |
| 210 | + Generates the SLC (System Level Configuration) for a specific board. |
| 211 | +
|
| 212 | + Args: |
| 213 | + board (Board): The board for which the SLC is to be generated. |
| 214 | + si_sdk_path (str): The path to the SI SDK. |
| 215 | + slcp_path (str): The path to the SLCP. |
| 216 | + output_path (str): The path where the output should be saved. |
| 217 | + delete_files (bool): A flag indicating whether to delete files after generation. |
| 218 | + revert_changes (bool); A flag indicating whether to revert changes after generation |
| 219 | + """ |
| 220 | + |
| 221 | + # Configure path variables |
| 222 | + si_sdk_path = Path(si_sdk_path) |
| 223 | + slcp_path = Path(slcp_path) |
| 224 | + output_path = Path(output_path) |
| 225 | + output_path = os.path.join(output_path, board.family.value, board.board.value) |
| 226 | + |
| 227 | + # run slc generate command |
| 228 | + subprocess.run(["slc", "generate", slcp_path, "-d", output_path, "--with", board.slc_arguments], check=True) |
| 229 | + |
| 230 | + # delete files generated files |
| 231 | + if delete_files: |
| 232 | + _delete_files(output_path) |
| 233 | + _delete_directories(output_path) |
| 234 | + |
| 235 | + # Revert changes |
| 236 | + if revert_changes: |
| 237 | + _revert_changes(output_path) |
| 238 | + |
| 239 | + |
| 240 | +def _delete_files(output_path: Path): |
| 241 | + """ |
| 242 | + Deletes specified files from the output path after each generation. |
| 243 | +
|
| 244 | + Args: |
| 245 | + output_path (Path): The path where the files to be deleted are located. |
| 246 | +
|
| 247 | + """ |
| 248 | + |
| 249 | + # Files to delete after each generation |
| 250 | + files_to_delete = ["autogen/.crc_config.crc", |
| 251 | + "autogen/linkerfile.ld", |
| 252 | + "config/FreeRTOSConfig.h", |
| 253 | + "config/SEGGER_RTT_Conf.h", |
| 254 | + "config/sl_openthread_ble_cli_config.h", |
| 255 | + "config/sl_openthread_generic_config.h", |
| 256 | + "config/sl_debug_swo_config.h", |
| 257 | + "matter-platform.Makefile", |
| 258 | + "matter-platform.project.mak", |
| 259 | + "vscode.conf"] |
| 260 | + |
| 261 | + # Loop and delete files if they exist |
| 262 | + for file_name in files_to_delete: |
| 263 | + path = os.path.join(output_path, file_name) |
| 264 | + if os.path.isfile(path): |
| 265 | + os.remove(path) |
| 266 | + print(f"{path} has been deleted.") |
| 267 | + else: |
| 268 | + print(f"{path} does not exist.") |
| 269 | + |
| 270 | + |
| 271 | +def _delete_directories(output_path: Path): |
| 272 | + """ |
| 273 | + Deletes specified directories from the output path after each generation. |
| 274 | +
|
| 275 | + Args: |
| 276 | + output_path (Path): The path where the directories to be deleted are located. |
| 277 | +
|
| 278 | + """ |
| 279 | + # Directories to delete after each generation |
| 280 | + directories_to_delete = ["linker_options", |
| 281 | + "matter-platform_cmake"] |
| 282 | + |
| 283 | + # Loop and delete directories if they exist |
| 284 | + for dir_path in directories_to_delete: |
| 285 | + path = os.path.join(output_path, dir_path) |
| 286 | + if os.path.isdir(path): |
| 287 | + shutil.rmtree(path) |
| 288 | + print(f"{path} and its contents have been deleted.") |
| 289 | + else: |
| 290 | + print(f"{path} does not exist or is not a directory.") |
| 291 | + |
| 292 | + |
| 293 | +def _revert_changes(output_path: Path): |
| 294 | + """ |
| 295 | + Reverts changes to specified files in the output path using Git. |
| 296 | +
|
| 297 | + Args: |
| 298 | + output_path (Path): The path where the files to be reset are located. |
| 299 | +
|
| 300 | + """ |
| 301 | + files_to_reset = ["autogen/gatt_db.h", |
| 302 | + "autogen//sl_component_catalog.h", |
| 303 | + "autogen/sl_simple_led_instances.h", |
| 304 | + "autogen/sl_event_handler.c", |
| 305 | + "autogen/sl_event_handler.h", |
| 306 | + "autogen/sl_ot_init.c", |
| 307 | + "autogen/sli_mbedtls_config_autogen.h", |
| 308 | + "autogen/sli_psa_builtin_config_autogen.h", |
| 309 | + ] |
| 310 | + |
| 311 | + for file in files_to_reset: |
| 312 | + path = os.path.join(output_path, file) |
| 313 | + try: |
| 314 | + subprocess.run(['git', 'checkout', '--', path], check=True) |
| 315 | + print(f"Changes removed for {path}") |
| 316 | + except subprocess.CalledProcessError as e: |
| 317 | + print(f"Failed to remove changes for {path}: {e}") |
| 318 | + |
| 319 | + |
| 320 | +def main(args) -> int: |
| 321 | + args.board = args.board.upper() |
| 322 | + |
| 323 | + # Step 1: Configure slc sdk permissions for generation |
| 324 | + _configure_sdk(args.sdk) |
| 325 | + |
| 326 | + # Step 2: Generate slc files |
| 327 | + _generate_slc(args) |
| 328 | + |
| 329 | + |
| 330 | +if __name__ == "__main__": |
| 331 | + args = _parse_args() |
| 332 | + sys.exit(main(args)) |
0 commit comments