Skip to content

Commit

Permalink
lib: nrf_modem_lib: nRF92 support
Browse files Browse the repository at this point in the history
Add support for running nrf_modem library on nRF92 application CPU.

Signed-off-by: Andreas Moltumyr <[email protected]>
  • Loading branch information
anhmolt committed Aug 26, 2024
1 parent 25340ef commit 64aca26
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 20 deletions.
1 change: 1 addition & 0 deletions lib/nrf_modem_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
zephyr_library()
zephyr_library_sources(nrf_modem_lib.c)
zephyr_library_sources(nrf_modem_os.c)
zephyr_library_sources_ifdef(CONFIG_SOC_SERIES_NRF92X nrf_modem_os_rpc.c)
zephyr_library_sources_ifdef(CONFIG_NRF_MODEM_LIB_CFUN_HOOKS cfun_hooks.c)
zephyr_library_sources_ifdef(CONFIG_NRF_MODEM_LIB_MEM_DIAG diag.c)
zephyr_library_sources_ifdef(CONFIG_NET_SOCKETS nrf9x_sockets.c)
Expand Down
3 changes: 1 addition & 2 deletions lib/nrf_modem_lib/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

menuconfig NRF_MODEM_LIB
bool "Modem library"
depends on SOC_SERIES_NRF91X
depends on TRUSTED_EXECUTION_NONSECURE
depends on (SOC_SERIES_NRF91X && TRUSTED_EXECUTION_NONSECURE) || SOC_NRF9280_CPUAPP
select NRF_MODEM
imply NET_SOCKETS_OFFLOAD
imply NET_SOCKETS_POSIX_NAMES if !POSIX_API
Expand Down
29 changes: 27 additions & 2 deletions lib/nrf_modem_lib/Kconfig.modemlib
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ config NRF_MODEM_LIB_HEAP_SIZE
Size of the library heap.
This heap is allocated from the application's RAM region.

DT_NODE_PATH_SHMEM_CTRL := $(dt_nodelabel_path,cpuapp_cpucell_ipc_shm_ctrl)

config NRF_MODEM_LIB_SHMEM_CTRL_SIZE
hex
default NRF_MODEM_SHMEM_CTRL_SIZE
Expand All @@ -28,26 +30,46 @@ config NRF_MODEM_LIB_SHMEM_CTRL_SIZE
This is a constant for a given library build, and is exported
by the library via NRF_MODEM_SHMEM_CTRL_SIZE.

DT_NODE_PATH_SHMEM_TX := $(dt_nodelabel_path,cpuapp_cpucell_ipc_shm_heap)

config NRF_MODEM_LIB_SHMEM_TX_SIZE
int "TX region size"
int "TX region size" if !SOC_SERIES_NRF92X
range 1024 32768
default $(dt_node_reg_size_int,$(DT_NODE_PATH_SHMEM_TX)) if SOC_SERIES_NRF92X
# Set default to 8k plus Zephyr heap overhead (128 bytes)
default 8320
help
Size of the shared memory region owned by the application. This area holds all outgoing
data from the application to the modem, e.g. buffers passed to `send()`, AT commands.
The size must be a multiple of four to keep the memory partitions word-aligned.

DT_NODE_PATH_SHMEM_RX := $(dt_nodelabel_path,cpucell_cpuapp_ipc_shm_heap)

config NRF_MODEM_LIB_SHMEM_RX_SIZE
int "RX region size"
int "RX region size" if !SOC_SERIES_NRF92X
range 2488 32768 if SOC_NRF9160
range 2616 32768 if SOC_NRF9120
default $(dt_node_reg_size_int,$(DT_NODE_PATH_SHMEM_RX)) if SOC_SERIES_NRF92X
default 8192
help
Size of the shared memory region owned by the modem.
This area holds all incoming data from the modem, plus the modem's own control structures.
The size must be a multiple of four to keep the memory partitions word-aligned.

if SOC_SERIES_NRF92X

config NRF_MODEM_LIB_TRANSPORT_MBOX
bool
default y
select MBOX
select IPC_SERVICE
select IPC_SERVICE_ICMSG
select IPC_SERVICE_ICMSG_SHMEM_ACCESS_SYNC

endif # SOC_SERIES_NRF92X

if SOC_SERIES_NRF91X || UNITY

config NRF_MODEM_LIB_SHMEM_TRACE_SIZE_OVERRIDE
bool "Custom trace region size"
depends on NRF_MODEM_LIB_TRACE
Expand All @@ -61,6 +83,8 @@ config NRF_MODEM_LIB_SHMEM_TRACE_SIZE
help
Size of the shared memory region used to receive modem traces.

endif # SOC_SERIES_NRF91X || UNITY

config NRF_MODEM_LIB_SENDMSG_BUF_SIZE
int "Size of the sendmsg intermediate buffer"
default 128
Expand Down Expand Up @@ -102,6 +126,7 @@ endmenu # Memory config

menuconfig NRF_MODEM_LIB_TRACE
bool "Tracing"
depends on SOC_SERIES_NRF91X || UNITY
help
When enabled, a portion of RAM (called Trace region) will be shared with the modem to receive modem's trace data.
The size of the Trace region is defined by the NRF_MODEM_LIB_SHMEM_TRACE_SIZE option.
Expand Down
51 changes: 40 additions & 11 deletions lib/nrf_modem_lib/nrf_modem_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,42 @@
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <nrfx_ipc.h>
#include <modem/nrf_modem_lib.h>
#include <nrf_modem.h>
#include <nrf_modem_at.h>
#include <zephyr/init.h>
#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/toolchain.h>
#include <modem/nrf_modem_lib.h>

#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(nrf_modem, CONFIG_NRF_MODEM_LIB_LOG_LEVEL);

#define AT_CFUN_READ "AT+CFUN?"
#define AT_CFUN0_VAL 0
#define AT_CFUN4_VAL 4

static void nrf_modem_lib_dfu_handler(uint32_t dfu_res);

#ifdef CONFIG_SOC_SERIES_NRF91X
#include <modem/nrf_modem_lib_trace.h>
#include <nrfx_ipc.h>
#include <pm_config.h>

#ifndef CONFIG_TRUSTED_EXECUTION_NONSECURE
#error nrf_modem_lib must be run as non-secure firmware.\
Are you building for the correct board ?
#endif /* CONFIG_TRUSTED_EXECUTION_NONSECURE */

LOG_MODULE_DECLARE(nrf_modem, CONFIG_NRF_MODEM_LIB_LOG_LEVEL);

/* Interrupt used for communication with the network layer. */
#define NRF_MODEM_IPC_IRQ DT_IRQ_BY_IDX(DT_NODELABEL(ipc), 0, irq)
BUILD_ASSERT(IPC_IRQn == NRF_MODEM_IPC_IRQ, "NRF_MODEM_IPC_IRQ mismatch");

#define AT_CFUN_READ "AT+CFUN?"
#define AT_CFUN0_VAL 0
#define AT_CFUN4_VAL 4

/* The heap implementation in `nrf_modem_os.c` require some overhead
* to allow allocating up to `NRF_MODEM_LIB_SHMEM_TX_SIZE` bytes.
*/
#define NRF_MODEM_LIB_SHMEM_TX_HEAP_OVERHEAD_SIZE 128

static void nrf_modem_lib_dfu_handler(uint32_t dfu_res);

static const struct nrf_modem_init_params init_params = {
.ipc_irq_prio = CONFIG_NRF_MODEM_LIB_IPC_IRQ_PRIO,
.shmem.ctrl = {
Expand Down Expand Up @@ -69,6 +71,27 @@ static const struct nrf_modem_bootloader_init_params bootloader_init_params = {
.shmem.size = PM_NRF_MODEM_LIB_SRAM_SIZE,
.fault_handler = nrf_modem_fault_handler
};
#endif /* CONFIG_SOC_SERIES_NRF91X */

#ifdef CONFIG_SOC_SERIES_NRF92X

static const struct nrf_modem_init_params init_params = {
.shmem.ctrl = {
.base = DT_REG_ADDR(DT_NODELABEL(cpuapp_cpucell_ipc_shm)),
.size = CONFIG_NRF_MODEM_LIB_SHMEM_CTRL_SIZE,
},
.shmem.tx = {
.base = DT_REG_ADDR(DT_NODELABEL(cpuapp_cpucell_ipc_shm_heap)),
.size = CONFIG_NRF_MODEM_LIB_SHMEM_TX_SIZE,
},
.shmem.rx = {
.base = DT_REG_ADDR(DT_NODELABEL(cpucell_cpuapp_ipc_shm_heap)),
.size = CONFIG_NRF_MODEM_LIB_SHMEM_RX_SIZE,
},
.fault_handler = nrf_modem_fault_handler,
.dfu_handler = nrf_modem_lib_dfu_handler,
};
#endif /* CONFIG_SOC_SERIES_NRF92X */

#if CONFIG_NRF_MODEM_LIB_TRACE
extern void nrf_modem_lib_trace_init(void);
Expand Down Expand Up @@ -129,11 +152,13 @@ int nrf_modem_lib_init(void)
{
int err;

#ifdef CONFIG_SOC_SERIES_NRF91X
/* Setup the network IRQ used by the Modem library.
* Note: No call to irq_enable() here, that is done through nrf_modem_init().
*/
IRQ_CONNECT(NRF_MODEM_IPC_IRQ, CONFIG_NRF_MODEM_LIB_IPC_IRQ_PRIO,
nrfx_isr, nrfx_ipc_irq_handler, 0);
#endif /* CONFIG_SOC_SERIES_NRF91X */

err = nrf_modem_init(&init_params);
if (err) {
Expand Down Expand Up @@ -161,7 +186,11 @@ int nrf_modem_lib_init(void)

int nrf_modem_lib_bootloader_init(void)
{
#ifdef CONFIG_SOC_SERIES_NRF91X
return nrf_modem_bootloader_init(&bootloader_init_params);
#else
return -ENOSYS;
#endif
}

int nrf_modem_lib_shutdown(void)
Expand Down
23 changes: 18 additions & 5 deletions lib/nrf_modem_lib/nrf_modem_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@
#include <nrf.h>
#include <nrf_errno.h>
#include <errno.h>
#include <pm_config.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(nrf_modem, CONFIG_NRF_MODEM_LIB_LOG_LEVEL);

#if CONFIG_SOC_SERIES_NRF91X
#include <pm_config.h>
#define SHMEM_TX_HEAP_ADDR (PM_NRF_MODEM_LIB_TX_ADDRESS)
#elif CONFIG_SOC_SERIES_NRF92X
#define SHMEM_TX_HEAP_ADDR (DT_REG_ADDR(DT_NODELABEL(cpuapp_cpucell_ipc_shm_heap)))
#endif
#define SHMEM_TX_HEAP_SIZE (CONFIG_NRF_MODEM_LIB_SHMEM_TX_SIZE)

#define UNUSED_FLAGS 0
#define THREAD_MONITOR_ENTRIES 10

LOG_MODULE_REGISTER(nrf_modem, CONFIG_NRF_MODEM_LIB_LOG_LEVEL);

struct sleeping_thread {
sys_snode_t node;
struct k_sem sem;
Expand Down Expand Up @@ -375,7 +382,14 @@ void nrf_modem_os_free(void *mem)
void *nrf_modem_os_shm_tx_alloc(size_t bytes)
{
extern uint32_t nrf_modem_lib_shmem_failed_allocs;

#if (CONFIG_SOC_SERIES_NRF92X && CONFIG_DCACHE)
/* Allocate cache line aligned memory. */
void * const addr = k_heap_aligned_alloc(&nrf_modem_lib_shmem_heap, CONFIG_DCACHE_LINE_SIZE,
ROUND_UP(bytes, CONFIG_DCACHE_LINE_SIZE), K_NO_WAIT);
#else
void * const addr = k_heap_alloc(&nrf_modem_lib_shmem_heap, bytes, K_NO_WAIT);
#endif

if (IS_ENABLED(CONFIG_NRF_MODEM_LIB_MEM_DIAG_ALLOC) && !addr) {
nrf_modem_lib_shmem_failed_allocs++;
Expand Down Expand Up @@ -487,8 +501,7 @@ void nrf_modem_os_init(void)
{
/* Initialize heaps */
k_heap_init(&nrf_modem_lib_heap, library_heap_buf, sizeof(library_heap_buf));
k_heap_init(&nrf_modem_lib_shmem_heap, (void *)PM_NRF_MODEM_LIB_TX_ADDRESS,
CONFIG_NRF_MODEM_LIB_SHMEM_TX_SIZE);
k_heap_init(&nrf_modem_lib_shmem_heap, (void *)SHMEM_TX_HEAP_ADDR, SHMEM_TX_HEAP_SIZE);
}

void nrf_modem_os_shutdown(void)
Expand Down
Loading

0 comments on commit 64aca26

Please sign in to comment.