Skip to content

Commit

Permalink
Fix compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Sep 16, 2022
1 parent 011e6a7 commit 41ab4aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
3 changes: 1 addition & 2 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use update_informer::{registry, Check};
use crate::{
cli::{monitor::monitor, serial::get_serial_port_info},
elf::{ElfFirmwareImage, FlashFrequency, FlashMode},
error::{Error, NoOtadataError},
error::NoOtadataError,
flasher::FlashSize,
interface::Interface,
partition_table, Chip, Flasher, ImageFormatId, InvalidPartitionTable, MissingPartitionTable,
Expand Down Expand Up @@ -132,7 +132,6 @@ pub fn connect(opts: &ConnectOpts, config: &Config) -> Result<Flasher> {
println!("Connecting...\n");

let interface = Interface::new(&port_info, opts, config)
.map_err(Error::from)
.wrap_err_with(|| format!("Failed to open serial port {}", port_info.port_name))?;

// NOTE: since `get_serial_port_info` filters out all non-USB serial ports, we
Expand Down
40 changes: 18 additions & 22 deletions espflash/src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::Read;

use crate::{cli::ConnectOpts, Config, Error};
use miette::Context;
use miette::{Context, Result};
use serialport::{FlowControl, SerialPort, SerialPortInfo};

#[cfg(feature = "raspberry")]
Expand Down Expand Up @@ -29,36 +29,44 @@ pub struct Interface {

#[cfg(feature = "raspberry")]
fn write_gpio(gpio: &mut OutputPin, level: bool) {
if pin_state {
if level {
gpio.set_high();
} else {
gpio.set_low();
}
}

fn open_port(port_info: &SerialPortInfo) -> Result<Box<dyn SerialPort>> {
serialport::new(&port_info.port_name, 115_200)
.flow_control(FlowControl::None)
.open()
.map_err(Error::from)
.wrap_err_with(|| format!("Failed to open serial port {}", port_info.port_name))
}

impl Interface {
#[cfg(feature = "raspberry")]
pub(crate) fn new(
port_info: &SerialPortInfo,
opts: &ConnectOpts,
config: &Config,
) -> Result<Self, Error> {
) -> Result<Self> {
let rts_gpio = opts.rts.or(config.rts);
let dtr_gpio = opts.dtr.or(config.dtr);

if port_info.port_type == serialport::SerialPortType::Unknown
&& (dtr_gpio.is_none() || rts_gpio.is_none())
{
// Assume internal UART, which has no DTR pin and usually no RTS either.
return Err(Error::from(SerialConfigError::MissingDtrRtsForInternalUart));
return Err(Error::from(SerialConfigError::MissingDtrRtsForInternalUart).into());
}

let mut gpios = Gpio::new().unwrap();
let gpios = Gpio::new().unwrap();

let rts = if let Some(gpio) = rts_gpio {
match gpios.get(gpio) {
Ok(pin) => Some(pin.into_output()),
Err(_) => return Err(SerialConfigError::GpioUnavailable),
Err(_) => return Err(Error::from(SerialConfigError::GpioUnavailable(gpio)).into()),
}
} else {
None
Expand All @@ -67,20 +75,14 @@ impl Interface {
let dtr = if let Some(gpio) = dtr_gpio {
match gpios.get(gpio) {
Ok(pin) => Some(pin.into_output()),
Err(_) => return Err(SerialConfigError::GpioUnavailable),
Err(_) => return Err(Error::from(SerialConfigError::GpioUnavailable(gpio)).into()),
}
} else {
None
};

let serial = serialport::new(&port_info.port_name, 115_200)
.flow_control(FlowControl::None)
.open()
.map_err(Error::from)
.wrap_err_with(|| format!("Failed to open serial port {}", port_info.port_name))?;

Ok(Self {
serial_port: serial,
serial_port: open_port(port_info)?,
rts,
dtr,
})
Expand All @@ -91,15 +93,9 @@ impl Interface {
port_info: &SerialPortInfo,
_opts: &ConnectOpts,
_config: &Config,
) -> Result<Self, Error> {
let serial = serialport::new(&port_info.port_name, 115_200)
.flow_control(FlowControl::None)
.open()
.map_err(Error::from)
.wrap_err_with(|| format!("Failed to open serial port {}", port_info.port_name))?;

) -> Result<Self> {
Ok(Self {
serial_port: serial,
serial_port: open_port(port_info)?,
})
}

Expand Down

0 comments on commit 41ab4aa

Please sign in to comment.