5.5.23 · D4 · HinglishEmbedded Systems & Real-Time Software

ExercisesWatchdog timers — purpose, feeding, types

3,635 words17 min read↑ Read in English

5.5.23 · D4 · Coding › Embedded Systems & Real-Time Software › Watchdog timers — purpose, feeding, types

Yeh page watchdog timer topic ke liye ek graded workout hai. Har problem ka full solution ek collapsible callout ke andar chhupa hua hai taaki tum pehle khud try kar sako. Levels hain L1 Recognition → L2 Application → L3 Analysis → L4 Synthesis → L5 Mastery.

Shuru karne se pehle, ek promise: neeche use kiya gaya har symbol wahi hai jo tumne parent note mein already earn kiya hai. Hum sirf ek timeout equation pe rely karte hain, toh chaliye usse plain words mein re-anchor karte hain — aur apne symbols ek baar pin down karte hain taaki baad mein kuch bhi surprise na kare.

Figure — Watchdog timers — purpose, feeding, types

Prerequisite links agar koi step shaky lage: Hardware Timer Peripherals, Interrupt Service Routines, System Reset Sources, Real-Time Operating Systems, Fault Tolerance, Brown-Out Detection, Safe State Design, Bootloader Design.


L1 — Recognition

Problem 1 (L1)

Ek watchdog timer ko inme se kaunse se best describe kiya jaata hai?

(a) Ek stopwatch jo CPU tasks schedule karne ke liye read karta hai. (b) Ek hardware down-counter jo system ko reset ya interrupt karta hai agar software periodically use "feed" karna fail kare. (c) Ek voltage sensor jo low battery detect karta hai. (d) Ek memory region jo last error code store karta hai.

Recall Solution

Answer: (b). Defining behaviour woh dead-man's-switch idea hai jo parent note se aaya hai: software ko timer feed karke yeh prove karte rehna hota hai ki woh alive hai. Agar feeding ruk jaaye, toh counter tak pahunch jaata hai aur reset/interrupt force karta hai.

  • (a) ek plain hardware timer describe karta hai — neglect hone par koi automatic reset nahi.
  • (c) Brown-Out Detection hai, ek alag reset source.
  • (d) sirf logging hai; yeh koi action nahi leta.

Problem 2 (L1)

Har term ko uske meaning se match karo: feed, timeout, prescaler.

Recall Solution
  • Feed ::: Software ka woh act jisme counter ko wapas par reload kiya jaata hai taaki woh kabhi tak na pahunche (AVR par yeh wdr instruction hai, yani wdt_reset()).
  • Timeout ::: Woh time jo counter ko se tak girne mein lagta hai jab koi feeding na ho — woh deadline jise software ko beat karna hota hai.
  • Prescaler ::: Ek clock divider jo counter ko slow karta hai taaki har decrement zyada time le, jisse ek chhota counter badi time measure kar sake.

L2 — Application

Problem 3 (L2)

Ek 8-bit watchdog counter () par ke saath clock kiya jaata hai. Timeout period milliseconds mein nikalo.

Recall Solution

Seedha formula mein plug karo. Toh yeh configuration lagbhag 996 ms mein — yaani ek second se thoda kam — timeout hoti hai. Figure s01 dekho: same staircase idea, bas ki jagah steps.

Problem 4 (L2)

Tumhe aur (1 MHz) wale WDT par 500 ms ka timeout chahiye. Tumhe kaunsi prescaler value chahiye? Kya yeh ek "typical" prescaler (1, 8, 64, 256, 1024) hai?

Recall Solution

Unknown solve karne ke liye formula rearrange karo — rearrange kyun? Kyunki yahan timeout given hai aur prescaler woh hai jo hume pick karna hai. Yahan woh timeout hai jo hum chahte hain. Nearest standard prescaler hai. ke saath real timeout ban jaata hai 500 ms se thoda zyada — safe hai, kyunki thoda lamba window kabhi false reset cause nahi karta; yeh sirf deadline relax karta hai.

Problem 5 (L2)

Ek main loop teen tasks run karta hai jinhe ms, ms, aur ms lagte hain, phir har iteration mein ek baar watchdog feed karta hai. Watchdog timeout ms hai. Margin (spare time) kya hai jisse pehle ek hang reset trip karta, aur loop timeout ka kitna fraction use karta hai?

Recall Solution

Ek full loop mein lagta hai. Healthy operation mein do feeds ke beech ka yeh sabse lamba gap hai, toh . Healthy utilisation comfortable headroom deta hai. Figure s02 mein feed ms par hoti dikhti hai, ms deadline bar ke against.

Figure — Watchdog timers — purpose, feeding, types

L3 — Analysis

Problem 6 (L3)

Do developers same 16-bit WDT (, ) configure karte hain.

  • Dev A pick karta hai.
  • Dev B pick karta hai.

Ek legitimate firmware-update routine 40 seconds leti hai jiske dauran watchdog feed nahi kiya ja sakta. Kaunse developer ki configuration false reset cause karti hai, aur kyun?

Recall Solution

Dono timeouts compute karo (, bas har config ka hai). Update ko tak watchdog quiet rehne ki zaroorat hai.

  • Dev A (): watchdog mid-update fire karta hai → false reset, possibly flash write corrupt karta hai (yeh careful Bootloader Design ka kaam hai).
  • Dev B (): update comfortably survive karta hai.

Analysis takeaway: timeout sabse lamba legitimate uninterrupted operation se zyada hona chahiye, warna false positives milenge. Short timeouts real hangs se faster recover karte hain lekin lambe valid work ke liye less tolerant hote hain — yeh ek genuine trade-off hai.

Problem 7 (L3)

Ek STM32 par ek window watchdog ek 6-bit down-counter use karta hai jo decrement per millisecond ki effective rate par feed hota hai, se shuru hokar. Chip ka rule (precisely stated taaki boundaries unambiguous rahen):

  • Feeding too early reject ki jaati hai jab tak counter upper threshold se strictly greater ho. Feed tabhi legal hai jab counter ya neeche aa jaaye.
  • Chip reset immediately issue karta hai jab counter lower threshold par pahunche (yani already "too late" hai).

(earliest legal feed time) aur (latest safe feed time) nikalo, last refresh se whole milliseconds mein measure karke. Dhyan raho counter discrete hai — yeh sirf integer milliseconds par integer values leta hai.

Recall Solution

Kyunki counter se har millisecond mein ek unit drop karta hai, whole millisecond par uski value hai Earliest legal feed (). Hume chahiye. Solve karo . par counter exactly hai, jo rule ke hisaab se legal hai (" ya neeche"). Toh earliest legal feed hai (Agar chip strictly se neeche maangta, toh pehla legal instant next step hota, ms, kyunki par counter abhi bhi read karta hai. Hum upar stated "" rule adopt karte hain.)

Latest safe feed (). Reset tab fire hoti hai jab , yani par. Toh ms already ek reset hai — too late. Isliye last safe whole-millisecond feed ek step pehle hai, jab counter abhi read karta hai: Legal window: kisi bhi whole millisecond par feed karo jahan ms (counter se tak) — ek -step-wide slot. Figure s03 yeh band draw karta hai aur dono boundary counter values label karta hai.

Figure — Watchdog timers — purpose, feeding, types

L4 — Synthesis

Problem 8 (L4)

Design goal: ek control loop ke liye WDT configuration choose karo.

  • Main loop, worst case, har iteration mein leta hai (measured).
  • Tum chahte ho ki timeout roughly worst-case loop time ka ho taaki ek single slow iteration kabhi false-reset na kare, lekin ek true hang ek fraction of a second mein recover ho jaaye. Is wish ko kaho.
  • Hardware: , , standard prescalers .

Prescaler pick karo, actual timeout report karo, aur ek worst-case loop par safety margin confirm karo.

Recall Solution

Step 1 — target. Desired .

Step 2 — kaunsa prescaler wahan land karta hai? Target par prescaler ke liye formula solve karo: Sabse chhota available prescaler hai, jo is hardware par sabse chhota possible timeout deta hai. Us best-case value ko naam do (yeh simply hai jab ): Step 3 — reality padhna. Fastest configuration bhi par timeout hoti hai, hamare wish se bahut upar. Yeh hardware ki ek feature hai: slow kHz clock aur 16-bit counter ke saath, tum sub-second timeout nahi pa sakte. Toh hum , accept karte hain.

Step 4 — margin check. Loop sirf window ka use karta hai — extremely safe, koi false resets nahi, aur ek true hang abhi bhi mein recover hoga. Synthesis lesson: kabhi kabhi "ideal" timeout reachable nahi hota aur design ka kaam yeh hota hai ki closest feasible configuration choose karo, phir verify karo ki margin comfortable hai — jo ki hai.

Problem 9 (L4)

Neeche diye buggy feed placement ko ek correct heartbeat mein refactor karo, aur ek sentence mein explain karo ki fix ab kaunsi class of bug catch karta hai.

void loop() {
    wdt_reset();                    // pehle feed kiya
    read_sensor();
    if (compute() < 0) {
        while (1) { /* error spin */ }   // intended "safe halt"
    }
    actuate();
}
Recall Solution

Corrected version — ek poori successful iteration ke baad feed karo:

void loop() {
    read_sensor();
    if (compute() < 0) {
        enter_safe_state();   // deliberate, phir WDT hume reset kare
        while (1) { }         // yahan koi feed nahi -> WDT recover karta hai
    }
    actuate();
    wdt_reset();              // heartbeat: sirf tab jab hum end tak pahunche
}

Yeh ab kya catch karta hai: koi bhi path jo poora loop body complete karne mein fail kare — deliberate error-spin sameta — kyunki feed tab hoti hai jab read_sensor → compute → actuate sab succeed kare. Buggy original mein, early feed ne error-spin ko recovery se pehle ek full fresh timeout diya, aur feed ke baad ek accidental infinite loop abhi bhi ek poore window ke liye mask hota. Safe State Design dekho ki hum reset hone se pehle outputs ko known-safe value par kyun drive karte hain, aur Fault Tolerance dekho recovery philosophy ke liye.


L5 — Mastery

Problem 10 (L5)

Ek window watchdog ek ISR-driven loop protect karta hai. Window hai (jaise parent note mein; open interval ke andar strictly feeds legal hain). Ek timing bug ek interrupt har fire karta hai, aur har firing loop run karke feed attempt karta hai.

(a) 8-firing span ( last successful refresh ke baad) mein, kitne attempted feeds legal window ke andar land karte hain? (b) Explain karo ki ek plain (non-window) watchdog is system ko perfectly healthy kyun report karta, aur window watchdog kya gain karta hai.

Recall Solution

(a) ISR last successful refresh ke baad par fire karta hai. Feed tabhi legal hai jab (strictly inside).

  • : too early () → har ek strict window watchdog par reject / reset trigger karta hai.
  • : boundary par, aur false hai → too early, legal nahi.
  • : legal ✅ — sirf yahi ek.
  • : boundary, false hai → too late.

Final count: exactly 1 legal feed ( par) 8 attempts mein se. Har doosra attempt ek window violation hai, toh window watchdog early feeds refuse karta hai aur tumhe timing bug confront karne par majboor karta hai.

(b) Ek plain watchdog sirf poochta hai "kya tum se pehle feed kiye gaye?" Har feeding easily satisfy karta hai — har span mein 8 healthy-looking heartbeats — toh timing bug use bilkul invisible hai. Window watchdog lower bound add karta hai, question ko convert karta hai "kya tum SAHI rate par feed kiye gaye?" (na too fast, na too slow). Yahi woh cheez hai jo interrupt storms aur skipped-delay bugs catch karta hai jinhe plain watchdog sail past karta hai. Interrupt Service Routines cross-reference karo ki ISRs ko short aur rate-correct rakhna kyun matter karta hai, aur Real-Time Operating Systems dekho ki ek RTOS task-based watchdog ise per-task heartbeats tak kaise generalize karta hai.

Problem 11 (L5)

Multi-task heartbeat. Ek RTOS teen tasks run karta hai jo sabhi alive rehni chahiye. Ek common pattern: har task ek cycle complete hone par ek shared alive_flags word mein apna bit set karta hai; ek supervisor hardware watchdog tab feed karta hai jab sab bits set hon, phir unhe clear karta hai. Task A har cycle karta hai, Task B har , Task C har . Minimum hardware-watchdog timeout kya hai jo healthy operation mein false-reset nahi karega, aur kyun?

Recall Solution

Supervisor sirf tab feed kar sakta hai jab teeno bits set hon, phir clear karta hai. Clear karne ke baad, agla feed tab tak nahi ho sakta jab tak har task ne kam se kam ek aur cycle complete na ki ho — aur sabse slow task, C, yeh gate karta hai, kyunki A aur B apne bits bahut pehle set kar dete hain aur C ke khatam hone se pehle already set ho jaate hain.

  • Do supervisor feeds ke beech worst-case gap Task C ki period (plus thoda scheduling jitter).
  • Isliye hardware timeout satisfy karna chahiye choose karna slowest heartbeat par margin deta hai jabki phir bhi genuine total hang ke andar recover karta hai. Mastery point: multi-task system mein watchdog sirf tumhare slowest mandatory heartbeat jitni fast hoti hai — timeout us task se set karo, fast walon se nahi, warna ek stalled Task C healthy dikhega jab A aur B shared feed alive rakhenge. Flags par AND-gate wahi hai jo prevent karta hai ki ek fast task ek dead slow task ko mask kare.

Recall Quick self-audit checklist
  • Timeout formula: prescaler se multiply karo ::: — bada prescaler matlab lamba timeout.
  • Prescaler solve karo ::: , phir standard value par round karo.
  • Kahan feed karo ::: ek baar, ek complete iteration ke end par (heartbeat), kabhi pehle nahi, kabhi per-function nahi.
  • Window watchdog ::: sirf mein legal; "reset when counter " matlab last safe step hai.
  • Multi-task ::: feed ko sab mandatory heartbeats ke AND par gate karo; timeout slowest task se set karo.