|
| 1 | +use std::ops::Range; |
| 2 | + |
| 3 | +use esp_idf_part::PartitionTable; |
| 4 | + |
| 5 | +use super::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target}; |
| 6 | +use crate::{ |
| 7 | + connection::Connection, |
| 8 | + elf::FirmwareImage, |
| 9 | + error::Error, |
| 10 | + flasher::{FlashFrequency, FlashMode, FlashSize}, |
| 11 | + image_format::{DirectBootFormat, IdfBootloaderFormat, ImageFormat, ImageFormatKind}, |
| 12 | +}; |
| 13 | + |
| 14 | +const CHIP_DETECT_MAGIC_VALUES: &[u32] = &[0x2CE0_806F]; |
| 15 | + |
| 16 | +const FLASH_RANGES: &[Range<u32>] = &[ |
| 17 | + 0x4200_0000..0x4280_0000, // IROM |
| 18 | + 0x4280_0000..0x4300_0000, // DROM |
| 19 | +]; |
| 20 | + |
| 21 | +const PARAMS: Esp32Params = Esp32Params::new( |
| 22 | + 0x0, |
| 23 | + 0x1_0000, |
| 24 | + 0x3f_0000, |
| 25 | + 13, |
| 26 | + include_bytes!("../../resources/bootloaders/esp32c6-bootloader.bin"), |
| 27 | +); |
| 28 | + |
| 29 | +/// ESP32-C6 Target |
| 30 | +pub struct Esp32c6; |
| 31 | + |
| 32 | +impl Esp32c6 { |
| 33 | + pub fn has_magic_value(value: u32) -> bool { |
| 34 | + CHIP_DETECT_MAGIC_VALUES.contains(&value) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl ReadEFuse for Esp32c6 { |
| 39 | + fn efuse_reg(&self) -> u32 { |
| 40 | + 0x600B_0800 |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl Target for Esp32c6 { |
| 45 | + fn addr_is_flash(&self, addr: u32) -> bool { |
| 46 | + FLASH_RANGES.iter().any(|range| range.contains(&addr)) |
| 47 | + } |
| 48 | + |
| 49 | + fn chip_features(&self, _connection: &mut Connection) -> Result<Vec<&str>, Error> { |
| 50 | + Ok(vec!["WiFi 6", "BT 5"]) |
| 51 | + } |
| 52 | + |
| 53 | + fn major_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> { |
| 54 | + Ok((self.read_efuse(connection, 22)? >> 24) & 0x3) |
| 55 | + } |
| 56 | + |
| 57 | + fn minor_chip_version(&self, connection: &mut Connection) -> Result<u32, Error> { |
| 58 | + let hi = (self.read_efuse(connection, 22)? >> 23) & 0x1; |
| 59 | + let lo = (self.read_efuse(connection, 20)? >> 18) & 0x7; |
| 60 | + |
| 61 | + Ok((hi << 3) + lo) |
| 62 | + } |
| 63 | + |
| 64 | + fn crystal_freq(&self, _connection: &mut Connection) -> Result<u32, Error> { |
| 65 | + // The ESP32-C6's XTAL has a fixed frequency of 40MHz. |
| 66 | + Ok(40) |
| 67 | + } |
| 68 | + |
| 69 | + fn get_flash_image<'a>( |
| 70 | + &self, |
| 71 | + image: &'a dyn FirmwareImage<'a>, |
| 72 | + bootloader: Option<Vec<u8>>, |
| 73 | + partition_table: Option<PartitionTable>, |
| 74 | + image_format: Option<ImageFormatKind>, |
| 75 | + _chip_revision: Option<(u32, u32)>, |
| 76 | + flash_mode: Option<FlashMode>, |
| 77 | + flash_size: Option<FlashSize>, |
| 78 | + flash_freq: Option<FlashFrequency>, |
| 79 | + ) -> Result<Box<dyn ImageFormat<'a> + 'a>, Error> { |
| 80 | + let image_format = image_format.unwrap_or(ImageFormatKind::EspBootloader); |
| 81 | + |
| 82 | + match image_format { |
| 83 | + ImageFormatKind::EspBootloader => Ok(Box::new(IdfBootloaderFormat::new( |
| 84 | + image, |
| 85 | + Chip::Esp32c6, |
| 86 | + PARAMS, |
| 87 | + partition_table, |
| 88 | + bootloader, |
| 89 | + flash_mode, |
| 90 | + flash_size, |
| 91 | + flash_freq, |
| 92 | + )?)), |
| 93 | + ImageFormatKind::DirectBoot => Ok(Box::new(DirectBootFormat::new(image, 0x0)?)), |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + fn spi_registers(&self) -> SpiRegisters { |
| 98 | + SpiRegisters { |
| 99 | + base: 0x6000_3000, |
| 100 | + usr_offset: 0x18, |
| 101 | + usr1_offset: 0x1c, |
| 102 | + usr2_offset: 0x20, |
| 103 | + w0_offset: 0x58, |
| 104 | + mosi_length_offset: Some(0x24), |
| 105 | + miso_length_offset: Some(0x28), |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + fn supported_image_formats(&self) -> &[ImageFormatKind] { |
| 110 | + &[ImageFormatKind::EspBootloader, ImageFormatKind::DirectBoot] |
| 111 | + } |
| 112 | + |
| 113 | + fn supported_build_targets(&self) -> &[&str] { |
| 114 | + &[ |
| 115 | + "riscv32imac-unknown-none-elf", |
| 116 | + "riscv32imc-esp-espidf", |
| 117 | + "riscv32imc-unknown-none-elf", |
| 118 | + ] |
| 119 | + } |
| 120 | +} |
0 commit comments