home *** CD-ROM | disk | FTP | other *** search
/ HTBasic 9.3 / HTBasic 9.3.iso / SICL / data1.cab / sicl32 / c / samples / misc / gpiointr.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-02  |  2.1 KB  |  73 lines

  1. /* gpiointr.c
  2.    This program does the following: 
  3.    - Creates a GPIO session with error checking
  4.    - Installs an interrupt handler and enables EIR interrupts
  5.    - Waits for EIR; invokes the handler for each interrupt
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <sicl.h>
  10.  
  11. void SICLCALLBACK handler(INST id, long reason, long sec)
  12. {
  13.    if (reason == I_INTR_GPIO_EIR) {
  14.       printf("EIR interrupt detected\n");
  15.       
  16.      /* Proper protocol is for the peripheral device to hold
  17.       * EIR asserted until the controller "acknowledges" the
  18.       * interrupt.  The method for acknowledging and/or responding
  19.       * to EIR is very device-dependent.  Perhaps a CTLx line is 
  20.       * pulsed, or data is read, etc.  The response should be
  21.       * executed at this point in the program.
  22.       */
  23.    }
  24.    else
  25.       printf("Unexpected Interrupt; reason=%d\n", reason);
  26. }
  27.  
  28. main()
  29. {
  30.    INST intf;     /* interface session id */
  31.  
  32.    #if defined (__BORLANDC__) && !defined (__WIN32__)
  33.       _InitEasyWin();  /* required for Borland EasyWin programs */
  34.    #endif
  35.  
  36.    /* log message and exit program on error */
  37.    ionerror(I_ERROR_EXIT);
  38.  
  39.    /* open GPIO interface session */
  40.    intf = iopen("gpio");
  41.  
  42.    /* suspend interrupts until configured */
  43.    iintroff();
  44.  
  45.    /* configure interrupts */
  46.    ionintr(intf, handler);
  47.    isetintr(intf, I_INTR_GPIO_EIR, 1);
  48.  
  49.    /* wait for interrupts */
  50.    printf("Ready for interrupts\n");
  51.    while (1) {
  52.       iwaithdlr(0);  /* optional timeout can be specified here */
  53.    }
  54.  
  55.    /* iwaithdlr performs an automatic iintron().  If your program
  56.     * does concurrent processing, instead of waiting, then you need
  57.     * to execute iintron() when you are ready for interrupts.
  58.     */
  59.  
  60.    /* This simplified example loops forever.  Most real applications
  61.     * would have termination conditions that cause the loop to exit.
  62.     */
  63.    iclose(intf);
  64.  
  65.    /* For WIN16 applications, call _siclcleanup before exiting to release
  66.       resources allocated by SICL for this application.  This call
  67.       is a no-op for WIN32 applications.
  68.    */
  69.    _siclcleanup();
  70.  
  71.    return 0;
  72. }
  73.