embedded-trainings-2020/embedded-workshop-book/src/generating-pac.md
Tanks Transfeld d84cc58db9
Update embedded-workshop-book/src/generating-pac.md
Co-authored-by: Jonathan Pallant <jonathan.pallant@ferrous-systems.com>
2023-03-21 18:21:41 +01:00

1.9 KiB

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.
  • Download the [nrf-svd][svd] file and place it into down-the-stack/dk-pac.
  • Run svd2rust on the file using the cortex-m target.
  • Format the generated file to make it readable.
  • Split the file into its modules using form.
  • Check the documentation.

Step-by-Step Solution

Install the necessary tools using the following commands:

cargo install svd2rust
cargo install form

Download [nrf-svd][svd](This version has an error: writeonce needs to be changed to writeOnce) 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.

In the terminal, go to the file's location. Run svd2rust with the SVD file to generate a PAC using the cortex-m flag.

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 sections(?)
  • build.rs - linker script

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:

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.

form -i src/lib.rs -o src/ 

Re-run cargo fmt.

[svd]: (https://github.com/NordicSemiconductor/nrfx/blob/master/mdk/nrf52.svd )