Skip to content

Commit

Permalink
Fix memory ownership issue in the init function
Browse files Browse the repository at this point in the history
  • Loading branch information
diondokter committed Jun 17, 2024
1 parent f9d4a24 commit 81d2daf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.4.2 (2024-06-17)

- 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, including in the original `nrfxlib` rust crate.
Originally this seems to have not been required, but this changed +-4 years ago
without documentation update from Nordic.

## 0.4.1 (2023-09-22)

- Added a new modem init function where the memory layout can be manually specified
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nrf-modem"
version = "0.4.1"
version = "0.4.2"
edition = "2021"
rust-version = "1.64"
license = "MIT OR Apache-2.0"
Expand All @@ -25,6 +25,7 @@ at-commands = "0.5.2"
no-std-net = "0.6.0"
critical-section = "1.1"
embassy-sync = "0.3.0"
grounded = "0.2.0"

[features]
default = []
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ pub async fn init_with_custom_layout(
}

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

let params = nrfxlib_sys::nrf_modem_init_params {
shmem: nrfxlib_sys::nrf_modem_shmem_cfg {
ctrl: nrfxlib_sys::nrf_modem_shmem_cfg__bindgen_ty_1 {
Expand Down Expand Up @@ -141,6 +144,8 @@ pub async fn init_with_custom_layout(
fault_handler: Some(modem_fault_handler),
};

critical_section::with(|_| unsafe { PARAMS.get().write(params) });

unsafe {
// Use the same TX memory region as above
cortex_m::interrupt::free(|cs| {
Expand All @@ -152,7 +157,7 @@ pub async fn init_with_custom_layout(
}

// OK, let's start the library
unsafe { nrfxlib_sys::nrf_modem_init(&params) }.into_result()?;
unsafe { nrfxlib_sys::nrf_modem_init(PARAMS.get()) }.into_result()?;

// Initialize AT notifications
at_notifications::initialize()?;
Expand Down

0 comments on commit 81d2daf

Please sign in to comment.