Try Embedded

The life of a trouble code

Monitors, debouncing, the status byte, operation cycles, confirmation, ageing, snapshots and storage.

From a bad sample to a stored fault

Detecting that a voltage is out of range takes one comparison. Deciding that the car has a fault, one worth a warning lamp, a workshop visit and a warranty claim, takes a small state machine per fault, and its rules are written by regulators and car makers. This lesson follows one fault through its whole life:

monitor -> debounce -> test result -> status byte -> confirmation -> storage -> healing -> ageing

In AUTOSAR all of it after the monitor is the Dem module's job. Whether you configure a Dem or write a small fault manager yourself, the concepts and the names are the same.

Monitors and events

A monitor is the code that checks one thing: a sensor's range, a signal's plausibility against another, a message's arrival, an actuator's current. It reports to the fault manager, for one event, a result of passed or failed, or with debouncing delegated, pre-passed or pre-failed.

A monitor is only meaningful when it is allowed to run. Enable conditions switch it off when its result would be wrong for a known reason:

  • No "sensor voltage low" checks while the starter motor pulls the battery down to 6 V.
  • No "message missing" checks for an ECU that is not fitted to this variant, or before the bus has started.
  • No plausibility check of two sensors when one of them already has a fault of its own.

Forgetting an enable condition is the classic source of the DTCs that appear on every cold start and send cars back to the workshop with nothing wrong. When a specification says "DTC shall be set if the voltage is below 9 V", the first review question is "under which conditions is the monitor active?"

Debouncing

A single failed sample proves little: connectors chatter, supply lines glitch, messages are occasionally late. Debouncing requires the failure to persist. Two algorithms are standard.

Counter-based

Each report moves a counter: up by a step for pre-failed, down by a step for pre-passed. Reaching the upper threshold is failed; reaching the lower is passed.

ParameterAUTOSAR's default rangeEffect
failed threshold, passed threshold+127, -128how many net reports decide
increment step, decrement step1 eachasymmetric steps make failing faster than healing, or the reverse
jump down (to a value, usually 0)optionala pre-passed report with the counter above the jump value first drops to it, so healing starts at once
jump upoptionalthe same for a pre-failed report with the counter below

Without a jump, a counter that has sat at -128 for an hour needs 255 failing reports before it trips, and one that has just failed needs 255 passing ones to heal. With jumps to 0 both take 128. Whether you want that depends on the fault: fast detection matters for a safety-relevant signal, slow healing for an intermittent one.

Time-based

A timer starts on pre-failed and the event fails if the condition holds for the whole failed time; a pre-passed report restarts it in the other direction. This suits monitors that report at irregular intervals, where a count of reports means nothing.

Either way the debounce time is a design value with a ceiling. For a safety-relevant fault, detection plus reaction must fit inside the fault tolerant time interval from the safety analysis (ISO 26262): if a stuck brake pressure sensor is dangerous after 100 ms, the debounce cannot be 500 ms, however much it reduces nuisance faults.

#include <stdio.h>
#include <stdint.h>

#define FAILED  3      /* small thresholds, to keep the trace short */
#define PASSED -3

typedef struct { int8_t counter; int8_t result; } debounce_t;   /* result: +1 failed, -1 passed, 0 undecided */

/* One report from the monitor. With jump set, a report against the trend first returns the counter to 0. */
static void report(debounce_t *d, int failing, int jump) {
  if (failing) {
    if (jump && d->counter < 0) d->counter = 0;
    if (d->counter < FAILED) d->counter++;
    if (d->counter == FAILED) d->result = 1;
  } else {
    if (jump && d->counter > 0) d->counter = 0;
    if (d->counter > PASSED) d->counter--;
    if (d->counter == PASSED) d->result = -1;
  }
}

static void run(const char *samples, int jump) {
  debounce_t d = { 0, 0 };
  printf("%s jump: ", jump ? "with   " : "without");
  for (const char *s = samples; *s; s++) {
    report(&d, *s == 'F', jump);
    printf("%c%+d%s ", *s, d.counter, d.result > 0 ? "!" : "");
  }
  printf("\n");
}

int main(void) {
  /* P = a passing sample, F = a failing one; "!" marks samples while the result is "failed". */
  const char *samples = "PPPPFFFFFFPPPP";
  run(samples, 0);
  run(samples, 1);
  return 0;
}

Read the two lines of output side by side: after four passes, the fault is found on the sixth failing sample without the jump and on the third with it.

The status byte through an operation cycle

The status bits were listed in the UDS lesson. Here is how each moves. An operation cycle is the period the fault's bookkeeping is counted in, most often an ignition cycle (other kinds: a power cycle, a warm-up cycle, an OBD driving cycle).

EventBits setBits cleared
operation cycle starts6 testNotCompletedThisOperationCycle1 testFailedThisOperationCycle
test result: failed0 testFailed, 1 testFailedThisOperationCycle, 2 pendingDTC, 5 testFailedSinceLastClear4 testNotCompletedSinceLastClear, 6 testNotCompletedThisOperationCycle
test result: passed0 testFailed, 4, 6
operation cycle ends with the test completed and not failed in it2 pendingDTC
confirmation criteria met3 confirmedDTC
aged out3 confirmedDTC (and the entry leaves the memory)
cleared by the tester4, 6all the others

Notice which bits remember and which forget. testFailed follows the latest result. pendingDTC remembers for one cycle. confirmedDTC remembers until ageing or a clear. testFailedSinceLastClear remembers until a clear and nothing else, which makes it the bit that tells a workshop "this did happen at some point" even after ageing has removed the entry.

Pending and confirmed

Confirmation is the step from "it failed" to "it is a fault". The criterion is a trip counter: the number of operation cycles in which the test failed.

  • Many body and comfort faults confirm on the first failure (threshold 1): the lamp is not regulated, and the workshop wants every occurrence.
  • Emission-relevant OBD faults typically need failures in two consecutive driving cycles. The first sets pending; if the next cycle passes, pending is cleared and nothing is stored; if it fails again, the DTC is confirmed and the malfunction indicator lamp comes on. This is the legislated "two-trip" logic.

warningIndicatorRequested (bit 7) follows its own rule, usually set on confirmation and cleared after a number of fault-free cycles (three, for the OBD lamp), which is fewer than ageing needs: the lamp goes out long before the entry is erased.

Healing and ageing

A fault that stops occurring goes through two stages.

Healing is the test passing again: testFailed clears, and after enough passing cycles the warning lamp request clears. The entry stays in memory with confirmedDTC set: this is the status 0x08 or 0x28 that a technician reads as "stored, not present".

Ageing removes it. An ageing counter counts operation cycles (for OBD, warm-up cycles) in which the test completed and did not fail. At the threshold, commonly 40, the DTC is aged: confirmedDTC clears, and its snapshots and extended data are freed. A failure at any time before that resets the counter. Whether an aged DTC leaves any trace (the testFailedSinceLastClear bit, an "aged" counter) is the manufacturer's choice.

Ageing exists because fault memory is small and history goes stale: an under-voltage event from the day the battery was changed should not be on the screen two years later.

Where the fault is stored

The primary event memory holds a fixed number of entries, often 10 to 40, far fewer than the hundreds of DTCs an ECU defines. Each entry is the DTC, its status and counters, and its records. Three policies have to be decided:

  • When to capture the snapshot. On the first failed, on pending, or on confirmed; and whether a later occurrence overwrites it. First-occurrence data shows how the fault began; latest-occurrence data shows its current form. Some memories keep both, as two record numbers.
  • What to do when the memory is full: displacement. A new fault may evict an entry by priority (a configured rank per DTC), then by status (an aged or merely pending entry before a confirmed, active one), then by age. If nothing can be displaced, the new fault is not stored, and an "overflow" flag tells the workshop that the list is incomplete.
  • When to write to non-volatile memory. Writing only at shutdown is gentle on flash but loses everything if power is cut, and a crash or a failing supply is precisely when faults are logged. Writing immediately on confirmation is safe but must be rate-limited, or a chattering fault wears the flash out. The usual compromise: immediate storage for the first few occurrences of each DTC, the rest at shutdown.

Pitfall: Under-voltage is both a fault to record and a reason not to write flash. An ECU that responds to a collapsing supply by starting a non-volatile write is likely to corrupt the block it is writing. Fault storage needs the same brown-out discipline as any other write: enough hold-up energy for the write, or redundancy and a CRC so that an interrupted one is detected and the old copy used.

Designing a monitor: a worked example

Suppose the requirement says: report a fault when the battery voltage is below 9.0 V. What a reviewer expects the design to answer:

QuestionA reasonable answer
Sample rateevery 10 ms, from the filtered ADC value
Enable conditionsignition on for at least 500 ms; not while cranking; ADC self-test passed
Debouncecounter, +1 / -1, thresholds +20 / -20: 200 ms to fail from neutral
Hysteresisfail below 9.0 V, pass above 9.5 V, neither in between, so that a voltage sitting at the limit does not toggle
Confirmationfirst trip
Snapshoton confirmation: battery voltage, ignition state, odometer, time since start
Reactionswitch off comfort loads; no lamp
Healing and ageingpass after 200 ms above 9.5 V; aged after 40 fault-free ignition cycles
Storageimmediate, at most three times per DTC per ignition cycle

The hysteresis row is the one most often missing. A debounce counter alone does not stop a value hovering at the threshold from spending its time half-way to failing; a band in which the monitor reports nothing does.

Interview question: What is the difference between a pending and a confirmed DTC?

Answer: Pending means the test failed in the current or the last completed operation cycle; it is a short memory, cleared when a full cycle passes without a failure. Confirmed means the fault has met its confirmation criteria, typically a trip counter: one failing cycle for many body faults, two consecutive failing driving cycles for emission-relevant OBD faults. Confirmation is what stores the fault durably, usually with its snapshot, and for regulated faults switches on the warning lamp. Confirmed stays set until the DTC is aged out after a number of fault-free cycles, often 40, or the memory is cleared by a tester.

Interview question: Explain counter-based debouncing as AUTOSAR's Dem does it.

Answer: Each diagnostic event has a signed debounce counter. A monitor reports pre-failed or pre-passed, and the counter goes up by a configured increment or down by a configured decrement. When it reaches the failed threshold, by default +127, the event is qualified as failed; when it reaches the passed threshold, by default -128, it is qualified as passed. Optional jump-down and jump-up settings move the counter to a configured value, usually zero, when a report arrives against the current trend, so that healing or detection does not have to work back through the whole range. The steps and thresholds set the detection and healing times, which must respect the fault tolerant time of safety-relevant faults.

Going deeper

Interview question: A fleet shows a DTC for a missing CAN message on almost every cold start, with no real fault. How do you find and fix the cause?

Answer: Suspect the monitor's enable conditions before the hardware. Read the snapshot: time since start-up, supply voltage and network state at the moment of failure. A timeout monitor that starts with the ECU fails when the sender boots more slowly, especially when cold and the voltage is low during cranking. The fix is to enable the monitor only after the network has been up for the sender's worst-case start-up time, to suspend it during cranking and under-voltage, and to keep the debounce time longer than the message's allowed jitter. Then check the same mistake in sibling monitors, and add a cold, low-voltage start to the test plan, since the bench supply never reproduced it.

Interview question: The event memory has 20 entries and a 21st fault occurs. What should happen?

Answer: Displacement rules decide. The usual order is by configured priority first, so a safety-relevant fault can evict a comfort one; among equal priorities, entries that are not active are displaced before active ones, by status such as aged, pending or not confirmed; and then the oldest by occurrence order. The evicted DTC's status bits that live outside the entry, such as testFailedSinceLastClear, may remain so that a trace survives. If nothing in the memory ranks lower than the new fault, it is not stored, and an overflow indication is set so that the workshop knows the memory is not the complete story.

Interview question: Why should a range-check monitor have hysteresis as well as debouncing?

Answer: Debouncing filters short disturbances by requiring a result to persist, but it treats a value just below the limit and one just above as opposite votes. A signal that sits at the threshold with a little noise produces a stream of alternating pre-failed and pre-passed reports: the counter wanders, the detection time becomes unpredictable, and the status can toggle between failed and passed, filling the memory's occurrence counters. Hysteresis gives separate limits for failing and passing with a band between them in which the monitor reports nothing, so the result changes only when the signal has clearly moved.

Explain it: In your own words, explain what happens to a confirmed DTC after the fault is repaired but nobody clears the memory.

Answer: At the next test the result is passed, so testFailed clears; the entry remains with confirmedDTC set, which a tester shows as stored but not present. If the DTC drives a warning lamp, the request clears after a few fault-free cycles. Each operation cycle in which the test completes without failing increments an ageing counter. When that reaches its threshold, commonly 40 cycles, the DTC is aged out: confirmedDTC clears and the entry with its snapshot and counters is released. If the fault returns at any point before that, the ageing counter starts again from zero.