Skip to main content

litz_esp/
lib.rs

1//! ESP32-family chips.
2//!
3//! Only the RISC-V parts are in scope first: ESP32-C3 / C6 / H2 share the
4//! RV32 core and the same peripheral vocabulary, and they run unchanged in
5//! WebAssembly. Xtensa (ESP32, S2, S3) is a separate ISA core and lands later.
6//!
7//! The mask ROM is not vendored here. Espressif publishes the ROM ELFs under
8//! Apache-2.0 at `espressif/esp-rom-elfs`; a chip loads one at runtime from a
9//! path or from an embedded blob the caller supplies, so the crate stays free
10//! of binaries and the licence question stays where Espressif put it.
11#![no_std]
12#![forbid(unsafe_code)]
13
14extern crate alloc;
15
16use litz_mcu::MemoryRegion;
17
18/// Which chip a [`Config`] describes.
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub enum Chip {
21    /// RV32IMC, 160 MHz, PLIC, no atomics.
22    Esp32C3,
23    /// RV32IMAC, 160 MHz, PLIC.
24    Esp32C6,
25    /// RV32IMAC, 96 MHz, PLIC, no radio.
26    Esp32H2,
27}
28
29/// External flash layout, in the order the ROM expects to find images.
30#[derive(Clone, Copy, Debug)]
31pub struct FlashLayout {
32    pub size: u32,
33    /// Offset of the second-stage bootloader.
34    pub bootloader_offset: u32,
35    /// Offset of the partition table.
36    pub partition_table_offset: u32,
37    /// Offset of the application image.
38    pub app_offset: u32,
39}
40
41impl Chip {
42    pub const fn memory_map(self) -> &'static [MemoryRegion] {
43        // Addresses are from the ESP32-C3/C6 technical reference manuals.
44        match self {
45            Chip::Esp32C3 | Chip::Esp32C6 | Chip::Esp32H2 => &PLIC_AND_RV_MAP,
46        }
47    }
48
49    pub const fn flash_layout(self) -> FlashLayout {
50        FlashLayout {
51            size: 4 * 1024 * 1024,
52            bootloader_offset: 0x0,
53            partition_table_offset: 0x8000,
54            app_offset: 0x1_0000,
55        }
56    }
57}
58
59const PLIC_AND_RV_MAP: [MemoryRegion; 6] = [
60    MemoryRegion::new("irom", 0x4200_0000, 0x0040_0000),
61    MemoryRegion::new("drom", 0x3C00_0000, 0x0040_0000),
62    MemoryRegion::new("dram", 0x3FC8_0000, 0x0004_0000),
63    MemoryRegion::new("irom-dram", 0x4037_C000, 0x0000_8000),
64    MemoryRegion::new("peripherals", 0x6000_0000, 0x0010_0000),
65    MemoryRegion::new("rtc", 0x5000_0000, 0x0001_0000),
66];