Skip to content

Commit d7dc3ae

Browse files
Fix lib compilation (#565)
* fix: Fix targets mod * fix: Fix lib compilatioon
1 parent 9729308 commit d7dc3ae

File tree

20 files changed

+155
-73
lines changed

20 files changed

+155
-73
lines changed

cargo-espflash/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use clap::{Args, CommandFactory, Parser, Subcommand};
99
use espflash::{
1010
cli::{
1111
self, board_info, checksum_md5, completions, config::Config, connect, erase_flash,
12-
erase_partitions, erase_region, flash_elf_image, monitor::monitor, parse_partition_table,
13-
partition_table, print_board_info, save_elf_as_image, serial_monitor, ChecksumMd5Args,
14-
CompletionsArgs, ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress,
15-
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
12+
erase_partitions, erase_region, flash_elf_image, monitor::monitor, partition_table,
13+
print_board_info, save_elf_as_image, serial_monitor, ChecksumMd5Args, CompletionsArgs,
14+
ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs,
15+
MonitorArgs, PartitionTableArgs,
1616
},
1717
error::Error as EspflashError,
18-
flasher::{FlashData, FlashSettings},
18+
flasher::{parse_partition_table, FlashData, FlashSettings},
1919
image_format::ImageFormatKind,
2020
logging::initialize_logger,
2121
targets::{Chip, XtalFrequency},

espflash/src/bin/espflash.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use clap::{Args, CommandFactory, Parser, Subcommand};
88
use espflash::{
99
cli::{
1010
self, board_info, checksum_md5, completions, config::Config, connect, erase_flash,
11-
erase_partitions, erase_region, flash_elf_image, monitor::monitor, parse_partition_table,
12-
parse_uint32, partition_table, print_board_info, save_elf_as_image, serial_monitor,
13-
ChecksumMd5Args, CompletionsArgs, ConnectArgs, EraseFlashArgs, EraseRegionArgs,
14-
EspflashProgress, FlashConfigArgs, MonitorArgs, PartitionTableArgs,
11+
erase_partitions, erase_region, flash_elf_image, monitor::monitor, parse_uint32,
12+
partition_table, print_board_info, save_elf_as_image, serial_monitor, ChecksumMd5Args,
13+
CompletionsArgs, ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress,
14+
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
1515
},
1616
error::Error,
17-
flasher::{FlashData, FlashSettings},
17+
flasher::{parse_partition_table, FlashData, FlashSettings},
1818
image_format::ImageFormatKind,
1919
logging::initialize_logger,
2020
targets::{Chip, XtalFrequency},

espflash/src/cli/mod.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@
1111
//! [espflash]: https://crates.io/crates/espflash
1212
1313
use std::num::ParseIntError;
14-
use std::{
15-
collections::HashMap,
16-
fs,
17-
io::Write,
18-
path::{Path, PathBuf},
19-
};
14+
use std::{collections::HashMap, fs, io::Write, path::PathBuf};
2015

2116
use clap::Args;
2217
use clap_complete::Shell;
@@ -35,7 +30,10 @@ use self::{
3530
use crate::{
3631
elf::ElfFirmwareImage,
3732
error::{Error, MissingPartition, MissingPartitionTable},
38-
flasher::{FlashData, FlashFrequency, FlashMode, FlashSize, Flasher, ProgressCallbacks},
33+
flasher::{
34+
parse_partition_table, FlashData, FlashFrequency, FlashMode, FlashSize, Flasher,
35+
ProgressCallbacks,
36+
},
3937
image_format::ImageFormatKind,
4038
interface::Interface,
4139
targets::{Chip, XtalFrequency},
@@ -592,15 +590,6 @@ pub fn flash_elf_image(
592590
Ok(())
593591
}
594592

595-
/// Parse a [PartitionTable] from the provided path
596-
pub fn parse_partition_table(path: &Path) -> Result<PartitionTable> {
597-
let data = fs::read(path)
598-
.into_diagnostic()
599-
.wrap_err("Failed to open partition table")?;
600-
601-
PartitionTable::try_from(data).into_diagnostic()
602-
}
603-
604593
/// Erase one or more partitions by label or [DataType]
605594
pub fn erase_partitions(
606595
flasher: &mut Flasher,

espflash/src/error.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ use std::{
55
io,
66
};
77

8-
use miette::Diagnostic;
9-
use slip_codec::SlipError;
10-
use strum::{FromRepr, VariantNames};
11-
use thiserror::Error;
12-
8+
#[cfg(feature = "cli")]
9+
use crate::cli::monitor::parser::esp_defmt::DefmtError;
10+
#[cfg(feature = "serialport")]
11+
use crate::interface::SerialConfigError;
1312
use crate::{
14-
cli::monitor::parser::esp_defmt::DefmtError,
1513
command::CommandType,
1614
flasher::{FlashFrequency, FlashSize},
1715
image_format::ImageFormatKind,
18-
interface::SerialConfigError,
1916
targets::Chip,
2017
};
2118

19+
use miette::Diagnostic;
20+
use slip_codec::SlipError;
21+
use strum::{FromRepr, VariantNames};
22+
use thiserror::Error;
23+
2224
/// All possible errors returned by espflash
2325
#[derive(Debug, Diagnostic, Error)]
2426
#[non_exhaustive]
@@ -88,6 +90,10 @@ pub enum Error {
8890
)]
8991
InvalidFlashSize(String),
9092

93+
#[cfg(not(feature = "serialport"))]
94+
#[error(transparent)]
95+
IoError(#[from] std::io::Error),
96+
9197
#[error("No serial ports could be detected")]
9298
#[diagnostic(
9399
code(espflash::no_serial),
@@ -102,6 +108,7 @@ pub enum Error {
102108
)]
103109
StubRequiredToEraseFlash,
104110

111+
#[cfg(feature = "serialport")]
105112
#[error("Incorrect serial port configuration")]
106113
#[diagnostic(
107114
code(espflash::serial_config),
@@ -176,6 +183,7 @@ pub enum Error {
176183
#[diagnostic(transparent)]
177184
UnsupportedImageFormat(#[from] UnsupportedImageFormatError),
178185

186+
#[cfg(feature = "serialport")]
179187
#[error(transparent)]
180188
#[diagnostic(transparent)]
181189
Defmt(#[from] DefmtError),
@@ -188,6 +196,7 @@ pub enum Error {
188196
InternalError,
189197
}
190198

199+
#[cfg(feature = "serialport")]
191200
impl From<io::Error> for Error {
192201
fn from(err: io::Error) -> Self {
193202
Self::Connection(err.into())
@@ -202,12 +211,14 @@ impl From<serialport::Error> for Error {
202211
}
203212
}
204213

214+
#[cfg(feature = "serialport")]
205215
impl From<SlipError> for Error {
206216
fn from(err: SlipError) -> Self {
207217
Self::Connection(err.into())
208218
}
209219
}
210220

221+
#[cfg(feature = "serialport")]
211222
impl From<SerialConfigError> for Error {
212223
fn from(err: SerialConfigError) -> Self {
213224
Self::SerialConfiguration(err)
@@ -259,6 +270,7 @@ pub enum ConnectionError {
259270
Serial(#[source] serialport::Error),
260271
}
261272

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

294+
#[cfg(feature = "serialport")]
282295
impl From<SlipError> for ConnectionError {
283296
fn from(err: SlipError) -> Self {
284297
match err {

espflash/src/flasher/mod.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ use std::{borrow::Cow, fs, path::Path, str::FromStr, thread::sleep};
99
use bytemuck::{Pod, Zeroable, __core::time::Duration};
1010
use esp_idf_part::PartitionTable;
1111
use log::{debug, info, warn};
12-
use miette::{IntoDiagnostic, Result};
12+
use miette::{Context, IntoDiagnostic, Result};
1313
use serde::{Deserialize, Serialize};
14+
#[cfg(feature = "serialport")]
1415
use serialport::UsbPortInfo;
1516
use strum::{Display, EnumIter, EnumVariantNames};
1617

1718
use self::stubs::FlashStub;
1819
use crate::{
19-
cli::parse_partition_table,
2020
command::{Command, CommandType},
21-
connection::Connection,
2221
elf::{ElfFirmwareImage, FirmwareImage, RomSegment},
2322
error::{ConnectionError, Error, ResultExt},
2423
image_format::ImageFormatKind,
25-
interface::Interface,
2624
targets::{Chip, XtalFrequency},
2725
};
28-
26+
#[cfg(feature = "serialport")]
27+
use crate::{connection::Connection, interface::Interface};
2928
mod stubs;
3029

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

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

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

10891090
checksum
10901091
}
1092+
1093+
/// Parse a [PartitionTable] from the provided path
1094+
pub fn parse_partition_table(path: &Path) -> Result<PartitionTable> {
1095+
let data = fs::read(path)
1096+
.into_diagnostic()
1097+
.wrap_err("Failed to open partition table")?;
1098+
1099+
PartitionTable::try_from(data).into_diagnostic()
1100+
}

espflash/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ pub mod command;
5656
pub mod connection;
5757
pub mod elf;
5858
pub mod error;
59-
#[cfg(feature = "serialport")]
60-
#[cfg_attr(docsrs, doc(cfg(feature = "serialport")))]
6159
pub mod flasher;
6260
pub mod image_format;
6361
#[cfg(feature = "serialport")]

espflash/src/targets/esp32.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::ops::Range;
22

3+
#[cfg(feature = "serialport")]
4+
use crate::{connection::Connection, targets::bytes_to_mac_addr};
35
use crate::{
4-
connection::Connection,
56
elf::FirmwareImage,
67
error::{Error, UnsupportedImageFormatError},
78
flasher::{FlashData, FlashFrequency},
89
image_format::{IdfBootloaderFormat, ImageFormat, ImageFormatKind},
9-
targets::{
10-
bytes_to_mac_addr, Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency,
11-
},
10+
targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency},
1211
};
1312

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

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

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

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

@@ -129,10 +131,12 @@ impl Target for Esp32 {
129131
}
130132
}
131133

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

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

203+
#[cfg(feature = "serialport")]
199204
fn mac_address(&self, connection: &mut Connection) -> Result<String, Error> {
200205
let word1 = self.read_efuse(connection, 1)?;
201206
let word2 = self.read_efuse(connection, 2)?;

espflash/src/targets/esp32c2.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::{collections::HashMap, ops::Range};
22

3+
#[cfg(feature = "serialport")]
4+
use crate::{connection::Connection, targets::bytes_to_mac_addr};
35
use crate::{
4-
connection::Connection,
56
elf::FirmwareImage,
67
error::Error,
78
flasher::{FlashData, FlashFrequency},
89
image_format::{DirectBootFormat, IdfBootloaderFormat, ImageFormat, ImageFormatKind},
9-
targets::{
10-
bytes_to_mac_addr, Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency,
11-
},
10+
targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency},
1211
};
1312

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

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

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

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

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

138+
#[cfg(feature = "serialport")]
135139
/// What is the MAC address?
136140
fn mac_address(&self, connection: &mut Connection) -> Result<String, Error> {
137141
let word5 = self.read_efuse(connection, 16)?;

espflash/src/targets/esp32c3.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::ops::Range;
22

3+
#[cfg(feature = "serialport")]
4+
use crate::connection::Connection;
35
use crate::{
4-
connection::Connection,
56
elf::FirmwareImage,
67
error::{Error, UnsupportedImageFormatError},
78
flasher::{FlashData, FlashFrequency},
@@ -51,21 +52,25 @@ impl Target for Esp32c3 {
5152
FLASH_RANGES.iter().any(|range| range.contains(&addr))
5253
}
5354

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

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

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

6670
Ok((hi << 3) + lo)
6771
}
6872

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

0 commit comments

Comments
 (0)