18
18
//! load a program.
19
19
use anyhow:: { anyhow, Context , Result } ;
20
20
use std:: future:: Future ;
21
- use tokio:: { io:: AsyncWriteExt , task:: spawn_blocking, time:: delay_for} ;
21
+ use tokio:: {
22
+ io:: { AsyncWriteExt , BufStream } ,
23
+ task:: spawn_blocking,
24
+ time:: delay_for,
25
+ } ;
22
26
use tokio_serial:: { Serial , SerialPortSettings } ;
23
27
24
- use super :: { jlink:: read_elf, Arch , DebugProbe , Target } ;
28
+ use super :: { demux :: Demux , jlink:: read_elf, Arch , DebugProbe , Target } ;
25
29
use crate :: utils:: retry_on_fail_with_delay;
26
30
27
31
pub struct RaspberryPiPico ;
@@ -72,7 +76,7 @@ impl Target for RaspberryPiPico {
72
76
a PICOBOOT USB interface failed with the following error: {}",
73
77
e
74
78
) ;
75
- Some ( serial)
79
+ Some ( BufStream :: new ( serial) )
76
80
}
77
81
( Err ( e) , Ok ( _picoboot_interface) ) => {
78
82
log:: debug!(
@@ -111,7 +115,7 @@ struct RaspberryPiPicoUsbDebugProbe {
111
115
/// Even if this field is set, the test driver's current state is
112
116
/// indeterminate in general, so the target must be rebooted before doing
113
117
/// anything meaningful.
114
- serial : Option < Serial > ,
118
+ serial : Option < BufStream < Serial > > ,
115
119
}
116
120
117
121
impl DebugProbe for RaspberryPiPicoUsbDebugProbe {
@@ -121,7 +125,10 @@ impl DebugProbe for RaspberryPiPicoUsbDebugProbe {
121
125
) -> std:: pin:: Pin < Box < dyn Future < Output = Result < super :: DynAsyncRead < ' _ > > > + ' _ > > {
122
126
let exe = exe. to_owned ( ) ;
123
127
Box :: pin ( async move {
124
- if let Some ( mut serial) = self . serial . take ( ) {
128
+ if let Some ( serial) = self . serial . take ( ) {
129
+ // Discard any leftover data and unwrap `BufStream`
130
+ let mut serial = serial. into_inner ( ) ;
131
+
125
132
// Reboot the target into BOOTSEL mode. This will sever the
126
133
// serial connection.
127
134
log:: debug!(
@@ -146,16 +153,15 @@ impl DebugProbe for RaspberryPiPicoUsbDebugProbe {
146
153
// Wait until the host operating system recognizes the USB device...
147
154
delay_for ( DEFAULT_PAUSE ) . await ;
148
155
149
- self . serial = Some (
156
+ let serial =
150
157
retry_on_fail_with_delay ( || async { spawn_blocking ( open_serial) . await . unwrap ( ) } )
151
158
. await
152
- . with_context ( || "Failed to connect to the test driver serial interface." ) ?,
153
- ) ;
159
+ . with_context ( || "Failed to connect to the test driver serial interface." ) ?;
154
160
155
- // TODO: give a 'go' signal, grab the output,
156
- // and then issue a reboot request by sending `r`
161
+ self . serial = Some ( BufStream :: new ( serial) ) ;
157
162
158
- todo ! ( )
163
+ // Now, pass the channel to the caller
164
+ Ok ( Box :: pin ( Demux :: new ( self . serial . as_mut ( ) . unwrap ( ) ) ) as _ )
159
165
} )
160
166
}
161
167
}
0 commit comments