/* * Version : @(#)serio.c 1.4 10/26/94 */ /************************************************************************** ** * ** FILE : serio.c * ** * ** DESCRIPTION : Implementation of _ioread() and _iowrite() * ** using the low level I/O functions getch(), putch(), * ** kbhit() and init_serio() working with serial * ** channel 0 or channel 1 of the SAB 80C166. * ** Default is serial channel 0 used for I/O. * ** If serial channel 1 is wanted for I/O compile * ** "serio.c" with the -DSER_PORT_1 option. * ** * ** COPYRIGHT : 1993 Tasking Software B.V., Amersfoort * ** * **************************************************************************/ #include #include #include "serio.h" /* S0/1 port: * 8 bit, asynchronuous operation, one stopbit, * receive enable, no parity/frame/overrun check * baud rate generator enable */ #define SXCON_MODE 0x8011 #define BAUDRATE 0x003f /* 9600 Baud using 39.3216 MHz */ void init_serio( void ) { _bfld( DP3, MSK_TDX_RDX, DP3_TDX_RDX ); /* direction bits */ _putbit( 1, P3, P3_TXD ); /* enable TXD0/TXD1 output */ SXBG = BAUDRATE; SXTIC = 0; /* clear errorflags */ SXRIC = 0; SXEIC = 0; SXTIR = 1; SXCON = SXCON_MODE; } /* * Read character from serial channel */ int getch( void ) { int c = EOF; if ( SXEIR ) { SXPE = 0; SXFE = 0; SXEIR = 0; SXRIR = 0; } else if ( SXRIR ) { c = SXRBUF & 0x7F; SXRIR = 0; } return ( c ); } /* * Return 1 if character available, otherwise 0 */ int kbhit( void ) { if ( SXRIR ) return ( 1 ); return ( 0 ); } /* * Write character to serial channel */ int putch( int c ) { while ( ! SXTIR ) ; SXTIR = 0; SXTBUF = c; return ( c ); } int _ioread( FILE * fin ) { /* At this point, it is known that 'fin' is a file opened for */ /* reading, and is not in error yet. */ /* This routine should read the input for the given filehandle */ /* (in the standard version only 'stdin') from the required */ /* hardware. */ /* Whenever an error occurs, 'EOF' should be returned */ /* and fin->_flag should be set to the proper error value */ /* i.e. _IOEOF on end of FILE */ /* _IOERR on any other error */ /* Note that when a read is done on 'stdin', this routine */ /* should do the scanning, and whenever neccessary, do the */ /* echoing to stdout */ int c = EOF; if ( fin == stdin ) { /* * Assume terminal sending either CR or CRLF. Allow * only LF as valid delimiter, so translate CR into LF * and ignore received LF's */ do /* blocking read from serial channel 0 */ { c = getch(); /* without echo */ if ( c == 0x0a ) /* LF */ c = EOF; /* ignore it */ else if ( c == 0x0d ) /* CR */ c = 0x0a; /* turn into LF */ } while ( c < 0 ); /* * when reading 'stdin', echo to 'stdout' * performing LF ==> CR-LF translation */ _iowrite( c, stdout ); } return( c ); /* return read character on succes */ } int _iowrite( int c, FILE * fout ) { /* At this point, it is known that 'fout' is a file opened for */ /* writing, and is not in error yet. */ /* This routine should write the output for the given filehandle*/ /* (in the standard version only 'stdout' or 'stderr') */ /* to the required hardware. */ /* Whenever an error occurs, 'EOF' should be returned */ /* and fout->_flag should be set to the proper error value */ /* i.e. _IOEOF on end of FILE */ /* _IOERR on any other error */ if ( fout == stdout || fout == stderr ) { if ( c == '\n' ) putch( '\r' ); putch( c ); } return( c ); /* return written character on success */ }