📄 uart.c
字号:
{
RxQ[1].buff[RxQ[1].wptr++] = US1_RHR;
if(RxQ[1].wptr == MAXEVENT)
RxQ[1].wptr = 0; /*loop back*/
}
#if 1 /* 2004/5/20 AM11:10, Stphen Yee*/
/* Modifed by Stephen Yee */
if( serial_drv ==1 )
UARTRxIntOff(1);
else
#endif
UARTRxIntOn(1);
}
US1_CR=0x00000100;
}
#endif
/************************************************************************/
/* Uart Tx Int Enable */
/************************************************************************/
void UARTTxIntOn(uint32 channel)
{
if(channel) {
US1_IER=0x00000002;
//Enable_Int(nUART1_TX_INT); /* Enable Interrupt */
//SetPendingBit(US0IRQ_INT);
//??????????
}
else {
US0_IER=0x00000002;
//Enable_Int(nUART0_TX_INT); /* Enable Interrupt */
//SetPendingBit(nUART0_TX_INT);
//????????
}
}
/************************************************************************/
/* Uart Rx Int Enable */
/************************************************************************/
void UARTRxIntOn(uint32 channel)
{
if(channel) {
US1_IER=0x00000001;
//Enable_Int(nUART1_RX_ERR_INT); /* Enable Interrupt */
}
else {
US0_IER=0x00000001;
//Enable_Int(nUART0_RX_ERR_INT); /* Enable Interrupt */
}
}
/************************************************************************/
/* Uart Tx Int Disable */
/************************************************************************/
void UARTTxIntOff(uint32 channel)
{
if(channel) {
US1_IDR=0x00000002;
//Disable_Int(nUART1_TX_INT); /* Enable Interrupt */
//Clear_PendingBit(nUART1_TX_INT) ;
//?????????????
}
else {
US0_IDR=0x00000002;
//Disable_Int(nUART0_TX_INT); /* Disable Interrupt */
//Clear_PendingBit(nUART0_TX_INT) ;
//?????????????
}
}
/************************************************************************/
/* Uart Rx Int Disable */
/************************************************************************/
void UARTRxIntOff(uint32 channel)
{
if(channel) {
US1_IDR=0x00000001;
//Disable_Int(US1IRQ_INT); /* Disable Interrupt */
}
else {
US0_IDR=0x00000001;
//Disable_Int(US0IRQ_INT); /* Disable Interrupt */
}
}
/************************************************************************/
/* Write data to transmit Que. */
/* called by dputchar */
/************************************************************************/
uint32 TxQWr(uint32 channel,uint8 data)
{
if(TxQ[channel].wptr+1 == TxQ[channel].rptr)
{
return(ERROR); /* ring buffer full state */
}
TxQ[channel].buff[TxQ[channel].wptr++] = data;
if(TxQ[channel].wptr == MAXEVENT)
{
TxQ[channel].wptr=0;
}
return(SUCCESS1);
}
/************************************************************************/
/* Read data to receive Que. */
/* called by dgetchar */
/************************************************************************/
uint16 RxQRd(uint32 channel)
{
if(RxQ[channel].rptr == MAXEVENT)
RxQ[channel].rptr=0; /*loop back*/
if(RxQ[channel].rptr == RxQ[channel].wptr)
return(0x0100); /* set the high byte to 0x1 indicate the buffer is empty*/
return( (uint16)(RxQ[channel].buff[RxQ[channel].rptr++] & 0x0ff));
}
/************************************************************************/
/* dputchar-send a character to uart0 */
/* modify api for iniche */
/************************************************************************/
void dputchar( int ch )
{
i_putc(SERIAL_DEV0,(uint8)ch);
if ( ch == '\n' )
i_putc(SERIAL_DEV0,'\r'); //return char
}
/************************************************************************/
/* i_putc-send a character to uart0 */
/* called by dputchar */
/************************************************************************/
int i_putc(uint32 channel, uint8 ch)
{
while(!U_TX_COMPLETE(channel)); /* wait transmit complet */
if(TxQWr(channel, ch)){ /* write data to tx que */
UARTTxIntOn(channel); /* Transmit interrupt on */
while(!U_BUFF_EMPTY(channel)); /* wait for tx buffer empty */
return(SUCCESS1);
}
return(ERROR);
}
/************************************************************************/
/* get char from uart0 */
/* modify api for iniche */
/************************************************************************/
int getch( void )
{
if ( !uart_done )
UART_Initialize(SERIAL_DEV0);
return i_getc(SERIAL_DEV0);
}
/************************************************************************/
/* get char from uart0 */
/* called by getchar */
/************************************************************************/
int i_getc(uint32 channel)
{
uint16 ret;
UARTRxIntOn(channel);
ret = RxQRd(channel);
if (ret >>8)
return -1;
else
return((int)(ret &0x0ff));
}
int uart_init(int unit)
{
int ret = ERROR;
/* ----Stephen Yee modified-----*/
if(unit==0){
if ( !uart_done )
ret = UART_Initialize(0);
else
ret = SUCCESS1;
}
else {
if ( !uart_com1_done )
ret = UART_Initialize(0);
else
ret = SUCCESS1;
}
if (SUCCESS1 == ret)
return 0;
else
return -1;
}
int uart_getc(int32 channel)
{
int ret = i_getc((uint32)channel);
// printf("ch:0x%x\n", ret);
return ret;
}
int
uart_putc(int unit, uint8 ch)
{
int ret;
ret = i_putc((uint32)unit, ch);
if (SUCCESS1 == ret)
return 0;
else
return -1;
}
int
uart_stats(void * pio, int unit)
{
return 1;
}
/* wait for uart Tx buffer to clear,Added by Stephen Yee. */
int uart_ready( uint32 channel)
{
if (U_BUFF_EMPTY(channel))
return 1; /* TRUE */
else
return 0; /* FLASE*/
}
void Uart_SendByte(char data)
{
char temp;
do
{
temp = US0_CSR;
} while ((temp & 0x02) == 0); /* Wait for TXRDY*/
US0_THR = data;
}
void Uart_SendString(char *pt)
{
while(*pt)
Uart_SendByte(*pt++);
}
void Uart_Printf(char *fmt,...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
Uart_SendString(string);
va_end(ap);
}
#if 1
/************************************************************************/
/* BaudRateVal */
/* This function return the value of UART baudrate divisior, */
/* UBRDIVn according to the baudrate. */
/************************************************************************/
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 */
}
#endif
/************************************************************************/
/* Key hitted */
/************************************************************************/
int kbhit(void)
{
int KbdHit ;
int serial_dev;
volatile unsigned int *us_csr;
serial_dev = SERIAL_DEV0;
if (serial_dev) {
if ( !uart_com1_done )
UART_Initialize(SERIAL_DEV0);
us_csr=(volatile unsigned int *)(USART1_BASE+0x14);
}
else {
if ( !uart_done )
UART_Initialize(SERIAL_DEV0);
us_csr= (volatile unsigned int *)(USART0_BASE+0x14);
}
if (*us_csr & USTAT_RCV_READY)
KbdHit = 1 ;
else
KbdHit = 0 ;
return KbdHit ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -