Skip to content

a5021/stm32codegen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

STM32 Bare-Metal Code Generator

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

Option Description
device Target STM32 device (e.g., stm32f103c8, stm32f407vg)
-m, --module Peripheral module to generate code for (tim, usart, adc, gpio, etc.)
-f, --function Function name for generated initialization code
-p, --peripheral Specific peripheral instances to include (e.g., TIM1, USART2)
-o, --output Output file name (default: stdout)
-h, --help Display help message

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 (F103, F105, F107)
  • STM32F2 series
  • STM32F3 series
  • STM32F4 series (F401, F407, F429, etc.)
  • STM32F7 series
  • STM32L0/L1/L4 series
  • STM32H7 series

Integration

Include generated code in your project

#include "stm32f1xx.h"  // CMSIS device header

// Generated initialization function
void init_tim(void);

int main(void) {
    // Initialize system clock first
    SystemInit();
    
    // Call generated peripheral initialization
    init_tim();
    
    // Your application code
    while (1) {
        // ...
    }
}

Makefile integration

# Generate peripheral initialization code during build
peripherals.c: stm32cgen.py
	python stm32cgen.py stm32f103c8 -m tim -f init_tim -p TIM > peripherals.c

Project Structure

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

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

This project is open source. Please check the repository for license details.

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published