/* * Version : @(#)serio.c 1.10 */ /************************************************************************** ** * ** 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 C166. * ** 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 : 2001 TASKING * ** * **************************************************************************/ #include #include "main.h" #include "asc0.h" #include "serio.h" void init_serio( void ) { ASC0_vInit(); } /* * Read character from serial channel */ int getch( void ) { int c = EOF; while(ASC0_RIC_IR == 0) { /* wait */ ; } ASC0_RIC_IR = 0; // reset receive interrupt request c = ASC0_RBUF; // return receive buffer register return ( c ); } /* * Return 1 if character available, otherwise 0 */ int kbhit( void ) { if ( ASC0_RIC_IR != 0 ) return ( 1 ); return ( 0 ); } /* * Write character to serial channel */ int putch( int c ) { ASC0_TBUF=c; while(!ASC0_TIC_IR) { _nop; } ASC0_TIC_IR=0; return ( c ); } int _ioread( int fd ) { /* 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 ( fd == 0 ) { /* * 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, 1 ); } return( c ); /* return read character on succes */ } int _iowrite( int c, int fd ) { /* 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 ( fd == 1 || fd == 2 ) { if ( c == '\n' ) putch( '\r' ); putch( c ); } return( c ); /* return written character on success */ } /************************************************************************* * * * DESCRIPTION : Reads a block of characters from the given stream * * returns nr of read characters * * * *************************************************************************/ size_t _read( int fd, char * base, size_t size ) { int c; size_t cnt = 0; /* 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 */ /* from the required hardware. */ /* when nothing is read, '0' should be returned, meaning EOF */ /* is found */ /* On any other error, the routine should return non zero */ /* Note that when a read is done on 'stdin', this routine */ /* should do the scanning, and whenever neccessary, do the */ /* echoing to stdout */ for( ; size; size--,cnt++ ) { /* _ioread will echo to stdout when neccessary */ if( (c = _ioread( fd )) == EOF ) break; /* Nothing more read */ *base++ = c; } return( cnt ); } /************************************************************************* * * * DESCRIPTION : Write a block of data to the given file * * As a working example, this routine just calls the low * * level routine to write one character, but a faster * * implementation is often possible. * * returns 0 on success, nonzero on error * * * * This routine should be customised. * * * *************************************************************************/ size_t _write( int fd, char * base, size_t size ) { size_t cnt = 0; for( ; size--; base++,cnt++ ) { /* Each character must be correctly written */ if( _iowrite( *base, fd ) != *base ) break; } return( cnt ); /* nr of characters correctly written */ }