0
0
FreeRTOSprogramming~20 mins

Critical sections and interrupt disabling in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Critical Section Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested critical sections in FreeRTOS
Consider the following FreeRTOS code snippet. What will be the output printed to the console?
FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>

void exampleFunction() {
    taskENTER_CRITICAL();
    printf("Inside first critical section\n");
    taskENTER_CRITICAL();
    printf("Inside nested critical section\n");
    taskEXIT_CRITICAL();
    printf("Exited nested critical section\n");
    taskEXIT_CRITICAL();
    printf("Exited first critical section\n");
}

int main() {
    exampleFunction();
    return 0;
}
A
Inside first critical section
Inside nested critical section
Exited first critical section
Exited nested critical section
B
Inside first critical section
Exited nested critical section
Inside nested critical section
Exited first critical section
C
Inside first critical section
Inside nested critical section
Exited nested critical section
Exited first critical section
D
Inside nested critical section
Inside first critical section
Exited nested critical section
Exited first critical section
Attempts:
2 left
💡 Hint
Remember that nested critical sections in FreeRTOS use a counter to track entry and exit.
Predict Output
intermediate
2:00remaining
Effect of interrupt disabling on task switching
What will happen if interrupts are disabled globally in FreeRTOS for a long time inside a task?
FreeRTOS
void vTaskFunction(void *pvParameters) {
    portDISABLE_INTERRUPTS();
    // Simulate long processing
    for (volatile int i = 0; i < 1000000; i++) {}
    portENABLE_INTERRUPTS();
    vTaskDelete(NULL);
}
AThe system will block all interrupts, causing no task switching and possible system freeze.
BThe task will yield automatically despite interrupts being disabled.
COnly software interrupts will be blocked, hardware interrupts continue normally.
DThe system will continue running normally with task switching unaffected.
Attempts:
2 left
💡 Hint
Think about what disabling interrupts globally means for the scheduler.
🔧 Debug
advanced
2:00remaining
Identify the error in critical section usage
What error will the following FreeRTOS code cause when run on a Cortex-M microcontroller?
FreeRTOS
void faultyFunction() {
    taskENTER_CRITICAL();
    // Critical code
    taskEXIT_CRITICAL();
    taskEXIT_CRITICAL(); // Extra exit
}
AUndefined behavior due to decrementing critical nesting count below zero.
BStack overflow due to unbalanced critical section exit calls.
CNo error, code runs normally.
DCompile-time error due to extra taskEXIT_CRITICAL call.
Attempts:
2 left
💡 Hint
Check how FreeRTOS tracks critical section nesting internally.
🧠 Conceptual
advanced
2:00remaining
Why use portENTER_CRITICAL instead of taskENTER_CRITICAL?
In FreeRTOS, what is the main difference between portENTER_CRITICAL() and taskENTER_CRITICAL(), and when should portENTER_CRITICAL be used?
AportENTER_CRITICAL disables interrupts globally and is used in ISR context; taskENTER_CRITICAL disables scheduler only and is used in tasks.
BportENTER_CRITICAL disables interrupts at the port level and is used in low-level code; taskENTER_CRITICAL is a wrapper used in tasks.
CportENTER_CRITICAL enables interrupts; taskENTER_CRITICAL disables interrupts.
DThere is no difference; both are aliases and can be used interchangeably.
Attempts:
2 left
💡 Hint
Consider the abstraction layers in FreeRTOS and hardware-specific code.
Predict Output
expert
3:00remaining
Output of interrupt priority masking in critical section
Given the following FreeRTOS code on a Cortex-M with configMAX_SYSCALL_INTERRUPT_PRIORITY set to 5, what is the value of variable x after the critical section?
FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"

volatile int x = 0;

void vTask(void *pvParameters) {
    taskENTER_CRITICAL();
    x = 10;
    taskEXIT_CRITICAL();
}

int main() {
    NVIC_SetPriorityGrouping(3); // Priority grouping
    NVIC_SetPriority(SysTick_IRQn, 6); // Lower priority than max syscall
    NVIC_SetPriority(USART1_IRQn, 4); // Higher priority than max syscall

    x = 0;
    vTask(NULL);
    return x;
}
AUndefined, depends on interrupt timing
B0
C1
D10
Attempts:
2 left
💡 Hint
Recall that taskENTER_CRITICAL disables interrupts up to configMAX_SYSCALL_INTERRUPT_PRIORITY.