2025-09-04 11:04 PM
How to add a python script to introduce build errors during compilation process when some of the GPIO settings are modified from the below ones. PFB the python script
import sys
import re
# :white_heavy_check_mark: Define the locked GPIO settings here
LOCKED_SETTINGS = {
# Format: "PinName.Property": "ExpectedValue"
"PA0.GPIO_Label": "Button1",
"PA0.GPIO_Mode": "GPIO_MODE_INPUT",
"PA0.GPIO_PuPd": "GPIO_NOPULL",
"PB5.GPIO_Label": "LED1",
"PB5.GPIO_Mode": "GPIO_MODE_OUTPUT_PP",
"PB5.GPIO_PuPd": "GPIO_NOPULL",
"PB5.GPIO_Speed": "GPIO_SPEED_FREQ_LOW",
}
def check_ioc_file(ioc_path):
try:
with open(ioc_path, "r", encoding="utf-8") as f:
lines = f.readlines()
except FileNotFoundError:
print(f":cross_mark: ERROR: File not found: {ioc_path}")
sys.exit(1)
errors = []
for key, expected_value in LOCKED_SETTINGS.items():
found = False
for line in lines:
if line.startswith(key + "="):
found = True
actual_value = line.strip().split("=")[1]
if actual_value != expected_value:
errors.append(f"{key} changed! Expected '{expected_value}', found '{actual_value}'")
break
if not found:
errors.append(f"{key} missing from {ioc_path}")
if errors:
print(":cross_mark: GPIO lock check failed:")
for err in errors:
print(" - " + err)
sys.exit(1)
else:
print(":white_heavy_check_mark: GPIO lock check passed.")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python check_ioc_lock.py Temp-STM32H743VIHX.ioc")
sys.exit(1)
check_ioc_file(sys.argv[1])
2025-09-12 2:16 AM
Hello @GHM
STM32CubeIDE supports running Python scripts in the Pre-build and Post-build steps, provided that Python is installed and accessible in your system’s PATH. To enforce GPIO configuration checks, place your check_ioc_lock.py script and your .ioc file in the project root directory. Then, in the IDE, right-click your project, select Properties ==> C/C++ Build ==> Settings ==> Build Steps, and enter the appropriate command to execute your Python script (e.g., python check_ioc_lock.py Temp-STM32H743VIHX.ioc) in the Pre-build steps field. This setup ensures that your script runs automatically before each build.
THX
Ghofrane
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.