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

📄 uart.c

📁 DM642 DSP串口中断测试程序
💻 C
字号:

/*
 * Copyright (C) 2005 Zhongxon Incorporated
 * All Rights Reserved
 */
/*
 *---------uart.c---------*/
#include <stdio.h>
//#include <csl.h>
#include "uart.h"

//#pragma DATA_SECTION(commbuf, ".ext_buf");
//#pragma DATA_ALIGN(commbuf,128);
//Uint8 	commbuf[1024*1024];

//volatile Uint32 commsta;
//Uint32  commbyte;
//extern volatile Uint32 ms10;
#define Uint8*  *(unsigned char *)
#define Uint8  int
#define Uint32 int
void set_baud(Uint8 dlh,Uint8 dll);
void set_xon_xoff(Uint8 xon1,Uint8 xoff1,Uint8 xon2,Uint8 xoff2);
void set_efr(Uint8 efr);
void set_tcr(Uint8 tcr);
void set_tlr(Uint8 tlr);
Uint8 get_frr();
void set_mcr(Uint8 val);
void set_fcr(Uint8 val);
void set_ier(Uint8 val);
void set_lcr(Uint8 val);

Uint32 commReadSta();
Uint32 commRead(char *pbuf,Uint32 nByte);
void commWrite(char *pbuf,Uint32 nByte);

volatile Uint8 temp=0;
void wait(int nWait);
char cString[17]={ "Hello PC!,Over|" },cReceive,cBuffer[17],cAnswer[16]={"Oh,you say"};
int bReceive,nLen;

/*****************************************************************************/
void commConfig()
{
	set_baud(0,13);//57.6Khz
	set_efr(0xd0);
	set_fcr(0x0f);
	set_mcr(0x00);
	set_tcr(0x1f);
	set_tlr(0xf8);
//	set_xon_xoff(0x55,0xaa,0x5a,0xa5);
	set_xon_xoff(0,0,0,0);
	set_lcr(0x03);
	set_ier(0x00);
}


main()
{
    commConfig();
    commRead(cBuffer,8);
    commWrite(cString,8);
	

} 

/*****************************************************************************/
Uint32 commReadSta()
{
	volatile  Uint8 *rdy;
	rdy=(Uint8 *)   UART_READY_ADDER;
	return(*rdy & 0x02);
}


/*****************************************************************************/
Uint32 commRead(char *pbuf,Uint32 nByte)
{
	Uint32 i;
	volatile  Uint8 *adder,*rdy;
	rdy=(Uint8 *)   UART_READY_ADDER;
	adder=(Uint8 *) UARTA_RHR_ADDER;
	for(i=0;i<nByte;i++)
	{

		while(*rdy & 0x02);

		pbuf[i]=*adder;
		
	}
	return i;
}

/*****************************************************************************/
void commWrite(char *pbuf,Uint32 nByte)
{
	Uint32 i;
	volatile  Uint8 *adder,*rdy;
	adder=(Uint8 *) UARTA_THR_ADDER;
	rdy=(Uint8 *) UART_READY_ADDER;
	
	for(i=0;i<nByte;i++)
	{
		//while(*rdy & 0x01);
		*adder=pbuf[i];
	}
}

/*set lcr*********************************************************************/
void set_lcr(Uint8 val)
{
	volatile  Uint8 *adder;
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=val;
	
}

/*set baud rate **************************************************************/
void set_baud(Uint8 dlh,Uint8 dll)
{
	volatile  Uint8 *adder;
	volatile  Uint8 temp1 ;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	temp1=*adder;
	*adder=0x80;
	
	
	adder=(Uint8 *) UARTA_DLL_ADDER;
	*adder=dll;
	
	
	adder=(Uint8 *) UARTA_DLH_ADDER;
	*adder=dlh;
	
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=temp1;
	
}

/*set xon1 and xoff1 and xon2 and xoff2*************************************/
void set_xon_xoff(Uint8 xon1,Uint8 xoff1,Uint8 xon2,Uint8 xoff2)
{
	volatile  Uint8 *adder;
	volatile  Uint8 temp1 ;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	temp1=*adder;
	*adder=0xbf;
	
	adder=(Uint8 *) UARTA_XON1_ADDER;
	*adder=xon1;
	temp=*adder;
	
	
	adder=(Uint8 *) UARTA_XOFF1_ADDER;
	*adder=xoff1;
	temp=*adder;
	
	adder=(Uint8 *) UARTA_XON2_ADDER;
	*adder=xon2;
	temp=*adder;
	
	adder=(Uint8 *) UARTA_XOFF2_ADDER;
	*adder=xoff2;
	temp=*adder;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=temp1;
}

/*set software flow control	-->EFR*****************************************/
void set_efr(Uint8 efr)
{
	volatile  Uint8 *adder;
	volatile  Uint8 temp1 ;

	adder=(Uint8 *) UARTA_LCR_ADDER;
	temp1=*adder;
	*adder=0xbf;
	
	adder=(Uint8 *) UARTA_EFR_ADDER;
	*adder=efr;

	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=temp1;

}


/*set flow control	threshold--->TCR**************************************/
void set_tcr(Uint8 tcr)
{

	volatile  Uint8 *adder;
	volatile  Uint8 temp1 ,temp2,temp3;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	temp1=*adder;
	*adder=0xbf;

	adder=(Uint8 *) UARTA_EFR_ADDER;
	temp2=*adder;
	*adder = temp2 | 0x10;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=0;
	
	adder=(Uint8 *) UARTA_MCR_ADDER;
	temp3=*adder;
	*adder=temp3 | 0x40;
	
	adder=(Uint8 *) UARTA_TCR_ADDER;
	*adder=tcr;
	
	adder=(Uint8 *) UARTA_MCR_ADDER;
	*adder=temp3;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=0xbf;
	
	adder=(Uint8 *) UARTA_EFR_ADDER;
	*adder = temp2 ;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder = temp1 ;
}


/*set xmt and rcv FIFO threshold---->TLR*************************************/
void set_tlr(Uint8 tlr)
{
	
	volatile  Uint8 *adder;
	volatile  Uint8 temp1 ,temp2,temp3;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	temp1=*adder,
	*adder=0xbf;
	
	adder=(Uint8 *) UARTA_EFR_ADDER;
	temp2=*adder;
	*adder = temp2 | 0x10;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=0;
	
	adder=(Uint8 *) UARTA_MCR_ADDER;
	temp3=*adder;
	*adder=temp3 | 0x40;
	
	adder=(Uint8 *) UARTA_TLR_ADDER;
	*adder=tlr;
	
	adder=(Uint8 *) UARTA_MCR_ADDER;
	*adder=temp3;								
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=0xbf;
	
	adder=(Uint8 *) UARTA_EFR_ADDER;
	*adder = temp2 ;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder = temp1 ;
}

/*read FIFO ready****************************************************/
Uint8 get_frr()
{
	volatile  Uint8 *adder;
	volatile  Uint8 temp1 ,temp2,temp3;
	
	adder=(Uint8 *) UARTA_MCR_ADDER;
	temp1=*adder;
	*adder=(temp1 & 0xef) | 0x40;
	
	adder=(Uint8 *) UARTA_FRR_ADDER;
	temp2=*adder;
	
	adder=(Uint8 *) UARTA_MCR_ADDER;
	*adder=temp1;
	
	return temp2;
}


/*set MCR*******************************************************************/
void set_mcr(Uint8 val)
{
	volatile  Uint8 *adder;
	volatile  Uint8 temp1 ,temp2;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	temp1=*adder;
	*adder=0xbf;
	
	adder=(Uint8 *) UARTA_EFR_ADDER;
	temp2=*adder;
	*adder=temp2 | 0x10;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=0;
	
	adder=(Uint8 *) UARTA_MCR_ADDER;
	*adder=val;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=0xbf;
	
	adder=(Uint8 *) UARTA_EFR_ADDER;
	*adder = temp2 ;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder = temp1 ;
}


/*set IER*******************************************************************/
void set_ier(Uint8 val)
{
	volatile  Uint8 *adder;
	volatile  Uint8 temp1 ,temp2;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	temp1=*adder;
	*adder=0xbf;
	
	adder=(Uint8 *) UARTA_EFR_ADDER;
	temp2=*adder;
	*adder=temp2 | 0x10;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=0;
	
	adder=(Uint8 *) UARTA_IER_ADDER;
	*adder=val;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=0xbf;
	
	adder=(Uint8 *) UARTA_EFR_ADDER;
	*adder = temp2 ;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder = temp1 ;
}


/*set MCR*******************************************************************/
void set_fcr(Uint8 val)
{
	volatile  Uint8 *adder;
	volatile  Uint8 temp1 ,temp2;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	temp1=*adder;
	*adder=0xbf;
	
	adder=(Uint8 *) UARTA_EFR_ADDER;
	temp2=*adder;
	*adder=temp2 | 0x10;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=0;
	
	adder=(Uint8 *) UARTA_FCR_ADDER;
	*adder=val;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder=0xbf;
	
	adder=(Uint8 *) UARTA_EFR_ADDER;
	*adder = temp2 ;
	
	adder=(Uint8 *) UARTA_LCR_ADDER;
	*adder = temp1 ;
}



void wait(int nWait)
{
	int i,j,k=0;
	for ( i=0;i<nWait;i++ )
		for ( j=0;j<64;j++ )
			k++;
}




⌨️ 快捷键说明

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