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

📄 uart_test.c

📁 基于visual dsp++开发环境
💻 C
字号:
/*****************************************************************************
**																			**
**	 Name: 	UART_test.c															**
**																			**
******************************************************************************

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

Project Name:	BF538F POST ATE

Date Modified:	01 Sept 2006

Software:		VisualDSP++ 4.5

Hardware:		ADSP-BF538F EZ-KIT Lite

Purpose:		Loopback test for UART

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

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

/*****************************************************************************
 *
 *  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;
			}
		}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;
			}
		}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 + -