The following example demonstrates how to utilize flags to manage the occurrence of multiple distinct interrupts. This approach enables interrupt prioritization and ensures that interrupts do not nest, all without requiring the manual disabling of interrupts. Interrupts are processed sequentially according to their assigned priority, which in the example below corresponds to the flag index (where a lower flag index represents a higher priority).
#include <stdio.h> #include "OS_API.h" HANDLE FlagsHandle; void InterruptHandler(int VectID) { INDEX ChangedFlags; /* Set the flag for specified interrupt number */ osSetFlags(FlagsHandle, (INDEX) (1 << VectID), &ChangedFlags); if(!ChangedFlags) { /* Failure: previous VectID interrupt was not yet processed */ /* ... */ } } ERROR MainTask(PVOID Arg) { int VectID; INDEX Flags; /* Mark parameter as unused */ AR_UNUSED_PARAM(Arg); /* Process received interrupts in order to the interrupt priority */ while(1) { /* Wait for interrupt occurrence */ osWaitForObject(FlagsHandle, OS_INFINITE); /* Get flags state */ osGetFlags(FlagsHandle, &Flags); /* Get the lowest vector ID */ VectID = 0; while(!((Flags >> VectID) & 1) && VectID < 32) VectID++; /* Process the interrupt */ printf("Processing the interrupt #%i.\n", VectID); /* Reset flag state for processed interrupt */ osResetFlags(FlagsHandle, (INDEX) (1 << VectID), NULL); } } int main(void) { int i; /* Initialization */ arInit(); stInit(); osInit(); /* Create objects */ FlagsHandle = osCreateFlags(NULL, 0x00000000); osCreateTask(MainTask, NULL, 0, 0, FALSE); /* Setup the interrupt handler */ /* ... */ /* Start the operating system */ osStart(); /* Deinitialization */ osDeinit(); arDeinit(); return 0; }