Skip to content

Commit

Permalink
Miscellaneous cleanup and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebraham committed Oct 5, 2022
1 parent a230ef8 commit 9fc6dad
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 85 deletions.
4 changes: 2 additions & 2 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2019-2020 Contributors to xtensa-lx6-rt
Copyright 2022 esp-rs

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
Expand All @@ -22,4 +22,4 @@ SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
DEALINGS IN THE SOFTWARE.
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
# esp-flasher-stub

Rust implementation of flasher stub located in esptool.
Currently only supports ESP32C3 through UART.
Rust implementation of flasher stub located in [esptool](https://github.com/espressif/esptool/).

Currently only supports ESP32-C3 through UART.

## Build

```
cargo build
```

## Test

```
cargo test --target=x86_64-unknown-linux-gnu
```

## Run esptool test
Since esptool uses precompiled stub binaries located in `stub_flasher.py`,

Since esptool uses precompiled stub binaries located in `stub_flasher.py`,
binary for ESP32C3 has to be replaced the one otained from `esp-flasher-stub`.

In order to run `test_esptool.py` follow steps below:
* Build `esp-flasher-stub` with `cargo build --release`
* Clone esptool to the same directory where `esp-flasher-stub` resides.

- Build `esp-flasher-stub` with `cargo build --release`
- Clone esptool to the same directory where `esp-flasher-stub` resides.

```
git clone https://github.com/espressif/esptool
```
* Navigate to `esptool`, checkout version for which patch located in `esp-flasher-stub` directory was created and apply it.

- Navigate to `esptool`, checkout version for which patch located in `esp-flasher-stub` directory was created and apply it.

```
cd esptool
git checkout 6488ebb
git am ../../esp-flasher-stub/esptool.patch
```
* Regenerate `stub_flasher.py` by running patched Makefile and run the tests

- Regenerate `stub_flasher.py` by running patched Makefile and run the tests

```
cd test
make -C ../flasher_stub/ && python test_esptool.py /dev/ttyUSB0 esp32c3 115200
```
This last step requires IDF to be exported.

This last step requires IDF to be exported.
3 changes: 0 additions & 3 deletions inactive-rust-toolchain.toml

This file was deleted.

27 changes: 17 additions & 10 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[allow(unused)]
#[repr(u8)]
#[derive(PartialEq, Copy, Clone, Debug)]
pub enum Error
{
pub enum Error {
NoError = 0,
BadDataLen = 0xC0,
BadDataChecksum = 0xC1,
Expand All @@ -22,9 +22,9 @@ pub enum Error
Err0x35 = 0x35,
}

#[allow(unused)]
#[derive(PartialEq, Copy, Clone, Debug)]
pub enum Code
{
pub enum Code {
FlashBegin = 0x02,
FlashData = 0x03,
FlashEnd = 0x04,
Expand Down Expand Up @@ -169,13 +169,20 @@ pub struct ReadFlash {
pub params: ReadFlashParams,
}

#[allow(unused)]
#[repr(u8)]
#[derive(PartialEq, Copy, Clone, Debug)]
enum Direction { In, Out }
enum Direction {
In,
Out,
}

#[repr(u8)]
#[derive(PartialEq, Copy, Clone)]
enum Status { Success, Failure }
enum Status {
Success,
Failure,
}

#[derive(PartialEq, Copy, Clone)]
#[repr(C, packed(1))]
Expand All @@ -186,14 +193,13 @@ pub struct Response<'a> {
value: u32,
status: Status,
error: Error,
pub data: &'a[u8]
pub data: &'a [u8],
}

// Size of sesponse without data reference
pub const RESPONSE_SIZE: usize = 10;

impl<'a> Response<'a> {

pub fn new(cmd: Code) -> Self {
Response {
direction: Direction::Out,
Expand All @@ -210,7 +216,8 @@ impl<'a> Response<'a> {
self.value = value;
}

pub fn data(&mut self, data: &'a[u8]) {
#[allow(unused)]
pub fn data(&mut self, data: &'a [u8]) {
self.size = 2 + data.len() as u16;
self.data = data;
}
Expand All @@ -219,4 +226,4 @@ impl<'a> Response<'a> {
self.status = Status::Failure;
self.error = error;
}
}
}
114 changes: 52 additions & 62 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_main)]
#![allow(dead_code)]

use esp32c3_hal::interrupt;
use core::panic::PanicInfo;

use esp32c3_hal::{
clock::ClockControl,
interrupt::{self, CpuInterrupt, InterruptKind, Priority},
pac,
prelude::SystemExt,
serial::{config::Config, Serial, TxRxPins},
Cpu,
IO,
};
use riscv_rt::entry;

use crate::{protocol::Stub, targets::esp32c3 as target};

mod commands;
mod dprint;
Expand All @@ -11,71 +23,49 @@ mod protocol;
mod serial_io;
mod targets;

// #[cfg(not(test))]
mod main {
use riscv_rt::entry;
use core::panic::PanicInfo;
use crate::{
protocol::Stub,
targets::esp32c3 as target,
dprintln,
};
const MSG_BUFFER_SIZE: usize = target::MAX_WRITE_BLOCK + 0x400;

use esp32c3_hal::IO;
use esp_hal_common::{
pac,
serial::{ Serial, TxRxPins, config::Config},
clock::ClockControl,
prelude::SystemExt,
interrupt,
interrupt::*,
interrupt::CpuInterrupt::*,
Cpu::*,
};
#[entry]
fn main() -> ! {
let peripherals = pac::Peripherals::take().unwrap();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

const MSG_BUFFER_SIZE: usize = target::MAX_WRITE_BLOCK + 0x400;
// Init debug UART
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let pins = TxRxPins::new_tx_rx(
io.pins.gpio10.into_push_pull_output(),
io.pins.gpio9.into_floating_input(),
);
let cfg = Config::default().baudrate(921600);
let _ = Serial::new_with_config(peripherals.UART1, Some(cfg), Some(pins), &clocks);

#[entry]
fn main() -> ! {
let mut buffer: [u8; MSG_BUFFER_SIZE] = [0; MSG_BUFFER_SIZE];
// Init IO serial
let mut serial = Serial::new(peripherals.UART0);
serial.set_rx_fifo_full_threshold(1);
serial.listen_rx_fifo_full();

let peripherals = pac::Peripherals::take().unwrap();

// Init debug UART
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let pins = TxRxPins::new_tx_rx(
io.pins.gpio10.into_push_pull_output(),
io.pins.gpio9.into_floating_input(),
);
let system = peripherals.SYSTEM.split();
let cfg = Config::default().baudrate(921600);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let _ = Serial::new_with_config(peripherals.UART1, Some(cfg), Some(pins), &clocks);

// Init IO serial
let mut serial = Serial::new(peripherals.UART0);
serial.set_rx_fifo_full_threshold(1);
serial.listen_rx_fifo_full();
interrupt::enable( pac::Interrupt::UART0, interrupt::Priority::Priority3 ).ok();
interrupt::set_kind( ProCpu, Interrupt3, InterruptKind::Level );
unsafe{ interrupt::set_priority( ProCpu, Interrupt3, Priority::Priority10 ) };
unsafe { riscv::interrupt::enable(); }

let mut stub = Stub::new(&mut serial);

stub.send_greeting();

target::init();

loop {
let data = stub.read_command(&mut buffer);
stub.process_command(data);
}
interrupt::enable(pac::Interrupt::UART0, interrupt::Priority::Priority3).ok();
interrupt::set_kind(Cpu::ProCpu, CpuInterrupt::Interrupt3, InterruptKind::Level);
unsafe {
interrupt::set_priority(Cpu::ProCpu, CpuInterrupt::Interrupt3, Priority::Priority10);
riscv::interrupt::enable();
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
dprintln!("Panic !!!");
loop {}
let mut stub = Stub::new(&mut serial);
stub.send_greeting();

target::init();

let mut buffer: [u8; MSG_BUFFER_SIZE] = [0; MSG_BUFFER_SIZE];
loop {
let data = stub.read_command(&mut buffer);
stub.process_command(data);
}
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
dprintln!("Panic !!!");
loop {}
}
1 change: 1 addition & 0 deletions src/miniz_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const TINFL_FAST_LOOKUP_SIZE: usize = 1 << TINFL_FAST_LOOKUP_BITS;
pub const TINFL_FLAG_PARSE_ZLIB_HEADER: u32 = 1;
pub const TINFL_FLAG_HAS_MORE_INPUT: u32 = 2;

#[allow(unused)]
#[repr(C)]
#[derive(PartialEq, PartialOrd)]
pub enum TinflStatus {
Expand Down

0 comments on commit 9fc6dad

Please sign in to comment.