📄 uart.c
字号:
/*
*********************************************************************************
* Copyright (c) 2005 ASIX Electronic Corporation All rights reserved.
*
* This is unpublished proprietary source code of ASIX Electronic Corporation
*
* The copyright notice above does not evidence any actual or intended
* publication of such source code.
*********************************************************************************
*/
/*================================================================================
* Module Name : uart.c
* Purpose : The UART module driver. It manages the character
* buffer and handles the ISR. This driver includes UART0 and UART1.
* Author : Robin Lee
* Date : 2006-01-10
* Notes : None.
* $Log: uart.c,v $
* Revision 1.3 2006/05/03 02:45:55 robin6633
* Changed the the function name UART_GetBufCount()
* to UART_GetRxBufCount() .
*
* Revision 1.2 2006/05/02 01:43:40 robin6633
* Add an expanding function to get the counter value of UART software buffer.
*
* Revision 1.1 2006/04/07 11:39:14 robin6633
* no message
*
*================================================================================
*/
/* INCLUDE FILE DECLARATIONS */
#include "reg80390.h"
#include "types.h"
#include "uart.h"
#include "ax11000_cfg.h"
#if HSUR_ENABLE
#include "hsuart.h"
#endif
/* STATIC VARIABLE DECLARATIONS */
#if UART0_ENABLE
static U8_T uart0_TxBuf[MAX_TX_UART0_BUF_SIZE];
static U16_T uart0_TxHead = 0;
static U16_T uart0_TxTail = 0;
static U16_T uart0_TxCount = 0;
static U8_T uart0_TxFlag = 0;
static U8_T uart0_RxBuf[MAX_RX_UART0_BUF_SIZE];
static U16_T uart0_RxHead = 0;
static U16_T uart0_RxTail = 0;
static U8_T uart0_Mode = 0;
U16_T uart0_RxCount = 0;
#endif
#if UART1_ENABLE
static U8_T uart1_TxBuf[MAX_TX_UART1_BUF_SIZE];
static U16_T uart1_TxHead = 0;
static U16_T uart1_TxTail = 0;
static U16_T uart1_TxCount = 0;
static U8_T uart1_TxFlag = 0;
static U8_T uart1_RxBuf[MAX_RX_UART1_BUF_SIZE];
static U16_T uart1_RxHead = 0;
static U16_T uart1_RxTail = 0;
static U8_T uart1_Mode = 0;
U16_T uart1_RxCount = 0;
#endif
static U8_T uartPort = 0;
/* LOCAL SUBPROGRAM DECLARATIONS */
#if UART0_ENABLE
static void uart0_ISR(void);
static void uart0_Init(void);
static S8_T uart0_PutChar(S8_T c);
static S8_T uart0_GetKey(void);
static S8_T UART0_NoBlockGetkey(void);
#endif
#if UART1_ENABLE
static void uart1_ISR(void);
static void uart1_Init(void);
static S8_T uart1_PutChar(S8_T c);
static S8_T uart1_GetKey(void);
static S8_T uart1_NoBlockGetkey(void);
#endif
/* LOCAL SUBPROGRAM BODIES */
#if UART0_ENABLE
/*
* ----------------------------------------------------------------------------
* static void UART0_ISR(void)
* Purpose : UART0 interrupt service routine. For sending out, it puts data
* from software buffer into hardware serial buffer register (SBUF0).
* For receiving, it gets data from hardware serial buffer register
* (SBUF0) and stores into software buffer.
* Params : none
* Returns : none
* Note : none
* ----------------------------------------------------------------------------
*/
static void UART0_ISR(void) interrupt 4
{
U8_T parity = 0;
if (RI0)
{
EA = 0;
if (uart0_RxCount != MAX_RX_UART0_BUF_SIZE)
{
uart0_RxBuf[uart0_RxHead] = SBUF0;
if (uart0_Mode & BIT1)
{
parity = UART_ParityChk((U8_T)uart0_RxBuf[uart0_RxHead]);
if (RB08 != parity)
P3 = 0xE7;
}
uart0_RxCount++;
uart0_RxHead++;
uart0_RxHead &= MAX_RX_UART0_MASK;
}
RI0 = 0;
EA = 1;
} /* End of if(RI0) */
if (TI0)
{
EA = 0;
uart0_TxTail++;
uart0_TxTail &= MAX_TX_UART0_MASK;
uart0_TxCount--;
if (uart0_TxCount > 0)
{
SBUF0 = uart0_TxBuf[uart0_TxTail];
if (uart0_Mode & BIT1)
{
parity = UART_ParityChk((U8_T)uart0_TxBuf[uart0_TxTail]);
if (parity)
TB08 = 1;
else
TB08 = 0;
}
}
else
uart0_TxFlag = 0;
TI0 = 0;
EA = 1;
} /* End of if(TI0) */
} /* End of UART_Int */
/*
* ----------------------------------------------------------------------------
* static void uart0_Init(void)
* Purpose : Setting operation mode of UART0 and initiating the global values.
* Params : none
* Returns : none
* Note : none
* ----------------------------------------------------------------------------
*/
static void uart0_Init(void)
{
U8_T sysClk = 0;
U16_T i;
uart0_TxHead = 0;
uart0_TxTail = 0;
uart0_TxCount = 0;
uart0_TxFlag = 0;
uart0_RxHead = 0;
uart0_RxTail = 0;
uart0_RxCount = 0;
for (i=0 ; i<MAX_TX_UART0_BUF_SIZE ; i++)
uart0_TxBuf[i] = 0;
for (i=0 ; i<MAX_RX_UART0_BUF_SIZE ; i++)
uart0_RxBuf[i] = 0;
// Initialize TIMER1 for standard 8051 UART clock
PCON = 0; // Disable BaudRate doubler.
SM01 = 1; // Use serial port 0 in mode 1 with 8-bits data.
REN0 = 1; // Enable UART0 receiver.
TMOD = 0x20; // Use timer 1 in mode 2, 8-bit counter with auto-reload.
uart0_Mode = 1;
sysClk = CSREPR & 0xC0;
switch (sysClk)
{
case SCS_100M :
AX_DBG_LED(0x10);
TH1 = 0xE4; // Baud rate = 9600 @ 100MHz.
break;
case SCS_50M :
AX_DBG_LED(0x50);
TH1 = 0xF2; // Baud rate = 9600 @ 50MHz.
break;
case SCS_25M :
AX_DBG_LED(0x25);
TH1 = 0xF9; // Baud rate = 9600 @ 25MHz.
break;
default :
AX_DBG_LED(0xAA);
TH1 = 0xF9; // Baud rate = 9600 @ 25MHz.
break;
}
ES0 = 1; // Enable serial port Interrupt request
TR1 = 1; // Run Timer 1
TI0 = 0;
} /* End of UART_Init */
/*
* ----------------------------------------------------------------------------
* static S8_T uart0_PutChar(S8_T c)
* Purpose : UART0 output function. This function puts one byte data into the
* software character buffer.
* Params : c - one byte character.
* Returns : c - one byte character.
* Note : none
* ----------------------------------------------------------------------------
*/
static S8_T uart0_PutChar(S8_T c)
{
U16_T count = 0;
if (c == 0xa)
{
do
{
EA = 0;
count = uart0_TxCount;
EA = 1;
} while (count == MAX_TX_UART0_BUF_SIZE);
uart0_TxBuf[uart0_TxHead] = 0xd;
EA = 0;
uart0_TxCount++;
EA = 1;
uart0_TxHead++;
uart0_TxHead &= MAX_TX_UART0_MASK;
}
do
{
EA = 0;
count = uart0_TxCount;
EA = 1;
} while (count == MAX_TX_UART0_BUF_SIZE);
uart0_TxBuf[uart0_TxHead] = c;
EA = 0;
uart0_TxCount++;
EA = 1;
uart0_TxHead++;
uart0_TxHead &= MAX_TX_UART0_MASK;
if (!uart0_TxFlag)
{
uart0_TxFlag = 1;
SBUF0 = uart0_TxBuf[uart0_TxTail];
}
return c;
}
/*
* ----------------------------------------------------------------------------
* static S8_T uart0_GetKey(void)
* Purpose : UART0 input function. This function replies one byte data from the
* software character buffer.
* Params : none
* Returns : c - one byte character.
* Note : none
* ----------------------------------------------------------------------------
*/
static S8_T uart0_GetKey (void)
{
/* NAMING CONSTANT DECLARATIONS */
/* LOCAL VARIABLE DECLARATIONS */
S8_T c = 0;
/* BODY */
while(uart0_RxCount == 0);
EA = 0;
uart0_RxCount--;
EA = 1;
c = uart0_RxBuf[uart0_RxTail];
uart0_RxTail++;
uart0_RxTail &= MAX_RX_UART0_MASK;
return c;
}
/*
* ----------------------------------------------------------------------------
* static S8_T uart0_NoBlockGetkey(void)
* Purpose : UART0 input function. This function replies one byte data from the
* software character buffer. But it only check the buffer one time.
* If no data, it will reply a FALSE condition.
* Params : none
* Returns : c - one byte character.
* Note : none
* ----------------------------------------------------------------------------
*/
static S8_T UART0_NoBlockGetkey (void)
{
char c = 0;
if (uart0_RxCount !=0 )
{
EA = 0;
uart0_RxCount--;
EA = 1;
c = uart0_RxBuf[uart0_RxTail];
uart0_RxTail++;
uart0_RxTail &= MAX_RX_UART0_MASK;
return c;
}
else
{
return FALSE;
}
}
#endif
#if UART1_ENABLE
////////////////////////////////////////////////////////////////////////////////
/*
* ----------------------------------------------------------------------------
* static void UART1_ISR(void)
* Purpose : UART1 interrupt service routine. For sending out, it puts data
* from software buffer into hardware serial buffer register (SBUF1).
* For receiving, it gets data from hardware serial buffer register
* (SBUF1) and stores into software buffer.
* Params : none
* Returns : none
* Note : none
* ----------------------------------------------------------------------------
*/
static void uart1_ISR(void) interrupt 6
{
U8_T parity = 0;
if (RI1)
{
EA = 0;
if (uart1_RxCount != MAX_RX_UART1_BUF_SIZE)
{
uart1_RxBuf[uart1_RxHead] = SBUF1;
if (uart1_Mode & BIT1)
{
parity = UART_ParityChk((U8_T)uart1_RxBuf[uart1_RxHead]);
if (RB18 != parity)
P3 = 0xE7;
}
uart1_RxCount++;
uart1_RxHead++;
uart1_RxHead &= MAX_RX_UART1_MASK;
}
RI1 = 0;
EA = 1;
} /* End of if(RI0) */
if (TI1)
{
EA = 0;
uart1_TxTail++;
uart1_TxTail &= MAX_TX_UART1_MASK;
uart1_TxCount--;
if (uart1_TxCount > 0)
{
SBUF1 = uart1_TxBuf[uart1_TxTail];
if (uart1_Mode & BIT1)
{
parity = UART_ParityChk((U8_T)uart1_TxBuf[uart1_TxTail]);
if (parity)
TB18 = 1;
else
TB18 = 0;
}
}
else
uart1_TxFlag = 0;
TI1 = 0;
EA = 1;
} /* End of if(TI0) */
}
/*
* ----------------------------------------------------------------------------
* static void uart1_Init(void)
* Purpose : Setting operation mode of UART1 and initiating the global values.
* Params : none
* Returns : none
* Note : none
* ----------------------------------------------------------------------------
*/
static void uart1_Init(void)
{
U8_T sysClk = 0;
U16_T i;
uart1_TxHead = 0;
uart1_TxTail = 0;
uart1_TxCount = 0;
uart1_TxFlag = 0;
uart1_RxHead = 0;
uart1_RxTail = 0;
uart1_RxCount = 0;
for (i=0 ; i<MAX_TX_UART1_BUF_SIZE ; i++)
uart1_TxBuf[i] = 0;
for (i=0 ; i<MAX_RX_UART1_BUF_SIZE ; i++)
uart1_RxBuf[i] = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -