Skip to content

Commit dd0b18d

Browse files
authored
Move construction of Connection outside of flasher (#882)
* Move construction of Connection outside of flasher * Move chip detect to Connection * Move more relevant things to Connection * Move more relevant things to Connection * use baud throughout the codebase * changelog
1 parent 56a23a7 commit dd0b18d

File tree

10 files changed

+114
-86
lines changed

10 files changed

+114
-86
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929
- `--after` options now work with `espflash board-info`, `espflash read-flash` and `espflash checksum-md5` (#867)
3030
- Add support for serial port configuration files. (#777)
3131
- Add a `check-app-descriptor` bool option to `ImageArgs` and add the flag to `flash` commad(#872)
32+
- `Connection::into_serial` to get the underlying port from the connection (#882)
3233

3334
### Changed
3435

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

5357
### Fixed
5458

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

6570
### Removed
6671

cargo-espflash/src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
397397
}
398398

399399
if args.flash_args.monitor {
400-
let pid = flasher.usb_pid();
400+
let pid = flasher.connection().usb_pid();
401401

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

411411
monitor_args.elf = Some(build_ctx.artifact_path);
412412

413-
monitor(flasher.into_serial(), Some(&elf_data), pid, monitor_args)
413+
monitor(
414+
flasher.into_connection().into_serial(),
415+
Some(&elf_data),
416+
pid,
417+
monitor_args,
418+
)
414419
} else {
415420
Ok(())
416421
}

espflash/src/bin/espflash.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
309309
}
310310

311311
if args.flash_args.monitor {
312-
let pid = flasher.usb_pid();
312+
let pid = flasher.connection().usb_pid();
313313

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

323323
monitor_args.elf = Some(args.image);
324324

325-
monitor(flasher.into_serial(), Some(&elf_data), pid, monitor_args)
325+
monitor(
326+
flasher.into_connection().into_serial(),
327+
Some(&elf_data),
328+
pid,
329+
monitor_args,
330+
)
326331
} else {
327332
Ok(())
328333
}

espflash/src/cli/mod.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ use self::{
3434
monitor::{LogFormat, check_monitor_args, monitor},
3535
};
3636
use crate::{
37-
connection::reset::{ResetAfterOperation, ResetBeforeOperation},
37+
connection::{
38+
Connection,
39+
reset::{ResetAfterOperation, ResetBeforeOperation},
40+
},
3841
error::{Error, MissingPartition, MissingPartitionTable},
3942
flasher::{
4043
FLASH_SECTOR_SIZE,
@@ -435,16 +438,21 @@ pub fn connect(
435438
_ => unreachable!(),
436439
};
437440

438-
Ok(Flasher::connect(
441+
let connection = Connection::new(
439442
*Box::new(serial_port),
440443
port_info,
441-
args.baud.or(config.project_config.baudrate),
444+
args.after,
445+
args.before,
446+
args.baud
447+
.or(config.project_config.baudrate)
448+
.unwrap_or(115_200),
449+
);
450+
Ok(Flasher::connect(
451+
connection,
442452
!args.no_stub,
443453
!no_verify,
444454
!no_skip,
445455
args.chip,
446-
args.after,
447-
args.before,
448456
)?)
449457
}
450458

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

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

649-
monitor(flasher.into_serial(), elf.as_deref(), pid, monitor_args)
657+
monitor(
658+
flasher.into_connection().into_serial(),
659+
elf.as_deref(),
660+
pid,
661+
monitor_args,
662+
)
650663
}
651664

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

11131126
if args.monitor {
1114-
let pid = flasher.usb_pid();
1127+
let pid = flasher.connection().usb_pid();
11151128
let mut monitor_args = args.monitor_args;
1116-
11171129
if chip == Chip::Esp32c2
11181130
&& target_xtal_freq == XtalFrequency::_26Mhz
11191131
&& monitor_args.monitor_baud == 115_200
11201132
{
1121-
// 115_200 * 26 MHz / 40 MHz = 74_880
11221133
monitor_args.monitor_baud = 74_880;
11231134
}
1124-
1125-
monitor(flasher.into_serial(), None, pid, monitor_args)?;
1135+
monitor(
1136+
flasher.into_connection().into_serial(),
1137+
None,
1138+
pid,
1139+
monitor_args,
1140+
)?;
11261141
}
11271142

11281143
Ok(())

espflash/src/connection/mod.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use self::{
3535
};
3636
use crate::{
3737
error::{ConnectionError, Error, ResultExt, RomError, RomErrorKind},
38+
flasher::stubs::CHIP_DETECT_MAGIC_REG_ADDR,
3839
targets::Chip,
3940
};
4041

@@ -134,6 +135,7 @@ pub struct Connection {
134135
after_operation: ResetAfterOperation,
135136
before_operation: ResetBeforeOperation,
136137
pub(crate) secure_download_mode: bool,
138+
pub(crate) baud: u32,
137139
}
138140

139141
impl Connection {
@@ -143,6 +145,7 @@ impl Connection {
143145
port_info: UsbPortInfo,
144146
after_operation: ResetAfterOperation,
145147
before_operation: ResetBeforeOperation,
148+
baud: u32,
146149
) -> Self {
147150
Connection {
148151
serial,
@@ -151,6 +154,7 @@ impl Connection {
151154
after_operation,
152155
before_operation,
153156
secure_download_mode: false,
157+
baud,
154158
}
155159
}
156160

@@ -388,15 +392,15 @@ impl Connection {
388392
}
389393

390394
/// Set baud rate for the serial port.
391-
pub fn set_baud(&mut self, speed: u32) -> Result<(), Error> {
392-
self.serial.set_baud_rate(speed)?;
393-
395+
pub fn set_baud(&mut self, baud: u32) -> Result<(), Error> {
396+
self.serial.set_baud_rate(baud)?;
397+
self.baud = baud;
394398
Ok(())
395399
}
396400

397401
/// Get the current baud rate of the serial port.
398-
pub fn baud(&self) -> Result<u32, Error> {
399-
Ok(self.serial.baud_rate()?)
402+
pub fn baud(&self) -> u32 {
403+
self.baud
400404
}
401405

402406
/// Run a command with a timeout defined by the command type.
@@ -618,6 +622,33 @@ impl Connection {
618622
pub(crate) fn is_using_usb_serial_jtag(&self) -> bool {
619623
self.port_info.pid == USB_SERIAL_JTAG_PID
620624
}
625+
626+
pub fn after_operation(&self) -> ResetAfterOperation {
627+
self.after_operation
628+
}
629+
pub fn before_operation(&self) -> ResetBeforeOperation {
630+
self.before_operation
631+
}
632+
633+
/// Detect which chip is connected to this connection
634+
pub fn detect_chip(
635+
&mut self,
636+
use_stub: bool,
637+
) -> Result<crate::targets::Chip, crate::error::Error> {
638+
// Try to read the magic value from the chip
639+
let magic = if use_stub {
640+
self.with_timeout(CommandType::ReadReg.timeout(), |connection| {
641+
connection.command(Command::ReadReg {
642+
address: CHIP_DETECT_MAGIC_REG_ADDR,
643+
})
644+
})?
645+
.try_into()?
646+
} else {
647+
self.read_reg(CHIP_DETECT_MAGIC_REG_ADDR)?
648+
};
649+
debug!("Read chip magic value: 0x{:08x}", magic);
650+
Chip::from_magic(magic)
651+
}
621652
}
622653

623654
mod encoder {

0 commit comments

Comments
 (0)