Simvik

A trouble code through its operation cycles

Maintain the UDS status byte: pending, confirmed after two failing cycles, and aged out after three clean ones. A hard embedded C problem (The life of a…

hard · The life of a trouble code · dtc · automotive · diagnostics · state-machine · bits

Write the four functions that keep one trouble code's status byte and counters. (Bit 7, the warning lamp, is not used here.)

dtc_clear(d): a tester cleared the memory. Status = NOT_COMPLETED_SINCE_CLEAR | NOT_COMPLETED_THIS_CYCLE; both counters 0.

dtc_cycle_start(d): ignition on. Clear TEST_FAILED_THIS_CYCLE; set NOT_COMPLETED_THIS_CYCLE.

dtc_result(d, failed): the debounced test produced a result. Either way, clear both "not completed" bits.

  • failed: set TEST_FAILED, TEST_FAILED_THIS_CYCLE, PENDING and FAILED_SINCE_CLEAR. If this is the first failure of this cycle, add one trip; when the trips reach TRIPS_TO_CONFIRM, set CONFIRMED. Any failure resets the ageing counter.
  • passed: clear TEST_FAILED.

dtc_cycle_end(d): ignition off. Only if the test completed in this cycle and did not fail in it:

  • clear PENDING and reset the trips to 0 (the failures were not consecutive);
  • if the DTC is CONFIRMED, add one to the ageing counter, and when it reaches CYCLES_TO_AGE, clear CONFIRMED and reset the counter.

A cycle in which the test did not complete changes nothing at its end.

Starter code

void dtc_clear(dtc_t *d) {
  d->status = 0;
  d->trips = 0;
  d->ageing = 0;
}

void dtc_cycle_start(dtc_t *d) {
  d->status &= (uint8_t)~TEST_FAILED_THIS_CYCLE;
}

void dtc_result(dtc_t *d, bool failed) {
  if (failed) {
    d->status |= TEST_FAILED | TEST_FAILED_THIS_CYCLE | PENDING | CONFIRMED;
    d->trips++;
  } else {
    d->status &= (uint8_t)~TEST_FAILED;
  }
}

void dtc_cycle_end(dtc_t *d) {
  d->status &= (uint8_t)~PENDING;
}

Example tests

After a clear, and a first clean cycle

dtc_clear(&dtc);
show("cleared");
cycle("P");

Expected output:

cleared                status 50 trips 0 ageing 0
P                      status 00 trips 0 ageing 0

Two failing cycles confirm it

dtc_clear(&dtc);
cycle("PF");
cycle("F");

Expected output:

PF                     status 27 trips 1 ageing 0
F                      status 2F trips 2 ageing 0

Learn it first

  • The life of a trouble code · Monitors, debouncing, the status byte, operation cycles, confirmation, ageing, snapshots and storage.

More The life of a trouble code problems