Skip to content

Add nrf softdevice feature #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,19 @@ default-features = false
version = "0.8.11"
features = ["const_mut_refs"]

[dependencies.critical-section]
version = "0.2.7"
optional = true

[dependencies.embassy]
version = "0.1.0"
optional = true

[features]
nrf-softdevice = ["dep:critical-section", "dep:embassy"]

[dev-dependencies]
cortex-m-rt = "0.6.12"

# [patch.crates-io]
# embassy = { git = "https://github.com/embassy-rs/embassy" }
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ This project is developed and maintained by the [Cortex-M team][team].

## [Documentation](https://docs.rs/alloc-cortex-m)

## Features

The `nrf-softdevice` feature replaces `cortex_m::interrupt::free()` with
`critical_section::with()` when block interrupts.

## [Change log](CHANGELOG.md)

## License
Expand Down
45 changes: 39 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,34 @@ use core::alloc::{GlobalAlloc, Layout};
use core::cell::RefCell;
use core::ptr::{self, NonNull};

use cortex_m::interrupt::Mutex;
#[cfg(not(feature = "nrf-softdevice"))]
use cortex_m::interrupt::{
Mutex,
CriticalSection,
};
#[cfg(feature = "nrf-softdevice")]
use {
critical_section::CriticalSection,
embassy::blocking_mutex::CriticalSectionMutex as Mutex,
};
use linked_list_allocator::Heap;

#[cfg(not(feature = "nrf-softdevice"))]
#[inline]
fn block_interrupts<F, R>(f: F) -> R
where F: FnOnce(&CriticalSection) -> R
{
cortex_m::interrupt::free(f)
}

#[cfg(feature = "nrf-softdevice")]
#[inline]
fn block_interrupts<F, R>(f: F) -> R
where F: FnOnce(CriticalSection) -> R
{
critical_section::with(f)
}

pub struct CortexMHeap {
heap: Mutex<RefCell<Heap>>,
}
Expand All @@ -24,12 +49,20 @@ impl CortexMHeap {
///
/// You must initialize this heap using the
/// [`init`](struct.CortexMHeap.html#method.init) method before using the allocator.
#[cfg(not(feature = "nrf-softdevice"))]
pub const fn empty() -> CortexMHeap {
CortexMHeap {
heap: Mutex::new(RefCell::new(Heap::empty())),
}
}

#[cfg(feature = "nrf-softdevice")]
pub fn empty() -> CortexMHeap {
CortexMHeap {
heap: Mutex::new(RefCell::new(Heap::empty())),
}
}

/// Initializes the heap
///
/// This function must be called BEFORE you run any code that makes use of the
Expand All @@ -54,25 +87,25 @@ impl CortexMHeap {
/// - This function must be called exactly ONCE.
/// - `size > 0`
pub unsafe fn init(&self, start_addr: usize, size: usize) {
cortex_m::interrupt::free(|cs| {
block_interrupts(|cs| {
self.heap.borrow(cs).borrow_mut().init(start_addr, size);
});
}

/// Returns an estimate of the amount of bytes in use.
pub fn used(&self) -> usize {
cortex_m::interrupt::free(|cs| self.heap.borrow(cs).borrow_mut().used())
block_interrupts(|cs| self.heap.borrow(cs).borrow_mut().used())
}

/// Returns an estimate of the amount of bytes available.
pub fn free(&self) -> usize {
cortex_m::interrupt::free(|cs| self.heap.borrow(cs).borrow_mut().free())
block_interrupts(|cs| self.heap.borrow(cs).borrow_mut().free())
}
}

unsafe impl GlobalAlloc for CortexMHeap {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
cortex_m::interrupt::free(|cs| {
block_interrupts(|cs| {
self.heap
.borrow(cs)
.borrow_mut()
Expand All @@ -83,7 +116,7 @@ unsafe impl GlobalAlloc for CortexMHeap {
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
cortex_m::interrupt::free(|cs| {
block_interrupts(|cs| {
self.heap
.borrow(cs)
.borrow_mut()
Expand Down