Skip to content

Commit c29b1dd

Browse files
connection: error handling (#270)
* connection: Check the error field in the response instead of status * Handle generic stub errors * esp32: Fix size in FlashDeflateBegin The `size` field for `Command::FlashDeflateBegin` is the uncompressed size of the segment. It was rounded up to the memory block size, and the stub was expecting more data when the flash ended, even though the inflator was returning successfully. * Fix typo in espflash/src/error.rs Co-authored-by: Jesse Braham <[email protected]> Co-authored-by: Jesse Braham <[email protected]>
1 parent 92251a9 commit c29b1dd

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

espflash/src/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl Connection {
226226
for _ in 0..100 {
227227
match self.read_response().for_command(ty)? {
228228
Some(response) if response.return_op == ty as u8 => {
229-
return if response.status == 1 {
229+
return if response.error != 0 {
230230
let _error = self.flush();
231231
Err(Error::RomError(RomError::new(
232232
command.command_type(),

espflash/src/error.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,38 @@ pub enum RomErrorKind {
287287
#[error("Malformed compressed data received")]
288288
#[diagnostic(code(espflash::rom::deflate))]
289289
DeflateError = 0x0b,
290+
291+
#[error("Bad data length")]
292+
#[diagnostic(code(espflash::rom::data_len))]
293+
BadDataLen = 0xc0,
294+
#[error("Bad data checksum")]
295+
#[diagnostic(code(espflash::rom::data_crc))]
296+
BadDataChecksum = 0xc1,
297+
#[error("Bad block size")]
298+
#[diagnostic(code(espflash::rom::block_size))]
299+
BadBlocksize = 0xc2,
300+
#[error("Invalid command")]
301+
#[diagnostic(code(espflash::rom::cmd))]
302+
InvalidCommand = 0xc3,
303+
#[error("SPI operation failed")]
304+
#[diagnostic(code(espflash::rom::spi))]
305+
FailedSpiOp = 0xc4,
306+
#[error("SPI unlock failed")]
307+
#[diagnostic(code(espflash::rom::spi_unlock))]
308+
FailedSpiUnlock = 0xc5,
309+
#[error("Not in flash mode")]
310+
#[diagnostic(code(espflash::rom::flash_mode))]
311+
NotInFlashMode = 0xc6,
312+
#[error("Error when uncompressing the data")]
313+
#[diagnostic(code(espflash::rom::inflate))]
314+
InflateError = 0xc7,
315+
#[error("Didn't receive enough data")]
316+
#[diagnostic(code(espflash::rom::not_enough))]
317+
NotEnoughData = 0xc8,
318+
#[error("Received too much data")]
319+
#[diagnostic(code(espflash::rom::too_much_data))]
320+
TooMuchData = 0xc9,
321+
290322
#[error("Other")]
291323
#[diagnostic(code(espflash::rom::other))]
292324
Other = 0xff,
@@ -302,6 +334,18 @@ impl From<u8> for RomErrorKind {
302334
0x09 => RomErrorKind::FlashReadError,
303335
0x0a => RomErrorKind::FlashReadLengthError,
304336
0x0b => RomErrorKind::DeflateError,
337+
338+
0xc0 => RomErrorKind::BadDataLen,
339+
0xc1 => RomErrorKind::BadDataChecksum,
340+
0xc2 => RomErrorKind::BadBlocksize,
341+
0xc3 => RomErrorKind::InvalidCommand,
342+
0xc4 => RomErrorKind::FailedSpiOp,
343+
0xc5 => RomErrorKind::FailedSpiUnlock,
344+
0xc6 => RomErrorKind::NotInFlashMode,
345+
0xc7 => RomErrorKind::InflateError,
346+
0xc8 => RomErrorKind::NotEnoughData,
347+
0xc9 => RomErrorKind::TooMuchData,
348+
305349
_ => RomErrorKind::Other,
306350
}
307351
}

espflash/src/targets/flash_target/esp32.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl FlashTarget for Esp32Target {
115115
CommandType::FlashDeflateBegin.timeout_for_size(erase_size),
116116
|connection| {
117117
connection.command(Command::FlashDeflateBegin {
118-
size: erase_size,
118+
size: segment.data.len() as u32,
119119
blocks: block_count as u32,
120120
block_size: flash_write_size as u32,
121121
offset: addr,

0 commit comments

Comments
 (0)