Skip to content

Recipes

Four jobs people use a PPK2 for, done from a script.

Flashing a DUT powered by the PPK2

A device under test (DUT) that gets its power from the PPK2 is off until a command switches it on, and its debug port does not answer. To flash it, power it for the time the programmer runs:

ppk2 run --source --vdd 3.3 -- west flash

ppk2 run switches power on, runs the command after --, and switches power off when it ends. It exits with the command's exit code, so it fits in a Makefile, a just recipe or a CI job. To record while the programmer runs, add --out flash.csv.

To flash from another terminal, or to debug, hold the power on until Ctrl-C:

ppk2 power on --source --vdd 3.3 --hold

The same from Python:

import subprocess

from ppk2 import Mode, Session, find_ppk2

with Session.open(find_ppk2().device) as session:
    session.set_mode(Mode.SOURCE)
    session.set_source_voltage(3300)
    session.power_on()  # the power is up about 1 ms later
    code = subprocess.run(["west", "flash"]).returncode
raise SystemExit(code)

An nRF chip stays in debug mode after it is flashed and draws more until it is power-cycled. On the nRF54LM20 DK, the bench firmware of this repository then idled at 330 µA, against 1.4 µA after a power cycle. So measure with a fresh ppk2 capture, which power-cycles the DUT, and not inside the session that flashed.

Current of a marked region

You want to know what one piece of firmware costs, a sensor read or a radio transmission, and not the average over a whole capture. The PPK2 samples its digital inputs with every current sample. Raise a GPIO while the code runs, and add up the current while the pin is high.

gpio_set(MARK_PIN, 1);
read_sensor();
gpio_set(MARK_PIN, 0);

Wire the pin to D0 of the logic port, with the port's GND and VCC connected as in getting started, and capture as usual. The D0 column of the file is 1 exactly while the code runs.

The current of a region is the mean of its samples. One sample lasts 10 µs, so the charge of a region is the sum of its samples times 10 µs, in microcoulombs. These are the figures the app shows for a selection: average, max, time and charge.

import pandas as pd

SAMPLE_S = 10e-6  # one sample lasts 10 µs

df = pd.read_csv("trace.csv", dtype={"D0-D7": str})
high = df["D0"] == 1
region = (high & ~high.shift(fill_value=False)).cumsum()  # counts the rising edges

regions = df[high].groupby(region[high])["Current(uA)"].agg(["size", "mean", "max", "sum"])
regions["duration_ms"] = regions["size"] * SAMPLE_S * 1e3
regions["charge_uC"] = regions["sum"] * SAMPLE_S  # µA × s = µC
print(regions[["duration_ms", "mean", "max", "charge_uC"]].round(2))

On source_ppk2_1.csv.xz, one of the captures of the validation, it prints these first rows. The firmware of that bench keeps the CPU busy for 100 ms while D0 is high:

    duration_ms     mean      max  charge_uC
D0
1        100.00  1386.36  2101.04     138.64
2        100.00  1385.54  2225.01     138.55
3         99.97  1384.69  2420.94     138.43
4        100.00  1385.79  2171.81     138.58

Over the 147 regions of that 30 s capture the mean current stayed between 1,382.40 and 1,387.90 µA. pandas reads the compressed file as it is.

Three things to watch:

  • A capture can start or end in the middle of a region. Drop the first and the last row if they are cut.
  • A lost sample is a row of 0.2 µA in the CSV, as in Nordic's app, so it lowers the mean and the sum. The summary at the end of a capture says whether any were lost.
  • There is no energy here. The PPK2 measures current only. An energy is the charge times the voltage at the DUT, which you have to know from elsewhere: the voltage you set in Source meter mode, or one you measured in Ampere meter mode.

To get the numbers without a file, add up inside the capture callback:

from ppk2 import Mode, Session, find_ppk2


class RegionCurrent:
    """Averages the current of each D0-high region; stops after ``wanted`` regions."""

    def __init__(self, wanted: int) -> None:
        self.wanted = wanted
        self.means_ua: list[float] = []
        self._inside = False
        self._sum_ua = 0.0
        self._count = 0

    def __call__(self, sample) -> bool:
        d0 = bool(sample.logic & 1)
        if d0 and not self._inside:
            self._inside, self._sum_ua, self._count = True, 0.0, 0
        if self._inside and d0:
            self._sum_ua += sample.current_micro_amps
            self._count += 1
        if self._inside and not d0:
            self._inside = False
            self.means_ua.append(self._sum_ua / self._count)
        return len(self.means_ua) < self.wanted


regions = RegionCurrent(wanted=10)
with Session.open(find_ppk2().device) as session:
    session.set_mode(Mode.SOURCE)
    session.set_source_voltage(3300)
    session.start_measuring()
    session.power_on()
    session.capture(regions)

mean = sum(regions.means_ua) / len(regions.means_ua)
print(f"{len(regions.means_ua)} regions, mean current {mean:.2f} µA")
10 regions, mean current 1384.43 µA

Long captures

The PPK2 sends 100,000 samples per second whatever the DUT does, so the first question is the size of the file.

Duration Samples CSV .ppk2
1 minute 6 million 240 MB about 7 MB
1 hour 360 million 14 GB about 0.45 GB
8 hours 2.9 billion 115 GB about 3.5 GB

The .ppk2 column comes from a capture of ten minutes of the bench firmware of this repository, which took 73.5 MB. A session file is compressed, so a noisier signal takes more.

While it is being written, a .ppk2 capture uses 6 bytes per sample in a file next to the output: 36 MB per minute, 17 GB for 8 hours.

ppk2 capture --out hour.ppk2 --seconds 3600

What to expect:

  • The longest capture run for this release lasted ten minutes, to a .ppk2 file: 60,000,000 samples, none lost. Nothing in the program depends on the duration, but hours-long captures have not been run, and a session file of several gigabytes has not been tried, here or in the app.
  • Ctrl-C stops the capture and completes the file. A CSV always ends on a complete row. A .ppk2 file appears complete or not at all.
  • When the computer does not keep up, samples are lost. The capture warns once 500 are missing and gives the total at the end. The trace keeps its true length as long as fewer than 64 samples are lost in a row. Past that the count can be short by a multiple of 64. A capture that loses that much warns that its stream rate is low: do not rely on the duration or the charge of a capture that printed that warning.
  • The rate cannot be lowered at the PPK2. To keep fewer values, average blocks of samples afterwards: ppk2 convert accepts a CSV reduced to a power of ten, such as 1,000 samples per second.

Comparing two firmware builds

You changed something and want to know whether the current went down. Capture the same workload before and after, and compare the files.

  1. Fix everything except the firmware: the same DUT, wiring, --vdd and duration. Whatever you leave free ends up in the comparison.
  2. Capture the first build: ppk2 capture --out before.csv --seconds 60.
  3. Flash the second build.
  4. Capture again, to after.csv.

To compare the two files and write a report, run this script from a checkout of the repository:

uv run --group validation python -m validation.compare_cli before.csv after.csv \
    --label-a before --label-b after --skip-initial-s 1 --out report.md

It reports the mean of each file and the Kolmogorov-Smirnov (KS) statistic between the two distributions: 0 when they are the same, 1 when they do not overlap.

Before you read a difference as a result, know how much two identical runs differ. In the validation, two captures of the same firmware taken four minutes apart differ by 0.05 % on the current of a marked region. At a few µA the spread can be larger.

Measure the spread of your own bench with two runs of one build, and trust only differences well above it.

Two more cautions:

  • A capture that starts at power-on contains the boot, which differs from one power cycle to the next. --skip-initial-s leaves it out.
  • The mean says how much, not how. For a workload made of bursts, mark them with a GPIO and compare region by region.