svd2c is a small command-line tool to generate C header files from CMSIS-SVD files.
It produces register structs, base address macros, bitfield definitions, and optional enum aliases.
- Converts CMSIS-SVD to C-style
typedef structheaders - Supports both per-peripheral and single-header output
- Optional macros for bitfields and enumerated values
git clone https://github.com/hana-day/svd2c.git
cd svd2c
pip install -e .Requires: Python ≥ 3.8 and
cmsis-svd
# Split header for each peripheral
svd2c path/to/device.svd -o include/
# Single combined header
svd2c path/to/device.svd --style single -o include/
# Disable bitfield macros
svd2c path/to/device.svd --no-bf -o include/| Option | Description |
|---|---|
--style |
Output style: peripheral (default) or single |
--no-bf |
Omit bitfield and enum macros |
-o / --out |
Output directory (default: include) |
#ifndef UART_H_
#define UART_H_
#include <stdint.h>
#define UART_BASE 0x40034000u
typedef struct {
volatile uint32_t DATA;
volatile uint32_t STATUS;
} UART_Type;
#define UART ((UART_Type*)UART_BASE)
// Bitfield macros
#define UART_STATUS_TX_READY (1u << 0)
#define UART_STATUS_RX_READY (1u << 1)
#endif /* UART_H_ */When bitfield macros are enabled (default), the following generic macros are available via _bf_helpers.h:
#define BF_PREP(val, field) (((val) << field##_LSB) & field##_MASK)
#define BF_GET(x, field) (((x) & field##_MASK) >> field##_LSB)
#define BF_SET(x, field, v) do { (x) = ((x) & ~(field##_MASK)) | BF_PREP((v), field); } while (0)
#define BF_CLEAR(x, field) do { (x) &= ~(field##_MASK); } while (0)// Suppose the following macros are generated:
#define TIMER_CTRL_MODE_LSB 2
#define TIMER_CTRL_MODE_WIDTH 2
#define TIMER_CTRL_MODE_MASK (0xCu)
#define TIMER_CTRL_BASE 0x40010000u
typedef struct {
volatile uint32_t CTRL;
} TIMER_Type;
#define TIMER ((TIMER_Type*)TIMER_CTRL_BASE)
// Set MODE field of the CTRL register to 3
BF_SET(TIMER->CTRL, TIMER_CTRL_MODE, 3);
// Read MODE field
uint32_t mode = BF_GET(TIMER->CTRL, TIMER_CTRL_MODE);
// Clear MODE field
BF_CLEAR(TIMER->CTRL, TIMER_CTRL_MODE);
TIMER->CTRLis a memory-mapped hardware register.
MIT License