📄 comm.c
字号:
#endif
// ****************************************************************************
// Set the UART's Line Control Register. This is for things like the number
// of data bits and any parity. See comm.h for the defines that will
// probably be sent to this routine.
//
// Input: Current port number. It is assumed to have baud rate set etc.
// The value to send to the UART's Line Control Register.
// Return: NONE.
// Side effects: Initializes the COM port specified by serial_port..
void set_cont_reg(int portid, unsigned char line_value)
{
if (what_port(portid)) // Select the current COM port.
outportb(com8250, line_value); // Set line control as passed.
}
// ****************************************************************************
// Enable or disable software handshaking (XON/XOFF). This is just a hidden
// data routine. The 'flag' is static to this file.
//
// Input: soft_hand - BOOLEAN: TURE use software handshaking (XON/XOFF).
// FALSE no software handshaking (XON/XOFF).
// Return: NONE.
// Side effects: Global flag for software handshaking is altered.
void new_soft_hand(int soft_hand)
{
xonxoff = soft_hand;
}
// ****************************************************************************
// Is software handshaking enabled? This is just a hidden
// data routine. The 'flag' is static to this file.
//
// Input: NONE.
// Return: BOOLEAN: TRUE - software handshaking is enabled.
// FALSE - software handshaking is disabled.
// Side effects: NONE.
int soft_handshaking(void)
{
return (xonxoff);
}
// ****************************************************************************
// Inidicate that XOFF has been sent. Update the XOFF sent flag.
//
// Input: NONE.
// Return: NONE.
// Side effects: Sets XOFF sent flag to be TRUE. XOFF should been sent.
void xoffed(void)
{
xofsnt = TRUE;
}
// ****************************************************************************
// Inidicate that XON has been sent. Update the XOFF sent flag to be FALSE.
//
// Input: NONE.
// Return: NONE.
// Side effects: Sets XOFF sent flag to be FALSE. XON should been sent.
void xoned(void)
{
xofsnt = FALSE;
}
// ****************************************************************************
// Inidicate that XON has been sent. Update the XOFF sent flag to be FALSE.
//
// Input: NONE.
// Return: NONE.
// Side effects: Sets XOFF sent flag to be FALSE. XON should been sent.
int xoff_sent(void)
{
return(xofsnt); // Return current XOFF sent flag value.
}
// ****************************************************************************
// initialize serial output port
//
// Input: portid - PORT number, one or two.
// speed - Baud rate setting.
// control_reg - UART controll register values. See comm.h.
// Return: BOOLEAN - TRUE if open OK.
// - FALSE if open error, like invalid parameter.
// Side effects: Initializes the COM port specified by serial_port..
int comm_open(int portid, long unsigned int speed, unsigned char control_reg)
{
#if PRINT_CMDS
if ((debug_file = fopen(prt_fname, "wt")) EQ NULL)
printf("%s not successfully open\n", prt_fname);
#endif
if (what_port(portid)) // Select the current COM port.
{
dobaud(speed); // set baud */
inptr = outptr = buffer; /* set circular buffer values */
c_in_buf = 0;
oldvec = getvect(intv); /* Save old int vector */
setvect(intv, serint); /* Set up SERINT as com ISR */
outportb(com8250, control_reg); // Set line control as passed.
outportb(com8250+1, 0x0B); /* Assert OUT2, RTS, and DTR */
inportb(dat8250);
outportb(dat8250+1, 0x01); /* Receiver-Data-Ready int */
/* Enable 8259 interrupts */
outportb(INTCONT, en8259 & inportb(INTCONT));
xoffpt = CBS / 50 * 49; /* chars in buff to send XOFF */
xonpt = CBS - xoffpt; /* chars in buff to send XON */
}
else
return(FALSE);
return(TRUE);
}
// ****************************************************************************
// Restore previous settings of 8259
//
// Input: NONE.
// Return: NONE.
// Side effects: NONE.
void comm_close(void)
{
/* Disable com interrupt at 8259 */
outportb(INTCONT, dis8259 | inportb(INTCONT));
setvect(intv, oldvec); /* Reset original interrupt vector */
#if PRINT_CMDS
fclose(debug_file); // Close the debug dump file.
#endif
}
// ****************************************************************************
// returns # characters available in buffer
//
// Input: NONE.
// Return: BYTE count of in receive buffer.
// Side effects: NONE.
int comm_avail(void)
{
return(c_in_buf);
}
// ****************************************************************************
// Send a char out port
//
// Input: BYTE to be transmitted out the COM port..
// Return: NONE.
// Side effects: NONE.
void comm_putc(BYTE c)
{
while ((inportb(stat8250) & 0x20) == 0)
; /* Wait until transmitter is ready */
outportb(dat8250, c); /* then send it */
}
// ****************************************************************************
// get a char from buffer
//
// Input: NONE.
// Return: NONE.
// Side effects: Gets a BYTE from the receive buffer, pointer change.
int comm_getc(void)
{
register char *ptr;
int c;
#if 0
if ((c_in_buf < xonpt) && xofsnt) { /* Check if we need to send */
xofsnt = FALSE; /* an XON to the host after */
comm_putc(XON); /* we had to send an XOFF */
}
#endif
while (c_in_buf == 0) /* If character not ready */
c = 0 ; /* then wait till one is ready. */
ptr = outptr;
c = *ptr++; /* Get next character in circular buff */
if (ptr == &buffer[CBS]) /* Check for end of circular buffer */
ptr = buffer; /* start from bottom of buff */
disable(); /* no interrupts during pointer manips */
outptr = ptr; /* set character output pointer */
c_in_buf--; /* and decrement the character count */
enable(); /* then allow interrupts to continue */
return(c); /* Return the character */
}
// ****************************************************************************
// flush all chars out of buffer
//
// Input: NONE.
// Return: NONE.
// Side effects: NONE.
void comm_flush(void)
{
#if 0
if (xofsnt) { /* Check if XON needs to be sent */
xofsnt = FALSE;
comm_putc(XON);
}
#endif
disable(); /* no interrupts during pointer manips */
inptr = outptr = buffer; /* reset buffer pointers */
c_in_buf = 0; /* and indicate no chars received */
enable();
}
// ****************************************************************************
// Get the number of requested bytes form COMX. Handle the COM port interface.
// This version is just a jacket around dread(), so Janus and Antares
// can call the same routine.
//
// Input: Pointer to a buffer big enough for the number of bytes wonted.
// The number of bytes being requested. How many wonted.
// Units of seconds for im_receive_buffer() timeout.
// Return: BOOLEAN - TRUE, got requested number of bytes.
// FALSE, did NOT get the requested number of bytes.
// Side effects: None.
int com_rec_buf(BYTE *gotten_bytes, WORD wonted, WORD timeout)
{
return(dread(gotten_bytes, wonted, timeout));
}
// ****************************************************************************
// Send out data stream through serial port
//
// Input: Pointer to bytes to be transmitted our COMX.
// Number of bytes to be transmitted.
// Return: None.
// Side effects: None.
void SendStream (BYTE *tx_buf, WORD data_size)
{
#if TEST_MUX32
comm_putc(0xAA); // Leading aa?
comm_putc(now_mux_addr); // MUX32 address.
#endif
#if PRINT_CMDS
#if TEST_MUX32
fprintf(debug_file, "\nSending: %02x %02x ", 0xAA, now_mux_addr);
#else
fprintf(debug_file, "\nSending:");
#endif
#endif
while(data_size > 0)
{
#if PRINT_CMDS
fprintf(debug_file, "%02x ", *tx_buf);
#endif
comm_putc(*tx_buf++);
data_size--;
}
#if PRINT_CMDS
fprintf(debug_file, "\n");
#endif
comm_flush();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -