Skip to content

Commit

Permalink
fix: Fix lib compilatioon
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioGasquez committed Jan 30, 2024
1 parent b95199d commit 28165b2
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 55 deletions.
10 changes: 5 additions & 5 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use clap::{Args, CommandFactory, Parser, Subcommand};
use espflash::{
cli::{
self, board_info, checksum_md5, completions, config::Config, connect, erase_flash,
erase_partitions, erase_region, flash_elf_image, monitor::monitor, parse_partition_table,
partition_table, print_board_info, save_elf_as_image, serial_monitor, ChecksumMd5Args,
CompletionsArgs, ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress,
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
erase_partitions, erase_region, flash_elf_image, monitor::monitor, partition_table,
print_board_info, save_elf_as_image, serial_monitor, ChecksumMd5Args, CompletionsArgs,
ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs,
MonitorArgs, PartitionTableArgs,
},
error::Error as EspflashError,
flasher::{FlashData, FlashSettings},
flasher::{parse_partition_table, FlashData, FlashSettings},
image_format::ImageFormatKind,
logging::initialize_logger,
targets::{Chip, XtalFrequency},
Expand Down
10 changes: 5 additions & 5 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use clap::{Args, CommandFactory, Parser, Subcommand};
use espflash::{
cli::{
self, board_info, checksum_md5, completions, config::Config, connect, erase_flash,
erase_partitions, erase_region, flash_elf_image, monitor::monitor, parse_partition_table,
parse_uint32, partition_table, print_board_info, save_elf_as_image, serial_monitor,
ChecksumMd5Args, CompletionsArgs, ConnectArgs, EraseFlashArgs, EraseRegionArgs,
EspflashProgress, FlashConfigArgs, MonitorArgs, PartitionTableArgs,
erase_partitions, erase_region, flash_elf_image, monitor::monitor, parse_uint32,
partition_table, print_board_info, save_elf_as_image, serial_monitor, ChecksumMd5Args,
CompletionsArgs, ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress,
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
},
error::Error,
flasher::{FlashData, FlashSettings},
flasher::{parse_partition_table, FlashData, FlashSettings},
image_format::ImageFormatKind,
logging::initialize_logger,
targets::{Chip, XtalFrequency},
Expand Down
21 changes: 5 additions & 16 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
//! [espflash]: https://crates.io/crates/espflash
use std::num::ParseIntError;
use std::{
collections::HashMap,
fs,
io::Write,
path::{Path, PathBuf},
};
use std::{collections::HashMap, fs, io::Write, path::PathBuf};

use clap::Args;
use clap_complete::Shell;
Expand All @@ -35,7 +30,10 @@ use self::{
use crate::{
elf::ElfFirmwareImage,
error::{Error, MissingPartition, MissingPartitionTable},
flasher::{FlashData, FlashFrequency, FlashMode, FlashSize, Flasher, ProgressCallbacks},
flasher::{
parse_partition_table, FlashData, FlashFrequency, FlashMode, FlashSize, Flasher,
ProgressCallbacks,
},
image_format::ImageFormatKind,
interface::Interface,
targets::{Chip, XtalFrequency},
Expand Down Expand Up @@ -592,15 +590,6 @@ pub fn flash_elf_image(
Ok(())
}

/// Parse a [PartitionTable] from the provided path
pub fn parse_partition_table(path: &Path) -> Result<PartitionTable> {
let data = fs::read(path)
.into_diagnostic()
.wrap_err("Failed to open partition table")?;

PartitionTable::try_from(data).into_diagnostic()
}

/// Erase one or more partitions by label or [DataType]
pub fn erase_partitions(
flasher: &mut Flasher,
Expand Down
27 changes: 20 additions & 7 deletions espflash/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ use std::{
io,
};

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

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

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

/// All possible errors returned by espflash
#[derive(Debug, Diagnostic, Error)]
#[non_exhaustive]
Expand Down Expand Up @@ -88,6 +90,10 @@ pub enum Error {
)]
InvalidFlashSize(String),

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

#[error("No serial ports could be detected")]
#[diagnostic(
code(espflash::no_serial),
Expand All @@ -102,6 +108,7 @@ pub enum Error {
)]
StubRequiredToEraseFlash,

#[cfg(feature = "serialport")]
#[error("Incorrect serial port configuration")]
#[diagnostic(
code(espflash::serial_config),
Expand Down Expand Up @@ -176,6 +183,7 @@ pub enum Error {
#[diagnostic(transparent)]
UnsupportedImageFormat(#[from] UnsupportedImageFormatError),

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

#[cfg(feature = "serialport")]
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::Connection(err.into())
Expand All @@ -202,12 +211,14 @@ impl From<serialport::Error> for Error {
}
}

#[cfg(feature = "serialport")]
impl From<SlipError> for Error {
fn from(err: SlipError) -> Self {
Self::Connection(err.into())
}
}

#[cfg(feature = "serialport")]
impl From<SerialConfigError> for Error {
fn from(err: SerialConfigError) -> Self {
Self::SerialConfiguration(err)
Expand Down Expand Up @@ -259,6 +270,7 @@ pub enum ConnectionError {
Serial(#[source] serialport::Error),
}

#[cfg(feature = "serialport")]
impl From<io::Error> for ConnectionError {
fn from(err: io::Error) -> Self {
from_error_kind(err.kind(), err)
Expand All @@ -279,6 +291,7 @@ impl From<serialport::Error> for ConnectionError {
}
}

#[cfg(feature = "serialport")]
impl From<SlipError> for ConnectionError {
fn from(err: SlipError) -> Self {
match err {
Expand Down
20 changes: 15 additions & 5 deletions espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ use std::{borrow::Cow, fs, path::Path, str::FromStr, thread::sleep};
use bytemuck::{Pod, Zeroable, __core::time::Duration};
use esp_idf_part::PartitionTable;
use log::{debug, info, warn};
use miette::{IntoDiagnostic, Result};
use miette::{Context, IntoDiagnostic, Result};
use serde::{Deserialize, Serialize};
#[cfg(feature = "serialport")]
use serialport::UsbPortInfo;
use strum::{Display, EnumIter, EnumVariantNames};

use self::stubs::FlashStub;
use crate::{
cli::parse_partition_table,
command::{Command, CommandType},
connection::Connection,
elf::{ElfFirmwareImage, FirmwareImage, RomSegment},
error::{ConnectionError, Error, ResultExt},
image_format::ImageFormatKind,
interface::Interface,
targets::{Chip, XtalFrequency},
};

#[cfg(feature = "serialport")]
use crate::{connection::Connection, interface::Interface};
mod stubs;

pub(crate) const CHECKSUM_INIT: u8 = 0xEF;
Expand Down Expand Up @@ -480,6 +479,7 @@ pub trait ProgressCallbacks {
fn finish(&mut self);
}

#[cfg(feature = "serialport")]
/// Connect to and flash a target device
pub struct Flasher {
/// Connection for flash operations
Expand All @@ -498,6 +498,7 @@ pub struct Flasher {
skip: bool,
}

#[cfg(feature = "serialport")]
impl Flasher {
pub fn connect(
serial: Interface,
Expand Down Expand Up @@ -1088,3 +1089,12 @@ pub(crate) fn checksum(data: &[u8], mut checksum: u8) -> u8 {

checksum
}

/// Parse a [PartitionTable] from the provided path
pub fn parse_partition_table(path: &Path) -> Result<PartitionTable> {
let data = fs::read(path)
.into_diagnostic()
.wrap_err("Failed to open partition table")?;

PartitionTable::try_from(data).into_diagnostic()
}
2 changes: 0 additions & 2 deletions espflash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ pub mod command;
pub mod connection;
pub mod elf;
pub mod error;
#[cfg(feature = "serialport")]
#[cfg_attr(docsrs, doc(cfg(feature = "serialport")))]
pub mod flasher;
pub mod image_format;
#[cfg(feature = "serialport")]
Expand Down
6 changes: 2 additions & 4 deletions espflash/src/targets/esp32s2.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::ops::Range;

#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{connection::Connection, targets::MAX_RAM_BLOCK_SIZE};
use crate::{
elf::FirmwareImage,
error::{Error, UnsupportedImageFormatError},
flasher::{FlashData, FlashFrequency, FLASH_WRITE_SIZE},
image_format::{IdfBootloaderFormat, ImageFormat, ImageFormatKind},
targets::{
Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency, MAX_RAM_BLOCK_SIZE,
},
targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency},
};

const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[0x0000_07c6];
Expand Down
6 changes: 5 additions & 1 deletion espflash/src/targets/flash_target/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ use flate2::{
use log::debug;
use md5::{Digest, Md5};

#[cfg(feature = "serialport")]
use crate::{
command::{Command, CommandType},
connection::{Connection, USB_SERIAL_JTAG_PID},
targets::FlashTarget,
};
use crate::{
elf::RomSegment,
error::Error,
flasher::{ProgressCallbacks, SpiAttachParams, FLASH_SECTOR_SIZE},
targets::Chip,
targets::FlashTarget,
};

/// Applications running from an ESP32's (or variant's) flash
Expand Down Expand Up @@ -46,6 +49,7 @@ impl Esp32Target {
}
}

#[cfg(feature = "serialport")]
impl FlashTarget for Esp32Target {
fn begin(&mut self, connection: &mut Connection) -> Result<(), Error> {
connection.with_timeout(CommandType::SpiAttach.timeout(), |connection| {
Expand Down
5 changes: 3 additions & 2 deletions espflash/src/targets/flash_target/esp8266.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
command::{Command, CommandType},
connection::Connection,
elf::RomSegment,
error::Error,
flasher::{get_erase_size, ProgressCallbacks, FLASH_WRITE_SIZE},
targets::FlashTarget,
};
#[cfg(feature = "serialport")]
use crate::{connection::Connection, targets::FlashTarget};

/// Applications running from an ESP8266's flash
#[derive(Default)]
Expand All @@ -17,6 +17,7 @@ impl Esp8266Target {
}
}

#[cfg(feature = "serialport")]
impl FlashTarget for Esp8266Target {
fn begin(&mut self, connection: &mut Connection) -> Result<(), Error> {
connection.command(Command::FlashBegin {
Expand Down
5 changes: 4 additions & 1 deletion espflash/src/targets/flash_target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ use bytemuck::{Pod, Zeroable};

pub(crate) use self::ram::MAX_RAM_BLOCK_SIZE;
pub use self::{esp32::Esp32Target, esp8266::Esp8266Target, ram::RamTarget};
use crate::{connection::Connection, elf::RomSegment, error::Error, flasher::ProgressCallbacks};
#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{elf::RomSegment, error::Error, flasher::ProgressCallbacks};

mod esp32;
mod esp8266;
mod ram;

#[cfg(feature = "serialport")]
/// Operations for interacting with a flash target
pub trait FlashTarget {
/// Begin the flashing operation
Expand Down
7 changes: 4 additions & 3 deletions espflash/src/targets/flash_target/ram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use bytemuck::{Pod, Zeroable};

use crate::{
command::{Command, CommandType},
connection::Connection,
elf::RomSegment,
error::Error,
flasher::ProgressCallbacks,
targets::FlashTarget,
};
#[cfg(feature = "serialport")]
use crate::{connection::Connection, targets::FlashTarget};

pub(crate) const MAX_RAM_BLOCK_SIZE: usize = 0x1800;
pub const MAX_RAM_BLOCK_SIZE: usize = 0x1800;

#[derive(Zeroable, Pod, Copy, Clone)]
#[repr(C)]
Expand All @@ -36,6 +36,7 @@ impl Default for RamTarget {
}
}

#[cfg(feature = "serialport")]
impl FlashTarget for RamTarget {
fn begin(&mut self, _connection: &mut Connection) -> Result<(), Error> {
Ok(())
Expand Down
11 changes: 7 additions & 4 deletions espflash/src/targets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use esp_idf_part::{AppType, DataType, Partition, PartitionTable, SubType, Type};
use serde::{Deserialize, Serialize};
use strum::{Display, EnumIter, EnumString, EnumVariantNames};

use self::flash_target::MAX_RAM_BLOCK_SIZE;
#[cfg(feature = "serialport")]
use self::flash_target::{FlashTarget, MAX_RAM_BLOCK_SIZE};
pub use self::{
esp32::Esp32,
esp32c2::Esp32c2,
Expand All @@ -23,10 +24,11 @@ pub use self::{
esp32s2::Esp32s2,
esp32s3::Esp32s3,
esp8266::Esp8266,
flash_target::{Esp32Target, Esp8266Target, FlashTarget, RamTarget},
flash_target::{Esp32Target, Esp8266Target, RamTarget},
};
#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{
connection::Connection,
elf::FirmwareImage,
error::Error,
flasher::{FlashData, FlashFrequency, SpiAttachParams, FLASH_WRITE_SIZE},
Expand All @@ -45,7 +47,6 @@ mod esp32p4;
mod esp32s2;
mod esp32s3;
mod esp8266;
#[cfg(feature = "serialport")]
mod flash_target;

/// Supported crystal frequencies
Expand Down Expand Up @@ -161,6 +162,7 @@ impl Chip {
}
}

#[cfg(feature = "serialport")]
pub fn flash_target(
&self,
spi_params: SpiAttachParams,
Expand All @@ -174,6 +176,7 @@ impl Chip {
}
}

#[cfg(feature = "serialport")]
pub fn ram_target(
&self,
entry: Option<u32>,
Expand Down

0 comments on commit 28165b2

Please sign in to comment.