Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lib compilation #565

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 9 additions & 4 deletions espflash/src/targets/esp32.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::ops::Range;

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

const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[0x00f0_1d83];
Expand All @@ -32,6 +31,7 @@ impl Esp32 {
CHIP_DETECT_MAGIC_VALUES.contains(&value)
}

#[cfg(feature = "serialport")]
/// Return the package version based on the eFuses
fn package_version(&self, connection: &mut Connection) -> Result<u32, Error> {
let word3 = self.read_efuse(connection, 3)?;
Expand All @@ -54,6 +54,7 @@ impl Target for Esp32 {
FLASH_RANGES.iter().any(|range| range.contains(&addr))
}

#[cfg(feature = "serialport")]
fn chip_features(&self, connection: &mut Connection) -> Result<Vec<&str>, Error> {
let word3 = self.read_efuse(connection, 3)?;
let word4 = self.read_efuse(connection, 4)?;
Expand Down Expand Up @@ -112,6 +113,7 @@ impl Target for Esp32 {
Ok(features)
}

#[cfg(feature = "serialport")]
fn major_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
let apb_ctl_date = connection.read_reg(0x3FF6_607C)?;

Expand All @@ -129,10 +131,12 @@ impl Target for Esp32 {
}
}

#[cfg(feature = "serialport")]
fn minor_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok((self.read_efuse(connection, 5)? >> 24) & 0x3)
}

#[cfg(feature = "serialport")]
fn crystal_freq(&self, connection: &mut Connection) -> Result<XtalFrequency, Error> {
let uart_div = connection.read_reg(UART_CLKDIV_REG)? & UART_CLKDIV_MASK;
let est_xtal = (connection.get_baud()? * uart_div) / 1_000_000 / XTAL_CLK_DIVIDER;
Expand Down Expand Up @@ -196,6 +200,7 @@ impl Target for Esp32 {
}
}

#[cfg(feature = "serialport")]
fn mac_address(&self, connection: &mut Connection) -> Result<String, Error> {
let word1 = self.read_efuse(connection, 1)?;
let word2 = self.read_efuse(connection, 2)?;
Expand Down
12 changes: 8 additions & 4 deletions espflash/src/targets/esp32c2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::{collections::HashMap, ops::Range};

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

const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[
Expand Down Expand Up @@ -47,18 +46,22 @@ impl Target for Esp32c2 {
FLASH_RANGES.iter().any(|range| range.contains(&addr))
}

#[cfg(feature = "serialport")]
fn chip_features(&self, _connection: &mut Connection) -> Result<Vec<&str>, Error> {
Ok(vec!["WiFi", "BLE"])
}

#[cfg(feature = "serialport")]
fn major_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok(self.read_efuse(connection, 17)? >> 20 & 0x3)
}

#[cfg(feature = "serialport")]
fn minor_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok(self.read_efuse(connection, 17)? >> 16 & 0xf)
}

#[cfg(feature = "serialport")]
fn crystal_freq(&self, connection: &mut Connection) -> Result<XtalFrequency, Error> {
let uart_div = connection.read_reg(UART_CLKDIV_REG)? & UART_CLKDIV_MASK;
let est_xtal = (connection.get_baud()? * uart_div) / 1_000_000 / XTAL_CLK_DIVIDER;
Expand Down Expand Up @@ -132,6 +135,7 @@ impl Target for Esp32c2 {
}
}

#[cfg(feature = "serialport")]
/// What is the MAC address?
fn mac_address(&self, connection: &mut Connection) -> Result<String, Error> {
let word5 = self.read_efuse(connection, 16)?;
Expand Down
7 changes: 6 additions & 1 deletion espflash/src/targets/esp32c3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::ops::Range;

#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{
connection::Connection,
elf::FirmwareImage,
error::{Error, UnsupportedImageFormatError},
flasher::{FlashData, FlashFrequency},
Expand Down Expand Up @@ -51,21 +52,25 @@ impl Target for Esp32c3 {
FLASH_RANGES.iter().any(|range| range.contains(&addr))
}

#[cfg(feature = "serialport")]
fn chip_features(&self, _connection: &mut Connection) -> Result<Vec<&str>, Error> {
Ok(vec!["WiFi", "BLE"])
}

#[cfg(feature = "serialport")]
fn major_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok(self.read_efuse(connection, 22)? >> 24 & 0x3)
}

#[cfg(feature = "serialport")]
fn minor_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
let hi = self.read_efuse(connection, 22)? >> 23 & 0x1;
let lo = self.read_efuse(connection, 20)? >> 18 & 0x7;

Ok((hi << 3) + lo)
}

#[cfg(feature = "serialport")]
fn crystal_freq(&self, _connection: &mut Connection) -> Result<XtalFrequency, Error> {
// The ESP32-C3's XTAL has a fixed frequency of 40MHz.
Ok(XtalFrequency::_40Mhz)
Expand Down
Loading
Loading