ch57x-pac 0.1.3

Peripheral Access Crate (PAC) for WCH CH57x RISC-V BLE microcontrollers
Documentation
Excellent — let’s make this simple, *complete*, and perfectly reproducible.
Here’s the **definitive sequence** to go from your `ch57x.svd` file to a **working, formatted, and checked Peripheral Access Crate (PAC)** under **Windows PowerShell**.

---

## 🧱 STEP 0 — Install the tools


Open PowerShell and install everything you’ll need:

```powershell
rustup update
cargo install svd2rust form
```

If PowerShell can’t find `svd2rust` or `form`, add Cargo’s bin folder to your path for this session:

```powershell
$env:Path += ";$env:USERPROFILE\.cargo\bin"
```

Verify they work:

```powershell
svd2rust --version
form --version
```

---

## 🪣 STEP 1 — Create a new directory


Pick a working folder and make your PAC directory:

```powershell
cd C:\SynologyDrive\Personal_Development\ch572_pac
New-Item -ItemType Directory -Name ch57x-pac
cd ch57x-pac
```

Copy your `.svd` file in:

```powershell
Copy-Item "C:\path\to\ch57x.svd" .
```

You now have:

```
ch57x-pac/
└── ch57x.svd
```

---

## ⚙️ STEP 2 — Generate the Rust source


```powershell
svd2rust -i .\ch57x.svd
```

This produces a single large `lib.rs` file in the current directory.

---

## 🧩 STEP 3 — Split into a proper crate structure


Remove any leftover `src` directory from previous attempts:

```powershell
Remove-Item -Recurse -Force .\src -ErrorAction SilentlyContinue
```

Then split the generated file and clean up:

```powershell
form -i .\lib.rs -o .\src
Remove-Item .\lib.rs
```

Now you have:

```
src/
  lib.rs
  sys.rs
  uart0.rs
  ...
```

---

## 🧾 STEP 4 — Create the Cargo crate


Initialize the crate:

```powershell
cargo init --lib
```

Remove the dummy `src/lib.rs` that Cargo made:

```powershell
Remove-Item .\src\lib.rs
```

---

## 🧩 STEP 5 — Ensure the generated `src/lib.rs` exists


If `form` didn’t produce `src/lib.rs` (sometimes it happens), create one manually:

```powershell
New-Item -ItemType File -Path .\src\lib.rs
```

and put this inside:

```rust
#![no_std]


pub mod generic;
pub mod sys;
pub mod uart0;
pub mod uart1;
pub mod uart2;
pub mod uart3;
pub mod pwmx;
pub mod spi0;
pub mod tmr0;
pub mod tmr1;
pub mod tmr2;
pub mod tmr3;
pub mod usb;
pub mod systick;
pub mod pfic;
```

(Adjust `pub mod` entries to match your folder names under `src/`.)

---

## 📦 STEP 6 — Edit `Cargo.toml`


Open `Cargo.toml` and replace everything with this:

```toml
[package]
name = "ch57x-pac"
version = "0.1.0"
edition = "2021"
authors = ["Your Name <you@example.com>"]
license = "MIT OR Apache-2.0"
description = "Peripheral Access Crate for the WCH CH57x series microcontrollers"
repository = "https://github.com/yourname/ch57x-pac"

[dependencies]
vcell = "0.1.3"
cortex-m = { version = "0.7", optional = true }

[features]
default = ["rt"]
rt = []
```

---

## 🧠 STEP 7 — (Optional) Add the linker script


If your `svd2rust` output generated a `device.x`, include it next to `Cargo.toml`:

```powershell
New-Item -ItemType File -Name device.x
```

---

## 🎨 STEP 8 — Format and check everything


Now that Cargo sees a proper crate with a valid `lib.rs`, run:

```powershell
cargo fmt
cargo check
```

You may see warnings like:

```
can't set imports_granularity = Module, unstable features are only available in nightly channel.
```

That’s harmless — formatting still succeeds.

---

## 🧩 STEP 9 — Verify build


Finally, build to confirm everything works:

```powershell
cargo build
```

If successful, your crate is now a fully working Rust library:

```
ch57x-pac/
├── Cargo.toml
├── device.x
├── src/
│   ├── lib.rs
│   ├── sys.rs
│   ├── uart0.rs
│   ├── ...
└── ch57x.svd
```

---

## ✅ STEP 10 — Use it in your firmware


In another project’s `Cargo.toml`, add:

```toml
[dependencies]
ch57x-pac = { path = "../ch57x-pac" }
```

Then in your code:

```rust
use ch57x_pac::UART0;
```

---

That’s the complete, correct Windows workflow — **end-to-end from `.svd` to working, formatted PAC crate**.

Would you like me to include an *optional nightly-based* formatting step (using rustfmt’s `imports_granularity` and `group_imports`) to make it match upstream svd2rust’s formatting perfectly?