Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for flashing the ESP32-C6 #317

Merged
merged 2 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Serial flasher utilities for Espressif devices, based loosely on [esptool.py](https://github.com/espressif/esptool/).

Supports the **ESP32**, **ESP32-C2/C3**, **ESP32-S2/S3**, and **ESP8266**.
Supports the **ESP32**, **ESP32-C2/C3/C6**, **ESP32-S2/S3**, and **ESP8266**.

## [cargo-espflash](./cargo-espflash/)

Expand Down
2 changes: 1 addition & 1 deletion cargo-espflash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Cross-compiler and Cargo extension for flashing Espressif devices over serial.

Supports the **ESP32**, **ESP32-C2/C3**, **ESP32-S2/S3**, and **ESP8266**.
Supports the **ESP32**, **ESP32-C2/C3/C6**, **ESP32-S2/S3**, and **ESP8266**.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion espflash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

A library and command-line tool for flashing Espressif devices over serial.

Supports the **ESP32**, **ESP32-C2/C3**, **ESP32-S2/S3**, and **ESP8266**.
Supports the **ESP32**, **ESP32-C2/C3/C6**, **ESP32-S2/S3**, and **ESP8266**.

## Installation

Expand Down
Binary file not shown.
5 changes: 5 additions & 0 deletions espflash/resources/stubs/stub_flasher_32c6.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
entry = 1_082_131_920
text = "ARG3BwBgSsgDqYcAJspOxlLEBs4izLcEAGD9WTcKhEDATBN09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hZ+Oa9eZmhe/wr9MjoJQBnbXjHwnm44kHnJMHgAwjqvkA2bKcROORB5wBRZfwf//ngEA3CWUTBQVxl/B//+eAIDOX8H//54CgN3m6wETjDQSYAUWX8H//54DANBMFgD6X8H//54DAMAKUvbr2UGZU1lRGWbZZJlqWWgZb9ktmTNZMRk22TQlhgoAAAA=="
text_start = 1_082_130_432
data = "DACEQA=="
data_start = 1_082_469_288
2 changes: 2 additions & 0 deletions espflash/src/flasher/stubs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct FlashStub {
const STUB_32: &str = include_str!("../../resources/stubs/stub_flasher_32.toml");
const STUB_32C2: &str = include_str!("../../resources/stubs/stub_flasher_32c2.toml");
const STUB_32C3: &str = include_str!("../../resources/stubs/stub_flasher_32c3.toml");
const STUB_32C6: &str = include_str!("../../resources/stubs/stub_flasher_32c6.toml");
const STUB_32S2: &str = include_str!("../../resources/stubs/stub_flasher_32s2.toml");
const STUB_32S3: &str = include_str!("../../resources/stubs/stub_flasher_32s3.toml");
const STUB_8266: &str = include_str!("../../resources/stubs/stub_flasher_8266.toml");
Expand All @@ -33,6 +34,7 @@ impl FlashStub {
Chip::Esp32 => STUB_32,
Chip::Esp32c2 => STUB_32C2,
Chip::Esp32c3 => STUB_32C3,
Chip::Esp32c6 => STUB_32C6,
Chip::Esp32s2 => STUB_32S2,
Chip::Esp32s3 => STUB_32S3,
Chip::Esp8266 => STUB_8266,
Expand Down
120 changes: 120 additions & 0 deletions espflash/src/targets/esp32c6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use std::ops::Range;

use esp_idf_part::PartitionTable;

use super::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target};
use crate::{
connection::Connection,
elf::FirmwareImage,
error::Error,
flasher::{FlashFrequency, FlashMode, FlashSize},
image_format::{DirectBootFormat, IdfBootloaderFormat, ImageFormat, ImageFormatKind},
};

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

const FLASH_RANGES: &[Range<u32>] = &[
0x4200_0000..0x4280_0000, // IROM
0x4280_0000..0x4300_0000, // DROM
];

const PARAMS: Esp32Params = Esp32Params::new(
0x0,
0x1_0000,
0x3f_0000,
13,
include_bytes!("../../resources/bootloaders/esp32c6-bootloader.bin"),
);

/// ESP32-C6 Target
pub struct Esp32c6;

impl Esp32c6 {
pub fn has_magic_value(value: u32) -> bool {
CHIP_DETECT_MAGIC_VALUES.contains(&value)
}
}

impl ReadEFuse for Esp32c6 {
fn efuse_reg(&self) -> u32 {
0x600B_0800
}
}

impl Target for Esp32c6 {
fn addr_is_flash(&self, addr: u32) -> bool {
FLASH_RANGES.iter().any(|range| range.contains(&addr))
}

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

fn major_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> {
Ok((self.read_efuse(connection, 22)? >> 24) & 0x3)
}

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)
}

fn crystal_freq(&self, _connection: &mut Connection) -> Result<u32, Error> {
// The ESP32-C6's XTAL has a fixed frequency of 40MHz.
Ok(40)
}

fn get_flash_image<'a>(
&self,
image: &'a dyn FirmwareImage<'a>,
bootloader: Option<Vec<u8>>,
partition_table: Option<PartitionTable>,
image_format: Option<ImageFormatKind>,
_chip_revision: Option<(u32, u32)>,
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
flash_freq: Option<FlashFrequency>,
) -> Result<Box<dyn ImageFormat<'a> + 'a>, Error> {
let image_format = image_format.unwrap_or(ImageFormatKind::EspBootloader);

match image_format {
ImageFormatKind::EspBootloader => Ok(Box::new(IdfBootloaderFormat::new(
image,
Chip::Esp32c6,
PARAMS,
partition_table,
bootloader,
flash_mode,
flash_size,
flash_freq,
)?)),
ImageFormatKind::DirectBoot => Ok(Box::new(DirectBootFormat::new(image, 0x0)?)),
}
}

fn spi_registers(&self) -> SpiRegisters {
SpiRegisters {
base: 0x6000_3000,
usr_offset: 0x18,
usr1_offset: 0x1c,
usr2_offset: 0x20,
w0_offset: 0x58,
mosi_length_offset: Some(0x24),
miso_length_offset: Some(0x28),
}
}

fn supported_image_formats(&self) -> &[ImageFormatKind] {
&[ImageFormatKind::EspBootloader, ImageFormatKind::DirectBoot]
}

fn supported_build_targets(&self) -> &[&str] {
&[
"riscv32imac-unknown-none-elf",
"riscv32imc-esp-espidf",
"riscv32imc-unknown-none-elf",
]
}
}
8 changes: 8 additions & 0 deletions espflash/src/targets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use self::{
esp32::Esp32,
esp32c2::Esp32c2,
esp32c3::Esp32c3,
esp32c6::Esp32c6,
esp32s2::Esp32s2,
esp32s3::Esp32s3,
esp8266::Esp8266,
Expand All @@ -32,6 +33,7 @@ use crate::{
mod esp32;
mod esp32c2;
mod esp32c3;
mod esp32c6;
mod esp32s2;
mod esp32s3;
mod esp8266;
Expand All @@ -47,6 +49,8 @@ pub enum Chip {
Esp32c2,
/// ESP32-C3, ESP8685
Esp32c3,
/// ESP32-C6
Esp32c6,
/// ESP32-S2
Esp32s2,
/// ESP32-S3
Expand All @@ -65,6 +69,7 @@ impl FromStr for Chip {
"esp32" => Ok(Esp32),
"esp32c2" => Ok(Esp32c2),
"esp32c3" => Ok(Esp32c3),
"esp32c6" => Ok(Esp32c6),
"esp32s2" => Ok(Esp32s2),
"esp32s3" => Ok(Esp32s3),
"esp8266" => Ok(Esp8266),
Expand All @@ -81,6 +86,8 @@ impl Chip {
Ok(Chip::Esp32c2)
} else if Esp32c3::has_magic_value(magic) {
Ok(Chip::Esp32c3)
} else if Esp32c6::has_magic_value(magic) {
Ok(Chip::Esp32c6)
} else if Esp32s2::has_magic_value(magic) {
Ok(Chip::Esp32s2)
} else if Esp32s3::has_magic_value(magic) {
Expand All @@ -97,6 +104,7 @@ impl Chip {
Chip::Esp32 => Box::new(Esp32),
Chip::Esp32c2 => Box::new(Esp32c2),
Chip::Esp32c3 => Box::new(Esp32c3),
Chip::Esp32c6 => Box::new(Esp32c6),
Chip::Esp32s2 => Box::new(Esp32s2),
Chip::Esp32s3 => Box::new(Esp32s3),
Chip::Esp8266 => Box::new(Esp8266),
Expand Down