Skip to content

Commit 9159ad7

Browse files
authored
Merge pull request #595 from skoe/update-build-and-readme
Add gen-all command to helper script and update README
2 parents 9385c08 + f02e2c8 commit 9159ad7

File tree

2 files changed

+98
-39
lines changed

2 files changed

+98
-39
lines changed

README.md

Lines changed: 90 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# stm32-data
22

3-
`stm32-data` is a project aiming to produce clean machine-readable data about the STM32 microcontroller
4-
families, including:
3+
`stm32-data` is a project that generates clean, machine-readable data for the STM32 microcontroller families. It also provides a tool to generate a Rust Peripheral Access Crate (`stm32-metapac`) for these devices.
4+
5+
The generated data includes:
56

67
- :heavy_check_mark: Base chip information
78
- RAM, flash
@@ -17,15 +18,56 @@ families, including:
1718

1819
:heavy_check_mark: = done, :construction: = work in progress, :x: = to do
1920

21+
## Generated JSON data
22+
2023
The generated JSON files are available [here in the `stm32-data-generated`](https://github.com/embassy-rs/stm32-data-generated/blob/main/data) repo.
2124

22-
## stm32-metapac
25+
## Generated PAC crate
2326

2427
If you're looking for the API docs for `stm32-metapac` customized to a
2528
particular chip, they are available here: https://docs.embassy.dev/stm32-metapac
2629

2730
The generated PAC is available [here in the `stm32-data-generated`](https://github.com/embassy-rs/stm32-data-generated/blob/main/stm32-metapac) repo.
2831

32+
## Quick guide
33+
34+
### How to generate everything
35+
36+
- Run `./d download-all`
37+
38+
> This will download all the required data sources into `sources/`.
39+
40+
- Run `./d gen-all`
41+
42+
> This (re)generates all the intermediate JSON's to `build/data/` and the `stm32-metapac` crate into `build/stm32-metapac/`.
43+
44+
The generated PAC crate can be included as a local dependency for testing or development:
45+
46+
```toml
47+
[dependencies]
48+
stm32-metapac = { path = "../../stm32-data/build/stm32-metapac" }
49+
```
50+
51+
### How to generate only the JSON data
52+
53+
The JSON files in `build/data/` are generated as follows:
54+
55+
- Run `cargo run --release --bin stm32-data-gen`
56+
57+
> This generates all the intermediate JSON's in `build/data/`.
58+
>
59+
> Assignments of registers to peripherals are done in a [perimap](#peripheral-mapping-perimap) and fixes to registers can be done in the files located in `data/registers`.
60+
61+
### How to generate only the `stm32-metapac` crate
62+
63+
When the JSON data was generated as described above, it can be used to generate the PAC:
64+
65+
- Run `cargo run --release --bin stm32-metapac-gen`
66+
67+
> This generates the `stm32-metapac` crate into `build/stm32-metapac/`.
68+
69+
## In-depth topics
70+
2971
## Data sources
3072

3173
These are the data sources currently used.
@@ -53,23 +95,44 @@ We don't maintain "patches" for registers unlike the `stm32-rs` project. This ha
5395
- Fixing mistakes and typos in the SVDs is now much easier (and yes, there are A LOT of those). It doesn't require a complicated patching system, you just edit the YAML to fix the mistake instead of writing a patch that will fix it when applied.
5496
- Ensuring consistency for the same peripherals between chips. (i.e. we check in a single `i2c_v2.yaml` and ensure it is good, vs trying to patch wildly differing SVDs for what's an identical IP block into being consistent). The `stm32-rs` project doesn't have this as a goal, while we do. Writing a single HAL for all stm32 chips (such as [the `embassy-stm32` HAL](https://github.com/embassy-rs/embassy/tree/main/embassy-stm32)) is impossible otherwise.
5597

56-
## Generate the `stm32-metapac` crate
57-
58-
- Run `./d download-all`
59-
60-
> This will download all the required data sources into `sources/`.
61-
62-
- Run `cargo run --release --bin stm32-data-gen`
63-
64-
> This generates all the intermediate JSON's in `build/data/`.
65-
>
66-
> > Assignments of registers to peripherals is done in a [perimap](#peripheral-mapping-perimap) and fixes to registers can be done in the files located in `data/registers`.
67-
68-
- Run `cargo run --release --bin stm32-metapac-gen`
69-
70-
> This generates the `stm32-metapac` crate into `build/stm32-metapac/`.
71-
72-
## Adding support for a new peripheral
98+
### Toolchain pipeline
99+
100+
This project is built in three stages:
101+
102+
1. **Source Acquisition**
103+
- `./d download-all` fetches STM32CubeDB XMLs, CubeProg data, mcufinder JSONs, and HAL headers into `sources/`.
104+
105+
2. **JSON Generation**
106+
- `stm32-data-gen` generates the JSON files from consolidated YAML and source data:
107+
1. Parse YAML files to build an in-memory IR for registers (`src/registers.rs`).
108+
- `data/extra/family/*.yaml`: STM32 family metadata (package options, flash/RAM sizes, low-level identifiers).
109+
- `data/header_map.yaml`: MCU slug to HAL C-header filename mapping for base addresses & IRQ extraction.
110+
- `data/registers/*.yaml`: Register-block definitions (offsets, fields, enums).
111+
- `data/dmamux/*.yaml`: DMAMUX profiles for families with a DMA multiplexer.
112+
- `src/perimap.rs`: Regex rules mapping each device-peripheral to the correct register-block YAML version.
113+
2. Parse HAL headers to extract `#define` macros (base addresses, IRQ numbers) (`src/header.rs`).
114+
3. Parse mcufinder docs to collect datasheet and reference manual links (`src/docs.rs`).
115+
4. Parse DMA XML to extract DMA channel configurations (`src/dma.rs`).
116+
5. Parse GPIO AF XML to extract alternate-function assignments (`src/gpio_af.rs`).
117+
6. Parse RCC registers for clock/reset settings (`src/rcc.rs`).
118+
7. Parse interrupts to map NVIC lines (`src/interrupts.rs`).
119+
8. Group all packages of a chip into `ChipGroup` structures (`src/chips.rs`).
120+
9. Use the parsed data to dump one JSON per MCU into `build/data/chips/*.json` (in `process_chip` of `src/chips.rs`).
121+
122+
3. **PAC Generation**
123+
- `stm32-metapac-gen` consumes the JSON files and generates the PAC crate:
124+
1. Load JSON from `build/data/chips` & `build/data/registers`.
125+
2. Construct IR (`chiptool::ir::IR`), apply transforms (`sort`, `sanitize`, `expand_extends`).
126+
3. Render Rust code via `chiptool::generate::render()`.
127+
4. Write outputs under `build/stm32-metapac/`, including:
128+
- `src/chips/<chip>/pac.rs`, `metadata.rs`, `device.x`
129+
- `src/peripherals/*.rs`
130+
- `src/registers/*.rs`
131+
- `Cargo.toml` and supporting files.
132+
133+
`chiptool` provides IR definitions and code-generation templates for PAC generation.
134+
135+
### Adding support for a new peripheral
73136

74137
This will help you add support for a new peripheral to all STM32 families. (Please take the time to add it for all families, even if you personally
75138
are only interested in one. It's easier than it looks, and doing all families at once is significantly less work than adding one now then having to revisit everything later when adding more. It also helps massively in catching mistakes and inconsistencies in the source SVDs.)
@@ -88,17 +151,17 @@ are only interested in one. It's easier than it looks, and doing all families at
88151
- Minimize the diff between each pair of versions. For example between `lpuart_v1.yaml` and `lpuart_v2.yaml`. If one is missing enums or descriptions, copy it from another.
89152
- Make sure the block
90153
- Add entries to [`perimap`](https://github.com/embassy-rs/stm32-data/blob/main/stm32-data-gen/src/perimap.rs), see below.
91-
- Regen, then:
154+
- Regen, then:
92155
- Check `data/chips/*.yaml` has the right `block: lpuart_vX/LPUART` fields.
93156
- Ensure a successful build of the affected pac. e.g.
94157
```
95158
cd build/stm32-metapac
96-
cargo build --features stm32u5a9nj --target thumbv8m.main-none-eabihf
97-
```
159+
cargo build --features stm32u5a9nj --target thumbv8m.main-none-eabihf
160+
```
98161
99162
Please separate manual changes and changes resulting from regen in separate commits. It helps tremendously with review and rebasing/merging.
100163
101-
## Register cleanup
164+
### Register cleanup
102165
103166
SVDs have some widespread annoyances that should be fixed when adding register YAMLs to this repo. Check out `chiptool` transforms, they can help in speeding up the cleanups.
104167
@@ -111,7 +174,7 @@ SVDs have some widespread annoyances that should be fixed when adding register Y
111174
- Check out the `MakeRegisterArray`, `MakeFieldArray` chiptool transforms.
112175
- Use `chiptool fmt` on each of the register yamls.
113176
114-
## Adding support for a new family (RCC)
177+
### Adding support for a new family (RCC)
115178
116179
NOTE: At the time of writing all families are supported, so this is only useful in particular situations, for example if you want to regenerate an RCC register yaml from scratch.
117180
@@ -145,16 +208,9 @@ place:
145208
mv regs_merged.yaml data/registers/rcc_g0.yaml
146209
```
147210
148-
To assign these newly generated registers to peripherals, utilize the mapping done in `parse.py`.
149-
An example mapping can be seen in the following snippet
150-
151-
```
152-
('STM32G0.*:RCC:.*', 'rcc_g0/RCC'),
153-
```
154-
155-
such mapping assigns the `rcc_g0/RCC` register block to the `RCC` peripheral in every device from the `STM32G0` family.
211+
To assign these newly generated registers to peripherals, utilize the mapping done in `stm32-data-gen/src/perimap.rs`.
156212
157-
## Peripheral mapping (perimap)
213+
### Peripheral mapping (perimap)
158214
159215
The `stm32-data-gen` binary has a map to match peripherals to the right version in all chips, the [perimap](https://github.com/embassy-rs/stm32-data/blob/main/stm32-data-gen/src/perimap.rs).
160216

d

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,20 @@ case "$CMD" in
4646
rm -rf build/data
4747
cargo run --release --bin stm32-data-gen
4848
;;
49+
gen-all)
50+
rm -rf build/{data,stm32-metapac}
51+
cargo run --release --bin stm32-data-gen
52+
cargo run --release --bin stm32-metapac-gen
53+
cd build/stm32-metapac
54+
find . -name '*.rs' -not -path '*target*' | xargs rustfmt --skip-children --unstable-features --edition 2021
55+
;;
4956
ci)
5057
[ -d sources ] || ./d download-all
5158
cd ./sources/
5259
git fetch origin $REV
5360
git checkout $REV
5461
cd ..
55-
rm -rf build/{data,stm32-metapac}
56-
cargo run --release --bin stm32-data-gen
57-
cargo run --release --bin stm32-metapac-gen
58-
cd build/stm32-metapac
59-
find . -name '*.rs' -not -path '*target*' | xargs rustfmt --skip-children --unstable-features --edition 2021
62+
./d gen-all
6063
;;
6164
check)
6265
cargo batch \

0 commit comments

Comments
 (0)