Skip to content

Commit 8d1b469

Browse files
Add ESP32-C2 support (#4)
* feat: Initial support for ESP32-C2 * docs: Update readme * docs: Update readme * feat: Add esp32c2 yaml * feat: Add load address * fix: Fix names * docs: Update instructions * docs: Update supported chips * Update output/esp32c2.yaml Co-authored-by: Jesse Braham <jessebraham@users.noreply.github.com> * docs: Add docstrings * docs: Update instructions --------- Co-authored-by: Jesse Braham <jessebraham@users.noreply.github.com>
1 parent b7e7e96 commit 8d1b469

File tree

6 files changed

+2455
-22
lines changed

6 files changed

+2455
-22
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ufmt = { version = "0.1.0", optional = true }
1313
log = ["ufmt"]
1414

1515
# targets
16+
esp32c2 = []
1617
esp32c3 = []
1718
esp32c6 = []
1819
esp32h2 = []
@@ -24,4 +25,4 @@ debug-assertions = false
2425
incremental = false
2526
lto = "fat"
2627
opt-level = 'z'
27-
overflow-checks = false
28+
overflow-checks = false

README.md

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,29 @@ To build the flash loader:
66

77
```bash
88
$ cargo build --release --feature $(CHIP_NAME) --target $(RUST_TARGET) # builds the flashing stub
9-
$ target-gen elf target/$(RUST_TARGET)/release/esp-flashloader output/esp32.yaml --update --name $(CHIP_NAME)-flashloader
9+
$ target-gen elf target/$(RUST_TARGET)/release/esp-flashloader output/$(CHIP_NAME).yaml --update --name $(CHIP_NAME)-flashloader
1010
```
1111

1212
Example for the updating the `esp32c3` flash algorithm.
1313

1414
```bash
1515
$ cargo build --release --features esp32c3 --target riscv32imc-unknown-none-elf
16-
$ target-gen elf target/riscv32imc-unknown-none-elf/release/esp-flashloader output/esp32.yaml --update --name esp32c3-flashloader
16+
$ target-gen elf target/riscv32imc-unknown-none-elf/release/esp-flashloader output/esp32c3.yaml --update --name esp32c3-flashloader
1717
```
1818

1919
## Chip support
2020

21-
|name |supported|
22-
|-------|---------|
23-
|esp32c3| Y |
24-
|esp32c6| Y |
21+
| name | supported |
22+
| ------- | --------- |
23+
| esp32c2 | Y |
24+
| esp32c3 | Y |
25+
| esp32c6 | Y |
26+
| esp32h2 | Y |
2527

2628
## Adding new chips
2729

2830
1. Add a feature for the chip inside `Cargo.toml`
29-
2. Add the ROM API linker script inside the `ld` directory.
31+
2. Add the [ROM API linker script](https://github.com/search?q=repo%3Aespressif%2Fesp-idf++path%3A*rom.api.ld&type=code) inside the `ld` directory.
3032
3. Inside the ROM API linker script, add a memory section detailing where the program will be loaded.
3133
```c
3234
MEMORY {
@@ -35,16 +37,28 @@ $ target-gen elf target/riscv32imc-unknown-none-elf/release/esp-flashloader outp
3537
}
3638
```
3739
It's important to note that the algorithm cannot be loaded at the start of RAM, because probe-rs has a header it loads prior to the algo hence the 64K offset.
38-
4. Add the following snippet to the bottom of the `main()` function inside `build.rs`, adapting it for the new chip name.
40+
IRAM origin and length can be obtained from esp-hal. Eg: [ESP32-C3 memory map](https://github.com/esp-rs/esp-hal/blob/ff80b69183739d04d1cb154b8232be01c0b26fd9/esp32c3-hal/ld/db-esp32c3-memory.x#L5-L22)
41+
4. Add the following snippet to the `main()` function inside `build.rs`, adapting it for the new chip name.
3942
```rust
4043
#[cfg(feature = "esp32c3")]
41-
{
42-
fs::copy("ld/esp32c3.x", out_dir.join("esp32c3.x")).unwrap();
43-
println!("cargo:rerun-if-changed=ld/esp32c3.x");
44-
println!("cargo:rustc-link-arg=-Tld/esp32c3.x");
45-
}
44+
let chip = "esp32c3";
4645
```
47-
5. Follow the instructions above for building
48-
6. Use `target-gen` _without_ the `update` flag to generate a new yaml algorithm.
49-
7. merge the new flash algorithm into the the main `esp32.yaml`
50-
8. Upstream the new updates to probe-rs.
46+
5. [Define `spiconfig` for your the target in `main.rs`](https://github.com/search?q=repo%3Aespressif%2Fesp-idf+ets_efuse_get_spiconfig+path%3A*c3*&type=code)
47+
6. Follow the instructions above for building
48+
- It may fail with: `rust-lld: error: undefined symbol: <symbol>`
49+
- In this case, you need to add the missing method in the ROM API linker script.
50+
- Eg. ESP32-C2 is missing `esp_rom_spiflash_attach`:
51+
1. [Search the symbol in esp-idf](https://github.com/search?q=repo%3Aespressif%2Fesp-idf+esp_rom_spiflash_attach+path%3A*c2*&type=code)
52+
2. Add it to the ROM API linker script: `PROVIDE(esp_rom_spiflash_attach = spi_flash_attach);`
53+
7. Use `target-gen` _without_ the `update` flag to generate a new yaml algorithm.
54+
8. Update the resulting yaml file
55+
1. Update `name`
56+
2. Update variants `name`, `type`, `core_access_options` and `memory_map`
57+
- The first `!Nvm` block represents the raw flash starting at 0 and up to the maximum supported external flash (check TRM for this, usually in "System and Memory/Features")
58+
- Next `!Ram` block corresponds to instruction bus for internal SRAM, see Internal Memory Address Mapping of TRM
59+
- Next `!Ram` block corresponds to data bus for internal SRAM, see Internal Memory Address Mapping of TRM
60+
- Next `!Nvm` corresponds to instruction bus for external memory, see External Memory Address Mapping of TRM
61+
- Next `!Nvm` corresponds to data bus for external memory, see External Memory Address Mapping of TRM
62+
3. Add `load_address` under `flash_algorithms` and assing the IRAM `ORIGIN` value (step 3).
63+
9. Merge the new flash algorithm into the the main `esp32c3.yaml`
64+
10. Upstream the new updates to probe-rs.

build.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ fn main() {
88
fs::copy("ld/loader.x", out_dir.join("loader.x")).unwrap();
99
println!("cargo:rerun-if-changed=ld/loader.x");
1010

11+
#[cfg(feature = "esp32c2")]
12+
let chip = "esp32c2";
1113
#[cfg(feature = "esp32c3")]
1214
let chip = "esp32c3";
1315
#[cfg(feature = "esp32c6")]
1416
let chip = "esp32c6";
1517
#[cfg(feature = "esp32h2")]
1618
let chip = "esp32h2";
1719

18-
1920
{
20-
fs::copy(format!("ld/{}.x", chip), out_dir.join(format!("{}.x", chip))).unwrap();
21+
fs::copy(
22+
format!("ld/{}.x", chip),
23+
out_dir.join(format!("{}.x", chip)),
24+
)
25+
.unwrap();
2126
println!("cargo:rerun-if-changed=ld/{}.x", chip);
2227
println!("cargo:rustc-link-arg=-Tld/{}.x", chip);
2328
}

0 commit comments

Comments
 (0)