Health monitoring and heartbeat help check if a system or task is working well. It tells us early if something stops or slows down.
Health monitoring and heartbeat in FreeRTOS
void vTaskHeartbeat(void *pvParameters) {
for (;;) {
// Send heartbeat signal or update status
vTaskDelay(pdMS_TO_TICKS(heartbeat_interval_ms));
}
}The heartbeat function usually runs in a FreeRTOS task that loops forever.
Use vTaskDelay() to wait between heartbeats without blocking other tasks.
void vTaskHeartbeat(void *pvParameters) {
for (;;) {
printf("Heartbeat sent\n");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}void vTaskHeartbeat(void *pvParameters) {
for (;;) {
// Toggle an LED to show system is alive
ToggleLED();
vTaskDelay(pdMS_TO_TICKS(500));
}
}This program creates a FreeRTOS task that prints a heartbeat message every 2 seconds. The scheduler runs the task repeatedly.
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void vTaskHeartbeat(void *pvParameters) { for (;;) { printf("Heartbeat: system is alive\n"); vTaskDelay(pdMS_TO_TICKS(2000)); } } int main(void) { xTaskCreate(vTaskHeartbeat, "HeartbeatTask", configMINIMAL_STACK_SIZE, NULL, 1, NULL); vTaskStartScheduler(); return 0; }
Heartbeat intervals should be chosen carefully to balance responsiveness and CPU load.
Use watchdog timers with heartbeat to reset system if heartbeat stops.
Heartbeat can be a simple message, LED toggle, or signal to another system part.
Heartbeat tasks run repeatedly to show system health.
Use vTaskDelay() to wait between heartbeats without blocking.
Heartbeat helps detect failures early and keep systems reliable.
Practice
Solution
Step 1: Understand heartbeat task role
A heartbeat task runs repeatedly to show the system is alive and working properly.Step 2: Eliminate incorrect options
Blocking other tasks or handling user input are not typical heartbeat functions.Final Answer:
To repeatedly indicate the system is running and healthy -> Option BQuick Check:
Heartbeat = system health indicator [OK]
- Thinking heartbeat blocks other tasks
- Assuming heartbeat handles user input
- Believing heartbeat increases CPU load unnecessarily
Solution
Step 1: Identify delay function for non-blocking wait
vTaskDelay() suspends the task for a time, allowing others to run.Step 2: Understand other functions
vTaskSuspend() pauses a task indefinitely, vTaskDelete() removes it, and vTaskResume() restarts a suspended task.Final Answer:
vTaskDelay() -> Option DQuick Check:
Use vTaskDelay() for timed waits [OK]
- Using vTaskSuspend() which blocks indefinitely
- Confusing vTaskDelete() with delay
- Trying vTaskResume() to wait
void vHeartbeatTask(void *pvParameters) {
for (;;) {
printf("Heartbeat\n");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}Solution
Step 1: Analyze the infinite loop
The for(;;) loop runs forever, printing "Heartbeat" each cycle.Step 2: Check the delay function
vTaskDelay(pdMS_TO_TICKS(1000)) pauses the task for 1000 ms (1 second) between prints.Final Answer:
Prints "Heartbeat" every second repeatedly -> Option CQuick Check:
Infinite loop + vTaskDelay(1s) = repeated print every second [OK]
- Thinking it prints only once
- Ignoring the delay causing continuous print
- Assuming syntax error without checking code
void vHeartbeatTask(void *pvParameters) {
while(1) {
printf("Heartbeat\n");
vTaskDelay(1000);
}
}Solution
Step 1: Check vTaskDelay argument type
vTaskDelay() expects delay in ticks, not milliseconds directly.Step 2: Correct usage
Use pdMS_TO_TICKS(1000) to convert 1000 ms to ticks for vTaskDelay.Final Answer:
vTaskDelay() argument should be in ticks, not milliseconds -> Option AQuick Check:
vTaskDelay(ms) wrong, use pdMS_TO_TICKS(ms) [OK]
- Passing milliseconds directly to vTaskDelay()
- Thinking printf() is disallowed in tasks
- Believing while(1) is invalid syntax
Solution
Step 1: Understand timing requirements
LED toggles every 500 ms, logging every 5 seconds require different intervals.Step 2: Evaluate task design options
Two separate tasks allow independent timing without complex counting or blocking.Step 3: Consider drawbacks of other options
Single task with 500 ms delay and counting adds complexity; 5000 ms delay misses LED timing; no delay wastes CPU.Final Answer:
Create two separate tasks: one toggling LED every 500 ms, another logging every 5 seconds -> Option AQuick Check:
Separate tasks for different intervals = clean design [OK]
- Trying to handle all timing in one task
- Using long delays missing faster toggles
- No delays causing CPU overload
Ready to prove it?
Pass the test and this pattern moves unknown
