📄 uart.c
字号:
// Initialize TIMER1 for standard 8051 UART clock
PCON = 0; // Disable BaudRate doubler.
SM10 = 0;
SM11 = 1; // Use serial port 1 in mode 1 with 8-bits data.
REN1 = 1; // Enable UART1 receiver.
TMOD = 0x20; // Use timer 1 in mode 2, 8-bit counter with auto-reload.
uart1_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;
}
ES1 = 1; // Enable serial port Interrupt request
TR1 = 1; // Run Timer 1
TI1 = 0;
}
/*
* ----------------------------------------------------------------------------
* static S8_T uart1_PutChar(S8_T c)
* Purpose : UART1 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 uart1_PutChar(S8_T c)
{
U16_T count = 0;
if (c == 0xa)
{
do
{
EA = 0;
count = uart1_TxCount;
EA = 1;
} while (count == MAX_TX_UART1_BUF_SIZE);
uart1_TxBuf[uart1_TxHead] = 0xd;
EA = 0;
uart1_TxCount++;
EA = 1;
uart1_TxHead++;
uart1_TxHead &= MAX_TX_UART1_MASK;
}
do
{
EA = 0;
count = uart1_TxCount;
EA = 1;
} while (count == MAX_TX_UART1_BUF_SIZE);
uart1_TxBuf[uart1_TxHead] = c;
EA = 0;
uart1_TxCount++;
EA = 1;
uart1_TxHead++;
uart1_TxHead &= MAX_TX_UART1_MASK;
if (!uart1_TxFlag)
{
uart1_TxFlag = 1;
SBUF1 = uart1_TxBuf[uart1_TxTail];
}
return c;
}
/*
* ----------------------------------------------------------------------------
* static S8_T uart1_GetKey(void)
* Purpose : UART1 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 uart1_GetKey (void)
{
/* NAMING CONSTANT DECLARATIONS */
/* LOCAL VARIABLE DECLARATIONS */
S8_T c;
/* BODY */
while(uart1_RxCount==0);
EA = 0;
uart1_RxCount--;
EA = 1;
c = uart1_RxBuf[uart1_RxTail];
uart1_RxTail++;
uart1_RxTail &= MAX_RX_UART1_MASK;
return c;
}
/*
* ----------------------------------------------------------------------------
* static S8_T uart1_NoBlockGetkey(void)
* Purpose : UART1 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 uart1_NoBlockGetkey (void)
{
char c = 0;
if (uart1_RxCount !=0 )
{
EA = 0;
uart1_RxCount--;
EA = 1;
c = uart1_RxBuf[uart1_RxTail];
uart1_RxTail++;
uart1_RxTail &= MAX_RX_UART1_MASK;
return c;
}
else
{
return FALSE;
}
}
#endif
/* EXPORTED SUBPROGRAM BODIES */
/*
* ----------------------------------------------------------------------------
* S8_T _getkey (void)
* Purpose : UART getkey function. This function is the entry of getting
* characters from software buffer of system's UART ports,
* UART0, UART1 and HSUR.
* Params : none
* Returns : ch - one byte character.
* Note : The default UART port is UART0.
* ----------------------------------------------------------------------------
*/
S8_T _getkey (void)
{
S8_T ch = 0;
switch (uartPort)
{
#if UART0_ENABLE
case 0 :
ch = uart0_GetKey();
break;
#endif
#if UART1_ENABLE
case 1 :
ch = uart1_GetKey();
break;
#endif
#if HSUR_ENABLE
case 2 :
ch = HSUR_GetChar();
break;
#endif
}
return ch;
}
/*
* ----------------------------------------------------------------------------
* S8_T putchar(S8_T c)
* Purpose : UART putchar function. This function is the entry of putting
* characters into software buffer of system's UART ports,
* UART0, UART1 and HSUR.
* Params : c - one byte character to be put.
* Returns : ch - the same character to be replied.
* Note : The default UART port is UART0.
* ----------------------------------------------------------------------------
*/
S8_T putchar(S8_T c)
{
S8_T ch = 0;
switch (uartPort)
{
#if UART0_ENABLE
case 0 :
ch = uart0_PutChar(c);
break;
#endif
#if UART1_ENABLE
case 1 :
ch = uart1_PutChar(c);
break;
#endif
#if HSUR_ENABLE
case 2 :
ch = HSUR_PutChar(c);
break;
#endif
}
return ch;
}
/*
* ----------------------------------------------------------------------------
* void UART_Init(void)
* Purpose : UART initial function. It will call a real initial function
* corresponding to the used UART port.
* Params : none
* Returns : none
* Note : none
* ----------------------------------------------------------------------------
*/
void UART_Init(void)
{
switch (uartPort)
{
#if UART0_ENABLE
case 0 :
uart0_Init();
break;
#endif
#if UART1_ENABLE
case 1 :
uart1_Init();
break;
#endif
#if HSUR_ENABLE
case 2 :
HSUR_Init();
HSUR_Setup(0x0060, (UR2_CHAR_8|UR2_STOP_10), (UR2_RDI_ENB|UR2_RLSI_ENB),
(UR2_FIFO_MODE|UR2_RXFIFO_RST|UR2_TXFIFO_RST|UR2_TRIG_08), UR2_RTS);
break;
#endif
}
}
/*
* ----------------------------------------------------------------------------
* S8_T NOBLK_getkey (void)
* Purpose : UART no blocking getkey function with one checked. This function
* is the entry of getting characters from software buffer of
* system's UART ports, UART0, UART1 and HSUR.
* Params : none
* Returns : ch - one byte character.
* Note : The default UART port is UART0.
* ----------------------------------------------------------------------------
*/
S8_T NOBLK_getkey (void)
{
S8_T c = 0;
switch (uartPort)
{
case 0 :
c = UART0_NoBlockGetkey();
break;
case 1 :
c = uart1_NoBlockGetkey();
break;
}
return c;
}
/*
* ----------------------------------------------------------------------------
* BOOL UART_ParityChk(S8_T checkByte)
* Purpose : UART parity checked function in one byte transfer.
* Params : checkByte - one byte character.
* Returns : TRUE - odd parity ; FALSE - even parity.
* Note : none
* ----------------------------------------------------------------------------
*/
BOOL UART_ParityChk(S8_T checkByte)
{
U8_T oneNum = 0;
U16_T i;
oneNum = 0;
for (i=0 ; i<=7 ; i++)
{
if (checkByte & (BIT0<<i))
{
oneNum ++;
}
}
if ((oneNum % 2) == 0)
return FALSE; // if '1' number is even, return 0
else
return TRUE; // if '1' number is odd, return 1
}
/*
* ----------------------------------------------------------------------------
* void UART0_SetMode(U8_T mode)
* Purpose : Setting operation mode of UART0.
* Params : mode - operation mode (0~3).
* Returns : none
* Note : none
* ----------------------------------------------------------------------------
*/
void UART0_SetMode(U8_T mode)
{
uart0_Mode = mode;
}
/*
* ----------------------------------------------------------------------------
* void UART1_SetMode(U8_T mode)
* Purpose : Setting operation mode of UART0.
* Params : mode - operation mode (0~3).
* Returns : none
* Note : none
* ----------------------------------------------------------------------------
*/
void UART1_SetMode(U8_T mode)
{
uart1_Mode = mode;
}
/*
* ----------------------------------------------------------------------------
* BOOL UART_SetPort(U8_T portNum)
* Purpose : Setting which UART port will be used.
* Params : portNum - uart port number (0~2).
* Returns : none
* Note : none
* ----------------------------------------------------------------------------
*/
BOOL UART_SetPort(U8_T portNum)
{
uartPort = portNum & 0x03;
UART_Init();
return TRUE;
}
/*
* ----------------------------------------------------------------------------
* U16_T UART_GetRxBufCount(void)
* Purpose : Getting the remain character number in UART Rx buffer.
* Params : none
* Returns : bufCount - the number of remain character in UART software RX buffer.
* Note : none
* ----------------------------------------------------------------------------
*/
U16_T UART_GetBufCount(void)
{
U16_T bufCount = 0;
#if UART0_ENABLE
bufCount = uart0_RxCount;
#elif UART1_ENABLE
bufCount = uart1_RxCount;
#else HSUR_ENABLE
bufCount = HSUR_GetRxBufCount();
#endif
return bufCount;
}
/*
* ----------------------------------------------------------------------------
* void PMM_Uart0Init(void)
* Purpose : Initiating the UART0 to work in power management mode.
* Params : none
* Returns : none
* Note : none
* ----------------------------------------------------------------------------
*/
#if UART_PMM_ENABLE
void PMM_Uart0Init(void)
{
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 = BIT7; // Enable 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.
TH1 = 0xFE; // Baud rate = 1200 @ 25MHz.
ES0 = 1; // Enable serial port Interrupt request
TR1 = 1; // Run Timer 1
TI0 = 0;
}
#endif
/* End of uart.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -