Skip to content

Commit 843e8ab

Browse files
Merge branch 'main' into fix/resets
2 parents 0cf9953 + 39fbdac commit 843e8ab

21 files changed

+133
-73
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Added reset strategies (#487)
1212

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

1516
### Fixed
1617

Cargo.lock

+9-34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-espflash/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,3 @@ cargo = { version = "0.73.1", features = ["vendored-openssl"] }
3131

3232
[target.'cfg(windows)'.dependencies]
3333
cargo = "0.73.1"
34-
35-
[features]
36-
defmt = ["espflash/defmt"]

cargo-espflash/src/main.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use std::{
66

77
use cargo_metadata::Message;
88
use clap::{Args, CommandFactory, Parser, Subcommand};
9-
use espflash::cli::{erase_flash, erase_region, EraseFlashArgs, EraseRegionArgs};
109
use espflash::{
1110
cli::{
12-
self, board_info, completions, config::Config, connect, erase_partitions, flash_elf_image,
13-
monitor::monitor, parse_partition_table, partition_table, print_board_info,
14-
save_elf_as_image, serial_monitor, CompletionsArgs, ConnectArgs, EspflashProgress,
15-
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
11+
self, board_info, completions, config::Config, connect, erase_flash, erase_partitions,
12+
erase_region, flash_elf_image, monitor::monitor_with, parse_partition_table,
13+
partition_table, print_board_info, save_elf_as_image, serial_monitor, CompletionsArgs,
14+
ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs,
15+
MonitorArgs, PartitionTableArgs,
1616
},
1717
error::Error as EspflashError,
1818
image_format::ImageFormatKind,
@@ -323,6 +323,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
323323
&elf_data,
324324
bootloader,
325325
partition_table,
326+
args.flash_args.target_app_partition,
326327
args.flash_args.format.or(metadata.format),
327328
args.build_args.flash_config_args.flash_mode,
328329
args.build_args.flash_config_args.flash_size,
@@ -341,11 +342,12 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
341342
115_200
342343
};
343344

344-
monitor(
345+
monitor_with(
345346
flasher.into_interface(),
346347
Some(&elf_data),
347348
pid,
348349
args.flash_args.monitor_baud.unwrap_or(default_baud),
350+
args.flash_args.log_format,
349351
)
350352
.into_diagnostic()?;
351353
}
@@ -563,6 +565,7 @@ fn save_image(args: SaveImageArgs) -> Result<()> {
563565
args.save_image_args.merge,
564566
bootloader,
565567
partition_table,
568+
args.save_image_args.target_app_partition,
566569
args.save_image_args.skip_padding,
567570
)?;
568571

espflash/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ clap_complete = { version = "4.4.3", optional = true }
3232
comfy-table = { version = "7.0.1", optional = true }
3333
crossterm = { version = "0.25.0", optional = true } # 0.26.x causes issues on Windows
3434
ctrlc = { version = "3.4.0", optional = true }
35-
defmt-decoder = { version = "=0.3.8", features = ["unstable"], optional = true }
35+
# defmt dependencies are pinned since defmt does not guarantee MSRV even for patch releases
36+
defmt-decoder = { version = "=0.3.9", features = ["unstable"], optional = true }
3637
defmt-parser = { version = "=0.3.3", features = ["unstable"], optional = true }
3738
dialoguer = { version = "0.10.4", optional = true }
3839
directories = { version = "5.0.1", optional = true }

espflash/src/bin/espflash.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use clap::{Args, CommandFactory, Parser, Subcommand};
88
use espflash::{
99
cli::{
1010
self, board_info, completions, config::Config, connect, erase_flash, erase_partitions,
11-
erase_region, flash_elf_image, monitor::monitor, parse_partition_table, parse_uint32,
11+
erase_region, flash_elf_image, monitor::monitor_with, parse_partition_table, parse_uint32,
1212
partition_table, print_board_info, save_elf_as_image, serial_monitor, CompletionsArgs,
1313
ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs,
1414
MonitorArgs, PartitionTableArgs,
@@ -245,6 +245,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
245245
&elf_data,
246246
bootloader,
247247
partition_table,
248+
args.flash_args.target_app_partition,
248249
args.flash_args.format,
249250
args.flash_config_args.flash_mode,
250251
args.flash_config_args.flash_size,
@@ -263,11 +264,12 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
263264
115_200
264265
};
265266

266-
monitor(
267+
monitor_with(
267268
flasher.into_interface(),
268269
Some(&elf_data),
269270
pid,
270271
args.flash_args.monitor_baud.unwrap_or(default_baud),
272+
args.flash_args.log_format,
271273
)
272274
.into_diagnostic()?;
273275
}
@@ -306,6 +308,7 @@ fn save_image(args: SaveImageArgs) -> Result<()> {
306308
args.save_image_args.merge,
307309
args.save_image_args.bootloader,
308310
args.save_image_args.partition_table,
311+
args.save_image_args.target_app_partition,
309312
args.save_image_args.skip_padding,
310313
)?;
311314

espflash/src/cli/mod.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ use log::{debug, info};
2424
use miette::{IntoDiagnostic, Result, WrapErr};
2525
use serialport::{SerialPortType, UsbPortInfo};
2626

27-
use self::{config::Config, monitor::monitor, serial::get_serial_port_info};
27+
use self::{
28+
config::Config,
29+
monitor::{monitor_with, LogFormat},
30+
serial::get_serial_port_info,
31+
};
2832
use crate::{
2933
elf::ElfFirmwareImage,
3034
error::{Error, MissingPartition, MissingPartitionTable},
@@ -131,9 +135,15 @@ pub struct FlashArgs {
131135
/// Path to a CSV file containing partition table
132136
#[arg(long, value_name = "FILE")]
133137
pub partition_table: Option<PathBuf>,
138+
/// Label of target app partition
139+
#[arg(long, value_name = "LABEL")]
140+
pub target_app_partition: Option<String>,
134141
/// Load the application to RAM instead of Flash
135142
#[arg(long)]
136143
pub ram: bool,
144+
/// Logging format.
145+
#[arg(long, short = 'f', default_value = "serial", requires = "monitor")]
146+
pub log_format: LogFormat,
137147
}
138148

139149
/// Operations for partitions tables
@@ -171,6 +181,9 @@ pub struct SaveImageArgs {
171181
/// Custom partition table for merging
172182
#[arg(long, short = 'T', requires = "merge", value_name = "FILE")]
173183
pub partition_table: Option<PathBuf>,
184+
/// Label of target app partition
185+
#[arg(long, value_name = "LABEL")]
186+
pub target_app_partition: Option<String>,
174187
/// Don't pad the image to the flash size
175188
#[arg(long, short = 'P', requires = "merge")]
176189
pub skip_padding: bool,
@@ -185,6 +198,9 @@ pub struct MonitorArgs {
185198
/// Connection configuration
186199
#[clap(flatten)]
187200
connect_args: ConnectArgs,
201+
/// Logging format.
202+
#[arg(long, short = 'f', default_value = "serial")]
203+
pub log_format: LogFormat,
188204
}
189205

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

294-
monitor(
310+
monitor_with(
295311
flasher.into_interface(),
296312
elf.as_deref(),
297313
pid,
298314
args.connect_args.baud.unwrap_or(default_baud),
315+
args.log_format,
299316
)
300317
.into_diagnostic()?;
301318

@@ -314,6 +331,7 @@ pub fn save_elf_as_image(
314331
merge: bool,
315332
bootloader_path: Option<PathBuf>,
316333
partition_table_path: Option<PathBuf>,
334+
target_app_partition: Option<String>,
317335
skip_padding: bool,
318336
) -> Result<()> {
319337
let image = ElfFirmwareImage::try_from(elf_data)?;
@@ -355,6 +373,7 @@ pub fn save_elf_as_image(
355373
&image,
356374
bootloader,
357375
partition_table,
376+
target_app_partition,
358377
image_format,
359378
None,
360379
flash_mode,
@@ -395,6 +414,7 @@ pub fn save_elf_as_image(
395414
&image,
396415
None,
397416
None,
417+
None,
398418
image_format,
399419
None,
400420
flash_mode,
@@ -508,6 +528,7 @@ pub fn flash_elf_image(
508528
elf_data: &[u8],
509529
bootloader: Option<&Path>,
510530
partition_table: Option<PartitionTable>,
531+
target_app_partition: Option<String>,
511532
image_format: Option<ImageFormatKind>,
512533
flash_mode: Option<FlashMode>,
513534
flash_size: Option<FlashSize>,
@@ -530,6 +551,7 @@ pub fn flash_elf_image(
530551
elf_data,
531552
bootloader,
532553
partition_table,
554+
target_app_partition,
533555
image_format,
534556
flash_mode,
535557
flash_size,

espflash/src/cli/monitor/mod.rs

+25-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crossterm::{
2121
};
2222
use log::error;
2323
use miette::{IntoDiagnostic, Result};
24+
use strum::{Display, EnumIter, EnumString, EnumVariantNames};
2425

2526
use crate::{
2627
cli::monitor::parser::{InputParser, ResolvingPrinter},
@@ -33,6 +34,17 @@ pub mod parser;
3334
mod line_endings;
3435
mod symbols;
3536

37+
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
38+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, EnumIter, EnumString, EnumVariantNames)]
39+
#[non_exhaustive]
40+
#[strum(serialize_all = "lowercase")]
41+
pub enum LogFormat {
42+
/// defmt
43+
Defmt,
44+
/// serial
45+
Serial,
46+
}
47+
3648
/// Type that ensures that raw mode is disabled when dropped.
3749
struct RawModeGuard;
3850

@@ -58,22 +70,16 @@ pub fn monitor(
5870
pid: u16,
5971
baud: u32,
6072
) -> serialport::Result<()> {
61-
#[cfg(feature = "defmt")]
62-
let parser = parser::esp_defmt::EspDefmt::new(elf);
63-
64-
#[cfg(not(feature = "defmt"))]
65-
let parser = parser::serial::Serial;
66-
67-
monitor_with(serial, elf, pid, baud, parser)
73+
monitor_with(serial, elf, pid, baud, LogFormat::Serial)
6874
}
6975

7076
/// Open a serial monitor on the given interface, using the given input parser.
71-
pub fn monitor_with<L: InputParser>(
77+
pub fn monitor_with(
7278
mut serial: Interface,
7379
elf: Option<&[u8]>,
7480
pid: u16,
7581
baud: u32,
76-
mut parser: L,
82+
log_format: LogFormat,
7783
) -> serialport::Result<()> {
7884
println!("Commands:");
7985
println!(" CTRL+R Reset chip");
@@ -102,7 +108,16 @@ pub fn monitor_with<L: InputParser>(
102108
err => err,
103109
}?;
104110

105-
parser.feed(&buff[0..read_count], &mut stdout);
111+
match log_format {
112+
LogFormat::Defmt => {
113+
let mut parser = parser::esp_defmt::EspDefmt::new(elf);
114+
parser.feed(&buff[0..read_count], &mut stdout);
115+
}
116+
LogFormat::Serial => {
117+
let mut parser = parser::serial::Serial;
118+
parser.feed(&buff[0..read_count], &mut stdout);
119+
}
120+
}
106121

107122
// Don't forget to flush the writer!
108123
stdout.flush().ok();

espflash/src/cli/monitor/parser/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#[cfg(feature = "defmt")]
21
pub mod esp_defmt;
32
pub mod serial;
43

0 commit comments

Comments
 (0)