eculocate

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit b5e6baed7681ad226410b1d266f9824a3c29da1c
parent 50786ba21392c14a4b65359c902ec2da6bf63504
Author: filipton <filipton12@gmail.com>
Date:   Sat,  7 Sep 2024 09:06:59 +0200

chore: better code comments

Diffstat:
MCargo.toml | 4----
Msrc/lib.rs | 6++++++
Msrc/paddr.rs | 47+++++++++++++++++++++++++++++++++++------------
3 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -15,10 +15,6 @@ log = { version = "0.4.22", optional = true } default = ["log"] log = ["dep:log"] -#esp32 = [] -#esp32c2 = [] esp32c3 = [] -#esp32c6 = [] -#esp32h2 = [] esp32s2 = [] esp32s3 = [] diff --git a/src/lib.rs b/src/lib.rs @@ -1,4 +1,10 @@ #![no_std] +#[cfg(all( + not(feature = "esp32c3"), + not(feature = "esp32s2"), + not(feature = "esp32s3") +))] +compile_error!("You need to select at least one target: \"esp32c3\", \"esp32s2\", \"esp32s3\"."); use embedded_storage::{ReadStorage, Storage}; diff --git a/src/paddr.rs b/src/paddr.rs @@ -1,12 +1,7 @@ -// this is for esp32s3, TODO: implement for other esp32's - struct ExtMemDefs {} + #[cfg(all( - //not(feature = "esp32"), - //not(feature = "esp32c2"), not(feature = "esp32c3"), - //not(feature = "esp32c6"), - //not(feature = "esp32h2"), not(feature = "esp32s2"), not(feature = "esp32s3") ))] @@ -14,15 +9,28 @@ impl ExtMemDefs { const SOC_MMU_VADDR_MASK: u32 = 0; const DR_REG_MMU_TABLE: u32 = 0; const SOC_MMU_VALID_VAL_MASK: u32 = 0; - const MMU_PAGE: u32 = 0; + const SOC_MMU_INVALID: u32 = 0; + const MMU_PAGE_SIZE: u32 = 0; +} + +/* +#[cfg(feature = "esp32c2")] +impl ExtMemDefs { + const SOC_MMU_VADDR_MASK: u32 = ExtMemDefs::MMU_PAGE_SIZE * 64 - 1; + const DR_REG_MMU_TABLE: u32 = 0x600c5000; + const SOC_MMU_VALID_VAL_MASK: u32 = 0x3f; + const SOC_MMU_INVALID: u32 = 1 << 6; + const MMU_PAGE_SIZE: u32 = 0x10000; } +*/ #[cfg(feature = "esp32c3")] impl ExtMemDefs { const SOC_MMU_VADDR_MASK: u32 = 0x7FFFFF; const DR_REG_MMU_TABLE: u32 = 0x600c5000; const SOC_MMU_VALID_VAL_MASK: u32 = 0xff; - const MMU_PAGE: u32 = 0x10000; + const SOC_MMU_INVALID: u32 = 1 << 8; + const MMU_PAGE_SIZE: u32 = 0x10000; } #[cfg(feature = "esp32s2")] @@ -30,7 +38,8 @@ impl ExtMemDefs { const SOC_MMU_VADDR_MASK: u32 = 0x3FFFFF; const DR_REG_MMU_TABLE: u32 = 0x61801000; const SOC_MMU_VALID_VAL_MASK: u32 = 0x3fff; - const MMU_PAGE: u32 = 0x10000; + const SOC_MMU_INVALID: u32 = 1 << 14; + const MMU_PAGE_SIZE: u32 = 0x10000; } #[cfg(feature = "esp32s3")] @@ -38,33 +47,47 @@ impl ExtMemDefs { const SOC_MMU_VADDR_MASK: u32 = 0x1FFFFFF; const DR_REG_MMU_TABLE: u32 = 0x600c5000; const SOC_MMU_VALID_VAL_MASK: u32 = 0x3fff; - const MMU_PAGE: u32 = 0x10000; + const SOC_MMU_INVALID: u32 = 1 << 14; + const MMU_PAGE_SIZE: u32 = 0x10000; } +// BUG: This might fail on esp32s2 (because getting entry_id is different there) +// https://github.com/espressif/esp-idf/blob/master/components/hal/esp32s2/include/hal/mmu_ll.h#L145 +// https://github.com/espressif/esp-idf/blob/master/components/hal/esp32c3/include/hal/mmu_ll.h#L143 fn mmu_ll_get_entry_id(_mmu_id: u32, vaddr: u32) -> u32 { (vaddr & ExtMemDefs::SOC_MMU_VADDR_MASK) >> 16 } +#[allow(dead_code)] +fn mmu_ll_check_entry_valid(_mmu_id: u32, entry_id: u32) -> bool { + let ptr = (ExtMemDefs::DR_REG_MMU_TABLE + entry_id * 4) as *const u32; + unsafe { ((*ptr) & ExtMemDefs::SOC_MMU_INVALID) == 0 } +} + fn mmu_ll_entry_id_to_paddr_base(_mmu_id: u32, entry_id: u32) -> u32 { let ptr = (ExtMemDefs::DR_REG_MMU_TABLE + entry_id * 4) as *const u32; unsafe { ((*ptr) & ExtMemDefs::SOC_MMU_VALID_VAL_MASK) << 16 } } fn mmu_hal_pages_to_bytes(_mmu_id: u32, page_num: u32) -> u32 { - let shift_code = match ExtMemDefs::MMU_PAGE { + let shift_code = match ExtMemDefs::MMU_PAGE_SIZE { 0x10000 => 16, 0x8000 => 15, 0x4000 => 14, - _ => panic!("WRONG MMU_PAGE SIZE! {:X?}", ExtMemDefs::MMU_PAGE), + _ => panic!("WRONG MMU_PAGE SIZE! 0x{:X?}", ExtMemDefs::MMU_PAGE_SIZE), }; page_num << shift_code } pub fn esp_get_current_running_partition(partitions: &[(u32, u32)]) -> Option<usize> { + // we are using 0 as mmu_id - all code paths for esp32c3,esp32s2,esp32s3 arent using mmu_id + let ptr = esp_get_current_running_partition as *const () as *const u32; let entry_id = mmu_ll_get_entry_id(0, ptr as u32); + // page_num is always 1 + // https://github.com/espressif/esp-idf/blob/master/components/hal/mmu_hal.c#L129 let page_size_in_bytes = mmu_hal_pages_to_bytes(0, 1); let offset = (ptr as u32) % page_size_in_bytes;