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

📄 uart_test.c

📁 ADI Blackfin BF51X 範例程式
💻 C
字号:
/*******************************************************************
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.

Project Name:  	Power_On_Self_Test

Hardware:		ADSP-BF518F EZ-Board

Description:	This file tests the UART0 interface on the EZ-Board.
*******************************************************************/

/*******************************************************************
*  include files
*******************************************************************/
#include <cdefBF518.h>
#include <ccblkfn.h>
#include "Timer_ISR.h"

/*******************************************************************
*  global variables and 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);


/*******************************************************************
*   Function:    Init_UART
*   Description: Initialize UART with the appropriate values
*******************************************************************/

void Init_UART(void)
{
	volatile int temp;

	*pPORTG_FER = 0x0600;
    ssync();

	/* configure UART0 RX and UART0 TX pins */
	*pPORTG_MUX = 0x400;
    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;

	*pUART0_IER = ETBEI ;
}


/*******************************************************************
*   Function:    PutChar
*   Description: Writes a character to the UART.
*******************************************************************/

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;
}


/*******************************************************************
*   Function:    GetChar
*   Description: Reads a character from the UART.
*******************************************************************/

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;
}

/*******************************************************************
*   Function:    TEST_UART
*   Description: Performs a test by writing characters to and then
					reading characters from the UART and comparing them.
					The EZ-KIT uses a loopback switch so that the data
					we send out will come back.
*******************************************************************/

int TEST_UART(void)
{
	int n;
	char cTxChar;
	char cRxChar;

	Init_UART();

	for(n = 0; n < MAX_TEST_CHARS; n++)
	{
		cTxChar = (n & 0xFF);

		/* write a char */
		if( 0 == PutChar(cTxChar) )
		{
			return 0;
		}

		/* read a char */
		if( 0 == GetChar( &cRxChar ) )
		{
			return 0;
		}

		/* it should match */
		if( cTxChar != cRxChar )
		{
			return 0;
		}
	}

	return 1;
}



⌨️ 快捷键说明

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