Using Pointer Queues

The following example demonstrates how to utilize a pointer queue to transfer data from a task to a device when an interrupt occurs.

#include <stdio.h>
#include <stdlib.h>
#include "OS_API.h"


HANDLE PtrQueueHandle;


void InterruptHandler(void)
{
  PVOID Data;
  BOOL Success, PrevLockState;

  /* Send data to the device every time, when the interrupt is received and
     data is available */
  Success = osPtrQueuePend(PtrQueueHandle, &Data);
  if(Success)
  {
    /* ... */
  }

  /* Print message */
  PrevLockState = arLock();
  if(Success)
    printf("Data %i is sent to device.\n", (int) Data);
  else
    printf("Queue is empty when interrupt occurs.\n");
  arRestore(PrevLockState);
}


ERROR MainTask(PVOID Arg)
{          
  PVOID Data;         
  BOOL Success, PrevLockState;
  TIME JobTime;

  /* Mark parameter as unused */
  AR_UNUSED_PARAM(Arg);

  /* Initialize random number generator */
  srand((unsigned) arGetTickCount());

  /* Infinite loop */
  while(1)
  {
    /* Perform work of varying length (from 100 up to 800 milliseconds)
       and produce some data*/
    JobTime = 100 + rand() % 701;
    Data = (PVOID) ((int) rand());
    osSleep(JobTime * AR_TICKS_PER_SECOND / 1000);

    /* Store produced data in the queue */
    Success = osPtrQueuePost(PtrQueueHandle, Data);

    /* Print message */
    PrevLockState = arLock();                  
    if(Success)
      printf("Task produces %i after %lu ms and stores it in the queue.\n",
        (int) Data, JobTime);
    else
      printf("Task produces %i after %lu ms, but queue is full.\n",
        (int) Data, JobTime);
    arRestore(PrevLockState);
  }
}


int main(void)
{
  /* Initialization */
  arInit();
  stInit();
  osInit();

  /* Create queue of pointers with buffer for 32 messages */
  PtrQueueHandle = osCreatePtrQueue(NULL, 32);

  /* Create main task */
  osCreateTask(MainTask, NULL, 0, 0, FALSE);

  /* Setup the interrupt handler */
  /* ... */

  /* Start the operating system */
  osStart();

  /* Deinitialization */
  osDeinit();
  arDeinit();
  return 0;
}
SpaceShadow documentation