Skip to content

Commit

Permalink
Initial support for increased flashing speeds for the ESP32
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev committed Sep 20, 2020
1 parent 4f21404 commit cd4ded3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
10 changes: 7 additions & 3 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ fn main() -> Result<(), MainError> {

let port = args.serial.or(config.connection.serial).unwrap();

let speed = args.speed.map(|v| BaudRate::from_speed(v as usize));

let chip = args
.chip
.as_ref()
Expand Down Expand Up @@ -63,11 +65,10 @@ fn main() -> Result<(), MainError> {
let mut serial = serial::open(&port)?;
serial.reconfigure(&|settings| {
settings.set_baud_rate(BaudRate::Baud115200)?;

Ok(())
})?;

let mut flasher = Flasher::connect(serial)?;
let mut flasher = Flasher::connect(serial, speed)?;
if args.board_info {
return board_info(&flasher);
}
Expand All @@ -94,6 +95,7 @@ struct AppArgs {
example: Option<String>,
chip: Option<String>,
build_tool: Option<String>,
speed: Option<u32>,
serial: Option<String>,
}

Expand All @@ -105,6 +107,7 @@ fn usage() -> Result<(), MainError> {
[--example EXAMPLE] \
[--tool {{cargo,xargo,xbuild}}] \
[--chip {{esp32,esp8266}}] \
[--speed BAUD] \
<serial>";

println!("{}", usage);
Expand Down Expand Up @@ -136,6 +139,7 @@ fn parse_args() -> Result<AppArgs, MainError> {
release: args.contains("--release"),
example: args.opt_value_from_str("--example")?,
chip: args.opt_value_from_str("--chip")?,
speed: args.opt_value_from_str("--speed")?,
build_tool: args.opt_value_from_str("--tool")?,
serial: args.free_from_str()?,
};
Expand Down Expand Up @@ -217,7 +221,7 @@ fn build(release: bool, example: &Option<String>, tool: &str, target: &str) -> E

fn chip_detect(port: &str) -> Option<&'static str> {
let serial = serial::open(port).ok()?;
let flasher = Flasher::connect(serial).ok()?;
let flasher = Flasher::connect(serial, None).ok()?;

let chip = match flasher.chip() {
Chip::Esp8266 => "esp8266",
Expand Down
11 changes: 9 additions & 2 deletions espflash/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::io::Write;
use std::io::{Write};
use std::thread::sleep;
use std::time::Duration;

use crate::encoder::SlipEncoder;
use crate::error::{Error, RomError};
use binread::io::Cursor;
use binread::{BinRead, BinReaderExt};
use serial::SerialPort;
use serial::{SerialPort, BaudRate, SerialPortSettings};
use slip_codec::Decoder;

pub struct Connection {
Expand Down Expand Up @@ -66,6 +66,13 @@ impl Connection {
Ok(())
}

pub fn set_baud(&mut self, speed: BaudRate) -> Result<(), Error> {
self.serial.reconfigure(&|setup: &mut dyn SerialPortSettings| {
setup.set_baud_rate(speed)
})?;
Ok(())
}

pub fn with_timeout<T, F: FnMut(&mut Connection) -> Result<T, Error>>(
&mut self,
timeout: Duration,
Expand Down
25 changes: 20 additions & 5 deletions espflash/src/flasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::error::RomError;
use crate::Error;
use bytemuck::__core::time::Duration;
use bytemuck::{bytes_of, Pod, Zeroable};
use serial::SerialPort;
use serial::{SerialPort,BaudRate};
use std::thread::sleep;

type Encoder<'a> = SlipEncoder<'a, Box<dyn SerialPort>>;
Expand Down Expand Up @@ -38,6 +38,7 @@ enum Command {
ReadReg = 0x0a,
SpiSetParams = 0x0B,
SpiAttach = 0x0D,
ChangeBaud = 0x0F,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -109,9 +110,9 @@ pub struct Flasher {
}

impl Flasher {
pub fn connect(serial: impl SerialPort + 'static) -> Result<Self, Error> {
pub fn connect(serial: impl SerialPort + 'static, speed: Option<BaudRate>) -> Result<Self, Error> {
let mut flasher = Flasher {
connection: Connection::new(serial),
connection: Connection::new(serial), // default baud is always 115200
chip: Chip::Esp8266, // dummy, set properly later
flash_size: FlashSize::Flash4MB,
};
Expand All @@ -121,6 +122,16 @@ impl Flasher {
flasher.enable_flash()?;
flasher.flash_detect()?;

if let Some(b) = speed {
match flasher.chip {
Chip::Esp8266 => (), /* Not available */
Chip::Esp32 => if b.speed() > BaudRate::Baud115200.speed() {
println!("WARN setting baud rate higher than 115200 can cause issues.");
flasher.change_baud(b)?;
}
}
}

Ok(flasher)
}

Expand Down Expand Up @@ -369,7 +380,6 @@ impl Flasher {
///
/// Note that this will not touch the flash on the device
pub fn load_elf_to_ram(&mut self, elf_data: &[u8]) -> Result<(), Error> {
self.start_connection()?;
let image = FirmwareImage::from_data(elf_data).map_err(|_| Error::InvalidElf)?;

if image.rom_segments(self.chip).next().is_some() {
Expand Down Expand Up @@ -401,7 +411,6 @@ impl Flasher {

/// Load an elf image to flash and execute it
pub fn load_elf_to_flash(&mut self, elf_data: &[u8]) -> Result<(), Error> {
self.start_connection()?;
self.enable_flash()?;
let mut image = FirmwareImage::from_data(elf_data).map_err(|_| Error::InvalidElf)?;
image.flash_size = self.flash_size();
Expand Down Expand Up @@ -436,6 +445,12 @@ impl Flasher {

Ok(())
}

pub fn change_baud(&mut self, speed: BaudRate) -> Result<(), Error> {
self.connection.command(Command::ChangeBaud as u8, &(speed.speed()).to_le_bytes()[..], 0)?;
self.connection.set_baud(speed)?;
Ok(())
}
}

fn get_erase_size(offset: usize, size: usize) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() -> Result<(), MainError> {
Ok(())
})?;

let mut flasher = Flasher::connect(serial)?;
let mut flasher = Flasher::connect(serial, None)?;

if board_info {
println!("Chip type: {:?}", flasher.chip());
Expand Down

0 comments on commit cd4ded3

Please sign in to comment.