ISO-TP: flow control and timing
Block size, STmin, the N timers, wait and overflow, and a receiver that survives a bad sender.
The receiver is in charge
After a first frame the sender may not send anything until the receiver says so. The receiver's flow control frame is three bytes of protocol and five of padding:
30 08 14 CC CC CC CC CC
| | |
| | +-- STmin: at least 0x14 = 20 ms between consecutive frames
| +----- BS: block size, 8 consecutive frames, then wait for flow control again
+------- PCI: type 3, flow status 0 (continue to send)
Each field exists because of a real limit in a real receiver:
| Field | Protects the receiver from | Typical cause |
|---|---|---|
| Flow status | being sent data it cannot take at all, or not yet | no buffer that large; still busy with the last block |
| Block size (BS) | running out of buffer part-way | a gateway that forwards through a small queue; a receiver that must write each block to flash |
| STmin | frames arriving faster than it can empty its CAN receive FIFO | a slow main loop, or a bridge onto a slower bus such as LIN or K-line |
Flow status
| Value | Name | The sender must |
|---|---|---|
0 | ContinueToSend (CTS) | take BS and STmin from this frame and start sending |
1 | Wait | keep waiting and restart its timeout; another flow control frame will follow |
2 | Overflow | abandon the message: the receiver has no room for the length the first frame announced |
3 to F | reserved | abandon the message, reporting an invalid flow status |
Overflow is only legal as the answer to a first frame. A receiver that can never take a message of that length says so at once, before any data has moved.
A receiver may answer "wait" several times, but not for ever: the sender counts them against a limit, N_WFTmax, and gives up when it is exceeded. If the limit is configured as 0, the receiver is not allowed to send wait frames at all.
Block size
BS = 0 means "send everything that is left, I will not send another flow control frame". Any other value is a count of consecutive frames. After that many, the sender stops and waits for the next flow control frame, which may carry different values of BS and STmin: the standard lets a receiver adapt as its buffer fills. A sender that stores BS and STmin once per message and reuses them is wrong, and passes every test written by someone who made the same assumption.
STmin
The encoding has a trap in it:
| Byte | Meaning |
|---|---|
00 to 7F | 0 to 127 milliseconds |
F1 to F9 | 100 to 900 microseconds |
80 to F0, FA to FF | reserved: treat as 7F, 127 ms, the longest valid value |
STmin is measured from the end of one consecutive frame to the start of the next, on the wire. The sender, not the receiver, enforces it. Two consequences for firmware:
- A sender with a 1 ms tick cannot time 300 µs. It may wait longer than STmin, never shorter, so it rounds up: the next tick, or a hardware timer if the project needs the throughput.
- "Send a frame, then start a 20 ms timer" measures from when the frame was queued, not when it finished. If the frame then waits in the transmit mailbox behind higher-priority traffic, the gap on the wire can be shorter than STmin. Careful implementations start the STmin timer from the transmit confirmation.
#include <stdio.h>
#include <stdint.h>
/* STmin from a flow control frame, in microseconds. */
static uint32_t stmin_us(uint8_t raw) {
if (raw <= 0x7F) return raw * 1000u;
if (raw >= 0xF1 && raw <= 0xF9) return (raw - 0xF0u) * 100u;
return 127000u; /* reserved: the longest valid value */
}
/* The same for a sender with a 1 ms tick: the ticks to wait, never fewer than STmin. */
static uint32_t stmin_ticks(uint8_t raw) {
return (stmin_us(raw) + 999u) / 1000u;
}
int main(void) {
const uint8_t samples[] = { 0x00, 0x14, 0x7F, 0x80, 0xF1, 0xF5, 0xF9, 0xFA };
for (unsigned i = 0; i < sizeof samples; i++)
printf("STmin 0x%02X: %6lu us, wait %3lu tick(s)\n", samples[i],
(unsigned long)stmin_us(samples[i]), (unsigned long)stmin_ticks(samples[i]));
return 0;
}
The timers
ISO 15765-2 names six timing parameters. Four of them are timeouts an implementation enforces; the default for each is 1000 ms.
| Parameter | Who | From | To | On timeout |
|---|---|---|---|---|
| N_As | sender | asking the driver to send a frame | its transmit confirmation | abandon the message |
| N_Ar | receiver | asking the driver to send a flow control frame | its transmit confirmation | abandon the reception |
| N_Bs | sender | first frame sent, or last consecutive frame of a block sent, or a wait frame received | the next flow control frame | abandon the message |
| N_Cr | receiver | flow control sent, or a consecutive frame received | the next consecutive frame | abandon the reception |
The other two, N_Br (how long the receiver takes to send its flow control frame) and N_Cs (how long the sender takes to send the next consecutive frame), are performance requirements on your own code rather than timeouts you watch. They must be short enough that the other side's N_Bs and N_Cr do not expire: in practice N_Br + N_Ar under about 900 ms.
The timers are what make the protocol safe against a peer that disappears. Without N_Cr a receiver whose sender was unplugged mid-message would hold its buffer, and its "busy" state, for ever; the next request would be refused or, worse, appended to the half-received one.
A receiver that survives a bad sender
A sender can be written in an afternoon. The receiver is where implementations differ, because it has to do something defined for every frame that arrives in every state. The standard gives a table for unexpected frames; these are the rows that matter:
| While | Arrives | A correct receiver |
|---|---|---|
| idle | consecutive frame | ignores it |
| idle | flow control (it is not sending) | ignores it |
| receiving a segmented message | single frame | ends the reception in progress, and processes the single frame as a new message |
| receiving a segmented message | first frame | ends the reception in progress, and starts a new one |
| receiving | consecutive frame with the wrong sequence number | ends the reception; nothing is passed up |
| receiving | nothing for N_Cr | ends the reception |
| any | a frame whose PCI claims more bytes than the frame holds, a first frame with a length under 8, a single frame with a length of 0 | ignores it |
The pattern: a new message always wins over an old one, and a damaged message is dropped whole. Nothing half-received ever reaches the application.
Here is the heart of a receiver. Read it for the order of the checks.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define BUF 64
typedef struct { uint8_t buf[BUF]; uint16_t len, got; uint8_t sn, active; } rx_t;
/* One frame in. Returns the length of a completed message, 0 while there is none. */
static uint16_t rx_frame(rx_t *r, const uint8_t *d, uint8_t dlc) {
uint8_t type = d[0] >> 4, low = d[0] & 0x0F;
if (type == 0) { /* single frame */
if (low == 0 || low > 7 || low >= dlc) return 0;
r->active = 0; /* a new message ends an old one */
memcpy(r->buf, &d[1], low);
return low;
}
if (type == 1) { /* first frame */
uint16_t len = (uint16_t)((low << 8) | d[1]);
if (dlc < 8 || len < 8) return 0;
r->active = 0;
if (len > BUF) { printf(" -> flow control: overflow\n"); return 0; }
memcpy(r->buf, &d[2], 6);
r->len = len; r->got = 6; r->sn = 1; r->active = 1;
printf(" -> flow control: continue\n");
return 0;
}
if (type == 2 && r->active) { /* consecutive frame */
if (low != r->sn) { r->active = 0; printf(" (sequence error: dropped)\n"); return 0; }
uint16_t n = (uint16_t)(r->len - r->got);
if (n > 7) n = 7;
memcpy(&r->buf[r->got], &d[1], n);
r->got = (uint16_t)(r->got + n);
r->sn = (uint8_t)((r->sn + 1) & 0x0F);
if (r->got == r->len) { r->active = 0; return r->len; }
}
return 0;
}
int main(void) {
rx_t r = { 0 };
const uint8_t frames[][8] = {
{ 0x10, 0x0A, 1, 2, 3, 4, 5, 6 }, /* 10 bytes announced */
{ 0x22, 7, 8, 9, 10, 0xCC, 0xCC, 0xCC }, /* sequence 2 where 1 is due */
{ 0x10, 0x0A, 1, 2, 3, 4, 5, 6 }, /* the sender tries again */
{ 0x21, 7, 8, 9, 10, 0xCC, 0xCC, 0xCC },
};
for (unsigned i = 0; i < 4; i++) {
printf("frame %02X ...\n", frames[i][0]);
uint16_t n = rx_frame(&r, frames[i], 8);
if (n) printf(" message of %u bytes, last byte %u\n", n, r.buf[n - 1]);
}
return 0;
}
What the sketch leaves out, and a production receiver has: the N_Cr timer, a flow control frame every BS frames when it advertises a non-zero block size, and one of these structures for each connection.
Half duplex, buffers and throughput
- Most ECUs are half duplex on a diagnostic connection: they do not send a segmented response while receiving a segmented request. UDS is a request-response protocol, so this costs nothing.
- The transmit and receive buffers set the largest message. An ECU that only needs 64-byte messages answers a larger first frame with overflow instead of reserving 4 KB of RAM. A bootloader, which receives firmware in blocks of several kilobytes, does reserve it, or (in AUTOSAR terms) hands each frame's payload straight to the upper layer as it arrives, so that no full-size buffer exists at all.
- Throughput is
7 bytes / (frame time + STmin)while a block lasts, plus a flow control exchange per block. With STmin 0 and BS 0 at 500 kbit/s that is about 25 KB/s. With STmin 20 ms it falls to 350 bytes a second. A slow flash programming session in a factory is very often one conservative STmin in one gateway, which is why production testers negotiate it and why CAN FD, with 63 bytes a frame, cut reprogramming times so sharply.
Interview question: What are block size and STmin in ISO-TP, and who enforces them?
Answer: Both are set by the receiver of a segmented message, in its flow control frame, and enforced by the sender. Block size is the number of consecutive frames the sender may send before it must stop and wait for another flow control frame; zero means no limit. STmin is the minimum time between the end of one consecutive frame and the start of the next: 0 to 127 ms as 0x00 to 0x7F, or 100 to 900 microseconds as 0xF1 to 0xF9, with other values reserved and treated as 127 ms. They let a receiver with a small buffer or a slow loop, or a gateway onto a slower bus, slow the sender down instead of losing frames.
Interview question: Which timeouts does an ISO-TP implementation need, and what does each one catch?
Answer: N_Bs on the sender: the time allowed for a flow control frame after the first frame, after the last frame of a block, or after a wait frame; it catches a receiver that never answers. N_Cr on the receiver: the time allowed for the next consecutive frame; it catches a sender that stops mid-message. N_As and N_Ar on each side: the time allowed for the CAN driver to confirm that a frame was sent; they catch a bus that is down or a node that cannot win arbitration. Each defaults to 1000 ms, and on expiry the transfer is abandoned and the upper layer is told.
Going deeper
Interview question: Your ECU must receive 4 KB firmware blocks but has 2 KB of RAM to spare. How do you design the transport layer?
Answer: Do not reassemble the whole message in RAM. Let the transport layer hand each frame's payload to the upper layer as it arrives, which writes it into a page buffer and programs flash a page at a time; this is how AUTOSAR's CanTp and PduR pass data with copy callbacks instead of a contiguous buffer. Use the flow control parameters to keep pace: advertise a block size equal to what fits in the page buffer, and send the next flow control frame only after the page has been programmed, using the wait status if programming takes longer than the sender's N_Bs allows. The integrity check is then done over the flash contents at the end, by the download service's checksum or signature.
Interview question: Why should the STmin timer start at the transmit confirmation rather than at the transmit request?
Answer: STmin is defined on the bus, from the end of one consecutive frame to the start of the next. A frame handed to the CAN controller may wait in its mailbox while higher-priority identifiers win arbitration, so the moment of the request and the moment the frame ends can be milliseconds apart. If the timer starts at the request, the next frame is queued STmin after the previous request, and the two can appear on the bus back to back, breaking the receiver's limit exactly when the bus is busiest. Starting from the confirmation, the interrupt or callback that says the frame is out, keeps the gap correct under load.
Interview question: How would you test an ISO-TP implementation before it meets a real tester?
Answer: Unit-test the state machine on a host with a fake CAN driver and a fake clock: feed it frames and ticks and check the frames it sends. Cover every row of the unexpected-frame table, sequence wrap from 15 to 0 with a message over 111 bytes, block sizes 1 and 255, a second flow control frame with different values, STmin in each encoding range, wait frames up to and beyond N_WFTmax, overflow, each timeout one tick before and after its limit, and malformed lengths. Then run it against an independent implementation, such as the Linux kernel's can-isotp sockets on a virtual CAN interface, since two implementations by the same author share the same misreadings of the standard.
Explain it: In your own words, explain what happens between a sender and a receiver when the flow control frame says 30 03 0A and the message needs seven consecutive frames.
Answer: The receiver has said continue, with a block size of 3 and an STmin of 10 ms. The sender transmits consecutive frames 1, 2 and 3, leaving at least 10 ms between the end of one and the start of the next, then stops and starts its N_Bs timer. The receiver sends another flow control frame when it is ready, perhaps with different values. If it repeats 30 03 0A, the sender sends frames 4, 5 and 6 the same way and waits again; after a third flow control frame it sends the seventh and last frame. If any flow control frame fails to arrive within N_Bs, or says overflow, the sender abandons the message.