Interrupt Management and Port IO Calls Programming Example

The following programming example demonstrates the use of RTX interrupt management and port I/O calls.

// Generate an interrupt
// with the digital outs
// Jumper any digital out [3-10] to IR INPUT [1]
// Jumper GND [11] to IR ENABLE [2]
#include <windows.h>
#include <rtapi.h>
#define CT_PORT ((unsigned char *) 0x340)
#define CT_PORT_9513DATA         (CT_PORT + 0)
#define CT_PORT_9513CMD          (CT_PORT + 1)
#define CT_PORT_9513STATUS       (CT_PORT + 1)
#define CT_PORT_DI               (CT_PORT + 2)
#define CT_PORT_DO               (CT_PORT + 3)
#define CT_PORT_SPACE (CT_PORT_DO - CT_PORT_9513DATA + 1)
#define CT_BASE_PORT_9513DATA(BASE) ((unsigned char *)((BASE) + 0))
#define CT_BASE_PORT_9513CMD(BASE) ((unsigned char *)((BASE) + 1))
#define CT_BASE_PORT_9513STATUS(BASE) ((unsigned char *)((BASE) + 1))
#define CT_BASE_PORT_DI(BASE) ((unsigned char *)((BASE) + 2))
#define CT_BASE_PORT_DO(BASE) ((unsigned char *)((BASE) + 3))
int ihc = 0 ;
int ihc2 = 0 ;
int limit = 30 ;
int TestIncrease = 1 ;
void
RTFCNDCL
InterruptHandler(void * unused)
{
      RtWritePortUchar(CT_BASE_PORT_DO(CT_PORT), 0x00) ;
      ihc++;
}
void 
_cdecl
main(
      int argc,
      char **argv,
      char **envp
      )
{
      HANDLE hInterrupt ;
      int i ;
      RtPrintf("Simple Interrupt Attach Handler Functional Test") ;
      // Enable CT range of ports
      if ( !RtEnablePortIo(CT_PORT, CT_PORT_SPACE))
      {
            (void)RtPrintf("RtEnablePortIo(0x%x, 0x%x) failed\n",
            CT_PORT, CT_PORT_SPACE);
            ExitProcess(1);
      }
      // Attach the interupt vector to the interrupt handler
      hInterrupt = RtAttachInterruptVectorEx(NULL,
                                                   0,
                                                   InterruptHandler,
                                                   (PVOID)&TestIncrease,
                                                   1,
                                                   1,
                                                   0,
                                                   7,
                                                   7,
                                                   FALSE,
                                                   LevelSensitive,
                                                   NULL);
      if ( NULL == hInterrupt )
      {
            RtPrintf("Could not register interrupt handler (%d)\n", GetLastError()) ;
            ExitProcess(2);
      }
      for(i=0; i<limit; i++)
      {
            Sleep;
            // Pulse the interrupt
            RtWritePortUchar(CT_BASE_PORT_DO(CT_PORT), 0xff) ;
      }
      // Determine what happened.
      if ( limit != ihc ) 
      {
            RtPrintf("FAIL\n") ;
      }
      else
      {
            RtPrintf("Got %d interrupts\n", limit) ;
      }
      RtWritePortUchar(CT_BASE_PORT_DO(CT_PORT), 0) ;

      ExitProcess(0);
}

APIs Referenced

RTAPI

Supported Windows API

See Also

Interrupts

Port IO