Nordic SoC
VS Code is the only IDE that has extensions for Nordic Connect, so you will want to use that and download the Nordic Connect extensions.
Contents
Installing nrfutil
Download nrfutil, make it executable and move it somewhere where the path can pick it up, e.g.: ~/.local/bin/. You will also need to install the device module with it:
nrfutil search
nrfutil install device
Installing the SDK
I recommend installing version 3.0.2 because it just works whereas 3.1.0 runs into many issues.
3.0.2
Follow the walkthrough for VS Code: vscode:/nordic-semiconductor.nrf-connect-extension-pack/quickstart and select 3.0.2.
3.1.0
Note: I could not get my project to build with this version.
It's easiest to follow the walkthrough for VS Code: vscode:/nordic-semiconductor.nrf-connect-extension-pack/quickstart. I had an issue where installing the SDK would fail, checking the logs it threw on unpacking the SDK archive. I attempting to manually extract this and found that it failed beacause it contained some ridiculously long filenames in there, specifically:
v3.1.0/modules/lib/matter/examples/chef/devices/rootnode_contactsensor_lightsensor_occupancysensor_temperaturesensor_pressuresensor_flowsensor_humiditysensor_airqualitysensor_powersource_367e7cea91
That wasn't the only long file, so I extracted everything but v3.1.0/modules/lib/matter/examples/chef/devices and then extracted file that wasn't too long in that. You will want it extracted to ~/ncs/v3.1.0 but you want to rename it to something else temporarily - click install in VS Code, select the SDK/Version and when it asks for the install path don't hit enter just yet, first rename the directory back to v3.1.0 then hit enter in VS Code and it will detect that it is already installed and work fine. If you don't rename it first VS Code will complain that the directory already exists and if you don't have it named in the right place VS Code will attempt to install it normally and fail again.
Networking
Modem Init
// CONFIG_NRF_MODEM_LIB=y
#include <modem/nrf_modem_lib.h>
// Init modem manually (auto init is disabled)
int err = nrf_modem_lib_init();
if (err < 0) {
printk("Modem init failed: %d\n", err);
}
RAT Selection
Radio Access Technology (RAT). You can enable LTE-M, NB-IoT and GNSS or a combination of these RATs. If you have more than one selected you can configure what one is prefered.
Option 1, force by config.
CONFIG_LTE_NETWORK_MODE_NBIOT=y
Option 2, use lte link control.
// CONFIG_LTE_LINK_CONTROL=y ?
#include <modem/lte_lc.h>
int err = lte_lc_system_mode_set(LTE_LC_SYSTEM_MODE_NBIOT, LTE_LC_SYSTEM_MODE_PREFER_NBIOT);
if (!err) {
printk("RAT Set: %d\n", err);
} else {
printk("Failed to set RAT: %d\n", err);
}
Option 3, use AT commands:
#include <nrf_modem_at.h>
char resp[64];
// AT%XSYSTEMMODE=<LTE-M 1/0>,<NB-IoT 1/0>,<GNSS 1/0>,<Preference 0=none,1=LTE-M,2=NB-IoT,3=SIM-preference-or-LTE-M,4=SIM-preference-or-NB-IoT>
int err = nrf_modem_at_cmd(resp, sizeof(resp), "AT%%XSYSTEMMODE=0,1,0,2");
if (!err) {
printk("Set system mode: %s\n", resp);
} else {
printk("Failed to set system mode: %d\n", err);
}
Public Land Mobile Network (PLMN)
The PLMN is a combination of Mobile Country Code (MCC) and Mobile Network Code (MNC). MCC is a 3 digit number assigned to the country, MNC is a 2 digit code assigned to the network operator. PLMN is MCC concatenated with MNC.
You can find the MCC and MNC here: https://www.mcc-mnc.com
Reading Current
#include <nrf_modem_at.h>
char resp[64];
int err = nrf_modem_at_cmd(resp, sizeof(resp), "AT+COPS?");
if (!err){
printk(resp);
/* E.g.
+COPS: 0,2,"26201",7
OK
*/
} else {
printk("Failed to assign operator: %d\n", err);
}
Manual Selection
This example uses NZ MCC: 530 with One's MNC 01:
#include <nrf_modem_at.h>
const char *PLMN = "53001";
char resp[64];
// AT+COPS=<Mode 0=automatic,1=manual>,<Format=0=Long-Alphanumeric,1=Short-Alphanumeric,2=Numeric>
int err = nrf_modem_at_cmd(resp, sizeof(resp), "AT+COPS=1,2,\"%s\"", PLMN);
if (!err){
printk("Assigned operator: %s\n", resp);
} else {
printk("Failed to assign operator: %d\n", err);
}
Manual Band Selection
You can get or set the bitmask for what bands are permitted, this looks like a string of 1s & 0s where the position (reading from the right) of the 1 is the band that is enabled.
You should check the band on NVM and only set it if it's not already set in order to save NVM write cycles and power.
This example selects bands 3 and 28.
#include <nrf_modem_at.h>
const char *BAND_BIT_MASK = "1000000000000000000000000100";
char resp[128];
// AT%XBANDLOCK=<Mode 0=remove-lock,1=persistant-lock,2=runtime-lock>,<Mask>
int err = nrf_modem_at_cmd(resp, sizeof(resp), "AT%%XBANDLOCK=1,\"%s\"", BAND_BIT_MASK);
if (!err) {
printk("Assigned bit mask: %s\n", resp);
} else {
printk("Failed to assign bit mask: %d\n", err);
}
Connecting
Sync
You can do a synchronous request which will hang your code until it connects or throws.
// CONFIG_LTE_LINK_CONTROL=y
#include <modem/lte_lc.h>
int err = lte_lc_connect();
if (err) {
printk("LTE connect failed: %d\n", err);
}
Async
// CONFIG_LTE_LINK_CONTROL=y
#include <modem/lte_lc.h>
static void lte_handler(const struct lte_lc_evt *const evt) {
if (evt->type == LTE_LC_EVT_NW_REG_STATUS) {
switch (evt->nw_reg_status) {
case LTE_LC_NW_REG_REGISTERED_HOME:
printk("Registered to home network\n");
break;
case LTE_LC_NW_REG_REGISTERED_ROAMING:
printk("Registered to roaming network\n");
break;
case LTE_LC_NW_REG_SEARCHING:
printk("Searching for network\n");
break;
case LTE_LC_NW_REG_REGISTRATION_DENIED:
printk("Registration denied\n");
break;
case LTE_LC_NW_REG_UNKNOWN:
printk("Network status unknown\n");
break;
default:
printk("Network registration failed or unknown status: %d\n", evt->nw_reg_status);
break;
}
}
}
int err = lte_lc_connect_async(lte_handler);
if (err) {
printk("start connect failed, err %d\n", err);
} else {
printk("connect started\n");
}