liveBook · Manning

Svd2rust

As explained in Embedded Software with Rust

svd2rust is a code-generation tool that converts a microcontroller SVD description into Rust peripheral-access source code.

svd2rust converts a microcontroller’s System View Description (SVD) into Rust source for a Peripheral Access Crate (PAC). The generated API names peripherals, registers, and fields, replacing hand-written pointers and offsets with strongly typed access. This helps scale memory-mapped peripheral access across a device. section 5.3.2

<register name="ODR" addressOffset="0x0C">
  <description>Output Data Register</description>
  <fields>
    <field name="ODR0" bitOffset="0" bitWidth="1" access="read-write"/>
    <!-- ... -->
  </fields>
</register>

An SVD register entry describes a register’s offset and fields; svd2rust uses such device descriptions as input. section 5.3.2

svd2rust -i STM32F103.svd

Run the generator on a vendor SVD file. Producing a complete PAC crate may require additional crate-structuring steps. section 5.3.2

Generated PACs expose peripherals through a Peripherals struct, whose take() method provides exclusive access. Register APIs offer read, write, and modify operations. For example, a GPIO BSRR write can set or reset pins without a read-modify-write cycle; modify is useful when other register fields must be preserved. section 5.3.3

Generation has practical limits and alternatives:

  • Output quality depends on the SVD, which may omit data or contain errors. section 5.3.2
  • Many projects use a published PAC maintained by an ecosystem group; some device families also have alternative data and generation pipelines. section 5.3.2

STM32F1 GPIO

A generated STM32F1 PAC can configure GPIO and write the BSRR register through typed methods such as bsrr().write(|w| w.br13().set_bit()). section 5.3.3

How Embedded Software with Rust covers it

How deep it goes
Defines SVD inputs and typed PAC output, then demonstrates register operations; leaves complete PAC crate structuring as additional work.
Examples
Uses an SVD register fragment, a generator command, and STM32F1 GPIO register operations.
What it assumes
Explains SVD contents as XML hardware descriptions and uses a vendor SVD and embedded Rust project for practical work.
What you can do after
Apply svd2rust to a vendor SVD to generate Rust source for a typed peripheral-access API; package the output separately.
How it connects
Connects System View Description files, Peripheral Access Crates, memory-mapped registers, and Rust register operations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.