1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Injects pre main init to ST startup scripts.""" 15 16 17import pathlib 18import re 19 20 21def add_pre_main_init(startup: str) -> str: 22 """Add pw_stm32cube_Init call to startup file 23 24 The stm32cube startup files directly call main(), while pigweed expects to 25 do some setup before main is called. This could include sys_io or system 26 clock initialization. 27 28 This adds a call to `pw_stm32cube_Init()` immediately before the call to 29 `main()` 30 31 Args: 32 startup: The startup script read into a string 33 34 Returns: 35 A new startup script with the `pw_stm32cube_Init()` call added. 36 37 Raises: 38 ValueError if the `main()` call is not found in `startup` 39 """ 40 match = re.search(r'\s*bl\s+main', startup) 41 if match is None: 42 raise ValueError("`bl main` not found in startup script") 43 44 return ( 45 startup[: match.start()] 46 + '\nbl pw_stm32cube_Init' 47 + startup[match.start() :] 48 ) 49 50 51def inject_init(startup_in: pathlib.Path, startup_out: pathlib.Path | None): 52 """Injects pw_stm32cube_Init before main in given ST startup script. 53 54 Args: 55 startup_in: path to startup_*.s file 56 startup_out: path to write generated startup file or None. 57 If None, output startup script printed to stdout 58 """ 59 with open(startup_in, 'rb') as startup_in_file: 60 startup_in_str = startup_in_file.read().decode('utf-8', errors='ignore') 61 62 startup_out_str = add_pre_main_init(startup_in_str) 63 64 if startup_out: 65 with open(startup_out, 'w') as startup_out_file: 66 startup_out_file.write(startup_out_str) 67 else: 68 print(startup_out_str) 69