Applicable Scenarios
- Need to develop BSP drivers for external devices (AT24C02, MPU6050, SSD1306, etc.).
- Want to find mature open-source driver libraries and adapt them to the project's BSP architecture.
- Already have open-source driver code and need to rename, organize, and inject HAL handles to comply with project specifications.
- The device is relatively simple, does not need an open-source library, and needs generated BSP skeleton files to get started quickly.
Required Inputs
- Target device name (e.g.,
AT24C02、MPU6050)。 - Communication bus type (I2C / SPI / UART / 1-Wire / GPIO).
- HAL handle name (e.g.,
hi2c1、hspi2), usually from CubeMX-generated code. - Optional: device I2C address, path to the downloaded open-source driver directory.
Automatic Detection
- Script
--scanmode automatically analyzes C/H files in the input directory, identifying function signatures, HAL call patterns, naming styles, and include dependencies. --list-devicesmode lists devices and recommended libraries already recorded indevice-adaptation.md.- If the target device is recorded in
device-adaptation.md, directly provide the recommended library and adaptation key points.
Execution Steps
- Confirm the target device and bus type. If the device is recorded in references/device-adaptation.md , directly refer to the recommended library and adaptation key points.
- Read references/search-and-evaluate.md, and search GitHub/Gitee for candidate open-source driver libraries according to the search strategy.
- Score the candidate libraries according to the evaluation checklist and select the most suitable library. If there is no suitable library, skip to step 6.
- Download the selected open-source library code to a local temporary directory.
- Run
--scanto analyze the open-source code and view the adaptation suggestion report:python3 scripts/bsp_adapter.py --scan ./downloaded_driver/ - Perform adaptation (choose one of two):
- Has an open-source library: Run
--adaptto adapt the code to the BSP specification:python3 scripts/bsp_adapter.py \ --adapt ./downloaded_driver/ \ --device <device_name> --handle <hal_handle> \ --output ./Hardware/bsp_<device>/ - No suitable library: Run
--scaffoldto generate BSP skeleton files:python3 scripts/bsp_adapter.py \ --scaffold --device <device_name> --bus <bus_type> \ --handle <hal_handle> --addr <i2c_addr> \ --output ./Hardware/bsp_<device>/
- Has an open-source library: Run
- Integrate the generated BSP files into the USER CODE area of
main.c, referring to the integration guide output by the script. - Compile and verify, then hand over to
build-*skill。
Driver architecture design and implementation best practices reference stm32-hal-development/references/peripheral-driver-guide.md。
Failure Triage
- When no usable open-source driver library can be found, use
--scaffoldto generate a skeleton and prompt the user to refer to the datasheet for manual implementation. - When the open-source library uses bare register operations or non-STM32 platform APIs, return
project-config-error, and recommend manually adapting the communication layer. - When the open-source library's license is GPL or there is no license, remind the user of license risks and recommend finding an alternative library or implementing it themselves.
- When compilation fails after adaptation, hand over to the
build-*skill to handle build errors.
Platform Notes
- The built-in script uses the Python standard library (
os、re、pathlib、argparse), with no additional dependencies. - The generated C code follows the STM32 HAL BSP specification and is compatible with GCC, IAR, and Keil toolchains.
- Path format follows platform-compatibility.md。
Output Conventions
--scanOutput adaptation recommendation report: detected functions, HAL calls, naming style, adaptation difficulty assessment.--adaptOutput the adapted BSP files andmain.cintegration guide.--scaffoldOutput skeleton files that conform to the BSP template specification.--list-devicesOutput the recommended libraries and adaptation difficulty for the recorded devices.- For detailed usage of all modes, see references/usage.md。
Handoff relationships
- Upstream:
stm32-hal-development(provides methodology and BSP templates). - Downstream:
build-cmake、build-iar、build-keil(compile verification).