Try Embedded

UDS: read the VIN, live data and fault codes

A tester asks an ECU for its VIN, a live coolant temperature and its fault codes with UDS services 0x22, 0x19 and 0x14, over ISO-TP on CAN.

Communication · Advanced

In a real product: Workshop diagnostic scan tool (Automotive): A scan tool reads a car's identity, live values and stored fault codes, and clears them after the repair.

UDS (ISO 14229, Unified Diagnostic Services) is the language a workshop tester speaks to every ECU in a car. Each request starts with a service id; a positive response is that id plus 0x40, and a refusal is 7F, the service and a negative response code (NRC) that says why.

The tester on the left runs a short diagnostic session against the ECU on the right. The ECU reads a "coolant temperature" from the potentiometer on its A0, sets fault codes from it, and lights LD2 as the malfunction indicator lamp (MIL) while a fault asks for it. Messages travel with ISO-TP on CAN ids 0x7E0 and 0x7E8, so the 20-byte VIN reply goes out as a first frame and consecutive frames.

What you'll learn

  • Read Data By Identifier (22): a 2-byte DID in, its value out; F190 is the VIN in every car
  • Read DTC Information (19): sub-function 01 counts fault codes, 02 lists them with their status bytes
  • Clear Diagnostic Information (14 FF FF FF) and why a fault that is still present comes straight back
  • The DTC status byte (test failed, pending, confirmed, warning lamp) and the 3-byte code behind "P0301"
  • Negative responses: 7F 22 31 (request out of range), 13 (wrong length), 11 (service not supported)

How it works

The tester sends six requests once, then reads the temperature and the number of faults every second:

RequestMeaningECU's answer
22 F1 90read the VIN62 F1 90 + 17 ASCII characters
22 01 05read coolant temperature62 01 05 + one byte, the temperature + 40
19 02 FFlist faults, any status59 02 8D + 4 bytes per fault
22 12 34read a DID that does not exist7F 22 31
14 FF FF FFclear every fault54
19 01 FFcount faults59 01 8D 01 + a 2-byte count

A fault record is 3 bytes of code and a status byte. The top two bits of the first byte give the letter (P powertrain, C chassis, B body, U network), and the rest are the four digits: 03 01 00 is P0301, a cylinder 1 misfire. The third byte (the failure type) is 00 here.

The status bits follow the standard. Test failed (bit 0) means the fault is present now. Pending (bit 2) means it failed this drive cycle. Confirmed (bit 3) means it was seen often enough to be stored. Warning indicator (bit 7) means the MIL should be lit. 8D, the availability mask in each reply, says that this ECU supports exactly those four bits.

The ECU starts with P0301 confirmed (MIL on) and P0420 pending. It watches the temperature itself: at 110 °C or more it sets P0217, engine overheating, with test failed, pending, confirmed and the lamp. When the temperature drops, test failed and the lamp clear, but the fault stays confirmed until a tester clears it. That is why a mechanic can still read a fault that happened yesterday.

Try this

  • Turn the potentiometer past three quarters (about 110 °C): LD2 on the ECU lights and the fault count goes to 1.
  • Turn it back down and watch the count stay at 1: P0217 stays confirmed until you clear it.
  • Send 22 F1 90 00 (one byte too many) from the tester and get 7F 22 13.
  • Add a DID F18C (ECU serial number) to the ECU's read_data() and read it from the tester.

main.c

// Bare-metal C on the Nucleo-F446RE: no Arduino core and no HAL. CMSIS names every register.
// The TESTER: UDS (ISO 14229) requests to the ECU over ISO-TP on CAN: VIN, live data, fault codes, clearing them.
#include <stdio.h>
#include <string.h>
#include "stm32f4xx.h"

#define TX_ID 0x7E0   // physical request: tester -> ECU
#define RX_ID 0x7E8   // response: ECU -> tester

static volatile uint32_t ms;
void SysTick_Handler(void) { ms++; }
static void delay_ms(uint32_t t) { uint32_t s = ms; while (ms - s < t) {} }

int __io_putchar(int ch) {
  while (!(USART2->SR & USART_SR_TXE)) {}
  USART2->DR = ch;
  return ch;
}

// ---- CAN: bxCAN CAN1 on PA11 (RX) and PA12 (TX), 500 kbit/s, polled -------------------------------------------
static int can_init(void) {
  CAN1->MCR = CAN_MCR_INRQ;
  while (!(CAN1->MSR & CAN_MSR_INAK)) {}
  CAN1->BTR = (1u << CAN_BTR_TS2_Pos) | (12u << CAN_BTR_TS1_Pos) | (1u << CAN_BTR_BRP_Pos);   // 16 MHz / 32
  CAN1->FMR = (14u << CAN_FMR_CAN2SB_Pos) | CAN_FMR_FINIT;
  CAN1->FA1R &= ~1u;
  CAN1->FS1R |= 1u;
  CAN1->FM1R |= 1u;                                          // list mode: only RX_ID passes
  CAN1->FFA1R &= ~1u;
  CAN1->sFilterRegister[0].FR1 = RX_ID << 21;
  CAN1->sFilterRegister[0].FR2 = RX_ID << 21;
  CAN1->FA1R |= 1u;
  CAN1->FMR &= ~CAN_FMR_FINIT;
  CAN1->MCR &= ~CAN_MCR_INRQ;
  uint32_t start = ms;
  while (CAN1->MSR & CAN_MSR_INAK) if (ms - start > 50) return 0;
  return 1;
}

static int can_send(const uint8_t *data, int len) {          // 8 bytes, padded with AA
  uint8_t f[8];
  memset(f, 0xAA, 8);
  memcpy(f, data, len);
  uint32_t start = ms;
  while (!(CAN1->TSR & CAN_TSR_TME0)) {
    if (ms - start > 10) { CAN1->TSR = CAN_TSR_ABRQ0; return 0; }
  }
  CAN_TxMailBox_TypeDef *box = &CAN1->sTxMailBox[0];
  box->TIR = TX_ID << CAN_TI0R_STID_Pos;
  box->TDTR = 8;
  box->TDLR = f[0] | f[1] << 8 | f[2] << 16 | (uint32_t)f[3] << 24;
  box->TDHR = f[4] | f[5] << 8 | f[6] << 16 | (uint32_t)f[7] << 24;
  box->TIR |= CAN_TI0R_TXRQ;
  return 1;
}

static int can_receive(uint8_t *f, uint32_t timeout) {
  uint32_t start = ms;
  while (!(CAN1->RF0R & CAN_RF0R_FMP0)) if (ms - start >= timeout) return 0;
  uint32_t lo = CAN1->sFIFOMailBox[0].RDLR, hi = CAN1->sFIFOMailBox[0].RDHR;
  for (int i = 0; i < 4; i++) { f[i] = lo >> (8 * i); f[4 + i] = hi >> (8 * i); }
  CAN1->RF0R = CAN_RF0R_RFOM0;
  return 1;
}

// ---- ISO-TP (ISO 15765-2): one message each way, as in the ISO-TP project ---------------------------------------
static int isotp_send(const uint8_t *msg, uint16_t len) {
  uint8_t f[8];
  if (len <= 7) { f[0] = len; memcpy(f + 1, msg, len); return can_send(f, len + 1); }
  f[0] = 0x10 | (len >> 8);
  f[1] = len & 0xFF;
  memcpy(f + 2, msg, 6);
  if (!can_send(f, 8)) return 0;
  uint16_t sent = 6;
  uint8_t sn = 1;
  while (sent < len) {
    uint8_t fc[8];
    do { if (!can_receive(fc, 1000)) return 0; } while ((fc[0] & 0xF0) != 0x30 || (fc[0] & 0x0F) == 1);
    if ((fc[0] & 0x0F) != 0) return 0;                       // overflow
    uint32_t stmin = fc[2] <= 0x7F ? fc[2] : 1;
    for (uint8_t n = 0; sent < len && (fc[1] == 0 || n < fc[1]); n++) {
      if (n) delay_ms(stmin);
      uint16_t chunk = len - sent > 7 ? 7 : len - sent;
      f[0] = 0x20 | (sn++ & 0x0F);
      memcpy(f + 1, msg + sent, chunk);
      if (!can_send(f, chunk + 1)) return 0;
      sent += chunk;
    }
  }
  return 1;
}

// Waits up to timeout ms for a message; returns its length, or -1.
static int isotp_receive(uint8_t *buf, uint16_t cap, uint32_t timeout) {
  uint8_t f[8];
  uint32_t start = ms;
  for (;;) {
    uint32_t spent = ms - start;
    if (spent >= timeout || !can_receive(f, timeout - spent)) return -1;
    if ((f[0] >> 4) == 0 && (f[0] & 0x0F) && (f[0] & 0x0F) <= 7) {
      memcpy(buf, f + 1, f[0] & 0x0F);
      return f[0] & 0x0F;
    }
    if ((f[0] >> 4) != 1) continue;
    uint16_t len = (f[0] & 0x0F) << 8 | f[1];
    uint8_t fc[3] = { len > cap ? 0x32 : 0x30, 0, 0 };       // block size 0: send it all
    can_send(fc, 3);
    if (len > cap) return -1;
    memcpy(buf, f + 2, 6);
    uint16_t got = 6;
    uint8_t sn = 1;
    while (got < len) {
      if (!can_receive(f, 1000) || (f[0] >> 4) != 2 || (f[0] & 0x0F) != (sn++ & 0x0F)) return -1;
      uint16_t chunk = len - got > 7 ? 7 : len - got;
      memcpy(buf + got, f + 1, chunk);
      got += chunk;
    }
    return len;
  }
}

static void setup_board(void) {
  SysTick_Config(SystemCoreClock / 1000);
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
  RCC->APB1ENR |= RCC_APB1ENR_USART2EN | RCC_APB1ENR_CAN1EN;
  // PA2, PA3: AF7 (USART2); PA11, PA12: AF9 (CAN1); PA5 (LD2) an output.
  GPIOA->MODER = (GPIOA->MODER & ~(0xFu << 4 | 0xFu << 22 | 3u << 10)) | (0xAu << 4) | (0xAu << 22) | (1u << 10);
  GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFFu << 8)) | (0x77u << 8);
  GPIOA->AFR[1] = (GPIOA->AFR[1] & ~(0xFFu << 12)) | (0x99u << 12);
  USART2->BRR = SystemCoreClock / 115200;
  USART2->CR1 = USART_CR1_TE | USART_CR1_UE;
}


static uint8_t resp[64];

static const char *nrc_name(uint8_t nrc) {
  switch (nrc) {
    case 0x11: return "service not supported";
    case 0x12: return "sub-function not supported";
    case 0x13: return "incorrect message length";
    case 0x22: return "conditions not correct";
    case 0x31: return "request out of range";
    case 0x33: return "security access denied";
    default:   return "other";
  }
}

// Sends a request and waits for its response (P2, 50 ms here, with room to spare); returns its length, or -1.
static int request(const uint8_t *req, uint16_t len) {
  printf("->");
  for (int i = 0; i < len; i++) printf(" %02X", req[i]);
  printf("\r\n");
  if (!isotp_send(req, len)) { printf("   not sent\r\n"); return -1; }
  int n = isotp_receive(resp, sizeof resp, 200);
  if (n < 0) { printf("   no response\r\n"); return -1; }
  printf("<-");
  for (int i = 0; i < n && i < 12; i++) printf(" %02X", resp[i]);
  printf(n > 12 ? " ...\r\n" : "\r\n");
  if (resp[0] == 0x7F && n >= 3) printf("   refused: %s\r\n", nrc_name(resp[2]));
  return n;
}

// "P0301 confirmed lamp" from a 4-byte record.
static void print_dtc(const uint8_t *r) {
  printf("   %c%d%X%X%X", "PCBU"[r[0] >> 6], (r[0] >> 4) & 3, r[0] & 15, r[1] >> 4, r[1] & 15);
  if (r[3] & 0x01) printf(" test-failed");
  if (r[3] & 0x04) printf(" pending");
  if (r[3] & 0x08) printf(" confirmed");
  if (r[3] & 0x80) printf(" lamp");
  printf("\r\n");
}

static void read_dtcs(void) {
  static const uint8_t req[] = { 0x19, 0x02, 0xFF };
  int n = request(req, sizeof req);
  if (n >= 3 && resp[0] == 0x59) {
    if (n == 3) printf("   no faults stored\r\n");
    for (int i = 3; i + 4 <= n; i += 4) print_dtc(resp + i);
  }
}

int main(void) {
  setup_board();
  printf("Tester: %s\r\n", can_init() ? "on the bus" : "no CAN bus");
  delay_ms(20);

  static const uint8_t vin[] = { 0x22, 0xF1, 0x90 }, temp[] = { 0x22, 0x01, 0x05 }, bad[] = { 0x22, 0x12, 0x34 };
  static const uint8_t clear[] = { 0x14, 0xFF, 0xFF, 0xFF }, count[] = { 0x19, 0x01, 0xFF };
  int n;

  printf("\r\n1. Read the VIN\r\n");
  n = request(vin, sizeof vin);
  if (n == 20 && resp[0] == 0x62) printf("   VIN %.17s\r\n", (char *)resp + 3);

  printf("\r\n2. Read the coolant temperature\r\n");
  n = request(temp, sizeof temp);
  if (n == 4 && resp[0] == 0x62) printf("   %d C\r\n", resp[3] - 40);

  printf("\r\n3. Read the fault codes\r\n");
  read_dtcs();

  printf("\r\n4. Read a DID the ECU does not have\r\n");
  request(bad, sizeof bad);

  printf("\r\n5. Clear the fault codes\r\n");
  n = request(clear, sizeof clear);
  if (n == 1 && resp[0] == 0x54) printf("   cleared\r\n");

  printf("\r\n6. Read them again\r\n");
  read_dtcs();

  for (;;) {
    delay_ms(1000);
    printf("\r\n");
    n = request(temp, sizeof temp);
    int c = n == 4 && resp[0] == 0x62 ? resp[3] - 40 : -999;
    n = request(count, sizeof count);
    if (n == 6 && resp[0] == 0x59 && c != -999) printf("   %d C, %d fault(s) stored\r\n", c, resp[4] << 8 | resp[5]);
  }
}

ecu.c

// Bare-metal C on the Nucleo-F446RE: no Arduino core and no HAL. CMSIS names every register.
// The ECU: answers UDS services 22 (read data), 19 (read fault codes) and 14 (clear them) over ISO-TP on CAN.
// A potentiometer on A0 (PA0) is the coolant temperature; LD2 (PA5) is the malfunction indicator lamp (MIL).
#include <stdio.h>
#include <string.h>
#include "stm32f4xx.h"

#define TX_ID 0x7E8
#define RX_ID 0x7E0
#define OVERHEAT_C 110

static volatile uint32_t ms;
void SysTick_Handler(void) { ms++; }
static void delay_ms(uint32_t t) { uint32_t s = ms; while (ms - s < t) {} }

int __io_putchar(int ch) {
  while (!(USART2->SR & USART_SR_TXE)) {}
  USART2->DR = ch;
  return ch;
}

// ---- CAN: bxCAN CAN1 on PA11 (RX) and PA12 (TX), 500 kbit/s, polled -------------------------------------------
static int can_init(void) {
  CAN1->MCR = CAN_MCR_INRQ;
  while (!(CAN1->MSR & CAN_MSR_INAK)) {}
  CAN1->BTR = (1u << CAN_BTR_TS2_Pos) | (12u << CAN_BTR_TS1_Pos) | (1u << CAN_BTR_BRP_Pos);   // 16 MHz / 32
  CAN1->FMR = (14u << CAN_FMR_CAN2SB_Pos) | CAN_FMR_FINIT;
  CAN1->FA1R &= ~1u;
  CAN1->FS1R |= 1u;
  CAN1->FM1R |= 1u;                                          // list mode: only RX_ID passes
  CAN1->FFA1R &= ~1u;
  CAN1->sFilterRegister[0].FR1 = RX_ID << 21;
  CAN1->sFilterRegister[0].FR2 = RX_ID << 21;
  CAN1->FA1R |= 1u;
  CAN1->FMR &= ~CAN_FMR_FINIT;
  CAN1->MCR &= ~CAN_MCR_INRQ;
  uint32_t start = ms;
  while (CAN1->MSR & CAN_MSR_INAK) if (ms - start > 50) return 0;
  return 1;
}

static int can_send(const uint8_t *data, int len) {          // 8 bytes, padded with AA
  uint8_t f[8];
  memset(f, 0xAA, 8);
  memcpy(f, data, len);
  uint32_t start = ms;
  while (!(CAN1->TSR & CAN_TSR_TME0)) {
    if (ms - start > 10) { CAN1->TSR = CAN_TSR_ABRQ0; return 0; }
  }
  CAN_TxMailBox_TypeDef *box = &CAN1->sTxMailBox[0];
  box->TIR = TX_ID << CAN_TI0R_STID_Pos;
  box->TDTR = 8;
  box->TDLR = f[0] | f[1] << 8 | f[2] << 16 | (uint32_t)f[3] << 24;
  box->TDHR = f[4] | f[5] << 8 | f[6] << 16 | (uint32_t)f[7] << 24;
  box->TIR |= CAN_TI0R_TXRQ;
  return 1;
}

static int can_receive(uint8_t *f, uint32_t timeout) {
  uint32_t start = ms;
  while (!(CAN1->RF0R & CAN_RF0R_FMP0)) if (ms - start >= timeout) return 0;
  uint32_t lo = CAN1->sFIFOMailBox[0].RDLR, hi = CAN1->sFIFOMailBox[0].RDHR;
  for (int i = 0; i < 4; i++) { f[i] = lo >> (8 * i); f[4 + i] = hi >> (8 * i); }
  CAN1->RF0R = CAN_RF0R_RFOM0;
  return 1;
}

// ---- ISO-TP (ISO 15765-2): one message each way, as in the ISO-TP project ---------------------------------------
static int isotp_send(const uint8_t *msg, uint16_t len) {
  uint8_t f[8];
  if (len <= 7) { f[0] = len; memcpy(f + 1, msg, len); return can_send(f, len + 1); }
  f[0] = 0x10 | (len >> 8);
  f[1] = len & 0xFF;
  memcpy(f + 2, msg, 6);
  if (!can_send(f, 8)) return 0;
  uint16_t sent = 6;
  uint8_t sn = 1;
  while (sent < len) {
    uint8_t fc[8];
    do { if (!can_receive(fc, 1000)) return 0; } while ((fc[0] & 0xF0) != 0x30 || (fc[0] & 0x0F) == 1);
    if ((fc[0] & 0x0F) != 0) return 0;                       // overflow
    uint32_t stmin = fc[2] <= 0x7F ? fc[2] : 1;
    for (uint8_t n = 0; sent < len && (fc[1] == 0 || n < fc[1]); n++) {
      if (n) delay_ms(stmin);
      uint16_t chunk = len - sent > 7 ? 7 : len - sent;
      f[0] = 0x20 | (sn++ & 0x0F);
      memcpy(f + 1, msg + sent, chunk);
      if (!can_send(f, chunk + 1)) return 0;
      sent += chunk;
    }
  }
  return 1;
}

// Waits up to timeout ms for a message; returns its length, or -1.
static int isotp_receive(uint8_t *buf, uint16_t cap, uint32_t timeout) {
  uint8_t f[8];
  uint32_t start = ms;
  for (;;) {
    uint32_t spent = ms - start;
    if (spent >= timeout || !can_receive(f, timeout - spent)) return -1;
    if ((f[0] >> 4) == 0 && (f[0] & 0x0F) && (f[0] & 0x0F) <= 7) {
      memcpy(buf, f + 1, f[0] & 0x0F);
      return f[0] & 0x0F;
    }
    if ((f[0] >> 4) != 1) continue;
    uint16_t len = (f[0] & 0x0F) << 8 | f[1];
    uint8_t fc[3] = { len > cap ? 0x32 : 0x30, 0, 0 };       // block size 0: send it all
    can_send(fc, 3);
    if (len > cap) return -1;
    memcpy(buf, f + 2, 6);
    uint16_t got = 6;
    uint8_t sn = 1;
    while (got < len) {
      if (!can_receive(f, 1000) || (f[0] >> 4) != 2 || (f[0] & 0x0F) != (sn++ & 0x0F)) return -1;
      uint16_t chunk = len - got > 7 ? 7 : len - got;
      memcpy(buf + got, f + 1, chunk);
      got += chunk;
    }
    return len;
  }
}

static void setup_board(void) {
  SysTick_Config(SystemCoreClock / 1000);
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
  RCC->APB1ENR |= RCC_APB1ENR_USART2EN | RCC_APB1ENR_CAN1EN;
  // PA2, PA3: AF7 (USART2); PA11, PA12: AF9 (CAN1); PA5 (LD2) an output.
  GPIOA->MODER = (GPIOA->MODER & ~(0xFu << 4 | 0xFu << 22 | 3u << 10)) | (0xAu << 4) | (0xAu << 22) | (1u << 10);
  GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFFu << 8)) | (0x77u << 8);
  GPIOA->AFR[1] = (GPIOA->AFR[1] & ~(0xFFu << 12)) | (0x99u << 12);
  USART2->BRR = SystemCoreClock / 115200;
  USART2->CR1 = USART_CR1_TE | USART_CR1_UE;
}


// The DTC status bits (ISO 14229-1 D.2), and the ones this ECU supports: its availability mask.
#define TEST_FAILED 0x01
#define PENDING     0x04
#define CONFIRMED   0x08
#define LAMP        0x80
#define AVAILABLE   (TEST_FAILED | PENDING | CONFIRMED | LAMP)

typedef struct { uint8_t code[3]; uint8_t status; } Dtc;
static Dtc dtcs[] = {
  { { 0x03, 0x01, 0x00 }, CONFIRMED | LAMP },     // P0301 cylinder 1 misfire, stored on an earlier drive
  { { 0x04, 0x20, 0x00 }, PENDING },              // P0420 catalyst efficiency, seen once
  { { 0x02, 0x17, 0x00 }, 0 },                    // P0217 engine overheating, watched live below
};
#define NDTC (int)(sizeof dtcs / sizeof dtcs[0])
static const char VIN[17] = "1TE7A2B30MC000042";

static int coolant_c(void) {                       // 0-3.3 V on A0 as -40 to 150 C
  ADC1->CR2 |= ADC_CR2_SWSTART;
  while (!(ADC1->SR & ADC_SR_EOC)) {}
  return (int)(ADC1->DR * 190u / 4095u) - 40;
}

// The overheating monitor: runs all the time, as an ECU's monitors do.
static void monitor(void) {
  Dtc *d = &dtcs[2];
  if (coolant_c() >= OVERHEAT_C) d->status |= TEST_FAILED | PENDING | CONFIRMED | LAMP;
  else d->status &= ~(TEST_FAILED | LAMP);        // stays confirmed until a tester clears it
  int lamp = 0;
  for (int i = 0; i < NDTC; i++) lamp |= dtcs[i].status & LAMP;
  if (lamp) GPIOA->BSRR = GPIO_BSRR_BS5; else GPIOA->BSRR = GPIO_BSRR_BR5;
}

static uint8_t req[32], res[64];

static int negative(uint8_t nrc) { res[0] = 0x7F; res[1] = req[0]; res[2] = nrc; return 3; }

static int read_data(int n) {
  if (n != 3) return negative(0x13);
  uint16_t did = req[1] << 8 | req[2];
  res[0] = 0x62; res[1] = req[1]; res[2] = req[2];
  if (did == 0xF190) { memcpy(res + 3, VIN, 17); return 20; }
  if (did == 0x0105) { res[3] = coolant_c() + 40; return 4; }
  return negative(0x31);
}

static int read_dtc_information(int n) {
  if (n < 2) return negative(0x13);
  if (req[1] != 0x01 && req[1] != 0x02) return negative(0x12);
  if (n != 3) return negative(0x13);
  uint8_t mask = req[2] & AVAILABLE;
  res[0] = 0x59; res[1] = req[1]; res[2] = AVAILABLE;
  int len = 3, count = 0;
  for (int i = 0; i < NDTC; i++) {
    if (!(dtcs[i].status & mask)) continue;
    count++;
    if (req[1] == 0x02) { memcpy(res + len, dtcs[i].code, 3); res[len + 3] = dtcs[i].status; len += 4; }
  }
  if (req[1] == 0x01) { res[3] = 0x01; res[4] = count >> 8; res[5] = count; len = 6; }   // format 01: ISO 14229
  return len;
}

static int clear_dtcs(int n) {
  if (n != 4) return negative(0x13);
  if (req[1] != 0xFF || req[2] != 0xFF || req[3] != 0xFF) return negative(0x31);   // only "every group"
  for (int i = 0; i < NDTC; i++) dtcs[i].status = 0;
  res[0] = 0x54;
  return 1;
}

int main(void) {
  setup_board();
  RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
  GPIOA->MODER |= 3u;                              // PA0 analog
  ADC1->SQR3 = 0;
  ADC1->SMPR2 |= ADC_SMPR2_SMP0_2;
  ADC1->CR2 |= ADC_CR2_ADON;
  printf("ECU: %s\r\n", can_init() ? "on the bus" : "no CAN bus");

  for (;;) {
    monitor();
    int n = isotp_receive(req, sizeof req, 20);
    if (n <= 0) continue;
    int len;
    switch (req[0]) {
      case 0x22: len = read_data(n); break;
      case 0x19: len = read_dtc_information(n); break;
      case 0x14: len = clear_dtcs(n); break;
      default:   len = negative(0x11); break;
    }
    isotp_send(res, len);
    printf("Service %02X: %s, %d bytes\r\n", req[0], res[0] == 0x7F ? "refused" : "answered", len);
  }
}

Lessons behind it