home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 019.lha / Aterm / SerialIO.c < prev    next >
C/C++ Source or Header  |  1986-11-10  |  8KB  |  324 lines

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