Consider a microcontroller project that needs to handle multiple tasks like sensor reading, communication, and user interface. Why might using an RTOS be better than bare-metal programming?
Think about how multitasking is handled and what RTOS provides automatically.
RTOS manages multiple tasks by switching between them quickly, giving the illusion of simultaneous execution. Bare-metal requires manual task management.
Given two FreeRTOS tasks with different priorities, what will be printed to the console?
void Task1(void *pvParameters) {
for (;;) {
printf("Task1 running\n");
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void Task2(void *pvParameters) {
for (;;) {
printf("Task2 running\n");
vTaskDelay(pdMS_TO_TICKS(50));
}
}
int main() {
xTaskCreate(Task1, "Task1", 1000, NULL, 1, NULL);
xTaskCreate(Task2, "Task2", 1000, NULL, 2, NULL);
vTaskStartScheduler();
return 0;
}Higher priority tasks run before lower priority ones and can preempt them.
Task2 has higher priority (2) than Task1 (1), so Task2 runs whenever ready, preempting Task1.
This bare-metal code tries to run two tasks by calling their functions in a loop. What is the main issue?
void taskA() {
while(1) {
// do something
}
}
void taskB() {
while(1) {
// do something else
}
}
int main() {
while(1) {
taskA();
taskB();
}
return 0;
}Consider what happens when a function has an infinite loop.
taskA has an infinite loop and never returns, so taskB is never reached or executed.
Choose the correct syntax to create a FreeRTOS task named "MyTask" with priority 3.
Check the order of parameters: function pointer, name, stack size, parameters, priority, handle.
The correct order is: task function, name string, stack size, parameters, priority, task handle pointer.
In a system with multiple time-critical tasks, how does using an RTOS improve responsiveness over bare-metal programming?
Think about how RTOS handles task priorities and switching.
RTOS preemptive scheduling allows immediate switching to higher priority tasks, improving response time.