Simvik

Debounce a fault with a counter

A configurable up/down debounce counter with thresholds and jumps, as AUTOSAR's Dem has. A medium embedded C problem (The life of a trouble code), run and…

medium · The life of a trouble code · dtc · automotive · debounce · diagnostics · autosar

A monitor checks its signal every few milliseconds and reports pre-failed or pre-passed. One bad sample must not store a fault, so a debounce counter decides when the reports add up to a result.

Write debounce_report(d, cfg, prefailed):

  1. Jump: if prefailed and cfg->jump_up and the counter is below 0, set it to 0 first. If pre-passed and cfg->jump_down and the counter is above 0, set it to 0 first.
  2. Step: add increment for pre-failed, subtract decrement for pre-passed. The counter never goes beyond a threshold: clamp it at failed_threshold and passed_threshold.
  3. Qualify: with the counter at failed_threshold the result is failed; at passed_threshold, passed.

Return EVENT_FAILED or EVENT_PASSED only when the qualified result changes: the first time a result is reached, or when it is the other one than last time. Otherwise return EVENT_NO_CHANGE. Keep failed and qualified up to date. d starts zeroed.

Starter code

event_result_t debounce_report(debounce_t *d, const debounce_cfg_t *cfg, bool prefailed) {
  if (prefailed) d->counter += cfg->increment;
  else d->counter -= cfg->decrement;
  if (d->counter >= cfg->failed_threshold) { d->failed = true; return EVENT_FAILED; }
  if (d->counter <= cfg->passed_threshold) { d->failed = false; return EVENT_PASSED; }
  return EVENT_NO_CHANGE;
}

Example tests

Five failing reports make a fault

const debounce_cfg_t cfg = { 5, -5, 1, 1, false, false };
debounce_t d = { 0 };
run(&d, &cfg, "fffffff");

Expected output:

f1. f2. f3. f4. f5F f5. f5.

A glitch is forgotten, and a fault heals

const debounce_cfg_t cfg = { 5, -5, 1, 1, false, false };
debounce_t d = { 0 };
run(&d, &cfg, "ffpppppp");
run(&d, &cfg, "ffffffffffpppppppppp");

Expected output:

f1. f2. p1. p0. p-1. p-2. p-3. p-4.
f-3. f-2. f-1. f0. f1. f2. f3. f4. f5F f5. p4. p3. p2. p1. p0. p-1. p-2. p-3. p-4. p-5P

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