diff --git a/LICENSE-MIT b/LICENSE-MIT index b03fd3c..3e8a085 100644 --- a/LICENSE-MIT +++ b/LICENSE-MIT @@ -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 @@ -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. \ No newline at end of file +DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 3c52340..5b5fa81 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file + +This last step requires IDF to be exported. diff --git a/inactive-rust-toolchain.toml b/inactive-rust-toolchain.toml deleted file mode 100644 index 663c792..0000000 --- a/inactive-rust-toolchain.toml +++ /dev/null @@ -1,3 +0,0 @@ -channel = "esp" -components = ["rustfmt", "rustc-dev"] -targets = ["xtensa-esp32-none-elf"] \ No newline at end of file diff --git a/src/commands.rs b/src/commands.rs index 7686e0f..79db93c 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -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, @@ -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, @@ -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))] @@ -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, @@ -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; } @@ -219,4 +226,4 @@ impl<'a> Response<'a> { self.status = Status::Failure; self.error = error; } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 5062cdd..6f9bc0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -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 {} +} diff --git a/src/miniz_types.rs b/src/miniz_types.rs index 235fe99..815c43d 100644 --- a/src/miniz_types.rs +++ b/src/miniz_types.rs @@ -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 {