Skip to content

Commit 15b9975

Browse files
committed
better chip and flash detect errors
1 parent 4440a51 commit 15b9975

File tree

4 files changed

+42
-35
lines changed

4 files changed

+42
-35
lines changed

espflash/src/chip/esp8266.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::{ChipType, EspCommonHeader, SegmentHeader, ESP_MAGIC};
44
use crate::{
55
chip::{Chip, SpiRegisters},
66
elf::{update_checksum, CodeSegment, FirmwareImage, RomSegment, ESP_CHECKSUM_MAGIC},
7+
error::FlashDetectError,
78
flasher::FlashSize,
89
Error, PartitionTable,
910
};
@@ -96,7 +97,7 @@ impl ChipType for Esp8266 {
9697
}
9798
}
9899

99-
fn encode_flash_size(size: FlashSize) -> Result<u8, Error> {
100+
fn encode_flash_size(size: FlashSize) -> Result<u8, FlashDetectError> {
100101
match size {
101102
FlashSize::Flash256Kb => Ok(0x10),
102103
FlashSize::Flash512Kb => Ok(0x00),
@@ -105,7 +106,7 @@ fn encode_flash_size(size: FlashSize) -> Result<u8, Error> {
105106
FlashSize::Flash4Mb => Ok(0x40),
106107
FlashSize::Flash8Mb => Ok(0x80),
107108
FlashSize::Flash16Mb => Ok(0x90),
108-
FlashSize::FlashRetry => Err(Error::UnsupportedFlash(size as u8)),
109+
FlashSize::FlashRetry => Err(FlashDetectError::from(size as u8)),
109110
}
110111
}
111112

espflash/src/chip/mod.rs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use crate::{
77
Error, PartitionTable,
88
};
99

10-
use std::{io::Write, str::FromStr};
10+
use std::io::Write;
1111

12+
use crate::error::{ChipDetectError, FlashDetectError};
1213
use crate::flash_target::{Esp32Target, Esp8266Target, FlashTarget, RamTarget};
1314
use crate::flasher::SpiAttachParams;
1415
pub use esp32::Esp32;
@@ -102,14 +103,14 @@ pub enum Chip {
102103
}
103104

104105
impl Chip {
105-
pub fn from_magic(magic: u32) -> Option<Self> {
106+
pub fn from_magic(magic: u32) -> Result<Self, ChipDetectError> {
106107
match magic {
107-
Esp32::CHIP_DETECT_MAGIC_VALUE => Some(Chip::Esp32),
108+
Esp32::CHIP_DETECT_MAGIC_VALUE => Ok(Chip::Esp32),
108109
Esp32c3::CHIP_DETECT_MAGIC_VALUE | Esp32c3::CHIP_DETECT_MAGIC_VALUE2 => {
109-
Some(Chip::Esp32c3)
110+
Ok(Chip::Esp32c3)
110111
}
111-
Esp8266::CHIP_DETECT_MAGIC_VALUE => Some(Chip::Esp8266),
112-
_ => None,
112+
Esp8266::CHIP_DETECT_MAGIC_VALUE => Ok(Chip::Esp8266),
113+
_ => Err(ChipDetectError::from(magic)),
113114
}
114115
}
115116

@@ -154,19 +155,6 @@ impl Chip {
154155
}
155156
}
156157

157-
impl FromStr for Chip {
158-
type Err = Error;
159-
160-
fn from_str(s: &str) -> Result<Self, Self::Err> {
161-
match s {
162-
"esp32" => Ok(Chip::Esp32),
163-
"esp32c3" => Ok(Chip::Esp32c3),
164-
"esp8266" => Ok(Chip::Esp8266),
165-
_ => Err(Error::UnrecognizedChip),
166-
}
167-
}
168-
}
169-
170158
#[derive(Copy, Clone, Zeroable, Pod, Debug)]
171159
#[repr(C)]
172160
struct EspCommonHeader {
@@ -186,16 +174,16 @@ struct SegmentHeader {
186174

187175
// Note that this function ONLY applies to the ESP32 and variants; the ESP8266
188176
// has defined its own version rather than using this implementation.
189-
fn encode_flash_size(size: FlashSize) -> Result<u8, Error> {
177+
fn encode_flash_size(size: FlashSize) -> Result<u8, FlashDetectError> {
190178
match size {
191-
FlashSize::Flash256Kb => Err(Error::UnsupportedFlash(size as u8)),
192-
FlashSize::Flash512Kb => Err(Error::UnsupportedFlash(size as u8)),
179+
FlashSize::Flash256Kb => Err(FlashDetectError::from(size as u8)),
180+
FlashSize::Flash512Kb => Err(FlashDetectError::from(size as u8)),
193181
FlashSize::Flash1Mb => Ok(0x00),
194182
FlashSize::Flash2Mb => Ok(0x10),
195183
FlashSize::Flash4Mb => Ok(0x20),
196184
FlashSize::Flash8Mb => Ok(0x30),
197185
FlashSize::Flash16Mb => Ok(0x40),
198-
FlashSize::FlashRetry => Err(Error::UnsupportedFlash(size as u8)),
186+
FlashSize::FlashRetry => Err(FlashDetectError::from(size as u8)),
199187
}
200188
}
201189

espflash/src/error.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,22 @@ pub enum Error {
2727
ElfNotRamLoadable,
2828
#[error("The bootloader returned an error")]
2929
#[diagnostic(transparent)]
30-
RomError(#[source] RomError),
30+
RomError(#[from] RomError),
3131
#[error("Chip not recognized, supported chip types are esp8266, esp32 and esp32-c3")]
3232
#[diagnostic(
3333
code(espflash::unrecognized_chip),
3434
help("If your chip is supported, try hard-resetting the device and try again")
3535
)]
36-
UnrecognizedChip,
37-
#[error(
38-
"Flash chip not supported, flash id: {0:#x}, flash sizes from 1 to 16MB are supported"
39-
)]
36+
UnrecognizedChip(#[from] ChipDetectError),
37+
#[error("Flash chip not supported, flash sizes from 1 to 16MB are supported")]
4038
#[diagnostic(code(espflash::unrecognized_flash))]
41-
UnsupportedFlash(u8),
39+
UnsupportedFlash(#[from] FlashDetectError),
4240
#[error("Failed to connect to on-device flash")]
4341
#[diagnostic(code(espflash::flash_connect))]
4442
FlashConnect,
4543
#[error(transparent)]
4644
#[diagnostic(transparent)]
47-
MalformedPartitionTable(PartitionTableError),
45+
MalformedPartitionTable(#[from] PartitionTableError),
4846
}
4947

5048
#[derive(Error, Debug, Diagnostic)]
@@ -285,3 +283,23 @@ impl From<&'static str> for ElfError {
285283
ElfError(err)
286284
}
287285
}
286+
287+
#[derive(Debug, Error)]
288+
#[error("Unrecognized magic value {0:#x}")]
289+
pub struct ChipDetectError(u32);
290+
291+
impl From<u32> for ChipDetectError {
292+
fn from(err: u32) -> Self {
293+
ChipDetectError(err)
294+
}
295+
}
296+
297+
#[derive(Debug, Error)]
298+
#[error("Unrecognized flash id {0:#x}")]
299+
pub struct FlashDetectError(u8);
300+
301+
impl From<u8> for FlashDetectError {
302+
fn from(err: u8) -> Self {
303+
FlashDetectError(err)
304+
}
305+
}

espflash/src/flasher.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use strum_macros::Display;
55
use std::thread::sleep;
66

77
use crate::elf::RomSegment;
8-
use crate::error::{ConnectionError, ElfError, ResultExt};
8+
use crate::error::{ConnectionError, ElfError, FlashDetectError, ResultExt};
99
use crate::{
1010
chip::Chip, connection::Connection, elf::FirmwareImage, encoder::SlipEncoder, error::RomError,
1111
Error, PartitionTable,
@@ -108,7 +108,7 @@ impl FlashSize {
108108
0x17 => Ok(FlashSize::Flash8Mb),
109109
0x18 => Ok(FlashSize::Flash16Mb),
110110
0xFF => Ok(FlashSize::FlashRetry),
111-
_ => Err(Error::UnsupportedFlash(value)),
111+
_ => Err(Error::UnsupportedFlash(FlashDetectError::from(value))),
112112
}
113113
}
114114
}
@@ -253,7 +253,7 @@ impl Flasher {
253253

254254
fn chip_detect(&mut self) -> Result<(), Error> {
255255
let magic = self.read_reg(CHIP_DETECT_MAGIC_REG_ADDR)?;
256-
let chip = Chip::from_magic(magic).ok_or(Error::UnrecognizedChip)?;
256+
let chip = Chip::from_magic(magic)?;
257257

258258
self.chip = chip;
259259
Ok(())

0 commit comments

Comments
 (0)