Skip to content

Commit

Permalink
fix: esp32c3 direct boot (#358)
Browse files Browse the repository at this point in the history
* fix: esp32c3 direct-boot format

Prior to this change, attempting to `espflash flash` with the `--format
direct-boot` argument targeting an esp32c3 would fail, reporting:

```
Error: espflash::unsupported_image_format

  × Image format direct-boot is not supported by the esp32c3 revision v0.3
  help: The esp32c3 only supports direct-boot starting with revision 3
```

However, direct-boot on this board was working prior to the changes in
b25af06, and looking at the [docs][esp32c3 revisions] it seems that the
mapping that was chosen for the `c3` specifically was `v0.REVISION`:

> ECO  | Revision (Major.Minor)
> -----------------------------
> ECO1 | v0.1
> ECO2 | v0.2
> ECO3 | v0.3
> ECO4 | v0.4

[esp32c3 revisions]: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-reference/system/chip_revision.html#revisions

* refactor+docs: move & reword esp32c3 specific err

This change rewords the error message from "revision 3" to "revision 3
(v0.3)" in an effort to clarify the mapping between hardware revisions
and major/minor version numbers.

Additionally, it moves the esp32c3-specific error closer to the actual
version check to make it easier to keep both in sync.

* chore: remove unused import

Previously, `cargo check --all-targets` reported this import as unused.
Since it's just `const u16`, the import is probably side-effect free and
indeed safe to remove.
  • Loading branch information
sethp authored Mar 8, 2023
1 parent 4665742 commit 7a67c41
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
22 changes: 13 additions & 9 deletions espflash/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ pub struct UnsupportedImageFormatError {
format: ImageFormatKind,
chip: Chip,
revision: Option<(u32, u32)>,
context: Option<String>,
}

impl UnsupportedImageFormatError {
Expand All @@ -409,6 +410,7 @@ impl UnsupportedImageFormatError {
format,
chip,
revision,
context: None,
}
}

Expand All @@ -421,6 +423,12 @@ impl UnsupportedImageFormatError {
.collect::<Vec<&'static str>>()
.join(", ")
}

pub fn with_context(mut self, ctx: String) -> Self {
self.context.replace(ctx);

self
}
}

impl std::error::Error for UnsupportedImageFormatError {}
Expand All @@ -447,19 +455,15 @@ impl Diagnostic for UnsupportedImageFormatError {
}

fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
let str = if self.chip == Chip::Esp32c3 && self.format == ImageFormatKind::DirectBoot {
format!(
"The {} only supports direct-boot starting with revision 3",
self.chip,
)
if let Some(ref ctx) = self.context {
Some(Box::new(ctx))
} else {
format!(
Some(Box::new(format!(
"The following image formats are supported by the {}: {}",
self.chip,
self.supported_formats()
)
};
Some(Box::new(str))
)))
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions espflash/src/targets/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ impl Target for Esp32c3 {
flash_size,
flash_freq,
)?)),
(ImageFormatKind::DirectBoot, None | Some((3.., _))) => {
(ImageFormatKind::DirectBoot, None | Some((_, 3..))) => {
Ok(Box::new(DirectBootFormat::new(image, 0)?))
}
_ => Err(
UnsupportedImageFormatError::new(image_format, Chip::Esp32c3, chip_revision).into(),
UnsupportedImageFormatError::new(image_format, Chip::Esp32c3, chip_revision)
.with_context(format!(
"The {} only supports direct-boot starting with revision 3 (v0.3)",
Chip::Esp32c3,
))
.into(),
),
}
}
Expand Down
2 changes: 1 addition & 1 deletion espflash/src/targets/flash_target/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use flate2::{
use super::FlashTarget;
use crate::{
command::{Command, CommandType},
connection::{Connection, USB_SERIAL_JTAG_PID},
connection::Connection,
elf::RomSegment,
error::Error,
flasher::{ProgressCallbacks, SpiAttachParams, FLASH_SECTOR_SIZE},
Expand Down

0 comments on commit 7a67c41

Please sign in to comment.