PeakRDL C-Header Generator

1.1.0 · active · verified Thu Apr 16

PeakRDL C-Header is a Python package used to generate a C Header file, typically representing a register abstraction layer, from a SystemRDL register model. It enables direct C-language access to hardware registers by generating C struct definitions that mirror the hardware address space. The library is currently active, with its latest version being 1.1.0, and maintains a regular release cadence, with updates addressing features and fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `peakrdl-cheader` via its Python API. It involves compiling a SystemRDL file using `systemrdl.RDLCompiler` to obtain an elaborated register model, then passing this model to `CHeaderExporter.export()` with desired configuration options to generate a C header file. This approach is useful for integrating the generator into custom build pipelines. Alternatively, for command-line usage, the `peakrdl` CLI tool provides a `c-header` subcommand.

import os
from systemrdl import RDLCompiler
from peakrdl_cheader.exporter import CHeaderExporter

# Create a dummy RDL file for demonstration
rdl_content = '''
  addrmap my_device {
    reg { field {} my_reg[31:0] = 0; } control_reg;
    regfile my_block[8] {
      reg { field {} status_field[7:0]; } status_reg;
    }
  };
'''
with open('example.rdl', 'w') as f:
    f.write(rdl_content)

# 1. Compile the SystemRDL file
rdlc = RDLCompiler()
rdlc.compile_file('example.rdl')
top_node = rdlc.elaborate()

# 2. Configure and generate the C header
exporter = CHeaderExporter(
    std="gnu11", # Specify C standard, e.g., gnu11, c99
    generate_bitfields=True, # Enable bitfield structs
    bitfield_order_ltoh=True # Specify bitfield packing order
)
output_path = 'example.h'
exporter.export(node=top_node, path=output_path)

print(f"C header generated successfully at {output_path}")

# Clean up dummy RDL file
os.remove('example.rdl')

# Example of how to use from CLI:
# peakrdl c-header example.rdl -o example.h --std gnu11 --bitfields ltoh

view raw JSON →