Skip to content

Commit

Permalink
Make most of PSRAM features into run-time configs
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernQ committed Sep 19, 2024
1 parent d9d7717 commit f1d0a51
Show file tree
Hide file tree
Showing 21 changed files with 516 additions and 380 deletions.
6 changes: 3 additions & 3 deletions esp-alloc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ macro_rules! heap_allocator {
macro_rules! psram_allocator {
($peripheral:expr,$psram_module:path) => {{
use $psram_module as _psram;
_psram::init_psram($peripheral);
let (start, size) = _psram::init_psram($peripheral, _psram::PsramConfig::default());
unsafe {
$crate::HEAP.add_region($crate::HeapRegion::new(
_psram::psram_vaddr_start() as *mut u8,
_psram::PSRAM_BYTES,
start,
size,
$crate::MemoryCapability::External.into(),
));
}
Expand Down
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Renamed `DummyPin` to `NoPin` and removed all internal logic from it. (#2133)
- The `NO_PIN` constant has been removed. (#2133)
- MSRV bump to 1.79 (#2156)
- Removed the PS-RAM related features, replaced by `quad-psram`/`octal-psram`, `init_psram` takes a configuration parameter, it's now possible to auto-detect PS-RAM size (#2178)

### Fixed

Expand Down
23 changes: 5 additions & 18 deletions esp-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,11 @@ defmt = [
]

#! ### PSRAM Feature Flags
## Use externally connected PSRAM (2MB).
psram-2m = []
## Use externally connected PSRAM (4MB).
psram-4m = []
## Use externally connected PSRAM (8MB).
psram-8m = []
## PSRAM 80Mhz frequency support
psram-80mhz = []

#! ### Octal RAM Feature Flags
## Use externally connected Octal RAM (2MB).
opsram-2m = []
## Use externally connected Octal RAM (4MB).
opsram-4m = []
## Use externally connected Octal RAM (8MB).
opsram-8m = []
## Use externally connected Octal RAM (16MB).
opsram-16m = []
## Use externally connected Quad PSRAM
quad-psram = []

## Use externally connected Octal RAM
octal-psram = []

# This feature is intended for testing; you probably don't want to enable it:
ci = ["defmt", "bluetooth"]
Expand Down
46 changes: 46 additions & 0 deletions esp-hal/MIGRATING-0.20.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,49 @@ We've replaced some usage of features with [esp-config](https://docs.rs/esp-conf
# key in .cargo/config.toml [env] section
+ ESP_HAL_PLACE_SPI_DRIVER_IN_RAM=true
```

## PS-RAM

Initializing PS-RAM now takes a chip specific config and returns start of the mapped memory and the size.

Example
```rust
let (start, size) = psram::init_psram(peripherals.PSRAM, psram::PsramConfig::default());
```

If you don't specify the size of PS-RAM via `PsramConfig::size` the size of PS-RAM is derived from the RAM-chip id (or via probing in case of ESP32).

`psram::psram_vaddr_start()` and `psram::PSRAM_BYTES` are removed.

The features `psram-Xm` and `opsram-Xm` are removed and replaced by `quad-psram`/`octal-psram`.
The feature `psram-80mhz` is removed and replaced by `PsramConfig`

Diff of the `psram_quad.rs` example
```diff
-//% FEATURES: psram-2m
+//% FEATURES: esp-hal/psram

...

-fn init_psram_heap() {
+fn init_psram_heap(start: *mut u8, size: usize) {
unsafe {
esp_alloc::HEAP.add_region(esp_alloc::HeapRegion::new(
- psram::psram_vaddr_start() as *mut u8,
- psram::PSRAM_BYTES,
+ start,
+ size,
esp_alloc::MemoryCapability::External.into(),
));
}

...

- psram::init_psram(peripherals.PSRAM);
- init_psram_heap();
+ let (start, size) = psram::init_psram(peripherals.PSRAM, psram::PsramConfig::default());
+ init_psram_heap(start, size);

...

```
14 changes: 6 additions & 8 deletions esp-hal/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ fn main() -> Result<(), Box<dyn Error>> {
let config = Config::for_chip(&chip);

// Check PSRAM features are only given if the target supports PSRAM:
if !config.contains(&String::from("psram"))
&& (cfg!(feature = "psram-2m") || cfg!(feature = "psram-4m") || cfg!(feature = "psram-8m"))
{
if !config.contains(&String::from("psram")) && cfg!(feature = "quad-psram") {
panic!("The target does not support PSRAM");
}

if !config.contains(&String::from("octal_psram")) && cfg!(feature = "octal-psram") {
panic!("The target does not support Octal PSRAM");
}

// Define all necessary configuration symbols for the configured device:
config.define_symbols();

Expand Down Expand Up @@ -226,11 +228,7 @@ fn generate_memory_extras() -> Vec<u8> {

#[cfg(feature = "esp32s2")]
fn generate_memory_extras() -> Vec<u8> {
let reserved_cache = if cfg!(any(
feature = "psram-2m",
feature = "psram-4m",
feature = "psram-8m"
)) {
let reserved_cache = if cfg!(feature = "quad-psram") {
"0x4000"
} else {
"0x2000"
Expand Down
30 changes: 29 additions & 1 deletion esp-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,14 @@
#![allow(asm_sub_register, async_fn_in_trait, stable_features)]
#![cfg_attr(xtensa, feature(asm_experimental_arch))]
#![deny(missing_docs, rust_2018_idioms)]
#![feature(doc_cfg)]
#![no_std]

// MUST be the first module
mod fmt;

use core::cell::UnsafeCell;

#[cfg(riscv)]
pub use esp_riscv_rt::{self, entry, riscv};
pub use procmacros as macros;
Expand All @@ -155,7 +158,9 @@ pub use self::soc::efuse;
#[cfg(lp_core)]
pub use self::soc::lp_core;
pub use self::soc::peripherals;
#[cfg(psram)]
#[cfg_attr(psram, doc(cfg(feature = "quad-psram")))]
#[cfg_attr(octal_psram, doc(cfg(feature = "octal-psram")))]
#[cfg(any(feature = "quad-psram", feature = "octal-psram"))]
pub use self::soc::psram;
#[cfg(ulp_riscv_core)]
pub use self::soc::ulp_core;
Expand Down Expand Up @@ -660,6 +665,29 @@ pub(crate) fn lock<T>(state: &LockState, f: impl FnOnce() -> T) -> T {
}
}

/// Data protected by [LockState]
pub(crate) struct Locked<T> {
lock_state: LockState,
data: UnsafeCell<T>,
}

impl<T> Locked<T> {
/// Create a new instance
pub(crate) const fn new(data: T) -> Self {
Self {
lock_state: LockState::new(),
data: UnsafeCell::new(data),
}
}

/// Provide exclusive access to the protected data to the given closure
pub(crate) fn with<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
crate::lock(&self.lock_state, || f(unsafe { &mut *self.data.get() }))
}
}

unsafe impl<T> Sync for Locked<T> {}

/// Default (unhandled) interrupt handler
pub const DEFAULT_INTERRUPT_HANDLER: interrupt::InterruptHandler = interrupt::InterruptHandler::new(
unsafe { core::mem::transmute::<*const (), extern "C" fn()>(EspDefaultHandler as *const ()) },
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/soc/esp32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod cpu_control;
pub mod efuse;
pub mod gpio;
pub mod peripherals;
#[cfg(psram)]
#[cfg(feature = "quad-psram")]
pub mod psram;
pub mod radio_clocks;
pub mod trng;
Expand Down
Loading

0 comments on commit f1d0a51

Please sign in to comment.