Skip to content

Commit

Permalink
Turn off some dependencies for library users (#599)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bugadani authored Feb 26, 2024
1 parent cd49141 commit 39e9611
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ jobs:
runs-on: ubuntu-22.04

steps:

- uses: actions/checkout@v4

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

- run: cargo check -p espflash --lib --no-default-features
- run: cargo check -p espflash --lib --no-default-features --features serialport

msrv:
name: Check MSRV (${{ matrix.platform.target }})
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Non-linux-musl: Only list the available USB Ports by default (#590)
- `FlashData::new` now returns `crate::Error` (#591)
- Moved `reset_after_flash` method to `reset` module (#594)
- The `command` module now requires `serialport`. (#599)

### Removed

Expand Down
19 changes: 11 additions & 8 deletions espflash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ indicatif = { version = "0.17.7", optional = true }
lazy_static = { version = "1.4.0", optional = true }
log = "0.4.20"
md-5 = "0.10.6"
miette = { version = "7.0.0", features = ["fancy"] }
miette = { version = "7.0.0" }
parse_int = { version = "0.6.0", optional = true }
regex = { version = "1.10.3", optional = true }
serde = { version = "1.0.196", features = ["derive"] }
serialport = { version = "4.3.0", optional = true }
sha2 = "0.10.8"
slip-codec = "0.4.0"
slip-codec = { version = "0.4.0", optional = true }
strum = { version = "0.26.1", features = ["derive"] }
thiserror = "1.0.56"
toml = "0.8.10"
toml = { version = "0.8.10", optional = true }
update-informer = { version = "1.1.0", optional = true }
xmas-elf = "0.9.1"
xmas-elf = { version = "0.9.1" }

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

[features]
default = ["cli", "serialport"]
default = ["cli"]
cli = [
"dep:addr2line",
"dep:clap",
Expand All @@ -78,8 +78,11 @@ cli = [
"dep:indicatif",
"dep:lazy_static",
"dep:parse_int",
"dep:regex",
"dep:serialport",
"dep:toml",
"dep:update-informer",
"serialport",
"miette/fancy"
]
serialport = ["dep:serialport"]

# enables connecting to a device via serial port
serialport = ["dep:serialport", "dep:slip-codec", "dep:regex", "dep:toml"]
14 changes: 12 additions & 2 deletions espflash/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Send commands to a target device
//! Commands to work with a flasher stub running on a target device

use std::{io::Write, mem::size_of, time::Duration};

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

use crate::flasher::{checksum, SpiAttachParams, SpiSetParams, CHECKSUM_INIT};
use crate::flasher::{SpiAttachParams, SpiSetParams};

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(3);
const ERASE_REGION_TIMEOUT_PER_MB: Duration = Duration::from_secs(30);
Expand Down Expand Up @@ -513,3 +513,13 @@ fn data_command<W: Write>(
}
Ok(())
}

const CHECKSUM_INIT: u8 = 0xEF;

fn checksum(data: &[u8], mut checksum: u8) -> u8 {
for byte in data {
checksum ^= *byte;
}

checksum
}
12 changes: 5 additions & 7 deletions espflash/src/connection/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
command::{Command, CommandType},
connection::{Connection, Port, USB_SERIAL_JTAG_PID},
error::Error,
flasher,
flasher::FLASH_WRITE_SIZE,
};

/// Default time to wait before releasing the boot pin after a reset
Expand Down Expand Up @@ -253,12 +253,11 @@ pub fn soft_reset(
connection.with_timeout(CommandType::FlashBegin.timeout(), |connection| {
let size: u32 = 0;
let offset: u32 = 0;
let blocks: u32 = (size + flasher::FLASH_WRITE_SIZE as u32 - 1)
/ flasher::FLASH_WRITE_SIZE as u32;
let blocks: u32 = (size + FLASH_WRITE_SIZE as u32 - 1) / FLASH_WRITE_SIZE as u32;
connection.command(Command::FlashBegin {
size,
blocks,
block_size: flasher::FLASH_WRITE_SIZE.try_into().unwrap(),
block_size: FLASH_WRITE_SIZE.try_into().unwrap(),
offset,
supports_encryption: false,
})
Expand All @@ -272,12 +271,11 @@ pub fn soft_reset(
connection.with_timeout(CommandType::FlashBegin.timeout(), |connection| {
let size: u32 = 0;
let offset: u32 = 0;
let blocks: u32 =
(size + flasher::FLASH_WRITE_SIZE as u32 - 1) / flasher::FLASH_WRITE_SIZE as u32;
let blocks: u32 = (size + FLASH_WRITE_SIZE as u32 - 1) / FLASH_WRITE_SIZE as u32;
connection.command(Command::FlashBegin {
size,
blocks,
block_size: flasher::FLASH_WRITE_SIZE.try_into().unwrap(),
block_size: FLASH_WRITE_SIZE.try_into().unwrap(),
offset,
supports_encryption: false,
})
Expand Down
36 changes: 24 additions & 12 deletions espflash/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
//! Library and application errors

use std::{
fmt::{Display, Formatter},
io,
};
#[cfg(feature = "serialport")]
use std::fmt::{Display, Formatter};

use miette::Diagnostic;
use slip_codec::SlipError;
use strum::{FromRepr, VariantNames};
use std::io;
use strum::VariantNames;
use thiserror::Error;

#[cfg(feature = "cli")]
use crate::cli::monitor::parser::esp_defmt::DefmtError;
#[cfg(feature = "serialport")]
use crate::command::CommandType;
use crate::{
command::CommandType,
flasher::{FlashFrequency, FlashSize},
targets::Chip,
};
#[cfg(feature = "serialport")]
use slip_codec::SlipError;

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

#[cfg(not(feature = "serialport"))]
#[error(transparent)]
IoError(#[from] std::io::Error),
IoError(#[from] io::Error),

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

#[error("The bootloader returned an error")]
#[cfg(feature = "serialport")]
#[diagnostic(transparent)]
RomError(#[from] RomError),

#[cfg(feature = "serialport")]
#[cfg(feature = "cli")]
#[error(transparent)]
#[diagnostic(transparent)]
Defmt(#[from] DefmtError),
Expand All @@ -204,7 +206,7 @@ pub enum Error {
InternalError,

#[error("Failed to open file: {0}")]
FileOpenError(String, #[source] std::io::Error),
FileOpenError(String, #[source] io::Error),

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

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

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

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

#[cfg(feature = "serialport")]
impl From<CommandType> for TimedOutCommand {
fn from(ct: CommandType) -> Self {
TimedOutCommand { command: Some(ct) }
}
}

/// Errors originating from a device's ROM functionality
#[derive(Clone, Copy, Debug, Default, Diagnostic, Error, FromRepr)]
#[derive(Clone, Copy, Debug, Default, Diagnostic, Error, strum::FromRepr)]
#[non_exhaustive]
#[repr(u8)]
#[cfg(feature = "serialport")]
pub enum RomErrorKind {
#[error("Invalid message received")]
#[diagnostic(code(espflash::rom::invalid_message))]
Expand Down Expand Up @@ -425,6 +432,7 @@ pub enum RomErrorKind {
Other = 0xff,
}

#[cfg(feature = "serialport")]
impl From<u8> for RomErrorKind {
fn from(raw: u8) -> Self {
Self::from_repr(raw).unwrap_or_default()
Expand All @@ -434,13 +442,15 @@ impl From<u8> for RomErrorKind {
/// An error originating from a device's ROM functionality
#[derive(Clone, Copy, Debug, Diagnostic, Error)]
#[error("Error while running {command} command")]
#[cfg(feature = "serialport")]
#[non_exhaustive]
pub struct RomError {
command: CommandType,
#[source]
kind: RomErrorKind,
}

#[cfg(feature = "serialport")]
impl RomError {
pub fn new(command: CommandType, kind: RomErrorKind) -> RomError {
RomError { command, kind }
Expand Down Expand Up @@ -482,13 +492,15 @@ impl From<&'static str> for ElfError {
}
}

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

#[cfg(feature = "serialport")]
impl<T> ResultExt for Result<T, Error> {
fn flashing(self) -> Self {
match self {
Expand Down Expand Up @@ -516,7 +528,7 @@ fn from_error_kind<E>(kind: io::ErrorKind, err: E) -> ConnectionError
where
E: Into<serialport::Error>,
{
use std::io::ErrorKind;
use io::ErrorKind;

match kind {
ErrorKind::TimedOut => ConnectionError::Timeout(TimedOutCommand::default()),
Expand Down
Loading

0 comments on commit 39e9611

Please sign in to comment.