Try Embedded

UDS: data identifiers and the fault memory

Reading and writing DIDs, and the services that report and clear trouble codes.

Data identifiers

Almost everything a tester reads from or writes to an ECU is addressed by a 16-bit data identifier (DID). A DID names one record: a serial number, a sensor value, a block of configuration. The record's length and layout are fixed for that DID in that ECU, and are described to the tester by a data file (ODX or CDD) that the ECU's supplier delivers with the software.

ISO 14229-1 reserves ranges; the ones you will recognise in any trace:

DIDRecord
F186activeDiagnosticSession
F187vehicle manufacturer's spare part number
F18CECU serial number
F190VIN
F191, F193ECU hardware number and version
F188, F195ECU software number and version
0100 to A5FF, FD00 to FEFF and othersfree for the manufacturer and the supplier

ReadDataByIdentifier (0x22)

request    22 F1 90
response   62 F1 90 54 45 31 42 43 4D 34 34 36 52 45 30 30 30 30 34 32

One request may list several DIDs, and the response gives each in turn, its identifier before its data:

request    22 F1 86 01 01
response   62 F1 86 03 01 01 2E E8        session 0x03; battery 0x2EE8 = 12008 mV

The rules that decide the negative responses:

  • The request is the SID and one or more 2-byte identifiers, so its length must be odd and at least 3. Otherwise 13. Servers also cap how many DIDs one request may hold; more is 13 too.
  • A DID that is unknown, or not readable in the active session, is skipped. If none of the requested DIDs can be read, the answer is 31. If at least one can, the response is positive and holds just those.
  • A DID that needs a security level the tester does not have: 33.
  • If the combined response would exceed what the transport can carry: 14, responseTooLong.

That second rule surprises people. It exists so that one tester script can ask a whole family of ECU variants for the same list and take whatever each one has.

WriteDataByIdentifier (0x2E)

request    2E 01 10 3C                    DID 0x0110 = 0x3C
response   6E 01 10

One DID per request. Writing is where an ECU can be damaged, so every check applies: the session (7F), the length for this DID (13), whether the DID is writable at all (31), the security level (33), the vehicle's state (22: not while driving), and the value's range (31). A successful write normally ends in non-volatile memory. Two consequences:

  • The positive response should mean stored, not queued. If the NvM write takes longer than P2, the server sends 78 and answers when the write is verified; if it fails, 72 generalProgrammingFailure.
  • Validate before storing, and store atomically. A write that is interrupted by a power cut must leave either the old value or the new one, never half of each (see the storage lessons: a CRC and two copies).
#include <stdio.h>
#include <stdint.h>
#include <string.h>

/* The server's DID table: where each record is, how long, and what may be done with it. */
typedef struct { uint16_t did; const uint8_t *data; uint8_t len; } did_t;

static const uint8_t session[1] = { 0x03 };
static const uint8_t battery[2] = { 0x2E, 0xE8 };
static const uint8_t serial[4]  = { 'A', '1', '2', '3' };
static const did_t DIDS[] = { { 0xF186, session, 1 }, { 0x0101, battery, 2 }, { 0xF18C, serial, 4 } };

/* Builds the response to a 0x22 request. Returns its length; a negative response is 3 bytes. */
static uint16_t read_data(const uint8_t *req, uint16_t len, uint8_t *resp) {
  if (len < 3 || (len - 1) % 2 != 0) { resp[0] = 0x7F; resp[1] = 0x22; resp[2] = 0x13; return 3; }
  uint16_t at = 1;
  resp[0] = 0x62;
  for (uint16_t i = 1; i < len; i += 2) {
    uint16_t did = (uint16_t)((req[i] << 8) | req[i + 1]);
    for (unsigned k = 0; k < sizeof DIDS / sizeof DIDS[0]; k++) {
      if (DIDS[k].did != did) continue;
      resp[at++] = req[i];
      resp[at++] = req[i + 1];
      memcpy(&resp[at], DIDS[k].data, DIDS[k].len);
      at = (uint16_t)(at + DIDS[k].len);
    }
  }
  if (at == 1) { resp[0] = 0x7F; resp[1] = 0x22; resp[2] = 0x31; return 3; }   /* none of them is known */
  return at;
}

static void ask(const uint8_t *req, uint16_t len) {
  uint8_t resp[32];
  uint16_t n = read_data(req, len, resp);
  for (uint16_t i = 0; i < n; i++) printf("%02X ", resp[i]);
  printf("\n");
}

int main(void) {
  const uint8_t two[] = { 0x22, 0xF1, 0x86, 0x01, 0x01 };
  const uint8_t mixed[] = { 0x22, 0x12, 0x34, 0xF1, 0x8C };     /* one unknown, one known */
  const uint8_t unknown[] = { 0x22, 0x12, 0x34 };
  const uint8_t odd[] = { 0x22, 0xF1, 0x86, 0x01 };
  ask(two, sizeof two);
  ask(mixed, sizeof mixed);
  ask(unknown, sizeof unknown);
  ask(odd, sizeof odd);
  return 0;
}

Diagnostic trouble codes

A DTC identifies one fault the ECU can detect. In UDS it is three bytes:

9A 01 16
|     |
|     +-- failure type byte: 0x16 "circuit voltage below threshold"
+-------- the fault's base code, two bytes

The two-byte base code is the familiar workshop code from SAE J2012. Its top two bits choose the letter and the next two the first digit:

Top bitsLetterSystem
00Ppowertrain
01Cchassis
10Bbody
11Unetwork

So 9A 01 is 10 01 1010 0000 0001: B, 1, A, 0, 1 = B1A01, and with its failure type the tester displays B1A01-16. The failure type byte comes from a standard list (ISO 15031-6): 11 short to ground, 12 short to battery, 13 open circuit, 16 voltage below threshold, 17 voltage above threshold, 87 missing message, and so on. One base code with several failure types lets a single circuit report how it failed.

The status byte

Each DTC has a status byte that the server maintains continuously. This byte is the most asked-about part of UDS in interviews, because reading it correctly is what a diagnostics engineer does all day.

BitMaskNameTrue when
001testFailedthe most recent result of the test was "failed"
102testFailedThisOperationCycleit failed at least once since this ignition cycle began
204pendingDTCit failed in this or the last completed cycle
308confirmedDTCit has failed often enough to be stored as a real fault
410testNotCompletedSinceLastClearthe test has not run to a result since the memory was cleared
520testFailedSinceLastClearit failed at least once since the memory was cleared
640testNotCompletedThisOperationCyclethe test has not run to a result in this cycle
780warningIndicatorRequestedthis DTC is asking for the warning lamp

Reading a few values the way a technician's tool does:

  • 09 (testFailed + confirmed): a stored fault that is present now.
  • 08 (confirmed only): a stored fault that is not present now: intermittent, or already repaired.
  • 50 (both "not completed" bits): nothing known yet; the monitor has not run. After clearing the memory every DTC reads like this until its test runs.
  • 00: tested, passed, never failed since the clear.

A server need not implement all eight bits. It reports which it does in the DTCStatusAvailabilityMask, and a tester ignores the others.

How the bits move over time, with debouncing, operation cycles and ageing, is the subject of The life of a trouble code.

ReadDTCInformation (0x19)

One service with many sub-functions. Four cover most work:

RequestMeaningResponse
19 01 <mask>reportNumberOfDTCByStatusMask59 01 <availability mask> <format> <count, 2 bytes>
19 02 <mask>reportDTCByStatusMask59 02 <availability mask> then <DTC, 3 bytes> <status> for each match
19 04 <DTC> <record>reportDTCSnapshotRecordByDTCNumber59 04 <DTC> <status> then the stored record(s)
19 06 <DTC> <record>reportDTCExtDataRecordByDTCNumber59 06 <DTC> <status> then extended data such as counters

A DTC matches a status mask when (status & mask & availability mask) != 0: any one of the requested bits is enough. 19 02 08 lists confirmed faults; 19 02 09 lists those confirmed or currently failing; 19 02 FF lists every DTC that has any supported bit set.

request    19 02 09
response   59 02 19 9A 01 16 09
                 |  |        |
                 |  |        +-- status: testFailed + confirmedDTC
                 |  +----------- DTC B1A01-16
                 +-------------- this server supports status bits 0x19

A response with no matching DTC is still positive: 59 02 19, an empty list. "No faults" is an answer, not an error.

The format byte in the count response says how the three DTC bytes are coded: 01 is ISO 14229-1, 00 SAE J2012-DA (OBD), 02 SAE J1939-73.

Snapshots and extended data

A code alone rarely explains a fault. Two kinds of record are stored with it:

  • A snapshot (the OBD world says freeze frame) is a set of DIDs captured at a defined moment, usually when the fault is first detected or confirmed: vehicle speed, battery voltage, temperature, odometer. In the response each record is its number, the count of DIDs in it, and each DID with its value.
  • Extended data is information about the fault's history: an occurrence counter, an ageing counter, the cycles since it last failed.

Record number FF means "all records".

ClearDiagnosticInformation (0x14)

request    14 FF FF FF            the group of DTCs: FFFFFF is all of them
response   54

Clearing resets each selected DTC's status to "not tested since clear" and removes its snapshots, extended data and counters. The positive response means the non-volatile memory has been updated, so this is another service that often needs 78. It deliberately needs no security and is available in the default session: a workshop must always be able to clear faults after a repair.

Two more services belong to the picture. ControlDTCSetting (85 02 off, 85 01 on) stops the ECU recording new DTCs, and CommunicationControl (28) silences its normal messages. A tester sends both to every ECU before flashing one of them, so that the others do not fill their memories with "lost communication" faults; the S3 timeout restores both if the tester disappears.

Interview question: Explain the UDS DTC status byte. Which bits do you look at first?

Answer: Each DTC has an 8-bit status. Bit 0, testFailed, is the latest test result. Bit 3, confirmedDTC, means the fault has met its confirmation criteria and is stored. Together they answer the first workshop question: 0x09 is a stored fault that is present now, 0x08 a stored fault that is not present now. Bit 2, pendingDTC, is a failure in the current or previous operation cycle that is not yet confirmed. Bits 4 and 6 say the test has not completed since the last clear or in this cycle, which tells you whether a clean status means "passed" or "not checked yet". Bits 1 and 5 record a failure in this cycle and since the last clear, and bit 7 requests the warning lamp. The server's availability mask says which bits it implements.

Interview question: How does reportDTCByStatusMask decide which DTCs to return, and what does an ECU with no faults answer?

Answer: The request carries a status mask. A DTC is returned when its status byte, ANDed with the mask and with the server's availability mask, is non-zero, so any one requested bit matching is enough. The response is 59 02, the availability mask, then four bytes per matching DTC: the three-byte code and its status. With no matching DTC the server still sends a positive response containing only 59 02 and the availability mask; an empty list is a valid result, not a negative response.

Going deeper

Interview question: A write to a configuration DID must survive a power cut at any moment. How do you implement the service?

Answer: Check everything first, in the standard's order, and reject bad values before touching storage. Then write the new record to non-volatile memory in a way that is atomic: a second copy or a new entry in a log-structured store, with a CRC and a sequence number, so that a cut during the write leaves the old valid copy. Read it back and verify. Send the positive response only after that, using response pending if the write outlasts P2, and send generalProgrammingFailure if verification fails. Update the RAM value that the application uses only once the stored value is known good, so that the ECU never runs with a setting it would lose at the next reset.

Interview question: Why does a tester send ControlDTCSetting off and CommunicationControl before reprogramming an ECU?

Answer: While one ECU is in its bootloader it stops sending its normal messages, and during the download the bus is loaded with transfer frames. Every other ECU that expects those messages would detect a timeout and store "lost communication" trouble codes, which would then have to be cleared from the whole car. The tester therefore sends ControlDTCSetting with DTC setting off to all ECUs so that they stop updating their fault memories, and CommunicationControl to disable normal transmission and leave the bus to the download. Both are restored explicitly afterwards, by a session change, or by the S3 timeout if the tester disappears.

Interview question: What is the difference between a snapshot record and extended data for a DTC?

Answer: A snapshot record captures the environment at a defined moment in the fault's life, typically first detection or confirmation: the values of a configured list of data identifiers such as voltage, speed, temperature and odometer. It answers "what were the conditions when it happened". Extended data describes the fault's history rather than the vehicle's state: an occurrence counter, an ageing counter, the number of cycles since it last failed, sometimes the debounce counter. It answers "how often, and how long ago". They are read with different sub-functions of ReadDTCInformation, 0x04 and 0x06, and both are erased by ClearDiagnosticInformation.

Explain it: In your own words, explain what a tester learns from sending 19 02 09 and receiving 59 02 19 9A 01 16 08.

Answer: The tester asked for every trouble code whose status has testFailed or confirmedDTC set. The response's first data byte, 0x19, is the availability mask: this ECU implements status bits 0, 3 and 4 only. One code follows: 9A 01 16, which is B1A01 with failure type 0x16, a circuit voltage below its threshold. Its status is 0x08: confirmedDTC is set and testFailed is not. So the ECU has a stored under-voltage fault that was real enough to be confirmed, but the most recent test passed: the voltage is fine at the moment, and the fault is intermittent or has been repaired without the memory being cleared.