@@ -65,7 +65,7 @@ pub struct Connection {
6565}
6666
6767impl Connection {
68- /// Create a new connection with a target device.
68+ /// Creates a new connection with a target device.
6969 pub fn new (
7070 serial : Port ,
7171 port_info : UsbPortInfo ,
@@ -84,7 +84,7 @@ impl Connection {
8484 }
8585 }
8686
87- /// Initialize a connection with a device.
87+ /// Initializes a connection with a device.
8888 pub fn begin ( & mut self ) -> Result < ( ) , Error > {
8989 let port_name = self . serial . name ( ) . unwrap_or_default ( ) ;
9090 let reset_sequence = construct_reset_strategy_sequence (
@@ -109,7 +109,7 @@ impl Connection {
109109 ) ) )
110110 }
111111
112- /// Try to connect to a device.
112+ /// Connects to a device.
113113 fn connect_attempt ( & mut self , reset_strategy : & dyn ResetStrategy ) -> Result < ( ) , Error > {
114114 // If we're doing no_sync, we're likely communicating as a pass through
115115 // with an intermediate device to the ESP32
@@ -187,7 +187,7 @@ impl Connection {
187187 ) ) )
188188 }
189189
190- /// Try to sync with the device for a given timeout .
190+ /// Syncs with a device .
191191 pub ( crate ) fn sync ( & mut self ) -> Result < ( ) , Error > {
192192 self . with_timeout ( CommandType :: Sync . timeout ( ) , |connection| {
193193 connection. command ( Command :: Sync ) ?;
@@ -221,14 +221,14 @@ impl Connection {
221221 Ok ( ( ) )
222222 }
223223
224- /// Reset the device.
224+ /// Resets the device.
225225 pub fn reset ( & mut self ) -> Result < ( ) , Error > {
226226 reset_after_flash ( & mut self . serial , self . port_info . pid ) ?;
227227
228228 Ok ( ( ) )
229229 }
230230
231- /// Reset the device taking into account the reset after argument.
231+ /// Resets the device taking into account the reset after argument.
232232 pub fn reset_after ( & mut self , is_stub : bool , chip : Chip ) -> Result < ( ) , Error > {
233233 let pid = self . usb_pid ( ) ;
234234
@@ -291,7 +291,7 @@ impl Connection {
291291 }
292292 }
293293
294- /// Reset the device to flash mode.
294+ /// Resets the device to flash mode.
295295 pub fn reset_to_flash ( & mut self , extra_delay : bool ) -> Result < ( ) , Error > {
296296 if self . is_using_usb_serial_jtag ( ) {
297297 UsbJtagSerialReset . reset ( & mut self . serial )
@@ -308,25 +308,25 @@ impl Connection {
308308 }
309309 }
310310
311- /// Set timeout for the serial port.
311+ /// Sets the timeout for the serial port.
312312 pub fn set_timeout ( & mut self , timeout : Duration ) -> Result < ( ) , Error > {
313313 self . serial . set_timeout ( timeout) ?;
314314 Ok ( ( ) )
315315 }
316316
317- /// Set baud rate for the serial port.
317+ /// Sets the baud rate for the serial port.
318318 pub fn set_baud ( & mut self , baud : u32 ) -> Result < ( ) , Error > {
319319 self . serial . set_baud_rate ( baud) ?;
320320 self . baud = baud;
321321 Ok ( ( ) )
322322 }
323323
324- /// Get the current baud rate of the serial port.
324+ /// Returns the current baud rate of the serial port.
325325 pub fn baud ( & self ) -> Result < u32 , Error > {
326326 Ok ( self . serial . baud_rate ( ) ?)
327327 }
328328
329- /// Run a command with a timeout defined by the command type.
329+ /// Runs a command with a timeout defined by the command type.
330330 pub fn with_timeout < T , F > ( & mut self , timeout : Duration , mut f : F ) -> Result < T , Error >
331331 where
332332 F : FnMut ( & mut Connection ) -> Result < T , Error > ,
@@ -346,7 +346,7 @@ impl Connection {
346346 result
347347 }
348348
349- /// Read the response from a serial port.
349+ /// Reads the response from a serial port.
350350 pub fn read_flash_response ( & mut self ) -> Result < Option < CommandResponse > , Error > {
351351 let mut response = Vec :: new ( ) ;
352352
@@ -369,7 +369,7 @@ impl Connection {
369369 Ok ( Some ( header) )
370370 }
371371
372- /// Read the response from a serial port.
372+ /// Reads the response from a serial port.
373373 pub fn read_response ( & mut self ) -> Result < Option < CommandResponse > , Error > {
374374 match self . read ( 10 ) ? {
375375 None => Ok ( None ) ,
@@ -432,7 +432,7 @@ impl Connection {
432432 }
433433 }
434434
435- /// Write raw data to the serial port.
435+ /// Writes raw data to the serial port.
436436 pub fn write_raw ( & mut self , data : u32 ) -> Result < ( ) , Error > {
437437 let mut binding = Box :: new ( & mut self . serial ) ;
438438 let serial = binding. as_mut ( ) ;
@@ -445,7 +445,7 @@ impl Connection {
445445 Ok ( ( ) )
446446 }
447447
448- /// Write a command to the serial port.
448+ /// Writes a command to the serial port.
449449 pub fn write_command ( & mut self , command : Command < ' _ > ) -> Result < ( ) , Error > {
450450 debug ! ( "Writing command: {command:02x?}" ) ;
451451 let mut binding = Box :: new ( & mut self . serial ) ;
@@ -460,7 +460,7 @@ impl Connection {
460460 Ok ( ( ) )
461461 }
462462
463- /// Write a command and reads the response.
463+ /// Writes a command and reads the response.
464464 pub fn command ( & mut self , command : Command < ' _ > ) -> Result < CommandResponseValue , Error > {
465465 let ty = command. command_type ( ) ;
466466 self . write_command ( command) . for_command ( ty) ?;
@@ -495,7 +495,7 @@ impl Connection {
495495 ) ) )
496496 }
497497
498- /// Read a register command with a timeout.
498+ /// Reads a register command with a timeout.
499499 pub fn read_reg ( & mut self , addr : u32 ) -> Result < u32 , Error > {
500500 let resp = self . with_timeout ( CommandType :: ReadReg . timeout ( ) , |connection| {
501501 connection. command ( Command :: ReadReg { address : addr } )
@@ -504,7 +504,7 @@ impl Connection {
504504 resp. try_into ( )
505505 }
506506
507- /// Write a register command with a timeout.
507+ /// Writes a register command with a timeout.
508508 pub fn write_reg ( & mut self , addr : u32 , value : u32 , mask : Option < u32 > ) -> Result < ( ) , Error > {
509509 self . with_timeout ( CommandType :: WriteReg . timeout ( ) , |connection| {
510510 connection. command ( Command :: WriteReg {
@@ -517,7 +517,7 @@ impl Connection {
517517 Ok ( ( ) )
518518 }
519519
520- /// Read a register command with a timeout.
520+ /// Reads a register command with a timeout.
521521 pub ( crate ) fn read ( & mut self , len : usize ) -> Result < Option < Vec < u8 > > , Error > {
522522 let mut tmp = Vec :: with_capacity ( 1024 ) ;
523523 loop {
@@ -528,18 +528,18 @@ impl Connection {
528528 }
529529 }
530530
531- /// Flush the serial port.
531+ /// Flushes the serial port.
532532 pub fn flush ( & mut self ) -> Result < ( ) , Error > {
533533 self . serial . flush ( ) ?;
534534 Ok ( ( ) )
535535 }
536536
537- /// Turn a serial port into a [Port].
537+ /// Turns a serial port into a [Port].
538538 pub fn into_serial ( self ) -> Port {
539539 self . serial
540540 }
541541
542- /// Get the USB PID of the serial port.
542+ /// Returns the USB PID of the serial port.
543543 pub fn usb_pid ( & self ) -> u16 {
544544 self . port_info . pid
545545 }
@@ -559,7 +559,7 @@ impl Connection {
559559 self . before_operation
560560 }
561561
562- /// Detect which chip is connected to this connection
562+ /// Detects which chip is connected to this connection.
563563 pub fn detect_chip (
564564 & mut self ,
565565 use_stub : bool ,
@@ -580,15 +580,24 @@ impl Connection {
580580 }
581581}
582582
583+ impl From < Connection > for Port {
584+ fn from ( conn : Connection ) -> Self {
585+ conn. into_serial ( )
586+ }
587+ }
588+
583589mod encoder {
584590 use std:: io:: Write ;
585591
592+ use serde:: Serialize ;
593+
586594 const END : u8 = 0xC0 ;
587595 const ESC : u8 = 0xDB ;
588596 const ESC_END : u8 = 0xDC ;
589597 const ESC_ESC : u8 = 0xDD ;
590598
591599 /// Encoder for the SLIP protocol.
600+ #[ derive( Debug , PartialEq , Eq , Serialize , Hash ) ]
592601 pub struct SlipEncoder < ' a , W : Write > {
593602 writer : & ' a mut W ,
594603 len : usize ,
@@ -601,7 +610,7 @@ mod encoder {
601610 Ok ( Self { writer, len } )
602611 }
603612
604- /// Finish the encoding.
613+ /// Finishes the encoding.
605614 pub fn finish ( mut self ) -> std:: io:: Result < usize > {
606615 self . len += self . writer . write ( & [ END ] ) ?;
607616 Ok ( self . len )
0 commit comments