Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Macros for more convenient usage #16

Merged
merged 2 commits into from
Nov 27, 2023
Merged

Conversation

bjoernQ
Copy link
Contributor

@bjoernQ bjoernQ commented Nov 17, 2023

This adds (totally optional) macros to setup the allocator for using PS-RAM or using a statically allocated memory region

Example

Without macro

#![no_std]
#![no_main]

extern crate alloc;
use core::mem::MaybeUninit;
use esp_backtrace as _;
use esp_println::println;
use hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, Delay};

#[global_allocator]
static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty();

fn init_heap() {
    const HEAP_SIZE: usize = 32 * 1024;
    static mut HEAP: MaybeUninit<[u8; HEAP_SIZE]> = MaybeUninit::uninit();

    unsafe {
        ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, HEAP_SIZE);
    }
}
#[entry]
fn main() -> ! {
    init_heap();


              ...

With macro

#![no_std]
#![no_main]

extern crate alloc;
use esp_backtrace as _;
use esp_println::println;
use hal::prelude::*;

#[entry]
fn main() -> ! {
    esp_alloc::heap_allocator!(32*1024);

              ...

The macros don't do magical things - they just hide the annoying boilerplate code

Macros could be feature gated but I don't really see harm in not doing that

@bjoernQ
Copy link
Contributor Author

bjoernQ commented Nov 17, 2023

for using PS-RAM

do you have an example for this, please? 🥺 or is there absolutely no other setup necessary?

looks like this

#![no_std]
#![no_main]

extern crate alloc;
use esp_backtrace as _;
use esp_println::println;
use hal::{peripherals::Peripherals, prelude::*};

#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    esp_alloc::psram_allocator!(peripherals.PSRAM, hal::psram);

           .............

@bugadani
Copy link

Just deleted my comment because silly me should have checked the PS-RAM example. Sorry for that, and this is really cool!

Copy link
Member

@jessebraham jessebraham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MabezDev MabezDev merged commit 0de0725 into esp-rs:main Nov 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants