⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart_test.c

📁 ADSP 地层驱动
💻 C
字号:
/*****************************************************************************
**																			**
**	 Name: 	UART_test.c													    **	
**																			**
******************************************************************************

(C) Copyright 2006 - Analog Devices, Inc.  All rights reserved.

This software is proprietary and confidential.  By using this software you agree
to the terms of the associated Analog Devices License Agreement.  

Purpose:	Perform a POST UART test on the BF533 EZ-Kit Lite	

                                                                               

******************************************************************************/   


#include <cdefBF533.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);




//////////////////////////////////////////////////////////////////////////////
// int TEST_UART(void)
// 
// PURPOSE:		Test the UART
//////////////////////////////////////////////////////////////////////////////
void Init_UART(void)
{
	volatile int temp;

/*****************************************************************************
 *
 *  First of all, enable UART clock. 
 *
 ****************************************************************************/ 
	*pUART_GCTL = UCEN;
	
/*****************************************************************************
 *
 *  Read period value and apply formula:  DL = PERIOD / 16 / 8
 *  Write result to the two 8-bit DL registers (DLH:DLL).
 *
 ****************************************************************************/ 
	*pUART_LCR = DLAB;
	*pUART_DLL = BAUD_RATE_115200;
	*pUART_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.
 *
 ****************************************************************************/ 
	*pUART_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 = *pUART_RBR;
	temp = *pUART_LSR;
	temp = *pUART_IIR;

	*pUART_IER = ETBEI;
}


int PutChar(const char cVal)
{
	int nStatus = 0;
	unsigned int nTimer = SetTimeout(1000);
	if( ((unsigned int)-1) != nTimer )
	{	
		do{ 
			if( (*pUART_LSR & THRE) )
			{
				*pUART_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 == (*pUART_LSR & DR) )
			{
				*cVal = *pUART_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_Timers();
	Init_Timer_Interrupts();
	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;
}


//////////////////////////////////////////////////////////////////////////////
// 
// 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_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -