Skip to content

Commit

Permalink
fix: Fix targets mod
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioGasquez committed Jan 30, 2024
1 parent 918c59f commit b95199d
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 20 deletions.
13 changes: 9 additions & 4 deletions espflash/src/targets/esp32.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::ops::Range;

#[cfg(feature = "serialport")]
use crate::{connection::Connection, targets::bytes_to_mac_addr};
use crate::{
connection::Connection,
elf::FirmwareImage,
error::{Error, UnsupportedImageFormatError},
flasher::{FlashData, FlashFrequency},
image_format::{IdfBootloaderFormat, ImageFormat, ImageFormatKind},
targets::{
bytes_to_mac_addr, Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency,
},
targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency},
};

const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[0x00f0_1d83];
Expand All @@ -32,6 +31,7 @@ impl Esp32 {
CHIP_DETECT_MAGIC_VALUES.contains(&value)
}

#[cfg(feature = "serialport")]
/// Return the package version based on the eFuses
fn package_version(&self, connection: &mut Connection) -> Result<u32, Error> {
let word3 = self.read_efuse(connection, 3)?;
Expand All @@ -54,6 +54,7 @@ impl Target for Esp32 {
FLASH_RANGES.iter().any(|range| range.contains(&addr))
}

#[cfg(feature = "serialport")]
fn chip_features(&self, connection: &mut Connection) -> Result<Vec<&str>, Error> {
let word3 = self.read_efuse(connection, 3)?;
let word4 = self.read_efuse(connection, 4)?;
Expand Down Expand Up @@ -112,6 +113,7 @@ impl Target for Esp32 {
Ok(features)
}

#[cfg(feature = "serialport")]
fn major_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
let apb_ctl_date = connection.read_reg(0x3FF6_607C)?;

Expand All @@ -129,10 +131,12 @@ impl Target for Esp32 {
}
}

#[cfg(feature = "serialport")]
fn minor_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok((self.read_efuse(connection, 5)? >> 24) & 0x3)
}

#[cfg(feature = "serialport")]
fn crystal_freq(&self, connection: &mut Connection) -> Result<XtalFrequency, Error> {
let uart_div = connection.read_reg(UART_CLKDIV_REG)? & UART_CLKDIV_MASK;
let est_xtal = (connection.get_baud()? * uart_div) / 1_000_000 / XTAL_CLK_DIVIDER;
Expand Down Expand Up @@ -196,6 +200,7 @@ impl Target for Esp32 {
}
}

#[cfg(feature = "serialport")]
fn mac_address(&self, connection: &mut Connection) -> Result<String, Error> {
let word1 = self.read_efuse(connection, 1)?;
let word2 = self.read_efuse(connection, 2)?;
Expand Down
12 changes: 8 additions & 4 deletions espflash/src/targets/esp32c2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::{collections::HashMap, ops::Range};

#[cfg(feature = "serialport")]
use crate::{connection::Connection, targets::bytes_to_mac_addr};
use crate::{
connection::Connection,
elf::FirmwareImage,
error::Error,
flasher::{FlashData, FlashFrequency},
image_format::{DirectBootFormat, IdfBootloaderFormat, ImageFormat, ImageFormatKind},
targets::{
bytes_to_mac_addr, Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency,
},
targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency},
};

const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[
Expand Down Expand Up @@ -47,18 +46,22 @@ impl Target for Esp32c2 {
FLASH_RANGES.iter().any(|range| range.contains(&addr))
}

#[cfg(feature = "serialport")]
fn chip_features(&self, _connection: &mut Connection) -> Result<Vec<&str>, Error> {
Ok(vec!["WiFi", "BLE"])
}

#[cfg(feature = "serialport")]
fn major_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok(self.read_efuse(connection, 17)? >> 20 & 0x3)
}

#[cfg(feature = "serialport")]
fn minor_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok(self.read_efuse(connection, 17)? >> 16 & 0xf)
}

#[cfg(feature = "serialport")]
fn crystal_freq(&self, connection: &mut Connection) -> Result<XtalFrequency, Error> {
let uart_div = connection.read_reg(UART_CLKDIV_REG)? & UART_CLKDIV_MASK;
let est_xtal = (connection.get_baud()? * uart_div) / 1_000_000 / XTAL_CLK_DIVIDER;
Expand Down Expand Up @@ -132,6 +135,7 @@ impl Target for Esp32c2 {
}
}

#[cfg(feature = "serialport")]
/// What is the MAC address?
fn mac_address(&self, connection: &mut Connection) -> Result<String, Error> {
let word5 = self.read_efuse(connection, 16)?;
Expand Down
7 changes: 6 additions & 1 deletion espflash/src/targets/esp32c3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::ops::Range;

#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{
connection::Connection,
elf::FirmwareImage,
error::{Error, UnsupportedImageFormatError},
flasher::{FlashData, FlashFrequency},
Expand Down Expand Up @@ -51,21 +52,25 @@ impl Target for Esp32c3 {
FLASH_RANGES.iter().any(|range| range.contains(&addr))
}

#[cfg(feature = "serialport")]
fn chip_features(&self, _connection: &mut Connection) -> Result<Vec<&str>, Error> {
Ok(vec!["WiFi", "BLE"])
}

#[cfg(feature = "serialport")]
fn major_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok(self.read_efuse(connection, 22)? >> 24 & 0x3)
}

#[cfg(feature = "serialport")]
fn minor_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
let hi = self.read_efuse(connection, 22)? >> 23 & 0x1;
let lo = self.read_efuse(connection, 20)? >> 18 & 0x7;

Ok((hi << 3) + lo)
}

#[cfg(feature = "serialport")]
fn crystal_freq(&self, _connection: &mut Connection) -> Result<XtalFrequency, Error> {
// The ESP32-C3's XTAL has a fixed frequency of 40MHz.
Ok(XtalFrequency::_40Mhz)
Expand Down
7 changes: 6 additions & 1 deletion espflash/src/targets/esp32c6.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::ops::Range;

#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{
connection::Connection,
elf::FirmwareImage,
error::Error,
flasher::{FlashData, FlashFrequency},
Expand Down Expand Up @@ -46,21 +47,25 @@ impl Target for Esp32c6 {
FLASH_RANGES.iter().any(|range| range.contains(&addr))
}

#[cfg(feature = "serialport")]
fn chip_features(&self, _connection: &mut Connection) -> Result<Vec<&str>, Error> {
Ok(vec!["WiFi 6", "BT 5"])
}

#[cfg(feature = "serialport")]
fn major_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok((self.read_efuse(connection, 22)? >> 24) & 0x3)
}

#[cfg(feature = "serialport")]
fn minor_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
let hi = (self.read_efuse(connection, 22)? >> 23) & 0x1;
let lo = (self.read_efuse(connection, 20)? >> 18) & 0x7;

Ok((hi << 3) + lo)
}

#[cfg(feature = "serialport")]
fn crystal_freq(&self, _connection: &mut Connection) -> Result<XtalFrequency, Error> {
// The ESP32-C6's XTAL has a fixed frequency of 40MHz.
Ok(XtalFrequency::_40Mhz)
Expand Down
10 changes: 7 additions & 3 deletions espflash/src/targets/esp32h2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::collections::HashMap;
use std::ops::Range;

use super::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target};
#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{
connection::Connection,
elf::FirmwareImage,
error::Error,
flasher::{FlashData, FlashFrequency},
image_format::{DirectBootFormat, IdfBootloaderFormat, ImageFormat, ImageFormatKind},
targets::XtalFrequency,
targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency},
};

const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[0xD7B7_3E80];
Expand Down Expand Up @@ -47,21 +47,25 @@ impl Target for Esp32h2 {
FLASH_RANGES.iter().any(|range| range.contains(&addr))
}

#[cfg(feature = "serialport")]
fn chip_features(&self, _connection: &mut Connection) -> Result<Vec<&str>, Error> {
Ok(vec!["BLE"])
}

#[cfg(feature = "serialport")]
fn major_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok((self.read_efuse(connection, 22)? >> 24) & 0x3)
}

#[cfg(feature = "serialport")]
fn minor_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
let hi = (self.read_efuse(connection, 22)? >> 23) & 0x1;
let lo = (self.read_efuse(connection, 20)? >> 18) & 0x7;

Ok((hi << 3) + lo)
}

#[cfg(feature = "serialport")]
fn crystal_freq(&self, _connection: &mut Connection) -> Result<XtalFrequency, Error> {
// The ESP32-H2's XTAL has a fixed frequency of 32MHz.
Ok(XtalFrequency::_32Mhz)
Expand Down
10 changes: 7 additions & 3 deletions espflash/src/targets/esp32p4.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::ops::Range;

use super::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target};
#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{
connection::Connection,
elf::FirmwareImage,
error::Error,
flasher::{FlashData, FlashFrequency},
image_format::{DirectBootFormat, IdfBootloaderFormat, ImageFormat, ImageFormatKind},
targets::XtalFrequency,
targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, XtalFrequency},
};

const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[0x0];
Expand Down Expand Up @@ -46,20 +46,24 @@ impl Target for Esp32p4 {
FLASH_RANGES.iter().any(|range| range.contains(&addr))
}

#[cfg(feature = "serialport")]
fn chip_features(&self, _connection: &mut Connection) -> Result<Vec<&str>, Error> {
Ok(vec!["High-Performance MCU"])
}

#[cfg(feature = "serialport")]
fn major_chip_version(&self, _connection: &mut Connection) -> Result<u32, Error> {
// TODO: https://github.com/espressif/esptool/blob/master/esptool/targets/esp32p4.py#L96
Ok(0)
}

#[cfg(feature = "serialport")]
fn minor_chip_version(&self, _connection: &mut Connection) -> Result<u32, Error> {
// TODO: https://github.com/espressif/esptool/blob/master/esptool/targets/esp32p4.py#L92
Ok(0)
}

#[cfg(feature = "serialport")]
fn crystal_freq(&self, _connection: &mut Connection) -> Result<XtalFrequency, Error> {
// The ESP32-P4's XTAL has a fixed frequency of 40MHz.
Ok(XtalFrequency::_40Mhz)
Expand Down
13 changes: 12 additions & 1 deletion espflash/src/targets/esp32s2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::ops::Range;

#[cfg(feature = "serialport")]
use crate::connection::Connection;
use crate::{
connection::Connection,
elf::FirmwareImage,
error::{Error, UnsupportedImageFormatError},
flasher::{FlashData, FlashFrequency, FLASH_WRITE_SIZE},
Expand Down Expand Up @@ -33,6 +34,7 @@ const PARAMS: Esp32Params = Esp32Params::new(
pub struct Esp32s2;

impl Esp32s2 {
#[cfg(feature = "serialport")]
/// Return if the connection is USB OTG
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
Expand All @@ -41,6 +43,7 @@ impl Esp32s2 {
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> {
let blk2_word4 = self.read_efuse(connection, 27)?;
Expand All @@ -49,6 +52,7 @@ impl Esp32s2 {
Ok(block2_version)
}

#[cfg(feature = "serialport")]
/// Return the flash version based on eFuses
fn get_flash_version(&self, connection: &mut Connection) -> Result<u32, Error> {
let blk1_word3 = self.read_efuse(connection, 20)?;
Expand All @@ -57,6 +61,7 @@ impl Esp32s2 {
Ok(flash_version)
}

#[cfg(feature = "serialport")]
/// Return the PSRAM version based on eFuses
fn get_psram_version(&self, connection: &mut Connection) -> Result<u32, Error> {
let blk1_word3 = self.read_efuse(connection, 20)?;
Expand All @@ -82,6 +87,7 @@ impl Target for Esp32s2 {
FLASH_RANGES.iter().any(|range| range.contains(&addr))
}

#[cfg(feature = "serialport")]
fn chip_features(&self, connection: &mut Connection) -> Result<Vec<&str>, Error> {
let mut features = vec!["WiFi"];

Expand Down Expand Up @@ -112,22 +118,26 @@ impl Target for Esp32s2 {
Ok(features)
}

#[cfg(feature = "serialport")]
fn major_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok(self.read_efuse(connection, 20)? >> 18 & 0x3)
}

#[cfg(feature = "serialport")]
fn minor_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
let hi = self.read_efuse(connection, 20)? >> 20 & 0x1;
let lo = self.read_efuse(connection, 21)? >> 4 & 0x7;

Ok((hi << 3) + lo)
}

#[cfg(feature = "serialport")]
fn crystal_freq(&self, _connection: &mut Connection) -> Result<XtalFrequency, Error> {
// The ESP32-S2's XTAL has a fixed frequency of 40MHz.
Ok(XtalFrequency::_40Mhz)
}

#[cfg(feature = "serialport")]
fn flash_write_size(&self, connection: &mut Connection) -> Result<usize, Error> {
Ok(if self.connection_is_usb_otg(connection)? {
MAX_USB_BLOCK_SIZE
Expand Down Expand Up @@ -170,6 +180,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)? {
MAX_USB_BLOCK_SIZE
Expand Down
Loading

0 comments on commit b95199d

Please sign in to comment.