5.5.6 · HinglishEmbedded Systems & Real-Time Software

CAN bus — frame format, arbitration, error handling — critical in aerospace

2,200 words10 min readRead in English

5.5.6 · Coding › Embedded Systems & Real-Time Software

CAN = Controller Area Network. Ek robust, multi-master serial bus jo Bosch ne (1986 mein) cars ke liye design kiya tha, aur ab aerospace, trains, medical, aur industrial control mein critical hai. CAN ki genius yeh hai ki bahut saare nodes ek pair of wires share karte hain, bina kisi master ke decide karte hain ki kaun baat kare, aur noisy environments ko tolerate karte hain — aur yeh sab hardware mein hota hai.


[!intuition] CAN ki ek-sentence wali soul

Ek polite committee of nodes jo ek hi wire par ek saath chillaate hain. Jiske paas sabse urgent message (lowest ID) hota hai woh automatically floor jeet leta hai — aur baaki sab apna message khoye bina step back kar lete hain. Koi central referee ki zaroorat nahi.

WHY yeh aerospace mein matter karta hai: wires heavy hote hain aur failure-prone bhi. CAN dozens of sensors/actuators ko do wires (CAN_H, CAN_L) share karne deta hai, life-critical messages ko self-prioritize karne deta hai (jaise engine fire alarm cabin-light status se pehle aata hai), aur bit errors ko automatically detect + recover karta hai — exactly wahi jo ek DO-178C / DO-254 certified system ko chahiye.


[!definition] Core vocabulary

  • Dominant bit : bus par logic 0. Agar koi bhi node 0 drive kare, bus 0 read karta hai.
  • Recessive bit : logic 1. Bus tab 1 hota hai jab har node use float hone de.
  • Wired-AND : electrical rule — bus value = sab transmitted bits ka AND. Yeh arbitration ka physical foundation hai.
  • Identifier (ID) : 11 bits (standard) ya 29 bits (extended). Lower numeric ID = higher priority.
  • Bit stuffing : 5 identical consecutive bits ke baad, transmitter 1 opposite bit insert karta hai, taaki receivers apni clocks ko resynchronize kar sakein.

WHY dominant=0? Kyunki AND mein, ek akela 0 result ko 0 force kar deta hai. Jo node 0 bhej raha hai woh "jeet" jaata hai bina yeh jaane bhi ki woh compete kar raha hai. Yahi woh trick hai jo arbitration ko free banati hai.


[!intuition] Wire ko ek giant AND gate ki tarah imagine karo

Agar teen nodes simultaneously bits bhejein, toh bus dikhata hai Jo node recessive (1) bhejta hai lekin bus par dominant (0) read karta hai, woh instantly jaan jaata hai: "koi higher priority wala baat kar raha hai — main peeche hat jaata hoon." Woh ruk jaata hai, wait karta hai, aur baad mein automatically retransmit karta hai. Uska message kabhi corrupt nahi hota, bas delay hota hai.

Figure — CAN bus — frame format, arbitration, error handling — critical in aerospace

Frame format — field by field derive kiya (Standard Data Frame)

Hum frame ko yeh puchh kar build karte hain: receiver ko kya chahiye, kis order mein?

Field Bits WHY yeh exist karta hai
SOF Start of Frame 1 (dominant) Falling edge par sab listeners ko wake aur synchronize karta hai.
Identifier 11 Message priority + content type.
RTR Remote Tx Request 1 0 = data frame, 1 = ek request jo data maangta hai.
IDE 1 0 = 11-bit (standard), 1 = 29-bit (extended).
r0 reserved 1 future use, dominant.
DLC Data Length Code 4 data bytes ki sankhya (0–8).
Data 0–64 actual payload.
CRC 15 + 1 delimiter preceding sab bits par error detection.
ACK 1 + 1 delimiter koi bhi receiver dominant pull karta hai yeh kehne ke liye "mujhe valid frame mila."
EOF End of Frame 7 (recessive) end mark karta hai.
IFS Inter-Frame Space 3 (recessive) next frame se pehle gap.

HOW ACK kaam karta hai (subtle!): transmitter ACK slot ko recessive bhejta hai. Har receiver jisne CRC check pass kiya woh use dominant se overwrite kar deta hai. Transmitter tab dominant read karta hai → "kam se kam ek node ne mujhe sahi suna." Koi individual acknowledgment nahi, bas ek collective thumbs-up.


[!example] Arbitration: do nodes ladte hain, kisi ka data nahi jaata

Node A ID 0b00010110000 (= 176) bhejna chahta hai. Node B ID 0b00010111000 (= 184) bhejna chahta hai.

Dono SOF par saath shuru karte hain. Woh ID bits MSB→LSB bhejte hain. Bit by bit dekho:

bit# A bhejta hai B bhejta hai bus(AND) result
10..4 same same same tie, chalte raho
3 0 1 0 B padhta hai 0≠1 → B backs off
2..0 A akele continue karta hai A jeet gaya

Yeh step kyun? Bit 3 par, A dominant (0) drive karta hai, B recessive (1) drive karta hai. Wired-AND → 0. B ka transceiver compare karta hai "maine 1 bheja lekin bus 0 hai" → arbitration haar jaata hai, receiver ban jaata hai, baad mein retry karta hai. A ki transmission kabhi interrupt nahi hui. Lower ID (176 < 184) = higher priority = jeet. ✅

Yeh hai non-destructive bitwise arbitration — CAN ki defining feature.


Error handling — paanch guardians

CAN hardware mein 5 independent error checks chalata hai:

  1. Bit error — ek transmitter woh bit read back karta hai jo usne bheja nahi tha (arbitration/ACK ko chhodkar).
  2. Stuff error — 6+ identical bits dekhe (bit-stuffing rule violate hua).
  3. CRC error — computed CRC ≠ received CRC.
  4. Form error — fixed-format field (delimiter, EOF) mein wrong bit value hai.
  5. ACK error — transmitter koi dominant ACK nahi dekhta → kisi ne nahi suna.

Jab koi bhi node error detect karta hai toh woh Error Frame bhejta hai (6 dominant bits = "active error flag"), jo jaanboojhkar bit-stuffing violate karta hai taaki sabko pata chale aur frame globally discard + automatically retransmit ho.

[!formula] Fault confinement — counters se derive kiya

Ek toota hua node bus ko hamesha ke liye jam karne se rokne ke liye, har node do counters rakhta hai:

  • TEC = Transmit Error Counter
  • REC = Receive Error Counter

Rules (key numbers):

  • Error par: TEC += 8 (transmitter), REC += 1 (receiver).
  • Success par: counters 1 se decrement hote hain.

WHY +8 / −1 asymmetry? Jo node errors cause kar raha hai (transmitter) use 8× zyada tezi se punish kiya jaata hai jitna woh heal hota hai, isliye ek faulty transmitter jaldi khud ko silent kar leta hai (Error-Passive → Bus-Off) jabki jo node sirf doosron ki errors dekh raha hai woh barely climb karta hai. Yeh hai fault confinement: bus khud ko offender ko isolate karke protect karta hai. Aerospace ke liye critical — ek kharaab LRU network ko down nahi kar sakta.


[!mistake] Steel-manned misconceptions

Mistake 1: "Highest ID number ki highest priority hoti hai." Yeh sahi kyun lagta hai: "high number = high importance" everyday intuition se match karta hai. The fix: Yeh ulta hai. Lower ID = higher priority, kyunki dominant=0 wired-AND mein jeet jaata hai, aur ek chota binary number mein zyada leading zeros (dominant bits) hote hain. Apne fire alarm ko ID 0x001 assign karo, 0x7FF nahi.

Mistake 2: "Arbitration loss message ko corrupt ya drop kar deta hai." Yeh sahi kyun lagta hai: Ethernet/CSMA-CD mein collision dono frames ko destroy kar deta hai → dono retransmit hote hain. The fix: CAN non-destructive hai. Winner ka frame untouched rehta hai; loser bas wait karta hai aur retry karta hai. Zero data lost.

Mistake 3: "Bus-Off ka matlab node permanently dead hai." Yeh sahi kyun lagta hai: "off" terminal laggta hai. The fix: Ek Bus-Off node 11 consecutive recessive bits (bus idle) ke 128 occurrences observe karne ke baad recover kar sakta hai, phir counters reset ke saath rejoin karta hai. Recovery spec ka part hai.

Mistake 4: "Zyada data bytes = higher priority kyunki yeh bada message hai." The fix: Priority SIRF arbitration ke dauran ID field par depend karta hai; DLC/data baad mein aate hain aur kabhi affect nahi karte ki kaun jeetta hai.


[!recall]- Feynman: ek 12-saal ke bachche ko explain karo

Socho ek classroom jahan sab ek microphone par ek saath bol sakte hain. Ek rule hai: agar koi bhi "0 button" press kare, toh speaker 0 kehta hai; sirf tab 1 kehta hai jab sab "1" press karein. Har bachche ke paas ek secret number hota hai. Woh sab apna number digit by digit count karna shuru karte hain. Jis moment ek bachcha dekhta hai ki mic 0 bol raha hai jabki usne 1 press kiya, woh politely chup ho jaata hai aur wait karta hai — use pata hai koi zyada important bol raha hai. Sabse chhote number wala bachcha hamesha jeetta hai aur apna sentence perfectly finish karta hai, kisi ka kuch garble nahi hota. Aur agar koi bachcha baar baar galti karta hai, toh ek scorekeeper uske fouls count karta hai; bahut zyada hone par woh khud ko bench kar leta hai taaki class ko kharaab na kare.


[!mnemonic]

"Dominant Drives Down, Lowest ID Leads."

  • Dominant = Down = 0 jeetta hai.
  • Lowest = Leads (highest priority). Frame order: "Some Idiots Read Data, Check ACK End"SOF, ID, RTR, DLC, Data, CRC, ACK, EOF.

Flashcards

CAN bus par dominant bit ka logic level kya hota hai?
Logic 0 (yeh recessive ko wired-AND ke zariye override karta hai).
Lowest ID arbitration kyun jeetta hai?
Dominant=0 wired-AND mein jeetta hai, aur ek lower binary ID mein zyada/pehle dominant bits hote hain, isliye woh bus par rehta hai jabki doosre mismatch read karke back off karte hain.
CAN arbitration ko "non-destructive" kya banata hai?
Winning frame kabhi interrupt nahi hota; losers detect karte hain ki unhone recessive bheja lekin dominant read kiya, ruk jaate hain, aur baad mein retransmit karte hain — koi data lost nahi.
ACK slot kaise kaam karta hai?
Transmitter use recessive bhejta hai; koi bhi receiver jisne CRC pass kiya woh use dominant se overwrite karta hai, signal karta hai "kam se kam ek node ne sahi receive kiya."
5 CAN error types ke naam batao.
Bit, Stuff, CRC, Form, ACK error.
Bit stuffing kya hai aur kyun?
5 identical consecutive bits ke baad, ek opposite bit insert karo, taaki receivers clocks resynchronize kar sakein (edges guarantee hoti hain).
Error-Active, Error-Passive, Bus-Off ke liye TEC/REC thresholds?
Active: TEC≤127 aur REC≤127. Passive: TEC>127 ya REC>127. Bus-Off: TEC>255.
Transmit error par error count 8 se increment kyun hota hai lekin receive error par sirf 1 se?
Likely-faulty transmitter ko passive witness se 8× zyada tezi se punish karne ke liye, fault confinement achieve hota hai taaki kharaab node khud ko silent kar le.
SOF field kiske liye hai?
Ek single dominant bit jo falling edge create karta hai taaki sab listening nodes synchronize ho sakein.
Standard vs extended identifier lengths?
Standard = 11 bits, Extended = 29 bits (IDE bit se select hota hai).
Kya ek Bus-Off node recover kar sakta hai?
Haan, 11 consecutive recessive bits ke 128 sequences observe karne ke baad, phir reset counters ke saath rejoin karta hai.
Priority kaun sa field determine karta hai, aur use kabhi affect nahi karta?
Identifier priority determine karta hai; DLC/Data length kabhi arbitration ko affect nahi karti.

Connections

  • Wired-AND logic aur Open-collector buses — dominant/recessive ka electrical basis.
  • CSMA-CD (Ethernet) — contrast: destructive collision vs CAN ki non-destructive arbitration.
  • CRC cyclic redundancy check — CAN error check mein use hota hai.
  • Bit stuffing and clock recovery — ek alag clock line ke bina synchronization.
  • DO-178C / DO-254 aerospace certification — kyun deterministic fault confinement matter karta hai.
  • Real-Time scheduling — CAN priority ≈ fixed-priority message scheduling (wire par rate-monotonic).
  • ARINC 825 — CAN ke upar banaya gaya aerospace profile.

Concept Map

0 read karta hai agar koi node 0 bheje

1 sirf tab agar sab float karein

enables

compete karta hai via

loser 1 bhejta hai 0 read karta hai

message intact

placed in

contains

contains

enables

allows

supports

critical msgs ko prioritize karta hai

Wired-AND bus

Dominant bit = 0

Recessive bit = 1

Arbitration

Lower ID = higher priority

Node backs off

Auto retransmit later

Standard Data Frame

CRC field

ACK slot

Error detection

Bit stuffing after 5 equal bits

Receiver resync

Aerospace safety DO-178C