Skip to content

Commit

Permalink
move connection_is_usb_otg to Connection struct and rename it to is_u…
Browse files Browse the repository at this point in the history
…sing_usb_otg
  • Loading branch information
JurajSadel committed Mar 3, 2025
1 parent 14dece3 commit d628373
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 96 deletions.
20 changes: 16 additions & 4 deletions espflash/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,15 @@ impl Connection {
}
}
Chip::Esp32p4 => {
let esp32p4 = esp32p4::Esp32p4;
// Check if the connection is USB OTG
if esp32p4.connection_is_usb_otg(self)? {
if self.is_using_usb_otg(chip)? {
wdt_reset(chip, self)?;
}
}
Chip::Esp32s2 => {
let esp32s2 = esp32s2::Esp32s2;
// Check if the connection is USB OTG
if esp32s2.connection_is_usb_otg(self)? {
if self.is_using_usb_otg(chip)? {
// Check the strapping register to see if we can perform RTC WDT
// reset
if esp32s2.can_wtd_reset(self)? {
Expand All @@ -322,7 +321,7 @@ impl Connection {
}
Chip::Esp32s3 => {
let esp32s3 = esp32s3::Esp32s3;
if pid == USB_SERIAL_JTAG_PID || esp32s3.connection_is_usb_otg(self)? {
if pid == USB_SERIAL_JTAG_PID || self.is_using_usb_otg(chip)? {
// Check the strapping register to see if we can perform RTC WDT
// reset
if esp32s3.can_wtd_reset(self)? {
Expand Down Expand Up @@ -573,6 +572,19 @@ impl Connection {
pub(crate) fn is_using_usb_serial_jtag(&self) -> bool {
self.port_info.pid == USB_SERIAL_JTAG_PID
}

#[cfg(feature = "serialport")]
/// Check if the connection is USB OTG
pub(crate) fn is_using_usb_otg(&mut self, chip: Chip) -> Result<bool, Error> {
let (buf_no, no_usb_otg) = match chip {
Chip::Esp32p4 => (esp32p4::UARTDEV_BUF_NO, esp32p4::UARTDEV_BUF_NO_USB_OTG),
Chip::Esp32s2 => (esp32s2::UARTDEV_BUF_NO, esp32s2::UARTDEV_BUF_NO_USB_OTG),
Chip::Esp32s3 => (esp32s3::UARTDEV_BUF_NO, esp32s3::UARTDEV_BUF_NO_USB_OTG),
_ => unreachable!(),
};

Ok(self.read_reg(buf_no)? == no_usb_otg)
}
}

mod encoder {
Expand Down
62 changes: 1 addition & 61 deletions espflash/src/connection/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,73 +200,13 @@ impl ResetStrategy for UsbJtagSerialReset {
}
}

trait RtcWdtReset {
pub(crate) trait RtcWdtReset {
fn wdt_wprotect(&self) -> u32;
fn wdt_wkey(&self) -> u32;
fn wdt_config0(&self) -> u32;
fn wdt_config1(&self) -> u32;
}

impl RtcWdtReset for crate::targets::esp32c3::Esp32c3 {
fn wdt_wprotect(&self) -> u32 {
0x6000_8000 + 0x00A8
}
fn wdt_wkey(&self) -> u32 {
0x50D8_3AA1
}
fn wdt_config0(&self) -> u32 {
0x6000_8000 + 0x0090
}
fn wdt_config1(&self) -> u32 {
0x6000_8000 + 0x0094
}
}

impl RtcWdtReset for crate::targets::esp32p4::Esp32p4 {
fn wdt_wprotect(&self) -> u32 {
0x5011_6000 + 0x0018
}
fn wdt_wkey(&self) -> u32 {
0x50D8_3AA1
}
fn wdt_config0(&self) -> u32 {
0x5011_6000 // no offset here
}
fn wdt_config1(&self) -> u32 {
0x5011_6000 + 0x0004
}
}

impl RtcWdtReset for crate::targets::esp32s2::Esp32s2 {
fn wdt_wprotect(&self) -> u32 {
0x3F40_8000 + 0x00AC
}
fn wdt_wkey(&self) -> u32 {
0x50D8_3AA1
}
fn wdt_config0(&self) -> u32 {
0x3F40_8000 + 0x0094
}
fn wdt_config1(&self) -> u32 {
0x3F40_8000 + 0x0098
}
}

impl RtcWdtReset for crate::targets::esp32s3::Esp32s3 {
fn wdt_wprotect(&self) -> u32 {
0x6000_8000 + 0x00B0
}
fn wdt_wkey(&self) -> u32 {
0x50D8_3AA1
}
fn wdt_config0(&self) -> u32 {
0x6000_8000 + 0x0098
}
fn wdt_config1(&self) -> u32 {
0x6000_8000 + 0x009C
}
}

/// Reset the target device
pub fn reset_after_flash(serial: &mut Port, pid: u16) -> Result<(), serialport::Error> {
sleep(Duration::from_millis(100));
Expand Down
16 changes: 16 additions & 0 deletions espflash/src/targets/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ops::Range;
#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{
connection::reset::RtcWdtReset,
elf::FirmwareImage,
flasher::{FlashData, FlashFrequency},
image_format::IdfBootloaderFormat,
Expand Down Expand Up @@ -123,3 +124,18 @@ impl Target for Esp32c3 {
]
}
}

impl RtcWdtReset for Esp32c3 {
fn wdt_wprotect(&self) -> u32 {
0x6000_8000 + 0x00A8
}
fn wdt_wkey(&self) -> u32 {
0x50D8_3AA1
}
fn wdt_config0(&self) -> u32 {
0x6000_8000 + 0x0090
}
fn wdt_config1(&self) -> u32 {
0x6000_8000 + 0x0094
}
}
28 changes: 19 additions & 9 deletions espflash/src/targets/esp32p4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ops::Range;
#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{
connection::reset::RtcWdtReset,
elf::FirmwareImage,
flasher::{FlashData, FlashFrequency},
image_format::IdfBootloaderFormat,
Expand All @@ -26,6 +27,9 @@ const PARAMS: Esp32Params = Esp32Params::new(
include_bytes!("../../resources/bootloaders/esp32p4-bootloader.bin"),
);

pub(crate) const UARTDEV_BUF_NO: u32 = 0x4FF3_FEC8; // Address which indicates OTG in use
pub(crate) const UARTDEV_BUF_NO_USB_OTG: u32 = 5; // Value of UARTDEV_BUF_NO when OTG is in use

/// ESP32-P4 Target
pub struct Esp32p4;

Expand All @@ -34,15 +38,6 @@ impl Esp32p4 {
pub fn has_magic_value(value: u32) -> bool {
CHIP_DETECT_MAGIC_VALUES.contains(&value)
}

#[cfg(feature = "serialport")]
/// Check if the connection is USB OTG
pub(crate) fn connection_is_usb_otg(&self, connection: &mut Connection) -> Result<bool, Error> {
const UARTDEV_BUF_NO: u32 = 0x4FF3_FEC8; // Address which indicates OTG in use
const UARTDEV_BUF_NO_USB_OTG: u32 = 5; // Value of UARTDEV_BUF_NO when OTG is in use

Ok(connection.read_reg(UARTDEV_BUF_NO)? == UARTDEV_BUF_NO_USB_OTG)
}
}

impl ReadEFuse for Esp32p4 {
Expand Down Expand Up @@ -120,3 +115,18 @@ impl Target for Esp32p4 {
&["riscv32imafc-esp-espidf", "riscv32imafc-unknown-none-elf"]
}
}

impl RtcWdtReset for crate::targets::esp32p4::Esp32p4 {
fn wdt_wprotect(&self) -> u32 {
0x5011_6000 + 0x0018
}
fn wdt_wkey(&self) -> u32 {
0x50D8_3AA1
}
fn wdt_config0(&self) -> u32 {
0x5011_6000 // no offset here
}
fn wdt_config1(&self) -> u32 {
0x5011_6000 + 0x0004
}
}
36 changes: 23 additions & 13 deletions espflash/src/targets/esp32s2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::ops::Range;

#[cfg(feature = "serialport")]
use crate::{connection::Connection, flasher::FLASH_WRITE_SIZE, targets::MAX_RAM_BLOCK_SIZE};
use crate::{
connection::reset::RtcWdtReset,
elf::FirmwareImage,
flasher::{FlashData, FlashFrequency},
image_format::IdfBootloaderFormat,
targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency},
Error,
};
#[cfg(feature = "serialport")]
use crate::{connection::Connection, flasher::FLASH_WRITE_SIZE, targets::MAX_RAM_BLOCK_SIZE};

const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[0x0000_07c6];

Expand All @@ -29,19 +30,13 @@ const PARAMS: Esp32Params = Esp32Params::new(
include_bytes!("../../resources/bootloaders/esp32s2-bootloader.bin"),
);

pub(crate) const UARTDEV_BUF_NO: u32 = 0x3FFF_FD14; // Address which indicates OTG in use
pub(crate) const UARTDEV_BUF_NO_USB_OTG: u32 = 2; // Value of UARTDEV_BUF_NO when OTG is in use

/// ESP32-S2 Target
pub struct Esp32s2;

impl Esp32s2 {
#[cfg(feature = "serialport")]
/// Check if the connection is USB OTG
pub(crate) fn connection_is_usb_otg(&self, connection: &mut Connection) -> Result<bool, Error> {
const UARTDEV_BUF_NO: u32 = 0x3FFF_FD14; // Address which indicates OTG in use
const UARTDEV_BUF_NO_USB_OTG: u32 = 2; // Value of UARTDEV_BUF_NO when OTG is in use

Ok(connection.read_reg(UARTDEV_BUF_NO)? == UARTDEV_BUF_NO_USB_OTG)
}

#[cfg(feature = "serialport")]
/// Return the block2 version based on eFuses
fn get_block2_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Expand Down Expand Up @@ -152,7 +147,7 @@ impl Target for Esp32s2 {

#[cfg(feature = "serialport")]
fn flash_write_size(&self, connection: &mut Connection) -> Result<usize, Error> {
Ok(if self.connection_is_usb_otg(connection)? {
Ok(if connection.is_using_usb_otg(Chip::Esp32s2)? {
MAX_USB_BLOCK_SIZE
} else {
FLASH_WRITE_SIZE
Expand Down Expand Up @@ -188,7 +183,7 @@ impl Target for Esp32s2 {

#[cfg(feature = "serialport")]
fn max_ram_block_size(&self, connection: &mut Connection) -> Result<usize, Error> {
Ok(if self.connection_is_usb_otg(connection)? {
Ok(if connection.is_using_usb_otg(Chip::Esp32s2)? {
MAX_USB_BLOCK_SIZE
} else {
MAX_RAM_BLOCK_SIZE
Expand All @@ -211,3 +206,18 @@ impl Target for Esp32s2 {
&["xtensa-esp32s2-none-elf", "xtensa-esp32s2-espidf"]
}
}

impl RtcWdtReset for Esp32s2 {
fn wdt_wprotect(&self) -> u32 {
0x3F40_8000 + 0x00AC
}
fn wdt_wkey(&self) -> u32 {
0x50D8_3AA1
}
fn wdt_config0(&self) -> u32 {
0x3F40_8000 + 0x0094
}
fn wdt_config1(&self) -> u32 {
0x3F40_8000 + 0x0098
}
}
28 changes: 19 additions & 9 deletions espflash/src/targets/esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ops::Range;
#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{
connection::reset::RtcWdtReset,
elf::FirmwareImage,
flasher::{FlashData, FlashFrequency},
image_format::IdfBootloaderFormat,
Expand All @@ -26,6 +27,9 @@ const PARAMS: Esp32Params = Esp32Params::new(
include_bytes!("../../resources/bootloaders/esp32s3-bootloader.bin"),
);

pub(crate) const UARTDEV_BUF_NO: u32 = 0x3FCE_F14C; // Address which indicates OTG in use
pub(crate) const UARTDEV_BUF_NO_USB_OTG: u32 = 3; // Value of UARTDEV_BUF_NO when OTG is in use

/// ESP32-S2 Target
pub struct Esp32s3;

Expand All @@ -42,15 +46,6 @@ impl Esp32s3 {
Ok((self.read_efuse(connection, 20)? >> 24) & 0x7)
}

#[cfg(feature = "serialport")]
/// Check if the connection is USB OTG
pub(crate) fn connection_is_usb_otg(&self, connection: &mut Connection) -> Result<bool, Error> {
const UARTDEV_BUF_NO: u32 = 0x3FCE_F14C; // Address which indicates OTG in use
const UARTDEV_BUF_NO_USB_OTG: u32 = 3; // Value of UARTDEV_BUF_NO when OTG is in use

Ok(connection.read_reg(UARTDEV_BUF_NO)? == UARTDEV_BUF_NO_USB_OTG)
}

#[cfg(feature = "serialport")]
/// Check the strapping register to see if we can perform RTC WDT reset
pub(crate) fn can_wtd_reset(&self, connection: &mut Connection) -> Result<bool, Error> {
Expand Down Expand Up @@ -161,3 +156,18 @@ impl Target for Esp32s3 {
&["xtensa-esp32s3-none-elf", "xtensa-esp32s3-espidf"]
}
}

impl RtcWdtReset for Esp32s3 {
fn wdt_wprotect(&self) -> u32 {
0x6000_8000 + 0x00B0
}
fn wdt_wkey(&self) -> u32 {
0x50D8_3AA1
}
fn wdt_config0(&self) -> u32 {
0x6000_8000 + 0x0098
}
fn wdt_config1(&self) -> u32 {
0x6000_8000 + 0x009C
}
}

0 comments on commit d628373

Please sign in to comment.