diff --git a/.vscode/launch.json b/.vscode/launch.json index b341d40a..dea90b45 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,48 +1,87 @@ { "version": "0.2.0", "configurations": [ - { - "type": "cortex-debug", - "request": "launch", - "name": "Build and Debug Project", - "servertype": "stlink", - "cwd": "${workspaceRoot}", - "runToEntryPoint": "main", - "showDevDebugOutput": "raw", - "executable": "out/build/latest.elf", - "device": "STM32H723ZG", - "configFiles": [".vscode/stlink.cfg", ".vscode/stm32h7x.cfg"], - "svdFile": ".vscode/STM32H723.svd", - "swoConfig": { - "enabled": true, - "cpuFrequency": 8000000, - "swoFrequency": 2000000, - "source": "probe", - "decoders": [{ "type": "console", "label": "ITM", "port": 0 }] - }, - "preLaunchTask": "CMake: build" - }, - { - "type": "cortex-debug", - "request": "launch", - "name": "Debug Project", - "servertype": "stlink", - "cwd": "${workspaceRoot}", - "runToEntryPoint": "main", - "showDevDebugOutput": "raw", - "executable": "out/build/latest.elf", - "device": "STM32H723ZG", - "configFiles": [".vscode/stlink.cfg", ".vscode/stm32h7x.cfg"], - "svdFile": "./STM32H723.svd", - "swoConfig": { - "enabled": true, - "cpuFrequency": 8000000, - "swoFrequency": 2000000, - "source": "probe", - "decoders": [{ "type": "console", "label": "ITM", "port": 0 }] - }, - "stm32cubeprogrammer": "/opt/st/stm32cubeclt_1.16.0/STM32CubeProgrammer/bin" - }, + { + "name": "Build and Debug STM32H723 (OpenOCD + RTT)", + "type": "cortex-debug", + "request": "launch", + "cwd": "${workspaceFolder}", + "executable": "${workspaceFolder}/out/build/latest.elf", + "servertype": "openocd", + "configFiles": [ + "interface/stlink.cfg", + "target/stm32h7x.cfg" + ], + "serverpath": "openocd", + "device": "STM32H723ZGTx", + "interface": "swd", + "runToEntryPoint": "main", + "svdFile":"./STM32H723.svd", + "showDevDebugOutput": "parsed", + "postLaunchCommands": [ + "monitor rtt setup 0x24000000 0x24080000 \"SEGGER RTT\"", + "monitor rtt start", + "monitor rtt server start 9090 0" + ], + "postRestartCommands": [ + "monitor halt", + "monitor rtt setup 0x24000000 0x24080000 \"SEGGER RTT\"", + "monitor rtt start", + "monitor rtt server start 9090 0" + ], + "rttConfig": { + "enabled": true, + "address": "auto", + "decoders": [ + { + "port": 0, + "type": "console" + } + ] + }, + "preLaunchTask": "CMake: build then pre-flash check" + + }, + { + "name": "Debug STM32H723 (OpenOCD + RTT) - No Build", + "type": "cortex-debug", + "request": "launch", + "cwd": "${workspaceFolder}", + "executable": "${workspaceFolder}/out/build/latest.elf", + "servertype": "openocd", + "configFiles": [ + "interface/stlink.cfg", + "target/stm32h7x.cfg" + ], + "serverpath": "openocd", + "device": "STM32H723ZGTx", + "interface": "swd", + "runToEntryPoint": "main", + "svdFile":"./STM32H723.svd", + "showDevDebugOutput": "parsed", + "postLaunchCommands": [ + "monitor rtt setup 0x24000000 0x24080000 \"SEGGER RTT\"", + "monitor rtt start", + "monitor rtt server start 9090 0" + ], + "postRestartCommands": [ + "monitor halt", + "monitor rtt setup 0x24000000 0x24080000 \"SEGGER RTT\"", + "monitor rtt start", + "monitor rtt server start 9090 0" + ], + "rttConfig": { + "enabled": true, + "address": "auto", + "decoders": [ + { + "port": 0, + "type": "console" + } + ] + }, + "preLaunchTask": "Pre-flash check" + }, { "name": "Debug simulator on Rosetta", "type": "cppdbg", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index fe78d81e..3c58499f 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,6 +7,25 @@ "command": "ROSETTA_DEBUGSERVER_PORT=1234 ./out/build/latest.elf", "problemMatcher": [], "isBackground": true + }, + { + "label": "Pre-flash check", + "type": "shell", + "command": "${workspaceFolder}/virtual/Scripts/python", + "args": [ + "${workspaceFolder}/tools/preflash_check.py" + ], + "problemMatcher": [], + "presentation": { + "reveal": "always", + "panel": "shared" + } + }, + { + "label": "CMake: build then pre-flash check", + "dependsOn": ["CMake: build", "Pre-flash check"], + "dependsOrder": "sequence", + "problemMatcher": [] } ] - } \ No newline at end of file + } diff --git a/CMakeLists.txt b/CMakeLists.txt index 44b7ec84..d07ece62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,8 +5,11 @@ project(template-project LANGUAGES ASM C CXX) set(EXECUTABLE ${PROJECT_NAME}.elf) set(STLIB_DIR ${CMAKE_CURRENT_LIST_DIR}/deps/ST-LIB) set(LD_SCRIPT ${CMAKE_SOURCE_DIR}/STM32H723ZGTX_FLASH.ld) -set(VENV_PYTHON ${CMAKE_SOURCE_DIR}/virtual/bin/python) - +if(CMAKE_HOST_WIN32 ) + set(VENV_PYTHON ${CMAKE_SOURCE_DIR}/virtual/Scripts/python) +else() + set(VENV_PYTHON ${CMAKE_SOURCE_DIR}/virtual/bin/python) +endif() option(USE_ETHERNET "Enable ethernet peripheral" OFF) option(TARGET_NUCLEO "Targets the STM32H723 Nucleo dev board" OFF) @@ -93,22 +96,36 @@ add_custom_target(generate_binary_metadata ALL WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMENT "Generating binary metadata" ) -add_dependencies(${EXECUTABLE} generate_binary_metadata) +add_dependencies(${EXECUTABLE} generate_binary_metadata) if (PROJECT_IS_TOP_LEVEL) execute_process( COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_BINARY_DIR}/compile_commands.json ${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json ) +endif() +# Post-build: Copy binary to out/build/latest.elf and create marker for BOARD builds +add_custom_command(TARGET ${EXECUTABLE} POST_BUILD + # Create output directory + COMMAND ${CMAKE_COMMAND} -E make_directory + ${CMAKE_SOURCE_DIR}/out/build + # Copy the built binary to out/build/latest.elf + COMMAND ${CMAKE_COMMAND} -E copy + $ + ${CMAKE_SOURCE_DIR}/out/build/latest.elf + COMMENT "Copying ${EXECUTABLE} to out/build/latest.elf" +) - add_custom_command( - TARGET ${EXECUTABLE} - POST_BUILD - COMMAND ${CMAKE_COMMAND} -E make_directory - ${CMAKE_CURRENT_SOURCE_DIR}/out/build - COMMAND ${CMAKE_COMMAND} -E create_symlink - $ - ${CMAKE_CURRENT_SOURCE_DIR}/out/build/latest.elf +# Create or remove marker file for BOARD builds (used by preflash_check.py) +if(NOT TARGET_NUCLEO) + add_custom_command(TARGET ${EXECUTABLE} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_SOURCE_DIR}/out/build/board_build_marker + COMMENT "Creating BOARD build marker for pre-flash check" + ) +else() + add_custom_command(TARGET ${EXECUTABLE} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E remove -f ${CMAKE_SOURCE_DIR}/out/build/board_build_marker + COMMENT "Removing BOARD build marker (NUCLEO build)" ) -endif() \ No newline at end of file +endif() diff --git a/tools/init.bat b/tools/init.bat new file mode 100644 index 00000000..a7edc385 --- /dev/null +++ b/tools/init.bat @@ -0,0 +1,27 @@ +@echo off +setlocal enabledelayedexpansion +# This script is supposed to be under ${REPO_PATH}/tools/init.bat + +REM Get the script directory and repo directory +set "SCRIPT_DIR=%~dp0" +set "REPO_DIR=%SCRIPT_DIR%.." + +echo Script directory: %SCRIPT_DIR% +echo Repository directory: %REPO_DIR% + +REM Change to repo directory +cd /d "%REPO_DIR%" + +REM Create virtual environment +python -m venv virtual + +REM Activate virtual environment +call virtual\Scripts\activate.bat + +REM Install requirements +pip install -r requirements.txt + +REM Update git submodules +git submodule update --init + +echo Setup complete! diff --git a/tools/init.sh b/tools/init.sh new file mode 100644 index 00000000..872b6705 --- /dev/null +++ b/tools/init.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -euo pipefail + +# This script is supposed to be under ${REPO_PATH}/tools/init.sh + +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" +REPO_DIR="$(cd -- "${SCRIPT_DIR}/.." && pwd)" + +echo "Script directory: $SCRIPT_DIR" +echo "Repository directory: $REPO_DIR" + +cd "$REPO_DIR" +python3 -m venv virtual +source ./virtual/bin/activate +pip install -r requirements.txt + +git submodule update --init diff --git a/tools/preflash_check.py b/tools/preflash_check.py new file mode 100644 index 00000000..0a4d4639 --- /dev/null +++ b/tools/preflash_check.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +""" +Pre-flash check script. +Checks for uncommitted changes and commits them if the binary was compiled with BOARD symbol. +""" + +import subprocess +import sys +import os +from pathlib import Path + +def get_script_dir() -> Path: + return Path(__file__).parent.resolve() + +def get_workspace_dir() -> Path: + return get_script_dir().parent + +def check_board_symbol(build_dir: Path) -> bool: + """Check if the binary was compiled with BOARD symbol.""" + marker_file = build_dir / "board_build_marker" + return marker_file.exists() + +def has_uncommitted_changes(workspace_dir: Path) -> bool: + """Check if there are uncommitted changes in the repository.""" + result = subprocess.run( + ["git", "status", "--porcelain"], + cwd=workspace_dir, + capture_output=True, + text=True + ) + return bool(result.stdout.strip()) + +def main(): + workspace_dir = get_workspace_dir() + + # Default build directory - can be overridden by command line argument + if len(sys.argv) > 1: + build_dir = Path(sys.argv[1]) + else: + build_dir = workspace_dir / "out" / "build" + + print(f"Workspace directory: {workspace_dir}") + print(f"Build directory: {build_dir}") + + # Check if this is a BOARD build + if not check_board_symbol(build_dir): + print("Binary was not compiled with BOARD symbol (or marker not found). Skipping pre-flash check.") + sys.exit(0) + + print("Binary was compiled with BOARD symbol. Checking for uncommitted changes...") + + if has_uncommitted_changes(workspace_dir): + print("Uncommitted changes detected. Aborting Flash...") + print("Please before flashing a board make sure all changes are commited"); + sys.exit(1) + else: + print("No uncommitted changes. Proceeding with flash.") + + sys.exit(0) + +if __name__ == "__main__": + main()