Configuring watchdog timeout in Embedded C - Try It Yourself
watchdog_timeout and set it to 5000 to represent the timeout in milliseconds.Use int watchdog_timeout = 5000; to define the timeout.
WatchdogConfig with one member timeout_ms of type int. Then create a variable called wd_config of type WatchdogConfig.Use typedef struct { int timeout_ms; } WatchdogConfig; and then WatchdogConfig wd_config;.
watchdog_timeout to the timeout_ms member of wd_config.Use wd_config.timeout_ms = watchdog_timeout; to assign the value.
printf to print the message: "Watchdog timeout set to: %d ms\n" with the value of wd_config.timeout_ms.Use printf("Watchdog timeout set to: %d ms\n", wd_config.timeout_ms); inside main().
Practice
What is the main purpose of configuring a watchdog timer's timeout in embedded C?
Solution
Step 1: Understand watchdog timer role
The watchdog timer resets the system if it stops responding.Step 2: Identify timeout configuration purpose
Timeout sets the wait time before reset triggers.Final Answer:
To set how long the system waits before resetting if it hangs -> Option CQuick Check:
Watchdog timeout = reset delay [OK]
- Confusing timeout with CPU speed
- Thinking timeout allocates memory
- Assuming timeout enables debugging
Which of the following is the correct syntax to set a watchdog timeout to 5000 milliseconds in embedded C?
void setup_watchdog() {
// Set timeout here
}Solution
Step 1: Identify common watchdog API
Many embedded C libraries use WDT_SetTimeoutMs() to set timeout in ms.Step 2: Check syntax correctness
WDT_SetTimeoutMs(5000); correctly sets 5000 ms timeout.Final Answer:
WDT_SetTimeoutMs(5000); -> Option BQuick Check:
Correct function name with ms unit [OK]
- Using incorrect function names
- Assigning timeout to struct without function
- Using generic function names not in API
Given the code below, what will be the watchdog timeout value after execution?
typedef struct {
unsigned int timeout_ms;
} Watchdog;
Watchdog wdt;
void configure_watchdog(unsigned int t) {
wdt.timeout_ms = t;
}
int main() {
configure_watchdog(3000);
configure_watchdog(7000);
return wdt.timeout_ms;
}Solution
Step 1: Trace function calls
First call sets timeout_ms to 3000, second call overwrites it to 7000.Step 2: Check return value
Main returns wdt.timeout_ms, which is 7000 after second call.Final Answer:
7000 -> Option AQuick Check:
Last set timeout = 7000 [OK]
- Assuming first call value remains
- Confusing return value with function return
- Thinking timeout resets automatically
Identify the error in the following watchdog timeout configuration code:
void setup_watchdog() {
int timeout = 2000;
WDT_SetTimeout(timeout);
timeout = 5000;
}Solution
Step 1: Analyze timeout variable usage
Timeout is set to 2000 and passed to WDT_SetTimeout, then changed to 5000 but not applied.Step 2: Understand effect of changing timeout after setting
Changing timeout variable after setting watchdog has no effect on watchdog timeout.Final Answer:
Timeout value is changed after setting watchdog, ineffective -> Option AQuick Check:
Changing variable after call doesn't update watchdog [OK]
- Thinking variable change updates watchdog automatically
- Confusing variable type with function parameter
- Expecting return in void function
You want to configure a watchdog timer that resets the system if it hangs for more than 10 seconds, but also want to avoid false resets during long processing tasks. Which approach is best?
Solution
Step 1: Understand watchdog timeout role
Timeout defines max hang time before reset; too short causes false resets.Step 2: Balance timeout and reset calls
Setting 10 seconds timeout and resetting watchdog during long tasks avoids false resets.Final Answer:
Set timeout to 10 seconds and reset watchdog regularly during long tasks -> Option DQuick Check:
Timeout + regular reset prevents false resets [OK]
- Setting too short timeout causing resets
- Disabling watchdog risking no recovery
- Never resetting watchdog causing reset
Ready to prove it?
Pass the test and this pattern moves unknown
