Skip to content

Instantly share code, notes, and snippets.

@michalmonday
Last active October 28, 2024 15:13
Show Gist options
  • Save michalmonday/457ec068a4240c31ede1a0ff83e92986 to your computer and use it in GitHub Desktop.
Save michalmonday/457ec068a4240c31ede1a0ff83e92986 to your computer and use it in GitHub Desktop.
Script that copies BSP and Utilities from ST repository to STM32F412G-Disovery project
'''
Usage: py -3 copy_bsp.py project_name
This script copies BSP files and Utilities to specified project.
This script should be placed in the workspace folder of STM32CubeIDE.
In the same directory where project folders are created.
REPOSITORY_PATH must be manually modified in this script in order to work.
Whenever "Project -> Generate Code" option is used, the Utilities folder
is deleted from the project, so a script like this one could be used to quickly restore it.
In 2023, this script was tested on Windows 10 with V1.27.1 sotfware for the STM32F412G-Discovery board.
In 2024, this script was tested on Windows 11 with V1.27.0 sotfware for the STM32F412G-Discovery board.
'''
import argparse
from pathlib import Path
import shutil
import os
import glob
def get_project_path():
parser = argparse.ArgumentParser(
description = (
'This program copies BSP files from STM32 repository to the specified project\n'
'(please modify path to STM32 repository before running it)'
),
usage = 'py -3 copy_bsp.py project_name'
)
parser.add_argument('project_name')
args = parser.parse_args()
return Path(__file__).parent / args.project_name
# 2023 version:
# REPOSITORY_PATH = Path("C:/ST/Repository/STM32Cube_FW_F4_V1.27.1")
# 2024 version missing "S" in the name on purpose because of the way the repository is named.
REPOSITORY_PATH = Path("C:/ST/Repository/TM32Cube_FW_F4_V1.27.0")
if not REPOSITORY_PATH.exists():
available_paths = glob.glob('C:/ST/Repository/*')
if not available_paths:
raise Exception('ERROR: No directories found in C:/ST/Repository.')
msg_str = f'WARNING: "{REPOSITORY_PATH}" was not found.\n\nInput ID of the available path:\n'
for i, path in enumerate(available_paths):
msg_str += f'{i+1}. {path}\n'
msg_str += '> '
chosen = input(msg_str)
path = available_paths[int(chosen)-1]
REPOSITORY_PATH = Path(path)
PROJECT_PATH = get_project_path()
if not PROJECT_PATH.exists():
raise Exception(f'ERROR: "{PROJECT_PATH}" was not found.')
# list of directories to copy
to_copy = [
'Drivers/BSP/STM32412G-Discovery',
'Drivers/BSP/Components',
'Utilities/Fonts'
]
# these files result in compilation errors for some reason
# more details: https://community.st.com/t5/stm32-mcus-products/how-to-setup-stm32f412g-discovery-bsp-for-the-display-in/m-p/592469#M223508
to_not_copy = [
'Drivers/BSP/STM32412G-Discovery/stm32412g_discovery_audio.h',
'Drivers/BSP/STM32412G-Discovery/stm32412g_discovery_audio.c'
]
for path in to_copy:
src_path = REPOSITORY_PATH / path
dst_path = PROJECT_PATH / path
shutil.copytree(src_path, dst_path, dirs_exist_ok=True)
# remove files that should not be copied (from the destination folder)
# the source files are not removed
for path in to_not_copy:
name = PROJECT_PATH / path
if os.path.isfile(name):
os.remove(name)
elif os.path.isdir(name):
os.rmdir(name)
else:
raise Exception(f'ERROR: "{name}" was not found.')
print('Done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment