@@ -9,12 +9,11 @@ use crate::{
9
9
command:: { Command , CommandType } ,
10
10
connection:: Connection ,
11
11
elf:: { FirmwareImage , RomSegment } ,
12
- error:: { ConnectionError , FlashDetectError , ResultExt , RomError , RomErrorKind } ,
12
+ error:: { ConnectionError , FlashDetectError , ResultExt } ,
13
13
image_format:: ImageFormatId ,
14
14
Error , PartitionTable ,
15
15
} ;
16
16
17
- const DEFAULT_CONNECT_ATTEMPTS : usize = 7 ;
18
17
const DEFAULT_TIMEOUT : Duration = Duration :: from_secs ( 3 ) ;
19
18
20
19
pub ( crate ) const FLASH_SECTOR_SIZE : usize = 0x1000 ;
@@ -178,17 +177,26 @@ impl Flasher {
178
177
port_info : UsbPortInfo ,
179
178
speed : Option < u32 > ,
180
179
) -> Result < Self , Error > {
180
+ // Establish a connection to the device using the default baud rate of 115,200
181
+ // and timeout of 3 seconds.
182
+ let mut connection = Connection :: new ( serial, port_info) ;
183
+ connection. begin ( ) ?;
184
+ connection. set_timeout ( DEFAULT_TIMEOUT ) ?;
185
+
186
+ // Detect which chip we are connected to.
187
+ let magic = connection. read_reg ( CHIP_DETECT_MAGIC_REG_ADDR ) ?;
188
+ let chip = Chip :: from_magic ( magic) ?;
189
+
181
190
let mut flasher = Flasher {
182
- connection : Connection :: new ( serial , port_info ) , // default baud is always 115200
183
- chip : Chip :: Esp8266 , // dummy, set properly later
191
+ connection,
192
+ chip,
184
193
flash_size : FlashSize :: Flash4Mb ,
185
- spi_params : SpiAttachParams :: default ( ) , // may be set when trying to attach to flash
194
+ spi_params : SpiAttachParams :: default ( ) ,
186
195
} ;
187
- flasher. start_connection ( ) ?;
188
- flasher. connection . set_timeout ( DEFAULT_TIMEOUT ) ?;
189
- flasher. chip_detect ( ) ?;
190
196
flasher. spi_autodetect ( ) ?;
191
197
198
+ // Now that we have established a connection and detected the chip and flash
199
+ // size, we can set the baud rate of the connection to the configured value.
192
200
if let Some ( b) = speed {
193
201
match flasher. chip {
194
202
Chip :: Esp8266 => ( ) , // Not available
@@ -223,14 +231,6 @@ impl Flasher {
223
231
Err ( Error :: FlashConnect )
224
232
}
225
233
226
- fn chip_detect ( & mut self ) -> Result < ( ) , Error > {
227
- let magic = self . connection . read_reg ( CHIP_DETECT_MAGIC_REG_ADDR ) ?;
228
- let chip = Chip :: from_magic ( magic) ?;
229
-
230
- self . chip = chip;
231
- Ok ( ( ) )
232
- }
233
-
234
234
fn flash_detect ( & mut self ) -> Result < Option < FlashSize > , Error > {
235
235
const FLASH_RETRY : u8 = 0xFF ;
236
236
@@ -257,74 +257,6 @@ impl Flasher {
257
257
Ok ( Some ( flash_size) )
258
258
}
259
259
260
- fn sync ( & mut self ) -> Result < ( ) , Error > {
261
- self . connection
262
- . with_timeout ( CommandType :: Sync . timeout ( ) , |connection| {
263
- connection. write_command ( Command :: Sync ) ?;
264
- connection. flush ( ) ?;
265
-
266
- for _ in 0 ..100 {
267
- match connection. read_response ( ) ? {
268
- Some ( response) if response. return_op == CommandType :: Sync as u8 => {
269
- if response. status == 1 {
270
- let _error = connection. flush ( ) ;
271
- return Err ( Error :: RomError ( RomError :: new (
272
- CommandType :: Sync ,
273
- RomErrorKind :: from ( response. error ) ,
274
- ) ) ) ;
275
- } else {
276
- break ;
277
- }
278
- }
279
- _ => continue ,
280
- }
281
- }
282
-
283
- Ok ( ( ) )
284
- } ) ?;
285
- for _ in 0 ..700 {
286
- match self . connection . read_response ( ) ? {
287
- Some ( _) => break ,
288
- _ => continue ,
289
- }
290
- }
291
- Ok ( ( ) )
292
- }
293
-
294
- fn start_connection ( & mut self ) -> Result < ( ) , Error > {
295
- let mut extra_delay = false ;
296
- for i in 0 ..DEFAULT_CONNECT_ATTEMPTS {
297
- if self . connect_attempt ( extra_delay) . is_err ( ) {
298
- extra_delay = !extra_delay;
299
-
300
- let delay_text = if extra_delay { "extra" } else { "default" } ;
301
- println ! ( "Unable to connect, retrying with {} delay..." , delay_text) ;
302
- } else {
303
- // Print a blank line if more than one connection attempt was made to visually
304
- // separate the status text and whatever comes next.
305
- if i > 0 {
306
- println ! ( ) ;
307
- }
308
- return Ok ( ( ) ) ;
309
- }
310
- }
311
-
312
- Err ( Error :: Connection ( ConnectionError :: ConnectionFailed ) )
313
- }
314
-
315
- fn connect_attempt ( & mut self , extra_delay : bool ) -> Result < ( ) , Error > {
316
- self . connection . reset_to_flash ( extra_delay) ?;
317
-
318
- for _ in 0 ..5 {
319
- self . connection . flush ( ) ?;
320
- if self . sync ( ) . is_ok ( ) {
321
- return Ok ( ( ) ) ;
322
- }
323
- }
324
-
325
- Err ( Error :: Connection ( ConnectionError :: ConnectionFailed ) )
326
- }
327
-
328
260
fn enable_flash ( & mut self , spi_params : SpiAttachParams ) -> Result < ( ) , Error > {
329
261
match self . chip {
330
262
Chip :: Esp8266 => {
0 commit comments