home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d109 / uupc.lha / UUpc / Source / LOCAL / SerialIO.c < prev    next >
C/C++ Source or Header  |  1987-10-28  |  8KB  |  308 lines

  1. /*--------------------------------------------------------------*/
  2. /*    SerialIO.c: Amiga Serial I/O Device Routines        */
  3. /*      Created: June 1986 by J.A. Lydiatt            */
  4. /*                                */
  5. /*    Note:                            */
  6. /*      A Task may only open the serial port once.        */
  7. /*--------------------------------------------------------------*/
  8.  
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <stdio.h>
  12. #include <devices/serial.h>
  13. #include <functions.h>
  14.  
  15. /* Allowable Mode Values */
  16. #define MODEHALF    0
  17. #define MODEFULL    1
  18. #define MODEECHO    2
  19.  
  20. /* Default starting values */
  21. #define STARTBAUD   1200  /* default baud rate */
  22. #define STARTMODE   MODEFULL
  23. #define SERFLAGS    (SERF_SHARED | SERF_XDISABLED)
  24. #define CTLCHAR     0x11130501    
  25.  
  26. static int Mode = STARTMODE;
  27. static int amClosing = FALSE;
  28.  
  29. /* declarations for the serial stuff */
  30. static struct IOExtSer *Read_Request = NULL;
  31. static UBYTE rs_in[2];
  32. static struct IOExtSer *Write_Request = NULL;
  33. static UBYTE rs_out[2];
  34.  
  35. /* stack to save Serial Port flags */
  36. #define MAXSTACK 10
  37. static int stackSize = 0;
  38. static int   modeStack[ MAXSTACK ];
  39. static UBYTE flagStack[ MAXSTACK ];
  40.  
  41.  
  42. /*------------------------------------------------------------*/
  43. /*  GetSerialSigBit: return Read_Request's Signal Bit          */
  44. /*------------------------------------------------------------*/
  45.  
  46. int GetSerialSigBit()
  47. {
  48.    return Read_Request->IOSer.io_Message.mn_ReplyPort->mp_SigBit;
  49. }
  50.  
  51. /*------------------------------------------------------------*/
  52. /*  CheckSerIO : return TRUE if serial port has a character   */
  53. /*------------------------------------------------------------*/
  54.  
  55. BOOL CheckSerIO()
  56. {
  57.    return (BOOL)( CheckIO( Read_Request ) != NULL);
  58. }
  59.  
  60. /*------------------------------------------------------------*/
  61. /*      FlushSerIO: Flush the receive buffer              */
  62. /*------------------------------------------------------------*/
  63.  
  64. void FlushSerIO()
  65. {
  66.    register struct IOExtSer *r = Read_Request;
  67.  
  68.    AbortIO( r );                 /* abort it */
  69.    AbortIO( Write_Request );
  70.  
  71.    r->IOSer.io_Command = CMD_FLUSH;
  72.    DoIO( r );                       /* flush all IO requests */
  73.    r->IOSer.io_Command = CMD_CLEAR;
  74.    DoIO( r);                       /* flush receive buffer  */
  75.  
  76.    if ( !amClosing )
  77.      {
  78.         Read_Request->IOSer.io_Command = CMD_READ;
  79.         BeginIO( r );           /* start receive request */
  80.      }
  81. }
  82.  
  83.  
  84. /*------------------------------------------------------------*/
  85. /*  PushSerState: save current io flags                       */
  86. /*------------------------------------------------------------*/
  87. void PushSerState()
  88. {
  89.    register struct IOExtSer *r = Read_Request; 
  90.  
  91.    if ( stackSize < MAXSTACK )
  92.      {
  93.         /* Save the current Mode */
  94.         modeStack[ stackSize ] = Mode;
  95.         
  96.     /* Get the current flags */
  97.     AbortIO( r );
  98.     flagStack[ stackSize++ ] = r->io_SerFlags;
  99.     BeginIO( r );
  100.      }
  101. }
  102.  
  103. /*------------------------------------------------------------*/
  104. /*  PullSerState: restore last saved flag state              */
  105. /*------------------------------------------------------------*/
  106. void PullSerState()
  107. {
  108.    register struct IOExtSer *r = Read_Request; 
  109.  
  110.    if ( stackSize > 0 )
  111.      {
  112.     /* Reset the Mode */
  113.     Mode = modeStack[ --stackSize ];
  114.  
  115.     /* Set the old flags */
  116.     AbortIO( r );
  117.     r->io_SerFlags = flagStack[ stackSize ];
  118.     r->IOSer.io_Command = SDCMD_SETPARAMS;
  119.     DoIO( r );
  120.     r->IOSer.io_Command = CMD_READ;
  121.     BeginIO( r );
  122.      }
  123. }
  124. /*-------------------------------------------------------------*/
  125. /*           CloseSerialIO: Close the serial port              */
  126. /*-------------------------------------------------------------*/
  127. void CloseSerialIO()
  128. {
  129.    register struct IOExtSer *r = Read_Request;
  130.    register struct IOExtSer *w = Write_Request;
  131.  
  132.    if ( r != NULL )
  133.      {
  134.     amClosing = TRUE;
  135.     CloseDevice( r );
  136.     DeletePort( r->IOSer.io_Message.mn_ReplyPort );
  137.     FreeMem( r, (long)sizeof( struct IOExtSer ) );
  138.     Read_Request = NULL;
  139.       }
  140.  
  141.     if ( w != NULL )
  142.       {
  143.     CloseDevice( w );
  144.     DeletePort( w->IOSer.io_Message.mn_ReplyPort );
  145.     FreeMem( w, (long)sizeof( struct IOExtSer ) );
  146.     Write_Request = NULL;
  147.       }
  148. }
  149. /*-------------------------------------------------------------*/
  150. /* InitSerialIO: Open serial IO - return read Port           */
  151. /*-------------------------------------------------------------*/    
  152. struct IOExtSer *InitSerialIO()
  153. {
  154.    register struct IOExtSer *r, *w;
  155.  
  156.    if ( Read_Request != NULL )
  157.       {
  158.     fprintf( stderr, "Error: Serial Port already open for read.\n");
  159.     return NULL;
  160.       }
  161.    r = (struct IOExtSer *) AllocMem( (long)sizeof(struct IOExtSer),
  162.      (long)(MEMF_PUBLIC|MEMF_CLEAR));
  163.    if (r == NULL)
  164.     return NULL;
  165.  
  166.    Read_Request = r;
  167.    r->io_SerFlags = SERFLAGS;
  168.    r->io_CtlChar  = CTLCHAR;
  169.    r->IOSer.io_Message.mn_ReplyPort = CreatePort("Read_RS",NULL);
  170.    if(OpenDevice(SERIALNAME,NULL,r,NULL))
  171.       {
  172.      fprintf( stderr, "Can't open Read device\n");
  173.     goto q4;
  174.       }
  175.  
  176.    r->io_Baud = STARTBAUD;
  177.    r->io_ReadLen = 8;
  178.    r->io_WriteLen = 8;
  179.    r->IOSer.io_Command = SDCMD_SETPARAMS;
  180.    DoIO(r);
  181.    r->IOSer.io_Command = CMD_READ;
  182.    r->IOSer.io_Length = 1;
  183.    r->IOSer.io_Data = (APTR)&rs_in[0];
  184.  
  185.  
  186.    if ( Write_Request != NULL )
  187.       {
  188.     fprintf( stderr, "Error: Serial Port already open for writing.\n");
  189.     goto q3;
  190.       }
  191.    w = (struct IOExtSer *)AllocMem( (long)sizeof( struct IOExtSer ),
  192.       (long)MEMF_PUBLIC|MEMF_CLEAR);
  193.    if (w == NULL) goto q3;
  194.  
  195.    Write_Request = w;
  196.    w->io_SerFlags = SERFLAGS;
  197.    w->io_CtlChar  = CTLCHAR;
  198.    w->IOSer.io_Message.mn_ReplyPort = CreatePort("Write_RS",NULL);
  199.    if(OpenDevice(SERIALNAME,NULL,w,NULL))
  200.       {
  201.     fprintf( stderr, "Can't open Write device\n");
  202.     goto q1;
  203.       }
  204.    w->io_Baud = STARTBAUD;
  205.    w->io_ReadLen = 8;
  206.    w->io_WriteLen = 8;
  207.    w->IOSer.io_Command = SDCMD_SETPARAMS;
  208.    DoIO(w);
  209.  
  210.    w->IOSer.io_Command = CMD_WRITE;
  211.    w->IOSer.io_Length = 1;
  212.    w->IOSer.io_Data = (APTR)&rs_out[0];
  213.  
  214.    BeginIO( r );
  215.    stackSize = 0;
  216.  
  217.    return r;
  218.  
  219. q1:   DeletePort( w->IOSer.io_Message.mn_ReplyPort );
  220. q2:   FreeMem( w, (long)sizeof( *w) );
  221. q3:   CloseDevice( r );
  222. q4:   DeletePort( r->IOSer.io_Message.mn_ReplyPort );
  223. q5:   FreeMem( r, (long)sizeof( *r) );
  224.  
  225.    return NULL;
  226. }
  227.  
  228. /*----------------------------------------------------------*/
  229. /*         SetXonMode: set Xon On or Off                    */
  230. /*----------------------------------------------------------*/
  231.  
  232. void SetXonMode( status )
  233. BOOL status;
  234. {
  235.  
  236.    register UBYTE flags;
  237.    register struct IOExtSer *r = Read_Request; 
  238.  
  239.    /* Get the current flags */
  240.    AbortIO( r );
  241.    flags = r->io_SerFlags;
  242.  
  243.    if ( status )
  244.       flags &= ~(SERF_XDISABLED);
  245.    else
  246.       flags |= SERF_XDISABLED; 
  247.    r->io_SerFlags = flags;
  248.    r->IOSer.io_Command = SDCMD_SETPARAMS;
  249.    DoIO( r );
  250.    r->IOSer.io_Command = CMD_READ;
  251.    BeginIO( r );
  252. }
  253.  
  254. /*----------------------------------------------------------*/
  255. /*         SetSerBaud: set Serial Baud Rate                 */
  256. /*----------------------------------------------------------*/
  257.  
  258. void SetSerBaud( baud )
  259. int baud;
  260. {
  261.    register struct IOExtSer *r = Read_Request; 
  262.  
  263.    /* Get the current flags */
  264.    AbortIO( r );
  265.    r->io_Baud = baud;
  266.    r->IOSer.io_Command = SDCMD_SETPARAMS;
  267.    DoIO( r );
  268.    r->IOSer.io_Command = CMD_READ;
  269.    BeginIO( r );
  270. }
  271.  
  272. /*----------------------------------------------------------*/
  273. /*         SetSerMode: set the Serial Mode            */
  274. /*----------------------------------------------------------*/
  275.  
  276. void SetSerMode( mode )
  277. int mode;
  278. {
  279.    Mode = mode;
  280. }
  281.  
  282. /*----------------------------------------------------------*/
  283. /*   SerIOWrite: Write a byte out the serial port (no echo) */
  284. /*----------------------------------------------------------*/
  285.  
  286. void SerIOWrite( c )
  287. register UBYTE c;
  288. {
  289.   *rs_out = c;
  290.   DoIO( Write_Request );
  291. }
  292.  
  293. /*----------------------------------------------------------*/
  294. /* SerIORead: read a byte from the serial port (no echo)    */
  295. /*----------------------------------------------------------*/
  296.  
  297. UBYTE SerIORead()
  298. {
  299.    register struct IOExtSer *r = Read_Request;
  300.    register UBYTE c;
  301.  
  302.    WaitIO( r );
  303.    c = *rs_in;
  304.    BeginIO( r );
  305.    return c;
  306. }
  307.  
  308.