Skip to content

Commit aa30d95

Browse files
committed
Add interface config error
1 parent 916d79e commit aa30d95

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

espflash/src/cli/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ pub fn connect(opts: &ConnectOpts, config: &Config) -> Result<Flasher> {
136136
.map_err(Error::from)
137137
.wrap_err_with(|| format!("Failed to open serial port {}", port_info.port_name))?;
138138

139+
#[cfg(feature = "raspberry")]
140+
let mut gpios = rppal::Gpio::new().unwrap();
141+
142+
let interface = Interface::new(serial, opts, config)
143+
.map_err(Error::from)
144+
.wrap_err_with(|| format!("Failed to open serial port {}", port_info.port_name))?;
145+
139146
// NOTE: since `get_serial_port_info` filters out all non-USB serial ports, we
140147
// can just pretend the remaining types don't exist here.
141148
let port_info = match port_info.port_type {
@@ -152,10 +159,6 @@ pub fn connect(opts: &ConnectOpts, config: &Config) -> Result<Flasher> {
152159
}
153160
_ => unreachable!(),
154161
};
155-
#[cfg(feature = "raspberry")]
156-
let mut gpios = rppal::Gpio::new().unwrap();
157-
158-
let interface = Interface::new(serial, opts, config);
159162

160163
Ok(Flasher::connect(
161164
interface,

espflash/src/error.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
elf::{FlashFrequency, FlashMode},
1414
flasher::FlashSize,
1515
image_format::ImageFormatId,
16+
interface::SerialConfigError,
1617
partition_table::{CoreType, SubType, Type},
1718
Chip,
1819
};
@@ -91,6 +92,12 @@ https://github.com/espressif/esp32c3-direct-boot-example"
9192
help("Make sure the correct device is connected to the host system")
9293
)]
9394
SerialNotFound(String),
95+
#[error("Incorrect serial port configuration")]
96+
#[diagnostic(
97+
code(espflash::serial_config),
98+
help("Make sure you have specified the DTR signal if you are using an internal UART peripherial")
99+
)]
100+
SerialConfiguration(SerialConfigError),
94101
#[error("Canceled by user")]
95102
Canceled,
96103
#[error("The flash mode '{0}' is not valid")]
@@ -246,6 +253,12 @@ impl From<binread::Error> for Error {
246253
}
247254
}
248255

256+
impl From<SerialConfigError> for Error {
257+
fn from(err: SerialConfigError) -> Self {
258+
Self::SerialConfiguration(err)
259+
}
260+
}
261+
249262
#[derive(Copy, Clone, Debug, Error, Diagnostic)]
250263
#[allow(dead_code)]
251264
#[repr(u8)]

espflash/src/interface.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ use rppal::gpio::OutputPin;
77

88
use crate::{cli::ConnectOpts, Config};
99

10+
#[derive(thiserror::Error, Debug)]
11+
pub enum SerialConfigError {
12+
#[cfg(feature = "raspberry")]
13+
#[error("You need to specify DTR when using an internal UART peripheral")]
14+
MissingDtrForInternalUart,
15+
}
16+
1017
/// Wrapper around SerialPort where platform-specific modifications can be implemented.
1118
pub struct Interface {
1219
pub serial_port: Box<dyn SerialPort>,
@@ -27,26 +34,30 @@ fn write_gpio(gpio: &mut OutputPin, level: bool) {
2734

2835
impl Interface {
2936
#[cfg(feature = "raspberry")]
30-
pub(crate) fn new(serial: Box<dyn SerialPort>, opts: &ConnectOpts, config: &Config) -> Self {
31-
Self {
37+
pub(crate) fn new(
38+
serial: Box<dyn SerialPort>,
39+
opts: &ConnectOpts,
40+
config: &Config,
41+
) -> Result<Self, SerialConfigError> {
42+
let rts_gpio = opts.rts.or(config.rts);
43+
let dtr_gpio = opts.dtr.or(config.dtr);
44+
45+
Ok(Self {
3246
serial_port: serial,
33-
rts: opts
34-
.rts
35-
.or(config.rts)
36-
.map(|num| gpios.get(num).into_output()),
37-
38-
dtr: opts
39-
.dtr
40-
.or(config.dtr)
41-
.map(|num| gpios.get(num).into_output()),
42-
}
47+
rts: rts_gpio.map(|num| gpios.get(num).into_output()),
48+
dtr: dtr_gpio.map(|num| gpios.get(num).into_output()),
49+
})
4350
}
4451

4552
#[cfg(not(feature = "raspberry"))]
46-
pub(crate) fn new(serial: Box<dyn SerialPort>, _opts: &ConnectOpts, _config: &Config) -> Self {
47-
Self {
53+
pub(crate) fn new(
54+
serial: Box<dyn SerialPort>,
55+
_opts: &ConnectOpts,
56+
_config: &Config,
57+
) -> Result<Self, SerialConfigError> {
58+
Ok(Self {
4859
serial_port: serial,
49-
}
60+
})
5061
}
5162

5263
pub fn write_data_terminal_ready(&mut self, pin_state: bool) -> serialport::Result<()> {

0 commit comments

Comments
 (0)