📄 sercomm.bak
字号:
}
if (pserinfo->inhead != pserinfo->intail) // there is a character
{
disable(); // disable irqs while getting char
*ch = pserinfo->inbuf[pserinfo->intail++]; // get character from buffer
if (pserinfo->intail == IBUF_LEN) // if at end of in buffer
pserinfo->intail=0; // reset pointer
enable(); // re-enable interrupt
return true; // return the char
}
return false;
}
/* Install our functions to handle communications */
void setvects(int comport)
{
switch(comport)
{
case COM2:
oldhandler2 = getvect(0x0B);
setvect(0x0B,com_int2);
break;
case COM1:
oldhandler1 = getvect(0x0C);
setvect(0x0C,com_int1);
break;
default: break;
}
}
/* Uninstall our vectors before exiting the program */
void resvects(int comport)
{
switch(comport)
{
case COM2:
setvect(0x0B,oldhandler2);
break;
case COM1:
setvect(0x0C,oldhandler1);
break;
default: break;
}
}
//////////////////////////////////////////////////////
void interrupt com_int1(__CPPARGS)
{
int temp;
disable();
temp = (inportb(COM1BASE+IIR)) & IIR_MASK; // why interrupt was called
switch(temp)
{
case 0x00: // modem status changed
inportb(COM1BASE+MSR); // read in useless char
break;
case 0x02: // Request To Send char
if (serinfo1.outhead != serinfo1.outtail) // there's a char to send
{
outportb(COM1BASE+TXR,serinfo1.outbuf[serinfo1.outhead++]); // send the character
if (serinfo1.outhead == OBUF_LEN)
serinfo1.outhead=0; // if at end of buffer, reset pointer
}
break;
case 0x04: // character ready to be read in
inportb(COM1BASE+MSR); // read in useless char
serinfo1.inbuf[serinfo1.inhead++] = inportb(COM1BASE+RXR);// read character into inbuffer
if (serinfo1.inhead == IBUF_LEN) // if at end of buffer
serinfo1.inhead=0; // reset pointer
break;
case 0x06: // line status has changed
inportb(COM1BASE+LSR); // read in useless char
break;
default: break;
}
outportb(PIC8259_ICR, PIC8259_EOI); // Signal end of hardware interrupt
enable(); // reenable interrupts at the end of the handler
}
/////////////////////////////////////////////////////////////////////
void interrupt com_int2(__CPPARGS)
{
int temp;
disable();
temp = (inportb(COM2BASE+IIR)) & IIR_MASK; // why interrupt was called
switch(temp)
{
case 0x00: // modem status changed
inportb(COM2BASE+MSR); // read in useless char
break;
case 0x02: // Request To Send char
if (serinfo2.outhead != serinfo2.outtail) // there's a char to send
{
outportb(COM2BASE+TXR, serinfo2.outbuf[serinfo2.outhead++]); // send the character
if (serinfo2.outhead == OBUF_LEN)
serinfo2.outhead=0; // if at end of buffer, reset pointer
}
break;
case 0x04: // character ready to be read in
inportb(COM2BASE+MSR); // read in useless char
serinfo2.inbuf[serinfo2.inhead++] = inportb(COM2BASE+RXR);// read character into inbuffer
if (serinfo2.inhead == IBUF_LEN) // if at end of buffer
serinfo2.inhead=0; // reset pointer
break;
case 0x06: // line status has changed
inportb(COM2BASE+LSR); // read in useless char
break;
default: break;
}
outportb(PIC8259_ICR, PIC8259_EOI); // Signal end of hardware interrupt
enable(); // reenable interrupts at the end of the handler
}
/////////////////////////////////////////////
int SetSerCom(int comport, int baud, int para)
{
int portn=COM1BASE;
//set port
switch(comport)
{
case COM1:
portn = COM1BASE;
break;
case COM2:
portn = COM2BASE;
break;
default:
printf("\n cannot find the serial port.\n");
return false;
}
//set speed
disable();
//c = inportb(portn + LCR);
outportb(portn + LCR, 0x80); /* Set DLAB */
if (baud==19200)
{
outportb(portn + DLL, 0x06); //06:19200 0c:9600
}else{
outportb(portn + DLL, 0x0C);
}
outportb(portn + DLH, 0x00);
//outportb(portn + LCR, c); /* Reset DLAB */
//Set other communications parameters
//c = LCR_BITS8|LCR_STOP_BITS1|LCR_NO_PARITY;
outportb(portn + LCR, para);
enable();
return true;
}
//serial online
int SerComOn(int comport)
{
char temp;
setvects(comport);
//set serial interrupt
disable();
switch(comport)
{
case COM1:
temp = inportb(PIC8259_IMR) & IRQ4;
outportb(PIC8259_IMR, temp);
//temp = inportb(portbase + MCR) | 0x0f;//MCR_INT;
//outportb(portbase + MCR, temp);
//temp = (inportb(portbase + IER)) | IER_RX_INT;//|IER_TX_INT;
//outportb(portbase + IER, temp);
outportb(COM1BASE + MCR, 0x0B);
outportb(COM1BASE + IER, 0x01);
//缓冲区信息初始化
serinfo1.inhead = 0;
serinfo1.intail = 0;
serinfo1.outhead = 0;
serinfo1.outtail = 0;
break;
case COM2:
temp = inportb(PIC8259_IMR) & IRQ3;
outportb(PIC8259_IMR, temp);
outportb(COM2BASE + MCR, 0x0B);
outportb(COM2BASE + IER, 0x01);
//缓冲区信息初始化
serinfo2.inhead = 0;
serinfo2.intail = 0;
serinfo2.outhead = 0;
serinfo2.outtail = 0;
break;
default:
return false;
}
outportb(PIC8259_ICR, PIC8259_EOI); // Signal end of hardware interrupt
setvects(comport);
//temp = inportb(portbase + MCR) | MCR_DTR | MCR_RTS | MCR_INT;
enable();
return true;
}
/* Go off-line */
void SerComOff(int comport)
{
char temp;
disable();
switch(comport)
{
case COM1:
temp = inportb(PIC8259_IMR) | ~IRQ4;
outportb(PIC8259_IMR, temp);
outportb(COM1BASE + IER, 0);
outportb(COM1BASE + MCR, 0);
break;
case COM2:
temp = inportb(PIC8259_IMR) | ~IRQ3;
outportb(PIC8259_IMR, temp);
outportb(COM2BASE + IER, 0);
outportb(COM2BASE + MCR, 0);
break;
default: break;
}
//temp = inportb(PIC8259_IMR) | ~IRQ3 | ~IRQ4;
outportb(PIC8259_ICR, PIC8259_EOI); // Signal end of hardware interrupt
enable();
resvects(comport);
}
/* Control-Break interrupt handler */
int OffAllSerial(void)
{
SerComOff(COM1);
SerComOff(COM2);
printf("\nthe serial port is close.\n");
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -