If you are having issues with the exercises, please create a ticket on DevZone:
Click or drag files to this area to upload. You can upload up to 2 files.

nRF Modem library

This section will touch upon the interfacing between the modem firmware running on the modem core and an application running on the application core.

Recalling the nRF91 Series communication stack, we are now discussing these layers

For the application to interface with the modem core on the nRF91 Series SiP, the nRF Connect SDK contains the Modem library. This is a set of standard function calls that can be used in your application to communicate with the modem.

The library implements the communication through the Remote procedure call (RPC) library, using the Inter Processor Communication (IPC) peripheral and a shared region of RAM.

The modem library contains, among other things, the socket API (nrf_socket.h), the AT API (nrf_modem_at.h) and the GNSS API (nrf_modem_gnss.h), which interfaces with their respective modules in the modem firmware.

Integration into nRF Connect SDK

On top of this, we have the Modem library integration layer which integrates the Modem library into nRF Connect SDK. This layer consists of the library wrapper, and functionalities like socket offloading, memory partitioning of the shared memory region, handling modem tracing (covered in Lesson 7), and diagnostics.

Socket offloading

Zephyr has the functionality to offload or redirect function calls at the socket API level. The integration layer uses this functionality to redirect all socket API calls to the Modem library’s native socket API, thereby offloading all socket calls to the IP stack in the nRF91 Series modem firmware.

The Modem library socket API sets the errno‘s as defined in nrf_errno.h, and the integration layer converts these errnos to adhere to the selected C library implementation (either minimal or Newlib).

When socket offloading is disabled, the socket calls will no longer be offloaded to the nRF91 Series modem firmware, but rather go to the Zephyr’s native IP stack.


Make sure you are consistent in only using either Zephyr native BSD socket API or the Modem library socket API directly in the application. We recommend using Zephyr’s native BSD socket API directly in the application, and then offloading to the Modem library through the offloading functionality.

Enabling and configuring the library

To enable the modem library in your application, first enable the configuration for the module:

Include the header file of the nRF Modem library in your source code.

#include <modem/nrf_modem_lib.h>

Additionally, want to enable the link layer and IP networking support (CONFIG_NETWORKING), the BSD Sockets compatible API (CONFIG_NET_SOCKETS) and the socket offloading functionality (CONFIG_NET_SOCKETS_OFFLOAD).

To expose all the socket calls without the zsock_ prefix, we enable the following config

We also need to disable the Zephyr native IP stack (CONFIG_NET_NATIVE), to be able to offload all socket calls to the IP stack running on the modem firmware.

Then we need to set some memory configs. The size of the heap memory pool (CONFIG_HEAP_MEM_POOL_SIZE) has a default value of zero, so it needs to be set to a non-zero value in order to be created.

The size of the stack used for initialization and the main thread (CONFIG_MAIN_STACK_SIZE) also needs to be increased

Initializing the library

v2.4.0 – v2.x.x

Now we need to initialize the nRF Modem library and turn on the modem by calling nrf_modem_lib_init(), which has the following signature

This is done by adding the following code to your application code

v2.2.0 – v2.3.0

The initialization of the nRF Modem library is handled automatically through the Kconfig CONFIG_NRF_MODEM_LIB_SYS_INIT which automatically initializes the library during the SYS_INIT sequence.

The Kconfig is enabled by default, so there is no need to add it to your application.

Register an account
Already have an account? Log in
(All fields are required unless specified optional)

  • 8 or more characters
  • Upper and lower case letters
  • At least one number or special character

Forgot your password?
Enter the email associated with your account, and we will send you a link to reset your password.