2023-03-13 17:28:18 +00:00
|
|
|
# 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.
|
2023-03-21 17:17:33 +00:00
|
|
|
* split the single PAC file into one file per module.
|
2023-03-13 17:28:18 +00:00
|
|
|
|
|
|
|
## Prerequisites
|
|
|
|
* usage of cargo install
|
|
|
|
* generating docs
|
|
|
|
|
|
|
|
## Tasks
|
|
|
|
* Install `svd2rust` and `form` via cargo.
|
2023-03-14 17:15:53 +00:00
|
|
|
* Download the [nrf-svd][svd] file and place it into `down-the-stack/dk-pac`.
|
2023-03-21 17:21:41 +00:00
|
|
|
* Run `svd2rust` on the file using the `cortex-m` target.
|
2023-03-13 17:28:18 +00:00
|
|
|
* Split the file into its modules using `form`.
|
2023-03-21 17:43:42 +00:00
|
|
|
* Format the generated file to make it readable.
|
2023-03-13 17:28:18 +00:00
|
|
|
* Check the documentation.
|
|
|
|
|
|
|
|
## Step-by-Step Solution
|
|
|
|
|
|
|
|
|
|
|
|
✅ Install the necessary tools using the following commands:
|
|
|
|
|
|
|
|
```terminal
|
|
|
|
cargo install svd2rust
|
|
|
|
cargo install form
|
|
|
|
```
|
|
|
|
|
2023-03-14 17:15:53 +00:00
|
|
|
✅ Download [nrf-svd][svd](This version has an error: writeonce needs to be changed to writeOnce)
|
2023-03-13 17:28:18 +00:00
|
|
|
Place the file into `down-the-stack/dk-pac`. Note how we provide a `Cargo.toml` file, as it will not be generated by svd2rust.
|
|
|
|
|
2023-03-21 17:23:03 +00:00
|
|
|
✅ In the terminal, go to the file's location. Run `svd2rust` with the SVD file to generate a PAC using the `cortex-m` target.
|
2023-03-13 17:28:18 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
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
|
2023-03-21 17:23:21 +00:00
|
|
|
* device.x - linker script that weakly aliases all the interrupt handlers to the default exception handler (DefaultHandler).
|
2023-03-21 17:24:47 +00:00
|
|
|
* build.rs - build script that places device.x somewhere the linker can find.
|
2023-03-13 17:28:18 +00:00
|
|
|
|
2023-03-14 17:15:53 +00:00
|
|
|
✅ Make an `/src` and move the generated `lib.rs` into it.
|
|
|
|
|
2023-03-13 17:28:18 +00:00
|
|
|
✅ 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/
|
|
|
|
```
|
|
|
|
|
2023-03-14 17:15:53 +00:00
|
|
|
✅ Re-run `cargo fmt`.
|
2023-03-13 17:28:18 +00:00
|
|
|
|
2023-03-14 17:15:53 +00:00
|
|
|
[svd]: (https://github.com/NordicSemiconductor/nrfx/blob/master/mdk/nrf52.svd )
|