Skip to content

Commit

Permalink
Merge branch 'main' into fix/resets
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioGasquez authored Nov 6, 2023
2 parents 0cf9953 + 39fbdac commit f9cdb23
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 72 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added reset strategies (#487)

- Read esp-println generated defmt messages (#466)
- Add --target-app-partition argument to flash command (#461)

### Fixed

Expand Down
43 changes: 9 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions cargo-espflash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,3 @@ cargo = { version = "0.73.1", features = ["vendored-openssl"] }

[target.'cfg(windows)'.dependencies]
cargo = "0.73.1"

[features]
defmt = ["espflash/defmt"]
15 changes: 9 additions & 6 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::{

use cargo_metadata::Message;
use clap::{Args, CommandFactory, Parser, Subcommand};
use espflash::cli::{erase_flash, erase_region, EraseFlashArgs, EraseRegionArgs};
use espflash::{
cli::{
self, board_info, completions, config::Config, connect, erase_partitions, flash_elf_image,
monitor::monitor, parse_partition_table, partition_table, print_board_info,
save_elf_as_image, serial_monitor, CompletionsArgs, ConnectArgs, EspflashProgress,
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
self, board_info, completions, config::Config, connect, erase_flash, erase_partitions,
erase_region, flash_elf_image, monitor::monitor_with, parse_partition_table,
partition_table, print_board_info, save_elf_as_image, serial_monitor, CompletionsArgs,
ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs,
MonitorArgs, PartitionTableArgs,
},
error::Error as EspflashError,
image_format::ImageFormatKind,
Expand Down Expand Up @@ -323,6 +323,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
&elf_data,
bootloader,
partition_table,
args.flash_args.target_app_partition,
args.flash_args.format.or(metadata.format),
args.build_args.flash_config_args.flash_mode,
args.build_args.flash_config_args.flash_size,
Expand All @@ -341,11 +342,12 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
115_200
};

monitor(
monitor_with(
flasher.into_interface(),
Some(&elf_data),
pid,
args.flash_args.monitor_baud.unwrap_or(default_baud),
args.flash_args.log_format,
)
.into_diagnostic()?;
}
Expand Down Expand Up @@ -563,6 +565,7 @@ fn save_image(args: SaveImageArgs) -> Result<()> {
args.save_image_args.merge,
bootloader,
partition_table,
args.save_image_args.target_app_partition,
args.save_image_args.skip_padding,
)?;

Expand Down
1 change: 1 addition & 0 deletions espflash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ clap_complete = { version = "4.4.3", optional = true }
comfy-table = { version = "7.0.1", optional = true }
crossterm = { version = "0.25.0", optional = true } # 0.26.x causes issues on Windows
ctrlc = { version = "3.4.0", optional = true }
# defmt dependencies are pinned since defmt does not guarantee MSRV even for patch releases
defmt-decoder = { version = "=0.3.8", features = ["unstable"], optional = true }
defmt-parser = { version = "=0.3.3", features = ["unstable"], optional = true }
dialoguer = { version = "0.10.4", optional = true }
Expand Down
7 changes: 5 additions & 2 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use clap::{Args, CommandFactory, Parser, Subcommand};
use espflash::{
cli::{
self, board_info, completions, config::Config, connect, erase_flash, erase_partitions,
erase_region, flash_elf_image, monitor::monitor, parse_partition_table, parse_uint32,
erase_region, flash_elf_image, monitor::monitor_with, parse_partition_table, parse_uint32,
partition_table, print_board_info, save_elf_as_image, serial_monitor, CompletionsArgs,
ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs,
MonitorArgs, PartitionTableArgs,
Expand Down Expand Up @@ -245,6 +245,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
&elf_data,
bootloader,
partition_table,
args.flash_args.target_app_partition,
args.flash_args.format,
args.flash_config_args.flash_mode,
args.flash_config_args.flash_size,
Expand All @@ -263,11 +264,12 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
115_200
};

monitor(
monitor_with(
flasher.into_interface(),
Some(&elf_data),
pid,
args.flash_args.monitor_baud.unwrap_or(default_baud),
args.flash_args.log_format,
)
.into_diagnostic()?;
}
Expand Down Expand Up @@ -306,6 +308,7 @@ fn save_image(args: SaveImageArgs) -> Result<()> {
args.save_image_args.merge,
args.save_image_args.bootloader,
args.save_image_args.partition_table,
args.save_image_args.target_app_partition,
args.save_image_args.skip_padding,
)?;

Expand Down
26 changes: 24 additions & 2 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ use log::{debug, info};
use miette::{IntoDiagnostic, Result, WrapErr};
use serialport::{SerialPortType, UsbPortInfo};

use self::{config::Config, monitor::monitor, serial::get_serial_port_info};
use self::{
config::Config,
monitor::{monitor_with, LogFormat},
serial::get_serial_port_info,
};
use crate::{
elf::ElfFirmwareImage,
error::{Error, MissingPartition, MissingPartitionTable},
Expand Down Expand Up @@ -131,9 +135,15 @@ pub struct FlashArgs {
/// Path to a CSV file containing partition table
#[arg(long, value_name = "FILE")]
pub partition_table: Option<PathBuf>,
/// Label of target app partition
#[arg(long, value_name = "LABEL")]
pub target_app_partition: Option<String>,
/// Load the application to RAM instead of Flash
#[arg(long)]
pub ram: bool,
/// Logging format.
#[arg(long, short = 'f', default_value = "serial", requires = "monitor")]
pub log_format: LogFormat,
}

/// Operations for partitions tables
Expand Down Expand Up @@ -171,6 +181,9 @@ pub struct SaveImageArgs {
/// Custom partition table for merging
#[arg(long, short = 'T', requires = "merge", value_name = "FILE")]
pub partition_table: Option<PathBuf>,
/// Label of target app partition
#[arg(long, value_name = "LABEL")]
pub target_app_partition: Option<String>,
/// Don't pad the image to the flash size
#[arg(long, short = 'P', requires = "merge")]
pub skip_padding: bool,
Expand All @@ -185,6 +198,9 @@ pub struct MonitorArgs {
/// Connection configuration
#[clap(flatten)]
connect_args: ConnectArgs,
/// Logging format.
#[arg(long, short = 'f', default_value = "serial")]
pub log_format: LogFormat,
}

/// Select a serial port and establish a connection with a target device
Expand Down Expand Up @@ -291,11 +307,12 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> {
115_200
};

monitor(
monitor_with(
flasher.into_interface(),
elf.as_deref(),
pid,
args.connect_args.baud.unwrap_or(default_baud),
args.log_format,
)
.into_diagnostic()?;

Expand All @@ -314,6 +331,7 @@ pub fn save_elf_as_image(
merge: bool,
bootloader_path: Option<PathBuf>,
partition_table_path: Option<PathBuf>,
target_app_partition: Option<String>,
skip_padding: bool,
) -> Result<()> {
let image = ElfFirmwareImage::try_from(elf_data)?;
Expand Down Expand Up @@ -355,6 +373,7 @@ pub fn save_elf_as_image(
&image,
bootloader,
partition_table,
target_app_partition,
image_format,
None,
flash_mode,
Expand Down Expand Up @@ -395,6 +414,7 @@ pub fn save_elf_as_image(
&image,
None,
None,
None,
image_format,
None,
flash_mode,
Expand Down Expand Up @@ -508,6 +528,7 @@ pub fn flash_elf_image(
elf_data: &[u8],
bootloader: Option<&Path>,
partition_table: Option<PartitionTable>,
target_app_partition: Option<String>,
image_format: Option<ImageFormatKind>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
Expand All @@ -530,6 +551,7 @@ pub fn flash_elf_image(
elf_data,
bootloader,
partition_table,
target_app_partition,
image_format,
flash_mode,
flash_size,
Expand Down
35 changes: 25 additions & 10 deletions espflash/src/cli/monitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crossterm::{
};
use log::error;
use miette::{IntoDiagnostic, Result};
use strum::{Display, EnumIter, EnumString, EnumVariantNames};

use crate::{
cli::monitor::parser::{InputParser, ResolvingPrinter},
Expand All @@ -33,6 +34,17 @@ pub mod parser;
mod line_endings;
mod symbols;

#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, EnumIter, EnumString, EnumVariantNames)]
#[non_exhaustive]
#[strum(serialize_all = "lowercase")]
pub enum LogFormat {
/// defmt
Defmt,
/// serial
Serial,
}

/// Type that ensures that raw mode is disabled when dropped.
struct RawModeGuard;

Expand All @@ -58,22 +70,16 @@ pub fn monitor(
pid: u16,
baud: u32,
) -> serialport::Result<()> {
#[cfg(feature = "defmt")]
let parser = parser::esp_defmt::EspDefmt::new(elf);

#[cfg(not(feature = "defmt"))]
let parser = parser::serial::Serial;

monitor_with(serial, elf, pid, baud, parser)
monitor_with(serial, elf, pid, baud, LogFormat::Serial)
}

/// Open a serial monitor on the given interface, using the given input parser.
pub fn monitor_with<L: InputParser>(
pub fn monitor_with(
mut serial: Interface,
elf: Option<&[u8]>,
pid: u16,
baud: u32,
mut parser: L,
log_format: LogFormat,
) -> serialport::Result<()> {
println!("Commands:");
println!(" CTRL+R Reset chip");
Expand Down Expand Up @@ -102,7 +108,16 @@ pub fn monitor_with<L: InputParser>(
err => err,
}?;

parser.feed(&buff[0..read_count], &mut stdout);
match log_format {
LogFormat::Defmt => {
let mut parser = parser::esp_defmt::EspDefmt::new(elf);
parser.feed(&buff[0..read_count], &mut stdout);
}
LogFormat::Serial => {
let mut parser = parser::serial::Serial;
parser.feed(&buff[0..read_count], &mut stdout);
}
}

// Don't forget to flush the writer!
stdout.flush().ok();
Expand Down
1 change: 0 additions & 1 deletion espflash/src/cli/monitor/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(feature = "defmt")]
pub mod esp_defmt;
pub mod serial;

Expand Down
Loading

0 comments on commit f9cdb23

Please sign in to comment.