home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / SEASON7A.LHA / async.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  6.2 KB  |  211 lines

  1. /*
  2. compile: Lc -cfistq -j73 $*
  3. */
  4.  
  5. #include <exec/types.h>
  6. #include <devices/serial.h>
  7. #include <exec/devices.h>
  8. #include <exec/errors.h>
  9. #include <exec/exec.h>
  10. #include <exec/io.h>
  11. #include <exec/ports.h>
  12. #include <exec/semaphores.h>
  13.  
  14. #include <proto/exec.h>
  15.  
  16. #include "async.h"
  17.  
  18. static struct MsgPort *MsgPort = NULL;
  19. static struct IOExtSer *IOExtSer = NULL;
  20. static long   DeviceError = 0x00ff;
  21. /*
  22. ;-----------------------------------------------------------------------------
  23. ; AsyncInit            Initalize Serial Port and install ISR
  24. ;-----------------------------------------------------------------------------
  25. ;    int    AsyncInit( int port)
  26. ;
  27. ;    Where Port is
  28. ;        0  =  COM1
  29. ;        1  =  COM2
  30. ;        2  =  COM3
  31. ;        3  =  COM4
  32. ;
  33. ;-----------------------------------------------------------------------------
  34. */
  35. int    AsyncInit( int port)
  36.     {
  37.     if (MsgPort = CreatePort(NULL, 0L))
  38.         {
  39.         if(IOExtSer = (struct IOExtSer *)CreateExtIO(MsgPort,sizeof(struct IOExtSer)))
  40.                         {
  41.                         IOExtSer->io_Baud=9600;
  42.                         IOExtSer->io_SerFlags=SERF_XDISABLED;
  43.                         DeviceError=OpenDevice("serial.device",0L,IOExtSer,0L);
  44.                         }
  45.                 }
  46.         return DeviceError;
  47.         };
  48.  
  49. /*
  50. ;-----------------------------------------------------------------------------
  51. ;  AsyncStop                Uninstall ISR
  52. ;-----------------------------------------------------------------------------
  53. ;    void  AsyncStop( void)
  54. ;-----------------------------------------------------------------------------
  55. */
  56. void  AsyncStop( void)
  57.         {
  58.     if (DeviceError == 0) CloseDevice(IOExtSer);
  59.     if (IOExtSer) DeleteExtIO(IOExtSer);
  60.     if (MsgPort) DeletePort(MsgPort);
  61.         };
  62. /*
  63. ;-----------------------------------------------------------------------------
  64. ;    AsyncIn            Gets a byte from the input buffer
  65. ;-----------------------------------------------------------------------------
  66. ;    int    AsyncIn( void)
  67. ;-----------------------------------------------------------------------------
  68. */
  69. int    AsyncIn( void)
  70.         {
  71.         char data=0x00;
  72.  
  73.         IOExtSer->IOSer.io_Command=CMD_READ;
  74.         IOExtSer->IOSer.io_Flags=NULL;
  75.         IOExtSer->IOSer.io_Length=1;
  76.         IOExtSer->IOSer.io_Data=&data;
  77.  
  78.         DoIO(IOExtSer);
  79.         return (int)data;
  80.         };
  81.  
  82. /*
  83. ;-----------------------------------------------------------------------------
  84. ;    AsyncOut        Output a byte
  85. ;-----------------------------------------------------------------------------
  86. ;    void    AsyncOut( int c)
  87. ;-----------------------------------------------------------------------------
  88. */
  89. void    AsyncOut(int c)
  90.         {
  91.         char d;
  92.         d = (char)c;
  93.         IOExtSer->IOSer.io_Command=CMD_WRITE;
  94.         IOExtSer->IOSer.io_Flags=NULL;
  95.         IOExtSer->IOSer.io_Length=1;
  96.         IOExtSer->IOSer.io_Data=&d;
  97.         DoIO(IOExtSer);
  98.         };
  99.  
  100. /*
  101. ;-----------------------------------------------------------------------------
  102. ;    AsyncSet        Set communication paramaters
  103. ;-----------------------------------------------------------------------------
  104. ;    void    AsyncSet( int Baud, int Control)
  105. ;
  106. ;    Baud = 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 28800, 38400, 57600
  107. ;    Control = The valure to place in the LCR
  108. ;-----------------------------------------------------------------------------
  109. */
  110. void    AsyncSet( int Baud, int Control)
  111.  
  112.         {
  113.         IOExtSer->io_Baud=9600;
  114.  
  115.                 IOExtSer->io_ReadLen=8;
  116.                 IOExtSer->io_WriteLen=8;
  117.                 IOExtSer->io_StopBits=2;
  118.  
  119.         if(Control&ODD_PARITY)
  120.                 {
  121.                 if(Control&0x04)
  122.                         {
  123.                         IOExtSer->io_SerFlags |= SERF_PARTY_ON;
  124.                         IOExtSer->io_SerFlags &= ~SERF_PARTY_ODD;
  125.                         }
  126.                 else
  127.                         {
  128.                         IOExtSer->io_SerFlags |= SERF_PARTY_ON;
  129.                         IOExtSer->io_SerFlags |= SERF_PARTY_ODD;
  130.                         };
  131.                 }
  132.         else
  133.                 {
  134.                 IOExtSer->io_SerFlags &= ~SERF_PARTY_ON;
  135.                 };
  136.  
  137.         IOExtSer->IOSer.io_Command=SDCMD_SETPARAMS;
  138.         IOExtSer->io_ExtFlags = NULL;
  139.  
  140.         DoIO(IOExtSer);
  141.         };
  142.  
  143. /*
  144. ;-----------------------------------------------------------------------------
  145. ;    AsyncInStat        Returns the # of characters in buffer
  146. ;-----------------------------------------------------------------------------
  147. ;    int    AsyncInStat( void)
  148. ;-----------------------------------------------------------------------------
  149. */
  150. int    AsyncInStat( void)
  151.         {
  152.         IOExtSer->IOSer.io_Command=SDCMD_QUERY;
  153.         IOExtSer->IOSer.io_Flags=NULL;
  154.         DoIO(IOExtSer);
  155.         return (int)(IOExtSer->IOSer.io_Actual);
  156.         };
  157.  
  158. /*
  159. ;-----------------------------------------------------------------------------
  160. ;    AsyncHand        Sets various handshaking lines
  161. ;-----------------------------------------------------------------------------
  162. ;    void    AsyncHand( int Hand)
  163. ;-----------------------------------------------------------------------------
  164. */
  165. void    AsyncHand( int Hand)
  166.         {
  167.         };
  168.  
  169. /*
  170. ;-----------------------------------------------------------------------------
  171. ;    AsyncStat        Returns Async/Modem status
  172. ;-----------------------------------------------------------------------------
  173. ;    unsigned    AsyncStat( void)
  174. ;
  175. ;    MSR is returned in the high byte, LSR in the low byte
  176. ;-----------------------------------------------------------------------------
  177. */
  178. unsigned    AsyncStat( void)
  179.  
  180.         {
  181.         IOExtSer->IOSer.io_Command=SDCMD_QUERY;
  182.         IOExtSer->IOSer.io_Flags=NULL;
  183.         DoIO(IOExtSer);
  184.         if(IOExtSer->io_Status & (1<<5))
  185.                 {
  186.                 return (unsigned)0;
  187.                 }
  188.         else
  189.                 {
  190.                 return (unsigned)DCD;
  191.                 };
  192.         };
  193.  
  194.  
  195. /*
  196. ;-----------------------------------------------------------------------------
  197. ; AsyncClear            Empty the recieve buffer
  198. ;-----------------------------------------------------------------------------
  199. ; void    AsyncClear( void);
  200. ;
  201. ;
  202. ;-----------------------------------------------------------------------------
  203. */
  204.  
  205. void    AsyncClear( void)
  206.         {
  207.         IOExtSer->IOSer.io_Command=CMD_CLEAR;
  208.         IOExtSer->IOSer.io_Flags=NULL;
  209.         DoIO(IOExtSer);
  210.         };
  211.