ISO-TP: frames and segmentation
Single, first, consecutive and flow control frames, addressing, padding, and CAN FD.
The problem ISO-TP solves
A classic CAN frame carries at most 8 bytes. A vehicle identification number is 17. A list of stored trouble codes can be 200, and a firmware download block 4 KB. Something has to cut a long message into frames at the sender and put it back together at the receiver, cope with a receiver that is slower than the sender, and notice when a frame goes missing.
That something is ISO 15765-2, known as ISO-TP (transport protocol) or, in AUTOSAR, CanTp. It sits between the CAN driver and the diagnostic protocol (UDS or OBD), and it is deliberately small: four frame types, three parameters and a handful of timers. Every ECU that speaks diagnostics over CAN implements it, which is why interviewers for automotive roles ask you to draw it on a whiteboard.
ISO-TP provides, in the OSI model's words, the network and transport layers:
| Layer | In a car |
|---|---|
| Application | UDS (ISO 14229-1), OBD (ISO 15031 / SAE J1979) |
| Transport and network | ISO-TP (ISO 15765-2) |
| Data link and physical | CAN (ISO 11898) |
It promises its user one thing: a message of up to 4095 bytes (more with the 2016 edition) either arrives complete and in order, or the user is told that it did not. It does not retransmit. If a frame is lost, the whole message is abandoned and the application decides what to do, which for UDS is usually "the tester asks again".
Protocol control information
The first byte of every ISO-TP frame is its PCI (protocol control information). Its high nibble is the frame type:
| High nibble | Frame | Rest of the PCI | Payload |
|---|---|---|---|
0 | Single frame (SF) | low nibble: length, 1 to 7 | up to 7 bytes |
1 | First frame (FF) | low nibble and next byte: 12-bit length, 8 to 4095 | the first 6 bytes |
2 | Consecutive frame (CF) | low nibble: sequence number | up to 7 bytes |
3 | Flow control (FC) | low nibble: flow status; then block size, STmin | none |
A message of 7 bytes or fewer travels in one single frame, with no handshake at all. Most diagnostic requests are this short, so most of the traffic on a diagnostic bus is single frames:
request 7E0 02 3E 00 CC CC CC CC CC 2 bytes: 3E 00 (TesterPresent)
response 7E8 02 7E 00 CC CC CC CC CC 2 bytes: 7E 00
A longer message needs the other three types. Here a tester reads a 17-character VIN; the response is 20 bytes (62 F1 90 and the 17 characters):
tester 7E0 03 22 F1 90 CC CC CC CC SF: ReadDataByIdentifier F190
ECU 7E8 10 14 62 F1 90 54 45 31 FF: length 0x014 = 20, then 6 bytes
tester 7E0 30 00 00 CC CC CC CC CC FC: continue, no block limit, no minimum gap
ECU 7E8 21 42 43 4D 34 34 36 52 CF 1: 7 bytes
ECU 7E8 22 45 30 30 30 30 34 32 CF 2: the last 7 bytes
Three details in that trace are worth being able to state exactly:
- The length in the first frame is the whole message's, not what the frame carries.
10 14is 0x014 = 20 bytes. The receiver uses it to size its buffer and to know when the last consecutive frame has arrived. - Sequence numbers start at 1, because the first frame counts as number 0. They run 1, 2 ... 15, then wrap to 0 and carry on. A 4-bit counter cannot tell you how many frames were lost, only that one was: any gap ends the reception.
- The last consecutive frame is usually part-filled. The receiver takes only as many bytes as the announced length still needs and ignores the rest.
Segmenting a message
The sender's arithmetic is short: 6 bytes in the first frame, 7 in each consecutive frame.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define PAD 0xCC
static void show(const uint8_t *f) {
for (int i = 0; i < 8; i++) printf("%02X ", f[i]);
printf("\n");
}
/* Prints the frames that carry msg[0..len). */
static void segment(const uint8_t *msg, uint16_t len) {
uint8_t f[8];
memset(f, PAD, sizeof f);
if (len <= 7) { /* single frame */
f[0] = (uint8_t)len;
memcpy(&f[1], msg, len);
show(f);
return;
}
f[0] = (uint8_t)(0x10 | (len >> 8)); /* first frame: 12 bits of length */
f[1] = (uint8_t)len;
memcpy(&f[2], msg, 6);
show(f);
printf(" (wait for flow control)\n");
uint8_t sn = 1;
for (uint16_t sent = 6; sent < len; sn = (uint8_t)((sn + 1) & 0x0F)) {
uint16_t n = (uint16_t)(len - sent);
if (n > 7) n = 7;
memset(f, PAD, sizeof f);
f[0] = (uint8_t)(0x20 | sn);
memcpy(&f[1], &msg[sent], n);
show(f);
sent = (uint16_t)(sent + n);
}
}
int main(void) {
uint8_t vin[20] = { 0x62, 0xF1, 0x90 };
memcpy(&vin[3], "TE1BCM446RE000042", 17);
segment(vin, sizeof vin);
return 0;
}
The number of consecutive frames for a message of len bytes (more than 7) is (len - 6 + 6) / 7, that is ceil((len - 6) / 7). A full 4095-byte message is one first frame and 585 consecutive frames. At 500 kbit/s a frame takes roughly a quarter of a millisecond on the wire, so the bus itself would allow that in about 150 ms; it is the receiver's flow control, in the next lesson, that usually sets the real speed.
Padding and frame length
Classic CAN lets a frame be shorter than 8 bytes, so a single frame with a 2-byte message could go out with a DLC of 3. ISO-TP allows that, but almost every vehicle manufacturer requires padding: every diagnostic frame is 8 bytes, and unused bytes carry a fixed value. Reasons:
- Receivers can reject any diagnostic frame whose DLC is not 8 before looking inside it.
- A constant frame length makes bus-load and timing calculations simple.
- The pad value is chosen to need few stuff bits.
0xCC(11001100) and0xAA/0x55never contain five equal bits in a row;0x00and0xFFare the worst choices, each adding a stuff bit every five bits.
A robust receiver checks the length the PCI claims against the bytes actually received. A single frame that says "7 bytes" in a frame with a DLC of 4 is malformed and must be ignored: not answered, not half-processed.
Pitfall: The pad bytes are not part of the message. Code that passes "8 minus the PCI" bytes up to the application, instead of the length from the PCI, works for every full frame in the test suite and then treats
CCas data in the field.
Addressing: who is talking to whom
CAN identifiers name messages, not nodes, so ISO-TP needs a convention for "this tester to that ECU". The common schemes:
| Scheme | How the address is carried | Where you meet it |
|---|---|---|
| Normal | A pair of 11-bit identifiers per ECU, one for each direction | Most passenger-car diagnostics: 0x7E0 request, 0x7E8 response |
| Normal fixed | 29-bit identifiers 0x18DA<target><source> (physical) and 0x18DB<target><source> (functional) | Trucks and buses, with J1939 on the same wires |
| Extended | The first data byte is the target address; the PCI moves to byte 1 and each frame carries one byte less | Gateways, some older European platforms |
| Mixed | The first data byte is an address extension, used for remote networks behind a gateway | Sub-networks such as LIN nodes reached through CAN |
Two more terms come up in every specification:
- Physical addressing is one tester to one ECU. Any message length is allowed.
- Functional addressing is one tester to every ECU at once (
0x7DFin the OBD identifier scheme). Only single frames are allowed, because flow control is a conversation between two nodes: thirty ECUs cannot each send a flow control frame for the same first frame.
The emissions regulations fix the OBD identifiers, which is why you will see them on every car: 0x7DF functional, 0x7E0 to 0x7E7 for requests to up to eight ECUs, and 0x7E8 to 0x7EF for their responses (request identifier + 8).
CAN FD
CAN FD frames hold up to 64 bytes, and ISO 15765-2:2016 extends the PCI to use them:
- A single frame in a frame longer than 8 bytes has a first byte of
00and the length in the second byte, so one frame carries up to 62 bytes. A 20-byte VIN response becomes a single frame, with no flow control at all. - A first frame with a 12-bit length of 0 is an escape: the next four bytes hold a 32-bit length, for messages beyond 4095 bytes (firmware downloads).
- Consecutive frames carry up to 63 bytes.
- CAN FD has no DLC for 9, 10 or 11 bytes (only 12, 16, 20, 24, 32, 48, 64), so frames are padded up to the next valid length. Padding is therefore mandatory, not a manufacturer's preference.
The state machine is unchanged, which is the point: an implementation written with the frame's capacity as a parameter, not the constant 8, handles both.
Interview question: Walk through the ISO-TP frames for a 20-byte response.
Answer: The sender transmits a first frame: PCI nibble 1, a 12-bit length of 20 (bytes 10 14), and the first 6 payload bytes. It then waits for the receiver's flow control frame, which carries a flow status (continue, wait or overflow), a block size and STmin, the minimum gap between consecutive frames. On "continue" it sends consecutive frames, each with PCI nibble 2 and a sequence number starting at 1, carrying 7 bytes each: here two of them. The receiver knows the message is complete when it has the 20 bytes the first frame announced. If the block size is non-zero the sender stops after that many consecutive frames and waits for another flow control frame.
Interview question: Why does ISO-TP not retransmit a lost frame?
Answer: CAN already detects corrupted frames and retransmits them at the data-link layer, so a frame that the controller reports as sent was received by at least one node. What is left are rare losses such as a receive buffer overrun in a slow ECU. Recovering from those inside the transport layer would need acknowledgements and buffers for every frame, which is costly on a small MCU. ISO-TP instead detects the loss, through the sequence number or a timeout, abandons the whole message, and reports the error upward. The diagnostic layer's request and response pattern makes a retry easy: the tester simply sends the request again.
Going deeper
Interview question: An ECU's ISO-TP layer works on the bench, but in the car long responses to one tester fail. What do you check first?
Answer: The flow control frame and how the sender obeys it. Capture the trace and read the tester's block size and STmin: a tester on a slow gateway may ask for a block size of 8 and 20 ms between frames, where the bench tool sent 30 00 00. Check that the ECU waits for a new flow control frame after each block, spaces its consecutive frames by at least STmin measured at the wire rather than at the call to the driver, handles the wait status, and restarts its timeout after every flow control frame. Then check the padding and DLC the car's gateway expects, and that the sequence number wraps from 15 to 0, which only shows on messages longer than 111 bytes.
Interview question: Why are 0x00 and 0xFF poor choices for the padding byte?
Answer: CAN inserts a stuff bit of opposite polarity after five equal bits, so the receiver always has edges to resynchronise on. A run of 0x00 or 0xFF bytes is nothing but equal bits and so collects a stuff bit every five bits, making each padded frame up to about 20 percent longer on the wire. Values such as 0xCC, 0xAA and 0x55 alternate often enough to need none. Shorter frames mean lower bus load and less latency for everything else on the bus, and frequent edges keep receivers better synchronised.
Interview question: How would you write an ISO-TP layer that serves several testers or connections at once?
Answer: Keep all the state of one transfer in a connection structure: the buffers, the length announced and received, the next sequence number, block and STmin counters, the timers, and the identifiers of both directions. A table maps each receive identifier to its connection, and the periodic function steps every active connection. Nothing is global, so a functional single frame can arrive while a physical multi-frame reception is under way, as the standard requires. AUTOSAR's CanTp is configured exactly this way, with one channel per connection and the N-SDU identifiers generated from the configuration.
Explain it: In your own words, explain how a receiver knows that a multi-frame ISO-TP message is complete and correct.
Answer: The first frame states the total length, so the receiver counts the bytes it has collected from the first frame and each consecutive frame and is done when the count reaches that length; the padding of the last frame is ignored. Each consecutive frame carries a 4-bit sequence number that must be exactly one more than the last, wrapping from 15 to 0, so a missing, repeated or reordered frame is noticed at once. A timer catches the case where the next frame never comes. If the count completes with every number in order and no timeout, the message is whole; otherwise the reception is abandoned and nothing is passed up.