Feedback
Feedback

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

LTE link controller library

The LTE link controller library is a layer above the nRF Modem library and provides functionality to control the LTE link on the nRF91 Series.

Enabling the library

1. To enable the LTE link controller library in your application, enable the following Kconfig:

2. Disable the automatic init and connect functionality of the library

The library supports the functionality to automatically initialize and connect to the modem before the application starts. This functionality can reduce complexity, but it also increases the time until the application starts while blocking all other operations.

Important

CONFIG_LTE_AUTO_INIT_AND_CONNECT is deprecated in nRF Connect SDK v2.4.0 and up.

3. Include the header file of the LTE link controller library in your source code.

#include <modem/lte_lc.h>

Defining a callback function

One advantage to using the LTE link controller library is having a callback function that allows the application to get callbacks from the library, thereby offloading this process.

4. Define the callback function lte_handler().

For this lesson and subsequent exercise, LTE_LC_EVT_NW_REG_STATUS is the only event where the callback function executes something. This event carries information about the modem’s network registration status, which can tell us if an LTE link has been established.

In the function below, we check the registration status of evt->nw_reg_status of type lte_lc_nw_reg_status and proceed to unblock the main() function by giving the semaphore only if the registration status is of type registered home or registered roaming, indicating an LTE connection.

static void lte_handler(const struct lte_lc_evt *const evt)
{
	switch (evt->type) {
	case LTE_LC_EVT_NW_REG_STATUS:
		if ((evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_HOME) &&
			(evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_ROAMING)) {
			break;
		}
		LOG_INF("Network registration status: %s",
				evt->nw_reg_status == LTE_LC_NW_REG_REGISTERED_HOME ?
				"Connected - home network" : "Connected - roaming");
		k_sem_give(&lte_connected);
		break;
	default:
		break;
	}
}

The documentation for enum lte_lc_evt_type explains all the event types in the LTE link controller library.

Initialize and connect

v2.4.0 – v2.x.x

5.1 Initialize the library with lte_lc_init(), which has the following signature.

Important

lte_lc_init() is deprecated in nRF Connect SDK v2.6.0 and higher.

5.2 Connect to the LTE network using the function lte_lc_connect_async(), which has the following signature.

This function takes the callback function lte_handler() that we defined in the previous step as a parameter.

We will use NCS_VERSION_NUMBER to only call lte_lc_init() if the nRF Connect SDK version is less than v2.6.0.

/* lte_lc_init deprecated in >= v2.6.0 */
#if NCS_VERSION_NUMBER < 0x20600
lte_lc_init();
#endif
	

lte_lc_connect_async(lte_handler);
v2.2.0 – v2.3.0

5.1 Initialize and connect to LET network using the function lte_lc_init_and_connect_async(lte_handler), which has the following signature

This function takes the callback function lte_handler() that we defined in the previous step as a parameter.

lte_lc_init_and_connect_async(lte_handler);

Using a semaphore

To ensure the application doesn’t proceed with other operations until an LTE connection is established, we will add a semaphore.

6. Define the semaphore lte_connected using K_SEM_DEFINE().

Since this semaphore will only be taken once, after the LTE connection function is called and given once, when the LTE connection is established, we pass initial_count 0 and count_limit 1.

K_SEM_DEFINE(lte_connected, 0, 1);

7. After initiating the LTE connection, take the semaphore using k_sem_take().

k_sem_take(&lte_connected, K_FOREVER);

8. To make sure the application doesn’t proceed until an LTE connection is established, we want to give the semaphore, using k_sem_give(), in the event handler, after the registration status is confirmed as connected.

case LTE_LC_EVT_NW_REG_STATUS:
         if ((evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_HOME) &&
         (evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_ROAMING)) {
                 break;
         }

         LOG_INF("Connected to: %s network\n",
         evt->nw_reg_status == LTE_LC_NW_REG_REGISTERED_HOME ? "home" : "roaming");
         k_sem_give(&lte_connected);
         break;

Even though we are using a semaphore to make sure the application doesn’t proceed until an LTE connection is established, this is a non-blocking method because the application can still call functions and create threads after lte_lc_connect_async() (or lte_lc_init_and_connect_async()) has been called.

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.