Skip to content

Commit f7590a7

Browse files
committed
Merge branch 'main' into dual-bank_g4
2 parents 45b2044 + 9e090b9 commit f7590a7

File tree

15 files changed

+416
-869
lines changed

15 files changed

+416
-869
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 \

data/extra/family/STM32H7.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
pin_cleanup:
3+
# H7 has some _C pin variants (e.g. PC2 and PC2_C). Digital stuff should always be in the non-C pin.
4+
# cubedb puts it either in both, or in the -C pin only! (in chips where the package has only the -C pin)
5+
# so we fix that up here.
6+
strip_suffix: "_C"
7+
exclude_peripherals:
8+
- ADC
9+
- DAC
10+
- COMP

data/registers/adc_u5.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,8 @@ fieldset/CFGR2:
659659
description: SMPTRIG.
660660
bit_offset: 15
661661
bit_size: 1
662-
- name: OSVR
663-
description: OSVR.
662+
- name: OVSR
663+
description: OVSR.
664664
bit_offset: 16
665665
bit_size: 10
666666
- name: LFTRIG

data/registers/adc_v4.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ fieldset/CFGR2:
272272
description: Right-shift data after Offset 4 correction
273273
bit_offset: 14
274274
bit_size: 1
275-
- name: OSVR
275+
- name: OVSR
276276
description: Oversampling ratio
277277
bit_offset: 16
278278
bit_size: 10

data/registers/opamp_f3.yaml

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
block/OPAMP:
2-
description: Operational Amplifier
2+
description: Operational amplifier
33
items:
44
- name: CSR
5-
description: OPAMP control/status register
5+
description: Control/status register
66
byte_offset: 0
77
fieldset: CSR
88
fieldset/CSR:
9-
description: OPAMP control/status register
9+
description: Control/status register
1010
fields:
1111
- name: OPAMPEN
12-
description: OPAMP enable
12+
description: Enable
1313
bit_offset: 0
1414
bit_size: 1
1515
- name: FORCE_VP
16-
description: Forces a calibration reference voltage on non-inverting input and disables external connections.
16+
description: Force internal reference on VP (reserved for test)
1717
bit_offset: 1
1818
bit_size: 1
19-
enum: FORCE_VP
2019
- name: VP_SEL
21-
description: OPAMP Non inverting input selection
20+
description: Non-inverting input selection
2221
bit_offset: 2
2322
bit_size: 2
2423
enum: VP_SEL
2524
- name: VM_SEL
26-
description: OPAMP inverting input selection
25+
description: Inverting input selection
2726
bit_offset: 5
2827
bit_size: 2
2928
enum: VM_SEL
@@ -32,12 +31,12 @@ fieldset/CSR:
3231
bit_offset: 7
3332
bit_size: 1
3433
- name: VMS_SEL
35-
description: OPAMP inverting input secondary selection
34+
description: Inverting input secondary selection
3635
bit_offset: 8
3736
bit_size: 1
3837
enum: VMS_SEL
3938
- name: VPS_SEL
40-
description: OPAMP Non inverting input secondary selection
39+
description: Non-inverting input secondary selection
4140
bit_offset: 9
4241
bit_size: 2
4342
enum: VPS_SEL
@@ -55,7 +54,7 @@ fieldset/CSR:
5554
bit_offset: 14
5655
bit_size: 4
5756
enum: PGA_GAIN
58-
- name: USER_TRIM
57+
- name: USERTRIM
5958
description: User trimming enable
6059
bit_offset: 18
6160
bit_size: 1
@@ -71,11 +70,10 @@ fieldset/CSR:
7170
description: Output the internal reference voltage
7271
bit_offset: 29
7372
bit_size: 1
74-
- name: OUTCAL
75-
description: OPAMP ouput status flag
73+
- name: CALOUT
74+
description: Calibration output
7675
bit_offset: 30
7776
bit_size: 1
78-
enum: OUTCAL
7977
- name: LOCK
8078
description: OPAMP lock
8179
bit_offset: 31
@@ -84,35 +82,17 @@ enum/CALSEL:
8482
bit_size: 2
8583
variants:
8684
- name: Percent3_3
87-
description: VREFOPAMP=3.3% VDDA
85+
description: VREFOPAMP = 3.3% VDDA
8886
value: 0
8987
- name: Percent10
90-
description: VREFOPAMP=10% VDDA
88+
description: VREFOPAMP = 10% VDDA
9189
value: 1
9290
- name: Percent50
93-
description: VREFOPAMP=50% VDDA
91+
description: VREFOPAMP = 50% VDDA
9492
value: 2
9593
- name: Percent90
96-
description: VREFOPAMP=90% VDDA
94+
description: VREFOPAMP = 90% VDDA
9795
value: 3
98-
enum/FORCE_VP:
99-
bit_size: 1
100-
variants:
101-
- name: Normal
102-
description: Normal operating mode
103-
value: 0
104-
- name: Calibration
105-
description: Calibration mode. Non-inverting input connected to calibration reference
106-
value: 1
107-
enum/OUTCAL:
108-
bit_size: 1
109-
variants:
110-
- name: Low
111-
description: Non-inverting < inverting
112-
value: 0
113-
- name: High
114-
description: Non-inverting > inverting
115-
value: 1
11696
enum/PGA_GAIN:
11797
bit_size: 4
11898
variants:
@@ -156,10 +136,10 @@ enum/VMS_SEL:
156136
bit_size: 1
157137
variants:
158138
- name: PC5
159-
description: PC5 (VM0) used as OPAMP2 inverting input when TCM_EN=1
139+
description: PC5 (VM0) used as OPAMP2 inverting input when TCM_EN = 1
160140
value: 0
161141
- name: PA5
162-
description: PA5 (VM1) used as OPAMP2 inverting input when TCM_EN=1
142+
description: PA5 (VM1) used as OPAMP2 inverting input when TCM_EN = 1
163143
value: 1
164144
enum/VM_SEL:
165145
bit_size: 2

0 commit comments

Comments
 (0)