📄 rs232.c
字号:
// break;
}
switch(dd) //com4中断标志寄存器
{
case 0x02: //发送中断
if ( com4->out.read_index == com4->out.write_index )
{
outportb( Com4UartBase + IER, IER_RX_DATA );
for(i=0;i<500;i++);
outportb(Com4UartBase + LCR,0x3b);//LCR_NO_PARITY | LCR_8_DATA_BITS);
outportb(Com4UartBase + MCR,MCR_RTS | MCR_DTR | MCR_OUT2);//0x0b
}
else
{
c = com4->out.buffer[ com4->out.read_index++ ];
outportb( Com4UartBase + THR, c );
}
break;
default:
//case 0x04: //接收中断
//case 0x0c: //接收超时中断,04和0c用来判断接收中断
while(inportb(Com4UartBase+LSR)&0x01)
{
c = (unsigned char) inportb( Com4UartBase+RBR );
if (((unsigned char)(com4->in.write_index+1 )) != com4->in.read_index)
{
com4->in.buffer[ com4->in.write_index++ ] = c ;
}
}
break;
//default:
// inportb(Com4UartBase+RBR);
// break;
}
aa=inportb(Com1UartBase+IIR)&0x0f;
bb=inportb(Com2UartBase+IIR)&0x0f;
cc=inportb(Com3UartBase+IIR)&0x0f;
dd=inportb(Com4UartBase+IIR)&0x0f;
}
outport(IntEoiReg,Int3NumEoi); //向中断结束寄存器送串口中断结束标志
}
void port_close( PORT *port )
{
int mask;
outportb( port->uart_base + IER, 0 );
outportb( port->uart_base + MCR, 0 );
free( port );
if(port==com1)
com1=NULL;
else if(port==com2)
com2=NULL;
else if(port==com3)
com3=NULL;
else if(port==com4)
com4=NULL;
if(NULL==com1 && NULL==com2 && NULL==com3 && NULL==com4)
{
mask=inport(IntMaskReg);
mask|=0x80;
outport(IntMaskReg,mask); //中断屏蔽寄存器中关闭串口中断
outport(Int3CtrlReg,0x0000); //IRQ7即串口中断控制寄存器,关中断
setvect( Int3NumIntr, old_vector );
}
}
/*int port_putc( unsigned char c, PORT *port )
{
if (( port->out.write_index+1 ) == port->out.read_index)
return( -1 );
port->out.buffer[ port->out.write_index ] = c;
port->out.write_index += 1;
if (( inportb( port->uart_base + IER ) & IER_THRE) == 0 )
outportb( port->uart_base+IER,IER_THRE);
return( c );
}
int port_getc(PORT *port)
{
if ( port->in.write_index == port->in.read_index )
return( -1 );
else
{
return( port->in.buffer[ port->in.read_index++ ] );
}
}*/
void port_puts(unsigned char str[],int len,PORT *port)
{
int i;
if (( port->out.write_index+1 ) == port->out.read_index)
return;
for(i=0;i<len;i++)
{
port->out.buffer[ port->out.write_index ] = str[i];
port->out.write_index ++;
}
if(port==com3 || port==com4)
return;
if (( inportb(port->uart_base + IER ) & IER_THRE) == 0 )
outportb( port->uart_base+IER,IER_THRE);
}
void port_send(PORT *port)
{
int i;
if(port==com1)
{
if (( inportb(port->uart_base + IER ) & IER_THRE) == 0 )
outportb( port->uart_base+IER,IER_THRE);
}
else if(port==com3)
{
//485 最低两位用来设置收发状态,最低两位置1为接收,置0为发送
outportb( port->uart_base + MCR, MCR_OUT2 );
for(i=0;i<500;i++);
outportb( com3->uart_base+IER,IER_THRE);
}
else if(port==com4)
{
outportb( port->uart_base + MCR, MCR_OUT2 );
for(i=0;i<500;i++);
outportb( com4->uart_base+IER,IER_THRE);
}
}
int port_gets(unsigned char str[],int len,PORT * port)
{
int i;
i=0;
str[0]=0;
if(port==NULL || len==0)
return(0);
if(port->in.read_index== port->in.write_index)
return(0);
for(i=0;i<len;i++)
{
if(port->in.read_index== port->in.write_index)
break;
str[i]=port->in.buffer[port->in.read_index];
port->in.read_index++;
}
//str[i]=0;
if(i)
return(i);
else
return(0);
}
void port_clear(PORT *port)
{
//com1->in.buffer[0]=0;
//com1->in.buffer[1]=0;
port->in.read_index=0;
port->in.write_index=0;
}
int port_getlen(PORT *port)
{
int len;
if(port->in.write_index>=port->in.read_index)
len=port->in.write_index-port->in.read_index;
else
len=256-port->in.read_index+port->in.write_index;
return(len);
}
void port_reset(int comid,long speed,char parity,int data,int stopbits)
{
PORT *port;
int mask;
unsigned char temp;
unsigned char lcr_out;
unsigned char mcr_out;
unsigned char low_divisor;
unsigned char high_divisor;
if(comid<1 || comid>4)
return;
switch(comid)
{
case 0x01:
port=com1;
port->uart_base=Com1UartBase;
break;
case 0x02:
port=com2;
port->uart_base=Com2UartBase;
break;
case 0x03:
port=com3;
port->uart_base=Com3UartBase;
break;
case 0x04:
port=com4;
port->uart_base=Com4UartBase;
break;
default: break;
}
port->in.write_index = port->in.read_index = 0;
port->out.write_index = port->out.read_index = 0;
if(NULL==old_vector)
old_vector = getvect( Int3NumIntr );
setvect( Int3NumIntr, com_int);
mask=inport(IntMaskReg);
mask&=0xff7c;
outport(IntMaskReg,mask); //中断屏蔽寄存器中允许串口中断
outport(Int3CtrlReg,0x0006); //IRQ7即串口中断控制寄存器,允许中断,中断级别6
outportb( port->uart_base + IER, 0 );
inportb( port->uart_base );
low_divisor = (char) (115200L / speed ) & 0xff;
high_divisor = (char) ((115200L / speed ) >> 8);
outportb( port->uart_base + LCR, LCR_DLAB );
outportb( port->uart_base + DLL, low_divisor );
outportb( port->uart_base + DLM, high_divisor );
outportb( port->uart_base + LCR, 0 );
if ( parity== 'E' )
lcr_out = LCR_EVEN_PARITY;
else if ( parity == 'O' )
lcr_out = LCR_ODD_PARITY;
else
lcr_out = LCR_NO_PARITY;
if ( stopbits == 2 )
lcr_out |= LCR_2_STOP_BITS;
if ( data == 6 )
lcr_out |= LCR_6_DATA_BITS;
else if ( data == 7 )
lcr_out |= LCR_7_DATA_BITS;
else if ( data == 8 )
lcr_out |= LCR_8_DATA_BITS;
if(comid==3)
outportb( port->uart_base + LCR,0x3b);
else
outportb( port->uart_base + LCR, lcr_out );
outportb(port->uart_base+2,0x07); //set FCR.FIFO控制寄存器,DMA=0,Enable FIFO,初始化时复位接收与保持寄存器
outportb(port->uart_base+2,0x01); //open FIFO
if(comid==1 || comid==2) //232
mcr_out=MCR_OUT2;//8
if(comid==3 || comid==4) //485 注意,最低两位用来设置收发状态,但和标准的相反,最低两位置1为接收,置0为发送
mcr_out = MCR_RTS | MCR_DTR | MCR_OUT2 ;//0x0b
outportb( port->uart_base + MCR, mcr_out );
outportb( port->uart_base + IER, IER_RX_DATA );//允许接收中断
}
PORT* init_port(int comid,long speed,char parity,int data,int stopbits)
{
PORT *port;
if ((port = malloc( sizeof( PORT ))) == NULL)
return( NULL );
switch(comid)
{
case 1:
com1=port;
break;
case 2:
com2=port;
break;
case 3:
com3=port;
break;
case 4:
com4=port;
break;
default:
return(NULL);
}
port_reset(comid,speed,parity,data,stopbits);
return(port);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -