📄 uart.c
字号:
#include <stdarg.h>
#include "isr.h"
#include "uart.h"
static UCHAR read_num(char *,UCHAR , ULONG *);
char inp_buff[INP_LEN]; /* Input buffer */
unsigned char inp_cnt; /* # of chars currently in buffer */
BaudTable U_BaudRate[BAUD_TABLE] = {
/* for KS32C50100 50MHz/2 UART clock */
9600, 0x00a20,
19200, 0x00500,
38400, 0x00280,
57600, 0x001a0,
115200, 0x000d0,
230400, 0x00060,
460800, 0x00020 // not available
};
U32 baudrate=38400; // set baudrate for UART0 and UART1
SERIAL_DEV uart_dev_init;
UART_BUFFER RxQ[NUM_OF_SERIAL_DEV],TxQ[NUM_OF_SERIAL_DEV];
uint32 UART_Initialize()
{
Disable_Int(nGLOBAL_INT); /* Global interrupt disabled */
/*****************************/
/* Initialize UART channel 0 */
/*****************************/
uart_dev_init.com_port = SERIAL_DEV0; /* com 0 */
uart_dev_init.baud_rate = baudrate;
uart_dev_init.data_mode = (UCON_RXM_INTREQ|UCON_TXM_INTREQ|UCON_RXSTAT_INT);
uart_dev_init.parity = ULCON_PMD_NO; /* No parity */
uart_dev_init.stop_bits = 0; /* one bit */
uart_dev_init.data_bits = ULCON_WL8; /* 8bits */
uart_dev_init.clk_sel = 0; /* internal clock */
UART_Init(&uart_dev_init);
/*****************************/
/* Initialize UART channel 1 */
/*****************************/
// uart_dev_init.com_port = SERIAL_DEV1; /* com 1 */
// UART_Init(&uart_dev_init);
Enable_Int(nGLOBAL_INT); /* Global interrupt enabled */
return(SUCCESS);
}
/***************************/
/* Uart main Init function */
/***************************/
uint32 UART_Init(SERIAL_DEV *s)
{
uint32 rUARTBRD;
/* UART interrupt off */
UARTRxIntOff(s->com_port);
UARTTxIntOff(s->com_port);
/* Initialize UART transmit & receive Queue */
TxQInit(s->com_port);
RxQInit(s->com_port);
/* default baud rate will be set. sysconf.h */
rUARTBRD = U_BaudRate[BaudRateVal(s->baud_rate)].div;
if(s->com_port)
{
/* Interrupt service routine setup */
SysSetInterrupt(nUART1_TX_INT, Uart1TxLisr);
SysSetInterrupt(nUART1_RX_ERR_INT, Uart1RxErrLisr);
UARTLCON1 = s->data_bits|s->stop_bits|s->parity|s->clk_sel;
UARTCONT1 = s->data_mode;
UARTBRD1 = rUARTBRD;
}
else
{
/* Interrupt service routine setup */
SysSetInterrupt(nUART0_TX_INT, Uart0TxLisr);
SysSetInterrupt(nUART0_RX_ERR_INT, Uart0RxErrLisr);
/* UART mode, default baud rate setup */
UARTLCON0 = s->data_bits|s->stop_bits|s->parity|s->clk_sel;
UARTCONT0 = s->data_mode;
UARTBRD0 = rUARTBRD;
}
UARTRxIntOn(s->com_port);
UARTTxIntOn(s->com_port);
return(SUCCESS);
}
uint32 BaudRateVal(uint32 baud)
{
uint32 index;
for(index = 0; index < BAUD_TABLE; index++)
{
if(U_BaudRate[index].baud == baud) return(index);
}
return(0); /* baudrate data doesn't in table */
}
uint32 BaudRate(uint32 div)
{
uint32 index;
for(index = 0; index < BAUD_TABLE; index++)
{
if(U_BaudRate[index].div == div) return(index);
}
return(0); /* baudrate data doesn't in table */
}
/***********************************************************************/
/* putb: Output a character */
/* */
/* INPUTS: ch = character to output */
/* */
/***********************************************************************/
static void putb(UCHAR ch)
{
SerialPollConout(ch);
if (ch == '\n')
SerialPollConout('\r');
}
uint32 get_digit(void)
{
uint8 RcvData[10] ;
int RcvNum=0 ;
int RcvDataSize=0 ;
int i ;
while ((RcvData[RcvDataSize] = get_byte()) != CR)
{
if(RcvDataSize<10)
RcvDataSize++ ;
}
for (i=0;i < RcvDataSize ; i++)
RcvNum = RcvNum*10 + (RcvData[i]-'0') ;
return RcvNum ;
}
void put_char(uint32 channel,char ch)
{
if(channel) {
WaitXmitter(UARTSTAT1){}
UARTTXH1 = ch;
}
else {
WaitXmitter(UARTSTAT0){}
UARTTXH0 = ch;
}
}
char get_char(uint32 channel)
{
char ch;
if(channel) { WaitRcver(UARTSTAT1) {}
while(!(UARTSTAT1 & USTAT_RCV_READY))
{ }
ch = UARTRXB1;
}
else { WaitRcver(UARTSTAT0){}
ch = UARTRXB0;
}
return ch;
}
void put_string(char *ptr )
{
while(*ptr )
{
put_byte(*ptr++ );
}
}
void Uart0TxLisr(void)
{
while(!(UARTSTAT0 & USTAT_TXB_EMPTY))
{
}
if(TxQ[0].rptr == MAXEVENT)
TxQ[0].rptr=0; /*loop back*/
if(TxQ[0].rptr != TxQ[0].wptr)
UARTTXH0 = TxQ[0].buff[TxQ[0].rptr++];
else
UARTTxIntOff(0);
}
/* Rcv, Error Interrupt Service Routine */
void Uart0RxErrLisr(void)
{
if(!(UARTSTAT0 & USTAT_ERROR))
{
if(RxQ[0].wptr+1 != RxQ[0].rptr)
{
RxQ[0].buff[RxQ[0].wptr++] = UARTRXB0;
if(RxQ[0].wptr == MAXEVENT)
RxQ[0].wptr = 0; /*loop back*/
}
}
// UARTRxIntOff(0);
}
void Uart1TxLisr(void)
{
while(!(UARTSTAT1 & USTAT_TXB_EMPTY))
{
}
if(TxQ[1].rptr == MAXEVENT)
TxQ[1].rptr=0; /*loop back*/
if(TxQ[1].rptr != TxQ[1].wptr)
UARTTXH1 = TxQ[1].buff[TxQ[1].rptr++];
else
UARTTxIntOff(1);
}
void Uart1RxErrLisr(void)
{
if(!(UARTSTAT1 & USTAT_ERROR))
{
if(RxQ[1].wptr+1 != RxQ[1].rptr)
{
RxQ[1].buff[RxQ[1].wptr++] = UARTRXB1;
if(RxQ[1].wptr == MAXEVENT)
RxQ[1].wptr = 0; /*loop back*/
}
}
// UARTRxIntOff(1);
}
void UARTTxIntOn(uint32 channel)
{
if(channel) {
/* Enable Interrupt */
Enable_Int(nUART1_TX_INT);
SetPendingBit(nUART1_TX_INT);
}
else {
/* Enable Interrupt */
Enable_Int(nUART0_TX_INT);
SetPendingBit(nUART0_TX_INT);
}
}
void UARTRxIntOn(uint32 channel)
{
if(channel) {
/* Enable Interrupt */
/* KS32C50100 */
Enable_Int(nUART1_RX_ERR_INT);
}
else {
/* Enable Interrupt */
/* KS32C50100 */
Enable_Int(nUART0_RX_ERR_INT);
}
}
void UARTTxIntOff(uint32 channel)
{
if(channel)
{
/* Enable Interrupt */
Disable_Int(nUART1_TX_INT);
Clear_PendingBit(nUART1_TX_INT) ;
}
else
{
/* Disable Interrupt */
Disable_Int(nUART0_TX_INT);
Clear_PendingBit(nUART0_TX_INT) ;
}
}
void UARTRxIntOff(uint32 channel)
{
if(channel)
{
/* Disable Interrupt */
/* KS32C50100 */
Disable_Int(nUART1_RX_ERR_INT);
//Clear_PendingBit(nUART1_RX_ERR_INT) ;
}
else
{
/* Disable Interrupt */
/* KS32C50100 */
Disable_Int(nUART0_RX_ERR_INT);
//Clear_PendingBit(nUART0_RX_ERR_INT) ;
}
}
/* Transmit Que initialize */
void TxQInit(uint32 channel)
{
int i;
for(i=0; i < MAXEVENT; i++)
{
TxQ[channel].buff[i] = '\0';
}
TxQ[channel].wptr = 0;
TxQ[channel].rptr = 0;
}
/* Receive Que initialize */
void RxQInit(uint32 channel)
{
int i;
for(i=0; i < MAXEVENT; i++)
{
RxQ[channel].buff[i] = '\0';
}
RxQ[channel].wptr = 0;
RxQ[channel].rptr = 0;
}
/***********************************************************************/
/* Print: Format output and send it to the console */
/* */
/* INPUTS: format = ptr to format string */
/* additional inputs as specified by *format. */
/* */
/* RETURNS: # of characters output */
/* NOTE: Format strings are explained in the header at the */
/* top of this module. */
/* */
/***********************************************************************/
ULONG Print(char *format, ...)
{
char buf[100];
int i=0;
int len=0;
va_list arg_pt;
if (format == NULL)
format = (char *)"(null)";
va_start(arg_pt, format);
vsprintf(buf,format,arg_pt);
va_end(arg_pt);
len=strlen(buf);
for(i=0;i<len;i++)
{
TxQWr(1,buf[i]);
// put_byte(buf[i]);
}
return len;
}
/***********************************************************************/
/* getch: Get a character from console, waiting if necessary */
/* */
/* RETURNS: Value of character */
/* NOTES: 1. Handles backspaces */
/* 2. Doesn't echo characters or update the input buffer */
/* (unless character is backspace) */
/* */
/***********************************************************************/
UCHAR getch(void)
{
UCHAR c;
for (;;)
{
/*-----------------------------------------------------------------*/
/* Get a character when it is typed in. Translate linefeed to CR. */
/*-----------------------------------------------------------------*/
while (SerialPollConsts(0) == 0)
{ }
c = SerialPollConin() & 0x7F;
if (c == NL) c = CR;
/*-----------------------------------------------------------------*/
/* Handle backspace */
/*-----------------------------------------------------------------*/
if ((c == BSP) || (c == RUBOUT))
{
if (inp_cnt > 0)
{
inp_cnt--;
}
}
/*-----------------------------------------------------------------*/
/* Don't allow the input buffer to overflow. */
/*-----------------------------------------------------------------*/
else if (inp_cnt >= INP_LEN)
SerialPollConout(7);
/*-----------------------------------------------------------------*/
/* Skip control characters (other than backspace and CR) */
/*-----------------------------------------------------------------*/
else if ((c <= CTRLZ) && (c != CR))
continue;
/*-----------------------------------------------------------------*/
/* Otherwise just return the character */
/*-----------------------------------------------------------------*/
else
return c;
}
}
/***********************************************************************/
/* getchar: Get a character from console, no waiting */
/* */
/* RETURNS: Value of character */
/* NOTES: 1. Didn't Handles backspaces */
/* 2. Doesn't echo characters or update the input buffer */
/* (unless character is backspace) */
/* */
/***********************************************************************/
UCHAR get_ch(U32 cur_pos)
{
UCHAR c;
for (;;)
{
/*-----------------------------------------------------------------*/
/* Get a character when it is typed in. Translate linefeed to CR. */
/*-----------------------------------------------------------------*/
while (SerialPollConsts(0) == 0)
{}
c = SerialPollConin() & 0x7F;
if (c == NL) c = CR;
/*-----------------------------------------------------------------*/
/* Only Send backspace */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -