Skip to content

Commit db1ed13

Browse files
committed
Remove the FlashSize::FlashRetry enum variant so we can serialize all values
1 parent c6d5b0f commit db1ed13

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

espflash/src/flasher.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const FLASH_SECTORS_PER_BLOCK: usize = FLASH_SECTOR_SIZE / FLASH_BLOCK_SIZE;
2626
const CHIP_DETECT_MAGIC_REG_ADDR: u32 = 0x40001000;
2727

2828
#[derive(Clone, Copy, Debug, Eq, PartialEq, Display)]
29-
#[allow(dead_code)]
3029
#[repr(u8)]
3130
pub enum FlashSize {
3231
#[strum(serialize = "256KB")]
@@ -47,7 +46,8 @@ pub enum FlashSize {
4746
Flash32Mb = 0x19,
4847
#[strum(serialize = "64MB")]
4948
Flash64Mb = 0x1a,
50-
FlashRetry = 0xFF, // used to hint that alternate detection should be tried
49+
#[strum(serialize = "128MB")]
50+
Flash128Mb = 0x21,
5151
}
5252

5353
impl FlashSize {
@@ -62,7 +62,6 @@ impl FlashSize {
6262
0x18 => Ok(FlashSize::Flash16Mb),
6363
0x19 => Ok(FlashSize::Flash32Mb),
6464
0x1a => Ok(FlashSize::Flash64Mb),
65-
0xFF => Ok(FlashSize::FlashRetry),
6665
_ => Err(Error::UnsupportedFlash(FlashDetectError::from(value))),
6766
}
6867
}
@@ -183,18 +182,21 @@ impl Flasher {
183182
}
184183

185184
fn spi_autodetect(&mut self) -> Result<(), Error> {
186-
// loop over all available spi params until we find one that successfully reads
187-
// the flash size
185+
// Loop over all available SPI parameters until we find one that successfully
186+
// reads the flash size.
188187
for spi_params in TRY_SPI_PARAMS.iter().copied() {
189188
self.enable_flash(spi_params)?;
190-
if self.flash_detect()? {
191-
// flash detect successful, save these spi params
189+
if let Some(flash_size) = self.flash_detect()? {
190+
// Flash detection was successful, so save the flash size and SPI parameters and
191+
// return.
192+
self.flash_size = flash_size;
192193
self.spi_params = spi_params;
194+
193195
return Ok(());
194196
}
195197
}
196198

197-
// none of the spi parameters were successful
199+
// None of the SPI parameters were successful.
198200
Err(Error::FlashConnect)
199201
}
200202

@@ -206,11 +208,18 @@ impl Flasher {
206208
Ok(())
207209
}
208210

209-
fn flash_detect(&mut self) -> Result<bool, Error> {
211+
fn flash_detect(&mut self) -> Result<Option<FlashSize>, Error> {
212+
const FLASH_RETRY: u8 = 0xFF;
213+
210214
let flash_id = self.spi_command(CommandType::FlashDetect, &[], 24)?;
211-
let size_id = flash_id >> 16;
215+
let size_id = (flash_id >> 16) as u8;
216+
217+
// This value indicates that an alternate detection method should be tried.
218+
if size_id == FLASH_RETRY {
219+
return Ok(None);
220+
}
212221

213-
self.flash_size = match FlashSize::from(size_id as u8) {
222+
let flash_size = match FlashSize::from(size_id) {
214223
Ok(size) => size,
215224
Err(_) => {
216225
eprintln!(
@@ -222,7 +231,7 @@ impl Flasher {
222231
}
223232
};
224233

225-
Ok(self.flash_size != FlashSize::FlashRetry)
234+
Ok(Some(flash_size))
226235
}
227236

228237
fn sync(&mut self) -> Result<(), Error> {

0 commit comments

Comments
 (0)