Using a flags

The following example shows how to use flags to determine the occurrence of a many different interrupts. It allows prioritizing interrupts, and ensures that interrupts are not nesting, without using the interrupts disabling. Interrupts will be executed one by one accordingly to the priority that in the example below is equal to the flag index (the lower flag index is the 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;
}
SpaceShadow documentation