Skip to content

Commit 39e9611

Browse files
authored
Turn off some dependencies for library users (#599)
* Enable serialport for cli through its feature * Add flashing feature, disable feature-specific dependencies * Make strum optional * Make toml optional * Oops * Changelog * Merge flashing back into serialport * Remove rppal mention * Move things back * Only enable miette/fancy for the CLI
1 parent cd49141 commit 39e9611

File tree

16 files changed

+144
-110
lines changed

16 files changed

+144
-110
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ jobs:
8080
runs-on: ubuntu-22.04
8181

8282
steps:
83-
8483
- uses: actions/checkout@v4
8584

8685
- uses: ./.github/actions/setup-target
@@ -89,6 +88,7 @@ jobs:
8988
target: ${{ matrix.platform.target }}
9089

9190
- run: cargo check -p espflash --lib --no-default-features
91+
- run: cargo check -p espflash --lib --no-default-features --features serialport
9292

9393
msrv:
9494
name: Check MSRV (${{ matrix.platform.target }})

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- Non-linux-musl: Only list the available USB Ports by default (#590)
1919
- `FlashData::new` now returns `crate::Error` (#591)
2020
- Moved `reset_after_flash` method to `reset` module (#594)
21+
- The `command` module now requires `serialport`. (#599)
2122

2223
### Removed
2324

espflash/Cargo.toml

+11-8
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,24 @@ indicatif = { version = "0.17.7", optional = true }
4444
lazy_static = { version = "1.4.0", optional = true }
4545
log = "0.4.20"
4646
md-5 = "0.10.6"
47-
miette = { version = "7.0.0", features = ["fancy"] }
47+
miette = { version = "7.0.0" }
4848
parse_int = { version = "0.6.0", optional = true }
4949
regex = { version = "1.10.3", optional = true }
5050
serde = { version = "1.0.196", features = ["derive"] }
5151
serialport = { version = "4.3.0", optional = true }
5252
sha2 = "0.10.8"
53-
slip-codec = "0.4.0"
53+
slip-codec = { version = "0.4.0", optional = true }
5454
strum = { version = "0.26.1", features = ["derive"] }
5555
thiserror = "1.0.56"
56-
toml = "0.8.10"
56+
toml = { version = "0.8.10", optional = true }
5757
update-informer = { version = "1.1.0", optional = true }
58-
xmas-elf = "0.9.1"
58+
xmas-elf = { version = "0.9.1" }
5959

6060
[target.'cfg(unix)'.dependencies]
6161
libc = "0.2.153"
6262

6363
[features]
64-
default = ["cli", "serialport"]
64+
default = ["cli"]
6565
cli = [
6666
"dep:addr2line",
6767
"dep:clap",
@@ -78,8 +78,11 @@ cli = [
7878
"dep:indicatif",
7979
"dep:lazy_static",
8080
"dep:parse_int",
81-
"dep:regex",
82-
"dep:serialport",
81+
"dep:toml",
8382
"dep:update-informer",
83+
"serialport",
84+
"miette/fancy"
8485
]
85-
serialport = ["dep:serialport"]
86+
87+
# enables connecting to a device via serial port
88+
serialport = ["dep:serialport", "dep:slip-codec", "dep:regex", "dep:toml"]

espflash/src/command.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
//! Send commands to a target device
1+
//! Commands to work with a flasher stub running on a target device
22
33
use std::{io::Write, mem::size_of, time::Duration};
44

55
use bytemuck::{bytes_of, Pod, Zeroable};
66
use strum::Display;
77

8-
use crate::flasher::{checksum, SpiAttachParams, SpiSetParams, CHECKSUM_INIT};
8+
use crate::flasher::{SpiAttachParams, SpiSetParams};
99

1010
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(3);
1111
const ERASE_REGION_TIMEOUT_PER_MB: Duration = Duration::from_secs(30);
@@ -513,3 +513,13 @@ fn data_command<W: Write>(
513513
}
514514
Ok(())
515515
}
516+
517+
const CHECKSUM_INIT: u8 = 0xEF;
518+
519+
fn checksum(data: &[u8], mut checksum: u8) -> u8 {
520+
for byte in data {
521+
checksum ^= *byte;
522+
}
523+
524+
checksum
525+
}

espflash/src/connection/reset.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
command::{Command, CommandType},
1616
connection::{Connection, Port, USB_SERIAL_JTAG_PID},
1717
error::Error,
18-
flasher,
18+
flasher::FLASH_WRITE_SIZE,
1919
};
2020

2121
/// Default time to wait before releasing the boot pin after a reset
@@ -253,12 +253,11 @@ pub fn soft_reset(
253253
connection.with_timeout(CommandType::FlashBegin.timeout(), |connection| {
254254
let size: u32 = 0;
255255
let offset: u32 = 0;
256-
let blocks: u32 = (size + flasher::FLASH_WRITE_SIZE as u32 - 1)
257-
/ flasher::FLASH_WRITE_SIZE as u32;
256+
let blocks: u32 = (size + FLASH_WRITE_SIZE as u32 - 1) / FLASH_WRITE_SIZE as u32;
258257
connection.command(Command::FlashBegin {
259258
size,
260259
blocks,
261-
block_size: flasher::FLASH_WRITE_SIZE.try_into().unwrap(),
260+
block_size: FLASH_WRITE_SIZE.try_into().unwrap(),
262261
offset,
263262
supports_encryption: false,
264263
})
@@ -272,12 +271,11 @@ pub fn soft_reset(
272271
connection.with_timeout(CommandType::FlashBegin.timeout(), |connection| {
273272
let size: u32 = 0;
274273
let offset: u32 = 0;
275-
let blocks: u32 =
276-
(size + flasher::FLASH_WRITE_SIZE as u32 - 1) / flasher::FLASH_WRITE_SIZE as u32;
274+
let blocks: u32 = (size + FLASH_WRITE_SIZE as u32 - 1) / FLASH_WRITE_SIZE as u32;
277275
connection.command(Command::FlashBegin {
278276
size,
279277
blocks,
280-
block_size: flasher::FLASH_WRITE_SIZE.try_into().unwrap(),
278+
block_size: FLASH_WRITE_SIZE.try_into().unwrap(),
281279
offset,
282280
supports_encryption: false,
283281
})

espflash/src/error.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
//! Library and application errors
22
3-
use std::{
4-
fmt::{Display, Formatter},
5-
io,
6-
};
3+
#[cfg(feature = "serialport")]
4+
use std::fmt::{Display, Formatter};
75

86
use miette::Diagnostic;
9-
use slip_codec::SlipError;
10-
use strum::{FromRepr, VariantNames};
7+
use std::io;
8+
use strum::VariantNames;
119
use thiserror::Error;
1210

1311
#[cfg(feature = "cli")]
1412
use crate::cli::monitor::parser::esp_defmt::DefmtError;
13+
#[cfg(feature = "serialport")]
14+
use crate::command::CommandType;
1515
use crate::{
16-
command::CommandType,
1716
flasher::{FlashFrequency, FlashSize},
1817
targets::Chip,
1918
};
19+
#[cfg(feature = "serialport")]
20+
use slip_codec::SlipError;
2021

2122
/// All possible errors returned by espflash
2223
#[derive(Debug, Diagnostic, Error)]
@@ -106,7 +107,7 @@ pub enum Error {
106107

107108
#[cfg(not(feature = "serialport"))]
108109
#[error(transparent)]
109-
IoError(#[from] std::io::Error),
110+
IoError(#[from] io::Error),
110111

111112
#[error("Specified partition table path is not a .bin or .csv file")]
112113
#[diagnostic(code(espflash::invalid_partition_table_path))]
@@ -183,10 +184,11 @@ pub enum Error {
183184
InvalidElf(#[from] ElfError),
184185

185186
#[error("The bootloader returned an error")]
187+
#[cfg(feature = "serialport")]
186188
#[diagnostic(transparent)]
187189
RomError(#[from] RomError),
188190

189-
#[cfg(feature = "serialport")]
191+
#[cfg(feature = "cli")]
190192
#[error(transparent)]
191193
#[diagnostic(transparent)]
192194
Defmt(#[from] DefmtError),
@@ -204,7 +206,7 @@ pub enum Error {
204206
InternalError,
205207

206208
#[error("Failed to open file: {0}")]
207-
FileOpenError(String, #[source] std::io::Error),
209+
FileOpenError(String, #[source] io::Error),
208210

209211
#[error("Failed to parse partition table")]
210212
Partition(#[from] esp_idf_part::Error),
@@ -278,6 +280,7 @@ pub enum ConnectionError {
278280
#[diagnostic(code(espflash::read_missmatch))]
279281
ReadMissmatch(u32, u32),
280282

283+
#[cfg(feature = "serialport")]
281284
#[error("Timeout while running {0}command")]
282285
#[diagnostic(code(espflash::timeout))]
283286
Timeout(TimedOutCommand),
@@ -327,10 +330,12 @@ impl From<SlipError> for ConnectionError {
327330

328331
/// An executed command which has timed out
329332
#[derive(Clone, Debug, Default)]
333+
#[cfg(feature = "serialport")]
330334
pub struct TimedOutCommand {
331335
command: Option<CommandType>,
332336
}
333337

338+
#[cfg(feature = "serialport")]
334339
impl Display for TimedOutCommand {
335340
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
336341
match &self.command {
@@ -340,16 +345,18 @@ impl Display for TimedOutCommand {
340345
}
341346
}
342347

348+
#[cfg(feature = "serialport")]
343349
impl From<CommandType> for TimedOutCommand {
344350
fn from(ct: CommandType) -> Self {
345351
TimedOutCommand { command: Some(ct) }
346352
}
347353
}
348354

349355
/// Errors originating from a device's ROM functionality
350-
#[derive(Clone, Copy, Debug, Default, Diagnostic, Error, FromRepr)]
356+
#[derive(Clone, Copy, Debug, Default, Diagnostic, Error, strum::FromRepr)]
351357
#[non_exhaustive]
352358
#[repr(u8)]
359+
#[cfg(feature = "serialport")]
353360
pub enum RomErrorKind {
354361
#[error("Invalid message received")]
355362
#[diagnostic(code(espflash::rom::invalid_message))]
@@ -425,6 +432,7 @@ pub enum RomErrorKind {
425432
Other = 0xff,
426433
}
427434

435+
#[cfg(feature = "serialport")]
428436
impl From<u8> for RomErrorKind {
429437
fn from(raw: u8) -> Self {
430438
Self::from_repr(raw).unwrap_or_default()
@@ -434,13 +442,15 @@ impl From<u8> for RomErrorKind {
434442
/// An error originating from a device's ROM functionality
435443
#[derive(Clone, Copy, Debug, Diagnostic, Error)]
436444
#[error("Error while running {command} command")]
445+
#[cfg(feature = "serialport")]
437446
#[non_exhaustive]
438447
pub struct RomError {
439448
command: CommandType,
440449
#[source]
441450
kind: RomErrorKind,
442451
}
443452

453+
#[cfg(feature = "serialport")]
444454
impl RomError {
445455
pub fn new(command: CommandType, kind: RomErrorKind) -> RomError {
446456
RomError { command, kind }
@@ -482,13 +492,15 @@ impl From<&'static str> for ElfError {
482492
}
483493
}
484494

495+
#[cfg(feature = "serialport")]
485496
pub(crate) trait ResultExt {
486497
/// Mark an error as having occurred during the flashing stage
487498
fn flashing(self) -> Self;
488499
/// Mark the command from which this error originates
489500
fn for_command(self, command: CommandType) -> Self;
490501
}
491502

503+
#[cfg(feature = "serialport")]
492504
impl<T> ResultExt for Result<T, Error> {
493505
fn flashing(self) -> Self {
494506
match self {
@@ -516,7 +528,7 @@ fn from_error_kind<E>(kind: io::ErrorKind, err: E) -> ConnectionError
516528
where
517529
E: Into<serialport::Error>,
518530
{
519-
use std::io::ErrorKind;
531+
use io::ErrorKind;
520532

521533
match kind {
522534
ErrorKind::TimedOut => ConnectionError::Timeout(TimedOutCommand::default()),

0 commit comments

Comments
 (0)