Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `--after` options now work with `espflash board-info`, `espflash read-flash` and `espflash checksum-md5` (#867)
- Add support for serial port configuration files. (#777)
- Add a `check-app-descriptor` bool option to `ImageArgs` and add the flag to `flash` commad(#872)
- `Connection::into_serial` to get the underlying port from the connection (#882)

### Changed

Expand All @@ -49,6 +50,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated bootloaders with `release/v5.4` ones from IDF (#857)
- Refactor image formatting to allow supporting more image formats in a backward compatible way (#877)
- Avoid having ESP-IDF format assumptions in the codebase (#877)
- `Flasher` now takes the `Connection` in new, instead of constructing the connection inside `Flasher::connect` (#882)
- `detect_chip` has moved to the `Connection` struct (#882)
- `Flasher::into_serial` has been replaced by `Flasher::into_connection` (#882)

### Fixed

Expand All @@ -61,6 +65,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a case where esplash transformed the firmware elf in a way that made it unbootable (#831)
- The app descriptor is now correctly placed in the front of the bianry (#835)
- espflash now extracts the MMU page size from the app descriptor (#835)
- `ResetBeforeOperation` & `ResetAfterOperation` are now public, to allow the creation of a `Connection` (#882)

### Removed

Expand Down
9 changes: 7 additions & 2 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
}

if args.flash_args.monitor {
let pid = flasher.usb_pid();
let pid = flasher.connection().usb_pid();

// The 26MHz ESP32-C2's need to be treated as a special case.
if chip == Chip::Esp32c2
Expand All @@ -410,7 +410,12 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {

monitor_args.elf = Some(build_ctx.artifact_path);

monitor(flasher.into_serial(), Some(&elf_data), pid, monitor_args)
monitor(
flasher.into_connection().into_serial(),
Some(&elf_data),
pid,
monitor_args,
)
} else {
Ok(())
}
Expand Down
9 changes: 7 additions & 2 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
}

if args.flash_args.monitor {
let pid = flasher.usb_pid();
let pid = flasher.connection().usb_pid();

// The 26MHz ESP32-C2's need to be treated as a special case.
if chip == Chip::Esp32c2
Expand All @@ -322,7 +322,12 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {

monitor_args.elf = Some(args.image);

monitor(flasher.into_serial(), Some(&elf_data), pid, monitor_args)
monitor(
flasher.into_connection().into_serial(),
Some(&elf_data),
pid,
monitor_args,
)
} else {
Ok(())
}
Expand Down
39 changes: 27 additions & 12 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ use self::{
monitor::{LogFormat, check_monitor_args, monitor},
};
use crate::{
connection::reset::{ResetAfterOperation, ResetBeforeOperation},
connection::{
Connection,
reset::{ResetAfterOperation, ResetBeforeOperation},
},
error::{Error, MissingPartition, MissingPartitionTable},
flasher::{
FLASH_SECTOR_SIZE,
Expand Down Expand Up @@ -435,16 +438,21 @@ pub fn connect(
_ => unreachable!(),
};

Ok(Flasher::connect(
let connection = Connection::new(
*Box::new(serial_port),
port_info,
args.baud.or(config.project_config.baudrate),
args.after,
args.before,
args.baud
.or(config.project_config.baudrate)
.unwrap_or(115_200),
);
Ok(Flasher::connect(
connection,
!args.no_stub,
!no_verify,
!no_skip,
args.chip,
args.after,
args.before,
)?)
}

Expand Down Expand Up @@ -618,7 +626,7 @@ pub fn print_board_info(flasher: &mut Flasher) -> Result<()> {
/// Open a serial monitor
pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> {
let mut flasher = connect(&args.connect_args, config, true, true)?;
let pid = flasher.usb_pid();
let pid = flasher.connection().usb_pid();

let elf = if let Some(elf_path) = args.monitor_args.elf.clone() {
let path = fs::canonicalize(elf_path).into_diagnostic()?;
Expand Down Expand Up @@ -646,7 +654,12 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> {
monitor_args.monitor_baud = 74_880;
}

monitor(flasher.into_serial(), elf.as_deref(), pid, monitor_args)
monitor(
flasher.into_connection().into_serial(),
elf.as_deref(),
pid,
monitor_args,
)
}

/// Convert the provided firmware image from ELF to binary
Expand Down Expand Up @@ -1111,18 +1124,20 @@ pub fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> {
)?;

if args.monitor {
let pid = flasher.usb_pid();
let pid = flasher.connection().usb_pid();
let mut monitor_args = args.monitor_args;

if chip == Chip::Esp32c2
&& target_xtal_freq == XtalFrequency::_26Mhz
&& monitor_args.monitor_baud == 115_200
{
// 115_200 * 26 MHz / 40 MHz = 74_880
monitor_args.monitor_baud = 74_880;
}

monitor(flasher.into_serial(), None, pid, monitor_args)?;
monitor(
flasher.into_connection().into_serial(),
None,
pid,
monitor_args,
)?;
}

Ok(())
Expand Down
41 changes: 36 additions & 5 deletions espflash/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use self::{
};
use crate::{
error::{ConnectionError, Error, ResultExt, RomError, RomErrorKind},
flasher::stubs::CHIP_DETECT_MAGIC_REG_ADDR,
targets::Chip,
};

Expand Down Expand Up @@ -134,6 +135,7 @@ pub struct Connection {
after_operation: ResetAfterOperation,
before_operation: ResetBeforeOperation,
pub(crate) secure_download_mode: bool,
pub(crate) baud: u32,
}

impl Connection {
Expand All @@ -143,6 +145,7 @@ impl Connection {
port_info: UsbPortInfo,
after_operation: ResetAfterOperation,
before_operation: ResetBeforeOperation,
baud: u32,
) -> Self {
Connection {
serial,
Expand All @@ -151,6 +154,7 @@ impl Connection {
after_operation,
before_operation,
secure_download_mode: false,
baud,
}
}

Expand Down Expand Up @@ -388,15 +392,15 @@ impl Connection {
}

/// Set baud rate for the serial port.
pub fn set_baud(&mut self, speed: u32) -> Result<(), Error> {
self.serial.set_baud_rate(speed)?;

pub fn set_baud(&mut self, baud: u32) -> Result<(), Error> {
self.serial.set_baud_rate(baud)?;
self.baud = baud;
Ok(())
}

/// Get the current baud rate of the serial port.
pub fn baud(&self) -> Result<u32, Error> {
Ok(self.serial.baud_rate()?)
pub fn baud(&self) -> u32 {
self.baud
}

/// Run a command with a timeout defined by the command type.
Expand Down Expand Up @@ -618,6 +622,33 @@ impl Connection {
pub(crate) fn is_using_usb_serial_jtag(&self) -> bool {
self.port_info.pid == USB_SERIAL_JTAG_PID
}

pub fn after_operation(&self) -> ResetAfterOperation {
self.after_operation
}
pub fn before_operation(&self) -> ResetBeforeOperation {
self.before_operation
}

/// Detect which chip is connected to this connection
pub fn detect_chip(
&mut self,
use_stub: bool,
) -> Result<crate::targets::Chip, crate::error::Error> {
// Try to read the magic value from the chip
let magic = if use_stub {
self.with_timeout(CommandType::ReadReg.timeout(), |connection| {
connection.command(Command::ReadReg {
address: CHIP_DETECT_MAGIC_REG_ADDR,
})
})?
.try_into()?
} else {
self.read_reg(CHIP_DETECT_MAGIC_REG_ADDR)?
};
debug!("Read chip magic value: 0x{:08x}", magic);
Chip::from_magic(magic)
}
}

mod encoder {
Expand Down
Loading