Skip to content

Commit

Permalink
Fix monitor after flash for ESP32-C2 (#330)
Browse files Browse the repository at this point in the history
* Fix espflash flashing

* Make sure to not read the xtal-freq after reset

* Fix cargo-espflash flashing
  • Loading branch information
bjoernQ authored Jan 5, 2023
1 parent 5727ed5 commit a0a9aef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
34 changes: 15 additions & 19 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use espflash::{
cli::{
self, board_info, clap_enum_variants, config::Config, connect, erase_partitions,
flash_elf_image, monitor::monitor, parse_partition_table, partition_table,
save_elf_as_image, serial_monitor, ConnectArgs, FlashConfigArgs, MonitorArgs,
PartitionTableArgs,
print_board_info, save_elf_as_image, serial_monitor, ConnectArgs, FlashConfigArgs,
MonitorArgs, PartitionTableArgs,
},
image_format::ImageFormatKind,
logging::initialize_logger,
Expand Down Expand Up @@ -159,13 +159,14 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
let cargo_config = CargoConfig::load(&metadata.workspace_root, &metadata.package_root);

let mut flasher = connect(&args.connect_args, config)?;
let build_ctx = build(&args.build_args, &cargo_config, flasher.chip())
.wrap_err("Failed to build project")?;
print_board_info(&mut flasher)?;

// Print the board information once the project has successfully built. We do
// here rather than upon connection to show the Cargo output prior to the board
// information, rather than breaking up cargo-espflash's output.
board_info(&args.connect_args, config)?;
let chip = flasher.chip();
let target = chip.into_target();
let target_xtal_freq = target.crystal_freq(&mut flasher.connection())?;

let build_ctx =
build(&args.build_args, &cargo_config, chip).wrap_err("Failed to build project")?;

// Read the ELF data from the build path and load it to the target.
let elf_data = fs::read(build_ctx.artifact_path).into_diagnostic()?;
Expand Down Expand Up @@ -216,18 +217,13 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
if args.flash_args.monitor {
let pid = flasher.get_usb_pid()?;

let chip = flasher.chip();
let target = chip.into_target();

// The 26MHz ESP32-C2's need to be treated as a special case.
let default_baud = if chip == Chip::Esp32c2
&& !args.connect_args.use_stub
&& target.crystal_freq(&mut flasher.connection())? == 26
{
74_880
} else {
115_200
};
let default_baud =
if chip == Chip::Esp32c2 && !args.connect_args.use_stub && target_xtal_freq == 26 {
74_880
} else {
115_200
};

monitor(
flasher.into_interface(),
Expand Down
27 changes: 13 additions & 14 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use espflash::{
cli::{
self, board_info, build_progress_bar_callback, clap_enum_variants, config::Config, connect,
erase_partitions, flash_elf_image, monitor::monitor, parse_partition_table,
partition_table, progress_bar, save_elf_as_image, serial_monitor, ConnectArgs,
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
partition_table, print_board_info, progress_bar, save_elf_as_image, serial_monitor,
ConnectArgs, FlashConfigArgs, MonitorArgs, PartitionTableArgs,
},
image_format::ImageFormatKind,
logging::initialize_logger,
Expand Down Expand Up @@ -117,7 +117,11 @@ fn main() -> Result<()> {

fn flash(args: FlashArgs, config: &Config) -> Result<()> {
let mut flasher = connect(&args.connect_args, config)?;
board_info(&args.connect_args, config)?;
print_board_info(&mut flasher)?;

let chip = flasher.chip();
let target = chip.into_target();
let target_xtal_freq = target.crystal_freq(&mut flasher.connection())?;

// Read the ELF data from the build path and load it to the target.
let elf_data = fs::read(&args.image).into_diagnostic()?;
Expand Down Expand Up @@ -155,18 +159,13 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
if args.flash_args.monitor {
let pid = flasher.get_usb_pid()?;

let chip = flasher.chip();
let target = chip.into_target();

// The 26MHz ESP32-C2's need to be treated as a special case.
let default_baud = if chip == Chip::Esp32c2
&& !args.connect_args.use_stub
&& target.crystal_freq(&mut flasher.connection())? == 26
{
74_880
} else {
115_200
};
let default_baud =
if chip == Chip::Esp32c2 && !args.connect_args.use_stub && target_xtal_freq == 26 {
74_880
} else {
115_200
};

monitor(
flasher.into_interface(),
Expand Down
7 changes: 7 additions & 0 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ pub fn connect(args: &ConnectArgs, config: &Config) -> Result<Flasher> {
/// Connect to a target device and print information about its chip
pub fn board_info(args: &ConnectArgs, config: &Config) -> Result<()> {
let mut flasher = connect(&args, config)?;
print_board_info(&mut flasher)?;

Ok(())
}

/// Print information about a chip
pub fn print_board_info(flasher: &mut Flasher) -> Result<()> {
let info = flasher.device_info()?;

print!("Chip type: {}", info.chip);
Expand Down

0 comments on commit a0a9aef

Please sign in to comment.