Skip to content

Commit

Permalink
Merge pull request #29 from diondokter/fix-memory-ownership
Browse files Browse the repository at this point in the history
Fix memory ownership issue in the init function
  • Loading branch information
diondokter authored Jun 17, 2024
2 parents b639e81 + 3365104 commit 3481077
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nrfxlib"
version = "0.6.0"
version = "0.6.1"
authors = ["Jonathan Pallant (42 Technology) <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand All @@ -18,3 +18,4 @@ linked_list_allocator = { version = "0.10", default-features = false, features =
] }
log = "0.4"
nrfxlib-sys = "=1.5.1"
grounded = "0.2.0"
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ See [nrf9160-demo](https://github.com/42-technology-ltd/nrf9160-demo) for a demo

## Changelog

### Unreleased Changes ([Source](https://github.com/42-technology-ltd/nrfxlib/tree/develop) | [Changes](https://github.com/42-technology-ltd/nrfxlib/compare/v0.6.0...develop))
### Unreleased Changes ([Source](https://github.com/42-technology-ltd/nrfxlib/tree/develop) | [Changes](https://github.com/42-technology-ltd/nrfxlib/compare/v0.6.1...develop))

* None

### v0.6.1 ([Source](https://github.com/42-technology-ltd/nrfxlib/tree/v0.6.1) | [Changes](https://github.com/42-technology-ltd/nrfxlib/compare/v0.6.0...v0.6.1))

* Fixed a memory ownership issue in `nrf_modem_init`. The `nrf_modem_init_params` pointer given to the init must
have a static lifetime. This has been a stack variable since forever.
Originally this seems to have not been required, but this changed +-4 years ago
without documentation update from Nordic.

### v0.6.0 ([Source](https://github.com/42-technology-ltd/nrfxlib/tree/v0.6.0) | [Changes](https://github.com/42-technology-ltd/nrfxlib/compare/v0.5.0...v0.6.0))

* Updated to nrfxlib-sys v1.5.1.
Expand Down
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,14 @@ pub fn init() -> Result<(), Error> {
let heap_start = HEAP_MEMORY.as_mut_ptr() as *mut _;
let heap_size = HEAP_MEMORY.len() * core::mem::size_of::<u32>();
cortex_m::interrupt::free(|cs| {
*LIBRARY_ALLOCATOR.borrow(cs).borrow_mut() =
Some(Heap::new(heap_start, heap_size))
*LIBRARY_ALLOCATOR.borrow(cs).borrow_mut() = Some(Heap::new(heap_start, heap_size))
});
}

// Tell nrf_modem what memory it can use.
static PARAMS: grounded::uninit::GroundedCell<nrfxlib_sys::nrf_modem_init_params_t> =
grounded::uninit::GroundedCell::uninit();

let params = sys::nrf_modem_init_params_t {
shmem: sys::nrf_modem_shmem_cfg {
ctrl: sys::nrf_modem_shmem_cfg__bindgen_ty_1 {
Expand All @@ -195,6 +197,8 @@ pub fn init() -> Result<(), Error> {
ipc_irq_prio: 0,
};

cortex_m::interrupt::free(|_| unsafe { PARAMS.get().write(params) });

unsafe {
// Use the same TX memory region as above
cortex_m::interrupt::free(|cs| {
Expand All @@ -206,7 +210,7 @@ pub fn init() -> Result<(), Error> {
}

// OK, let's start the library
let result = unsafe { sys::nrf_modem_init(&params, sys::nrf_modem_mode_t_NORMAL_MODE) };
let result = unsafe { sys::nrf_modem_init(PARAMS.get(), sys::nrf_modem_mode_t_NORMAL_MODE) };

// Was it happy?
if result < 0 {
Expand Down

0 comments on commit 3481077

Please sign in to comment.