@@ -65,7 +65,7 @@ pub struct Connection {
65
65
}
66
66
67
67
impl Connection {
68
- /// Create a new connection with a target device.
68
+ /// Creates a new connection with a target device.
69
69
pub fn new (
70
70
serial : Port ,
71
71
port_info : UsbPortInfo ,
@@ -84,7 +84,7 @@ impl Connection {
84
84
}
85
85
}
86
86
87
- /// Initialize a connection with a device.
87
+ /// Initializes a connection with a device.
88
88
pub fn begin ( & mut self ) -> Result < ( ) , Error > {
89
89
let port_name = self . serial . name ( ) . unwrap_or_default ( ) ;
90
90
let reset_sequence = construct_reset_strategy_sequence (
@@ -109,7 +109,7 @@ impl Connection {
109
109
) ) )
110
110
}
111
111
112
- /// Try to connect to a device.
112
+ /// Connects to a device.
113
113
fn connect_attempt ( & mut self , reset_strategy : & dyn ResetStrategy ) -> Result < ( ) , Error > {
114
114
// If we're doing no_sync, we're likely communicating as a pass through
115
115
// with an intermediate device to the ESP32
@@ -187,7 +187,7 @@ impl Connection {
187
187
) ) )
188
188
}
189
189
190
- /// Try to sync with the device for a given timeout .
190
+ /// Syncs with a device .
191
191
pub ( crate ) fn sync ( & mut self ) -> Result < ( ) , Error > {
192
192
self . with_timeout ( CommandType :: Sync . timeout ( ) , |connection| {
193
193
connection. command ( Command :: Sync ) ?;
@@ -221,14 +221,14 @@ impl Connection {
221
221
Ok ( ( ) )
222
222
}
223
223
224
- /// Reset the device.
224
+ /// Resets the device.
225
225
pub fn reset ( & mut self ) -> Result < ( ) , Error > {
226
226
reset_after_flash ( & mut self . serial , self . port_info . pid ) ?;
227
227
228
228
Ok ( ( ) )
229
229
}
230
230
231
- /// Reset the device taking into account the reset after argument.
231
+ /// Resets the device taking into account the reset after argument.
232
232
pub fn reset_after ( & mut self , is_stub : bool , chip : Chip ) -> Result < ( ) , Error > {
233
233
let pid = self . usb_pid ( ) ;
234
234
@@ -291,7 +291,7 @@ impl Connection {
291
291
}
292
292
}
293
293
294
- /// Reset the device to flash mode.
294
+ /// Resets the device to flash mode.
295
295
pub fn reset_to_flash ( & mut self , extra_delay : bool ) -> Result < ( ) , Error > {
296
296
if self . is_using_usb_serial_jtag ( ) {
297
297
UsbJtagSerialReset . reset ( & mut self . serial )
@@ -308,25 +308,25 @@ impl Connection {
308
308
}
309
309
}
310
310
311
- /// Set timeout for the serial port.
311
+ /// Sets the timeout for the serial port.
312
312
pub fn set_timeout ( & mut self , timeout : Duration ) -> Result < ( ) , Error > {
313
313
self . serial . set_timeout ( timeout) ?;
314
314
Ok ( ( ) )
315
315
}
316
316
317
- /// Set baud rate for the serial port.
317
+ /// Sets the baud rate for the serial port.
318
318
pub fn set_baud ( & mut self , baud : u32 ) -> Result < ( ) , Error > {
319
319
self . serial . set_baud_rate ( baud) ?;
320
320
self . baud = baud;
321
321
Ok ( ( ) )
322
322
}
323
323
324
- /// Get the current baud rate of the serial port.
324
+ /// Returns the current baud rate of the serial port.
325
325
pub fn baud ( & self ) -> Result < u32 , Error > {
326
326
Ok ( self . serial . baud_rate ( ) ?)
327
327
}
328
328
329
- /// Run a command with a timeout defined by the command type.
329
+ /// Runs a command with a timeout defined by the command type.
330
330
pub fn with_timeout < T , F > ( & mut self , timeout : Duration , mut f : F ) -> Result < T , Error >
331
331
where
332
332
F : FnMut ( & mut Connection ) -> Result < T , Error > ,
@@ -346,7 +346,7 @@ impl Connection {
346
346
result
347
347
}
348
348
349
- /// Read the response from a serial port.
349
+ /// Reads the response from a serial port.
350
350
pub fn read_flash_response ( & mut self ) -> Result < Option < CommandResponse > , Error > {
351
351
let mut response = Vec :: new ( ) ;
352
352
@@ -369,7 +369,7 @@ impl Connection {
369
369
Ok ( Some ( header) )
370
370
}
371
371
372
- /// Read the response from a serial port.
372
+ /// Reads the response from a serial port.
373
373
pub fn read_response ( & mut self ) -> Result < Option < CommandResponse > , Error > {
374
374
match self . read ( 10 ) ? {
375
375
None => Ok ( None ) ,
@@ -432,7 +432,7 @@ impl Connection {
432
432
}
433
433
}
434
434
435
- /// Write raw data to the serial port.
435
+ /// Writes raw data to the serial port.
436
436
pub fn write_raw ( & mut self , data : u32 ) -> Result < ( ) , Error > {
437
437
let mut binding = Box :: new ( & mut self . serial ) ;
438
438
let serial = binding. as_mut ( ) ;
@@ -445,7 +445,7 @@ impl Connection {
445
445
Ok ( ( ) )
446
446
}
447
447
448
- /// Write a command to the serial port.
448
+ /// Writes a command to the serial port.
449
449
pub fn write_command ( & mut self , command : Command < ' _ > ) -> Result < ( ) , Error > {
450
450
debug ! ( "Writing command: {command:02x?}" ) ;
451
451
let mut binding = Box :: new ( & mut self . serial ) ;
@@ -460,7 +460,7 @@ impl Connection {
460
460
Ok ( ( ) )
461
461
}
462
462
463
- /// Write a command and reads the response.
463
+ /// Writes a command and reads the response.
464
464
pub fn command ( & mut self , command : Command < ' _ > ) -> Result < CommandResponseValue , Error > {
465
465
let ty = command. command_type ( ) ;
466
466
self . write_command ( command) . for_command ( ty) ?;
@@ -495,7 +495,7 @@ impl Connection {
495
495
) ) )
496
496
}
497
497
498
- /// Read a register command with a timeout.
498
+ /// Reads a register command with a timeout.
499
499
pub fn read_reg ( & mut self , addr : u32 ) -> Result < u32 , Error > {
500
500
let resp = self . with_timeout ( CommandType :: ReadReg . timeout ( ) , |connection| {
501
501
connection. command ( Command :: ReadReg { address : addr } )
@@ -504,7 +504,7 @@ impl Connection {
504
504
resp. try_into ( )
505
505
}
506
506
507
- /// Write a register command with a timeout.
507
+ /// Writes a register command with a timeout.
508
508
pub fn write_reg ( & mut self , addr : u32 , value : u32 , mask : Option < u32 > ) -> Result < ( ) , Error > {
509
509
self . with_timeout ( CommandType :: WriteReg . timeout ( ) , |connection| {
510
510
connection. command ( Command :: WriteReg {
@@ -517,7 +517,7 @@ impl Connection {
517
517
Ok ( ( ) )
518
518
}
519
519
520
- /// Read a register command with a timeout.
520
+ /// Reads a register command with a timeout.
521
521
pub ( crate ) fn read ( & mut self , len : usize ) -> Result < Option < Vec < u8 > > , Error > {
522
522
let mut tmp = Vec :: with_capacity ( 1024 ) ;
523
523
loop {
@@ -528,18 +528,18 @@ impl Connection {
528
528
}
529
529
}
530
530
531
- /// Flush the serial port.
531
+ /// Flushes the serial port.
532
532
pub fn flush ( & mut self ) -> Result < ( ) , Error > {
533
533
self . serial . flush ( ) ?;
534
534
Ok ( ( ) )
535
535
}
536
536
537
- /// Turn a serial port into a [Port].
537
+ /// Turns a serial port into a [Port].
538
538
pub fn into_serial ( self ) -> Port {
539
539
self . serial
540
540
}
541
541
542
- /// Get the USB PID of the serial port.
542
+ /// Returns the USB PID of the serial port.
543
543
pub fn usb_pid ( & self ) -> u16 {
544
544
self . port_info . pid
545
545
}
@@ -559,7 +559,7 @@ impl Connection {
559
559
self . before_operation
560
560
}
561
561
562
- /// Detect which chip is connected to this connection
562
+ /// Detects which chip is connected to this connection.
563
563
pub fn detect_chip (
564
564
& mut self ,
565
565
use_stub : bool ,
@@ -580,15 +580,24 @@ impl Connection {
580
580
}
581
581
}
582
582
583
+ impl From < Connection > for Port {
584
+ fn from ( conn : Connection ) -> Self {
585
+ conn. into_serial ( )
586
+ }
587
+ }
588
+
583
589
mod encoder {
584
590
use std:: io:: Write ;
585
591
592
+ use serde:: Serialize ;
593
+
586
594
const END : u8 = 0xC0 ;
587
595
const ESC : u8 = 0xDB ;
588
596
const ESC_END : u8 = 0xDC ;
589
597
const ESC_ESC : u8 = 0xDD ;
590
598
591
599
/// Encoder for the SLIP protocol.
600
+ #[ derive( Debug , PartialEq , Eq , Serialize , Hash ) ]
592
601
pub struct SlipEncoder < ' a , W : Write > {
593
602
writer : & ' a mut W ,
594
603
len : usize ,
@@ -601,7 +610,7 @@ mod encoder {
601
610
Ok ( Self { writer, len } )
602
611
}
603
612
604
- /// Finish the encoding.
613
+ /// Finishes the encoding.
605
614
pub fn finish ( mut self ) -> std:: io:: Result < usize > {
606
615
self . len += self . writer . write ( & [ END ] ) ?;
607
616
Ok ( self . len )
0 commit comments