Skip to content

a5021/stm32codegen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

279 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

STM32 Bare-Metal Code Generator

Build Python License

CMSIS-based initialization code generator for STM32 microcontrollers

stm32cgen.py automates the generation of bare-metal peripheral initialization code for STM32 microcontrollers directly from CMSIS header files. Eliminate manual register configuration and reduce initialization errors with auto-generated, CMSIS-compliant C code for timers, UARTs, ADCs, GPIO, and other peripherals.

Features

  • CMSIS-native code generation – generates code using CMSIS register definitions without HAL overhead
  • Multi-peripheral support – timers, UARTs, ADCs, GPIO, and more
  • Flexible output modes – generate code blocks, functions, or complete header files
  • Microcontroller-aware – extracts peripheral configurations directly from device-specific CMSIS headers
  • Bare-metal optimization – produces minimal, hardware-level initialization code

Quick Start

Prerequisites

  • Python 3.6 or higher
  • STM32 CMSIS device headers for your target microcontroller

Installation

git clone https://github.com/a5021/stm32codegen.git
cd stm32codegen

Basic Usage

Generate timer initialization code for STM32F103C8:

python stm32cgen.py stm32f103c8 -m tim -f init_tim -p TIM

This command generates an init_tim() function that initializes all TIM peripherals on the STM32F103C8 microcontroller

Usage Examples

Generate UART initialization code

python stm32cgen.py stm32f103c8 -m usart -f init_usart -p USART

Generate ADC configuration header

python stm32cgen.py stm32f103c8 -m adc -o adc_config.h -p ADC

Generate GPIO setup for specific ports

python stm32cgen.py stm32f103c8 -m gpio -f gpio_init -p GPIOA,GPIOB

Command-Line Options

See the full reference: docs/cli-reference.md

How It Works

The generator parses CMSIS device header files to extract peripheral register structures and base addresses. It then produces initialization code using direct register access patterns optimized for bare-metal embedded systems. The generated code follows CMSIS naming conventions and register access semantics.

Supported Microcontrollers

The tool supports any STM32 family with CMSIS headers, including:

  • STM32F0 series
  • STM32F1 series
  • STM32F2 series
  • STM32F3 series
  • STM32F4 series
  • STM32F7 series
  • STM32G0 series
  • STM32G4 series
  • STM32H7 series
  • STM32L0 series
  • STM32L1 series
  • STM32L4 series

Project Structure

stm32codegen/
├── stm32cmsis.py         # CMSIS module
├── stm32cgen.py          # Main code generator script
├── README.md             # This file
└── EXAMPLES/             # Usage examples

Examples

EXAMPLES/ contains shell scripts that generate complete demo projects. To set up an example:

cd EXAMPLES
bash <script_name>.sh

The script uses stm32cgen.py to generate initialization code, downloads CMSIS headers from the STM32 and ARM repositories, creates a Makefile, and builds the project. The only prerequisites are Python 3 and arm-none-eabi-gcc.

After a successful build, flash the target with:

make program    # ST-LINK
make jprogram   # J-Link

The scripts are safe to re-run: existing source files are not overwritten (only generated headers are recreated).

Contributing

Contributions are welcome. When submitting pull requests:

  • Follow Python PEP 8 style guidelines
  • Add examples for new features
  • Update documentation to reflect changes
  • Test with multiple STM32 device families

License

MIT License

Related Projects

Support

For issues, feature requests, or questions, please open an issue on the GitHub repository.


Note: This tool generates initialization templates. Always review and customize the generated code according to your specific application requirements and hardware configuration.

About

CMSIS-based bare-metal initialization code generator for STM32

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors