Section 16.9

nrfjprog

Full name: nrfjprog: a tool name, not an abbreviation, usually read as nRF (the prefix of Nordic Semiconductor’s chip names), j (J-Link, the debug probe the tool works through) and prog (programming).

Definition

nrfjprog is a command-line program from Nordic Semiconductor that programs and debugs the company’s nRF chips through a J-Link §6.27 debug probe, an adapter made by the company SEGGER that connects a host computer to a chip’s debug port. One run performs the operations its options name, such as erasing flash memory §6.6, writing a firmware file, reading the written bytes back for comparison and resetting the chip. It ends with exit status §13.24 0 only if all of them completed. Nordic has archived nrfjprog and names nRF Util as its replacement.

The idea

A test script must put new firmware on a board and then measure the board. Between the request and a board that runs the new firmware lie separate events: the host starts a program; the probe erases flash pages, writes the new bytes, reads them back and resets the chip; the firmware starts and reaches a working state. Each event can fail on its own, and a probe attached to the wrong board does everything correctly to the wrong chip.

A registered parcel is a fair picture. The counter receipt says the parcel was accepted; the delivery confirmation says it arrived intact; only a reply from the recipient says the recipient acted on it. nrfjprog supplies the delivery confirmation: an exit status that covers writing, read-back comparison and reset. The reply, a sign that the new firmware runs, must come from the firmware. The picture stops at the contents: a parcel service cannot open the parcel, while nrfjprog can read any byte of the chip’s memory.

How it works

nrfjprog drives the probe through SEGGER’s J-Link software, and the probe reaches the chip over SWD §6.25 (Serial Wire Debug), the two-wire debug connection of Arm processors. A typical programming run is nrfjprog -f NRF52 --snr 683012345 --program app.hex --sectorerase --verify --reset. Its options are:

Flash memory must be erased before it is written, because a write can only change bits from 1 to 0, so --program is combined with an erase option. Other options include --memrd, which reads memory, and --recover, which erases the whole chip to remove readout protection §16.5, as the entry on destructive debug recovery §16.6 describes.

One run performs its operations in a fixed order, connect, erase, write, verify, reset, and then exits. Nordic’s documentation states that nrfjprog returns exit code 0 if the requested operation completed successfully, and an error code otherwise. A host program therefore starts the process, recording the tool version and the options; waits for the process to exit; reads the exit status and keeps the printed output; and, for a claim about the running firmware, waits for a separate signal from the firmware, such as a line printed on a UART §6.23 (universal asynchronous receiver-transmitter), a serial port.

Nordic has archived the nRF Command Line Tools, the package that contains nrfjprog, so nrfjprog receives no further updates. Nordic names nRF Util as the replacement: a program called nrfutil whose device command performs the same kinds of operation.

Worked example

This timeline is invented to show the arithmetic. All times are on the host’s monotonic clock §9.35, a clock that never jumps. A script starts the command above at 0.0 s. Erasing ends at 0.4 s, writing at 2.6 s, verification at 3.4 s and the reset at 3.5 s; the process exits with status 0 at 3.6 s. The firmware prints ready on the UART at 4.3 s.

The erase option decides what survives. Let app.hex hold 100 KiB of code starting at address 0, on a chip with 512 KiB of flash memory in 4 KiB pages, which is 512/4=128512 / 4 = 128 pages. --sectorerase erases the 100/4=25100 / 4 = 25 pages the file touches and leaves the other 128−25=103128 - 25 = 103 pages and the UICR as they were. --chiperase erases all 128 pages and the UICR, including any keys stored there. In both cases --verify compares only the 100 KiB that the file contains.

Stated precisely

For one run, exit status 0 states that every requested operation completed without an error that the tool detected. With the options above, it states four facts:

  1. the pages that the file touches were erased;

  2. the file’s bytes were written;

  3. at the time of the comparison, the byte at every address in the file equaled the file’s byte;

  4. a reset was issued.

Exit status 0 does not state that the probe was attached to the intended board, that addresses outside the file hold particular values, that the new firmware started, or that it reached a working state.

Let tst_s, tet_e and trt_r be the times of the process start, the process exit and the firmware’s ready signal, all on one host clock. Programming time is te−tst_e - t_s; time to service is tr−tst_r - t_s. The reset comes before tet_e, so fast firmware can signal ready before the process exits: trt_r follows the reset but does not always follow tet_e. Each measured interval names its timing endpoint §18.1.6, the event that ends it.

These statements assume the installed version’s documented meaning of the exit status, a probe serial number that belongs to the intended board, and a file that is the intended firmware. The last is a question of artifact provenance §13.1, settled by recording a cryptographic hash §5.1 of the file with the result.

Limits and common mistakes

Starting a process is not completing it. A call that launches nrfjprog and returns at once, such as Python’s §12.23 subprocess.Popen, shows only that the process started; subprocess.run waits for the exit and returns the exit status.

Verification is optional and partial. Without --verify, a run includes no read-back comparison. With it, only the addresses in the file are compared, at one moment. Old content elsewhere survives --sectorerase: that can be intended, as in credential-preserving programming §16.7, which keeps provisioned keys across a firmware update, or a hazard, when stale code or data stays behind. Firmware that rewrites its own flash memory after the reset changes the verified bytes later.

A serial number names a probe, not a chip. Reading the chip’s own device identifier, which the factory stores in the FICR (Factory Information Configuration Registers) §16.2, confirms which chip was programmed.

Some erase options destroy provisioning. --chiperase, --eraseall, which erases all flash memory, and --recover remove keys and settings stored in the UICR or elsewhere in flash memory.

Error codes other than 0 differ between versions. Rely on 0 versus non-zero, look up other values in the installed version’s documentation, and record the output of nrfjprog --version with every result.

Questions answered

Which command reports completion of actual programming rather than only local invocation?

A single nrfjprog run that both programs and verifies, such as nrfjprog -f NRF52 --snr 683012345 --program app.hex --sectorerase --verify --reset, reports completion of actual programming, provided the host waits for the process to exit and reads exit status 0. That status states that the pages the file touches were erased, the file’s bytes were written, every address in the file read back equal to the file, and the chip was reset. Local invocation, merely starting the process, reports nothing about the chip: a launcher that returns at once shows only that the program started.

Two limits remain. The run does not report that the new firmware started or works; that needs a separate signal from the firmware, such as a ready message on a serial port. And the meaning of the exit status belongs to the installed tool version, which should be recorded. nrfjprog is archived; its replacement, nRF Util, raises the same questions.

Related entries: J-Link (§6.27), Bootloader (§6.30), NVMC (Non-Volatile Memory Controller) (§16.3), Credential-preserving programming (§16.7), Artifact provenance (§13.1), SWD (§6.25), Exit status (§13.24), ERASEALL and destructive debug recovery (§16.6). Further reading: [58].