diff --git a/CHANGELOG.md b/CHANGELOG.md index 937c3ba..7d344bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 5606baa..828cabb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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 = [] diff --git a/src/lib.rs b/src/lib.rs index 55b0ec1..9d70106 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,6 +113,9 @@ pub async fn init_with_custom_layout( } // Tell nrf_modem what memory it can use. + static PARAMS: grounded::uninit::GroundedCell = + 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 { @@ -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| { @@ -152,7 +157,7 @@ pub async fn init_with_custom_layout( } // OK, let's start the library - unsafe { nrfxlib_sys::nrf_modem_init(¶ms) }.into_result()?; + unsafe { nrfxlib_sys::nrf_modem_init(PARAMS.get()) }.into_result()?; // Initialize AT notifications at_notifications::initialize()?;