Skip to content

Commit 7320026

Browse files
authored
bugfix: fix reset sequence for usb-serial-jtag chips (#157)
1 parent d1016ef commit 7320026

File tree

5 files changed

+45
-20
lines changed

5 files changed

+45
-20
lines changed

cargo-espflash/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ fn flash(
178178
}
179179

180180
if opts.flash_opts.monitor {
181-
monitor(flasher.into_serial(), &elf_data).into_diagnostic()?;
181+
let pid = flasher.get_usb_pid()?;
182+
monitor(flasher.into_serial(), &elf_data, pid).into_diagnostic()?;
182183
}
183184

184185
Ok(())

espflash/src/cli/monitor.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
22
io::{stdout, ErrorKind, Read, Write},
3-
thread::sleep,
43
time::Duration,
54
};
65

@@ -12,6 +11,8 @@ use espmonitor::{handle_serial, load_bin_context, SerialState};
1211
use miette::{IntoDiagnostic, Result};
1312
use serialport::SerialPort;
1413

14+
use crate::connection::reset_after_flash;
15+
1516
/// Converts key events from crossterm into appropriate character/escape
1617
/// sequences which are then sent over the serial connection.
1718
///
@@ -82,7 +83,7 @@ impl Drop for RawModeGuard {
8283
}
8384
}
8485

85-
pub fn monitor(mut serial: Box<dyn SerialPort>, elf: &[u8]) -> serialport::Result<()> {
86+
pub fn monitor(mut serial: Box<dyn SerialPort>, elf: &[u8], pid: u16) -> serialport::Result<()> {
8687
println!("Commands:");
8788
println!(" CTRL+R Reset chip");
8889
println!(" CTRL+C Exit");
@@ -125,12 +126,7 @@ pub fn monitor(mut serial: Box<dyn SerialPort>, elf: &[u8]) -> serialport::Resul
125126
// https://github.com/crossterm-rs/crossterm/pull/629
126127
KeyCode::Char('c') | KeyCode::Char('C') => break,
127128
KeyCode::Char('r') | KeyCode::Char('R') => {
128-
serial.write_data_terminal_ready(false)?;
129-
serial.write_request_to_send(true)?;
130-
131-
sleep(Duration::from_millis(100));
132-
133-
serial.write_request_to_send(false)?;
129+
reset_after_flash(&mut *serial, pid)?;
134130
continue;
135131
}
136132
_ => {}

espflash/src/connection.rs

+33-10
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,8 @@ impl Connection {
122122
}
123123

124124
pub fn reset(&mut self) -> Result<(), Error> {
125-
sleep(Duration::from_millis(100));
126-
127-
self.serial.write_data_terminal_ready(false)?;
128-
self.serial.write_request_to_send(true)?;
129-
130-
sleep(Duration::from_millis(100));
131-
132-
self.serial.write_request_to_send(false)?;
133-
134-
Ok(())
125+
let pid = self.port_info.pid;
126+
Ok(reset_after_flash(&mut *self.serial, pid)?)
135127
}
136128

137129
pub fn reset_to_flash(&mut self, extra_delay: bool) -> Result<(), Error> {
@@ -280,4 +272,35 @@ impl Connection {
280272
pub fn into_serial(self) -> Box<dyn SerialPort> {
281273
self.serial
282274
}
275+
276+
pub fn get_usb_pid(&self) -> Result<u16, Error> {
277+
Ok(self.port_info.pid)
278+
}
279+
}
280+
281+
pub fn reset_after_flash(serial: &mut dyn SerialPort, pid: u16) -> Result<(), serialport::Error> {
282+
sleep(Duration::from_millis(100));
283+
284+
if pid == USB_SERIAL_JTAG_PID {
285+
serial.write_data_terminal_ready(false)?;
286+
287+
sleep(Duration::from_millis(100));
288+
289+
serial.write_request_to_send(true)?;
290+
serial.write_data_terminal_ready(false)?;
291+
serial.write_request_to_send(true)?;
292+
293+
sleep(Duration::from_millis(100));
294+
295+
serial.write_request_to_send(false)?;
296+
} else {
297+
serial.write_data_terminal_ready(false)?;
298+
serial.write_request_to_send(true)?;
299+
300+
sleep(Duration::from_millis(100));
301+
302+
serial.write_request_to_send(false)?;
303+
}
304+
305+
Ok(())
283306
}

espflash/src/flasher.rs

+4
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,10 @@ impl Flasher {
521521
pub fn into_serial(self) -> Box<dyn SerialPort> {
522522
self.connection.into_serial()
523523
}
524+
525+
pub fn get_usb_pid(&self) -> Result<u16, Error> {
526+
self.connection.get_usb_pid()
527+
}
524528
}
525529

526530
pub(crate) fn get_erase_size(offset: usize, size: usize) -> usize {

espflash/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ fn flash(opts: Opts, config: Config) -> Result<()> {
134134
}
135135

136136
if opts.flash_opts.monitor {
137-
monitor(flasher.into_serial(), &elf_data).into_diagnostic()?;
137+
let pid = flasher.get_usb_pid()?;
138+
monitor(flasher.into_serial(), &elf_data, pid).into_diagnostic()?;
138139
}
139140

140141
Ok(())

0 commit comments

Comments
 (0)