home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / rbf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  5.8 KB  |  139 lines

  1. ;/* rbf.c - Execute me to compile me with SAS C 5.10
  2. LC -d0 -b1 -cfistq -v -y -j73 rbf.c
  3. Blink FROM LIB:c.o,rbf.o,rbfhandler.o TO rbf LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. ** rbf.c - serial receive buffer full interrupt handler example.
  7. ** Must be linked with assembler handler rbfhandler.o
  8. **
  9. ** To receive characters, this example requires ASCII serial input
  10. ** at your Amiga's current serial hardware baud rate (ie. 9600 after
  11. ** reboot, else last baud rate used)
  12. */
  13.  
  14. #include <exec/execbase.h>
  15. #include <exec/memory.h>
  16. #include <exec/interrupts.h>
  17. #include <resources/misc.h>
  18. #include <hardware/custom.h>
  19. #include <hardware/intbits.h>
  20. #include <dos/dos.h>
  21.  
  22. #include <clib/exec_protos.h>
  23. #include <clib/misc_protos.h>
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27.  
  28. #ifdef LATTICE
  29. int CXBRK(void)  { return(0); }  /* Disable Lattice CTRL/C handling */
  30. void chkabort(void) { return; }  /* really */
  31. #endif
  32.  
  33. #define BUFFERSIZE 256
  34.  
  35. extern void RBFHandler();   /* proto for asm interrupt handler */
  36. void main(void);
  37.  
  38. struct MiscResource *MiscBase;
  39. extern struct ExecBase *SysBase;
  40. extern struct Custom far custom;    /* defined in amiga.lib */
  41.  
  42. static UBYTE *allocname = "rbf-example";
  43.  
  44. struct RBFData {
  45.     struct Task *rd_Task;
  46.     ULONG rd_Signal;
  47.     ULONG rd_BufferCount;
  48.     UBYTE rd_CharBuffer[BUFFERSIZE + 2];
  49.     UBYTE rd_FlagBuffer[BUFFERSIZE + 2];
  50.     UBYTE rd_Name[32];
  51. };
  52.  
  53. void main(void)
  54. {
  55.     struct RBFData *rbfdata;
  56.     UBYTE *currentuser;
  57.     BYTE signr;
  58.     struct Device *serdevice;
  59.     struct Interrupt *rbfint, *priorint;
  60.     BOOL priorenable;
  61.     ULONG signal;
  62.  
  63.     if (MiscBase = OpenResource("misc.resource"))
  64.     {
  65.         currentuser = AllocMiscResource(MR_SERIALPORT, allocname);        /* Allocate the serial */
  66.         if (currentuser)                                                  /* port registers.     */
  67.         {
  68.             printf("serial hardware allocated by %s. Trying to remove it\n",
  69.                    currentuser);                                         /* Hey! someone got it! */
  70.             Forbid();
  71.             if (serdevice = (struct Device *)FindName(&SysBase->DeviceList, currentuser))
  72.                 RemDevice(serdevice);
  73.             Permit();
  74.  
  75.             currentuser = AllocMiscResource(MR_SERIALPORT, allocname);          /* and try again */
  76.         }
  77.         if (currentuser == NULL)
  78.         {                                                                      /* Get the serial */
  79.             currentuser = AllocMiscResource(MR_SERIALBITS, allocname);         /* control bits.  */
  80.             if (currentuser)
  81.             {
  82.                 printf("serial control allocated by %s\n", currentuser);            /* Give up. */
  83.                 FreeMiscResource(MR_SERIALPORT);
  84.             }
  85.             else
  86.             {                                                                  /* Got them both. */
  87.                 printf("serial hardware allocated\n");
  88.                 if ((signr = AllocSignal(-1)) != -1)          /* Allocate a signal bit for the   */
  89.                 {                                             /* interrupt handler to signal us. */
  90.                     if (rbfint = AllocMem(sizeof(struct Interrupt), MEMF_PUBLIC|MEMF_CLEAR))
  91.                     {
  92.                         if (rbfdata = AllocMem(sizeof(struct RBFData), MEMF_PUBLIC|MEMF_CLEAR))
  93.                         {
  94.                             rbfdata->rd_Task = FindTask(NULL);        /* Init rfbdata structure. */
  95.                             rbfdata->rd_Signal = 1L << signr;
  96.  
  97.                             rbfint->is_Node.ln_Type = NT_INTERRUPT;      /* Init interrupt node. */
  98.                             strcpy(rbfdata->rd_Name, allocname);
  99.                             rbfint->is_Node.ln_Name = rbfdata->rd_Name;
  100.                             rbfint->is_Data = (APTR)rbfdata;
  101.                             rbfint->is_Code = RBFHandler;
  102.                                                                         /* Save state of RBF and */
  103.                             priorenable = custom.intenar & INTF_RBF ? TRUE : FALSE; /* interrupt */
  104.                             custom.intena = INTF_RBF;                             /* disable it. */
  105.                             priorint = SetIntVector(INTB_RBF, rbfint);
  106.  
  107.                             if (priorint) printf("replaced the %s RBF interrupt handler\n",
  108.                                                  priorint->is_Node.ln_Name);
  109.                             printf("enabling RBF interrupt\n");
  110.                             custom.intena = INTF_SETCLR | INTF_RBF;
  111.  
  112.                             printf("waiting for buffer to fill up. Use CTRL-C to break\n");
  113.                             signal = Wait(1L << signr | SIGBREAKF_CTRL_C);
  114.  
  115.                             if (signal & SIGBREAKF_CTRL_C) printf(">break<\n");
  116.                             printf("Character buffer contains:\n%s\n", rbfdata->rd_CharBuffer);
  117.  
  118.                             custom.intena = INTF_RBF;               /* Restore previous handler. */
  119.                             SetIntVector(INTB_RBF, priorint);
  120.                                                                   /* Enable it if it was enabled */
  121.                             if (priorenable) custom.intena = INTF_SETCLR|INTF_RBF;    /* before. */
  122.  
  123.                             FreeMem(rbfdata, sizeof(struct RBFData));
  124.                         }
  125.                         else  printf("can't allocate memory for rbf data\n");
  126.                         FreeMem(rbfint, sizeof(struct Interrupt));
  127.                     }
  128.                     else printf("can't allocate memory for interrupt structure\n");
  129.                     FreeSignal(signr);
  130.                 }
  131.                 else printf("can't allocate signal\n");
  132.  
  133.                 FreeMiscResource(MR_SERIALBITS);   /* release serial hardware */
  134.                 FreeMiscResource(MR_SERIALPORT);
  135.             }
  136.         }
  137.     } /* There is no 'CloseResource()' function */
  138. }
  139.