Skip to content
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

Fix USB JTAG Serial HardReset sequence in Windows #594

Merged
merged 5 commits into from
Feb 21, 2024
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Fixed
- Change the `hard_reset` sequence to fix Windows issues (#594)

### Changed

- `FlashData::new` now returns `crate::Error` (#591)
- Moved `reset_after_flash` method to `reset` module (#594)

### Removed

Expand Down
9 changes: 5 additions & 4 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,13 @@ pub fn erase_flash(args: EraseFlashArgs, config: &Config) -> Result<()> {
return Err(Error::StubRequired.into());
}

let mut flash = connect(&args.connect_args, config, true, true)?;

let mut flasher = connect(&args.connect_args, config, true, true)?;
info!("Erasing Flash...");

flash.erase_flash()?;
flash.connection().reset_after(!args.connect_args.no_stub)?;
flasher.erase_flash()?;
flasher
.connection()
.reset_after(!args.connect_args.no_stub)?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion espflash/src/cli/monitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use strum::{Display, EnumIter, EnumString, VariantNames};

use crate::{
cli::monitor::parser::{InputParser, ResolvingPrinter},
connection::{reset_after_flash, Port},
connection::{reset::reset_after_flash, Port},
};

pub mod parser;
Expand Down
35 changes: 5 additions & 30 deletions espflash/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use self::reset::UnixTightReset;
use self::{
encoder::SlipEncoder,
reset::{
construct_reset_strategy_sequence, ClassicReset, HardReset, ResetAfterOperation,
ResetBeforeOperation, ResetStrategy, UsbJtagSerialReset,
construct_reset_strategy_sequence, hard_reset, reset_after_flash, ClassicReset,
ResetAfterOperation, ResetBeforeOperation, ResetStrategy, UsbJtagSerialReset,
},
};
use crate::{
Expand Down Expand Up @@ -256,8 +256,10 @@ impl Connection {

// Reset the device taking into account the reset after argument
pub fn reset_after(&mut self, is_stub: bool) -> Result<(), Error> {
let pid = self.get_usb_pid()?;

match self.after_operation {
ResetAfterOperation::HardReset => HardReset.reset(&mut self.serial),
ResetAfterOperation::HardReset => hard_reset(&mut self.serial, pid),
ResetAfterOperation::NoReset => {
info!("Staying in bootloader");
soft_reset(self, true, is_stub)?;
Expand Down Expand Up @@ -486,33 +488,6 @@ impl Connection {
}
}

/// Reset the target device when flashing has completed
pub fn reset_after_flash(serial: &mut Port, 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_request_to_send(true)?;

sleep(Duration::from_millis(100));

serial.write_request_to_send(false)?;
}

Ok(())
}

mod encoder {
use std::io::Write;

Expand Down
45 changes: 33 additions & 12 deletions espflash/src/connection/reset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This entire module is copied from `esptool.py` (https://github.com/espressif/esptool/blob/a8586d02b1305ebc687d31783437a7f4d4dbb70f/esptool/reset.py)
//! Most of this module is copied from `esptool.py` (https://github.com/espressif/esptool/blob/a8586d02b1305ebc687d31783437a7f4d4dbb70f/esptool/reset.py)

#[cfg(unix)]
use std::{io, os::fd::AsRawFd};
Expand Down Expand Up @@ -198,22 +198,43 @@ impl ResetStrategy for UsbJtagSerialReset {
}
}

/// Reset sequence for hard resetting the chip.
///
/// Can be used to reset out of the bootloader or to restart a running app.
#[derive(Debug, Clone, Copy)]
pub struct HardReset;
/// Reset the target device
pub fn reset_after_flash(serial: &mut Port, pid: u16) -> Result<(), serialport::Error> {
sleep(Duration::from_millis(100));

impl ResetStrategy for HardReset {
fn reset(&self, serial_port: &mut Port) -> Result<(), Error> {
debug!("Using HardReset reset strategy");
if pid == USB_SERIAL_JTAG_PID {
serial.write_data_terminal_ready(false)?;

self.set_rts(serial_port, true)?;
sleep(Duration::from_millis(100));
self.set_rts(serial_port, false)?;

Ok(())
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_request_to_send(true)?;

sleep(Duration::from_millis(100));

serial.write_request_to_send(false)?;
}

Ok(())
}

/// Reset sequence for hard resetting the chip.
pub fn hard_reset(serial_port: &mut Port, pid: u16) -> Result<(), Error> {
debug!("Using HardReset reset strategy");

// Using esptool HardReset strategy (https://github.com/espressif/esptool/blob/3301d0ff4638d4db1760a22540dbd9d07c55ec37/esptool/reset.py#L132-L153)
// leads to https://github.com/esp-rs/espflash/issues/592 in Windows, using `reset_after_flash` instead works fine for all platforms.
// We had similar issues in the past: https://github.com/esp-rs/espflash/pull/157
reset_after_flash(serial_port, pid)?;

Ok(())
}

/// Perform a soft reset of the device.
Expand Down
Loading