embedded-trainings-2020/embedded-workshop-book/src/generating-pac.md
2023-03-21 19:26:04 +01:00

79 lines
2.1 KiB
Markdown

# Generating the PAC
Generate your own PAC from an SVD file.
## In this exercise you will learn how to
* generate a PAC from an SVD file.
* format the generated code.
* split the single PAC file into one file per module.
## Prerequisites
* usage of cargo install
* generating docs
## Tasks
* Install `svd2rust` and `form` via cargo.
* Run `svd2rust` on `nrf52.svd` using the `cortex-m` target.
* Split the file into its modules using `form`.
* Format the generated file to make it readable.
* Check the documentation.
## Step-by-Step Solution
✅ Install the necessary tools using the following commands:
```terminal
cargo install svd2rust
cargo install form
```
✅ Go `down-the-stack/dk-pac`. The folder contains the SVD file `nrf52.svd`. We also provide a `Cargo.toml` file, as it will not be generated by svd2rust.
✅ In the terminal, go to the SVD file's location. Run `svd2rust` with the SVD file to generate a PAC using the `cortex-m` target.
```
svd2rust --target cortex-m -i nrf52.svd
```
If you check the folder `down-the-stack/dk-pac` now, you see three new files:
* lib.rs - the file that contains the generated code for the pac
* device.x - linker script that weakly aliases all the interrupt handlers to the default exception handler (DefaultHandler).
* build.rs - build script that places device.x somewhere the linker can find.
✅ Make an `/src` and move the generated `lib.rs` into it.
✅ Open the generated `lib.rs` with an editor.
Notice how it's barely correctly formatted.
✅ Look at the PAC docs with the following command:
```terminal
cargo doc --open
```
✅ Format the crate using `cargo fmt`.
This does not change to the docs, but `lib.rs` is a bit more readable.
✅ Use form to process the `lib.rs` to split it into modules, so that each module in in it's own file.
```terminal
form -i src/lib.rs -o src/
```
✅ Re-run `cargo fmt`.
## Troubleshooting
### `form`
In case the `form` command above causes problems, try this instead:
1. Move `lib.rs` out of `/src`.
2. Run the following command:
```terminal
form -i ./lib.rs -o ./src
```