⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rt_com.c

📁 fsmlabs的real time linux的内核
💻 C
📖 第 1 页 / 共 2 页
字号:
/** rt_com *  ====== * * RT-Linux kernel module for communication across serial lines. * * Copyright (C) 1997 Jens Michaelsen * Copyright (C) 1997-2000 Jochen K黳per * Copyright (C) 1999 Hua Mao <hmao@nmt.edu> * Copyright (C) 1999 Roberto Finazzi * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file License. if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307, USA. */#ifndef RTLINUX_V3#define __RT__#define __KERNEL__#define MODULE#endif#include <linux/config.h>#include <linux/errno.h>#include <linux/ioport.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/version.h>#include <asm/system.h>#include <asm/io.h>#if defined RTAI#  include <rtai.h>#elif defined RTLINUX_V1#  include <rtl_sync.h>#  include <rt_irq.h>#  include <rt_time.h>#elif defined RTLINUX_V3#  include <rtl_conf.h>#  include <rtl_core.h>#  include <rtl_sync.h>#else#  include <rtl.h>#  include <rtl_core.h>#  include <rtl_sync.h>#  include <rtl_time.h>#  include <arch/rt_irq.h>#endif#include "rt_com.h"#include "rt_comP.h"#define RT_COM_NAME "rt_com"/* amount of free space on input buffer for RTS reset */#define RT_COM_BUF_LOW  RT_COM_BUF_SIZ / 3/* amount of free space on input buffer for RTS set */#define RT_COM_BUF_HI   RT_COM_BUF_SIZ * 2 / 3/* amount of free space on input buffer for buffer full error */#define RT_COM_BUF_FULL 20/** Number and descriptions of serial ports to manage.  You also need * to create an ISR ( rt_comN_isr() ) for each port.  */#define RT_COM_CNT 2/** Default: mode=0 - DSR needed on TX, no hw flow control *           used=0 - port and irq setting by rt_com_set_param. If you want to work like *                    a standard rt_com you can set used=1. */struct rt_com_struct rt_com_table[ RT_COM_CNT ] ={    { 0, RT_COM_BASE_BAUD, 0x3f8, 4, RT_COM_STD_COM_FLAG, rt_com0_isr, 1, 1 },  /* ttyS0 - COM1 */    { 0, RT_COM_BASE_BAUD, 0x2f8, 3, RT_COM_STD_COM_FLAG, rt_com1_isr, 1, 1 }   /* ttyS1 - COM2 */};/** Remaining free space of buffer * * @return amount of free space remaining in a buffer (input or output) * * Note that, although the buffer is RT_COM_BUF_SIZ bytes long, it can * only hold RT_COM_BUF_SIZ - 1 bytes. */#define rt_com_buff_free( head, tail ) ( RT_COM_BUF_SIZ - 1 - ( ( head - tail ) & ( RT_COM_BUF_SIZ - 1 ) ) )/** Enable hardware fifo buffers. * * This requires an 16550A or better ! * * @param base    Port base address to work on. * @param trigger Bytecount to buffer before an interrupt. * @param clr     Clear fifos: 0= no reset, 2= RCVR fifo reset, 4= XMIT fifo reset * * @author Jochen K黳per * @version 1999/07/20 */static inline void rt_com_enable_fifo( int base, int trigger, int clr ){    switch ( trigger ) {    case  0:	outb( 0x00, base + RT_COM_FCR );	break;    case  1:	outb( 0x01 | clr, base + RT_COM_FCR );	break;    case  4:	outb( 0x41 | clr, base + RT_COM_FCR );	break;    case  8:	outb( 0x81 | clr, base + RT_COM_FCR );	break;    case 14:	outb( 0xC1 | clr, base + RT_COM_FCR );	break;    default:	break;    }}/** Returns amount of free space remaining in the output buffer. * * @param com    Port to use; corresponding to internal numbering scheme. * @return       Number of bytes or -1 on error * * @author Roberto Finazzi * @version 1999/10/31 */int rt_com_bout_free( unsigned int com ){    if( com < RT_COM_CNT ) {	struct rt_com_struct *p = &( rt_com_table[ com ] );	struct rt_buf_struct *b = &( p->obuf );	if( p->used > 0 )	    return( rt_com_buff_free( b->head, b->tail ) );    }    return( -1 );}/** Clear input buffer. * * @param com    Port to use; corresponding to internal numbering scheme. * @return       -1 on error or 0 if all right * * @author Roberto Finazzi * @version 1999/10/31 */int rt_com_clr_in( unsigned int com ){    if( com < RT_COM_CNT ) {	struct rt_com_struct *p = &( rt_com_table[ com ] );	struct rt_buf_struct *b = &( p->ibuf );	long state;	if( p->used > 0 ) {	    rt_com_irq_off( state );	    b->tail = b->head;	    if( RT_COM_FIFO_TRIGGER > 0)		rt_com_enable_fifo(p->port, RT_COM_FIFO_TRIGGER, 0x02);	    rt_com_irq_on( state );	    if( p->mode & 0x02 ) { /* if hardware flow set RTS */		p->mcr |= 0x02;		outb( p->mcr, p->port + RT_COM_MCR );	    }	    return( 0 );	}    }    return ( -1 );}/** Clear output buffer. * * @param com    Port to use; corresponding to internal numbering scheme. * @return       -1 on error or 0 if all right * * @author Roberto Finazzi * @version 1999/10/31 */int rt_com_clr_out( unsigned int com ){    if( com < RT_COM_CNT ) {	struct rt_com_struct *p = &( rt_com_table[ com ] );	struct rt_buf_struct *b = &( p->obuf );	long state;	if( p->used > 0 ) {	    rt_com_irq_off( state );	    p->ier &= ~0x02;	    outb ( p->ier, p->port+RT_COM_IER);	    b->tail = b->head;	    if ( RT_COM_FIFO_TRIGGER > 0)		rt_com_enable_fifo( p->port, RT_COM_FIFO_TRIGGER, 0x04 );	    rt_com_irq_on( state );	    return( 0 );	}    }    return (-1);}/** Set functioning mode. * * @param com    Port to use; corresponding to internal numbering scheme. * @param mode   functioning mode * @return       -1 on error or 0 if all right * * @author Roberto Finazzi * @version 1999/10/31 */int rt_com_set_mode( unsigned int com, int mode ){    if( com < RT_COM_CNT ) {	struct rt_com_struct *p = &( rt_com_table[ com ] );	if( p->used > 0 ) {	    p->mode = mode;	    if( p->used & 0x02 ) { /* if com setup done */		if( mode & 0x01 )		    p->ier &= ~0x08; /* if no hs signals disable modem interrupts */		else		    p->ier |= 0x08;  /* else enable it */		outb( p->ier, p->port + RT_COM_IER );	    }	    return( 0 );	}    }    return( -1 );}/** Set output signal for modem control (DTR, RTS). * * @param com    Port to use; corresponding to internal numbering scheme. * @param signal Output signals: DTR or RTS. * @param value  Status: 0 or 1. * @return       -1 on error or 0 if all right * * @author Roberto Finazzi * @version 1999/10/31 */int rt_com_wr_modem( unsigned int com, int signal, int value ){    if( com < RT_COM_CNT ) {	struct rt_com_struct *p = &( rt_com_table[ com ] );	if (p->used > 0) {	    if( value )		p->mcr |= signal;	    else		p->mcr &= ~signal;	    outb( p->mcr, p->port+RT_COM_MCR );	    return( 0 );	}    }    return( -1 );}/** Read input signal from modem (CTS, DSR, RI, DCD). * * @param com    Port to use; corresponding to internal numbering scheme. * @param signal Input signals: CTS, DSR, RI, DCD or any combination. * @return       -1 on error or input signal status (0 or 1) as follows: *               bit4 = CTS; bit5 = DSR; bit6 = RI; bit7 = DCD. * * @author Roberto Finazzi * @version 1999/10/31 */int rt_com_rd_modem( unsigned int com, int signal ){    if( com < RT_COM_CNT ) {	struct rt_com_struct *p = &( rt_com_table[ com ] );	if( 0 < p->used ) {	    return( inb( p->port + RT_COM_MSR ) & signal );	}    }    return( -1 );}/** Return last error detected. * * @param com    Port to use; corresponding to internal numbering scheme. * @return       bit 0 :1 = Input buffer overflow *               bit 1 :1 = Receive data overrun *               bit 2 :1 = Parity error *               bit 3 :1 = Framing error *               bit 4 :1 = Break detected * * @author Roberto Finazzi * @version 1999/10/31 */int rt_com_error( unsigned int com ){    int tmp = 0;    if( com < RT_COM_CNT ) {	struct rt_com_struct *p = &( rt_com_table[ com ] );	tmp = p->error;	p->error = 0;    }    return( tmp );}/** Write data to a line. * * @param com    Port to use; corresponding to internal numbering scheme. * @param buffer Address of data. * @param count  Number of bytes to write. * * @author Jens Michaelsen, Jochen K黳per * @version 2000/05/02 */void rt_com_write( unsigned int com, const char *buffer, int count ){    const char *data = buffer;    if( com < RT_COM_CNT ) {	struct rt_com_struct *p = &( rt_com_table[ com ] );	struct rt_buf_struct *b = &( p->obuf );	long state;	if( p->used > 0 ) {	    rt_com_irq_off( state );	    while( --count >= 0 ) {		/* put byte into buffer, move pointers to next elements */		b->buf[ b->head++ ] = *data++;		/* if( head == RT_COM_BUF_SIZ ), wrap head to the first buffer element */		b->head &= ( RT_COM_BUF_SIZ - 1 );	    }	    p->ier |= 0x02;	    outb( p->ier, p->port + RT_COM_IER );	    rt_com_irq_on( state );	}    }}/** Read data we got from a line. * * @param com  Port to use corresponding to internal numbering scheme. * @param ptr  Address of data buffer. Needs to be of size > cnt ! * @param cnt  Number of bytes to read. * @return     Number of bytes actually read. * * @author Jens Michaelsen, Jochen K黳per * @version 1999/11/18 */int rt_com_read( unsigned int com, char *ptr, int cnt ){    int done = 0;    if( com < RT_COM_CNT ) {	struct rt_com_struct *p = &( rt_com_table[ com ] );	struct rt_buf_struct *b = &( p->ibuf );	long state;	if( p->used > 0) {	    rt_com_irq_off( state );	    while( ( b->head != b->tail ) && ( --cnt >= 0 ) ) {		done++;		*ptr++ = b->buf[ b->tail++ ];		b->tail &= ( RT_COM_BUF_SIZ - 1 );	    }	    rt_com_irq_on( state );	    if( ( p->mode & 0x02 )		&& ( rt_com_buff_free( b->head, b->tail ) > RT_COM_BUF_HI ) ) {		/* if hardware flow and enough free space on input buffer		   then set RTS */		p->mcr |= 0x02;		outb( p->mcr, p->port+RT_COM_MCR );	    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -