Consider the following embedded C code snippet that sets interrupt priorities and triggers interrupts. What will be the printed output?
#include <stdio.h> volatile int interrupt_flag = 0; void ISR_high_priority() { interrupt_flag = 1; printf("High priority interrupt handled\n"); } void ISR_low_priority() { interrupt_flag = 2; printf("Low priority interrupt handled\n"); } int main() { int priority_high = 1; // Higher priority int priority_low = 2; // Lower priority // Simulate interrupts if (priority_low > priority_high) { ISR_high_priority(); ISR_low_priority(); } else { ISR_low_priority(); ISR_high_priority(); } printf("Interrupt flag value: %d\n", interrupt_flag); return 0; }
Think about the order in which the interrupt service routines are called and how the interrupt_flag variable changes.
The code checks if the low priority number is greater than the high priority number. Since 2 > 1, it calls the high priority ISR first, then the low priority ISR. Each ISR sets the interrupt_flag. The last ISR called is the low priority one, so the flag ends with value 2.
In embedded systems, what does a lower numerical interrupt priority level usually mean?
Think about how priority numbers are assigned in many microcontrollers.
In many embedded systems, a lower numerical value for interrupt priority means higher priority. For example, priority level 0 is often the highest priority.
Examine the following code snippet. What error will occur when compiling or running it?
void ISR() {
// Interrupt service routine
int priority = 1;
if(priority < 0) {
printf("Invalid priority\n");
}
}
int main() {
ISR();
return 0;
}Check if all required headers are included for used functions.
The code uses printf but does not include
Given the following code snippet, which option correctly sets the interrupt priority to 3?
void set_interrupt_priority(int priority); int main() { // Set priority here return 0; }
Remember how to call functions with arguments in C.
Option B correctly calls the function with argument 3. Option B tries to assign a value to a function name, which is invalid. Option B uses an assignment inside the argument which is unnecessary and confusing. Option B uses brackets which is invalid syntax for function calls.
In an embedded system, three interrupts have priorities 1, 2, and 3 (1 is highest). The system mask level is set to 2, which blocks interrupts with priority 2 and lower. How many interrupts will be serviced?
Consider which priorities are blocked by the mask level.
The mask level blocks interrupts with priority 2 and lower (meaning priority 2 and 3). Only priority 1 is higher than the mask and will be serviced.