diff --git a/README.md b/README.md index 60a84c0..bfa53f7 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ All the relevant settings are under the 'Project Manager' tab and should be set Setting | Value -- | -- -Project > Do not generate the main() | SET +Project > Do not generate the main() | UNSET Project > Toolchain / IDE | 'CMake' Code Generator > STM32Cube MCU packages and embedded software packages | 'Copy only necessary library files' Code Generator > Generate peripheral initialization as a pair of '.c/.h' files per peripheral | SET @@ -66,3 +66,7 @@ After project creation of after modifying the IOC, code generation needs to be i ### The remaining work After code generation and convertion, a more manual part of project setup remains. MX Convert only prepares code for integration with CMake projects, the base CMake should be built around the cubemx folder, and it's setup is outside the scope of this tool and guide. + +### TODO + +Explain `my_main` and FREERTOS use. diff --git a/mx_convert/mx_convert.py b/mx_convert/mx_convert.py index caf39d8..1c970dc 100644 --- a/mx_convert/mx_convert.py +++ b/mx_convert/mx_convert.py @@ -10,10 +10,6 @@ MX_DISCARD_PATHS = [ ".mxproject", "CMakePresets.json", ] -#MX_MOVE_PATHS = [ -# ("Core/Inc/main.h", "Core/Inc/mx-main.h"), #TODO: Force rename in includes as well -# ("Core/Src/main.c", "Core/Src/mx-main.c"), -#] SOURCE_EXTS = [ "*.c", @@ -26,9 +22,10 @@ HEADER_EXTS = [ CMAKE_EXCLUDE_SOURCES = [ "*_template.c" ] -CMAKE_TARGET = "" # Set from config +CMAKE_TARGET = "" ASSUME_YES = False -WEAK_HANDLERS = True +STRONG_HANDLERS = False +NO_MY_MAIN = False def mx_cleanup(path: Path): @@ -113,10 +110,10 @@ def weaken_handlers_file(path: Path): content = _if.read() myre = re.compile(r"void ([A-Z][A-Za-z]+)_Handler\(void\)") - replaced = myre.sub(r"__attribute__((weak)) void \1_Handler(void)", content) + patched = myre.sub(r"__attribute__((weak)) void \1_Handler(void)", content) with open(path, "w") as of: - of.write(replaced) + of.write(patched) def weaken_handlers(path: Path): root = path / "Core/Src" @@ -126,6 +123,17 @@ def weaken_handlers(path: Path): not any(child.match(exclude) for exclude in CMAKE_EXCLUDE_SOURCES): weaken_handlers_file(child) +def call_my_main(path: Path): + main_c = path / "Core/Src/main.c" + + with open(main_c, "r") as _if: + content = _if.read() + + patched = content.replace("/* USER CODE BEGIN PFP */", "/* USER CODE BEGIN PFP */\nvoid my_main();").replace("/* USER CODE BEGIN WHILE */", "/* USER CODE BEGIN WHILE */\nmy_main();") + + with open(main_c, "w") as of: + of.write(patched) + def load_config(path: Path): if not path.exists(): return @@ -145,7 +153,8 @@ def overlay_config(config: dict): global CMAKE_TARGET global CMAKE_EXCLUDE_SOURCES global ASSUME_YES - global WEAK_HANDLERS + global STRONG_HANDLERS + global NO_MY_MAIN if "discard" in config and config["discard"]: if not isinstance(config["discard"], list): @@ -177,11 +186,16 @@ def overlay_config(config: dict): print("assume-yes must be a bool") exit(1) ASSUME_YES |= config["assume-yes"] - if "weak-handlers" in config and config["weak-handlers"]: - if not isinstance(config["weak-handlers"], bool): - print("weak-handlers must be a bool") + if "strong-handlers" in config and config["strong-handlers"]: + if not isinstance(config["strong-handlers"], bool): + print("strong-handlers must be a bool") exit(1) - WEAK_HANDLERS &= config["weak-handlers"] + STRONG_HANDLERS |= config["strong-handlers"] + if "no-my-main" in config and config["no-my-main"]: + if not isinstance(config["no-my-main"], bool): + print("no-my-main must be a bool") + exit(1) + NO_MY_MAIN |= config["no-my-main"] def print_config(): print("MX_DISCARD_PATHS", MX_DISCARD_PATHS) @@ -190,7 +204,8 @@ def print_config(): print("CMAKE_EXCLUDE_SOURCES", CMAKE_EXCLUDE_SOURCES) print("CMAKE_TARGET", CMAKE_TARGET) print("ASSUME_YES", ASSUME_YES) - print("WEAK_HANDLERS", WEAK_HANDLERS) + print("STRONG_HANDLERS", STRONG_HANDLERS) + print("NO_MY_MAIN", NO_MY_MAIN) def main2(ns: Namespace): if not ns.path.is_dir(): @@ -208,16 +223,20 @@ def main2(ns: Namespace): recurse_cmakelists(ns.path) root_cmakelists(ns.path) - if WEAK_HANDLERS: + if not STRONG_HANDLERS: weaken_handlers(ns.path) + if not NO_MY_MAIN: + call_my_main(ns.path) + def main(): parser = ArgumentParser(prog="MX Convert") parser.add_argument("-c", "--config", action="store", type=Path, default="mx_convert.toml", help="The config file to read. If not specified, defaults to 'mx_convert.toml'.") parser.add_argument("-d", "--discard", action="append", help="Add a file to the discard list (delete if found).") parser.add_argument("-H", "--header", action="append", help="Add a pattern to the headers list, single wildcards allowed. Includes *.h by default.") + parser.add_argument("-n", "--no-my-main", action="store_true", help="Disable added call to my_main inside the generated main function.") parser.add_argument("-s", "--source", action="append", help="Add a pattern to the sources list, single wildcards allowed. Includes *.c and *.s by default.") - parser.add_argument("-S", "--strong-handlers", action="store_false", help="Disable automatic adding of __weak__ attribute to handler functions.") + parser.add_argument("-S", "--strong-handlers", action="store_true", help="Disable automatic adding of __weak__ attribute to handler functions.") parser.add_argument("-t", "--target", action="store", default="cubemx", help="The CMake target to generate references to. Defaults to 'cubemx'.") parser.add_argument("-x", "--exclude", action="append", help="Add a file to the source exclusion list, single wildcards allowed.") parser.add_argument("-y", "--assume-yes", action="store_true", help="Assume yes on all queries, useful for scripts.") diff --git a/pyproject.toml b/pyproject.toml index aa8bbd9..2c04915 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "mx_convert" -version = "0.3.2" +version = "0.4.0" description = "A Cube MX projct conversion helper" authors = [{ name = "Didas72" }] dependencies = []