Back

LIN

Loading views...

LIN Protocol

Overview

LIN (Local Interconnect Network) is a low-cost, single-wire serial protocol widely used in automotive body electronics to connect sensors and actuators that don't require the bandwidth or robustness of CAN. Examples include window controls, seat adjusters, mirror controls, and climate sensors.

1. Theory & Fundamentals

  • Single wire (12V bus) + GND — uses the vehicle's battery voltage
  • Master-slave architecture: 1 master, up to 16 slaves
  • Speed: 1–20 kbps (typically 10.4 or 19.2 kbps)
  • Frame-based: Master sends header; slaves respond or receive data field
  • No crystal needed on slave: Slave auto-detects baud from break field
  • LIN versions: LIN 1.3, LIN 2.0, LIN 2.2A (most current)

2. Frame / Packet Structure

LIN Frame:
  BREAK (≥13 bit times LOW) | SYNC (0x55) | PID(8) | DATA(1–8B) | CHECKSUM(1B)

PID (Protected ID):
  ID[5:0] + P0 + P1 (parity bits)
  P0 = ID0^ID1^ID2^ID4
  P1 = ~(ID1^ID3^ID4^ID5)

Checksum:
  Classic: Sum of data bytes (inverted)
  Enhanced (LIN 2.x): Sum of PID + data bytes (inverted)

3. Protocol Mechanics

  • Master initiates every frame by sending break + sync + PID
  • Publisher slave (or master) sends data field
  • Subscriber slaves receive data field silently
  • Schedule table: Master cycles through frames on fixed schedule
  • Sleep mode: Master sends go-to-sleep frame; wakeup via dominant pulse

4. Hardware Implementation

  • LIN transceiver: TJA1020, MCP2003, SN65HVDA100
  • Single wire at 12V bus; transceiver converts to MCU UART levels
  • Pull-up: 1kΩ to 12V on LIN bus (in transceiver or external)
  • LIN bus termination: Resistor + diode on master side
  • Connector: Typically part of vehicle wiring harness; no standard connector for bus itself

5. Register-Level / Configuration

// LIN break generation (STM32: UART break)
USART1->CR1 |= USART_CR1_SBK; // Send break (13+ bit-times LOW)

// Sync byte (0x55 at 10.4kbps = 96µs bit time)
UART_Send(0x55);

// PID with parity
uint8_t LIN_MakePID(uint8_t id) {
    uint8_t p0 = ((id>>0)^(id>>1)^(id>>2)^(id>>4)) & 1;
    uint8_t p1 = (~((id>>1)^(id>>3)^(id>>4)^(id>>5))) & 1;
    return (id & 0x3F) | (p0 << 6) | (p1 << 7);
}

// Send LIN header
void LIN_SendHeader(uint8_t id) {
    LIN_SendBreak();
    UART_Send(0x55);           // Sync
    UART_Send(LIN_MakePID(id)); // Protected ID
}

6. Driver / Software Development

uint8_t LIN_ComputeChecksum(uint8_t pid, uint8_t *data, uint8_t len, uint8_t enhanced) {
    uint16_t sum = enhanced ? pid : 0;
    for(int i=0;i<len;i++) { sum += data[i]; if(sum>0xFF) sum -= 0xFF; }
    return (uint8_t)(~sum & 0xFF);
}

void LIN_MasterSendFrame(uint8_t id, uint8_t *data, uint8_t len) {
    LIN_SendHeader(id);
    for(int i=0;i<len;i++) UART_Send(data[i]);
    UART_Send(LIN_ComputeChecksum(LIN_MakePID(id), data, len, 1));
}

7. Debugging & Testing

  • Logic analyzer with LIN decode: Verify break width, sync, PID, data, checksum
  • Common issues: Break too short; wrong parity in PID; enhanced vs classic checksum mismatch
  • LIN slave auto-baud: Measures sync byte edges; verify 0x55 timing correct
  • Check 12V supply to transceiver

8. Real-World Applications

  1. Window and sunroof motor control
  2. Seat position memory and adjustment
  3. Interior lighting control
  4. Climate sensors (temperature, humidity)
  5. Rain/light sensors for wipers and headlights

9. Advanced Topics & Edge Cases

  • LIN schedule table: Fixed timing for all frames in a cycle
  • LIN diagnostics: ID 0x3C/0x3D for master diagnostic frames
  • LIN conformance testing: LIN compliance test suites
  • LIN-to-CAN gateway: Bridge LIN subsystems to CAN backbone
  • LIN slave addressing: First 6 bits of PID = frame ID (not device address)

10. Standards & Variants

Version Notes
LIN 1.3 Classic checksum
LIN 2.0 Enhanced checksum, improved diagnostics
LIN 2.2A Automotive Electronics Council standard, current
SAE J2602 North American variant of LIN 2.x

💡 Practical Examples

Example 1: Read window position sensor

LIN_SendHeader(0x10); // Request window sensor frame
uint8_t data[2]; LIN_ReceiveData(data, 2);
uint16_t position = (data[0]<<8)|data[1]; // 0=closed, 1000=open

Example 2: Control seat motor

uint8_t cmd[] = {0x01, 0x64}; // Move forward 100 steps
LIN_MasterSendFrame(0x20, cmd, 2);

Example 3: Sleep and wake-up

uint8_t sleep[] = {0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
LIN_MasterSendFrame(0x3C, sleep, 8); // Go-to-sleep command
// Wake: Pull LIN bus dominant for 250µs–5ms

🧪 Practice Questions

Beginner

  1. How many slaves can a LIN network have?
  2. What is the maximum LIN speed?
  3. What is the purpose of the SYNC byte?
  4. What voltage level is the LIN bus?
  5. Does LIN need a dedicated crystal on slave nodes?

Intermediate

  1. Calculate PID parity bits for frame ID 0x10.
  2. Explain the difference between classic and enhanced checksum.
  3. Implement a LIN master schedule table in C.
  4. How does a LIN slave auto-detect baud rate?
  5. What are diagnostic frames (ID 0x3C/0x3D) used for?

Advanced

  1. Implement a complete LIN 2.2A master with schedule table and diagnostics.
  2. Design a LIN slave node for a temperature sensor with sleep/wake support.
  3. Build a LIN-to-CAN gateway for body control module integration.
  4. Implement LIN conformance testing for PID parity and checksum.
  5. Design a LIN network for a 10-zone climate control system.

Hands-on Projects

  1. Window Controller: LIN master + 4 slave motor nodes for window lift.
  2. Body Control Module: LIN master scheduling 8 sensors/actuators.
  3. LIN Sniffer: Logic analyzer + decoder for all LIN frames on bus.

Checklist

  • [ ] Explain LIN frame structure and break timing
  • [ ] Calculate PID parity bits
  • [ ] Generate LIN break using UART peripheral
  • [ ] Implement master send and receive frame
  • [ ] Implement enhanced checksum calculation
  • [ ] Interface with real LIN transceiver (TJA1020)
  • [ ] Build LIN schedule table
  • [ ] Debug with logic analyzer
  • [ ] Implement sleep/wake-up sequence
  • [ ] Design a LIN slave node from scratch