5.5.7 · D1 · Coding › Embedded Systems & Real-Time Software › Interrupts — ISR design, NVIC priority, interrupt latency
Ek computer normally aapka program line by line chalata hai, lekin kabhi kabhi bahar ki duniya mein koi event (ek button, ek timer, ek byte ka aana) itna urgent hota hai ki woh aapke loop ke notice karne ka intezaar nahi kar sakta. Ek interrupt ek doorbell hai jo seedha CPU mein wire hoti hai: hardware aapke program ko pause karta hai, ek special chhoti si function ki taraf jump karta hai us event ko handle karne ke liye, phir aapke program ko bilkul wahin wapas rakh deta hai jahan tha — aur poori art yeh hai ki woh pause fast, safe, aur correctly ordered ho.
Yeh page kuch bhi assume nahi karta. parent topic padhne se pehle, aapko uske har word aur symbol mein fluent hona chahiye. Hum har ek ko ek picture se build karte hain.
Intuition Program counter
Socho memory mein instructions ki ek lambi list hai, har ek apne numbered address par. CPU ke paas ek single finger hai jo "woh instruction jo main abhi kar raha hoon" ki taraf point karti hai. Us finger ka ek naam hai.
Definition Program Counter — symbol
P C
P C ::: CPU ke andar ek special storage slot jo current (ya next) instruction ka address hold karta hai.
Picture: upar figure mein moving finger, instruction list ke neeche slide karti hui.
Topic ko kyun chahiye: ek interrupt define hota hai "finger ko kahin aur jump karwao, phir wapas lao." Jump ke baare mein baat kiye bina jump karne wale ko naam diye nahi ho sakti — woh hai P C .
Normally finger bas aage badhti hai: address, address+1, address+2. Ek jump matlab finger achanak kisi alag address par land karti hai. Interrupt ek aisa jump hai jo hardware force karta hai, koi aapne likha nahi.
Intuition Registers kyun exist karte hain
Main memory (RAM) se padhna slow hai. Isliye CPU apne arithmetic unit ke paas kuch ultra-fast slots rakhta hai. Woh values in slots mein dalta hai, compute karta hai, aur results wapas dalta hai. Yeh slots registers hain.
Definition Registers — symbols
R 0 –R 12 , L R , P C , x P S R
Register ::: CPU ke andar ek named, super-fast storage cell (ek word hold karta hai — Cortex-M par 32 bits).
R 0 –R 12 ::: general-purpose scratch slots aapke numbers ke liye.
L R (Link Register) ::: "return address" hold karta hai — kahan wapas jana hai function call ke baad.
x P S R (Program Status Register) ::: status flags hold karta hai (kya last result zero tha? negative? kya overflow hua?).
Picture: chhote labelled boxes ki ek row; neeche figure unhe dikhata hai.
Topic ko kyun chahiye: jab CPU ko interrupt ke liye yanked away kiya jata hai, in boxes mein values program ke mid-sentence thoughts hain. Agar yeh clobber ho jayein, toh interrupted program resume hone par corrupted nonsense ban jata hai. Isliye topic baar baar in exact boxes ko save aur restore karne ki baat karta hai.
Definition Caller-saved vs callee-saved
Parent note kehta hai hardware auto-saves R 0 –R 3 , R 12 , L R , P C , x P S R (caller-saved set) aur compiler R 4 –R 11 (callee-saved ) handle karta hai agar use ho.
Caller-saved ::: registers jo ek function trample karne ke liye free hai, isliye jo called karta hai woh pehle save kare agar zaroorat ho. Hardware yeh interrupts ke liye karta hai → 8 words.
Callee-saved ::: registers jo ek function preserve karna chahiye, isliye agar ISR unhe use kare, toh compiler push/pop insert karta hai.
Kyun: yahi split hai kyun "ek ISR ek normal C function ho sakta hai" — caller-saved half free hai, callee-saved half compiler already protect karna janta hai.
Intuition Plates ka stack
Jab aap kisi ko mid-task interrupt karte ho, toh aap unki current state ek note par likhte ho aur ek pile par stack karte ho. Resume karne ke liye, aap top note wapas lete ho. Sabse naya save, sabse pehle restore — "Last In, First Out."
Definition Stack aur symbol
S P
Stack ::: RAM ka ek region jo LIFO (Last In, First Out) use karta hai values temporarily park karne ke liye.
Push ::: ek value top par rakhna. Pop ::: top value hatana.
S P (Stack Pointer) ::: ek register jo stack ke current top ka address hold karta hai.
Picture: upar plates ka badhta/ghatta hua pile.
Topic ko kyun chahiye: parent ka "stacking = hardware 8 registers push karta hai" aur exit par "unstacking " literally is stack par push-then-pop hai. 8 saved registers hi program ka parked context hain.
Recall LIFO kyun aur queue kyun nahi saved context ke liye?
Interrupts nest ho sakte hain (ek interrupt ke andar ek interrupt). Sabse recently interrupted context pehle resume hona chahiye — yeh exactly LIFO hai.
Stack ke kis end ko push aur pop dono par touch kiya jata hai? ::: Top, S P dwara track kiya gaya.
Definition Function call vs interrupt call
Function call ::: aapka code ek aur chunk run karne ke liye kehta hai, L R mein return address save karta hai, phir wapas aata hai.
ISR (Interrupt Service Routine) ::: hardware ek chunk ki taraf jump force karta hai, stack par context save karta hai, phir wapas aata hai.
Topic ko kyun chahiye: parent ka one-sentence picture — "ek interrupt ek hardware-triggered function call hai jo CPU forced hai karne ke liye" — tabhi samajh aata hai jab aap jaante ho ki ek ordinary function call ek voluntary "jump and come back" hai. Interrupt uska involuntary cousin hai.
Intuition Device jo haath uthata hai
Ek peripheral CPU ke paas ek hardware block hai — ek timer, ek UART (serial port), ek GPIO pin. Jab uska event hota hai, woh ek wire (the interrupt line ) raise karta hai aur ek flag bit set karta hai jo kehta hai "main tha."
Definition Peripheral, interrupt line, flag, pending
Peripheral ::: ek on-chip device jo events generate karta hai (timer expiry, byte received, pin change).
Interrupt line / request (IRQ) ::: electrical signal jo ek peripheral assert karta hai kehne ke liye "mera dhyan do."
Flag ::: peripheral ke andar ek status bit jo set rehta hai jab tak aap clear nahi karte, record karta hai ki event hua.
Pending ::: NVIC ka note ki ek IRQ service hone ka wait kar raha hai lekin abhi run nahi hua.
Topic ko kyun chahiye: "interrupt flag early clear karo" aur "NVIC ise pending mark karta hai" dono yahan hain. Agar aap flag kabhi clear nahi karte, peripheral haath uthata rehta hai → ISR forever fire karta rehta hai.
Intuition Ek receptionist with a phone book
Bahut se peripherals ek saath haath utha sakte hain. Koi cheez decide karni chahiye kaun pehle jaata hai aur kaun si function call hogi . Woh cheez hai NVIC . Uska phone book — "IRQ number → run hone wali function ka address" — vector table hai.
Definition NVIC aur vector table
NVIC (Nested Vectored Interrupt Controller) ::: Cortex-M peripheral jo IRQs receive karta hai, unhe priority ke hisaab se rank karta hai, nesting allow karta hai (higher priority ek running lower-priority ISR ko preempt karta hai), aur CPU ko bata ta hai kahan jump karna hai.
Vector table ::: memory mein ek array; entry k mein IRQ number k ke liye ISR ka address hota hai.
Vectored ::: "controller pehle se jump address jaanta hai" (table mein lookup karta hai) — koi software search nahi chahiye.
Topic ko kyun chahiye: baad ke har idea (priority grouping, latency, tail-chaining) ek NVIC ka behaviour hai. Aur "ISR address vector table se load karke P C mein daalo" woh exact moment hai jab finger jump karta hai.
2 n — bits se counting
2 n ::: kitni distinct values n bits represent kar sakti hain (1 bit ke liye 2, 2 bits ke liye 4, 3 bits ke liye 8...).
Picture: har extra bit options ki sankhya double karta hai — ek branching tree.
Topic ko kyun chahiye: priority levels is tarah count hote hain. "N implemented bits → 2 N levels," split 2 p preempt groups × 2 N − p sub-levels mein. Yeh sab powers of two hai.
Worked example Warm-up conversion
Ek critical section 100 MHz par 50 cycles ke liye interrupts mask karta hai. Yeh kitna time hai?
50 × 10 ns = 500 ns .
12-cycle hardware part (120 ns ) add karo aur worst-case latency 620 ns hai — exactly parent ka number.
Definition Masking / interrupts disable karna
Mask ::: CPU ko temporarily batana "interrupts ignore (delay) karo." __disable_irq() se ya BASEPRI raise karke kiya jata hai (ek threshold: chosen level se kam urgent kuch bhi block karo).
Critical section ::: code ka ek chhota span jahan interrupts mask hote hain taaki ek shared variable half-updated na ho jab ISR bich mein aaye.
Picture: doorbell par ek mute button — pizza abhi bhi aata hai, lekin aap ise tab tak nahi sunte jab tak unmute nahi karte.
Topic ko kyun chahiye: masking latency ka software-controllable part hai (t blocked ) aur woh tool hai jo shared-variable access ko atomic banata hai.
volatile aur atomic
volatile ::: compiler ko batane wala keyword "yeh variable aapke peeth peeche change ho sakta hai (ek ISR ise likhta hai) — har baar ise memory se re-read karo, kabhi register mein cache mat karo."
Atomic ::: ek update jo beech mein interrupt nahi ho sakti; ya poori ho gayi ya shuru hi nahi hui.
Topic ko kyun chahiye: yeh dono classic ISR-vs-main-loop bugs rokate hain (infinite while(!flag) aur torn reads).
Program Counter - the moving finger
Registers R0 to R12 LR xPSR
Stack and SP - LIFO parking
ISR - forced function call
Peripherals flags IRQ lines
Vector table - address phone book
Cycles and clock frequency
Khud test karo — agar koi bhi jawab fuzzy lage, toh parent note se pehle woh section dobara padho.
P C kya hold karta hai, aur interrupt ke dauran uske saath kya hota hai?Current instruction ka address; hardware ise save karta hai aur ISR ka address load karta hai taaki finger jump kare.
Cortex-M ke 8 auto-stacked registers ke naam batao. R 0 –R 3 , R 12 , L R , P C , x P S R .
Saved context ke liye LIFO (stack) kyun aur queue kyun nahi? Nested interrupts ko most-recently-interrupted pehle resume karna chahiye — Last In, First Out.
"Flag" kya hai aur ISR ko ise early kyun clear karna chahiye? Peripheral status bit jo event record karta hai; clear na karne par woh IRQ baar baar assert hoti rehti hai → ISR bar bar re-enter karta hai.
Vector table kya map karta hai? IRQ number → us interrupt ke ISR ka memory address.
N implemented priority bits kitne levels dete hain?2 N total, split 2 p preempt × 2 N − p sub.
100 MHz par 12 cycles ko time mein convert karo. 12 × 10 ns = 120 ns .
volatile compiler ko kya force karta hai?Variable ko register mein cache karne ki bajaye har access par memory se re-read karna.
Interrupts mask (disable) karna aapko kya deta hai, aur uski kya cost hai? Shared data tak atomic access milti hai; cost hai added interrupt latency (t blocked ).
Next: har symbol earn karne ke baad, parent topic padho. Related foundations jo aap saath rakhna chahein: Polling vs Interrupt-driven I/O , Concurrency & Race Conditions , Ring Buffers / FIFO Queues , ARM Cortex-M Architecture , Real-Time Scheduling & Deadlines , Power Management & Sleep Modes .