Skip to content

bugfix: fix reset sequence for usb-serial-jtag chips #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ fn flash(
}

if opts.flash_opts.monitor {
monitor(flasher.into_serial(), &elf_data).into_diagnostic()?;
let pid = flasher.get_usb_pid()?;
monitor(flasher.into_serial(), &elf_data, pid).into_diagnostic()?;
}

Ok(())
Expand Down
12 changes: 4 additions & 8 deletions espflash/src/cli/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
io::{stdout, ErrorKind, Read, Write},
thread::sleep,
time::Duration,
};

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

use crate::connection::reset_after_flash;

/// Converts key events from crossterm into appropriate character/escape
/// sequences which are then sent over the serial connection.
///
Expand Down Expand Up @@ -82,7 +83,7 @@ impl Drop for RawModeGuard {
}
}

pub fn monitor(mut serial: Box<dyn SerialPort>, elf: &[u8]) -> serialport::Result<()> {
pub fn monitor(mut serial: Box<dyn SerialPort>, elf: &[u8], pid: u16) -> serialport::Result<()> {
println!("Commands:");
println!(" CTRL+R Reset chip");
println!(" CTRL+C Exit");
Expand Down Expand Up @@ -125,12 +126,7 @@ pub fn monitor(mut serial: Box<dyn SerialPort>, elf: &[u8]) -> serialport::Resul
// https://github.com/crossterm-rs/crossterm/pull/629
KeyCode::Char('c') | KeyCode::Char('C') => break,
KeyCode::Char('r') | KeyCode::Char('R') => {
serial.write_data_terminal_ready(false)?;
serial.write_request_to_send(true)?;

sleep(Duration::from_millis(100));

serial.write_request_to_send(false)?;
reset_after_flash(&mut *serial, pid)?;
continue;
}
_ => {}
Expand Down
43 changes: 33 additions & 10 deletions espflash/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,8 @@ impl Connection {
}

pub fn reset(&mut self) -> Result<(), Error> {
sleep(Duration::from_millis(100));

self.serial.write_data_terminal_ready(false)?;
self.serial.write_request_to_send(true)?;

sleep(Duration::from_millis(100));

self.serial.write_request_to_send(false)?;

Ok(())
let pid = self.port_info.pid;
Ok(reset_after_flash(&mut *self.serial, pid)?)
}

pub fn reset_to_flash(&mut self, extra_delay: bool) -> Result<(), Error> {
Expand Down Expand Up @@ -280,4 +272,35 @@ impl Connection {
pub fn into_serial(self) -> Box<dyn SerialPort> {
self.serial
}

pub fn get_usb_pid(&self) -> Result<u16, Error> {
Ok(self.port_info.pid)
}
}

pub fn reset_after_flash(serial: &mut dyn SerialPort, pid: u16) -> Result<(), serialport::Error> {
sleep(Duration::from_millis(100));

if pid == USB_SERIAL_JTAG_PID {
serial.write_data_terminal_ready(false)?;

sleep(Duration::from_millis(100));

serial.write_request_to_send(true)?;
serial.write_data_terminal_ready(false)?;
serial.write_request_to_send(true)?;

sleep(Duration::from_millis(100));

serial.write_request_to_send(false)?;
} else {
serial.write_data_terminal_ready(false)?;
serial.write_request_to_send(true)?;

sleep(Duration::from_millis(100));

serial.write_request_to_send(false)?;
}

Ok(())
}
4 changes: 4 additions & 0 deletions espflash/src/flasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ impl Flasher {
pub fn into_serial(self) -> Box<dyn SerialPort> {
self.connection.into_serial()
}

pub fn get_usb_pid(&self) -> Result<u16, Error> {
self.connection.get_usb_pid()
}
}

pub(crate) fn get_erase_size(offset: usize, size: usize) -> usize {
Expand Down
3 changes: 2 additions & 1 deletion espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ fn flash(opts: Opts, config: Config) -> Result<()> {
}

if opts.flash_opts.monitor {
monitor(flasher.into_serial(), &elf_data).into_diagnostic()?;
let pid = flasher.get_usb_pid()?;
monitor(flasher.into_serial(), &elf_data, pid).into_diagnostic()?;
}

Ok(())
Expand Down