Version: 0.008
A modular Python application for managing OpenOCD connections and performing common embedded development operations on STM32 microcontrollers. Supports both interactive mode and automated scripting via configuration files.
.
├── main.py # Entry point and main application logic
├── openocd_manager.py # OpenOCD process and communication management
├── ui.py # User interface (menus, prompts, interactive loop)
├── colors.py # Color utilities for terminal output
├── config_parser.py # Configuration file parser
├── requirements.txt # Python dependencies
└── example_config.txt # Example configuration file
- Two operation modes:
- Interactive mode: Menu-driven interface for manual operations
- Automated mode: Configuration file support for scripted workflows
- Modular, maintainable architecture
- Automatic OpenOCD process management
- Telnet connection to OpenOCD
- Support for 15 STM32 MCU families
- Robust error handling:
- Automatic command retry with halt checking (up to 3 attempts)
- File existence validation before flash/verify operations
- Proper exception handling with clear error messages
- Automatic flash erase on failure in automated mode to prevent bricked devices
- Color-coded terminal output for better readability:
- Green for success messages
- Red for errors
- Yellow for warnings and prompts
- Cyan for information
- Blue for menu options
- Common operations:
- Halt/Reset MCU
- Erase flash memory
- Flash firmware
- Verify firmware
- Read/Write memory locations
- Send custom OpenOCD commands
- Python 3.6+
- OpenOCD installed and in PATH
- Python package:
colorama(for colored terminal output) - Target hardware connected via debug probe (ST-Link, J-Link, etc.)
-
Install OpenOCD:
# Ubuntu/Debian sudo apt-get install openocd # macOS brew install openocd # Windows # Download from https://openocd.org/
-
Clone or download all project files:
main.pyopenocd_manager.pyui.pycolors.pyconfig_parser.pyrequirements.txtexample_config.txt(optional example)
-
Install Python dependencies:
pip install -r requirements.txt
Or install manually:
pip install colorama
-
Ensure all files are in the same directory
-
Make the main script executable (Linux/macOS):
chmod +x main.py
The script is pre-configured for ST-Link debug probe and supports the following STM32 targets:
F Series:
- STM32F0, F1, F2, F3, F4, F7
G Series:
- STM32G0, G4
H Series:
- STM32H7
L Series:
- STM32L0, L1, L4, L5
Wireless Series:
- STM32WB, WL
No additional configuration files are needed. Simply select your target when prompted during script startup.
The application supports two modes of operation:
Run the script without arguments for interactive mode:
python3 main.pyOr:
./main.pyYou'll be prompted to select your STM32 target from 15 supported families:
Select STM32 target:
F Series:
1. STM32F0 (target/stm32f0x.cfg)
2. STM32F1 (target/stm32f1x.cfg)
...
(15 options total)
Enter your choice (1-15):
After selecting your target, you'll see an interactive menu with the following options:
- Halt MCU - Stop the microcontroller
- Reset and Halt MCU - Reset and immediately halt
- Reset and Run MCU - Reset and let it run
- Erase Flash - Erase the flash memory
- Flash Firmware - Program firmware to the device
- Verify Firmware - Verify programmed firmware
- Read Memory - Read from memory address
- Write Memory - Write to memory address
- Get Target Info - Display target information
- Send Custom Command - Send any OpenOCD command
- Reconnect to OpenOCD - Reconnect telnet session
- Exit - Clean up and exit
For scripted/automated operations, you can use a configuration file:
python3 main.py config.txtThe configuration file uses a simple text format with two types of directives:
target: stm32f4
Supported target values: stm32f0, stm32f1, stm32f2, stm32f3, stm32f4, stm32f7, stm32g0, stm32g4, stm32h7, stm32l0, stm32l1, stm32l4, stm32l5, stm32wb, stm32wl
Commands are executed sequentially in the order they appear:
command: halt
command: erase_flash
command: flash firmware.bin
command: verify firmware.bin
command: reset_run
Available commands:
halt- Halt the MCUreset_halt- Reset and halt the MCUreset_run- Reset and run the MCUerase_flash- Erase flash memoryflash <filepath> [address]- Flash firmware, optionally at a specific address- Example:
flash build/firmware.bin - Example:
flash build/firmware.bin 0x08004000(flash at bootloader offset)
- Example:
verify <filepath> [address]- Verify firmware, optionally at a specific address- Example:
verify build/firmware.bin - Example:
verify build/firmware.bin 0x08004000
- Example:
read_memory <address> [count]- Read memory (e.g.,read_memory 0x08000000 16)write_memory <address> <value>- Write memory (e.g.,write_memory 0x20000000 0x12345678)custom <command>- Send custom OpenOCD command (e.g.,custom targets)
Example Configuration File:
# Flash and verify firmware on STM32F4
target: stm32f4
# Prepare MCU
command: halt
command: erase_flash
# Program and verify
command: flash firmware.bin
command: verify firmware.bin
# Start execution
command: reset_run
Comments (lines starting with #) and blank lines are ignored. See example_config.txt for a complete example.
The script provides robust error handling for operations that require the MCU to be halted:
Pre-command Halt Check:
- Before executing flash operations, memory writes, or verification, the script checks if the MCU is halted
- If not halted, it displays:
MCU not halted, attempting to halt...and halts it automatically - This prevents common errors like
"Target not halted\nfailed erasing sectors 0 to 127"
Automatic Retry Logic:
- If a command fails, it automatically retries up to 3 times
- Displays:
Command failed, retrying (2/3)... - Before each retry, the script checks if the MCU is halted and halts it if needed
- Detects OpenOCD failure patterns like "failed", "error", "target not halted", "unable to", etc.
- This improves reliability when working with unstable connections or busy targets
Automated Mode Error Handling:
- When a command fails in automated mode (config file), the script:
- Skips all remaining commands in the sequence
- Performs a flash erase to ensure the device is in a clean state
- Displays "Task Failed" to clearly indicate the failure
- Exits with return code 1 for CI/CD integration
- This safety mechanism prevents partially-programmed devices that could fail to boot
File Validation:
- Flash and verify operations now include automatic file existence checking
- If a firmware file is not found, the operation immediately fails with a clear error message
- Proper exception handling (
FileNotFoundError) ensures errors are caught early before attempting communication with the device - Helps prevent wasted time on operations that cannot succeed due to missing files
- Start the script:
python3 main.py - Select target: Enter
1-15based on your STM32 family (e.g.,5for STM32F4,10for STM32L0) - Select option
4to erase flash - Select option
5to flash firmware - Enter firmware path:
build/firmware.bin - Select option
6to verify - Select option
3to reset and run
- Select option
7(Read Memory) - Enter address:
0x08000000(flash start on STM32) - Enter count:
16(read 16 words)
- Select option
8(Write Memory) - Enter address:
0x20000000(RAM address) - Enter value:
0x12345678
- Create a configuration file
flash_config.txt:
target: stm32l4
command: halt
command: erase_flash
command: flash build/firmware.bin
command: verify build/firmware.bin
command: reset_run
- Run with config file:
python3 main.py flash_config.txtThe script will automatically execute all commands and report the results.
For systems with bootloader and application firmware at different addresses:
target: stm32f4
# Flash bootloader at base address
command: halt
command: erase_flash
command: flash bootloader.bin 0x08000000
command: verify bootloader.bin 0x08000000
# Flash application at offset
command: flash application.bin 0x08004000
command: verify application.bin 0x08004000
command: reset_run
This allows you to program firmware at specific memory locations, useful for:
- Bootloader + application partitioning
- Multi-region firmware updates
- Factory programming scenarios
Use configuration files in your build pipeline for automated testing:
# Build firmware
make build
# Flash and verify using config file
python3 main.py deploy_config.txt
# Check exit code
if [ $? -eq 0 ]; then
echo "Deployment successful"
else
echo "Deployment failed"
exit 1
fi- Check that OpenOCD is installed:
openocd --version - Ensure your ST-Link debug probe is connected
- Verify you selected the correct target for your MCU
- Check that OpenOCD started successfully
- Verify the telnet port (default: 4444)
- Try connecting manually:
telnet localhost 4444
- Ensure the MCU is halted before flashing (the script will attempt this automatically)
- Check that the firmware file path is correct - the script will now report "Firmware file 'filename' not found" if the file doesn't exist
- Verify the firmware is compatible with your target
- Ensure the file path is correct relative to where you're running the script from
GPL-3.0