Cellular: Modem callbacks

The Modem callbacks sample demonstrates how to set up callbacks for Modem library initialization and shutdown calls, using the Modem library integration layer and how to set up a callback for changes to the modem functional mode using the LTE link control library.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Board target

nRF9161 DK

PCA10153

nrf9161dk

nrf9161dk/nrf9161/ns

nRF9160 DK

PCA10090

nrf9160dk

nrf9160dk/nrf9160/ns

nRF9151 DK

PCA10171

nrf9151dk

nrf9151dk/nrf9151/ns

For more security, it is recommended to use the */ns variant of the board target. When built for this variant, the sample is configured to compile and run as a non-secure application using security by separation. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.

Overview

The sample performs the following operations:

  1. Registers callbacks during compile time for modem initialization, functional mode changes, and shutdown using the NRF_MODEM_LIB_ON_INIT, NRF_MODEM_LIB_ON_CFUN and NRF_MODEM_LIB_ON_SHUTDOWN macros respectively.

  2. Initializes the Modem library.

  3. Changes functional mode using the lte_lc_func_mode_set() function in the LTE link control library

  4. Shuts down the Modem library.

This triggers the callbacks for on_modem_init(), on_cfun() and on_modem_shutdown() functions.

Sending traces over UART on an nRF91 Series DK

To send modem traces over UART on an nRF91 Series DK, configuration must be added for the UART device in the devicetree and Kconfig. This is done by adding the modem trace UART snippet when building and programming.

Use the Cellular Monitor app for capturing and analyzing modem traces.

TF-M logging must use the same UART as the application. For more details, see shared TF-M logging.

Building and running

This sample can be found under samples/cellular/modem_callbacks in the nRF Connect SDK folder structure.

For more security, it is recommended to use the */ns variant of the board target (see the Requirements section above.) When built for this variant, the sample is configured to compile and run as a non-secure application using security by separation. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.

To build the sample, follow the instructions in Building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.

Note

When building repository applications in the SDK repositories, building with sysbuild is enabled by default. If you work with out-of-tree freestanding applications, you need to manually pass the --sysbuild parameter to every build command or configure west to always use it.

Testing

After programming the sample to your development kit, complete the following steps to test it:

  1. Connect the kit to the computer using a USB cable. The kit is assigned a serial port. Serial ports are referred to as COM ports on Windows, /dev/ttyACM devices on Linux, and /dev/tty devices on macOS. To list Nordic Semiconductor devices connected to your computer together with their serial ports, open a terminal and run the nrfutil device list command. Alternatively, check your operating system’s device manager or its equivalent.

  2. Connect to the kit with a terminal emulator (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps.

  3. Power on or reset your nRF91 Series DK.

  4. Observe that the sample starts, initializes the modem, changes functional mode and shuts down the modem.

Sample output

The sample shows the following output:

Modem callbacks sample started
Initializing modem library
> Initialized with value 0
Changing functional mode
> Functional mode has changed to 1
Shutting down modem library
> Functional mode has changed to 0
> Shutting down
Bye

Dependencies

This sample uses the following nRF Connect SDK libraries:

It uses the following sdk-nrfxlib library:

In addition, it uses the following secure firmware component: