Section 6.27

J-Link

Definition

J-Link is a family of debug probes made by the company SEGGER Microcontroller §6.1, together with the host software that drives them. A debug probe is an adapter that connects a host computer, usually over USB, to the debug port of a target chip, such as a JTAG §6.26 (Joint Test Action Group) or SWD §6.25 (Serial Wire Debug) port. Through the probe, host programs halt and resume the processor, read and write its registers and memory, and erase and program its flash memory §6.6. The name is a product name, not an abbreviation.

The idea

A microcontroller fresh from the factory has empty flash memory, and a crashed one may run no useful code at all. Neither can help to load or inspect its own program. Yet the chip contains debug logic that works independently of the program: circuitry, reached through the debug port, that can stop the processor and read or write any address. A probe connects the host’s software to that debug logic. It turns a request such as “read four bytes at address 0x20000100” into the bit sequences that the debug port expects, and sends the answer back to the host over USB.

The consequence matters more than the convenience. A probe acts beneath the application. The application is not asked, is not told and cannot refuse. When the probe halts the processor, the application’s code stops between two instructions; when the probe resumes it, the application continues with no record that time passed or that memory changed. A remote control for the processor is a fair picture of this. The picture stops being accurate for the rest of the chip: the probe halts the processor, but timers, radios and the devices around the chip keep going.

How it works

SEGGER’s software package contains several host programs that share one library for talking to the probe.

Many development boards §6.5 carry a built-in version of the probe, so that the board needs only a USB cable.

A session follows these steps.

  1. Select the target device by name. The name tells the software the chip’s memory layout and how to program its flash memory.

  2. Select the interface, SWD or JTAG, and the clock speed of the debug port, for example 4000 kHz (kilohertz), which is 4 million ticks per second.

  3. Connect. The probe establishes communication with the chip’s debug logic.

  4. Issue commands.

The J-Link Commander documentation lists commands such as these, grouped here by what they do to the target:

Flash memory cannot be changed by ordinary memory writes: its controller needs an erase and a program sequence. To program it, the J-Link software typically copies a small flash-programming routine into the target’s RAM and runs that routine on the target’s own processor. Programming therefore stops whatever the application was doing and overwrites part of RAM. The documentation also states that Erase and LoadFile reset the device by default before they start.

Worked example

A hypothetical application counts received packets in a 32-bit variable at address 0x20000100 in RAM. A developer runs J-Link Commander with a command file of five lines: H, Regs, Mem32 0x20000100 1, W4 0x20000100 0x64 and G.

  1. H stops the processor between two instructions. The application’s code is frozen; the rest of the chip is not.

  2. Regs reads the registers, including the program counter §2.9, which holds the address of the next instruction. Reading them changes nothing.

  3. Mem32 reads one word and shows 0x00000029, which is 2×16+9=412 \times 16 + 9 = 41 packets.

  4. W4 writes 0x64, which is 6×16+4=1006 \times 16 + 4 = 100. The application’s count is now 100, although only 41 packets arrived.

  5. G resumes the processor. The application continues from the value 100 and has no record that the probe changed it.

Now suppose the developer pauses for 5 seconds after H. The application uses a watchdog timer §6.16, a hardware timer that resets the chip unless software restarts it before a timeout §9.34 runs out; restarting it is called feeding it. Here the timeout is 2 seconds, and the application feeds the watchdog once every second. The last feed happened between 0 and 1 second before the halt, so the timeout expires between 2−1=12 - 1 = 1 and 2−0=22 - 0 = 2 seconds into the pause. If the watchdog keeps counting while the processor is halted, the chip resets during the pause, and the later commands act on a freshly restarted program, not on the halted one. Many watchdogs have a setting that pauses them while a debugger halts the processor; with that setting, no reset occurs.

Stated precisely

A probe operation is a request to the chip’s debug logic, which acts on hardware state directly. It neither runs the application’s code nor asks for its consent. The exception is flash programming, which runs the probe software’s own routine on the target’s processor. The state that a probe can change falls into these classes:

These powers depend on three conditions. Debug access must not be locked by the chip’s protection setting. The selected device name must match the chip, so that addresses and the flash routine are right. And the selected reset type must match the board’s wiring. A command reported as successful means that the probe completed its access. It does not show that the application reached any state, did any work, or still runs.

Limits and common mistakes

Reading is not always harmless. Reading some peripheral registers changes the peripheral: reading the data register of a serial port removes the byte from its receive buffer, and reading some status registers clears them. The J-Link Commander documentation also says that Mem32 halts the processor if that is necessary to read memory.

A halt stops only the processor. Timers, the radio and other peripherals keep running unless the chip allows them to be frozen during debugging, and devices at the other end of a connection keep their own deadlines. The GDB entry works through a radio example.

A locked chip refuses most debug requests. Usually the only remaining operation is a full erase that removes the protection together with all flash content; see the entry on destructive debug recovery §16.6.

The exit status §13.24 of a command file shows that the commands were carried out. It does not show that the application afterwards did the job it was meant to do. Evidence of that must come from the application itself, such as a message it sends.

Interrupts §6.11 (hardware requests that make the processor run a handler routine) that became pending while the processor was halted may run as soon as it resumes, so the first instructions after G need not be the ones at the address the program counter showed.

Questions answered

Which state changes can a probe perform without application cooperation?

A debug probe can change the processor’s execution state, its registers, memory and peripherals, its breakpoints and the content of flash memory, all without any cooperation from the application. A probe such as J-Link can halt the processor, resume it, step it one instruction at a time and reset it. It can read and write every processor register, including the program counter and the stack pointer. It can read and write RAM and the registers of memory-mapped peripherals, where even a read can have side effects, such as removing a received byte from a serial port’s buffer. It can set breakpoints and watchpoints, and it can erase and program flash memory; on most chips programming runs a routine that the probe loads into RAM, which overwrites part of RAM and replaces whatever the application was doing.

The application is not told about any of this and cannot refuse it; only the chip’s debug protection can, and a locked chip usually allows only a full erase. A halt also changes state indirectly: the processor stops while timers, watchdog timers, radios and remote devices continue, so deadlines can expire and a watchdog can reset the chip during the pause.

Related entries: SWD (§6.25), GDB (§6.28), Asynchronous completion (§9.28), JTAG (§6.26), OpenOCD (§6.29), Watchdog timer (§6.16), ERASEALL and destructive debug recovery (§16.6). Further reading: [34, 177].