@@ -28,10 +28,12 @@ const ERASE_WRITE_TIMEOUT_PER_MB: Duration = Duration::from_secs(40);
28
28
const MEM_END_TIMEOUT : Duration = Duration :: from_millis ( 50 ) ;
29
29
const SYNC_TIMEOUT : Duration = Duration :: from_millis ( 100 ) ;
30
30
31
- #[ derive( Copy , Clone , Debug ) ]
31
+ #[ derive( Copy , Clone , Debug , Display ) ]
32
32
#[ allow( dead_code) ]
33
33
#[ repr( u8 ) ]
34
- pub ( crate ) enum Command {
34
+ #[ non_exhaustive]
35
+ pub enum Command {
36
+ Unknown = 0 ,
35
37
FlashBegin = 0x02 ,
36
38
FlashData = 0x03 ,
37
39
FlashEnd = 0x04 ,
@@ -48,6 +50,7 @@ pub(crate) enum Command {
48
50
FlashDeflateData = 0x11 ,
49
51
FlashDeflateEnd = 0x12 ,
50
52
FlashMd5 = 0x13 ,
53
+ FlashDetect = 0x9f ,
51
54
}
52
55
53
56
impl Command {
@@ -68,7 +71,9 @@ impl Command {
68
71
)
69
72
}
70
73
match self {
71
- Command :: FlashBegin => calc_timeout ( ERASE_REGION_TIMEOUT_PER_MB , size) ,
74
+ Command :: FlashBegin | Command :: FlashDeflateBegin => {
75
+ calc_timeout ( ERASE_REGION_TIMEOUT_PER_MB , size)
76
+ }
72
77
Command :: FlashData | Command :: FlashDeflateData => {
73
78
calc_timeout ( ERASE_WRITE_TIMEOUT_PER_MB , size)
74
79
}
@@ -258,7 +263,7 @@ impl Flasher {
258
263
}
259
264
260
265
fn flash_detect ( & mut self ) -> Result < bool , Error > {
261
- let flash_id = self . spi_command ( 0x9f , & [ ] , 24 ) ?;
266
+ let flash_id = self . spi_command ( Command :: FlashDetect , & [ ] , 24 ) ?;
262
267
let size_id = flash_id >> 16 ;
263
268
264
269
self . flash_size = FlashSize :: from ( size_id as u8 ) ?;
@@ -340,7 +345,7 @@ impl Flasher {
340
345
341
346
self . connection
342
347
. with_timeout ( command. timeout_for_size ( size) , |connection| {
343
- connection. command ( command as u8 , data, 0 ) ?;
348
+ connection. command ( command, data, 0 ) ?;
344
349
Ok ( ( ) )
345
350
} )
346
351
}
@@ -354,14 +359,14 @@ impl Flasher {
354
359
let spi_params = spi_attach_params. encode ( ) ;
355
360
self . connection
356
361
. with_timeout ( Command :: SpiAttach . timeout ( ) , |connection| {
357
- connection. command ( Command :: SpiAttach as u8 , spi_params. as_slice ( ) , 0 )
362
+ connection. command ( Command :: SpiAttach , spi_params. as_slice ( ) , 0 )
358
363
} ) ?;
359
364
}
360
365
}
361
366
Ok ( ( ) )
362
367
}
363
368
364
- fn spi_command ( & mut self , command : u8 , data : & [ u8 ] , read_bits : u32 ) -> Result < u32 , Error > {
369
+ fn spi_command ( & mut self , command : Command , data : & [ u8 ] , read_bits : u32 ) -> Result < u32 , Error > {
365
370
assert ! ( read_bits < 32 ) ;
366
371
assert ! ( data. len( ) < 64 ) ;
367
372
@@ -421,7 +426,7 @@ impl Flasher {
421
426
}
422
427
i += 1 ;
423
428
if i > 10 {
424
- return Err ( Error :: Connection ( ConnectionError :: Timeout ) ) ;
429
+ return Err ( Error :: Connection ( ConnectionError :: Timeout ( command . into ( ) ) ) ) ;
425
430
}
426
431
}
427
432
@@ -435,7 +440,7 @@ impl Flasher {
435
440
fn read_reg ( & mut self , reg : u32 ) -> Result < u32 , Error > {
436
441
self . connection
437
442
. with_timeout ( Command :: ReadReg . timeout ( ) , |connection| {
438
- connection. command ( Command :: ReadReg as u8 , & reg. to_le_bytes ( ) [ ..] , 0 )
443
+ connection. command ( Command :: ReadReg , & reg. to_le_bytes ( ) [ ..] , 0 )
439
444
} )
440
445
}
441
446
@@ -448,7 +453,7 @@ impl Flasher {
448
453
} ;
449
454
self . connection
450
455
. with_timeout ( Command :: WriteReg . timeout ( ) , |connection| {
451
- connection. command ( Command :: WriteReg as u8 , bytes_of ( & params) , 0 )
456
+ connection. command ( Command :: WriteReg , bytes_of ( & params) , 0 )
452
457
} ) ?;
453
458
Ok ( ( ) )
454
459
}
@@ -524,11 +529,7 @@ impl Flasher {
524
529
525
530
self . connection
526
531
. with_timeout ( Command :: ChangeBaud . timeout ( ) , |connection| {
527
- connection. command (
528
- Command :: ChangeBaud as u8 ,
529
- & [ new_speed, old_speed] . concat ( ) [ ..] ,
530
- 0 ,
531
- )
532
+ connection. command ( Command :: ChangeBaud , & [ new_speed, old_speed] . concat ( ) [ ..] , 0 )
532
533
} ) ?;
533
534
self . connection . set_baud ( speed) ?;
534
535
std:: thread:: sleep ( Duration :: from_secs_f32 ( 0.05 ) ) ;
0 commit comments