📄 uart_test.c
字号:
////////////////////////////////////////////////////////////////////////////
//
// Program to check the functionality of the UART
// device
//
// - PRD
//
#include <cdefBF537.h>
#include <ccblkfn.h>
#include "Timer_ISR.h"
//////////////////////////////////////////////////////////////////////////////
//
// COMMON DEFINES
//
//////////////////////////////////////////////////////////////////////////////
// Apply formula DL = PERIOD / (16 x 8 bits) and call uart_init that writes
// the result to the two 8-bit DL registers (DLH:DLL).
#define BAUD_RATE_115200 (0x15A2 >> 7)
#define MAX_TEST_CHARS 5000
//////////////////////////////////////////////////////////////////////////////
//
// function prototypes
//
//////////////////////////////////////////////////////////////////////////////
void Init_UART(void);
int PutChar(const char c);
int GetChar(char *const c);
int TEST_UART(void);
//////////////////////////////////////////////////////////////////////////////
//
// standalone test jig
//
//////////////////////////////////////////////////////////////////////////////
#ifdef _STANDALONE_ // use this to run standalone tests
int main(void)
{
int bPassed = 0;
Init_UART();
while(1)
{
bPassed = TEST_UART();
if( 0 == bPassed )
{
break;
}
}
return 0;
}
#endif //#ifdef _STANDALONE_
//////////////////////////////////////////////////////////////////////////////
// int TEST_UART(void)
//
// PURPOSE: Test the UART
//////////////////////////////////////////////////////////////////////////////
void Init_UART(void)
{
volatile int temp;
// set FERF registers
#if (__SILICON_REVISION__ < 0x0001) // Workaround silicon anomaly 05000212
temp = *pPORTF_FER; //--possibly anomaly 05000157?
ssync();
*pPORTF_FER = 0x0003;
ssync();
#endif
*pPORTF_FER = 0x0003;
ssync();
// Configure UART0 RX and UART0 TX pins
#if (__SILICON_REVISION__ < 0x0001) // Workaround silicon anomaly 05000212
temp = *pPORT_MUX; //--possibly anomaly 05000157?
ssync();
*pPORT_MUX = 0;
ssync();
#endif
*pPORT_MUX = 0;
ssync();
/*****************************************************************************
*
* First of all, enable UART clock.
*
****************************************************************************/
*pUART0_GCTL = UCEN;
/*****************************************************************************
*
* Read period value and apply formula: DL = PERIOD / 16 / 8
* Write result to the two 8-bit DL registers (DLH:DLL).
*
****************************************************************************/
*pUART0_LCR = DLAB;
*pUART0_DLL = BAUD_RATE_115200;
*pUART0_DLH = (BAUD_RATE_115200 >> 8);
/*****************************************************************************
*
* Clear DLAB again and set UART frame to 8 bits, no parity, 1 stop bit.
* This may differ in other scenarious.
*
****************************************************************************/
*pUART0_LCR = 0x03;
/*****************************************************************************
*
* Finally enable interrupts inside UART module, by setting proper bits
* in the IER register. It is good programming style to clear potential
* UART interrupt latches in advance, by reading RBR, LSR and IIR.
*
* Setting the ETBEI bit automatically fires a TX interrupt request.
*
****************************************************************************/
temp = *pUART0_RBR;
temp = *pUART0_LSR;
temp = *pUART0_IIR;
*pUART0_IER = ETBEI;
}
int PutChar(const char cVal)
{
int nStatus = 0;
unsigned int nTimer = SetTimeout(1000);
if( ((unsigned int)-1) != nTimer )
{
do{
if( (*pUART0_LSR & THRE) )
{
*pUART0_THR = cVal;
nStatus = 1;
break; //return 1;
//asm("nop;");
}
}while( !IsTimedout(nTimer) );
}
ClearTimeout(nTimer);
return nStatus;
}
int GetChar(char *const cVal)
{
int nStatus = 0;
unsigned int nTimer = SetTimeout(1000);
if( ((unsigned int)-1) != nTimer )
{
do{
if( DR == (*pUART0_LSR & DR) )
{
*cVal = *pUART0_RBR;
nStatus = 1;
break; //return 1;
//asm("nop;");
}
}while( !IsTimedout(nTimer) );
}
ClearTimeout(nTimer);
return nStatus;
}
//////////////////////////////////////////////////////////////////////////////
// int TEST_UART(void)
//
// PURPOSE: Test the UART
//////////////////////////////////////////////////////////////////////////////
int TEST_UART(void)
{
int n;
char cTxChar;
char cRxChar;
Init_UART();
for(n = 0; n < MAX_TEST_CHARS; n++)
{
cTxChar = (n & 0xFF);
if( 0 == PutChar(cTxChar) )
{
return 0;
}
if( 0 == GetChar( &cRxChar ) )
{
return 0;
}
if( cTxChar != cRxChar )
{
return 0;
}
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -